1 /**************************************************************************
3 * Copyright (C) 2000 David Elliott <dfe@infinite-internet.net>
4 * Copyright (C) 2005 Vitaliy Margolen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* These routines are to be called from either WNASPI32 or WINASPI */
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.
34 #include "wine/port.h"
38 #include <sys/types.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
56 #include "wine/debug.h"
57 #include "wine/unicode.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
62 static void set_last_error(void)
64 int save_errno = errno; /* errno gets overwritten by printf */
68 SetLastError( ERROR_SHARING_VIOLATION );
71 SetLastError( ERROR_INVALID_HANDLE );
74 SetLastError( ERROR_HANDLE_DISK_FULL );
79 SetLastError( ERROR_ACCESS_DENIED );
82 SetLastError( ERROR_LOCK_VIOLATION );
85 SetLastError( ERROR_FILE_NOT_FOUND );
88 SetLastError( ERROR_CANNOT_MAKE );
92 SetLastError( ERROR_NO_MORE_FILES );
95 SetLastError( ERROR_FILE_EXISTS );
99 SetLastError( ERROR_SEEK );
102 SetLastError( ERROR_DIR_NOT_EMPTY );
105 SetLastError( ERROR_BAD_FORMAT );
108 WARN( "unknown file error: %s\n", strerror(save_errno) );
109 SetLastError( ERROR_GEN_FAILURE );
116 static const WCHAR wDevicemapScsi[] = {'H','A','R','D','W','A','R','E','\\','D','E','V','I','C','E','M','A','P','\\','S','c','s','i',0};
118 /* Exported functions */
119 int ASPI_GetNumControllers(void)
121 HKEY hkeyScsi, hkeyPort;
122 DWORD i = 0, numPorts, num_ha = 0;
125 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wDevicemapScsi, 0,
126 KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hkeyScsi) != ERROR_SUCCESS )
128 ERR("Could not open HKLM\\%s\n", debugstr_w(wDevicemapScsi));
131 while (RegEnumKeyW(hkeyScsi, i++, wPortName, sizeof(wPortName)) == ERROR_SUCCESS)
133 if (RegOpenKeyExW(hkeyScsi, wPortName, 0, KEY_QUERY_VALUE, &hkeyPort) == ERROR_SUCCESS)
135 if (RegQueryInfoKeyW(hkeyPort, NULL, NULL, NULL, &numPorts, NULL, NULL,
136 NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
140 RegCloseKey(hkeyPort);
144 RegCloseKey(hkeyScsi);
145 TRACE("Returning %d host adapters\n", num_ha );
149 BOOL SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
155 snprintf(buffer, sizeof(buffer), KEYNAME_SCSI, h, c, t, d);
156 if( RegOpenKeyExA(HKEY_LOCAL_MACHINE, buffer, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
158 TRACE("Could not open HKLM\\%s; device does not exist\n", buffer);
162 if( RegQueryValueExA(hkeyScsi, "UnixDeviceName", NULL, &type, (LPBYTE)devstr, lpcbData) != ERROR_SUCCESS )
164 WARN("Could not query value HKLM\\%s\\UnixDeviceName\n", buffer);
165 RegCloseKey(hkeyScsi);
168 RegCloseKey(hkeyScsi);
170 TRACE("Device name: %s\n", devstr);
174 /* SCSI_GetHCforController
176 * HIWORD: Host Adapter
179 DWORD ASPI_GetHCforController( int controller )
181 HKEY hkeyScsi, hkeyPort;
182 DWORD i = 0, numPorts;
183 int num_ha = controller + 1;
187 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wDevicemapScsi, 0,
188 KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hkeyScsi) != ERROR_SUCCESS )
190 ERR("Could not open HKLM\\%s\n", debugstr_w(wDevicemapScsi));
193 while (RegEnumKeyW(hkeyScsi, i++, wPortName, sizeof(wPortName)) == ERROR_SUCCESS)
195 if (RegOpenKeyExW(hkeyScsi, wPortName, 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS,
196 &hkeyPort) == ERROR_SUCCESS)
198 if (RegQueryInfoKeyW(hkeyPort, NULL, NULL, NULL, &numPorts, NULL, NULL,
199 NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
202 if (num_ha <= 0) break;
205 RegCloseKey(hkeyPort);
208 RegCloseKey(hkeyScsi);
212 ERR("Invalid controller(%d)\n", controller);
216 if (RegEnumKeyW(hkeyPort, -num_ha, wBusName, sizeof(wBusName)) != ERROR_SUCCESS)
218 ERR("Failed to enumerate keys\n");
219 RegCloseKey(hkeyPort);
222 RegCloseKey(hkeyPort);
224 return (atoiW(&wPortName[9]) << 16) + atoiW(&wBusName[9]);
227 int SCSI_OpenDevice( int h, int c, int t, int d )
233 if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
235 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
239 TRACE("Opening device %s mode O_RDWR\n",devstr);
240 fd = open(devstr, O_RDWR);
242 char *errstring = strerror(errno);
243 ERR("Failed to open device %s: %s\n", devstr, errstring);
251 * Checks to make sure the CMD_LEN is correct
254 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
256 /* This is what the linux kernel thinks.... */
257 static const unsigned char scsi_command_size[8] =
263 int index=(cmd>>5)&7;
265 if (len!=scsi_command_size[index])
267 TRACE("CDBLen for command %d claims to be %d, expected %d\n",
268 cmd, len, scsi_command_size[index]);
269 ioctl(fd,SG_NEXT_CMD_LEN,&len);
274 SCSI_LinuxSetTimeout( int fd, int timeout )
277 TRACE("Setting timeout to %d jiffies\n", timeout);
278 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
281 WARN("Could not set timeout ! (%s)\n", strerror(errno));
287 /* This function takes care of the write/read to the linux sg device.
288 * It returns TRUE or FALSE and uses set_last_error() to convert
289 * UNIX errno to Windows GetLastError(). The reason for that is that
290 * several programs will check that error and we might as well set
291 * it here. We also return the value of the read call in
294 BOOL /* NOTE: This function SHOULD BLOCK */
295 SCSI_LinuxDeviceIo( int fd,
296 struct sg_header * lpInBuffer, DWORD cbInBuffer,
297 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
298 LPDWORD lpcbBytesReturned )
303 TRACE("Writing to Linux sg device\n");
304 dwBytes = write( fd, lpInBuffer, cbInBuffer );
305 if( dwBytes != cbInBuffer )
308 save_error = GetLastError();
309 WARN("Not enough bytes written to scsi device. bytes=%d .. %d\n", cbInBuffer, dwBytes );
310 /* FIXME: set_last_error() never sets error to ERROR_NOT_ENOUGH_MEMORY... */
311 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
312 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)");
313 WARN("error= %d\n", save_error );
314 *lpcbBytesReturned = 0;
318 TRACE("Reading reply from Linux sg device\n");
319 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
320 if( *lpcbBytesReturned != cbOutBuffer )
323 save_error = GetLastError();
324 WARN("Not enough bytes read from scsi device. bytes=%d .. %d\n", cbOutBuffer, *lpcbBytesReturned);
325 WARN("error= %d\n", save_error );
331 static void SCSI_Linux_CheckDevices(void)
334 struct dirent *dent = NULL;
336 devdir = opendir("/dev");
337 for (dent=readdir(devdir);dent;dent=readdir(devdir))
339 if (!(strncmp(dent->d_name, "sg", 2)))
346 TRACE("WARNING: You don't have any /dev/sgX generic scsi devices ! \"man MAKEDEV\" !\n");
355 SCSI_Linux_CheckDevices();