Removed a few dependencies on kernel32 functions.
[wine] / dlls / winaspi / aspi.c
1 /**************************************************************************
2 ASPI routines
3 (C) 2000 David Elliott <dfe@infinite-internet.net>
4 Licensed under the WINE (X11) license
5 */
6
7 /* These routines are to be called from either WNASPI32 or WINASPI */
8
9 /* FIXME:
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.
17  */
18
19 /*
20 Registry format is currently:
21 HKEY_DYN_DATA
22         WineScsi
23                 (default)=number of host adapters
24                 hHHcCCtTTdDD=linux device name
25 */
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <fcntl.h>
31 #include <dirent.h>
32 #include <unistd.h>
33 #include <errno.h>
34
35 #include "debugtools.h"
36 #include "winreg.h"
37 #include "winerror.h"
38 #include "winescsi.h"
39 #include "file.h"
40
41 DEFAULT_DEBUG_CHANNEL(aspi);
42
43 /* Internal function prototypes */
44 static void
45 SCSI_GetProcinfo();
46
47 #ifdef linux
48 static void
49 SCSI_MapHCtoController();
50 #endif
51
52 /* Exported functions */
53 void
54 SCSI_Init()
55 {
56         /* For now we just call SCSI_GetProcinfo */
57         SCSI_GetProcinfo();
58 #ifdef linux
59         SCSI_MapHCtoController();
60 #endif
61 }
62
63 int
64 ASPI_GetNumControllers()
65 {
66         HKEY hkeyScsi;
67         HKEY hkeyControllerMap;
68         DWORD error;
69         DWORD type = REG_DWORD;
70         DWORD num_ha = 0;
71         DWORD cbData = sizeof(num_ha);
72
73         if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
74         {
75                 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
76                 return 0;
77         }
78
79         if( (error=RegOpenKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, KEY_ALL_ACCESS, &hkeyControllerMap )) != ERROR_SUCCESS )
80         {
81                 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
82                 RegCloseKey(hkeyScsi);
83                 SetLastError(error);
84                 return 0;
85         }
86         if( RegQueryValueExA(hkeyControllerMap, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
87         {
88                 ERR("Could not query value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
89                 num_ha=0;
90         }
91         RegCloseKey(hkeyControllerMap);
92         RegCloseKey(hkeyScsi);
93         TRACE("Returning %ld host adapters\n", num_ha );
94         return num_ha;
95 }
96
97 BOOL
98 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
99 {
100
101         char idstr[20];
102         HKEY hkeyScsi;
103         DWORD type;
104
105         if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
106         {
107                 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
108                 return FALSE;
109         }
110
111
112         sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
113
114         if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
115         {
116                 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
117                 RegCloseKey(hkeyScsi);
118                 return FALSE;
119         }
120         RegCloseKey(hkeyScsi);
121
122         TRACE("scsi %s: Device name: %s\n",idstr,devstr);
123         return TRUE;
124 }
125
126 /* SCSI_GetHCforController
127  * RETURNS
128  *      HIWORD: Host Adapter
129  *      LOWORD: Channel
130  */
131 DWORD
132 ASPI_GetHCforController( int controller )
133 {
134         DWORD hc = 0xFFFFFFFF;
135         char cstr[20];
136         DWORD error;
137         HKEY hkeyScsi;
138         HKEY hkeyControllerMap;
139         DWORD type = REG_DWORD;
140         DWORD cbData = sizeof(DWORD);
141         DWORD disposition;
142 #if 0
143         FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
144         hc = (controller << 16);
145         return hc;
146 #endif
147         if( (error=RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition )) != ERROR_SUCCESS )
148         {
149                 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
150                 SetLastError(error);
151                 return hc;
152         }
153         if( disposition != REG_OPENED_EXISTING_KEY )
154         {
155                 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
156         }
157         if( (error=RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition )) != ERROR_SUCCESS )
158         {
159                 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
160                 RegCloseKey(hkeyScsi);
161                 SetLastError(error);
162                 return hc;
163         }
164         if( disposition != REG_OPENED_EXISTING_KEY )
165         {
166                 WARN("Created HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
167         }
168
169         sprintf(cstr, "c%02d", controller);
170         if( (error=RegQueryValueExA( hkeyControllerMap, cstr, 0, &type, (LPBYTE)&hc, &cbData)) != ERROR_SUCCESS )
171         {
172                 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\\%s, error=%lx\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr, error );
173                 SetLastError( error );
174         }
175         RegCloseKey(hkeyControllerMap);
176         RegCloseKey(hkeyScsi);
177         return hc;
178
179 };
180
181 int
182 SCSI_OpenDevice( int h, int c, int t, int d )
183 {
184         char devstr[20];
185         DWORD cbData = 20;
186         int fd = -1;
187         char dainbread_linux_hack = 1;
188
189         if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
190         {
191                 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
192                 return -1;
193         }
194
195 linux_hack:
196         TRACE("Opening device %s mode O_RDWR\n",devstr);
197         fd = open(devstr, O_RDWR);
198
199         if( fd < 0 )
200         {
201                 int len = strlen(devstr);
202                 FILE_SetDosError(); /* SetLastError() to errno */
203                 TRACE("Open failed (%s)\n", strerror(errno));
204
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) &&
213                      (len >= 3) &&
214                      (devstr[len-3] == 's') && (devstr[len-2] == 'g') )
215                 {
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);
220                         goto linux_hack;
221                 }
222         }
223         return fd;
224 }
225
226 #ifdef linux
227 /* SCSI_Fix_CMD_LEN
228  *      Checks to make sure the CMD_LEN is correct
229  */
230 void
231 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
232 {
233         int index=(cmd>>5)&7;
234
235         if (len!=scsi_command_size[index])
236         {
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);
240         }
241 }
242
243 int
244 SCSI_LinuxSetTimeout( int fd, int timeout )
245 {
246         int retval;
247         TRACE("Setting timeout to %d jiffies\n", timeout);
248         retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
249         if(retval)
250         {
251                 WARN("Could not set timeout ! (%s)\n", strerror(errno));
252         }
253         return retval;
254         
255 }
256
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
262  * lpcbBytesReturned.
263  */
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 )
269 {
270         DWORD dwBytes;
271         DWORD save_error;
272
273         TRACE("Writing to Linux sg device\n");
274         dwBytes = write( fd, lpInBuffer, cbInBuffer );
275         if( dwBytes != cbInBuffer )
276         {
277                 FILE_SetDosError();
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;
284                 return FALSE;
285         }
286         
287         TRACE("Reading reply from Linux sg device\n");
288         *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
289         if( *lpcbBytesReturned != cbOutBuffer )
290         {
291                 FILE_SetDosError();
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 );
295                 return FALSE;
296         }
297         return TRUE;
298 }
299
300 /* Internal functions */
301 struct LinuxProcScsiDevice
302 {
303         int host;
304         int channel;
305         int target;
306         int lun;
307         char vendor[9];
308         char model[17];
309         char rev[5];
310         char type[33];
311         int ansirev;
312 };
313
314 static int
315 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
316 {
317         int result;
318         result = fscanf( procfile,
319                 "Host: scsi%d Channel: %d Id: %d Lun: %d\n",
320                 &dev->host,
321                 &dev->channel,
322                 &dev->target,
323                 &dev->lun );
324         if( result == EOF )
325                 return EOF;
326         if( result != 4 )
327                 return 0;
328         result = fscanf( procfile,
329                 "  Vendor: %8c Model: %16c Rev: %4c\n",
330                 dev->vendor,
331                 dev->model,
332                 dev->rev );
333         if( result != 3 )
334                 return 0;
335
336         result = fscanf( procfile,
337                 "  Type:   %32c ANSI SCSI revision: %d\n",
338                 dev->type,
339                 &dev->ansirev );
340         if( result != 2 )
341                 return 0;
342         /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
343         dev->vendor[8] = 0;
344         dev->model[16] = 0;
345         dev->rev[4] = 0;
346         dev->type[32] = 0;
347
348         return 1;
349 }
350
351 static void
352 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
353 {
354         TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
355                 dev->host,
356                 dev->channel,
357                 dev->target,
358                 dev->lun );
359         TRACE( "  Vendor: %s Model: %s Rev: %s\n",
360                 dev->vendor,
361                 dev->model,
362                 dev->rev );
363         TRACE( "  Type:   %s ANSI SCSI revision: %02d\n",
364                 dev->type,
365                 dev->ansirev );
366 }
367
368 static BOOL
369 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
370 {
371         DWORD error;
372         char cstr[20];
373         DWORD hc;
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 )
377         {
378                 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
379         }
380         return error;
381 }
382
383 static void
384 SCSI_MapHCtoController()
385 {
386         HKEY hkeyScsi;
387         HKEY hkeyControllerMap;
388         DWORD disposition;
389
390         char idstr[20];
391         DWORD cbIdStr = 20;
392         int i = 0;
393         DWORD type = 0;
394         DWORD error;
395
396         DWORD num_controller = 0;
397         int last_ha = -1;
398         int last_chan = -1;
399
400         int ha = 0;
401         int chan = 0;
402
403         if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
404         {
405                 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
406                 return;
407         }
408         if( disposition != REG_OPENED_EXISTING_KEY )
409         {
410                 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
411         }
412         if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
413         {
414                 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
415                 RegCloseKey(hkeyScsi);
416                 return;
417         }
418         
419         for( i=0; (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
420         {
421                 sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan);
422                 if( last_ha < ha )
423                 {       /* Next HA */
424                         last_ha = ha;
425                         last_chan = chan;
426                         SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
427                         num_controller++;
428                 }
429                 else if( last_ha > ha )
430                 {
431                         FIXME("Expected registry to be sorted\n");
432                 }
433                 /* last_ha == ha */
434                 else if( last_chan < chan )
435                 {
436                         last_chan = chan;
437                         SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
438                         num_controller++;
439                 }
440                 else if( last_chan > chan )
441                 {
442                         FIXME("Expected registry to be sorted\n");
443                 }
444                 /* else last_ha == ha && last_chan == chan so do nothing */
445         }
446         /* Set (default) value to number of controllers */
447         if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
448         {
449                 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
450         }
451         RegCloseKey(hkeyControllerMap);
452         RegCloseKey(hkeyScsi);
453         return;
454 }
455 #endif
456
457 int SCSI_Linux_CheckDevices(void)
458 {
459     DIR *devdir;
460     struct dirent *dent = NULL;
461
462     devdir = opendir("/dev");
463     for (dent=readdir(devdir);dent;dent=readdir(devdir))
464     {
465         if (!(strncmp(dent->d_name, "sg", 2)))
466             break;
467     }
468     closedir(devdir);
469
470     if (dent == NULL)
471     {
472         MESSAGE("WARNING: You don't have any /dev/sgX generic scsi devices ! \"man MAKEDEV\" !\n");
473         return 0;
474     }
475     return 1;
476 }
477
478 static void
479 SCSI_GetProcinfo()
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
483  */
484 {
485 #ifdef linux
486         static const char procname[] = "/proc/scsi/scsi";
487         FILE * procfile = NULL;
488
489         char read_line[40], read1[10] = "\0", read2[10] = "\0";
490         int result = 0;
491
492         struct LinuxProcScsiDevice dev;
493
494         char idstr[20];
495         char devstr[20];
496
497         int devnum=0;
498         int num_ha = 0;
499
500         HKEY hkeyScsi;
501         DWORD disposition;
502
503         /* Check whether user has generic scsi devices at all */
504         if (!(SCSI_Linux_CheckDevices()))
505             return;
506         
507         procfile = fopen( procname, "r" );
508         if( !procfile )
509         {
510                 ERR("Could not open %s\n", procname);
511                 return;
512         }
513
514         fgets(read_line, 40, procfile);
515         sscanf( read_line, "Attached %9s %9s", read1, read2);
516
517         if(strcmp(read1, "devices:"))
518         {
519                 ERR("Incorrect %s format\n", procname);
520                 return;
521         }
522
523         if(!(strcmp(read2, "none")))
524         {
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);
526                 return;
527         }
528
529         if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
530         {
531                 ERR("Could not create HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
532                 return;
533         }
534
535         /* Read info for one device */
536         while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
537         {
538                 /* Add to registry */
539
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 )
543                 {
544                         ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
545                 }
546
547                 /* Debug output */
548                 SCSI_printprocentry( &dev );
549
550                 /* FIXME: We *REALLY* need number of controllers.. not ha */
551                 /* num of hostadapters is highest ha + 1 */
552                 if( dev.host >= num_ha )
553                         num_ha = dev.host+1;
554                 devnum++;
555         } /* while(1) */
556         if( result != EOF )
557         {
558                 ERR("Sorry, incorrect %s format\n", procname);
559         }
560         fclose( procfile );
561         if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
562         {
563                 ERR("Could not set value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
564         }
565         RegCloseKey(hkeyScsi);
566         return;
567 #endif
568 }