1 /**************************************************************************
3 * Copyright (C) 2000 David Elliott <dfe@infinite-internet.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* These routines are to be called from either WNASPI32 or WINASPI */
23 * - Registry format is stupid for now.. fix that later
24 * - No way to override automatic /proc detection, maybe provide an
25 * HKEY_LOCAL_MACHINE\Software\Wine\Wine\Scsi regkey
26 * - Somewhat debating an #ifdef linux... technically all this code will
27 * run on another UNIX.. it will fail nicely.
28 * - Please add support for mapping multiple channels on host adapters to
29 * aspi controllers, e-mail me if you need help.
33 Registry format is currently:
36 (default)=number of host adapters
37 hHHcCCtTTdDD=linux device name
44 #include <sys/types.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
66 /* Internal function prototypes */
72 SCSI_MapHCtoController();
75 static void set_last_error(void)
77 int save_errno = errno; /* errno gets overwritten by printf */
81 SetLastError( ERROR_SHARING_VIOLATION );
84 SetLastError( ERROR_INVALID_HANDLE );
87 SetLastError( ERROR_HANDLE_DISK_FULL );
92 SetLastError( ERROR_ACCESS_DENIED );
95 SetLastError( ERROR_LOCK_VIOLATION );
98 SetLastError( ERROR_FILE_NOT_FOUND );
101 SetLastError( ERROR_CANNOT_MAKE );
105 SetLastError( ERROR_NO_MORE_FILES );
108 SetLastError( ERROR_FILE_EXISTS );
112 SetLastError( ERROR_SEEK );
115 SetLastError( ERROR_DIR_NOT_EMPTY );
118 SetLastError( ERROR_BAD_FORMAT );
121 WARN( "unknown file error: %s\n", strerror(save_errno) );
122 SetLastError( ERROR_GEN_FAILURE );
128 /* Exported functions */
132 /* For now we just call SCSI_GetProcinfo */
135 SCSI_MapHCtoController();
140 ASPI_GetNumControllers()
143 HKEY hkeyControllerMap;
145 DWORD type = REG_DWORD;
147 DWORD cbData = sizeof(num_ha);
149 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
151 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
155 if( (error=RegOpenKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, KEY_ALL_ACCESS, &hkeyControllerMap )) != ERROR_SUCCESS )
157 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
158 RegCloseKey(hkeyScsi);
162 if( RegQueryValueExA(hkeyControllerMap, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
164 ERR("Could not query value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
167 RegCloseKey(hkeyControllerMap);
168 RegCloseKey(hkeyScsi);
169 TRACE("Returning %ld host adapters\n", num_ha );
174 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
181 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
183 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
188 sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
190 if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
192 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
193 RegCloseKey(hkeyScsi);
196 RegCloseKey(hkeyScsi);
198 TRACE("scsi %s: Device name: %s\n",idstr,devstr);
202 /* SCSI_GetHCforController
204 * HIWORD: Host Adapter
208 ASPI_GetHCforController( int controller )
210 DWORD hc = 0xFFFFFFFF;
214 HKEY hkeyControllerMap;
215 DWORD type = REG_DWORD;
216 DWORD cbData = sizeof(DWORD);
219 FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
220 hc = (controller << 16);
223 if( (error=RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition )) != ERROR_SUCCESS )
225 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
229 if( disposition != REG_OPENED_EXISTING_KEY )
231 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
233 if( (error=RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition )) != ERROR_SUCCESS )
235 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
236 RegCloseKey(hkeyScsi);
240 if( disposition != REG_OPENED_EXISTING_KEY )
242 WARN("Created HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
245 sprintf(cstr, "c%02d", controller);
246 if( (error=RegQueryValueExA( hkeyControllerMap, cstr, 0, &type, (LPBYTE)&hc, &cbData)) != ERROR_SUCCESS )
248 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\\%s, error=%lx\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr, error );
249 SetLastError( error );
251 RegCloseKey(hkeyControllerMap);
252 RegCloseKey(hkeyScsi);
258 SCSI_OpenDevice( int h, int c, int t, int d )
263 char dainbread_linux_hack = 1;
265 if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
267 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
272 TRACE("Opening device %s mode O_RDWR\n",devstr);
273 fd = open(devstr, O_RDWR);
277 int len = strlen(devstr);
278 set_last_error(); /* SetLastError() to errno */
279 TRACE("Open failed (%s)\n", strerror(errno));
281 /* in case of "/dev/sgX", convert from sga to sg0
282 * and the other way around.
283 * FIXME: remove it if the distributions
284 * finally manage to agree on something.
285 * The best would probably be a complete
286 * rewrite of the Linux SCSI layer
287 * to use CAM + devfs :-) */
288 if ( (dainbread_linux_hack) &&
290 (devstr[len-3] == 's') && (devstr[len-2] == 'g') )
292 char *p = &devstr[len-1];
293 *p = (*p >= 'a') ? *p - 'a' + '0' : *p - '0' + 'a';
294 dainbread_linux_hack = 0;
295 TRACE("Retry with \"equivalent\" Linux device '%s'\n", devstr);
304 * Checks to make sure the CMD_LEN is correct
307 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
309 int index=(cmd>>5)&7;
311 if (len!=scsi_command_size[index])
313 TRACE("CDBLen for command %d claims to be %d, expected %d\n",
314 cmd, len, scsi_command_size[index]);
315 ioctl(fd,SG_NEXT_CMD_LEN,&len);
320 SCSI_LinuxSetTimeout( int fd, int timeout )
323 TRACE("Setting timeout to %d jiffies\n", timeout);
324 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
327 WARN("Could not set timeout ! (%s)\n", strerror(errno));
333 /* This function takes care of the write/read to the linux sg device.
334 * It returns TRUE or FALSE and uses set_last_error() to convert
335 * UNIX errno to Windows GetLastError(). The reason for that is that
336 * several programs will check that error and we might as well set
337 * it here. We also return the value of the read call in
340 BOOL /* NOTE: This function SHOULD BLOCK */
341 SCSI_LinuxDeviceIo( int fd,
342 struct sg_header * lpInBuffer, DWORD cbInBuffer,
343 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
344 LPDWORD lpcbBytesReturned )
349 TRACE("Writing to Linux sg device\n");
350 dwBytes = write( fd, lpInBuffer, cbInBuffer );
351 if( dwBytes != cbInBuffer )
354 save_error = GetLastError();
355 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
356 /* FIXME: set_last_error() never sets error to ERROR_NOT_ENOUGH_MEMORY... */
357 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
358 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)");
359 WARN("error= %ld\n", save_error );
360 *lpcbBytesReturned = 0;
364 TRACE("Reading reply from Linux sg device\n");
365 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
366 if( *lpcbBytesReturned != cbOutBuffer )
369 save_error = GetLastError();
370 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
371 WARN("error= %ld\n", save_error );
377 /* Internal functions */
378 struct LinuxProcScsiDevice
392 * we need to declare white spaces explicitly via %*1[ ],
393 * as there are very dainbread CD-ROM devices out there
394 * which have their manufacturer name blanked out via spaces,
395 * which confuses fscanf's parsing (skips all blank spaces)
398 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
401 result = fscanf( procfile,
402 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
409 /* "end of entries" return, so no TRACE() here */
414 ERR("bus id line scan count error\n");
417 result = fscanf( procfile,
418 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
424 ERR("model line scan count error\n");
428 result = fscanf( procfile,
429 " Type:%*3[ ]%32c%*1[ ]ANSI%*1[ ]SCSI%*1[ ]revision:%*1[ ]%d\n",
434 ERR("SCSI type line scan count error\n");
437 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
447 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
449 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
454 TRACE( " Vendor: %s Model: %s Rev: %s\n",
458 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
464 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
469 hc = (ha << 16) + chan;
470 sprintf(cstr, "c%02d", num_controller);
471 if( (error=RegSetValueExA( hkeyControllerMap, cstr, 0, REG_DWORD, (LPBYTE)&hc, sizeof(DWORD))) != ERROR_SUCCESS )
473 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
479 SCSI_MapHCtoController()
482 HKEY hkeyControllerMap;
491 DWORD num_controller = 0;
498 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
500 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
503 if( disposition != REG_OPENED_EXISTING_KEY )
505 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
507 if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
509 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
510 RegCloseKey(hkeyScsi);
514 for( i=0; cbIdStr = sizeof(idstr), (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
516 if(idstr[0] == '\0') continue; /* skip the default value */
518 if(sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan) != 2) {
519 ERR("incorrect reg. value %s\n", debugstr_a(idstr));
527 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
530 else if( last_ha > ha )
532 FIXME("Expected registry to be sorted\n");
535 else if( last_chan < chan )
538 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
541 else if( last_chan > chan )
543 FIXME("Expected registry to be sorted\n");
545 /* else last_ha == ha && last_chan == chan so do nothing */
547 /* Set (default) value to number of controllers */
548 if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
550 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
552 RegCloseKey(hkeyControllerMap);
553 RegCloseKey(hkeyScsi);
558 int SCSI_Linux_CheckDevices(void)
561 struct dirent *dent = NULL;
563 devdir = opendir("/dev");
564 for (dent=readdir(devdir);dent;dent=readdir(devdir))
566 if (!(strncmp(dent->d_name, "sg", 2)))
573 TRACE("WARNING: You don't have any /dev/sgX generic scsi devices ! \"man MAKEDEV\" !\n");
581 /* I'll admit, this function is somewhat of a mess... it was originally
582 * designed to make some sort of linked list then I realized that
583 * HKEY_DYN_DATA would be a lot less messy
587 static const char procname[] = "/proc/scsi/scsi";
588 FILE * procfile = NULL;
590 char read_line[40], read1[10] = "\0", read2[10] = "\0";
593 struct LinuxProcScsiDevice dev;
604 /* Check whether user has generic scsi devices at all */
605 if (!(SCSI_Linux_CheckDevices()))
608 procfile = fopen( procname, "r" );
611 ERR("Could not open %s\n", procname);
615 fgets(read_line, 40, procfile);
616 sscanf( read_line, "Attached %9s %9s", read1, read2);
618 if(strcmp(read1, "devices:"))
620 ERR("Incorrect %s format\n", procname);
624 if(!(strcmp(read2, "none")))
626 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);
630 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
632 ERR("Could not create HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
636 /* Read info for one device */
637 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
639 /* Add to registry */
641 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
642 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
643 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
645 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
649 SCSI_printprocentry( &dev );
651 /* FIXME: We *REALLY* need number of controllers.. not ha */
652 /* num of hostadapters is highest ha + 1 */
653 if( dev.host >= num_ha )
659 ERR("Sorry, incorrect %s format\n", procname);
662 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
664 ERR("Could not set value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
666 RegCloseKey(hkeyScsi);