]> git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
9ed0c353bf1f5ca4756eb0b3d06ea4fdeec25071
[usb-driver] / usb-driver.c
1 /* libusb/ppdev connector for XILINX impact
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 */
23
24 #define _GNU_SOURCE 1
25
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>
36 #include <usb.h>
37 #include <signal.h>
38 #include <pthread.h>
39 #include <errno.h>
40 #include <inttypes.h>
41 #include <sys/ioctl.h>
42 #include <sys/utsname.h>
43 #include <bits/wordsize.h>
44 #include "usb-driver.h"
45 #include "config.h"
46 #include "xpcu.h"
47
48 static int (*ioctl_func) (int, int, void *) = NULL;
49 static int windrvrfd = -1;
50 static unsigned long ppbase = 0;
51 static unsigned long ecpbase = 0;
52 static struct parport_config *pport = NULL;
53 static struct xpcu_s *xpcu = NULL;
54 static FILE *modulesfp = NULL;
55 static FILE *baseaddrfp = NULL;
56 static int baseaddrnum = 0;
57 static int modules_read = 0;
58 static int ints_enabled = 0;
59 static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
60
61 #define NO_WINDRVR 1
62
63 void 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 }
71 fprintf(stderr,"\n");
72 }
73
74
75 static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
76 struct header_struct* wdheader = (struct header_struct*)wdioctl;
77 struct version_struct *version;
78 int ret = 0;
79
80 if (wdheader->magic != MAGIC) {
81 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
82 return (*ioctl_func) (fd, request, wdioctl);
83 }
84
85 switch(request & ~(0xc0000000)) {
86 case VERSION:
87 version = (struct version_struct*)(wdheader->data);
88 strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION);
89 version->versionul = 802;
90 DPRINTF("VERSION\n");
91 break;
92
93 case LICENSE:
94 DPRINTF("LICENSE\n");
95 break;
96
97 case CARD_REGISTER_OLD:
98 case CARD_REGISTER:
99 DPRINTF("CARD_REGISTER\n");
100 {
101 struct card_register* cr = (struct card_register*)(wdheader->data);
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);
114 #ifndef NO_WINDRVR
115 ret = (*ioctl_func) (fd, request, wdioctl);
116 #else
117
118 pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
119 if (!pport)
120 break;
121
122 ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
123
124 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
125
126 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr)
127 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
128
129 if (ret >= 0) {
130 cr->hCard = ret;
131 } else {
132 cr->hCard = 0;
133 }
134 #endif
135 DPRINTF("hCard: %lu\n", cr->hCard);
136 }
137 break;
138
139 case USB_TRANSFER:
140 DPRINTF("in USB_TRANSFER");
141 {
142 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
143
144 #ifdef DEBUG
145 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
146 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
147 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
148 if (ut->dwPipeNum == 0) {
149 DPRINTF("setup packet: ");
150 hexdump(ut->SetupPacket, 8);
151 }
152
153 if (!ut->fRead && ut->dwBufferSize)
154 {
155 hexdump(ut->pBuffer, ut->dwBufferSize);
156 }
157 #endif
158
159 #ifndef NO_WINDRVR
160 ret = (*ioctl_func) (fd, request, wdioctl);
161 #else
162 xpcu_transfer(xpcu, ut);
163 #endif
164
165 #ifdef DEBUG
166 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
167 if (ut->fRead && ut->dwBytesTransferred)
168 {
169 DPRINTF("Read: ");
170 hexdump(ut->pBuffer, ut->dwBytesTransferred);
171 }
172 #endif
173 }
174 break;
175
176 case INT_ENABLE_OLD:
177 case INT_ENABLE:
178 DPRINTF("INT_ENABLE\n");
179 {
180 struct interrupt *it = (struct interrupt*)(wdheader->data);
181
182 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
183 it->hInterrupt, it->dwOptions,
184 it->dwCmds, it->fEnableOk, it->dwCounter,
185 it->dwLost, it->fStopped);
186
187 it->fEnableOk = 1;
188 it->fStopped = 0;
189 ints_enabled = 1;
190 pthread_mutex_trylock(&int_wait);
191 }
192
193 break;
194
195 case INT_DISABLE:
196 DPRINTF("INT_DISABLE\n");
197 {
198 struct interrupt *it = (struct interrupt*)(wdheader->data);
199
200 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
201 it->hInterrupt, it->dwOptions,
202 it->dwCmds, it->fEnableOk, it->dwCounter,
203 it->dwLost, it->fStopped);
204 #ifndef NO_WINDRVR
205 ret = (*ioctl_func) (fd, request, wdioctl);
206 #else
207 it->dwCounter = 0;
208 it->fStopped = 1;
209 ints_enabled = 0;
210 if (pthread_mutex_trylock(&int_wait) == EBUSY)
211 pthread_mutex_unlock(&int_wait);
212 #endif
213 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
214 it->hInterrupt, it->dwOptions,
215 it->dwCmds, it->fEnableOk, it->dwCounter,
216 it->dwLost, it->fStopped);
217 }
218 break;
219
220 case USB_SET_INTERFACE:
221 DPRINTF("USB_SET_INTERFACE\n");
222 {
223 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
224
225 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
226 usi->dwUniqueID, usi->dwInterfaceNum,
227 usi->dwAlternateSetting, usi->dwOptions);
228 #ifndef NO_WINDRVR
229 ret = (*ioctl_func) (fd, request, wdioctl);
230 #else
231 xpcu_set_interface(xpcu, usi);
232 #endif
233 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
234 usi->dwUniqueID, usi->dwInterfaceNum,
235 usi->dwAlternateSetting, usi->dwOptions);
236 }
237 break;
238
239 case USB_GET_DEVICE_DATA_OLD:
240 case USB_GET_DEVICE_DATA:
241 DPRINTF("USB_GET_DEVICE_DATA\n");
242 {
243 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
244 int pSize;
245
246 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
247 ugdd->dwUniqueID, ugdd->dwBytes,
248 ugdd->dwOptions);
249
250 pSize = ugdd->dwBytes;
251 if (!ugdd->dwBytes) {
252 if (xpcu->dev) {
253 ugdd->dwBytes = xpcu_deviceinfo(xpcu, NULL);
254 }
255 } else {
256 xpcu_deviceinfo(xpcu, (unsigned char*)ugdd->pBuf);
257 }
258 }
259 break;
260
261 case EVENT_REGISTER_OLD:
262 case EVENT_REGISTER:
263 DPRINTF("EVENT_REGISTER\n");
264 {
265 struct event *e = (struct event*)(wdheader->data);
266 struct usb_bus *bus;
267 char* devpos;
268 int busnum = -1, devnum = -1;
269 int i;
270
271 DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
272 e->handle, e->dwAction,
273 e->dwStatus, e->dwEventId, e->dwCardType,
274 e->hKernelPlugIn, e->dwOptions,
275 e->u.Usb.deviceId.dwVendorId,
276 e->u.Usb.deviceId.dwProductId,
277 e->u.Usb.dwUniqueID, e->dwEventVer,
278 e->dwNumMatchTables);
279
280 devpos = getenv("XILINX_USB_DEV");
281 if (devpos != NULL) {
282 int j;
283 char *devstr = NULL, *remainder;
284
285 DPRINTF("XILINX_USB_DEV=%s\n", devpos);
286
287 for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) {
288 if (devpos[j] == ':') {
289 devpos[j] = 0;
290 devstr = &(devpos[j+1]);
291 }
292 }
293
294 if (devstr && strlen(devstr) > 0) {
295 busnum = strtol(devpos, &remainder, 10);
296 if (devpos == remainder) {
297 busnum = -1;
298 } else {
299 devnum = strtol(devstr, &remainder, 10);
300 if (devstr == remainder) {
301 busnum = -1;
302 devnum = -1;
303 } else {
304 fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n",
305 busnum, devnum);
306 }
307 }
308 }
309 }
310
311 for (i = 0; i < e->dwNumMatchTables; i++) {
312
313 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
314 e->matchTables[i].VendorId,
315 e->matchTables[i].ProductId,
316 e->matchTables[i].bDeviceClass,
317 e->matchTables[i].bDeviceSubClass,
318 e->matchTables[i].bInterfaceClass,
319 e->matchTables[i].bInterfaceSubClass,
320 e->matchTables[i].bInterfaceProtocol);
321
322 for (bus = xpcu->busses; bus; bus = bus->next) {
323 struct usb_device *dev;
324
325 if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum))
326 continue;
327
328 for (dev = bus->devices; dev; dev = dev->next) {
329 struct usb_device_descriptor *desc = &(dev->descriptor);
330
331 if((desc->idVendor == e->matchTables[i].VendorId) &&
332 (desc->idProduct == e->matchTables[i].ProductId) &&
333 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
334 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) &&
335 ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) {
336 int ac;
337 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
338 struct usb_interface *interface = dev->config[ac].interface;
339 int ai;
340
341 for (ai = 0; ai < interface->num_altsetting; ai++) {
342
343 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
344 interface->altsetting[i].bInterfaceClass,
345 interface->altsetting[i].bInterfaceSubClass,
346 interface->altsetting[i].bInterfaceProtocol);
347
348 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
349 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
350 /* TODO: check interfaceClass! */
351 DPRINTF("found device with libusb\n");
352 xpcu->dev = dev;
353 xpcu->card_type = e->dwCardType;
354 }
355 }
356 }
357 }
358 }
359 }
360 }
361
362 #ifndef NO_WINDRVR
363 ret = (*ioctl_func) (fd, request, wdioctl);
364 #else
365 e->handle++;
366 #endif
367
368 #ifdef DEBUG
369 DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
370 e->handle, e->dwAction,
371 e->dwStatus, e->dwEventId, e->dwCardType,
372 e->hKernelPlugIn, e->dwOptions,
373 e->u.Usb.deviceId.dwVendorId,
374 e->u.Usb.deviceId.dwProductId,
375 e->u.Usb.dwUniqueID, e->dwEventVer,
376 e->dwNumMatchTables);
377
378 for (i = 0; i < e->dwNumMatchTables; i++)
379 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
380 e->matchTables[i].VendorId,
381 e->matchTables[i].ProductId,
382 e->matchTables[i].bDeviceClass,
383 e->matchTables[i].bDeviceSubClass,
384 e->matchTables[i].bInterfaceClass,
385 e->matchTables[i].bInterfaceSubClass,
386 e->matchTables[i].bInterfaceProtocol);
387 #endif
388 }
389 break;
390
391 case TRANSFER_OLD:
392 case TRANSFER:
393 DPRINTF("TRANSFER\n");
394 {
395 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
396
397 #ifndef NO_WINDRVR
398 ret = (*ioctl_func) (fd, request, wdioctl);
399 #else
400 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1);
401 #endif
402 }
403 break;
404
405 case MULTI_TRANSFER_OLD:
406 case MULTI_TRANSFER:
407 DPRINTF("MULTI_TRANSFER\n");
408 {
409 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
410 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
411 #ifndef NO_WINDRVR
412 ret = (*ioctl_func) (fd, request, wdioctl);
413 #else
414 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num);
415 #endif
416 }
417 break;
418
419 case EVENT_UNREGISTER:
420 DPRINTF("EVENT_UNREGISTER\n");
421 #ifndef NO_WINDRVR
422 ret = (*ioctl_func) (fd, request, wdioctl);
423 #endif
424 break;
425
426 case INT_WAIT:
427 DPRINTF("INT_WAIT\n");
428 {
429 struct interrupt *it = (struct interrupt*)(wdheader->data);
430
431 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
432 it->hInterrupt, it->dwOptions,
433 it->dwCmds, it->fEnableOk, it->dwCounter,
434 it->dwLost, it->fStopped);
435
436 #ifndef NO_WINDRVR
437 ret = (*ioctl_func) (fd, request, wdioctl);
438 #else
439 if (xpcu->dev) {
440 if (it->dwCounter == 0) {
441 it->dwCounter = 1;
442 } else {
443 pthread_mutex_lock(&int_wait);
444 pthread_mutex_unlock(&int_wait);
445 }
446 } else {
447 pthread_mutex_lock(&int_wait);
448 pthread_mutex_unlock(&int_wait);
449 }
450 #endif
451
452 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
453 it->hInterrupt, it->dwOptions, it->dwCmds,
454 it->fEnableOk, it->dwCounter, it->dwLost,
455 it->fStopped);
456 }
457 break;
458
459 case CARD_UNREGISTER:
460 DPRINTF("CARD_UNREGISTER\n");
461 {
462 struct card_register* cr = (struct card_register*)(wdheader->data);
463
464 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
465 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
466 cr->Card.Item[0].I.IO.dwBytes,
467 cr->Card.Item[0].I.IO.dwBar);
468
469 DPRINTF("hCard: %lu\n", cr->hCard);
470
471 #ifndef NO_WINDRVR
472 ret = (*ioctl_func) (fd, request, wdioctl);
473 #else
474 if (pport)
475 pport->close(cr->hCard);
476
477 pport = NULL;
478 #endif
479 }
480 break;
481
482 case EVENT_PULL:
483 DPRINTF("EVENT_PULL\n");
484 {
485 struct event *e = (struct event*)(wdheader->data);
486 #ifdef DEBUG
487 int i;
488
489 DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
490 e->handle, e->dwAction, e->dwStatus,
491 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
492 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
493 e->u.Usb.deviceId.dwProductId,
494 e->u.Usb.dwUniqueID, e->dwEventVer,
495 e->dwNumMatchTables);
496
497 for (i = 0; i < e->dwNumMatchTables; i++)
498 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
499 e->matchTables[i].VendorId,
500 e->matchTables[i].ProductId,
501 e->matchTables[i].bDeviceClass,
502 e->matchTables[i].bDeviceSubClass,
503 e->matchTables[i].bInterfaceClass,
504 e->matchTables[i].bInterfaceSubClass,
505 e->matchTables[i].bInterfaceProtocol);
506 #endif
507
508 #ifndef NO_WINDRVR
509 ret = (*ioctl_func) (fd, request, wdioctl);
510 #else
511 if (xpcu->dev) {
512 struct usb_interface *interface = xpcu->dev->config->interface;
513
514 e->dwCardType = xpcu->card_type;
515 e->dwAction = 1;
516 e->dwEventId = 109;
517 e->u.Usb.dwUniqueID = 110;
518 e->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor;
519 e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct;
520 e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass;
521 e->matchTables[0].bDeviceSubClass = xpcu->dev->descriptor.bDeviceSubClass;
522 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
523 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
524 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
525 }
526 #endif
527
528 #ifdef DEBUG
529 DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
530 e->handle, e->dwAction, e->dwStatus,
531 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
532 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
533 e->u.Usb.deviceId.dwProductId,
534 e->u.Usb.dwUniqueID, e->dwEventVer,
535 e->dwNumMatchTables);
536
537 for (i = 0; i < e->dwNumMatchTables; i++)
538 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
539 e->matchTables[i].VendorId,
540 e->matchTables[i].ProductId,
541 e->matchTables[i].bDeviceClass,
542 e->matchTables[i].bDeviceSubClass,
543 e->matchTables[i].bInterfaceClass,
544 e->matchTables[i].bInterfaceSubClass,
545 e->matchTables[i].bInterfaceProtocol);
546 #endif
547
548 }
549 break;
550
551 default:
552 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
553 #ifndef NO_WINDRVR
554 ret = (*ioctl_func) (fd, request, wdioctl);
555 #endif
556 break;
557 }
558
559 return ret;
560 }
561
562 int ioctl(int fd, unsigned long int request, ...) {
563 va_list args;
564 void *argp;
565 int ret;
566
567 if (!ioctl_func)
568 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
569
570 va_start (args, request);
571 argp = va_arg (args, void *);
572 va_end (args);
573
574 if (fd == windrvrfd)
575 ret = do_wdioctl(fd, request, argp);
576 else
577 ret = (*ioctl_func) (fd, request, argp);
578
579 return ret;
580 }
581
582 int open (const char *pathname, int flags, ...) {
583 static int (*func) (const char *, int, mode_t) = NULL;
584 mode_t mode = 0;
585 va_list args;
586 int fd;
587
588 if (!func)
589 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
590
591 if (flags & O_CREAT) {
592 va_start(args, flags);
593 mode = va_arg(args, mode_t);
594 va_end(args);
595 }
596
597 if (!strcmp (pathname, "/dev/windrvr6")) {
598 DPRINTF("opening windrvr6\n");
599 #ifdef NO_WINDRVR
600 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
601 #else
602 windrvrfd = fd = (*func) (pathname, flags, mode);
603 #endif
604 if (!xpcu)
605 xpcu = xpcu_open();
606
607 return fd;
608 }
609
610 return (*func) (pathname, flags, mode);
611 }
612
613 int close(int fd) {
614 static int (*func) (int) = NULL;
615
616 if (!func)
617 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
618
619 if (fd == windrvrfd && windrvrfd >= 0) {
620 DPRINTF("close windrvrfd\n");
621
622 if (xpcu)
623 xpcu_close(xpcu);
624
625 xpcu = NULL;
626 windrvrfd = -1;
627 }
628
629 return (*func) (fd);
630 }
631
632 FILE *fopen(const char *path, const char *mode) {
633 FILE *ret;
634 static FILE* (*func) (const char*, const char*) = NULL;
635 char buf[256];
636 int i;
637
638 if (!func)
639 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
640
641 for (i = 0; i < 4; i++) {
642 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
643 if (!strcmp(path, buf)) {
644 DPRINTF("open base-addr of parport%d\n", i);
645 if (config_is_real_pport(i)) {
646 ret = (*func) (path, mode);
647 } else {
648 ret = (*func) ("/dev/null", mode);
649 }
650
651 if (ret) {
652 baseaddrfp = ret;
653 baseaddrnum = i;
654 }
655
656 return ret;
657 }
658 }
659
660 ret = (*func) (path, mode);
661
662 if (!strcmp(path, "/proc/modules")) {
663 DPRINTF("opening /proc/modules\n");
664 #ifdef NO_WINDRVR
665 modulesfp = ret;
666 modules_read = 0;
667 #endif
668 }
669
670 return ret;
671 }
672
673 char *fgets(char *s, int size, FILE *stream) {
674 static char* (*func) (char*, int, FILE*) = NULL;
675 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
676 char buf[256];
677 char *ret = NULL;
678
679
680 if (!func)
681 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
682
683 if (modulesfp == stream) {
684 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
685 strcpy(s, modules[modules_read]);
686 ret = s;
687 modules_read++;
688 }
689 } else if (baseaddrfp == stream) {
690 snprintf(s, sizeof(buf), "%d\t%d\n",
691 (baseaddrnum) * 0x10,
692 ((baseaddrnum) * 0x10) + 0x400);
693 ret = s;
694 } else {
695 ret = (*func)(s,size,stream);
696 }
697
698 return ret;
699 }
700
701 int fclose(FILE *fp) {
702 static int (*func) (FILE*) = NULL;
703
704 if (!func)
705 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
706
707 if (fp == modulesfp) {
708 modulesfp = NULL;
709 }
710
711 if (fp == baseaddrfp) {
712 baseaddrfp = NULL;
713 }
714
715 return (*func)(fp);
716 }
717
718 int access(const char *pathname, int mode) {
719 static int (*func) (const char*, int);
720
721 if (!func)
722 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
723
724 if (pathname && !strcmp(pathname, "/dev/windrvr6")) {
725 return 0;
726 } else {
727 return (*func)(pathname, mode);
728 }
729 }
730
731 #if __WORDSIZE == 32
732 int uname (struct utsname *__name) {
733 static int (*func) (struct utsname*);
734 int ret;
735
736 if (!func)
737 func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname");
738
739 ret = (*func)(__name);
740
741 if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) {
742 strcpy(__name->machine, "i686");
743 }
744
745 return ret;
746 }
747 #endif
Impressum, Datenschutz