1 /**************************************************************************
3 (C) 2000 David Elliott <dfe@infinite-internet.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>
35 #include "debugtools.h"
41 DEFAULT_DEBUG_CHANNEL(aspi);
43 /* Internal function prototypes */
49 SCSI_MapHCtoController();
52 /* Exported functions */
56 /* For now we just call SCSI_GetProcinfo */
59 SCSI_MapHCtoController();
64 ASPI_GetNumControllers()
67 HKEY hkeyControllerMap;
69 DWORD type = REG_DWORD;
71 DWORD cbData = sizeof(num_ha);
73 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
75 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
79 if( (error=RegOpenKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, KEY_ALL_ACCESS, &hkeyControllerMap )) != ERROR_SUCCESS )
81 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
82 RegCloseKey(hkeyScsi);
86 if( RegQueryValueExA(hkeyControllerMap, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
88 ERR("Could not query value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
91 RegCloseKey(hkeyControllerMap);
92 RegCloseKey(hkeyScsi);
93 TRACE("Returning %ld host adapters\n", num_ha );
98 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
105 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
107 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
112 sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
114 if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
116 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
117 RegCloseKey(hkeyScsi);
120 RegCloseKey(hkeyScsi);
122 TRACE("scsi %s: Device name: %s\n",idstr,devstr);
126 /* SCSI_GetHCforController
128 * HIWORD: Host Adapter
132 ASPI_GetHCforController( int controller )
134 DWORD hc = 0xFFFFFFFF;
138 HKEY hkeyControllerMap;
139 DWORD type = REG_DWORD;
140 DWORD cbData = sizeof(DWORD);
143 FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
144 hc = (controller << 16);
147 if( (error=RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition )) != ERROR_SUCCESS )
149 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
153 if( disposition != REG_OPENED_EXISTING_KEY )
155 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
157 if( (error=RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition )) != ERROR_SUCCESS )
159 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
160 RegCloseKey(hkeyScsi);
164 if( disposition != REG_OPENED_EXISTING_KEY )
166 WARN("Created HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
169 sprintf(cstr, "c%02d", controller);
170 if( (error=RegQueryValueExA( hkeyControllerMap, cstr, 0, &type, (LPBYTE)&hc, &cbData)) != ERROR_SUCCESS )
172 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\\%s, error=%lx\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr, error );
173 SetLastError( error );
175 RegCloseKey(hkeyControllerMap);
176 RegCloseKey(hkeyScsi);
182 SCSI_OpenDevice( int h, int c, int t, int d )
187 char dainbread_linux_hack = 1;
189 if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
191 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
196 TRACE("Opening device %s mode O_RDWR\n",devstr);
197 fd = open(devstr, O_RDWR);
201 int len = strlen(devstr);
202 FILE_SetDosError(); /* SetLastError() to errno */
203 TRACE("Open failed (%s)\n", strerror(errno));
205 /* in case of "/dev/sgX", convert from sga to sg0
206 * and the other way around.
207 * FIXME: remove it if the distributions
208 * finally manage to agree on something.
209 * The best would probably be a complete
210 * rewrite of the Linux SCSI layer
211 * to use CAM + devfs :-) */
212 if ( (dainbread_linux_hack) &&
214 (devstr[len-3] == 's') && (devstr[len-2] == 'g') )
216 char *p = &devstr[len-1];
217 *p = (*p >= 'a') ? *p - 'a' + '0' : *p - '0' + 'a';
218 dainbread_linux_hack = 0;
219 TRACE("Retry with \"equivalent\" Linux device '%s'\n", devstr);
228 * Checks to make sure the CMD_LEN is correct
231 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
233 int index=(cmd>>5)&7;
235 if (len!=scsi_command_size[index])
237 TRACE("CDBLen for command %d claims to be %d, expected %d\n",
238 cmd, len, scsi_command_size[index]);
239 ioctl(fd,SG_NEXT_CMD_LEN,&len);
244 SCSI_LinuxSetTimeout( int fd, int timeout )
247 TRACE("Setting timeout to %d jiffies\n", timeout);
248 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
251 WARN("Could not set timeout ! (%s)\n", strerror(errno));
257 /* This function takes care of the write/read to the linux sg device.
258 * It returns TRUE or FALSE and uses FILE_SetDosError() to convert
259 * UNIX errno to Windows GetLastError(). The reason for that is that
260 * several programs will check that error and we might as well set
261 * it here. We also return the value of the read call in
264 BOOL /* NOTE: This function SHOULD BLOCK */
265 SCSI_LinuxDeviceIo( int fd,
266 struct sg_header * lpInBuffer, DWORD cbInBuffer,
267 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
268 LPDWORD lpcbBytesReturned )
273 TRACE("Writing to Linux sg device\n");
274 dwBytes = write( fd, lpInBuffer, cbInBuffer );
275 if( dwBytes != cbInBuffer )
278 save_error = GetLastError();
279 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
280 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
281 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)");
282 WARN("error= %ld\n", save_error );
283 *lpcbBytesReturned = 0;
287 TRACE("Reading reply from Linux sg device\n");
288 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
289 if( *lpcbBytesReturned != cbOutBuffer )
292 save_error = GetLastError();
293 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
294 WARN("error= %ld\n", save_error );
300 /* Internal functions */
301 struct LinuxProcScsiDevice
315 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
318 result = fscanf( procfile,
319 "Host: scsi%d Channel: %d Id: %d Lun: %d\n",
328 result = fscanf( procfile,
329 " Vendor: %8c Model: %16c Rev: %4c\n",
336 result = fscanf( procfile,
337 " Type: %32c ANSI SCSI revision: %d\n",
342 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
352 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
354 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
359 TRACE( " Vendor: %s Model: %s Rev: %s\n",
363 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
369 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
374 hc = (ha << 16) + chan;
375 sprintf(cstr, "c%02d", num_controller);
376 if( (error=RegSetValueExA( hkeyControllerMap, cstr, 0, REG_DWORD, (LPBYTE)&hc, sizeof(DWORD))) != ERROR_SUCCESS )
378 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
384 SCSI_MapHCtoController()
387 HKEY hkeyControllerMap;
396 DWORD num_controller = 0;
403 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
405 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
408 if( disposition != REG_OPENED_EXISTING_KEY )
410 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
412 if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
414 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
415 RegCloseKey(hkeyScsi);
419 for( i=0; (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
421 sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan);
426 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
429 else if( last_ha > ha )
431 FIXME("Expected registry to be sorted\n");
434 else if( last_chan < chan )
437 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
440 else if( last_chan > chan )
442 FIXME("Expected registry to be sorted\n");
444 /* else last_ha == ha && last_chan == chan so do nothing */
446 /* Set (default) value to number of controllers */
447 if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
449 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
451 RegCloseKey(hkeyControllerMap);
452 RegCloseKey(hkeyScsi);
457 int SCSI_Linux_CheckDevices(void)
460 struct dirent *dent = NULL;
462 devdir = opendir("/dev");
463 for (dent=readdir(devdir);dent;dent=readdir(devdir))
465 if (!(strncmp(dent->d_name, "sg", 2)))
472 MESSAGE("WARNING: You don't have any /dev/sgX generic scsi devices ! \"man MAKEDEV\" !\n");
480 /* I'll admit, this function is somewhat of a mess... it was originally
481 * designed to make some sort of linked list then I realized that
482 * HKEY_DYN_DATA would be a lot less messy
486 static const char procname[] = "/proc/scsi/scsi";
487 FILE * procfile = NULL;
489 char read_line[40], read1[10] = "\0", read2[10] = "\0";
492 struct LinuxProcScsiDevice dev;
503 /* Check whether user has generic scsi devices at all */
504 if (!(SCSI_Linux_CheckDevices()))
507 procfile = fopen( procname, "r" );
510 ERR("Could not open %s\n", procname);
514 fgets(read_line, 40, procfile);
515 sscanf( read_line, "Attached %9s %9s", read1, read2);
517 if(strcmp(read1, "devices:"))
519 ERR("Incorrect %s format\n", procname);
523 if(!(strcmp(read2, "none")))
525 ERR("No devices found in %s. Make sure you loaded your SCSI driver or set up ide-scsi emulation for your IDE device if this app needs it !\n", procname);
529 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
531 ERR("Could not create HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
535 /* Read info for one device */
536 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
538 /* Add to registry */
540 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
541 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
542 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
544 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
548 SCSI_printprocentry( &dev );
550 /* FIXME: We *REALLY* need number of controllers.. not ha */
551 /* num of hostadapters is highest ha + 1 */
552 if( dev.host >= num_ha )
558 ERR("Sorry, incorrect %s format\n", procname);
561 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
563 ERR("Could not set value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
565 RegCloseKey(hkeyScsi);