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
26 #include <sys/types.h>
29 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
33 #include <linux/ppdev.h>
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(int);
44 typedef struct _PPDEVICESTRUCT{
45 int fd; /* NULL if device not available */
47 int userbase; /* where wine thinks the ports are*/
48 DWORD lastaccess; /* or NULL if release */
49 int timeout; /* time in second of inactivity to release the port*/
52 static PPDeviceStruct PPDeviceList[5];
53 static int PPDeviceNum=0;
55 static int IO_pp_sort(const void *p1,const void *p2)
57 return ((PPDeviceStruct*)p1)->userbase - ((PPDeviceStruct*)p2)->userbase;
62 * Read the ppdev entries from wine.conf, open the device and check
63 * for nescessary IOCTRL
64 * Report verbose about possible errors
72 int i,idx=0,fd,res,userbase,nports=0;
78 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppdev", &hkey ) != ERROR_SUCCESS)
83 DWORD type, count = sizeof(buffer), name_len = sizeof(name);
85 if (RegEnumValueA( hkey, idx, name, &name_len, NULL, &type, buffer, &count )!= ERROR_SUCCESS)
91 FIXME("Make the PPDeviceList larger then 5 elements\n");
94 TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
95 timeout = strchr(buffer,',');
98 fd=open(buffer,O_RDWR);
102 WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
103 WARN("Rejecting configuration item\n");
104 if (lasterror == ENODEV)
105 FIXME("Is the ppdev module loaded?\n");
108 userbase = strtol(name,(char **)NULL, 16);
109 if ( errno == ERANGE)
111 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
112 WARN("Rejecting configuration item\n");
115 if (ioctl (fd,PPCLAIM,0))
117 ERR("PPCLAIM rejected %s\n",buffer);
118 ERR("Perhaps the device is already in use or non-existant\n");
123 for (i=0; i<= nports; i++)
125 if (PPDeviceList[i].userbase == userbase)
127 WARN("Configuration: %s uses the same virtual ports as %s\n",
128 buffer,PPDeviceList[0].devicename);
129 WARN("Configuration: Rejecting configuration item\n");
134 if (!userbase) continue;
136 /* Check for the minimum required IOCTLS */
137 if ((ioctl(fd,PPRDATA,&res))||
138 (ioctl(fd,PPRCONTROL,&res))||
139 (ioctl(fd,PPRCONTROL,&res)))
141 ERR("PPUSER IOCTL not available for parport device %s\n",temp);
144 if (ioctl (fd,PPRELEASE,0))
146 ERR("PPRELEASE rejected %s\n",buffer);
147 ERR("Perhaps the device is already in use or non-existant\n");
150 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
151 if (!PPDeviceList[nports].devicename)
153 ERR("No (more)space for devicename\n");
156 strcpy(PPDeviceList[nports].devicename,buffer);
157 PPDeviceList[nports].fd = fd;
158 PPDeviceList[nports].userbase = userbase;
159 PPDeviceList[nports].lastaccess=GetTickCount();
162 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
165 WARN("Configuration:Invalid timeout %s in configuration for %s, Setting to 0\n",
167 PPDeviceList[nports].timeout = 0;
171 PPDeviceList[nports].timeout = 0;
174 TRACE("found %d ports\n",nports);
179 /* sort in accending order for userbase for faster access*/
180 qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
184 for (idx= 0;idx<PPDeviceNum; idx++)
185 TRACE("found device %s userbase %x fd %x timeout %d\n",
186 PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
187 PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
189 register a timer callback perhaps every 30 second to release unused ports
190 Set lastaccess = 0 as indicator when port was released
197 * Do the actual IOCTL
198 * Return NULL on success
200 static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
203 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
205 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
206 PPDeviceList[idx].devicename);
209 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
210 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
212 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
213 PPDeviceList[idx].devicename);
222 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
223 * Return NULL on success
225 int IO_pp_inp(int port, DWORD* res)
229 for (idx=0;idx<PPDeviceNum ;idx++)
231 j = port - PPDeviceList[idx].userbase;
236 return IO_pp_do_access(idx,PPRDATA,res);
238 return IO_pp_do_access(idx,PPRSTATUS,res);
240 return IO_pp_do_access(idx,PPRCONTROL,res);
246 FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
247 FIXME("If this is causing problems, try direct port access\n");
258 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
259 * Return NULL on success
261 BOOL IO_pp_outp(int port, DWORD* res)
265 for (idx=0;idx<PPDeviceNum ;idx++)
267 j = port - PPDeviceList[idx].userbase;
272 return IO_pp_do_access(idx,PPWDATA,res);
274 return IO_pp_do_access(idx,PPWCONTROL,res);
281 FIXME("Port %d not accessible for writing with ppdev\n",port);
282 FIXME("If this is causing problems, try direct port access\n");
292 #else /* HAVE_PPDEV */
296 char IO_pp_init(void)
301 int IO_pp_inp(int port, DWORD* res)
306 BOOL IO_pp_outp(int port, DWORD* res)
310 #endif /* HAVE_PPDEV */