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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
39 #ifdef HAVE_LINUX_IOCTL_H
40 # include <linux/ioctl.h>
42 #include <linux/ppdev.h>
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(int);
53 typedef struct _PPDEVICESTRUCT{
54 int fd; /* NULL if device not available */
56 int userbase; /* where wine thinks the ports are */
57 DWORD lastaccess; /* or NULL if release */
58 int timeout; /* time in second of inactivity to release the port */
61 static PPDeviceStruct PPDeviceList[5];
62 static int PPDeviceNum=0;
64 static int IO_pp_sort(const void *p1,const void *p2)
66 return ((const PPDeviceStruct*)p1)->userbase - ((const PPDeviceStruct*)p2)->userbase;
71 * Read the ppdev entries from wine.conf, open the device and check
72 * for necessary IOCTRL
73 * Report verbose about possible errors
80 int i,idx=0,fd,res,userbase,nports=0;
84 OBJECT_ATTRIBUTES attr;
87 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
88 'W','i','n','e','\\','V','D','M','\\','p','p','d','e','v',0};
92 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
93 attr.Length = sizeof(attr);
94 attr.RootDirectory = root;
95 attr.ObjectName = &nameW;
97 attr.SecurityDescriptor = NULL;
98 attr.SecurityQualityOfService = NULL;
99 RtlInitUnicodeString( &nameW, configW );
101 /* @@ Wine registry key: HKCU\Software\Wine\VDM\ppdev */
102 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
108 DWORD total_size, len;
110 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)temp;
112 if (NtEnumerateValueKey( hkey, idx, KeyValueFullInformation,
113 temp, sizeof(temp), &total_size )) break;
114 if (info->Type != REG_SZ) break;
116 RtlUnicodeToMultiByteN( name, sizeof(name)-1, &len, info->Name, info->NameLength );
118 RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
119 (WCHAR *)(temp + info->DataOffset), total_size-info->DataOffset );
125 FIXME("Make the PPDeviceList larger than 5 elements\n");
128 TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
129 timeout = strchr(buffer,',');
132 fd=open(buffer,O_RDWR);
136 WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
137 WARN("Rejecting configuration item\n");
138 if (lasterror == ENODEV)
139 ERR("Is the ppdev module loaded?\n");
142 userbase = strtol(name,(char **)NULL, 16);
143 if ( errno == ERANGE)
145 WARN("Configuration: Invalid base %s for %s\n",name,buffer);
146 WARN("Rejecting configuration item\n");
149 if (ioctl (fd,PPCLAIM,0))
151 ERR("PPCLAIM rejected %s\n",buffer);
152 ERR("Perhaps the device is already in use or nonexistent\n");
157 for (i=0; i<= nports; i++)
159 if (PPDeviceList[i].userbase == userbase)
161 WARN("Configuration: %s uses the same virtual ports as %s\n",
162 buffer,PPDeviceList[0].devicename);
163 WARN("Configuration: Rejecting configuration item\n");
168 if (!userbase) continue;
170 /* Check for the minimum required IOCTLS */
171 if ((ioctl(fd,PPRDATA,&res))||
172 (ioctl(fd,PPRSTATUS,&res))||
173 (ioctl(fd,PPRCONTROL,&res)))
175 ERR("PPUSER IOCTL not available for parport device %s\n",buffer);
178 if (ioctl (fd,PPRELEASE,0))
180 ERR("PPRELEASE rejected %s\n",buffer);
181 ERR("Perhaps the device is already in use or nonexistent\n");
184 PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
185 if (!PPDeviceList[nports].devicename)
187 ERR("No (more) space for devicename\n");
190 strcpy(PPDeviceList[nports].devicename,buffer);
191 PPDeviceList[nports].fd = fd;
192 PPDeviceList[nports].userbase = userbase;
193 PPDeviceList[nports].lastaccess=GetTickCount();
196 PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
199 WARN("Configuration: Invalid timeout %s in configuration for %s, Setting to 0\n",
201 PPDeviceList[nports].timeout = 0;
205 PPDeviceList[nports].timeout = 0;
208 TRACE("found %d ports\n",nports);
213 /* sort in ascending order for userbase for faster access */
214 qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
218 for (idx= 0;idx<PPDeviceNum; idx++)
219 TRACE("found device %s userbase %x fd %x timeout %d\n",
220 PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
221 PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
223 register a timer callback perhaps every 30 seconds to release unused ports
224 Set lastaccess = 0 as indicator when port was released
231 * Do the actual IOCTL
232 * Return NULL on success
234 static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
237 if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
239 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
240 PPDeviceList[idx].devicename);
243 ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
244 if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
246 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
247 PPDeviceList[idx].devicename);
256 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
257 * Return NULL on success
259 int IO_pp_inp(int port, DWORD* res)
263 for (idx=0;idx<PPDeviceNum ;idx++)
265 j = port - PPDeviceList[idx].userbase;
270 return IO_pp_do_access(idx,PPRDATA,res);
272 return IO_pp_do_access(idx,PPRSTATUS,res);
274 return IO_pp_do_access(idx,PPRCONTROL,res);
280 FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
281 FIXME("If this is causing problems, try direct port access\n");
292 * Check if we can satisfy the OUTP command with some of the configured PPDEV deviced
293 * Return NULL on success
295 BOOL IO_pp_outp(int port, DWORD* res)
299 for (idx=0;idx<PPDeviceNum ;idx++)
301 j = port - PPDeviceList[idx].userbase;
306 return IO_pp_do_access(idx,PPWDATA,res);
309 /* We can't switch port direction via PPWCONTROL,
310 so do it via PPDATADIR
312 DWORD mode = *res & 0x20;
313 IO_pp_do_access(idx,PPDATADIR,&mode);
314 mode = (*res & ~0x20);
315 return IO_pp_do_access(idx,PPWCONTROL,&mode);
324 FIXME("Port %d not accessible for writing with ppdev\n",port);
325 FIXME("If this is causing problems, try direct port access\n");
335 #else /* HAVE_PPDEV */
338 char IO_pp_init(void)
343 int IO_pp_inp(int port, DWORD* res)
348 BOOL IO_pp_outp(int port, DWORD* res)
352 #endif /* HAVE_PPDEV */