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
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_STAT_H
31 # include <sys/stat.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
38 #ifdef HAVE_LINUX_IOCTL_H
39 # include <linux/ioctl.h>
41 #include <linux/ppdev.h>
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(int);
52 typedef struct _PPDEVICESTRUCT{
53 int fd; /* NULL if device not available */
55 int userbase; /* where wine thinks the ports are */
56 DWORD lastaccess; /* or NULL if release */
57 int timeout; /* time in second of inactivity to release the port */
60 static PPDeviceStruct PPDeviceList[5];
61 static int PPDeviceNum=0;
63 static int IO_pp_sort(const void *p1,const void *p2)
65 return ((const PPDeviceStruct*)p1)->userbase - ((const PPDeviceStruct*)p2)->userbase;
70 * Read the ppdev entries from wine.conf, open the device and check
71 * for necessary IOCTRL
72 * Report verbose about possible errors
79 int i,idx=0,fd,res,userbase,nports=0;
83 OBJECT_ATTRIBUTES attr;
86 static const WCHAR configW[] = {'M','a','c','h','i','n','e','\\',
87 'S','o','f','t','w','a','r','e','\\',
90 'C','o','n','f','i','g','\\',
91 'p','p','d','e','v',0};
95 attr.Length = sizeof(attr);
96 attr.RootDirectory = 0;
97 attr.ObjectName = &nameW;
99 attr.SecurityDescriptor = NULL;
100 attr.SecurityQualityOfService = NULL;
101 RtlInitUnicodeString( &nameW, configW );
103 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) return 1;
107 DWORD total_size, len;
109 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)temp;
111 if (NtEnumerateValueKey( hkey, idx, KeyValueFullInformation,
112 temp, sizeof(temp), &total_size )) break;
113 if (info->Type != REG_SZ) break;
115 RtlUnicodeToMultiByteN( name, sizeof(name)-1, &len, info->Name, info->NameLength );
117 RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
118 (WCHAR *)(temp + info->DataOffset), total_size-info->DataOffset );
124 FIXME("Make the PPDeviceList larger than 5 elements\n");
127 TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
128 timeout = strchr(buffer,',');
131 fd=open(buffer,O_RDWR);
135 WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
136 WARN("Rejecting configuration item\n");
137 if (lasterror == ENODEV)
138 ERR("Is the ppdev module loaded?\n");
141 userbase = strtol(name,(char **)NULL, 16);
142 if ( errno == ERANGE)
144 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
145 WARN("Rejecting configuration item\n");
148 if (ioctl (fd,PPCLAIM,0))
150 ERR("PPCLAIM rejected %s\n",buffer);
151 ERR("Perhaps the device is already in use or nonexistent\n");
156 for (i=0; i<= nports; i++)
158 if (PPDeviceList[i].userbase == userbase)
160 WARN("Configuration: %s uses the same virtual ports as %s\n",
161 buffer,PPDeviceList[0].devicename);
162 WARN("Configuration: Rejecting configuration item\n");
167 if (!userbase) continue;
169 /* Check for the minimum required IOCTLS */
170 if ((ioctl(fd,PPRDATA,&res))||
171 (ioctl(fd,PPRCONTROL,&res))||
172 (ioctl(fd,PPRCONTROL,&res)))
174 ERR("PPUSER IOCTL not available for parport device %s\n",buffer);
177 if (ioctl (fd,PPRELEASE,0))
179 ERR("PPRELEASE rejected %s\n",buffer);
180 ERR("Perhaps the device is already in use or nonexistent\n");
183 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
184 if (!PPDeviceList[nports].devicename)
186 ERR("No (more) space for devicename\n");
189 strcpy(PPDeviceList[nports].devicename,buffer);
190 PPDeviceList[nports].fd = fd;
191 PPDeviceList[nports].userbase = userbase;
192 PPDeviceList[nports].lastaccess=GetTickCount();
195 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
198 WARN("Configuration: Invalid timeout %s in configuration for %s, Setting to 0\n",
200 PPDeviceList[nports].timeout = 0;
204 PPDeviceList[nports].timeout = 0;
207 TRACE("found %d ports\n",nports);
212 /* sort in ascending order for userbase for faster access */
213 qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
217 for (idx= 0;idx<PPDeviceNum; idx++)
218 TRACE("found device %s userbase %x fd %x timeout %d\n",
219 PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
220 PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
222 register a timer callback perhaps every 30 seconds to release unused ports
223 Set lastaccess = 0 as indicator when port was released
230 * Do the actual IOCTL
231 * Return NULL on success
233 static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
236 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
238 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
239 PPDeviceList[idx].devicename);
242 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
243 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
245 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
246 PPDeviceList[idx].devicename);
255 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
256 * Return NULL on success
258 int IO_pp_inp(int port, DWORD* res)
262 for (idx=0;idx<PPDeviceNum ;idx++)
264 j = port - PPDeviceList[idx].userbase;
269 return IO_pp_do_access(idx,PPRDATA,res);
271 return IO_pp_do_access(idx,PPRSTATUS,res);
273 return IO_pp_do_access(idx,PPRCONTROL,res);
279 FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
280 FIXME("If this is causing problems, try direct port access\n");
291 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
292 * Return NULL on success
294 BOOL IO_pp_outp(int port, DWORD* res)
298 for (idx=0;idx<PPDeviceNum ;idx++)
300 j = port - PPDeviceList[idx].userbase;
305 return IO_pp_do_access(idx,PPWDATA,res);
308 /* We can't switch port direction via PPWCONTROL,
309 so do it via PPDATADIR
311 DWORD mode = *res & 0x20;
312 IO_pp_do_access(idx,PPDATADIR,&mode);
313 mode = (*res & ~0x20);
314 return IO_pp_do_access(idx,PPWCONTROL,&mode);
323 FIXME("Port %d not accessible for writing with ppdev\n",port);
324 FIXME("If this is causing problems, try direct port access\n");
334 #else /* HAVE_PPDEV */
337 char IO_pp_init(void)
342 int IO_pp_inp(int port, DWORD* res)
347 BOOL IO_pp_outp(int port, DWORD* res)
351 #endif /* HAVE_PPDEV */