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>
33 #ifdef HAVE_SYS_IOCTL_H
34 # include <sys/ioctl.h>
36 #ifdef HAVE_LINUX_IOCTL_H
37 # include <linux/ioctl.h>
39 #include <linux/ppdev.h>
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(int);
51 typedef struct _PPDEVICESTRUCT
{
52 int fd
; /* NULL if device not available */
54 int userbase
; /* where wine thinks the ports are */
55 DWORD lastaccess
; /* or NULL if release */
56 int timeout
; /* time in second of inactivity to release the port */
59 static PPDeviceStruct PPDeviceList
[5];
60 static int PPDeviceNum
=0;
62 static int IO_pp_sort(const void *p1
,const void *p2
)
64 return ((PPDeviceStruct
*)p1
)->userbase
- ((PPDeviceStruct
*)p2
)->userbase
;
69 * Read the ppdev entries from wine.conf, open the device and check
70 * for necessary IOCTRL
71 * Report verbose about possible errors
78 int i
,idx
=0,fd
,res
,userbase
,nports
=0;
82 OBJECT_ATTRIBUTES attr
;
85 static const WCHAR configW
[] = {'M','a','c','h','i','n','e','\\',
86 'S','o','f','t','w','a','r','e','\\',
89 'C','o','n','f','i','g','\\',
90 'p','p','d','e','v',0};
94 attr
.Length
= sizeof(attr
);
95 attr
.RootDirectory
= 0;
96 attr
.ObjectName
= &nameW
;
98 attr
.SecurityDescriptor
= NULL
;
99 attr
.SecurityQualityOfService
= NULL
;
100 RtlInitUnicodeString( &nameW
, configW
);
102 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) return 1;
106 DWORD total_size
, len
;
108 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)temp
;
110 if (NtEnumerateValueKey( hkey
, idx
, KeyValueFullInformation
,
111 temp
, sizeof(temp
), &total_size
)) break;
112 if (info
->Type
!= REG_SZ
) break;
114 RtlUnicodeToMultiByteN( name
, sizeof(name
)-1, &len
, info
->Name
, info
->NameLength
);
116 RtlUnicodeToMultiByteN( buffer
, sizeof(buffer
)-1, &len
,
117 (WCHAR
*)(temp
+ info
->DataOffset
), total_size
-info
->DataOffset
);
123 FIXME("Make the PPDeviceList larger than 5 elements\n");
126 TRACE("Device '%s' at virtual userbase '%s'\n", buffer
,name
);
127 timeout
= strchr(buffer
,',');
130 fd
=open(buffer
,O_RDWR
);
134 WARN("Configuration: No access to %s Cause: %s\n",buffer
,strerror(lasterror
));
135 WARN("Rejecting configuration item\n");
136 if (lasterror
== ENODEV
)
137 ERR("Is the ppdev module loaded?\n");
140 userbase
= strtol(name
,(char **)NULL
, 16);
141 if ( errno
== ERANGE
)
143 WARN("Configuration: Invalid base %s for %s\n",name
,buffer
);
144 WARN("Rejecting configuration item\n");
147 if (ioctl (fd
,PPCLAIM
,0))
149 ERR("PPCLAIM rejected %s\n",buffer
);
150 ERR("Perhaps the device is already in use or non-existent\n");
155 for (i
=0; i
<= nports
; i
++)
157 if (PPDeviceList
[i
].userbase
== userbase
)
159 WARN("Configuration: %s uses the same virtual ports as %s\n",
160 buffer
,PPDeviceList
[0].devicename
);
161 WARN("Configuration: Rejecting configuration item\n");
166 if (!userbase
) continue;
168 /* Check for the minimum required IOCTLS */
169 if ((ioctl(fd
,PPRDATA
,&res
))||
170 (ioctl(fd
,PPRCONTROL
,&res
))||
171 (ioctl(fd
,PPRCONTROL
,&res
)))
173 ERR("PPUSER IOCTL not available for parport device %s\n",buffer
);
176 if (ioctl (fd
,PPRELEASE
,0))
178 ERR("PPRELEASE rejected %s\n",buffer
);
179 ERR("Perhaps the device is already in use or non-existent\n");
182 PPDeviceList
[nports
].devicename
= malloc(sizeof(buffer
)+1);
183 if (!PPDeviceList
[nports
].devicename
)
185 ERR("No (more) space for devicename\n");
188 strcpy(PPDeviceList
[nports
].devicename
,buffer
);
189 PPDeviceList
[nports
].fd
= fd
;
190 PPDeviceList
[nports
].userbase
= userbase
;
191 PPDeviceList
[nports
].lastaccess
=GetTickCount();
194 PPDeviceList
[nports
].timeout
= strtol(timeout
,(char **)NULL
, 10);
197 WARN("Configuration: Invalid timeout %s in configuration for %s, Setting to 0\n",
199 PPDeviceList
[nports
].timeout
= 0;
203 PPDeviceList
[nports
].timeout
= 0;
206 TRACE("found %d ports\n",nports
);
211 /* sort in ascending order for userbase for faster access */
212 qsort (PPDeviceList
,PPDeviceNum
,sizeof(PPDeviceStruct
),IO_pp_sort
);
216 for (idx
= 0;idx
<PPDeviceNum
; idx
++)
217 TRACE("found device %s userbase %x fd %x timeout %d\n",
218 PPDeviceList
[idx
].devicename
, PPDeviceList
[idx
].userbase
,
219 PPDeviceList
[idx
].fd
,PPDeviceList
[idx
].timeout
);
221 register a timer callback perhaps every 30 seconds to release unused ports
222 Set lastaccess = 0 as indicator when port was released
229 * Do the actual IOCTL
230 * Return NULL on success
232 static int IO_pp_do_access(int idx
,int ppctl
, DWORD
* res
)
235 if (ioctl(PPDeviceList
[idx
].fd
,PPCLAIM
,0))
237 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
238 PPDeviceList
[idx
].devicename
);
241 ret
= ioctl(PPDeviceList
[idx
].fd
,ppctl
,res
);
242 if (ioctl(PPDeviceList
[idx
].fd
,PPRELEASE
,0))
244 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
245 PPDeviceList
[idx
].devicename
);
254 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
255 * Return NULL on success
257 int IO_pp_inp(int port
, DWORD
* res
)
261 for (idx
=0;idx
<PPDeviceNum
;idx
++)
263 j
= port
- PPDeviceList
[idx
].userbase
;
268 return IO_pp_do_access(idx
,PPRDATA
,res
);
270 return IO_pp_do_access(idx
,PPRSTATUS
,res
);
272 return IO_pp_do_access(idx
,PPRCONTROL
,res
);
278 FIXME("Port 0x%x not accessible for reading with ppdev\n",port
);
279 FIXME("If this is causing problems, try direct port access\n");
290 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
291 * Return NULL on success
293 BOOL
IO_pp_outp(int port
, DWORD
* res
)
297 for (idx
=0;idx
<PPDeviceNum
;idx
++)
299 j
= port
- PPDeviceList
[idx
].userbase
;
304 return IO_pp_do_access(idx
,PPWDATA
,res
);
307 /* We can't switch port direction via PPWCONTROL,
308 so do it via PPDATADIR
310 DWORD mode
= *res
& 0x20;
311 IO_pp_do_access(idx
,PPDATADIR
,&mode
);
312 mode
= (*res
& ~0x20);
313 return IO_pp_do_access(idx
,PPWCONTROL
,&mode
);
322 FIXME("Port %d not accessible for writing with ppdev\n",port
);
323 FIXME("If this is causing problems, try direct port access\n");
333 #else /* HAVE_PPDEV */
336 char IO_pp_init(void)
341 int IO_pp_inp(int port
, DWORD
* res
)
346 BOOL
IO_pp_outp(int port
, DWORD
* res
)
350 #endif /* HAVE_PPDEV */