blob: 78a102a11c2b32d9c4b87fc0be43d584f134dc4b [file] [log] [blame]
Uwe Bonnes6509fa92001-06-26 21:06:07 +00001/*
2 * Parallel-port device support
3 */
4
5#include "config.h"
6
Patrik Stridvall33929be2001-07-18 21:04:23 +00007#ifdef HAVE_PPDEV
Uwe Bonnes6509fa92001-06-26 21:06:07 +00008
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>
16
17#include "winerror.h"
18#include "winreg.h"
19
Patrik Stridvall33929be2001-07-18 21:04:23 +000020#include "miscemu.h"
21
22#include "debugtools.h"
23
24DEFAULT_DEBUG_CHANNEL(int);
Uwe Bonnes6509fa92001-06-26 21:06:07 +000025
26typedef 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;
33
34static PPDeviceStruct PPDeviceList[5];
35static int PPDeviceNum=0;
36
37static int IO_pp_sort(const void *p1,const void *p2)
38{
39 return ((PPDeviceStruct*)p1)->userbase - ((PPDeviceStruct*)p2)->userbase;
40}
41
42/* IO_pp_init
43 *
44 * Read the ppdev entries from wine.conf, open the device and check
45 * for nescessary IOCTRL
46 * Report verbose about possible errors
47 */
48char IO_pp_init(void)
49{
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;
58
59 TRACE("\n");
60 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppdev", &hkey ) != ERROR_SUCCESS)
61 return 1;
62
63 for (;;)
64 {
65 DWORD type, count = sizeof(buffer), name_len = sizeof(name);
66
67 if (RegEnumValueA( hkey, idx, name, &name_len, NULL, &type, buffer, &count )!= ERROR_SUCCESS)
68 break;
69
70 idx++;
71 if(nports >4)
72 {
73 FIXME("Make the PPDeviceList larger then 5 elements\n");
74 break;
75 }
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)
83 {
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;
89 }
90 userbase = strtol(name,(char **)NULL, 16);
91 if ( errno == ERANGE)
92 {
93 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
94 WARN("Rejecting configuration item\n");
95 continue;
96 }
Uwe Bonnes6509fa92001-06-26 21:06:07 +000097 if (ioctl (fd,PPCLAIM,0))
98 {
99 ERR("PPCLAIM rejected %s\n",buffer);
100 ERR("Perhaps the device is already in use or non-existant\n");
101 continue;
102 }
103 if (nports > 0)
104 {
105 for (i=0; i<= nports; i++)
106 {
107 if (PPDeviceList[i].userbase == userbase)
108 {
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;
114 }
115 }
116 if (!userbase) continue;
117 }
118 /* Check for the minimum required IOCTLS */
119 if ((ioctl(fd,PPRDATA,&res))||
120 (ioctl(fd,PPRCONTROL,&res))||
121 (ioctl(fd,PPRCONTROL,&res)))
122 {
123 ERR("PPUSER IOCTL not available for parport device %s\n",temp);
124 continue;
125 }
Uwe Bonnescf735f72001-07-14 00:46:22 +0000126 if (ioctl (fd,PPRELEASE,0))
127 {
128 ERR("PPRELEASE rejected %s\n",buffer);
129 ERR("Perhaps the device is already in use or non-existant\n");
130 continue;
131 }
Uwe Bonnes6509fa92001-06-26 21:06:07 +0000132 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
133 if (!PPDeviceList[nports].devicename)
134 {
135 ERR("No (more)space for devicename\n");
136 break;
137 }
138 strcpy(PPDeviceList[nports].devicename,buffer);
139 PPDeviceList[nports].fd = fd;
140 PPDeviceList[nports].userbase = userbase;
141 PPDeviceList[nports].lastaccess=GetTickCount();
142 if (timeout)
143 {
144 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
145 if (errno == ERANGE)
146 {
147 WARN("Configuration:Invalid timeout %s in configuration for %s, Setting to 0\n",
148 timeout,buffer);
149 PPDeviceList[nports].timeout = 0;
150 }
151 }
152 else
153 PPDeviceList[nports].timeout = 0;
154 nports++;
155 }
156 TRACE("found %d ports\n",nports);
157 RegCloseKey( hkey );
158
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);
163
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
173 */
174 return ret;
175}
176
177/* IO_pp_do_access
178 *
179 * Do the actual IOCTL
180 * Return NULL on success
181 */
182static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
183{
Uwe Bonnescf735f72001-07-14 00:46:22 +0000184 int ret;
185 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
186 {
187 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
188 PPDeviceList[idx].devicename);
189 return 1;
190 }
191 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
192 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
193 {
194 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
195 PPDeviceList[idx].devicename);
196 return 1;
197 }
198 return ret;
199
Uwe Bonnes6509fa92001-06-26 21:06:07 +0000200}
201
202/* IO_pp_inp
203 *
204 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
205 * Return NULL on success
206 */
207int IO_pp_inp(int port, DWORD* res)
208{
209 int idx,j=0;
210
211 for (idx=0;idx<PPDeviceNum ;idx++)
212 {
213 j = port - PPDeviceList[idx].userbase;
214 if (j <0) return 1;
215 switch (j)
216 {
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;
233 }
234 }
235 return 1;
236}
237
238/* IO_pp_outp
239 *
240 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
241 * Return NULL on success
242 */
243BOOL IO_pp_outp(int port, DWORD* res)
244{
245 int idx,j=0;
246
247 for (idx=0;idx<PPDeviceNum ;idx++)
248 {
249 j = port - PPDeviceList[idx].userbase;
250 if (j <0) return 1;
251 switch (j)
252 {
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;
268 }
269 }
270 return TRUE;
271}
272
273
274#else /* HAVE_PPDEV */
275
Patrik Stridvall33929be2001-07-18 21:04:23 +0000276#include "windef.h"
277
Uwe Bonnes6509fa92001-06-26 21:06:07 +0000278char IO_pp_init(void)
279{
280 return 1;
281}
282
283int IO_pp_inp(int port, DWORD* res)
284{
285 return 1;
286}
287
288BOOL IO_pp_outp(int port, DWORD* res)
289{
290 return TRUE;
291}
292#endif /* HAVE_PPDEV */