Add exclusion defs & remaining funcs, remove internal types &
[wine/hacks.git] / msdos / ppdev.c
blob78a102a11c2b32d9c4b87fc0be43d584f134dc4b
1 /*
2 * Parallel-port device support
3 */
5 #include "config.h"
7 #ifdef HAVE_PPDEV
9 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/ioctl.h>
14 #include <errno.h>
15 #include <linux/ppdev.h>
17 #include "winerror.h"
18 #include "winreg.h"
20 #include "miscemu.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(int);
26 typedef struct _PPDEVICESTRUCT{
27 int fd; /* NULL if device not available */
28 char *devicename;
29 int userbase; /* where wine thinks the ports are*/
30 DWORD lastaccess; /* or NULL if release */
31 int timeout; /* time in second of inactivity to release the port*/
32 } PPDeviceStruct;
34 static PPDeviceStruct PPDeviceList[5];
35 static int PPDeviceNum=0;
37 static int IO_pp_sort(const void *p1,const void *p2)
39 return ((PPDeviceStruct*)p1)->userbase - ((PPDeviceStruct*)p2)->userbase;
42 /* IO_pp_init
44 * Read the ppdev entries from wine.conf, open the device and check
45 * for nescessary IOCTRL
46 * Report verbose about possible errors
48 char IO_pp_init(void)
50 char name[80];
51 char buffer[1024];
52 HKEY hkey;
53 char temp[256];
54 int i,idx=0,fd,res,userbase,nports=0;
55 char * timeout;
56 char ret=1;
57 int lasterror;
59 TRACE("\n");
60 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppdev", &hkey ) != ERROR_SUCCESS)
61 return 1;
63 for (;;)
65 DWORD type, count = sizeof(buffer), name_len = sizeof(name);
67 if (RegEnumValueA( hkey, idx, name, &name_len, NULL, &type, buffer, &count )!= ERROR_SUCCESS)
68 break;
70 idx++;
71 if(nports >4)
73 FIXME("Make the PPDeviceList larger then 5 elements\n");
74 break;
76 TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
77 timeout = strchr(buffer,',');
78 if (timeout)
79 *timeout++=0;
80 fd=open(buffer,O_RDWR);
81 lasterror=errno;
82 if (fd == -1)
84 WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
85 WARN("Rejecting configuration item\n");
86 if (lasterror == ENODEV)
87 FIXME("Is the ppdev module loaded?\n");
88 continue;
90 userbase = strtol(name,(char **)NULL, 16);
91 if ( errno == ERANGE)
93 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
94 WARN("Rejecting configuration item\n");
95 continue;
97 if (ioctl (fd,PPCLAIM,0))
99 ERR("PPCLAIM rejected %s\n",buffer);
100 ERR("Perhaps the device is already in use or non-existant\n");
101 continue;
103 if (nports > 0)
105 for (i=0; i<= nports; i++)
107 if (PPDeviceList[i].userbase == userbase)
109 WARN("Configuration: %s uses the same virtual ports as %s\n",
110 buffer,PPDeviceList[0].devicename);
111 WARN("Configuration: Rejecting configuration item");
112 userbase = 0;
113 break;
116 if (!userbase) continue;
118 /* Check for the minimum required IOCTLS */
119 if ((ioctl(fd,PPRDATA,&res))||
120 (ioctl(fd,PPRCONTROL,&res))||
121 (ioctl(fd,PPRCONTROL,&res)))
123 ERR("PPUSER IOCTL not available for parport device %s\n",temp);
124 continue;
126 if (ioctl (fd,PPRELEASE,0))
128 ERR("PPRELEASE rejected %s\n",buffer);
129 ERR("Perhaps the device is already in use or non-existant\n");
130 continue;
132 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
133 if (!PPDeviceList[nports].devicename)
135 ERR("No (more)space for devicename\n");
136 break;
138 strcpy(PPDeviceList[nports].devicename,buffer);
139 PPDeviceList[nports].fd = fd;
140 PPDeviceList[nports].userbase = userbase;
141 PPDeviceList[nports].lastaccess=GetTickCount();
142 if (timeout)
144 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
145 if (errno == ERANGE)
147 WARN("Configuration:Invalid timeout %s in configuration for %s, Setting to 0\n",
148 timeout,buffer);
149 PPDeviceList[nports].timeout = 0;
152 else
153 PPDeviceList[nports].timeout = 0;
154 nports++;
156 TRACE("found %d ports\n",nports);
157 RegCloseKey( hkey );
159 PPDeviceNum= nports;
160 if (nports > 1)
161 /* sort in accending order for userbase for faster access*/
162 qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
164 if (nports)
165 ret=0;
166 for (idx= 0;idx<PPDeviceNum; idx++)
167 TRACE("found device %s userbase %x fd %x timeout %d\n",
168 PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
169 PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
170 /* FIXME:
171 register a timer callback perhaps every 30 second to release unused ports
172 Set lastaccess = 0 as indicator when port was released
174 return ret;
177 /* IO_pp_do_access
179 * Do the actual IOCTL
180 * Return NULL on success
182 static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
184 int ret;
185 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
187 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
188 PPDeviceList[idx].devicename);
189 return 1;
191 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
192 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
194 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
195 PPDeviceList[idx].devicename);
196 return 1;
198 return ret;
202 /* IO_pp_inp
204 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
205 * Return NULL on success
207 int IO_pp_inp(int port, DWORD* res)
209 int idx,j=0;
211 for (idx=0;idx<PPDeviceNum ;idx++)
213 j = port - PPDeviceList[idx].userbase;
214 if (j <0) return 1;
215 switch (j)
217 case 0:
218 return IO_pp_do_access(idx,PPRDATA,res);
219 case 1:
220 return IO_pp_do_access(idx,PPRSTATUS,res);
221 case 2:
222 return IO_pp_do_access(idx,PPRCONTROL,res);
223 case 0x400:
224 case 0x402:
225 case 3:
226 case 4:
227 case 0x401:
228 FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
229 FIXME("If this is causing problems, try direct port access\n");
230 return 1;
231 default:
232 break;
235 return 1;
238 /* IO_pp_outp
240 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
241 * Return NULL on success
243 BOOL IO_pp_outp(int port, DWORD* res)
245 int idx,j=0;
247 for (idx=0;idx<PPDeviceNum ;idx++)
249 j = port - PPDeviceList[idx].userbase;
250 if (j <0) return 1;
251 switch (j)
253 case 0:
254 return IO_pp_do_access(idx,PPWDATA,res);
255 case 2:
256 return IO_pp_do_access(idx,PPWCONTROL,res);
257 case 1:
258 case 0x400:
259 case 0x402:
260 case 3:
261 case 4:
262 case 0x401:
263 FIXME("Port %d not accessible for writing with ppdev\n",port);
264 FIXME("If this is causing problems, try direct port access\n");
265 return 1;
266 default:
267 break;
270 return TRUE;
274 #else /* HAVE_PPDEV */
276 #include "windef.h"
278 char IO_pp_init(void)
280 return 1;
283 int IO_pp_inp(int port, DWORD* res)
285 return 1;
288 BOOL IO_pp_outp(int port, DWORD* res)
290 return TRUE;
292 #endif /* HAVE_PPDEV */