wnaspi32: Renamed the winaspi directory to wnaspi32.
[wine] / dlls / wnaspi32 / aspi.c
1 /**************************************************************************
2  * ASPI routines
3  * Copyright (C) 2000 David Elliott <dfe@infinite-internet.net>
4  * Copyright (C) 2005 Vitaliy Margolen
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /* These routines are to be called from either WNASPI32 or WINASPI */
22
23 /* FIXME:
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.
30  */
31
32
33 #include "config.h"
34 #include "wine/port.h"
35
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42 #include <fcntl.h>
43 #include <dirent.h>
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47 #include <errno.h>
48 #include <string.h>
49
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winreg.h"
53 #include "winerror.h"
54 #include "winescsi.h"
55
56 #include "wine/debug.h"
57 #include "wine/unicode.h"
58
59 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
60
61 #ifdef linux
62 static void set_last_error(void)
63 {
64     int save_errno = errno; /* errno gets overwritten by printf */
65     switch (errno)
66     {
67     case EAGAIN:
68         SetLastError( ERROR_SHARING_VIOLATION );
69         break;
70     case EBADF:
71         SetLastError( ERROR_INVALID_HANDLE );
72         break;
73     case ENOSPC:
74         SetLastError( ERROR_HANDLE_DISK_FULL );
75         break;
76     case EACCES:
77     case EPERM:
78     case EROFS:
79         SetLastError( ERROR_ACCESS_DENIED );
80         break;
81     case EBUSY:
82         SetLastError( ERROR_LOCK_VIOLATION );
83         break;
84     case ENOENT:
85         SetLastError( ERROR_FILE_NOT_FOUND );
86         break;
87     case EISDIR:
88         SetLastError( ERROR_CANNOT_MAKE );
89         break;
90     case ENFILE:
91     case EMFILE:
92         SetLastError( ERROR_NO_MORE_FILES );
93         break;
94     case EEXIST:
95         SetLastError( ERROR_FILE_EXISTS );
96         break;
97     case EINVAL:
98     case ESPIPE:
99         SetLastError( ERROR_SEEK );
100         break;
101     case ENOTEMPTY:
102         SetLastError( ERROR_DIR_NOT_EMPTY );
103         break;
104     case ENOEXEC:
105         SetLastError( ERROR_BAD_FORMAT );
106         break;
107     default:
108         WARN( "unknown file error: %s\n", strerror(save_errno) );
109         SetLastError( ERROR_GEN_FAILURE );
110         break;
111     }
112     errno = save_errno;
113 }
114 #endif
115
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};
117
118 /* Exported functions */
119 int ASPI_GetNumControllers()
120 {
121     HKEY hkeyScsi, hkeyPort;
122     DWORD i = 0, numPorts, num_ha = 0;
123     WCHAR wPortName[11];
124
125     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wDevicemapScsi, 0,
126         KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hkeyScsi) != ERROR_SUCCESS )
127     {
128         ERR("Could not open HKLM\\%s\n", debugstr_w(wDevicemapScsi));
129         return 0;
130     }
131     while (RegEnumKeyW(hkeyScsi, i++, wPortName, sizeof(wPortName)) == ERROR_SUCCESS)
132     {
133         if (RegOpenKeyExW(hkeyScsi, wPortName, 0, KEY_QUERY_VALUE, &hkeyPort) == ERROR_SUCCESS)
134         {
135             if (RegQueryInfoKeyW(hkeyPort, NULL, NULL, NULL, &numPorts, NULL, NULL,
136                              NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
137             {
138                 num_ha += numPorts;
139             }
140             RegCloseKey(hkeyPort);
141         }
142     }
143
144     RegCloseKey(hkeyScsi);
145     TRACE("Returning %ld host adapters\n", num_ha );
146     return num_ha;
147 }
148
149 BOOL SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
150 {
151     char buffer[200];
152     HKEY hkeyScsi;
153     DWORD type;
154
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 )
157     {
158         ERR("Could not open HKLM\\%s\n", buffer);
159         return FALSE;
160     }
161
162     if( RegQueryValueExA(hkeyScsi, "UnixDeviceName", NULL, &type, (LPBYTE)devstr, lpcbData) != ERROR_SUCCESS )
163     {
164         WARN("Could not query value HKLM\\%s\\UnixDeviceName\n", buffer);
165         RegCloseKey(hkeyScsi);
166         return FALSE;
167     }
168     RegCloseKey(hkeyScsi);
169
170     TRACE("Device name: %s\n", devstr);
171     return TRUE;
172 }
173
174 /* SCSI_GetHCforController
175  * RETURNS
176  *      HIWORD: Host Adapter
177  *      LOWORD: Channel
178  */
179 DWORD ASPI_GetHCforController( int controller )
180 {
181     HKEY hkeyScsi, hkeyPort;
182     DWORD i = 0, numPorts;
183     int num_ha = controller + 1;
184     WCHAR wPortName[11];
185
186     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wDevicemapScsi, 0,
187         KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hkeyScsi) != ERROR_SUCCESS )
188     {
189         ERR("Could not open HKLM\\%s\n", debugstr_w(wDevicemapScsi));
190         return 0xFFFFFFFF;
191     }
192     while (RegEnumKeyW(hkeyScsi, i++, wPortName, sizeof(wPortName)) == ERROR_SUCCESS)
193     {
194         if (RegOpenKeyExW(hkeyScsi, wPortName, 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS,
195                           &hkeyPort) == ERROR_SUCCESS)
196         {
197             if (RegQueryInfoKeyW(hkeyPort, NULL, NULL, NULL, &numPorts, NULL, NULL,
198                              NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
199             {
200                 num_ha -= numPorts;
201                 if (num_ha <= 0) break;
202             }
203             else
204             RegCloseKey(hkeyPort);
205         }
206     }
207     RegCloseKey(hkeyScsi);
208
209     if (num_ha > 0)
210     {
211         ERR("Invalid controller(%d)\n", controller);
212         return 0xFFFFFFFF;
213     }
214
215     if (RegEnumKeyW(hkeyPort, -num_ha, wPortName, sizeof(wPortName)) != ERROR_SUCCESS)
216     {
217         ERR("Failed to enumerate keys\n");
218         RegCloseKey(hkeyPort);
219         return 0xFFFFFFFF;
220     }
221     RegCloseKey(hkeyPort);
222
223     return ((--i) << 16) + atoiW(&wPortName[9]);
224 }
225
226 int SCSI_OpenDevice( int h, int c, int t, int d )
227 {
228     char devstr[20];
229     DWORD cbData = 20;
230     int fd = -1;
231
232     if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
233     {
234         WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
235         return -1;
236     }
237
238     TRACE("Opening device %s mode O_RDWR\n",devstr);
239     fd = open(devstr, O_RDWR);
240
241     return fd;
242 }
243
244 #ifdef linux
245 /* SCSI_Fix_CMD_LEN
246  *      Checks to make sure the CMD_LEN is correct
247  */
248 void
249 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
250 {
251         /* This is what the linux kernel thinks.... */
252         static const unsigned char scsi_command_size[8] =
253         {
254                 6, 10, 10, 12,
255                 12, 12, 10, 10
256         };
257
258         int index=(cmd>>5)&7;
259
260         if (len!=scsi_command_size[index])
261         {
262                 TRACE("CDBLen for command %d claims to be %d, expected %d\n",
263                                 cmd, len, scsi_command_size[index]);
264                 ioctl(fd,SG_NEXT_CMD_LEN,&len);
265         }
266 }
267
268 int
269 SCSI_LinuxSetTimeout( int fd, int timeout )
270 {
271         int retval;
272         TRACE("Setting timeout to %d jiffies\n", timeout);
273         retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
274         if(retval)
275         {
276                 WARN("Could not set timeout ! (%s)\n", strerror(errno));
277         }
278         return retval;
279
280 }
281
282 /* This function takes care of the write/read to the linux sg device.
283  * It returns TRUE or FALSE and uses set_last_error() to convert
284  * UNIX errno to Windows GetLastError().  The reason for that is that
285  * several programs will check that error and we might as well set
286  * it here.  We also return the value of the read call in
287  * lpcbBytesReturned.
288  */
289 BOOL /* NOTE: This function SHOULD BLOCK */
290 SCSI_LinuxDeviceIo( int fd,
291                 struct sg_header * lpInBuffer, DWORD cbInBuffer,
292                 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
293                 LPDWORD lpcbBytesReturned )
294 {
295         DWORD dwBytes;
296         DWORD save_error;
297
298         TRACE("Writing to Linux sg device\n");
299         dwBytes = write( fd, lpInBuffer, cbInBuffer );
300         if( dwBytes != cbInBuffer )
301         {
302                 set_last_error();
303                 save_error = GetLastError();
304                 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
305                 /* FIXME: set_last_error() never sets error to ERROR_NOT_ENOUGH_MEMORY... */
306                 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
307                         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)");
308                 WARN("error= %ld\n", save_error );
309                 *lpcbBytesReturned = 0;
310                 return FALSE;
311         }
312
313         TRACE("Reading reply from Linux sg device\n");
314         *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
315         if( *lpcbBytesReturned != cbOutBuffer )
316         {
317                 set_last_error();
318                 save_error = GetLastError();
319                 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
320                 WARN("error= %ld\n", save_error );
321                 return FALSE;
322         }
323         return TRUE;
324 }
325
326 static void SCSI_Linux_CheckDevices(void)
327 {
328     DIR *devdir;
329     struct dirent *dent = NULL;
330
331     devdir = opendir("/dev");
332     for (dent=readdir(devdir);dent;dent=readdir(devdir))
333     {
334         if (!(strncmp(dent->d_name, "sg", 2)))
335             break;
336     }
337     closedir(devdir);
338
339     if (dent == NULL)
340     {
341         TRACE("WARNING: You don't have any /dev/sgX generic scsi devices ! \"man MAKEDEV\" !\n");
342         return;
343     }
344 }
345 #endif
346
347 void SCSI_Init(void)
348 {
349 #ifdef linux
350     SCSI_Linux_CheckDevices();
351 #endif
352 }