Porting fixes.
[wine.git] / dlls / winaspi / aspi.c
blobf85d5c71ca76d63b572fb41881243ac82faf04e5
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 <sys/types.h>
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
46 #endif
47 #include <fcntl.h>
48 #include <dirent.h>
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
52 #include <errno.h>
53 #include <string.h>
55 #include "wine/debug.h"
56 #include "winreg.h"
57 #include "winerror.h"
58 #include "winescsi.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
62 /* Internal function prototypes */
63 static void
64 SCSI_GetProcinfo();
66 #ifdef linux
67 static void
68 SCSI_MapHCtoController();
69 #endif
71 static void set_last_error(void)
73 int save_errno = errno; /* errno gets overwritten by printf */
74 switch (errno)
76 case EAGAIN:
77 SetLastError( ERROR_SHARING_VIOLATION );
78 break;
79 case EBADF:
80 SetLastError( ERROR_INVALID_HANDLE );
81 break;
82 case ENOSPC:
83 SetLastError( ERROR_HANDLE_DISK_FULL );
84 break;
85 case EACCES:
86 case EPERM:
87 case EROFS:
88 SetLastError( ERROR_ACCESS_DENIED );
89 break;
90 case EBUSY:
91 SetLastError( ERROR_LOCK_VIOLATION );
92 break;
93 case ENOENT:
94 SetLastError( ERROR_FILE_NOT_FOUND );
95 break;
96 case EISDIR:
97 SetLastError( ERROR_CANNOT_MAKE );
98 break;
99 case ENFILE:
100 case EMFILE:
101 SetLastError( ERROR_NO_MORE_FILES );
102 break;
103 case EEXIST:
104 SetLastError( ERROR_FILE_EXISTS );
105 break;
106 case EINVAL:
107 case ESPIPE:
108 SetLastError( ERROR_SEEK );
109 break;
110 case ENOTEMPTY:
111 SetLastError( ERROR_DIR_NOT_EMPTY );
112 break;
113 case ENOEXEC:
114 SetLastError( ERROR_BAD_FORMAT );
115 break;
116 default:
117 WARN( "unknown file error: %s\n", strerror(save_errno) );
118 SetLastError( ERROR_GEN_FAILURE );
119 break;
121 errno = save_errno;
124 /* Exported functions */
125 void
126 SCSI_Init()
128 /* For now we just call SCSI_GetProcinfo */
129 SCSI_GetProcinfo();
130 #ifdef linux
131 SCSI_MapHCtoController();
132 #endif
136 ASPI_GetNumControllers()
138 HKEY hkeyScsi;
139 HKEY hkeyControllerMap;
140 DWORD error;
141 DWORD type = REG_DWORD;
142 DWORD num_ha = 0;
143 DWORD cbData = sizeof(num_ha);
145 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
147 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
148 return 0;
151 if( (error=RegOpenKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, KEY_ALL_ACCESS, &hkeyControllerMap )) != ERROR_SUCCESS )
153 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
154 RegCloseKey(hkeyScsi);
155 SetLastError(error);
156 return 0;
158 if( RegQueryValueExA(hkeyControllerMap, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
160 ERR("Could not query value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
161 num_ha=0;
163 RegCloseKey(hkeyControllerMap);
164 RegCloseKey(hkeyScsi);
165 TRACE("Returning %ld host adapters\n", num_ha );
166 return num_ha;
169 BOOL
170 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
173 char idstr[20];
174 HKEY hkeyScsi;
175 DWORD type;
177 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
179 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
180 return FALSE;
184 sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
186 if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
188 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
189 RegCloseKey(hkeyScsi);
190 return FALSE;
192 RegCloseKey(hkeyScsi);
194 TRACE("scsi %s: Device name: %s\n",idstr,devstr);
195 return TRUE;
198 /* SCSI_GetHCforController
199 * RETURNS
200 * HIWORD: Host Adapter
201 * LOWORD: Channel
203 DWORD
204 ASPI_GetHCforController( int controller )
206 DWORD hc = 0xFFFFFFFF;
207 char cstr[20];
208 DWORD error;
209 HKEY hkeyScsi;
210 HKEY hkeyControllerMap;
211 DWORD type = REG_DWORD;
212 DWORD cbData = sizeof(DWORD);
213 DWORD disposition;
214 #if 0
215 FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
216 hc = (controller << 16);
217 return hc;
218 #endif
219 if( (error=RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition )) != ERROR_SUCCESS )
221 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
222 SetLastError(error);
223 return hc;
225 if( disposition != REG_OPENED_EXISTING_KEY )
227 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
229 if( (error=RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition )) != ERROR_SUCCESS )
231 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
232 RegCloseKey(hkeyScsi);
233 SetLastError(error);
234 return hc;
236 if( disposition != REG_OPENED_EXISTING_KEY )
238 WARN("Created HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
241 sprintf(cstr, "c%02d", controller);
242 if( (error=RegQueryValueExA( hkeyControllerMap, cstr, 0, &type, (LPBYTE)&hc, &cbData)) != ERROR_SUCCESS )
244 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\\%s, error=%lx\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr, error );
245 SetLastError( error );
247 RegCloseKey(hkeyControllerMap);
248 RegCloseKey(hkeyScsi);
249 return hc;
254 SCSI_OpenDevice( int h, int c, int t, int d )
256 char devstr[20];
257 DWORD cbData = 20;
258 int fd = -1;
259 char dainbread_linux_hack = 1;
261 if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
263 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
264 return -1;
267 linux_hack:
268 TRACE("Opening device %s mode O_RDWR\n",devstr);
269 fd = open(devstr, O_RDWR);
271 if( fd < 0 )
273 int len = strlen(devstr);
274 set_last_error(); /* SetLastError() to errno */
275 TRACE("Open failed (%s)\n", strerror(errno));
277 /* in case of "/dev/sgX", convert from sga to sg0
278 * and the other way around.
279 * FIXME: remove it if the distributions
280 * finally manage to agree on something.
281 * The best would probably be a complete
282 * rewrite of the Linux SCSI layer
283 * to use CAM + devfs :-) */
284 if ( (dainbread_linux_hack) &&
285 (len >= 3) &&
286 (devstr[len-3] == 's') && (devstr[len-2] == 'g') )
288 char *p = &devstr[len-1];
289 *p = (*p >= 'a') ? *p - 'a' + '0' : *p - '0' + 'a';
290 dainbread_linux_hack = 0;
291 TRACE("Retry with \"equivalent\" Linux device '%s'\n", devstr);
292 goto linux_hack;
295 return fd;
298 #ifdef linux
299 /* SCSI_Fix_CMD_LEN
300 * Checks to make sure the CMD_LEN is correct
302 void
303 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
305 int index=(cmd>>5)&7;
307 if (len!=scsi_command_size[index])
309 TRACE("CDBLen for command %d claims to be %d, expected %d\n",
310 cmd, len, scsi_command_size[index]);
311 ioctl(fd,SG_NEXT_CMD_LEN,&len);
316 SCSI_LinuxSetTimeout( int fd, int timeout )
318 int retval;
319 TRACE("Setting timeout to %d jiffies\n", timeout);
320 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
321 if(retval)
323 WARN("Could not set timeout ! (%s)\n", strerror(errno));
325 return retval;
329 /* This function takes care of the write/read to the linux sg device.
330 * It returns TRUE or FALSE and uses set_last_error() to convert
331 * UNIX errno to Windows GetLastError(). The reason for that is that
332 * several programs will check that error and we might as well set
333 * it here. We also return the value of the read call in
334 * lpcbBytesReturned.
336 BOOL /* NOTE: This function SHOULD BLOCK */
337 SCSI_LinuxDeviceIo( int fd,
338 struct sg_header * lpInBuffer, DWORD cbInBuffer,
339 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
340 LPDWORD lpcbBytesReturned )
342 DWORD dwBytes;
343 DWORD save_error;
345 TRACE("Writing to Linux sg device\n");
346 dwBytes = write( fd, lpInBuffer, cbInBuffer );
347 if( dwBytes != cbInBuffer )
349 set_last_error();
350 save_error = GetLastError();
351 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
352 /* FIXME: set_last_error() never sets error to ERROR_NOT_ENOUGH_MEMORY... */
353 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
354 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)");
355 WARN("error= %ld\n", save_error );
356 *lpcbBytesReturned = 0;
357 return FALSE;
360 TRACE("Reading reply from Linux sg device\n");
361 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
362 if( *lpcbBytesReturned != cbOutBuffer )
364 set_last_error();
365 save_error = GetLastError();
366 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
367 WARN("error= %ld\n", save_error );
368 return FALSE;
370 return TRUE;
373 /* Internal functions */
374 struct LinuxProcScsiDevice
376 int host;
377 int channel;
378 int target;
379 int lun;
380 char vendor[9];
381 char model[17];
382 char rev[5];
383 char type[33];
384 int ansirev;
388 * we need to declare white spaces explicitly via %*1[ ],
389 * as there are very dainbread CD-ROM devices out there
390 * which have their manufacturer name blanked out via spaces,
391 * which confuses fscanf's parsing (skips all blank spaces)
393 static int
394 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
396 int result;
397 result = fscanf( procfile,
398 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
399 &dev->host,
400 &dev->channel,
401 &dev->target,
402 &dev->lun );
403 if( result == EOF )
405 /* "end of entries" return, so no TRACE() here */
406 return EOF;
408 if( result != 4 )
410 ERR("bus id line scan count error\n");
411 return 0;
413 result = fscanf( procfile,
414 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
415 dev->vendor,
416 dev->model,
417 dev->rev );
418 if( result != 3 )
420 ERR("model line scan count error\n");
421 return 0;
424 result = fscanf( procfile,
425 " Type:%*3[ ]%32c%*1[ ]ANSI%*1[ ]SCSI%*1[ ]revision:%*1[ ]%d\n",
426 dev->type,
427 &dev->ansirev );
428 if( result != 2 )
430 ERR("SCSI type line scan count error\n");
431 return 0;
433 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
434 dev->vendor[8] = 0;
435 dev->model[16] = 0;
436 dev->rev[4] = 0;
437 dev->type[32] = 0;
439 return 1;
442 static void
443 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
445 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
446 dev->host,
447 dev->channel,
448 dev->target,
449 dev->lun );
450 TRACE( " Vendor: %s Model: %s Rev: %s\n",
451 dev->vendor,
452 dev->model,
453 dev->rev );
454 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
455 dev->type,
456 dev->ansirev );
459 static BOOL
460 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
462 DWORD error;
463 char cstr[20];
464 DWORD hc;
465 hc = (ha << 16) + chan;
466 sprintf(cstr, "c%02d", num_controller);
467 if( (error=RegSetValueExA( hkeyControllerMap, cstr, 0, REG_DWORD, (LPBYTE)&hc, sizeof(DWORD))) != ERROR_SUCCESS )
469 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
471 return error;
474 static void
475 SCSI_MapHCtoController()
477 HKEY hkeyScsi;
478 HKEY hkeyControllerMap;
479 DWORD disposition;
481 char idstr[20];
482 DWORD cbIdStr;
483 int i = 0;
484 DWORD type = 0;
485 DWORD error;
487 DWORD num_controller = 0;
488 int last_ha = -1;
489 int last_chan = -1;
491 int ha = 0;
492 int chan = 0;
494 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
496 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
497 return;
499 if( disposition != REG_OPENED_EXISTING_KEY )
501 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
503 if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
505 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
506 RegCloseKey(hkeyScsi);
507 return;
510 for( i=0; cbIdStr = sizeof(idstr), (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
512 if(idstr[0] == '\0') continue; /* skip the default value */
514 if(sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan) != 2) {
515 ERR("incorrect reg. value %s\n", debugstr_a(idstr));
516 continue;
519 if( last_ha < ha )
520 { /* Next HA */
521 last_ha = ha;
522 last_chan = chan;
523 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
524 num_controller++;
526 else if( last_ha > ha )
528 FIXME("Expected registry to be sorted\n");
530 /* last_ha == ha */
531 else if( last_chan < chan )
533 last_chan = chan;
534 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
535 num_controller++;
537 else if( last_chan > chan )
539 FIXME("Expected registry to be sorted\n");
541 /* else last_ha == ha && last_chan == chan so do nothing */
543 /* Set (default) value to number of controllers */
544 if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
546 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
548 RegCloseKey(hkeyControllerMap);
549 RegCloseKey(hkeyScsi);
550 return;
552 #endif
554 int SCSI_Linux_CheckDevices(void)
556 DIR *devdir;
557 struct dirent *dent = NULL;
559 devdir = opendir("/dev");
560 for (dent=readdir(devdir);dent;dent=readdir(devdir))
562 if (!(strncmp(dent->d_name, "sg", 2)))
563 break;
565 closedir(devdir);
567 if (dent == NULL)
569 TRACE("WARNING: You don't have any /dev/sgX generic scsi devices ! \"man MAKEDEV\" !\n");
570 return 0;
572 return 1;
575 static void
576 SCSI_GetProcinfo()
577 /* I'll admit, this function is somewhat of a mess... it was originally
578 * designed to make some sort of linked list then I realized that
579 * HKEY_DYN_DATA would be a lot less messy
582 #ifdef linux
583 static const char procname[] = "/proc/scsi/scsi";
584 FILE * procfile = NULL;
586 char read_line[40], read1[10] = "\0", read2[10] = "\0";
587 int result = 0;
589 struct LinuxProcScsiDevice dev;
591 char idstr[20];
592 char devstr[20];
594 int devnum=0;
595 int num_ha = 0;
597 HKEY hkeyScsi;
598 DWORD disposition;
600 /* Check whether user has generic scsi devices at all */
601 if (!(SCSI_Linux_CheckDevices()))
602 return;
604 procfile = fopen( procname, "r" );
605 if( !procfile )
607 ERR("Could not open %s\n", procname);
608 return;
611 fgets(read_line, 40, procfile);
612 sscanf( read_line, "Attached %9s %9s", read1, read2);
614 if(strcmp(read1, "devices:"))
616 ERR("Incorrect %s format\n", procname);
617 return;
620 if(!(strcmp(read2, "none")))
622 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);
623 return;
626 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
628 ERR("Could not create HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
629 return;
632 /* Read info for one device */
633 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
635 /* Add to registry */
637 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
638 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
639 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
641 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
644 /* Debug output */
645 SCSI_printprocentry( &dev );
647 /* FIXME: We *REALLY* need number of controllers.. not ha */
648 /* num of hostadapters is highest ha + 1 */
649 if( dev.host >= num_ha )
650 num_ha = dev.host+1;
651 devnum++;
652 } /* while(1) */
653 if( result != EOF )
655 ERR("Sorry, incorrect %s format\n", procname);
657 fclose( procfile );
658 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
660 ERR("Could not set value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
662 RegCloseKey(hkeyScsi);
663 return;
664 #endif