2001-06-26 23:06:07 +02:00
|
|
|
/*
|
2003-09-11 03:03:47 +02:00
|
|
|
* Parallel port device support
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* Copyright 2001 Uwe Bonnes
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-06-26 23:06:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
|
|
|
|
2001-07-18 23:04:23 +02:00
|
|
|
#ifdef HAVE_PPDEV
|
2001-06-26 23:06:07 +02:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2001-06-26 23:06:07 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2005-05-06 18:22:54 +02:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
2001-06-26 23:06:07 +02:00
|
|
|
#include <fcntl.h>
|
2004-03-01 22:32:02 +01:00
|
|
|
#include <errno.h>
|
2002-05-22 03:55:18 +02:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
2004-03-01 22:32:02 +01:00
|
|
|
#ifdef HAVE_LINUX_IOCTL_H
|
|
|
|
# include <linux/ioctl.h>
|
|
|
|
#endif
|
2001-06-26 23:06:07 +02:00
|
|
|
#include <linux/ppdev.h>
|
|
|
|
|
|
|
|
#include "winerror.h"
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winreg.h"
|
2002-09-13 19:47:44 +02:00
|
|
|
#include "winternl.h"
|
2001-07-18 23:04:23 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
2001-07-18 23:04:23 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(int);
|
2001-06-26 23:06:07 +02:00
|
|
|
|
|
|
|
typedef struct _PPDEVICESTRUCT{
|
|
|
|
int fd; /* NULL if device not available */
|
|
|
|
char *devicename;
|
2003-09-11 03:03:47 +02:00
|
|
|
int userbase; /* where wine thinks the ports are */
|
2001-06-26 23:06:07 +02:00
|
|
|
DWORD lastaccess; /* or NULL if release */
|
2003-09-11 03:03:47 +02:00
|
|
|
int timeout; /* time in second of inactivity to release the port */
|
2001-06-26 23:06:07 +02:00
|
|
|
} PPDeviceStruct;
|
|
|
|
|
|
|
|
static PPDeviceStruct PPDeviceList[5];
|
|
|
|
static int PPDeviceNum=0;
|
|
|
|
|
|
|
|
static int IO_pp_sort(const void *p1,const void *p2)
|
|
|
|
{
|
2004-11-30 22:38:57 +01:00
|
|
|
return ((const PPDeviceStruct*)p1)->userbase - ((const PPDeviceStruct*)p2)->userbase;
|
2001-06-26 23:06:07 +02:00
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/* IO_pp_init
|
2001-06-26 23:06:07 +02:00
|
|
|
*
|
|
|
|
* Read the ppdev entries from wine.conf, open the device and check
|
2003-09-11 03:03:47 +02:00
|
|
|
* for necessary IOCTRL
|
2001-06-26 23:06:07 +02:00
|
|
|
* Report verbose about possible errors
|
|
|
|
*/
|
|
|
|
char IO_pp_init(void)
|
|
|
|
{
|
|
|
|
char name[80];
|
2002-09-13 19:47:44 +02:00
|
|
|
char buffer[256];
|
2005-06-27 20:48:24 +02:00
|
|
|
HANDLE root, hkey;
|
2001-06-26 23:06:07 +02:00
|
|
|
int i,idx=0,fd,res,userbase,nports=0;
|
|
|
|
char * timeout;
|
|
|
|
char ret=1;
|
|
|
|
int lasterror;
|
2002-09-13 19:47:44 +02:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
UNICODE_STRING nameW;
|
|
|
|
|
2005-06-27 20:48:24 +02:00
|
|
|
static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
|
|
|
|
'W','i','n','e','\\','V','D','M','\\','p','p','d','e','v',0};
|
2001-06-26 23:06:07 +02:00
|
|
|
|
|
|
|
TRACE("\n");
|
2002-09-13 19:47:44 +02:00
|
|
|
|
2005-06-27 20:48:24 +02:00
|
|
|
RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
|
2002-09-13 19:47:44 +02:00
|
|
|
attr.Length = sizeof(attr);
|
2005-06-27 20:48:24 +02:00
|
|
|
attr.RootDirectory = root;
|
2002-09-13 19:47:44 +02:00
|
|
|
attr.ObjectName = &nameW;
|
|
|
|
attr.Attributes = 0;
|
|
|
|
attr.SecurityDescriptor = NULL;
|
|
|
|
attr.SecurityQualityOfService = NULL;
|
|
|
|
RtlInitUnicodeString( &nameW, configW );
|
|
|
|
|
2005-06-27 20:48:24 +02:00
|
|
|
/* @@ Wine registry key: HKCU\Software\Wine\VDM\ppdev */
|
|
|
|
if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
|
|
|
|
NtClose( root );
|
|
|
|
if (!hkey) return 1;
|
2001-06-26 23:06:07 +02:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2002-09-13 19:47:44 +02:00
|
|
|
DWORD total_size, len;
|
|
|
|
char temp[256];
|
|
|
|
KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)temp;
|
|
|
|
|
|
|
|
if (NtEnumerateValueKey( hkey, idx, KeyValueFullInformation,
|
|
|
|
temp, sizeof(temp), &total_size )) break;
|
|
|
|
if (info->Type != REG_SZ) break;
|
2001-06-26 23:06:07 +02:00
|
|
|
|
2002-09-13 19:47:44 +02:00
|
|
|
RtlUnicodeToMultiByteN( name, sizeof(name)-1, &len, info->Name, info->NameLength );
|
|
|
|
name[len] = 0;
|
|
|
|
RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
|
|
|
|
(WCHAR *)(temp + info->DataOffset), total_size-info->DataOffset );
|
|
|
|
buffer[len] = 0;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2001-06-26 23:06:07 +02:00
|
|
|
idx++;
|
|
|
|
if(nports >4)
|
|
|
|
{
|
2003-09-11 03:03:47 +02:00
|
|
|
FIXME("Make the PPDeviceList larger than 5 elements\n");
|
2001-06-26 23:06:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name);
|
|
|
|
timeout = strchr(buffer,',');
|
|
|
|
if (timeout)
|
|
|
|
*timeout++=0;
|
|
|
|
fd=open(buffer,O_RDWR);
|
|
|
|
lasterror=errno;
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror));
|
|
|
|
WARN("Rejecting configuration item\n");
|
|
|
|
if (lasterror == ENODEV)
|
2003-09-11 03:03:47 +02:00
|
|
|
ERR("Is the ppdev module loaded?\n");
|
2001-06-26 23:06:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
userbase = strtol(name,(char **)NULL, 16);
|
|
|
|
if ( errno == ERANGE)
|
|
|
|
{
|
|
|
|
WARN("Configuration: Invalid base %s for %s\n",name,buffer);
|
|
|
|
WARN("Rejecting configuration item\n");
|
|
|
|
continue;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
if (ioctl (fd,PPCLAIM,0))
|
2001-06-26 23:06:07 +02:00
|
|
|
{
|
|
|
|
ERR("PPCLAIM rejected %s\n",buffer);
|
2005-02-10 20:19:35 +01:00
|
|
|
ERR("Perhaps the device is already in use or nonexistent\n");
|
2001-06-26 23:06:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (nports > 0)
|
|
|
|
{
|
|
|
|
for (i=0; i<= nports; i++)
|
|
|
|
{
|
|
|
|
if (PPDeviceList[i].userbase == userbase)
|
|
|
|
{
|
|
|
|
WARN("Configuration: %s uses the same virtual ports as %s\n",
|
|
|
|
buffer,PPDeviceList[0].devicename);
|
2002-05-24 23:17:59 +02:00
|
|
|
WARN("Configuration: Rejecting configuration item\n");
|
2001-06-26 23:06:07 +02:00
|
|
|
userbase = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!userbase) continue;
|
|
|
|
}
|
|
|
|
/* Check for the minimum required IOCTLS */
|
|
|
|
if ((ioctl(fd,PPRDATA,&res))||
|
|
|
|
(ioctl(fd,PPRCONTROL,&res))||
|
|
|
|
(ioctl(fd,PPRCONTROL,&res)))
|
|
|
|
{
|
2002-09-13 19:47:44 +02:00
|
|
|
ERR("PPUSER IOCTL not available for parport device %s\n",buffer);
|
2001-06-26 23:06:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
if (ioctl (fd,PPRELEASE,0))
|
2001-07-14 02:46:22 +02:00
|
|
|
{
|
|
|
|
ERR("PPRELEASE rejected %s\n",buffer);
|
2005-02-10 20:19:35 +01:00
|
|
|
ERR("Perhaps the device is already in use or nonexistent\n");
|
2001-07-14 02:46:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
2001-06-26 23:06:07 +02:00
|
|
|
PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1);
|
|
|
|
if (!PPDeviceList[nports].devicename)
|
|
|
|
{
|
2003-09-11 03:03:47 +02:00
|
|
|
ERR("No (more) space for devicename\n");
|
2001-06-26 23:06:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
strcpy(PPDeviceList[nports].devicename,buffer);
|
|
|
|
PPDeviceList[nports].fd = fd;
|
|
|
|
PPDeviceList[nports].userbase = userbase;
|
|
|
|
PPDeviceList[nports].lastaccess=GetTickCount();
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10);
|
|
|
|
if (errno == ERANGE)
|
|
|
|
{
|
2003-09-11 03:03:47 +02:00
|
|
|
WARN("Configuration: Invalid timeout %s in configuration for %s, Setting to 0\n",
|
2001-06-26 23:06:07 +02:00
|
|
|
timeout,buffer);
|
|
|
|
PPDeviceList[nports].timeout = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PPDeviceList[nports].timeout = 0;
|
|
|
|
nports++;
|
|
|
|
}
|
|
|
|
TRACE("found %d ports\n",nports);
|
2002-09-13 19:47:44 +02:00
|
|
|
NtClose( hkey );
|
2001-06-26 23:06:07 +02:00
|
|
|
|
|
|
|
PPDeviceNum= nports;
|
|
|
|
if (nports > 1)
|
2003-09-11 03:03:47 +02:00
|
|
|
/* sort in ascending order for userbase for faster access */
|
2001-06-26 23:06:07 +02:00
|
|
|
qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2001-06-26 23:06:07 +02:00
|
|
|
if (nports)
|
|
|
|
ret=0;
|
|
|
|
for (idx= 0;idx<PPDeviceNum; idx++)
|
|
|
|
TRACE("found device %s userbase %x fd %x timeout %d\n",
|
|
|
|
PPDeviceList[idx].devicename, PPDeviceList[idx].userbase,
|
|
|
|
PPDeviceList[idx].fd,PPDeviceList[idx].timeout);
|
|
|
|
/* FIXME:
|
2003-09-11 03:03:47 +02:00
|
|
|
register a timer callback perhaps every 30 seconds to release unused ports
|
2001-06-26 23:06:07 +02:00
|
|
|
Set lastaccess = 0 as indicator when port was released
|
|
|
|
*/
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IO_pp_do_access
|
|
|
|
*
|
|
|
|
* Do the actual IOCTL
|
|
|
|
* Return NULL on success
|
|
|
|
*/
|
|
|
|
static int IO_pp_do_access(int idx,int ppctl, DWORD* res)
|
|
|
|
{
|
2001-07-14 02:46:22 +02:00
|
|
|
int ret;
|
|
|
|
if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0))
|
|
|
|
{
|
|
|
|
ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n",
|
|
|
|
PPDeviceList[idx].devicename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = ioctl(PPDeviceList[idx].fd,ppctl,res);
|
|
|
|
if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0))
|
|
|
|
{
|
|
|
|
ERR("Can't release device %s, PPUSER/PPDEV handling confused\n",
|
|
|
|
PPDeviceList[idx].devicename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
2001-06-26 23:06:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* IO_pp_inp
|
|
|
|
*
|
|
|
|
* Check if we can satisfy the INP command with some of the configured PPDEV deviced
|
|
|
|
* Return NULL on success
|
|
|
|
*/
|
|
|
|
int IO_pp_inp(int port, DWORD* res)
|
|
|
|
{
|
|
|
|
int idx,j=0;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2001-06-26 23:06:07 +02:00
|
|
|
for (idx=0;idx<PPDeviceNum ;idx++)
|
|
|
|
{
|
|
|
|
j = port - PPDeviceList[idx].userbase;
|
|
|
|
if (j <0) return 1;
|
|
|
|
switch (j)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return IO_pp_do_access(idx,PPRDATA,res);
|
|
|
|
case 1:
|
|
|
|
return IO_pp_do_access(idx,PPRSTATUS,res);
|
|
|
|
case 2:
|
|
|
|
return IO_pp_do_access(idx,PPRCONTROL,res);
|
|
|
|
case 0x400:
|
|
|
|
case 0x402:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 0x401:
|
|
|
|
FIXME("Port 0x%x not accessible for reading with ppdev\n",port);
|
|
|
|
FIXME("If this is causing problems, try direct port access\n");
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IO_pp_outp
|
|
|
|
*
|
|
|
|
* Check if we can satisfy the INP command with some of the configured PPDEV deviced
|
|
|
|
* Return NULL on success
|
|
|
|
*/
|
|
|
|
BOOL IO_pp_outp(int port, DWORD* res)
|
|
|
|
{
|
|
|
|
int idx,j=0;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2001-06-26 23:06:07 +02:00
|
|
|
for (idx=0;idx<PPDeviceNum ;idx++)
|
|
|
|
{
|
|
|
|
j = port - PPDeviceList[idx].userbase;
|
|
|
|
if (j <0) return 1;
|
|
|
|
switch (j)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return IO_pp_do_access(idx,PPWDATA,res);
|
|
|
|
case 2:
|
2004-02-24 01:59:19 +01:00
|
|
|
{
|
|
|
|
/* We can't switch port direction via PPWCONTROL,
|
|
|
|
so do it via PPDATADIR
|
|
|
|
*/
|
|
|
|
DWORD mode = *res & 0x20;
|
|
|
|
IO_pp_do_access(idx,PPDATADIR,&mode);
|
|
|
|
mode = (*res & ~0x20);
|
|
|
|
return IO_pp_do_access(idx,PPWCONTROL,&mode);
|
|
|
|
}
|
|
|
|
|
2001-06-26 23:06:07 +02:00
|
|
|
case 1:
|
|
|
|
case 0x400:
|
|
|
|
case 0x402:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 0x401:
|
|
|
|
FIXME("Port %d not accessible for writing with ppdev\n",port);
|
|
|
|
FIXME("If this is causing problems, try direct port access\n");
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else /* HAVE_PPDEV */
|
|
|
|
|
2001-07-18 23:04:23 +02:00
|
|
|
|
2001-06-26 23:06:07 +02:00
|
|
|
char IO_pp_init(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int IO_pp_inp(int port, DWORD* res)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL IO_pp_outp(int port, DWORD* res)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_PPDEV */
|