Many typo fixes and case fixes.
[wine.git] / msdos / ppdev.c
blobd3ad3a5e68b71ee6a78d7ee40883df1650149986
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 #ifdef HAVE_PPDEV
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
31 #endif
32 #include <errno.h>
33 #include <linux/ppdev.h>
35 #include "winerror.h"
36 #include "winternl.h"
37 #include "miscemu.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(int);
43 typedef struct _PPDEVICESTRUCT{
44 int fd; /* NULL if device not available */
45 char *devicename;
46 int userbase; /* where wine thinks the ports are*/
47 DWORD lastaccess; /* or NULL if release */
48 int timeout; /* time in second of inactivity to release the port*/
49 } PPDeviceStruct;
51 static PPDeviceStruct PPDeviceList[5];
52 static int PPDeviceNum=0;
54 static int IO_pp_sort(const void *p1,const void *p2)
56 return ((PPDeviceStruct*)p1)->userbase - ((PPDeviceStruct*)p2)->userbase;
59 /* IO_pp_init
61 * Read the ppdev entries from wine.conf, open the device and check
62 * for nescessary IOCTRL
63 * Report verbose about possible errors
65 char IO_pp_init(void)
67 char name[80];
68 char buffer[256];
69 HKEY hkey;
70 int i,idx=0,fd,res,userbase,nports=0;
71 char * timeout;
72 char ret=1;
73 int lasterror;
74 OBJECT_ATTRIBUTES attr;
75 UNICODE_STRING nameW;
77 static const WCHAR configW[] = {'M','a','c','h','i','n','e','\\',
78 'S','o','f','t','w','a','r','e','\\',
79 'W','i','n','e','\\',
80 'W','i','n','e','\\',
81 'C','o','n','f','i','g','\\',
82 'p','p','d','e','v',0};
84 TRACE("\n");
86 attr.Length = sizeof(attr);
87 attr.RootDirectory = 0;
88 attr.ObjectName = &nameW;
89 attr.Attributes = 0;
90 attr.SecurityDescriptor = NULL;
91 attr.SecurityQualityOfService = NULL;
92 RtlInitUnicodeString( &nameW, configW );
94 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) return 1;
96 for (;;)
98 DWORD total_size, len;
99 char temp[256];
100 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)temp;
102 if (NtEnumerateValueKey( hkey, idx, KeyValueFullInformation,
103 temp, sizeof(temp), &total_size )) break;
104 if (info->Type != REG_SZ) break;
106 RtlUnicodeToMultiByteN( name, sizeof(name)-1, &len, info->Name, info->NameLength );
107 name[len] = 0;
108 RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
109 (WCHAR *)(temp + info->DataOffset), total_size-info->DataOffset );
110 buffer[len] = 0;
112 idx++;
113 if(nports >4)
115 FIXME("Make the PPDeviceList larger then 5 elements\n");
116 break;
118 TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
119 timeout = strchr(buffer,',');
120 if (timeout)
121 *timeout++=0;
122 fd=open(buffer,O_RDWR);
123 lasterror=errno;
124 if (fd == -1)
126 WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
127 WARN("Rejecting configuration item\n");
128 if (lasterror == ENODEV)
129 FIXME("Is the ppdev module loaded?\n");
130 continue;
132 userbase = strtol(name,(char **)NULL, 16);
133 if ( errno == ERANGE)
135 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
136 WARN("Rejecting configuration item\n");
137 continue;
139 if (ioctl (fd,PPCLAIM,0))
141 ERR("PPCLAIM rejected %s\n",buffer);
142 ERR("Perhaps the device is already in use or non-existant\n");
143 continue;
145 if (nports > 0)
147 for (i=0; i<= nports; i++)
149 if (PPDeviceList[i].userbase == userbase)
151 WARN("Configuration: %s uses the same virtual ports as %s\n",
152 buffer,PPDeviceList[0].devicename);
153 WARN("Configuration: Rejecting configuration item\n");
154 userbase = 0;
155 break;
158 if (!userbase) continue;
160 /* Check for the minimum required IOCTLS */
161 if ((ioctl(fd,PPRDATA,&res))||
162 (ioctl(fd,PPRCONTROL,&res))||
163 (ioctl(fd,PPRCONTROL,&res)))
165 ERR("PPUSER IOCTL not available for parport device %s\n",buffer);
166 continue;
168 if (ioctl (fd,PPRELEASE,0))
170 ERR("PPRELEASE rejected %s\n",buffer);
171 ERR("Perhaps the device is already in use or non-existant\n");
172 continue;
174 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
175 if (!PPDeviceList[nports].devicename)
177 ERR("No (more)space for devicename\n");
178 break;
180 strcpy(PPDeviceList[nports].devicename,buffer);
181 PPDeviceList[nports].fd = fd;
182 PPDeviceList[nports].userbase = userbase;
183 PPDeviceList[nports].lastaccess=GetTickCount();
184 if (timeout)
186 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
187 if (errno == ERANGE)
189 WARN("Configuration:Invalid timeout %s in configuration for %s, Setting to 0\n",
190 timeout,buffer);
191 PPDeviceList[nports].timeout = 0;
194 else
195 PPDeviceList[nports].timeout = 0;
196 nports++;
198 TRACE("found %d ports\n",nports);
199 NtClose( hkey );
201 PPDeviceNum= nports;
202 if (nports > 1)
203 /* sort in accending order for userbase for faster access*/
204 qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
206 if (nports)
207 ret=0;
208 for (idx= 0;idx<PPDeviceNum; idx++)
209 TRACE("found device %s userbase %x fd %x timeout %d\n",
210 PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
211 PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
212 /* FIXME:
213 register a timer callback perhaps every 30 second to release unused ports
214 Set lastaccess = 0 as indicator when port was released
216 return ret;
219 /* IO_pp_do_access
221 * Do the actual IOCTL
222 * Return NULL on success
224 static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
226 int ret;
227 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
229 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
230 PPDeviceList[idx].devicename);
231 return 1;
233 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
234 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
236 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
237 PPDeviceList[idx].devicename);
238 return 1;
240 return ret;
244 /* IO_pp_inp
246 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
247 * Return NULL on success
249 int IO_pp_inp(int port, DWORD* res)
251 int idx,j=0;
253 for (idx=0;idx<PPDeviceNum ;idx++)
255 j = port - PPDeviceList[idx].userbase;
256 if (j <0) return 1;
257 switch (j)
259 case 0:
260 return IO_pp_do_access(idx,PPRDATA,res);
261 case 1:
262 return IO_pp_do_access(idx,PPRSTATUS,res);
263 case 2:
264 return IO_pp_do_access(idx,PPRCONTROL,res);
265 case 0x400:
266 case 0x402:
267 case 3:
268 case 4:
269 case 0x401:
270 FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
271 FIXME("If this is causing problems, try direct port access\n");
272 return 1;
273 default:
274 break;
277 return 1;
280 /* IO_pp_outp
282 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
283 * Return NULL on success
285 BOOL IO_pp_outp(int port, DWORD* res)
287 int idx,j=0;
289 for (idx=0;idx<PPDeviceNum ;idx++)
291 j = port - PPDeviceList[idx].userbase;
292 if (j <0) return 1;
293 switch (j)
295 case 0:
296 return IO_pp_do_access(idx,PPWDATA,res);
297 case 2:
298 return IO_pp_do_access(idx,PPWCONTROL,res);
299 case 1:
300 case 0x400:
301 case 0x402:
302 case 3:
303 case 4:
304 case 0x401:
305 FIXME("Port %d not accessible for writing with ppdev\n",port);
306 FIXME("If this is causing problems, try direct port access\n");
307 return 1;
308 default:
309 break;
312 return TRUE;
316 #else /* HAVE_PPDEV */
318 #include "windef.h"
320 char IO_pp_init(void)
322 return 1;
325 int IO_pp_inp(int port, DWORD* res)
327 return 1;
330 BOOL IO_pp_outp(int port, DWORD* res)
332 return TRUE;
334 #endif /* HAVE_PPDEV */