Fixed atl.rgs.
[wine/hacks.git] / dlls / winaspi / aspi.c
blobd1fb0a0b8d043065566d6bd0a1e0f8ec3060d607
1 /**************************************************************************
2 * ASPI routines
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 */
22 /* FIXME:
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:
34 HKEY_DYN_DATA
35 WineScsi
36 (default)=number of host adapters
37 hHHcCCtTTdDD=linux device name
40 #include "config.h"
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <sys/types.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #include <fcntl.h>
49 #include <dirent.h>
50 #ifdef HAVE_UNISTD_H
51 # include <unistd.h>
52 #endif
53 #include <errno.h>
54 #include <string.h>
56 #include "windef.h"
57 #include "winbase.h"
58 #include "winreg.h"
59 #include "winerror.h"
60 #include "winescsi.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
66 /* Internal function prototypes */
67 static void
68 SCSI_GetProcinfo(void);
70 #ifdef linux
71 static void
72 SCSI_MapHCtoController(void);
73 #endif
75 static void set_last_error(void)
77 int save_errno = errno; /* errno gets overwritten by printf */
78 switch (errno)
80 case EAGAIN:
81 SetLastError( ERROR_SHARING_VIOLATION );
82 break;
83 case EBADF:
84 SetLastError( ERROR_INVALID_HANDLE );
85 break;
86 case ENOSPC:
87 SetLastError( ERROR_HANDLE_DISK_FULL );
88 break;
89 case EACCES:
90 case EPERM:
91 case EROFS:
92 SetLastError( ERROR_ACCESS_DENIED );
93 break;
94 case EBUSY:
95 SetLastError( ERROR_LOCK_VIOLATION );
96 break;
97 case ENOENT:
98 SetLastError( ERROR_FILE_NOT_FOUND );
99 break;
100 case EISDIR:
101 SetLastError( ERROR_CANNOT_MAKE );
102 break;
103 case ENFILE:
104 case EMFILE:
105 SetLastError( ERROR_NO_MORE_FILES );
106 break;
107 case EEXIST:
108 SetLastError( ERROR_FILE_EXISTS );
109 break;
110 case EINVAL:
111 case ESPIPE:
112 SetLastError( ERROR_SEEK );
113 break;
114 case ENOTEMPTY:
115 SetLastError( ERROR_DIR_NOT_EMPTY );
116 break;
117 case ENOEXEC:
118 SetLastError( ERROR_BAD_FORMAT );
119 break;
120 default:
121 WARN( "unknown file error: %s\n", strerror(save_errno) );
122 SetLastError( ERROR_GEN_FAILURE );
123 break;
125 errno = save_errno;
128 /* Exported functions */
129 void
130 SCSI_Init()
132 /* For now we just call SCSI_GetProcinfo */
133 SCSI_GetProcinfo();
134 #ifdef linux
135 SCSI_MapHCtoController();
136 #endif
140 ASPI_GetNumControllers()
142 HKEY hkeyScsi;
143 HKEY hkeyControllerMap;
144 DWORD error;
145 DWORD type = REG_DWORD;
146 DWORD num_ha = 0;
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);
152 return 0;
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);
159 SetLastError(error);
160 return 0;
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);
165 num_ha=0;
167 RegCloseKey(hkeyControllerMap);
168 RegCloseKey(hkeyScsi);
169 TRACE("Returning %ld host adapters\n", num_ha );
170 return num_ha;
173 BOOL
174 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
177 char idstr[20];
178 HKEY hkeyScsi;
179 DWORD type;
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);
184 return FALSE;
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);
194 return FALSE;
196 RegCloseKey(hkeyScsi);
198 TRACE("scsi %s: Device name: %s\n",idstr,devstr);
199 return TRUE;
202 /* SCSI_GetHCforController
203 * RETURNS
204 * HIWORD: Host Adapter
205 * LOWORD: Channel
207 DWORD
208 ASPI_GetHCforController( int controller )
210 DWORD hc = 0xFFFFFFFF;
211 char cstr[20];
212 DWORD error;
213 HKEY hkeyScsi;
214 HKEY hkeyControllerMap;
215 DWORD type = REG_DWORD;
216 DWORD cbData = sizeof(DWORD);
217 DWORD disposition;
218 #if 0
219 FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
220 hc = (controller << 16);
221 return hc;
222 #endif
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);
226 SetLastError(error);
227 return hc;
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);
237 SetLastError(error);
238 return hc;
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);
253 return hc;
258 SCSI_OpenDevice( int h, int c, int t, int d )
260 char devstr[20];
261 DWORD cbData = 20;
262 int fd = -1;
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);
268 return -1;
271 linux_hack:
272 TRACE("Opening device %s mode O_RDWR\n",devstr);
273 fd = open(devstr, O_RDWR);
275 if( fd < 0 )
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) &&
289 (len >= 3) &&
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);
296 goto linux_hack;
299 return fd;
302 #ifdef linux
303 /* SCSI_Fix_CMD_LEN
304 * Checks to make sure the CMD_LEN is correct
306 void
307 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
309 /* This is what the linux kernel thinks.... */
310 static const unsigned char scsi_command_size[8] =
312 6, 10, 10, 12,
313 12, 12, 10, 10
316 int index=(cmd>>5)&7;
318 if (len!=scsi_command_size[index])
320 TRACE("CDBLen for command %d claims to be %d, expected %d\n",
321 cmd, len, scsi_command_size[index]);
322 ioctl(fd,SG_NEXT_CMD_LEN,&len);
327 SCSI_LinuxSetTimeout( int fd, int timeout )
329 int retval;
330 TRACE("Setting timeout to %d jiffies\n", timeout);
331 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
332 if(retval)
334 WARN("Could not set timeout ! (%s)\n", strerror(errno));
336 return retval;
340 /* This function takes care of the write/read to the linux sg device.
341 * It returns TRUE or FALSE and uses set_last_error() to convert
342 * UNIX errno to Windows GetLastError(). The reason for that is that
343 * several programs will check that error and we might as well set
344 * it here. We also return the value of the read call in
345 * lpcbBytesReturned.
347 BOOL /* NOTE: This function SHOULD BLOCK */
348 SCSI_LinuxDeviceIo( int fd,
349 struct sg_header * lpInBuffer, DWORD cbInBuffer,
350 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
351 LPDWORD lpcbBytesReturned )
353 DWORD dwBytes;
354 DWORD save_error;
356 TRACE("Writing to Linux sg device\n");
357 dwBytes = write( fd, lpInBuffer, cbInBuffer );
358 if( dwBytes != cbInBuffer )
360 set_last_error();
361 save_error = GetLastError();
362 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
363 /* FIXME: set_last_error() never sets error to ERROR_NOT_ENOUGH_MEMORY... */
364 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
365 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)");
366 WARN("error= %ld\n", save_error );
367 *lpcbBytesReturned = 0;
368 return FALSE;
371 TRACE("Reading reply from Linux sg device\n");
372 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
373 if( *lpcbBytesReturned != cbOutBuffer )
375 set_last_error();
376 save_error = GetLastError();
377 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
378 WARN("error= %ld\n", save_error );
379 return FALSE;
381 return TRUE;
384 /* Internal functions */
385 struct LinuxProcScsiDevice
387 int host;
388 int channel;
389 int target;
390 int lun;
391 char vendor[9];
392 char model[17];
393 char rev[5];
394 char type[33];
395 int ansirev;
399 * we need to declare white spaces explicitly via %*1[ ],
400 * as there are very dainbread CD-ROM devices out there
401 * which have their manufacturer name blanked out via spaces,
402 * which confuses fscanf's parsing (skips all blank spaces)
404 static int
405 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
407 int result;
408 result = fscanf( procfile,
409 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
410 &dev->host,
411 &dev->channel,
412 &dev->target,
413 &dev->lun );
414 if( result == EOF )
416 /* "end of entries" return, so no TRACE() here */
417 return EOF;
419 if( result != 4 )
421 ERR("bus id line scan count error\n");
422 return 0;
424 result = fscanf( procfile,
425 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
426 dev->vendor,
427 dev->model,
428 dev->rev );
429 if( result != 3 )
431 ERR("model line scan count error\n");
432 return 0;
435 result = fscanf( procfile,
436 " Type:%*3[ ]%32c%*1[ ]ANSI%*1[ ]SCSI%*1[ ]revision:%*1[ ]%d\n",
437 dev->type,
438 &dev->ansirev );
439 if( result != 2 )
441 ERR("SCSI type line scan count error\n");
442 return 0;
444 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
445 dev->vendor[8] = 0;
446 dev->model[16] = 0;
447 dev->rev[4] = 0;
448 dev->type[32] = 0;
450 return 1;
453 static void
454 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
456 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
457 dev->host,
458 dev->channel,
459 dev->target,
460 dev->lun );
461 TRACE( " Vendor: %s Model: %s Rev: %s\n",
462 dev->vendor,
463 dev->model,
464 dev->rev );
465 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
466 dev->type,
467 dev->ansirev );
470 static BOOL
471 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
473 DWORD error;
474 char cstr[20];
475 DWORD hc;
476 hc = (ha << 16) + chan;
477 sprintf(cstr, "c%02d", num_controller);
478 if( (error=RegSetValueExA( hkeyControllerMap, cstr, 0, REG_DWORD, (LPBYTE)&hc, sizeof(DWORD))) != ERROR_SUCCESS )
480 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
482 return error;
485 static void
486 SCSI_MapHCtoController()
488 HKEY hkeyScsi;
489 HKEY hkeyControllerMap;
490 DWORD disposition;
492 char idstr[20];
493 DWORD cbIdStr;
494 int i = 0;
495 DWORD type = 0;
496 DWORD error;
498 DWORD num_controller = 0;
499 int last_ha = -1;
500 int last_chan = -1;
502 int ha = 0;
503 int chan = 0;
505 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
507 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
508 return;
510 if( disposition != REG_OPENED_EXISTING_KEY )
512 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
514 if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
516 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
517 RegCloseKey(hkeyScsi);
518 return;
521 for( i=0; cbIdStr = sizeof(idstr), (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
523 if(idstr[0] == '\0') continue; /* skip the default value */
525 if(sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan) != 2) {
526 ERR("incorrect reg. value %s\n", debugstr_a(idstr));
527 continue;
530 if( last_ha < ha )
531 { /* Next HA */
532 last_ha = ha;
533 last_chan = chan;
534 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
535 num_controller++;
537 else if( last_ha > ha )
539 FIXME("Expected registry to be sorted\n");
541 /* last_ha == ha */
542 else if( last_chan < chan )
544 last_chan = chan;
545 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
546 num_controller++;
548 else if( last_chan > chan )
550 FIXME("Expected registry to be sorted\n");
552 /* else last_ha == ha && last_chan == chan so do nothing */
554 /* Set (default) value to number of controllers */
555 if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
557 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
559 RegCloseKey(hkeyControllerMap);
560 RegCloseKey(hkeyScsi);
561 return;
563 #endif
565 int SCSI_Linux_CheckDevices(void)
567 DIR *devdir;
568 struct dirent *dent = NULL;
570 devdir = opendir("/dev");
571 for (dent=readdir(devdir);dent;dent=readdir(devdir))
573 if (!(strncmp(dent->d_name, "sg", 2)))
574 break;
576 closedir(devdir);
578 if (dent == NULL)
580 TRACE("WARNING: You don't have any /dev/sgX generic scsi devices ! \"man MAKEDEV\" !\n");
581 return 0;
583 return 1;
586 static void
587 SCSI_GetProcinfo()
588 /* I'll admit, this function is somewhat of a mess... it was originally
589 * designed to make some sort of linked list then I realized that
590 * HKEY_DYN_DATA would be a lot less messy
593 #ifdef linux
594 static const char procname[] = "/proc/scsi/scsi";
595 FILE * procfile = NULL;
597 char read_line[40], read1[10] = "\0", read2[10] = "\0";
598 int result = 0;
600 struct LinuxProcScsiDevice dev;
602 char idstr[20];
603 char devstr[20];
605 int devnum=0;
606 int num_ha = 0;
608 HKEY hkeyScsi;
609 DWORD disposition;
611 /* Check whether user has generic scsi devices at all */
612 if (!(SCSI_Linux_CheckDevices()))
613 return;
615 procfile = fopen( procname, "r" );
616 if( !procfile )
618 ERR("Could not open %s\n", procname);
619 return;
622 fgets(read_line, 40, procfile);
623 sscanf( read_line, "Attached %9s %9s", read1, read2);
625 if(strcmp(read1, "devices:"))
627 ERR("Incorrect %s format\n", procname);
628 return;
631 if(!(strcmp(read2, "none")))
633 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);
634 return;
637 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
639 ERR("Could not create HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
640 return;
643 /* Read info for one device */
644 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
646 /* Add to registry */
648 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
649 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
650 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
652 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
655 /* Debug output */
656 SCSI_printprocentry( &dev );
658 /* FIXME: We *REALLY* need number of controllers.. not ha */
659 /* num of hostadapters is highest ha + 1 */
660 if( dev.host >= num_ha )
661 num_ha = dev.host+1;
662 devnum++;
663 } /* while(1) */
664 if( result != EOF )
666 ERR("Sorry, incorrect %s format\n", procname);
668 fclose( procfile );
669 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
671 ERR("Could not set value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
673 RegCloseKey(hkeyScsi);
674 return;
675 #endif