rework more XPCU code. INT_* still needs to be moved
[usb-driver] / usb-driver.c
CommitLineData
54357994 1/* libusb/ppdev connector for XILINX impact
f1405f13 2 *
3 * Copyright (c) 2007 Michael Gernoth <michael@gernoth.net>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
cdc711dc 23
24#define _GNU_SOURCE 1
25
cdc711dc 26#include <dlfcn.h>
27#include <stdarg.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/time.h>
35#include <stdio.h>
f10480d1 36#include <usb.h>
2a7af812 37#include <signal.h>
533f4b68 38#include <pthread.h>
9ba1e383 39#include <errno.h>
11d01742 40#include <inttypes.h>
54357994 41#include <sys/ioctl.h>
c42237a3
MG
42#include <sys/utsname.h>
43#include <bits/wordsize.h>
2c0c1255 44#include "usb-driver.h"
3e670223 45#include "config.h"
cbfa0ac6 46#include "xpcu.h"
cdc711dc 47
9c9fd67c 48static int (*ioctl_func) (int, int, void *) = NULL;
332ced7a 49static int windrvrfd = -1;
54357994 50static unsigned long ppbase = 0;
51static unsigned long ecpbase = 0;
25ba7a49 52static struct parport_config *pport = NULL;
cbfa0ac6 53static struct xpcu_s *xpcu = NULL;
be59993b
MG
54static FILE *modulesfp = NULL;
55static FILE *baseaddrfp = NULL;
56static int baseaddrnum = 0;
723d9aa0 57static int modules_read = 0;
ca18111b 58static int ints_enabled = 0;
533f4b68 59static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
292160ed 60
795992ad 61#define NO_WINDRVR 1
723d9aa0 62
63void hexdump(unsigned char *buf, int len) {
64 int i;
65
66 for(i=0; i<len; i++) {
67 fprintf(stderr,"%02x ", buf[i]);
68 if ((i % 16) == 15)
69 fprintf(stderr,"\n");
70 }
f1405f13 71 fprintf(stderr,"\n");
723d9aa0 72}
cdc711dc 73
cf55ed64 74
be59993b 75static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 76 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 77 struct version_struct *version;
78 int ret = 0;
cdc711dc 79
da3ba95a 80 if (wdheader->magic != MAGIC) {
2c2119eb 81 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
82 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 83 }
84
bdc65937 85 switch(request & ~(0xc0000000)) {
da3ba95a 86 case VERSION:
9c9fd67c 87 version = (struct version_struct*)(wdheader->data);
81cf3658 88 strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION);
90831fba 89 version->versionul = 802;
f1405f13 90 DPRINTF("VERSION\n");
cdc711dc 91 break;
2c2119eb 92
93 case LICENSE:
f1405f13 94 DPRINTF("LICENSE\n");
2c2119eb 95 break;
96
f152c048 97 case CARD_REGISTER_OLD:
9c9fd67c 98 case CARD_REGISTER:
54357994 99 DPRINTF("CARD_REGISTER\n");
9c9fd67c 100 {
533f4b68 101 struct card_register* cr = (struct card_register*)(wdheader->data);
54357994 102
103 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
104 cr->Card.dwItems,
105 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
106 cr->Card.Item[0].I.IO.dwBytes,
107 cr->Card.Item[0].I.IO.dwBar);
108
109 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
110 cr->Card.dwItems,
111 (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
112 cr->Card.Item[1].I.IO.dwBytes,
113 cr->Card.Item[1].I.IO.dwBar);
576995a8 114#ifndef NO_WINDRVR
115 ret = (*ioctl_func) (fd, request, wdioctl);
ac9e3f59 116#else
bccac33b 117
25ba7a49 118 pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
61e0de7a 119 if (!pport)
120 break;
121
25ba7a49 122 ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
3e670223 123
5e3d963b 124 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
bccac33b 125
5e3d963b 126 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr)
127 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
19b2e286 128
5e3d963b 129 if (ret >= 0) {
130 cr->hCard = ret;
131 } else {
19b2e286 132 cr->hCard = 0;
5e3d963b 133 }
576995a8 134#endif
ac9e3f59 135 DPRINTF("hCard: %lu\n", cr->hCard);
9c9fd67c 136 }
da3ba95a 137 break;
2c2119eb 138
da3ba95a 139 case USB_TRANSFER:
f1405f13 140 DPRINTF("in USB_TRANSFER");
da3ba95a 141 {
142 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
143
723d9aa0 144#ifdef DEBUG
f92c0fbc 145 DPRINTF(" unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
f1405f13 146 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
147 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
cf55ed64
MG
148 if (ut->dwPipeNum == 0) {
149 DPRINTF("setup packet: ");
150 hexdump(ut->SetupPacket, 8);
151 }
723d9aa0 152
9c9fd67c 153 if (!ut->fRead && ut->dwBufferSize)
154 {
155 hexdump(ut->pBuffer, ut->dwBufferSize);
9c9fd67c 156 }
723d9aa0 157#endif
9c9fd67c 158
795992ad 159#ifndef NO_WINDRVR
9c9fd67c 160 ret = (*ioctl_func) (fd, request, wdioctl);
411af373 161#else
6c235d59 162 xpcu_transfer(xpcu, ut);
292160ed 163#endif
9c9fd67c 164
723d9aa0 165#ifdef DEBUG
f1405f13 166 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 167 if (ut->fRead && ut->dwBytesTransferred)
168 {
f1405f13 169 DPRINTF("Read: ");
9c9fd67c 170 hexdump(ut->pBuffer, ut->dwBytesTransferred);
171 }
723d9aa0 172#endif
da3ba95a 173 }
cdc711dc 174 break;
2c2119eb 175
f152c048 176 case INT_ENABLE_OLD:
da3ba95a 177 case INT_ENABLE:
f1405f13 178 DPRINTF("INT_ENABLE\n");
da3ba95a 179 {
9c9fd67c 180 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 181
f92c0fbc 182 DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 183 it->hInterrupt, it->dwOptions,
184 it->dwCmds, it->fEnableOk, it->dwCounter,
185 it->dwLost, it->fStopped);
cdc711dc 186
9c9fd67c 187 it->fEnableOk = 1;
9ba1e383 188 it->fStopped = 0;
ca18111b 189 ints_enabled = 1;
533f4b68 190 pthread_mutex_trylock(&int_wait);
9c9fd67c 191 }
cdc711dc 192
cdc711dc 193 break;
9c9fd67c 194
da3ba95a 195 case INT_DISABLE:
f1405f13 196 DPRINTF("INT_DISABLE\n");
da3ba95a 197 {
9c9fd67c 198 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 199
f92c0fbc 200 DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 201 it->hInterrupt, it->dwOptions,
202 it->dwCmds, it->fEnableOk, it->dwCounter,
203 it->dwLost, it->fStopped);
795992ad 204#ifndef NO_WINDRVR
9c9fd67c 205 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 206#else
207 it->dwCounter = 0;
208 it->fStopped = 1;
ca18111b 209 ints_enabled = 0;
9ba1e383 210 if (pthread_mutex_trylock(&int_wait) == EBUSY)
211 pthread_mutex_unlock(&int_wait);
292160ed 212#endif
f92c0fbc 213 DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 214 it->hInterrupt, it->dwOptions,
215 it->dwCmds, it->fEnableOk, it->dwCounter,
216 it->dwLost, it->fStopped);
da3ba95a 217 }
da3ba95a 218 break;
da3ba95a 219
9c9fd67c 220 case USB_SET_INTERFACE:
f1405f13 221 DPRINTF("USB_SET_INTERFACE\n");
da3ba95a 222 {
9c9fd67c 223 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
224
f92c0fbc 225 DPRINTF("unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
f1405f13 226 usi->dwUniqueID, usi->dwInterfaceNum,
227 usi->dwAlternateSetting, usi->dwOptions);
795992ad 228#ifndef NO_WINDRVR
9c9fd67c 229 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 230#else
0c2db148 231 xpcu_set_interface(xpcu, usi);
292160ed 232#endif
f92c0fbc 233 DPRINTF("unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
f1405f13 234 usi->dwUniqueID, usi->dwInterfaceNum,
235 usi->dwAlternateSetting, usi->dwOptions);
da3ba95a 236 }
cdc711dc 237 break;
da3ba95a 238
f152c048 239 case USB_GET_DEVICE_DATA_OLD:
9c9fd67c 240 case USB_GET_DEVICE_DATA:
f1405f13 241 DPRINTF("USB_GET_DEVICE_DATA\n");
9c9fd67c 242 {
243 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
9c9fd67c 244
f92c0fbc 245 DPRINTF("unique: 0x%lx, bytes: %lu, options: %lx\n",
f1405f13 246 ugdd->dwUniqueID, ugdd->dwBytes,
247 ugdd->dwOptions);
248
f92c0fbc
MG
249 ugdd->dwBytes = xpcu_deviceinfo(xpcu, ugdd);
250
da3ba95a 251 }
9c9fd67c 252 break;
253
f152c048 254 case EVENT_REGISTER_OLD:
b0f621dd 255 case EVENT_REGISTER:
f1405f13 256 DPRINTF("EVENT_REGISTER\n");
b0f621dd 257 {
258 struct event *e = (struct event*)(wdheader->data);
f92c0fbc 259#ifdef DEBUG
b0f621dd 260 int i;
f92c0fbc 261#endif
b0f621dd 262
f92c0fbc 263 DPRINTF("handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 264 e->handle, e->dwAction,
265 e->dwStatus, e->dwEventId, e->dwCardType,
266 e->hKernelPlugIn, e->dwOptions,
267 e->u.Usb.deviceId.dwVendorId,
268 e->u.Usb.deviceId.dwProductId,
269 e->u.Usb.dwUniqueID, e->dwEventVer,
270 e->dwNumMatchTables);
271
f92c0fbc 272 xpcu = xpcu_find(e);
795992ad 273#ifndef NO_WINDRVR
b0f621dd 274 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 275#else
f92c0fbc 276 e->handle = (unsigned long)xpcu;
292160ed 277#endif
b0f621dd 278
723d9aa0 279#ifdef DEBUG
f92c0fbc 280 DPRINTF("handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 281 e->handle, e->dwAction,
282 e->dwStatus, e->dwEventId, e->dwCardType,
283 e->hKernelPlugIn, e->dwOptions,
284 e->u.Usb.deviceId.dwVendorId,
285 e->u.Usb.deviceId.dwProductId,
286 e->u.Usb.dwUniqueID, e->dwEventVer,
287 e->dwNumMatchTables);
288
b0f621dd 289 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 290 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
291 e->matchTables[i].VendorId,
292 e->matchTables[i].ProductId,
293 e->matchTables[i].bDeviceClass,
294 e->matchTables[i].bDeviceSubClass,
295 e->matchTables[i].bInterfaceClass,
296 e->matchTables[i].bInterfaceSubClass,
297 e->matchTables[i].bInterfaceProtocol);
723d9aa0 298#endif
b0f621dd 299 }
300 break;
301
f152c048 302 case TRANSFER_OLD:
da3ba95a 303 case TRANSFER:
f1405f13 304 DPRINTF("TRANSFER\n");
ac9e3f59 305 {
306 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
307
1dac5195 308#ifndef NO_WINDRVR
309 ret = (*ioctl_func) (fd, request, wdioctl);
310#else
25ba7a49 311 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1);
1dac5195 312#endif
ac9e3f59 313 }
9c9fd67c 314 break;
315
54357994 316 case MULTI_TRANSFER_OLD:
576995a8 317 case MULTI_TRANSFER:
318 DPRINTF("MULTI_TRANSFER\n");
ac9e3f59 319 {
320 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
321 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
1dac5195 322#ifndef NO_WINDRVR
323 ret = (*ioctl_func) (fd, request, wdioctl);
324#else
25ba7a49 325 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num);
ac9e3f59 326#endif
ac9e3f59 327 }
576995a8 328 break;
329
da3ba95a 330 case EVENT_UNREGISTER:
f92c0fbc
MG
331 {
332 struct event *e = (struct event*)(wdheader->data);
333
334 DPRINTF("EVENT_UNREGISTER\n");
795992ad 335#ifndef NO_WINDRVR
f92c0fbc
MG
336 ret = (*ioctl_func) (fd, request, wdioctl);
337#else
338 xpcu_close(xpcu, e);
292160ed 339#endif
f92c0fbc 340 }
9c9fd67c 341 break;
342
da3ba95a 343 case INT_WAIT:
f1405f13 344 DPRINTF("INT_WAIT\n");
b0f621dd 345 {
346 struct interrupt *it = (struct interrupt*)(wdheader->data);
347
f92c0fbc 348 DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 349 it->hInterrupt, it->dwOptions,
350 it->dwCmds, it->fEnableOk, it->dwCounter,
351 it->dwLost, it->fStopped);
b0f621dd 352
795992ad 353#ifndef NO_WINDRVR
b0f621dd 354 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 355#else
f92c0fbc 356 if (xpcu) {
2a7af812 357 if (it->dwCounter == 0) {
358 it->dwCounter = 1;
359 } else {
533f4b68 360 pthread_mutex_lock(&int_wait);
be452175 361 pthread_mutex_unlock(&int_wait);
2a7af812 362 }
94038a57 363 } else {
533f4b68 364 pthread_mutex_lock(&int_wait);
be452175 365 pthread_mutex_unlock(&int_wait);
2a7af812 366 }
292160ed 367#endif
368
f92c0fbc 369 DPRINTF("INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 370 it->hInterrupt, it->dwOptions, it->dwCmds,
371 it->fEnableOk, it->dwCounter, it->dwLost,
372 it->fStopped);
b0f621dd 373 }
9c9fd67c 374 break;
375
da3ba95a 376 case CARD_UNREGISTER:
f1405f13 377 DPRINTF("CARD_UNREGISTER\n");
54357994 378 {
379 struct card_register* cr = (struct card_register*)(wdheader->data);
380
381 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
382 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
383 cr->Card.Item[0].I.IO.dwBytes,
384 cr->Card.Item[0].I.IO.dwBar);
385
386 DPRINTF("hCard: %lu\n", cr->hCard);
387
795992ad 388#ifndef NO_WINDRVR
54357994 389 ret = (*ioctl_func) (fd, request, wdioctl);
390#else
72ce448b
MG
391 if (pport)
392 pport->close(cr->hCard);
393
394 pport = NULL;
292160ed 395#endif
54357994 396 }
9c9fd67c 397 break;
398
da3ba95a 399 case EVENT_PULL:
f1405f13 400 DPRINTF("EVENT_PULL\n");
b1831983 401 {
402 struct event *e = (struct event*)(wdheader->data);
723d9aa0 403#ifdef DEBUG
b1831983 404 int i;
405
f92c0fbc 406 DPRINTF("handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 407 e->handle, e->dwAction, e->dwStatus,
408 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
409 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
410 e->u.Usb.deviceId.dwProductId,
411 e->u.Usb.dwUniqueID, e->dwEventVer,
412 e->dwNumMatchTables);
413
b1831983 414 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 415 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
416 e->matchTables[i].VendorId,
417 e->matchTables[i].ProductId,
418 e->matchTables[i].bDeviceClass,
419 e->matchTables[i].bDeviceSubClass,
420 e->matchTables[i].bInterfaceClass,
421 e->matchTables[i].bInterfaceSubClass,
422 e->matchTables[i].bInterfaceProtocol);
723d9aa0 423#endif
b1831983 424
795992ad 425#ifndef NO_WINDRVR
b1831983 426 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 427#else
f92c0fbc 428 xpcu_found(xpcu, e);
292160ed 429#endif
b1831983 430
723d9aa0 431#ifdef DEBUG
f92c0fbc 432 DPRINTF("handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 433 e->handle, e->dwAction, e->dwStatus,
434 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
435 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
436 e->u.Usb.deviceId.dwProductId,
437 e->u.Usb.dwUniqueID, e->dwEventVer,
438 e->dwNumMatchTables);
439
b1831983 440 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 441 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
442 e->matchTables[i].VendorId,
443 e->matchTables[i].ProductId,
444 e->matchTables[i].bDeviceClass,
445 e->matchTables[i].bDeviceSubClass,
446 e->matchTables[i].bInterfaceClass,
447 e->matchTables[i].bInterfaceSubClass,
448 e->matchTables[i].bInterfaceProtocol);
723d9aa0 449#endif
ca18111b 450
b1831983 451 }
9c9fd67c 452 break;
453
da3ba95a 454 default:
292160ed 455 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
795992ad 456#ifndef NO_WINDRVR
9c9fd67c 457 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 458#endif
459 break;
cdc711dc 460 }
da3ba95a 461
9c9fd67c 462 return ret;
cdc711dc 463}
464
54357994 465int ioctl(int fd, unsigned long int request, ...) {
dbda1264 466 va_list args;
467 void *argp;
468 int ret;
469
470 if (!ioctl_func)
f1405f13 471 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
dbda1264 472
473 va_start (args, request);
474 argp = va_arg (args, void *);
475 va_end (args);
476
332ced7a 477 if (fd == windrvrfd)
dbda1264 478 ret = do_wdioctl(fd, request, argp);
479 else
480 ret = (*ioctl_func) (fd, request, argp);
481
482 return ret;
483}
cdc711dc 484
be452175 485int open (const char *pathname, int flags, ...) {
486 static int (*func) (const char *, int, mode_t) = NULL;
cdc711dc 487 mode_t mode = 0;
488 va_list args;
489 int fd;
490
491 if (!func)
f1405f13 492 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
cdc711dc 493
494 if (flags & O_CREAT) {
495 va_start(args, flags);
496 mode = va_arg(args, mode_t);
497 va_end(args);
498 }
499
cdc711dc 500 if (!strcmp (pathname, "/dev/windrvr6")) {
f1405f13 501 DPRINTF("opening windrvr6\n");
795992ad 502#ifdef NO_WINDRVR
01b99d52 503 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
504#else
505 windrvrfd = fd = (*func) (pathname, flags, mode);
506#endif
723d9aa0 507
508 return fd;
cdc711dc 509 }
510
723d9aa0 511 return (*func) (pathname, flags, mode);
cdc711dc 512}
513
723d9aa0 514int close(int fd) {
515 static int (*func) (int) = NULL;
cdc711dc 516
723d9aa0 517 if (!func)
f1405f13 518 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
723d9aa0 519
11d01742 520 if (fd == windrvrfd && windrvrfd >= 0) {
f1405f13 521 DPRINTF("close windrvrfd\n");
010cbaa6 522
cbfa0ac6 523 xpcu = NULL;
332ced7a 524 windrvrfd = -1;
cdc711dc 525 }
cdc711dc 526
723d9aa0 527 return (*func) (fd);
cdc711dc 528}
529
723d9aa0 530FILE *fopen(const char *path, const char *mode) {
531 FILE *ret;
532 static FILE* (*func) (const char*, const char*) = NULL;
0dca330e 533 char buf[256];
534 int i;
ca18111b 535
536 if (!func)
f1405f13 537 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
ca18111b 538
3e670223 539 for (i = 0; i < 4; i++) {
540 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
541 if (!strcmp(path, buf)) {
542 DPRINTF("open base-addr of parport%d\n", i);
543 if (config_is_real_pport(i)) {
544 ret = (*func) (path, mode);
545 } else {
546 ret = (*func) ("/dev/null", mode);
547 }
548
549 if (ret) {
550 baseaddrfp = ret;
551 baseaddrnum = i;
552 }
553
554 return ret;
555 }
556 }
557
558 ret = (*func) (path, mode);
ca18111b 559
0dca330e 560 if (!strcmp(path, "/proc/modules")) {
f1405f13 561 DPRINTF("opening /proc/modules\n");
723d9aa0 562#ifdef NO_WINDRVR
dbda1264 563 modulesfp = ret;
723d9aa0 564 modules_read = 0;
565#endif
566 }
cdc711dc 567
568 return ret;
569}
570
dbda1264 571char *fgets(char *s, int size, FILE *stream) {
572 static char* (*func) (char*, int, FILE*) = NULL;
16f6b164 573 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
0dca330e 574 char buf[256];
dbda1264 575 char *ret = NULL;
576
cdc711dc 577
578 if (!func)
f1405f13 579 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
723d9aa0 580
dbda1264 581 if (modulesfp == stream) {
54357994 582 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
16f6b164 583 strcpy(s, modules[modules_read]);
dbda1264 584 ret = s;
16f6b164 585 modules_read++;
dbda1264 586 }
0dca330e 587 } else if (baseaddrfp == stream) {
588 snprintf(s, sizeof(buf), "%d\t%d\n",
589 (baseaddrnum) * 0x10,
590 ((baseaddrnum) * 0x10) + 0x400);
591 ret = s;
723d9aa0 592 } else {
dbda1264 593 ret = (*func)(s,size,stream);
723d9aa0 594 }
cdc711dc 595
596 return ret;
597}
598
dbda1264 599int fclose(FILE *fp) {
600 static int (*func) (FILE*) = NULL;
cdc711dc 601
dbda1264 602 if (!func)
f1405f13 603 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
cdc711dc 604
dbda1264 605 if (fp == modulesfp) {
606 modulesfp = NULL;
607 }
0dca330e 608
609 if (fp == baseaddrfp) {
610 baseaddrfp = NULL;
611 }
dbda1264 612
613 return (*func)(fp);
cdc711dc 614}
419f2c98 615
616int access(const char *pathname, int mode) {
617 static int (*func) (const char*, int);
618
619 if (!func)
f1405f13 620 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
3eee002c 621
b8b756f5 622 if (pathname && !strcmp(pathname, "/dev/windrvr6")) {
419f2c98 623 return 0;
624 } else {
625 return (*func)(pathname, mode);
626 }
627}
c42237a3
MG
628
629#if __WORDSIZE == 32
630int uname (struct utsname *__name) {
631 static int (*func) (struct utsname*);
632 int ret;
633
634 if (!func)
635 func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname");
636
637 ret = (*func)(__name);
638
639 if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) {
640 strcpy(__name->machine, "i686");
641 }
642
643 return ret;
644}
645#endif
Impressum, Datenschutz