Moved port configuration to HKCU\Software\Wine\VDM\ppdev.
[wine/dcerpc.git] / dlls / winedos / ppdev.c
blob259dbacf25e5f5f83492c2d148ef3900db47be72
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include "windef.h"
25 #ifdef HAVE_PPDEV
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_STAT_H
31 # include <sys/stat.h>
32 #endif
33 #include <fcntl.h>
34 #include <errno.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
37 #endif
38 #ifdef HAVE_LINUX_IOCTL_H
39 # include <linux/ioctl.h>
40 #endif
41 #include <linux/ppdev.h>
43 #include "winerror.h"
44 #include "winbase.h"
45 #include "winreg.h"
46 #include "winternl.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(int);
52 typedef struct _PPDEVICESTRUCT{
53 int fd; /* NULL if device not available */
54 char *devicename;
55 int userbase; /* where wine thinks the ports are */
56 DWORD lastaccess; /* or NULL if release */
57 int timeout; /* time in second of inactivity to release the port */
58 } PPDeviceStruct;
60 static PPDeviceStruct PPDeviceList[5];
61 static int PPDeviceNum=0;
63 static int IO_pp_sort(const void *p1,const void *p2)
65 return ((const PPDeviceStruct*)p1)->userbase - ((const PPDeviceStruct*)p2)->userbase;
68 /* IO_pp_init
70 * Read the ppdev entries from wine.conf, open the device and check
71 * for necessary IOCTRL
72 * Report verbose about possible errors
74 char IO_pp_init(void)
76 char name[80];
77 char buffer[256];
78 HANDLE root, hkey;
79 int i,idx=0,fd,res,userbase,nports=0;
80 char * timeout;
81 char ret=1;
82 int lasterror;
83 OBJECT_ATTRIBUTES attr;
84 UNICODE_STRING nameW;
86 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
87 'W','i','n','e','\\','V','D','M','\\','p','p','d','e','v',0};
89 TRACE("\n");
91 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
92 attr.Length = sizeof(attr);
93 attr.RootDirectory = root;
94 attr.ObjectName = &nameW;
95 attr.Attributes = 0;
96 attr.SecurityDescriptor = NULL;
97 attr.SecurityQualityOfService = NULL;
98 RtlInitUnicodeString( &nameW, configW );
100 /* @@ Wine registry key: HKCU\Software\Wine\VDM\ppdev */
101 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
102 NtClose( root );
103 if (!hkey) return 1;
105 for (;;)
107 DWORD total_size, len;
108 char temp[256];
109 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)temp;
111 if (NtEnumerateValueKey( hkey, idx, KeyValueFullInformation,
112 temp, sizeof(temp), &total_size )) break;
113 if (info->Type != REG_SZ) break;
115 RtlUnicodeToMultiByteN( name, sizeof(name)-1, &len, info->Name, info->NameLength );
116 name[len] = 0;
117 RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
118 (WCHAR *)(temp + info->DataOffset), total_size-info->DataOffset );
119 buffer[len] = 0;
121 idx++;
122 if(nports >4)
124 FIXME("Make the PPDeviceList larger than 5 elements\n");
125 break;
127 TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
128 timeout = strchr(buffer,',');
129 if (timeout)
130 *timeout++=0;
131 fd=open(buffer,O_RDWR);
132 lasterror=errno;
133 if (fd == -1)
135 WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
136 WARN("Rejecting configuration item\n");
137 if (lasterror == ENODEV)
138 ERR("Is the ppdev module loaded?\n");
139 continue;
141 userbase = strtol(name,(char **)NULL, 16);
142 if ( errno == ERANGE)
144 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
145 WARN("Rejecting configuration item\n");
146 continue;
148 if (ioctl (fd,PPCLAIM,0))
150 ERR("PPCLAIM rejected %s\n",buffer);
151 ERR("Perhaps the device is already in use or nonexistent\n");
152 continue;
154 if (nports > 0)
156 for (i=0; i<= nports; i++)
158 if (PPDeviceList[i].userbase == userbase)
160 WARN("Configuration: %s uses the same virtual ports as %s\n",
161 buffer,PPDeviceList[0].devicename);
162 WARN("Configuration: Rejecting configuration item\n");
163 userbase = 0;
164 break;
167 if (!userbase) continue;
169 /* Check for the minimum required IOCTLS */
170 if ((ioctl(fd,PPRDATA,&res))||
171 (ioctl(fd,PPRCONTROL,&res))||
172 (ioctl(fd,PPRCONTROL,&res)))
174 ERR("PPUSER IOCTL not available for parport device %s\n",buffer);
175 continue;
177 if (ioctl (fd,PPRELEASE,0))
179 ERR("PPRELEASE rejected %s\n",buffer);
180 ERR("Perhaps the device is already in use or nonexistent\n");
181 continue;
183 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
184 if (!PPDeviceList[nports].devicename)
186 ERR("No (more) space for devicename\n");
187 break;
189 strcpy(PPDeviceList[nports].devicename,buffer);
190 PPDeviceList[nports].fd = fd;
191 PPDeviceList[nports].userbase = userbase;
192 PPDeviceList[nports].lastaccess=GetTickCount();
193 if (timeout)
195 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
196 if (errno == ERANGE)
198 WARN("Configuration: Invalid timeout %s in configuration for %s, Setting to 0\n",
199 timeout,buffer);
200 PPDeviceList[nports].timeout = 0;
203 else
204 PPDeviceList[nports].timeout = 0;
205 nports++;
207 TRACE("found %d ports\n",nports);
208 NtClose( hkey );
210 PPDeviceNum= nports;
211 if (nports > 1)
212 /* sort in ascending order for userbase for faster access */
213 qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
215 if (nports)
216 ret=0;
217 for (idx= 0;idx<PPDeviceNum; idx++)
218 TRACE("found device %s userbase %x fd %x timeout %d\n",
219 PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
220 PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
221 /* FIXME:
222 register a timer callback perhaps every 30 seconds to release unused ports
223 Set lastaccess = 0 as indicator when port was released
225 return ret;
228 /* IO_pp_do_access
230 * Do the actual IOCTL
231 * Return NULL on success
233 static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
235 int ret;
236 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
238 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
239 PPDeviceList[idx].devicename);
240 return 1;
242 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
243 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
245 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
246 PPDeviceList[idx].devicename);
247 return 1;
249 return ret;
253 /* IO_pp_inp
255 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
256 * Return NULL on success
258 int IO_pp_inp(int port, DWORD* res)
260 int idx,j=0;
262 for (idx=0;idx<PPDeviceNum ;idx++)
264 j = port - PPDeviceList[idx].userbase;
265 if (j <0) return 1;
266 switch (j)
268 case 0:
269 return IO_pp_do_access(idx,PPRDATA,res);
270 case 1:
271 return IO_pp_do_access(idx,PPRSTATUS,res);
272 case 2:
273 return IO_pp_do_access(idx,PPRCONTROL,res);
274 case 0x400:
275 case 0x402:
276 case 3:
277 case 4:
278 case 0x401:
279 FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
280 FIXME("If this is causing problems, try direct port access\n");
281 return 1;
282 default:
283 break;
286 return 1;
289 /* IO_pp_outp
291 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
292 * Return NULL on success
294 BOOL IO_pp_outp(int port, DWORD* res)
296 int idx,j=0;
298 for (idx=0;idx<PPDeviceNum ;idx++)
300 j = port - PPDeviceList[idx].userbase;
301 if (j <0) return 1;
302 switch (j)
304 case 0:
305 return IO_pp_do_access(idx,PPWDATA,res);
306 case 2:
308 /* We can't switch port direction via PPWCONTROL,
309 so do it via PPDATADIR
311 DWORD mode = *res & 0x20;
312 IO_pp_do_access(idx,PPDATADIR,&mode);
313 mode = (*res & ~0x20);
314 return IO_pp_do_access(idx,PPWCONTROL,&mode);
317 case 1:
318 case 0x400:
319 case 0x402:
320 case 3:
321 case 4:
322 case 0x401:
323 FIXME("Port %d not accessible for writing with ppdev\n",port);
324 FIXME("If this is causing problems, try direct port access\n");
325 return 1;
326 default:
327 break;
330 return TRUE;
334 #else /* HAVE_PPDEV */
337 char IO_pp_init(void)
339 return 1;
342 int IO_pp_inp(int port, DWORD* res)
344 return 1;
347 BOOL IO_pp_outp(int port, DWORD* res)
349 return TRUE;
351 #endif /* HAVE_PPDEV */