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