Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Parallel-port device support |
| 3 | */ |
| 4 | |
| 5 | #include "config.h" |
| 6 | |
| 7 | #include "debugtools.h" |
| 8 | |
| 9 | DEFAULT_DEBUG_CHANNEL(int); |
| 10 | |
| 11 | #ifdef HAVE_PPDEV |
| 12 | |
| 13 | #include <stdlib.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <errno.h> |
| 19 | #include <linux/ppdev.h> |
| 20 | |
| 21 | #include "winerror.h" |
| 22 | #include "winreg.h" |
| 23 | |
| 24 | |
| 25 | typedef struct _PPDEVICESTRUCT{ |
| 26 | int fd; /* NULL if device not available */ |
| 27 | char *devicename; |
| 28 | int userbase; /* where wine thinks the ports are*/ |
| 29 | DWORD lastaccess; /* or NULL if release */ |
| 30 | int timeout; /* time in second of inactivity to release the port*/ |
| 31 | } PPDeviceStruct; |
| 32 | |
| 33 | static PPDeviceStruct PPDeviceList[5]; |
| 34 | static int PPDeviceNum=0; |
| 35 | |
| 36 | static int IO_pp_sort(const void *p1,const void *p2) |
| 37 | { |
| 38 | return ((PPDeviceStruct*)p1)->userbase - ((PPDeviceStruct*)p2)->userbase; |
| 39 | } |
| 40 | |
| 41 | /* IO_pp_init |
| 42 | * |
| 43 | * Read the ppdev entries from wine.conf, open the device and check |
| 44 | * for nescessary IOCTRL |
| 45 | * Report verbose about possible errors |
| 46 | */ |
| 47 | char IO_pp_init(void) |
| 48 | { |
| 49 | char name[80]; |
| 50 | char buffer[1024]; |
| 51 | HKEY hkey; |
| 52 | char temp[256]; |
| 53 | int i,idx=0,fd,res,userbase,nports=0; |
| 54 | char * timeout; |
| 55 | char ret=1; |
| 56 | int lasterror; |
| 57 | |
| 58 | TRACE("\n"); |
| 59 | if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppdev", &hkey ) != ERROR_SUCCESS) |
| 60 | return 1; |
| 61 | |
| 62 | for (;;) |
| 63 | { |
| 64 | DWORD type, count = sizeof(buffer), name_len = sizeof(name); |
| 65 | |
| 66 | if (RegEnumValueA( hkey, idx, name, &name_len, NULL, &type, buffer, &count )!= ERROR_SUCCESS) |
| 67 | break; |
| 68 | |
| 69 | idx++; |
| 70 | if(nports >4) |
| 71 | { |
| 72 | FIXME("Make the PPDeviceList larger then 5 elements\n"); |
| 73 | break; |
| 74 | } |
| 75 | TRACE("Device '%s' at virtual userbase '%s'\n", buffer,name); |
| 76 | timeout = strchr(buffer,','); |
| 77 | if (timeout) |
| 78 | *timeout++=0; |
| 79 | fd=open(buffer,O_RDWR); |
| 80 | lasterror=errno; |
| 81 | if (fd == -1) |
| 82 | { |
| 83 | WARN("Configuration: No access to %s Cause: %s\n",buffer,strerror(lasterror)); |
| 84 | WARN("Rejecting configuration item\n"); |
| 85 | if (lasterror == ENODEV) |
| 86 | FIXME("Is the ppdev module loaded?\n"); |
| 87 | continue; |
| 88 | } |
| 89 | userbase = strtol(name,(char **)NULL, 16); |
| 90 | if ( errno == ERANGE) |
| 91 | { |
| 92 | WARN("Configuration: Invalid base %s for %s\n",name,buffer); |
| 93 | WARN("Rejecting configuration item\n"); |
| 94 | continue; |
| 95 | } |
Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 96 | if (ioctl (fd,PPCLAIM,0)) |
| 97 | { |
| 98 | ERR("PPCLAIM rejected %s\n",buffer); |
| 99 | ERR("Perhaps the device is already in use or non-existant\n"); |
| 100 | continue; |
| 101 | } |
| 102 | if (nports > 0) |
| 103 | { |
| 104 | for (i=0; i<= nports; i++) |
| 105 | { |
| 106 | if (PPDeviceList[i].userbase == userbase) |
| 107 | { |
| 108 | WARN("Configuration: %s uses the same virtual ports as %s\n", |
| 109 | buffer,PPDeviceList[0].devicename); |
| 110 | WARN("Configuration: Rejecting configuration item"); |
| 111 | userbase = 0; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | if (!userbase) continue; |
| 116 | } |
| 117 | /* Check for the minimum required IOCTLS */ |
| 118 | if ((ioctl(fd,PPRDATA,&res))|| |
| 119 | (ioctl(fd,PPRCONTROL,&res))|| |
| 120 | (ioctl(fd,PPRCONTROL,&res))) |
| 121 | { |
| 122 | ERR("PPUSER IOCTL not available for parport device %s\n",temp); |
| 123 | continue; |
| 124 | } |
Uwe Bonnes | cf735f7 | 2001-07-14 00:46:22 +0000 | [diff] [blame] | 125 | if (ioctl (fd,PPRELEASE,0)) |
| 126 | { |
| 127 | ERR("PPRELEASE rejected %s\n",buffer); |
| 128 | ERR("Perhaps the device is already in use or non-existant\n"); |
| 129 | continue; |
| 130 | } |
Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 131 | PPDeviceList[nports].devicename = malloc(sizeof(buffer)+1); |
| 132 | if (!PPDeviceList[nports].devicename) |
| 133 | { |
| 134 | ERR("No (more)space for devicename\n"); |
| 135 | break; |
| 136 | } |
| 137 | strcpy(PPDeviceList[nports].devicename,buffer); |
| 138 | PPDeviceList[nports].fd = fd; |
| 139 | PPDeviceList[nports].userbase = userbase; |
| 140 | PPDeviceList[nports].lastaccess=GetTickCount(); |
| 141 | if (timeout) |
| 142 | { |
| 143 | PPDeviceList[nports].timeout = strtol(timeout,(char **)NULL, 10); |
| 144 | if (errno == ERANGE) |
| 145 | { |
| 146 | WARN("Configuration:Invalid timeout %s in configuration for %s, Setting to 0\n", |
| 147 | timeout,buffer); |
| 148 | PPDeviceList[nports].timeout = 0; |
| 149 | } |
| 150 | } |
| 151 | else |
| 152 | PPDeviceList[nports].timeout = 0; |
| 153 | nports++; |
| 154 | } |
| 155 | TRACE("found %d ports\n",nports); |
| 156 | RegCloseKey( hkey ); |
| 157 | |
| 158 | PPDeviceNum= nports; |
| 159 | if (nports > 1) |
| 160 | /* sort in accending order for userbase for faster access*/ |
| 161 | qsort (PPDeviceList,PPDeviceNum,sizeof(PPDeviceStruct),IO_pp_sort); |
| 162 | |
| 163 | if (nports) |
| 164 | ret=0; |
| 165 | for (idx= 0;idx<PPDeviceNum; idx++) |
| 166 | TRACE("found device %s userbase %x fd %x timeout %d\n", |
| 167 | PPDeviceList[idx].devicename, PPDeviceList[idx].userbase, |
| 168 | PPDeviceList[idx].fd,PPDeviceList[idx].timeout); |
| 169 | /* FIXME: |
| 170 | register a timer callback perhaps every 30 second to release unused ports |
| 171 | Set lastaccess = 0 as indicator when port was released |
| 172 | */ |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | /* IO_pp_do_access |
| 177 | * |
| 178 | * Do the actual IOCTL |
| 179 | * Return NULL on success |
| 180 | */ |
| 181 | static int IO_pp_do_access(int idx,int ppctl, DWORD* res) |
| 182 | { |
Uwe Bonnes | cf735f7 | 2001-07-14 00:46:22 +0000 | [diff] [blame] | 183 | int ret; |
| 184 | if (ioctl(PPDeviceList[idx].fd,PPCLAIM,0)) |
| 185 | { |
| 186 | ERR("Can't reclaim device %s, PPUSER/PPDEV handling confused\n", |
| 187 | PPDeviceList[idx].devicename); |
| 188 | return 1; |
| 189 | } |
| 190 | ret = ioctl(PPDeviceList[idx].fd,ppctl,res); |
| 191 | if (ioctl(PPDeviceList[idx].fd,PPRELEASE,0)) |
| 192 | { |
| 193 | ERR("Can't release device %s, PPUSER/PPDEV handling confused\n", |
| 194 | PPDeviceList[idx].devicename); |
| 195 | return 1; |
| 196 | } |
| 197 | return ret; |
| 198 | |
Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* IO_pp_inp |
| 202 | * |
| 203 | * Check if we can satisfy the INP command with some of the configured PPDEV deviced |
| 204 | * Return NULL on success |
| 205 | */ |
| 206 | int IO_pp_inp(int port, DWORD* res) |
| 207 | { |
| 208 | int idx,j=0; |
| 209 | |
| 210 | for (idx=0;idx<PPDeviceNum ;idx++) |
| 211 | { |
| 212 | j = port - PPDeviceList[idx].userbase; |
| 213 | if (j <0) return 1; |
| 214 | switch (j) |
| 215 | { |
| 216 | case 0: |
| 217 | return IO_pp_do_access(idx,PPRDATA,res); |
| 218 | case 1: |
| 219 | return IO_pp_do_access(idx,PPRSTATUS,res); |
| 220 | case 2: |
| 221 | return IO_pp_do_access(idx,PPRCONTROL,res); |
| 222 | case 0x400: |
| 223 | case 0x402: |
| 224 | case 3: |
| 225 | case 4: |
| 226 | case 0x401: |
| 227 | FIXME("Port 0x%x not accessible for reading with ppdev\n",port); |
| 228 | FIXME("If this is causing problems, try direct port access\n"); |
| 229 | return 1; |
| 230 | default: |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | /* IO_pp_outp |
| 238 | * |
| 239 | * Check if we can satisfy the INP command with some of the configured PPDEV deviced |
| 240 | * Return NULL on success |
| 241 | */ |
| 242 | BOOL IO_pp_outp(int port, DWORD* res) |
| 243 | { |
| 244 | int idx,j=0; |
| 245 | |
| 246 | for (idx=0;idx<PPDeviceNum ;idx++) |
| 247 | { |
| 248 | j = port - PPDeviceList[idx].userbase; |
| 249 | if (j <0) return 1; |
| 250 | switch (j) |
| 251 | { |
| 252 | case 0: |
| 253 | return IO_pp_do_access(idx,PPWDATA,res); |
| 254 | case 2: |
| 255 | return IO_pp_do_access(idx,PPWCONTROL,res); |
| 256 | case 1: |
| 257 | case 0x400: |
| 258 | case 0x402: |
| 259 | case 3: |
| 260 | case 4: |
| 261 | case 0x401: |
| 262 | FIXME("Port %d not accessible for writing with ppdev\n",port); |
| 263 | FIXME("If this is causing problems, try direct port access\n"); |
| 264 | return 1; |
| 265 | default: |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | return TRUE; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | #else /* HAVE_PPDEV */ |
| 274 | |
| 275 | char IO_pp_init(void) |
| 276 | { |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | int IO_pp_inp(int port, DWORD* res) |
| 281 | { |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | BOOL IO_pp_outp(int port, DWORD* res) |
| 286 | { |
| 287 | return TRUE; |
| 288 | } |
| 289 | #endif /* HAVE_PPDEV */ |