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