1 /**************************************************************************
3 (C) 2000 David Elliott <dfe@netnitco.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>
34 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(aspi);
42 /* Internal function prototypes */
46 /* Exported functions */
50 /* For now we just call SCSI_GetProcinfo */
55 ASPI_GetNumControllers()
58 DWORD type = REG_DWORD;
60 DWORD cbData = sizeof(num_ha);
62 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
64 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
68 if( RegQueryValueExA(hkeyScsi, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
70 ERR("Could not query value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
73 RegCloseKey(hkeyScsi);
74 FIXME("Please fix to return number of controllers\n");
75 TRACE("Returning %ld host adapters\n", num_ha );
80 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
87 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
89 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
94 sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
96 if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
98 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
99 RegCloseKey(hkeyScsi);
102 RegCloseKey(hkeyScsi);
104 TRACE("scsi %s: Device name: %s\n",idstr,devstr);
108 /* SCSI_GetHCforController
110 * HIWORD: Host Adapter
114 ASPI_GetHCforController( int controller )
117 FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
118 retval = (controller << 16);
123 SCSI_OpenDevice( int h, int c, int t, int d )
129 if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
131 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
135 TRACE("Opening device %s mode O_RDWR\n",devstr);
136 fd = open(devstr, O_RDWR);
140 TRACE("open failed\n");
141 FILE_SetDosError(); /* SetLastError() to errno */
142 TRACE("GetLastError: %ld\n", GetLastError());
148 SCSI_LinuxSetTimeout( int fd, int timeout )
151 TRACE("Setting timeout to %d jiffies\n", timeout);
152 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
155 WARN("Could not set timeout errno=%d!\n",errno);
161 /* This function takes care of the write/read to the linux sg device.
162 * It returns TRUE or FALSE and uses FILE_SetDosError() to convert
163 * UNIX errno to Windows GetLastError(). The reason for that is that
164 * several programs will check that error and we might as well set
165 * it here. We also return the value of the read call in
168 BOOL /* NOTE: This function SHOULD BLOCK */
169 SCSI_LinuxDeviceIo( int fd,
170 struct sg_header * lpInBuffer, DWORD cbInBuffer,
171 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
172 LPDWORD lpcbBytesReturned )
177 TRACE("Writing to Liunx sg device\n");
178 dwBytes = write( fd, lpInBuffer, cbInBuffer );
179 if( dwBytes != cbInBuffer )
182 save_error = GetLastError();
183 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
184 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
185 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");
186 WARN("error= %ld\n", save_error );
187 *lpcbBytesReturned = 0;
191 TRACE("Reading reply from Linux sg device\n");
192 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
193 if( *lpcbBytesReturned != cbOutBuffer )
196 save_error = GetLastError();
197 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
198 WARN("error= %ld\n", save_error );
204 /* Internal functions */
205 struct LinuxProcScsiDevice
219 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
222 result = fscanf( procfile,
223 "Host: scsi%d Channel: %d Id: %d Lun: %d\n",
232 result = fscanf( procfile,
233 " Vendor: %8c Model: %16c Rev: %4c\n",
240 result = fscanf( procfile,
241 " Type: %32c ANSI SCSI revision: %d\n",
246 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
256 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
258 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
263 TRACE( " Vendor: %s Model: %s Rev: %s\n",
267 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
274 /* I'll admit, this function is somewhat of a mess... it was originally
275 * designed to make some sort of linked list then I realized that
276 * HKEY_DYN_DATA would be a lot less messy
279 FILE * procfile = NULL;
283 struct LinuxProcScsiDevice dev;
294 procfile = fopen( "/proc/scsi/scsi", "r" );
297 ERR("Could not open /proc/scsi/scsi\n");
301 result = fscanf( procfile, "Attached devices: \n");
304 ERR("Incorrect /proc/scsi/scsi format");
308 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
310 ERR("Could not create HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
314 /* Read info for one device */
315 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
317 /* Add to registry */
319 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
320 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
321 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
323 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
327 SCSI_printprocentry( &dev );
329 /* FIXME: We *REALLY* need number of controllers.. not ha */
330 /* num of hostadapters is highest ha + 1 */
331 if( dev.host >= num_ha )
337 ERR("Incorrect /proc/scsi/scsi format");
340 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
342 ERR("Could not set value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
344 RegCloseKey(hkeyScsi);