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 #include <sys/ioctl.h>
31 #include <linux/ppdev.h>
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(int);
42 typedef struct _PPDEVICESTRUCT{
43 int fd; /* NULL if device not available */
45 int userbase; /* where wine thinks the ports are*/
46 DWORD lastaccess; /* or NULL if release */
47 int timeout; /* time in second of inactivity to release the port*/
50 static PPDeviceStruct PPDeviceList[5];
51 static int PPDeviceNum=0;
53 static int IO_pp_sort(const void *p1,const void *p2)
55 return ((PPDeviceStruct*)p1)->userbase - ((PPDeviceStruct*)p2)->userbase;
60 * Read the ppdev entries from wine.conf, open the device and check
61 * for nescessary IOCTRL
62 * Report verbose about possible errors
70 int i,idx=0,fd,res,userbase,nports=0;
76 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppdev", &hkey ) != ERROR_SUCCESS)
81 DWORD type, count = sizeof(buffer), name_len = sizeof(name);
83 if (RegEnumValueA( hkey, idx, name, &name_len, NULL, &type, buffer, &count )!= ERROR_SUCCESS)
89 FIXME("Make the PPDeviceList larger then 5 elements\n");
92 TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
93 timeout = strchr(buffer,',');
96 fd=open(buffer,O_RDWR);
100 WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
101 WARN("Rejecting configuration item\n");
102 if (lasterror == ENODEV)
103 FIXME("Is the ppdev module loaded?\n");
106 userbase = strtol(name,(char **)NULL, 16);
107 if ( errno == ERANGE)
109 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
110 WARN("Rejecting configuration item\n");
113 if (ioctl (fd,PPCLAIM,0))
115 ERR("PPCLAIM rejected %s\n",buffer);
116 ERR("Perhaps the device is already in use or non-existant\n");
121 for (i=0; i<= nports; i++)
123 if (PPDeviceList[i].userbase == userbase)
125 WARN("Configuration: %s uses the same virtual ports as %s\n",
126 buffer,PPDeviceList[0].devicename);
127 WARN("Configuration: Rejecting configuration item");
132 if (!userbase) continue;
134 /* Check for the minimum required IOCTLS */
135 if ((ioctl(fd,PPRDATA,&res))||
136 (ioctl(fd,PPRCONTROL,&res))||
137 (ioctl(fd,PPRCONTROL,&res)))
139 ERR("PPUSER IOCTL not available for parport device %s\n",temp);
142 if (ioctl (fd,PPRELEASE,0))
144 ERR("PPRELEASE rejected %s\n",buffer);
145 ERR("Perhaps the device is already in use or non-existant\n");
148 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
149 if (!PPDeviceList[nports].devicename)
151 ERR("No (more)space for devicename\n");
154 strcpy(PPDeviceList[nports].devicename,buffer);
155 PPDeviceList[nports].fd = fd;
156 PPDeviceList[nports].userbase = userbase;
157 PPDeviceList[nports].lastaccess=GetTickCount();
160 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
163 WARN("Configuration:Invalid timeout %s in configuration for %s, Setting to 0\n",
165 PPDeviceList[nports].timeout = 0;
169 PPDeviceList[nports].timeout = 0;
172 TRACE("found %d ports\n",nports);
177 /* sort in accending order for userbase for faster access*/
178 qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
182 for (idx= 0;idx<PPDeviceNum; idx++)
183 TRACE("found device %s userbase %x fd %x timeout %d\n",
184 PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
185 PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
187 register a timer callback perhaps every 30 second to release unused ports
188 Set lastaccess = 0 as indicator when port was released
195 * Do the actual IOCTL
196 * Return NULL on success
198 static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
201 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
203 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
204 PPDeviceList[idx].devicename);
207 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
208 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
210 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
211 PPDeviceList[idx].devicename);
220 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
221 * Return NULL on success
223 int IO_pp_inp(int port, DWORD* res)
227 for (idx=0;idx<PPDeviceNum ;idx++)
229 j = port - PPDeviceList[idx].userbase;
234 return IO_pp_do_access(idx,PPRDATA,res);
236 return IO_pp_do_access(idx,PPRSTATUS,res);
238 return IO_pp_do_access(idx,PPRCONTROL,res);
244 FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
245 FIXME("If this is causing problems, try direct port access\n");
256 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
257 * Return NULL on success
259 BOOL IO_pp_outp(int port, DWORD* res)
263 for (idx=0;idx<PPDeviceNum ;idx++)
265 j = port - PPDeviceList[idx].userbase;
270 return IO_pp_do_access(idx,PPWDATA,res);
272 return IO_pp_do_access(idx,PPWCONTROL,res);
279 FIXME("Port %d not accessible for writing with ppdev\n",port);
280 FIXME("If this is causing problems, try direct port access\n");
290 #else /* HAVE_PPDEV */
294 char IO_pp_init(void)
299 int IO_pp_inp(int port, DWORD* res)
304 BOOL IO_pp_outp(int port, DWORD* res)
308 #endif /* HAVE_PPDEV */