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 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
33 #include <linux/ppdev.h>
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(int);
43 typedef struct _PPDEVICESTRUCT
{
44 int fd
; /* NULL if device not available */
46 int userbase
; /* where wine thinks the ports are*/
47 DWORD lastaccess
; /* or NULL if release */
48 int timeout
; /* time in second of inactivity to release the port*/
51 static PPDeviceStruct PPDeviceList
[5];
52 static int PPDeviceNum
=0;
54 static int IO_pp_sort(const void *p1
,const void *p2
)
56 return ((PPDeviceStruct
*)p1
)->userbase
- ((PPDeviceStruct
*)p2
)->userbase
;
61 * Read the ppdev entries from wine.conf, open the device and check
62 * for nescessary IOCTRL
63 * Report verbose about possible errors
70 int i
,idx
=0,fd
,res
,userbase
,nports
=0;
74 OBJECT_ATTRIBUTES attr
;
77 static const WCHAR configW
[] = {'M','a','c','h','i','n','e','\\',
78 'S','o','f','t','w','a','r','e','\\',
81 'C','o','n','f','i','g','\\',
82 'p','p','d','e','v',0};
86 attr
.Length
= sizeof(attr
);
87 attr
.RootDirectory
= 0;
88 attr
.ObjectName
= &nameW
;
90 attr
.SecurityDescriptor
= NULL
;
91 attr
.SecurityQualityOfService
= NULL
;
92 RtlInitUnicodeString( &nameW
, configW
);
94 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) return 1;
98 DWORD total_size
, len
;
100 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)temp
;
102 if (NtEnumerateValueKey( hkey
, idx
, KeyValueFullInformation
,
103 temp
, sizeof(temp
), &total_size
)) break;
104 if (info
->Type
!= REG_SZ
) break;
106 RtlUnicodeToMultiByteN( name
, sizeof(name
)-1, &len
, info
->Name
, info
->NameLength
);
108 RtlUnicodeToMultiByteN( buffer
, sizeof(buffer
)-1, &len
,
109 (WCHAR
*)(temp
+ info
->DataOffset
), total_size
-info
->DataOffset
);
115 FIXME("Make the PPDeviceList larger then 5 elements\n");
118 TRACE("Device '%s' at virtual userbase '%s'\n", buffer
,name
);
119 timeout
= strchr(buffer
,',');
122 fd
=open(buffer
,O_RDWR
);
126 WARN("Configuration: No access to %s Cause: %s\n",buffer
,strerror(lasterror
));
127 WARN("Rejecting configuration item\n");
128 if (lasterror
== ENODEV
)
129 FIXME("Is the ppdev module loaded?\n");
132 userbase
= strtol(name
,(char **)NULL
, 16);
133 if ( errno
== ERANGE
)
135 WARN("Configuration: Invalid base %s for %s\n",name
,buffer
);
136 WARN("Rejecting configuration item\n");
139 if (ioctl (fd
,PPCLAIM
,0))
141 ERR("PPCLAIM rejected %s\n",buffer
);
142 ERR("Perhaps the device is already in use or non-existant\n");
147 for (i
=0; i
<= nports
; i
++)
149 if (PPDeviceList
[i
].userbase
== userbase
)
151 WARN("Configuration: %s uses the same virtual ports as %s\n",
152 buffer
,PPDeviceList
[0].devicename
);
153 WARN("Configuration: Rejecting configuration item\n");
158 if (!userbase
) continue;
160 /* Check for the minimum required IOCTLS */
161 if ((ioctl(fd
,PPRDATA
,&res
))||
162 (ioctl(fd
,PPRCONTROL
,&res
))||
163 (ioctl(fd
,PPRCONTROL
,&res
)))
165 ERR("PPUSER IOCTL not available for parport device %s\n",buffer
);
168 if (ioctl (fd
,PPRELEASE
,0))
170 ERR("PPRELEASE rejected %s\n",buffer
);
171 ERR("Perhaps the device is already in use or non-existant\n");
174 PPDeviceList
[nports
].devicename
= malloc(sizeof(buffer
)+1);
175 if (!PPDeviceList
[nports
].devicename
)
177 ERR("No (more)space for devicename\n");
180 strcpy(PPDeviceList
[nports
].devicename
,buffer
);
181 PPDeviceList
[nports
].fd
= fd
;
182 PPDeviceList
[nports
].userbase
= userbase
;
183 PPDeviceList
[nports
].lastaccess
=GetTickCount();
186 PPDeviceList
[nports
].timeout
= strtol(timeout
,(char **)NULL
, 10);
189 WARN("Configuration:Invalid timeout %s in configuration for %s, Setting to 0\n",
191 PPDeviceList
[nports
].timeout
= 0;
195 PPDeviceList
[nports
].timeout
= 0;
198 TRACE("found %d ports\n",nports
);
203 /* sort in accending order for userbase for faster access*/
204 qsort (PPDeviceList
,PPDeviceNum
,sizeof(PPDeviceStruct
),IO_pp_sort
);
208 for (idx
= 0;idx
<PPDeviceNum
; idx
++)
209 TRACE("found device %s userbase %x fd %x timeout %d\n",
210 PPDeviceList
[idx
].devicename
, PPDeviceList
[idx
].userbase
,
211 PPDeviceList
[idx
].fd
,PPDeviceList
[idx
].timeout
);
213 register a timer callback perhaps every 30 second to release unused ports
214 Set lastaccess = 0 as indicator when port was released
221 * Do the actual IOCTL
222 * Return NULL on success
224 static int IO_pp_do_access(int idx
,int ppctl
, DWORD
* res
)
227 if (ioctl(PPDeviceList
[idx
].fd
,PPCLAIM
,0))
229 ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
230 PPDeviceList
[idx
].devicename
);
233 ret
= ioctl(PPDeviceList
[idx
].fd
,ppctl
,res
);
234 if (ioctl(PPDeviceList
[idx
].fd
,PPRELEASE
,0))
236 ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
237 PPDeviceList
[idx
].devicename
);
246 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
247 * Return NULL on success
249 int IO_pp_inp(int port
, DWORD
* res
)
253 for (idx
=0;idx
<PPDeviceNum
;idx
++)
255 j
= port
- PPDeviceList
[idx
].userbase
;
260 return IO_pp_do_access(idx
,PPRDATA
,res
);
262 return IO_pp_do_access(idx
,PPRSTATUS
,res
);
264 return IO_pp_do_access(idx
,PPRCONTROL
,res
);
270 FIXME("Port 0x%x not accessible for reading with ppdev\n",port
);
271 FIXME("If this is causing problems, try direct port access\n");
282 * Check if we can satisfy the INP command with some of the configured PPDEV deviced
283 * Return NULL on success
285 BOOL
IO_pp_outp(int port
, DWORD
* res
)
289 for (idx
=0;idx
<PPDeviceNum
;idx
++)
291 j
= port
- PPDeviceList
[idx
].userbase
;
296 return IO_pp_do_access(idx
,PPWDATA
,res
);
298 return IO_pp_do_access(idx
,PPWCONTROL
,res
);
305 FIXME("Port %d not accessible for writing with ppdev\n",port
);
306 FIXME("If this is causing problems, try direct port access\n");
316 #else /* HAVE_PPDEV */
320 char IO_pp_init(void)
325 int IO_pp_inp(int port
, DWORD
* res
)
330 BOOL
IO_pp_outp(int port
, DWORD
* res
)
334 #endif /* HAVE_PPDEV */