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>
32 #ifdef HAVE_SYS_IOCTL_H
33 # include <sys/ioctl.h>
36 #include <linux/ppdev.h>
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(int);
48 typedef struct _PPDEVICESTRUCT
{
49 int fd
; /* NULL if device not available */
51 int userbase
; /* where wine thinks the ports are*/
52 DWORD lastaccess
; /* or NULL if release */
53 int timeout
; /* time in second of inactivity to release the port*/
56 static PPDeviceStruct PPDeviceList
[5];
57 static int PPDeviceNum
=0;
59 static int IO_pp_sort(const void *p1
,const void *p2
)
61 return ((PPDeviceStruct
*)p1
)->userbase
- ((PPDeviceStruct
*)p2
)->userbase
;
66 * Read the ppdev entries from wine.conf, open the device and check
67 * for nescessary IOCTRL
68 * Report verbose about possible errors
75 int i
,idx
=0,fd
,res
,userbase
,nports
=0;
79 OBJECT_ATTRIBUTES attr
;
82 static const WCHAR configW
[] = {'M','a','c','h','i','n','e','\\',
83 'S','o','f','t','w','a','r','e','\\',
86 'C','o','n','f','i','g','\\',
87 'p','p','d','e','v',0};
91 attr
.Length
= sizeof(attr
);
92 attr
.RootDirectory
= 0;
93 attr
.ObjectName
= &nameW
;
95 attr
.SecurityDescriptor
= NULL
;
96 attr
.SecurityQualityOfService
= NULL
;
97 RtlInitUnicodeString( &nameW
, configW
);
99 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) return 1;
103 DWORD total_size
, len
;
105 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)temp
;
107 if (NtEnumerateValueKey( hkey
, idx
, KeyValueFullInformation
,
108 temp
, sizeof(temp
), &total_size
)) break;
109 if (info
->Type
!= REG_SZ
) break;
111 RtlUnicodeToMultiByteN( name
, sizeof(name
)-1, &len
, info
->Name
, info
->NameLength
);
113 RtlUnicodeToMultiByteN( buffer
, sizeof(buffer
)-1, &len
,
114 (WCHAR
*)(temp
+ info
->DataOffset
), total_size
-info
->DataOffset
);
120 FIXME("Make the PPDeviceList larger then 5 elements\n");
123 TRACE("Device '%s' at virtual userbase '%s'\n", buffer
,name
);
124 timeout
= strchr(buffer
,',');
127 fd
=open(buffer
,O_RDWR
);
131 WARN("Configuration: No access to %s Cause: %s\n",buffer
,strerror(lasterror
));
132 WARN("Rejecting configuration item\n");
133 if (lasterror
== ENODEV
)
134 FIXME("Is the ppdev module loaded?\n");
137 userbase
= strtol(name
,(char **)NULL
, 16);
138 if ( errno
== ERANGE
)
140 WARN("Configuration: Invalid base %s for %s\n",name
,buffer
);
141 WARN("Rejecting configuration item\n");
144 if (ioctl (fd
,PPCLAIM
,0))
146 ERR("PPCLAIM rejected %s\n",buffer
);
147 ERR("Perhaps the device is already in use or non-existent\n");
152 for (i
=0; i
<= nports
; i
++)
154 if (PPDeviceList
[i
].userbase
== userbase
)
156 WARN("Configuration: %s uses the same virtual ports as %s\n",
157 buffer
,PPDeviceList
[0].devicename
);
158 WARN("Configuration: Rejecting configuration item\n");
163 if (!userbase
) continue;
165 /* Check for the minimum required IOCTLS */
166 if ((ioctl(fd
,PPRDATA
,&res
))||
167 (ioctl(fd
,PPRCONTROL
,&res
))||
168 (ioctl(fd
,PPRCONTROL
,&res
)))
170 ERR("PPUSER IOCTL not available for parport device %s\n",buffer
);
173 if (ioctl (fd
,PPRELEASE
,0))
175 ERR("PPRELEASE rejected %s\n",buffer
);
176 ERR("Perhaps the device is already in use or non-existent\n");
179 PPDeviceList
[nports
].devicename
= malloc(sizeof(buffer
)+1);
180 if (!PPDeviceList
[nports
].devicename
)
182 ERR("No (more)space for devicename\n");
185 strcpy(PPDeviceList
[nports
].devicename
,buffer
);
186 PPDeviceList
[nports
].fd
= fd
;
187 PPDeviceList
[nports
].userbase
= userbase
;
188 PPDeviceList
[nports
].lastaccess
=GetTickCount();
191 PPDeviceList
[nports
].timeout
= strtol(timeout
,(char **)NULL
, 10);
194 WARN("Configuration:Invalid timeout %s in configuration for %s, Setting to 0\n",
196 PPDeviceList
[nports
].timeout
= 0;
200 PPDeviceList
[nports
].timeout
= 0;
203 TRACE("found %d ports\n",nports
);
208 /* sort in accending order for userbase for faster access*/
209 qsort (PPDeviceList
,PPDeviceNum
,sizeof(PPDeviceStruct
),IO_pp_sort
);
213 for (idx
= 0;idx
<PPDeviceNum
; idx
++)
214 TRACE("found device %s userbase %x fd %x timeout %d\n",
215 PPDeviceList
[idx
].devicename
, PPDeviceList
[idx
].userbase
,
216 PPDeviceList
[idx
].fd
,PPDeviceList
[idx
].timeout
);
218 register a timer callback perhaps every 30 second to release unused ports
219 Set lastaccess = 0 as indicator when port was released
226 * Do the actual IOCTL
227 * Return NULL on success
229 static int IO_pp_do_access(int idx
,int ppctl
, DWORD
* res
)
232 if (ioctl(PPDeviceList
[idx
].fd
,PPCLAIM
,0))
234 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
235 PPDeviceList
[idx
].devicename
);
238 ret
= ioctl(PPDeviceList
[idx
].fd
,ppctl
,res
);
239 if (ioctl(PPDeviceList
[idx
].fd
,PPRELEASE
,0))
241 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
242 PPDeviceList
[idx
].devicename
);
251 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
252 * Return NULL on success
254 int IO_pp_inp(int port
, DWORD
* res
)
258 for (idx
=0;idx
<PPDeviceNum
;idx
++)
260 j
= port
- PPDeviceList
[idx
].userbase
;
265 return IO_pp_do_access(idx
,PPRDATA
,res
);
267 return IO_pp_do_access(idx
,PPRSTATUS
,res
);
269 return IO_pp_do_access(idx
,PPRCONTROL
,res
);
275 FIXME("Port 0x%x not accessible for reading with ppdev\n",port
);
276 FIXME("If this is causing problems, try direct port access\n");
287 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
288 * Return NULL on success
290 BOOL
IO_pp_outp(int port
, DWORD
* res
)
294 for (idx
=0;idx
<PPDeviceNum
;idx
++)
296 j
= port
- PPDeviceList
[idx
].userbase
;
301 return IO_pp_do_access(idx
,PPWDATA
,res
);
303 return IO_pp_do_access(idx
,PPWCONTROL
,res
);
310 FIXME("Port %d not accessible for writing with ppdev\n",port
);
311 FIXME("If this is causing problems, try direct port access\n");
321 #else /* HAVE_PPDEV */
324 char IO_pp_init(void)
329 int IO_pp_inp(int port
, DWORD
* res
)
334 BOOL
IO_pp_outp(int port
, DWORD
* res
)
338 #endif /* HAVE_PPDEV */