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