1 /**************************************************************************
3 (C) 2000 David Elliott <dfe@netnitco.net>
4 Licensed under the WINE (X11) license
7 /* These routines are to be called from either WNASPI32 or WINASPI */
10 * - Registry format is stupid for now.. fix that later
11 * - No way to override automatic /proc detection, maybe provide an
12 * HKEY_LOCAL_MACHINE\Software\Wine\Wine\Scsi regkey
13 * - Somewhat debating an #ifdef linux... technically all this code will
14 * run on another UNIX.. it will fail nicely.
15 * - Please add support for mapping multiple channels on host adapters to
16 * aspi controllers, e-mail me if you need help.
20 Registry format is currently:
23 (default)=number of host adapters
24 hHHcCCtTTdDD=linux device name
27 #include <sys/types.h>
29 #include <sys/ioctl.h>
34 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(aspi);
42 /* Internal function prototypes */
46 /* Exported functions */
50 /* For now we just call SCSI_GetProcinfo */
55 ASPI_GetNumControllers()
58 DWORD type = REG_DWORD;
60 DWORD cbData = sizeof(num_ha);
62 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
64 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
68 if( RegQueryValueExA(hkeyScsi, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
70 ERR("Could not query value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
73 RegCloseKey(hkeyScsi);
74 FIXME("Please fix to return number of controllers\n");
75 TRACE("Returning %ld host adapters\n", num_ha );
80 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
87 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
89 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
94 sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
96 if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
98 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
99 RegCloseKey(hkeyScsi);
102 RegCloseKey(hkeyScsi);
104 TRACE("scsi %s: Device name: %s\n",idstr,devstr);
108 /* SCSI_GetHCforController
110 * HIWORD: Host Adapter
114 ASPI_GetHCforController( int controller )
117 FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
118 retval = (controller << 16);
123 SCSI_OpenDevice( int h, int c, int t, int d )
129 if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
131 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
135 TRACE("Opening device %s mode O_RDWR\n",devstr);
136 fd = open(devstr, O_RDWR);
140 TRACE("open failed\n");
141 FILE_SetDosError(); /* SetLastError() to errno */
142 TRACE("GetLastError: %ld\n", GetLastError());
149 SCSI_LinuxSetTimeout( int fd, int timeout )
152 TRACE("Setting timeout to %d jiffies\n", timeout);
153 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
156 WARN("Could not set timeout errno=%d!\n",errno);
162 /* This function takes care of the write/read to the linux sg device.
163 * It returns TRUE or FALSE and uses FILE_SetDosError() to convert
164 * UNIX errno to Windows GetLastError(). The reason for that is that
165 * several programs will check that error and we might as well set
166 * it here. We also return the value of the read call in
169 BOOL /* NOTE: This function SHOULD BLOCK */
170 SCSI_LinuxDeviceIo( int fd,
171 struct sg_header * lpInBuffer, DWORD cbInBuffer,
172 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
173 LPDWORD lpcbBytesReturned )
178 TRACE("Writing to Liunx sg device\n");
179 dwBytes = write( fd, lpInBuffer, cbInBuffer );
180 if( dwBytes != cbInBuffer )
183 save_error = GetLastError();
184 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
185 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
186 MESSAGE("Your Linux kernel was not able to handle the amount of data sent to the scsi device. Try recompiling with a larger SG_BIG_BUFF value (kernel 2.0.x sg.h");
187 WARN("error= %ld\n", save_error );
188 *lpcbBytesReturned = 0;
192 TRACE("Reading reply from Linux sg device\n");
193 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
194 if( *lpcbBytesReturned != cbOutBuffer )
197 save_error = GetLastError();
198 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
199 WARN("error= %ld\n", save_error );
205 /* Internal functions */
206 struct LinuxProcScsiDevice
220 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
223 result = fscanf( procfile,
224 "Host: scsi%d Channel: %d Id: %d Lun: %d\n",
233 result = fscanf( procfile,
234 " Vendor: %8c Model: %16c Rev: %4c\n",
241 result = fscanf( procfile,
242 " Type: %32c ANSI SCSI revision: %d\n",
247 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
257 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
259 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
264 TRACE( " Vendor: %s Model: %s Rev: %s\n",
268 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
276 /* I'll admit, this function is somewhat of a mess... it was originally
277 * designed to make some sort of linked list then I realized that
278 * HKEY_DYN_DATA would be a lot less messy
282 FILE * procfile = NULL;
286 struct LinuxProcScsiDevice dev;
297 procfile = fopen( "/proc/scsi/scsi", "r" );
300 ERR("Could not open /proc/scsi/scsi\n");
304 result = fscanf( procfile, "Attached devices: \n");
307 ERR("Incorrect /proc/scsi/scsi format");
311 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
313 ERR("Could not create HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
317 /* Read info for one device */
318 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
320 /* Add to registry */
322 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
323 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
324 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
326 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
330 SCSI_printprocentry( &dev );
332 /* FIXME: We *REALLY* need number of controllers.. not ha */
333 /* num of hostadapters is highest ha + 1 */
334 if( dev.host >= num_ha )
340 ERR("Incorrect /proc/scsi/scsi format");
343 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
345 ERR("Could not set value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
347 RegCloseKey(hkeyScsi);