Avoid copying invalid data on error.
[wine/wine-kai.git] / dlls / winedos / ppdev.c
blob4ef847fde2d34ff83c0ba70b131bd55dc5cb5e86
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 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #ifdef HAVE_SYS_IOCTL_H
34 # include <sys/ioctl.h>
35 #endif
36 #ifdef HAVE_LINUX_IOCTL_H
37 # include <linux/ioctl.h>
38 #endif
39 #include <linux/ppdev.h>
41 #include "winerror.h"
42 #include "winbase.h"
43 #include "winreg.h"
44 #include "winternl.h"
45 #include "miscemu.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(int);
51 typedef struct _PPDEVICESTRUCT{
52 int fd; /* NULL if device not available */
53 char *devicename;
54 int userbase; /* where wine thinks the ports are */
55 DWORD lastaccess; /* or NULL if release */
56 int timeout; /* time in second of inactivity to release the port */
57 } PPDeviceStruct;
59 static PPDeviceStruct PPDeviceList[5];
60 static int PPDeviceNum=0;
62 static int IO_pp_sort(const void *p1,const void *p2)
64 return ((PPDeviceStruct*)p1)->userbase - ((PPDeviceStruct*)p2)->userbase;
67 /* IO_pp_init
69 * Read the ppdev entries from wine.conf, open the device and check
70 * for necessary IOCTRL
71 * Report verbose about possible errors
73 char IO_pp_init(void)
75 char name[80];
76 char buffer[256];
77 HKEY hkey;
78 int i,idx=0,fd,res,userbase,nports=0;
79 char * timeout;
80 char ret=1;
81 int lasterror;
82 OBJECT_ATTRIBUTES attr;
83 UNICODE_STRING nameW;
85 static const WCHAR configW[] = {'M','a','c','h','i','n','e','\\',
86 'S','o','f','t','w','a','r','e','\\',
87 'W','i','n','e','\\',
88 'W','i','n','e','\\',
89 'C','o','n','f','i','g','\\',
90 'p','p','d','e','v',0};
92 TRACE("\n");
94 attr.Length = sizeof(attr);
95 attr.RootDirectory = 0;
96 attr.ObjectName = &nameW;
97 attr.Attributes = 0;
98 attr.SecurityDescriptor = NULL;
99 attr.SecurityQualityOfService = NULL;
100 RtlInitUnicodeString( &nameW, configW );
102 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) return 1;
104 for (;;)
106 DWORD total_size, len;
107 char temp[256];
108 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)temp;
110 if (NtEnumerateValueKey( hkey, idx, KeyValueFullInformation,
111 temp, sizeof(temp), &total_size )) break;
112 if (info->Type != REG_SZ) break;
114 RtlUnicodeToMultiByteN( name, sizeof(name)-1, &len, info->Name, info->NameLength );
115 name[len] = 0;
116 RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
117 (WCHAR *)(temp + info->DataOffset), total_size-info->DataOffset );
118 buffer[len] = 0;
120 idx++;
121 if(nports >4)
123 FIXME("Make the PPDeviceList larger than 5 elements\n");
124 break;
126 TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
127 timeout = strchr(buffer,',');
128 if (timeout)
129 *timeout++=0;
130 fd=open(buffer,O_RDWR);
131 lasterror=errno;
132 if (fd == -1)
134 WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
135 WARN("Rejecting configuration item\n");
136 if (lasterror == ENODEV)
137 ERR("Is the ppdev module loaded?\n");
138 continue;
140 userbase = strtol(name,(char **)NULL, 16);
141 if ( errno == ERANGE)
143 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
144 WARN("Rejecting configuration item\n");
145 continue;
147 if (ioctl (fd,PPCLAIM,0))
149 ERR("PPCLAIM rejected %s\n",buffer);
150 ERR("Perhaps the device is already in use or non-existent\n");
151 continue;
153 if (nports > 0)
155 for (i=0; i<= nports; i++)
157 if (PPDeviceList[i].userbase == userbase)
159 WARN("Configuration: %s uses the same virtual ports as %s\n",
160 buffer,PPDeviceList[0].devicename);
161 WARN("Configuration: Rejecting configuration item\n");
162 userbase = 0;
163 break;
166 if (!userbase) continue;
168 /* Check for the minimum required IOCTLS */
169 if ((ioctl(fd,PPRDATA,&res))||
170 (ioctl(fd,PPRCONTROL,&res))||
171 (ioctl(fd,PPRCONTROL,&res)))
173 ERR("PPUSER IOCTL not available for parport device %s\n",buffer);
174 continue;
176 if (ioctl (fd,PPRELEASE,0))
178 ERR("PPRELEASE rejected %s\n",buffer);
179 ERR("Perhaps the device is already in use or non-existent\n");
180 continue;
182 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
183 if (!PPDeviceList[nports].devicename)
185 ERR("No (more) space for devicename\n");
186 break;
188 strcpy(PPDeviceList[nports].devicename,buffer);
189 PPDeviceList[nports].fd = fd;
190 PPDeviceList[nports].userbase = userbase;
191 PPDeviceList[nports].lastaccess=GetTickCount();
192 if (timeout)
194 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
195 if (errno == ERANGE)
197 WARN("Configuration: Invalid timeout %s in configuration for %s, Setting to 0\n",
198 timeout,buffer);
199 PPDeviceList[nports].timeout = 0;
202 else
203 PPDeviceList[nports].timeout = 0;
204 nports++;
206 TRACE("found %d ports\n",nports);
207 NtClose( hkey );
209 PPDeviceNum= nports;
210 if (nports > 1)
211 /* sort in ascending order for userbase for faster access */
212 qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
214 if (nports)
215 ret=0;
216 for (idx= 0;idx<PPDeviceNum; idx++)
217 TRACE("found device %s userbase %x fd %x timeout %d\n",
218 PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
219 PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
220 /* FIXME:
221 register a timer callback perhaps every 30 seconds to release unused ports
222 Set lastaccess = 0 as indicator when port was released
224 return ret;
227 /* IO_pp_do_access
229 * Do the actual IOCTL
230 * Return NULL on success
232 static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
234 int ret;
235 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
237 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
238 PPDeviceList[idx].devicename);
239 return 1;
241 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
242 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
244 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
245 PPDeviceList[idx].devicename);
246 return 1;
248 return ret;
252 /* IO_pp_inp
254 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
255 * Return NULL on success
257 int IO_pp_inp(int port, DWORD* res)
259 int idx,j=0;
261 for (idx=0;idx<PPDeviceNum ;idx++)
263 j = port - PPDeviceList[idx].userbase;
264 if (j <0) return 1;
265 switch (j)
267 case 0:
268 return IO_pp_do_access(idx,PPRDATA,res);
269 case 1:
270 return IO_pp_do_access(idx,PPRSTATUS,res);
271 case 2:
272 return IO_pp_do_access(idx,PPRCONTROL,res);
273 case 0x400:
274 case 0x402:
275 case 3:
276 case 4:
277 case 0x401:
278 FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
279 FIXME("If this is causing problems, try direct port access\n");
280 return 1;
281 default:
282 break;
285 return 1;
288 /* IO_pp_outp
290 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
291 * Return NULL on success
293 BOOL IO_pp_outp(int port, DWORD* res)
295 int idx,j=0;
297 for (idx=0;idx<PPDeviceNum ;idx++)
299 j = port - PPDeviceList[idx].userbase;
300 if (j <0) return 1;
301 switch (j)
303 case 0:
304 return IO_pp_do_access(idx,PPWDATA,res);
305 case 2:
307 /* We can't switch port direction via PPWCONTROL,
308 so do it via PPDATADIR
310 DWORD mode = *res & 0x20;
311 IO_pp_do_access(idx,PPDATADIR,&mode);
312 mode = (*res & ~0x20);
313 return IO_pp_do_access(idx,PPWCONTROL,&mode);
316 case 1:
317 case 0x400:
318 case 0x402:
319 case 3:
320 case 4:
321 case 0x401:
322 FIXME("Port %d not accessible for writing with ppdev\n",port);
323 FIXME("If this is causing problems, try direct port access\n");
324 return 1;
325 default:
326 break;
329 return TRUE;
333 #else /* HAVE_PPDEV */
336 char IO_pp_init(void)
338 return 1;
341 int IO_pp_inp(int port, DWORD* res)
343 return 1;
346 BOOL IO_pp_outp(int port, DWORD* res)
348 return TRUE;
350 #endif /* HAVE_PPDEV */