ntdll: Avoid inter-process APCs when called for the process itself.
[wine/multimedia.git] / dlls / winedos / ppdev.c
blob8c83f386b3fcbac6e1b56bb85b93f2e3e17e3dab
1 /*
2 * Parallel port device support
4 * Copyright 2001 Uwe Bonnes
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 #include "config.h"
22 #include "wine/port.h"
24 #include "windef.h"
26 #ifdef HAVE_PPDEV
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #include <fcntl.h>
35 #include <errno.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
38 #endif
39 #ifdef HAVE_LINUX_IOCTL_H
40 # include <linux/ioctl.h>
41 #endif
42 #include <linux/ppdev.h>
44 #include "winerror.h"
45 #include "winbase.h"
46 #include "winreg.h"
47 #include "winternl.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(int);
53 typedef struct _PPDEVICESTRUCT{
54 int fd; /* NULL if device not available */
55 char *devicename;
56 int userbase; /* where wine thinks the ports are */
57 DWORD lastaccess; /* or NULL if release */
58 int timeout; /* time in second of inactivity to release the port */
59 } PPDeviceStruct;
61 static PPDeviceStruct PPDeviceList[5];
62 static int PPDeviceNum=0;
64 static int IO_pp_sort(const void *p1,const void *p2)
66 return ((const PPDeviceStruct*)p1)->userbase - ((const PPDeviceStruct*)p2)->userbase;
69 /* IO_pp_init
71 * Read the ppdev entries from wine.conf, open the device and check
72 * for necessary IOCTRL
73 * Report verbose about possible errors
75 char IO_pp_init(void)
77 char name[80];
78 char buffer[256];
79 HANDLE root, hkey;
80 int i,idx=0,fd,res,userbase,nports=0;
81 char * timeout;
82 char ret=1;
83 int lasterror;
84 OBJECT_ATTRIBUTES attr;
85 UNICODE_STRING nameW;
87 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
88 'W','i','n','e','\\','V','D','M','\\','p','p','d','e','v',0};
90 TRACE("\n");
92 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
93 attr.Length = sizeof(attr);
94 attr.RootDirectory = root;
95 attr.ObjectName = &nameW;
96 attr.Attributes = 0;
97 attr.SecurityDescriptor = NULL;
98 attr.SecurityQualityOfService = NULL;
99 RtlInitUnicodeString( &nameW, configW );
101 /* @@ Wine registry key: HKCU\Software\Wine\VDM\ppdev */
102 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
103 NtClose( root );
104 if (!hkey) return 1;
106 for (;;)
108 DWORD total_size, len;
109 char temp[256];
110 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)temp;
112 if (NtEnumerateValueKey( hkey, idx, KeyValueFullInformation,
113 temp, sizeof(temp), &total_size )) break;
114 if (info->Type != REG_SZ) break;
116 RtlUnicodeToMultiByteN( name, sizeof(name)-1, &len, info->Name, info->NameLength );
117 name[len] = 0;
118 RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
119 (WCHAR *)(temp + info->DataOffset), total_size-info->DataOffset );
120 buffer[len] = 0;
122 idx++;
123 if(nports >4)
125 FIXME("Make the PPDeviceList larger than 5 elements\n");
126 break;
128 TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
129 timeout = strchr(buffer,',');
130 if (timeout)
131 *timeout++=0;
132 fd=open(buffer,O_RDWR);
133 lasterror=errno;
134 if (fd == -1)
136 WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
137 WARN("Rejecting configuration item\n");
138 if (lasterror == ENODEV)
139 ERR("Is the ppdev module loaded?\n");
140 continue;
142 userbase = strtol(name,(char **)NULL, 16);
143 if ( errno == ERANGE)
145 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
146 WARN("Rejecting configuration item\n");
147 continue;
149 if (ioctl (fd,PPCLAIM,0))
151 ERR("PPCLAIM rejected %s\n",buffer);
152 ERR("Perhaps the device is already in use or nonexistent\n");
153 continue;
155 if (nports > 0)
157 for (i=0; i<= nports; i++)
159 if (PPDeviceList[i].userbase == userbase)
161 WARN("Configuration: %s uses the same virtual ports as %s\n",
162 buffer,PPDeviceList[0].devicename);
163 WARN("Configuration: Rejecting configuration item\n");
164 userbase = 0;
165 break;
168 if (!userbase) continue;
170 /* Check for the minimum required IOCTLS */
171 if ((ioctl(fd,PPRDATA,&res))||
172 (ioctl(fd,PPRSTATUS,&res))||
173 (ioctl(fd,PPRCONTROL,&res)))
175 ERR("PPUSER IOCTL not available for parport device %s\n",buffer);
176 continue;
178 if (ioctl (fd,PPRELEASE,0))
180 ERR("PPRELEASE rejected %s\n",buffer);
181 ERR("Perhaps the device is already in use or nonexistent\n");
182 continue;
184 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
185 if (!PPDeviceList[nports].devicename)
187 ERR("No (more) space for devicename\n");
188 break;
190 strcpy(PPDeviceList[nports].devicename,buffer);
191 PPDeviceList[nports].fd = fd;
192 PPDeviceList[nports].userbase = userbase;
193 PPDeviceList[nports].lastaccess=GetTickCount();
194 if (timeout)
196 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
197 if (errno == ERANGE)
199 WARN("Configuration: Invalid timeout %s in configuration for %s, Setting to 0\n",
200 timeout,buffer);
201 PPDeviceList[nports].timeout = 0;
204 else
205 PPDeviceList[nports].timeout = 0;
206 nports++;
208 TRACE("found %d ports\n",nports);
209 NtClose( hkey );
211 PPDeviceNum= nports;
212 if (nports > 1)
213 /* sort in ascending order for userbase for faster access */
214 qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
216 if (nports)
217 ret=0;
218 for (idx= 0;idx<PPDeviceNum; idx++)
219 TRACE("found device %s userbase %x fd %x timeout %d\n",
220 PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
221 PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
222 /* FIXME:
223 register a timer callback perhaps every 30 seconds to release unused ports
224 Set lastaccess = 0 as indicator when port was released
226 return ret;
229 /* IO_pp_do_access
231 * Do the actual IOCTL
232 * Return NULL on success
234 static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
236 int ret;
237 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
239 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
240 PPDeviceList[idx].devicename);
241 return 1;
243 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
244 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
246 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
247 PPDeviceList[idx].devicename);
248 return 1;
250 return ret;
254 /* IO_pp_inp
256 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
257 * Return NULL on success
259 int IO_pp_inp(int port, DWORD* res)
261 int idx,j=0;
263 for (idx=0;idx<PPDeviceNum ;idx++)
265 j = port - PPDeviceList[idx].userbase;
266 if (j <0) return 1;
267 switch (j)
269 case 0:
270 return IO_pp_do_access(idx,PPRDATA,res);
271 case 1:
272 return IO_pp_do_access(idx,PPRSTATUS,res);
273 case 2:
274 return IO_pp_do_access(idx,PPRCONTROL,res);
275 case 0x400:
276 case 0x402:
277 case 3:
278 case 4:
279 case 0x401:
280 FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
281 FIXME("If this is causing problems, try direct port access\n");
282 return 1;
283 default:
284 break;
287 return 1;
290 /* IO_pp_outp
292 * Check if we can satisfy the OUTP command with some of the configured PPDEV deviced
293 * Return NULL on success
295 BOOL IO_pp_outp(int port, DWORD* res)
297 int idx,j=0;
299 for (idx=0;idx<PPDeviceNum ;idx++)
301 j = port - PPDeviceList[idx].userbase;
302 if (j <0) return 1;
303 switch (j)
305 case 0:
306 return IO_pp_do_access(idx,PPWDATA,res);
307 case 2:
309 /* We can't switch port direction via PPWCONTROL,
310 so do it via PPDATADIR
312 DWORD mode = *res & 0x20;
313 IO_pp_do_access(idx,PPDATADIR,&mode);
314 mode = (*res & ~0x20);
315 return IO_pp_do_access(idx,PPWCONTROL,&mode);
318 case 1:
319 case 0x400:
320 case 0x402:
321 case 3:
322 case 4:
323 case 0x401:
324 FIXME("Port %d not accessible for writing with ppdev\n",port);
325 FIXME("If this is causing problems, try direct port access\n");
326 return 1;
327 default:
328 break;
331 return TRUE;
335 #else /* HAVE_PPDEV */
338 char IO_pp_init(void)
340 return 1;
343 int IO_pp_inp(int port, DWORD* res)
345 return 1;
348 BOOL IO_pp_outp(int port, DWORD* res)
350 return TRUE;
352 #endif /* HAVE_PPDEV */