]> git.zerfleddert.de Git - usb-driver/blame_incremental - usb-driver.c
make it possible to return an error everywhere
[usb-driver] / usb-driver.c
... / ...
CommitLineData
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 <errno.h>
39#include <inttypes.h>
40#include <sys/ioctl.h>
41#include <sys/utsname.h>
42#include <bits/wordsize.h>
43#include "usb-driver.h"
44#include "config.h"
45#include "xpcu.h"
46
47static int (*ioctl_func) (int, int, void *) = NULL;
48static int windrvrfd = -1;
49static unsigned long ppbase = 0;
50static unsigned long ecpbase = 0;
51static struct parport_config *pport = NULL;
52static struct xpcu_s *xpcu = NULL;
53static FILE *modulesfp = NULL;
54static FILE *baseaddrfp = NULL;
55static int baseaddrnum = 0;
56static int modules_read = 0;
57
58#define NO_WINDRVR 1
59
60void hexdump(unsigned char *buf, int len) {
61 int i;
62
63 for(i=0; i<len; i++) {
64 fprintf(stderr,"%02x ", buf[i]);
65 if ((i % 16) == 15)
66 fprintf(stderr,"\n");
67 }
68 fprintf(stderr,"\n");
69}
70
71
72static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
73 struct header_struct* wdheader = (struct header_struct*)wdioctl;
74 struct version_struct *version;
75 int ret = 0;
76
77 if (wdheader->magic != MAGIC) {
78 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
79 return (*ioctl_func) (fd, request, wdioctl);
80 }
81
82 switch(request & ~(0xc0000000)) {
83 case VERSION:
84 version = (struct version_struct*)(wdheader->data);
85 strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION);
86 version->versionul = 802;
87 DPRINTF("VERSION\n");
88 break;
89
90 case LICENSE:
91 DPRINTF("LICENSE\n");
92 break;
93
94 case CARD_REGISTER_OLD:
95 case CARD_REGISTER:
96 DPRINTF("CARD_REGISTER\n");
97 {
98 struct card_register* cr = (struct card_register*)(wdheader->data);
99
100 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
101 cr->Card.dwItems,
102 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
103 cr->Card.Item[0].I.IO.dwBytes,
104 cr->Card.Item[0].I.IO.dwBar);
105
106 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
107 cr->Card.dwItems,
108 (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
109 cr->Card.Item[1].I.IO.dwBytes,
110 cr->Card.Item[1].I.IO.dwBar);
111#ifndef NO_WINDRVR
112 ret = (*ioctl_func) (fd, request, wdioctl);
113#else
114
115 pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
116 if (!pport)
117 break;
118
119 ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
120
121 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
122
123 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr)
124 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
125
126 if (ret >= 0) {
127 cr->hCard = ret;
128 } else {
129 cr->hCard = 0;
130 }
131#endif
132 DPRINTF("<-hCard: %lu\n", cr->hCard);
133 }
134 break;
135
136 case USB_TRANSFER:
137 DPRINTF("USB_TRANSFER\n");
138 {
139 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
140
141#ifdef DEBUG
142 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
143 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
144 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
145 if (ut->dwPipeNum == 0) {
146 DPRINTF("-> setup packet: ");
147 hexdump(ut->SetupPacket, 8);
148 }
149
150 if (!ut->fRead && ut->dwBufferSize)
151 {
152 hexdump(ut->pBuffer, ut->dwBufferSize);
153 }
154#endif
155
156#ifndef NO_WINDRVR
157 ret = (*ioctl_func) (fd, request, wdioctl);
158#else
159 ret = xpcu_transfer(xpcu, ut);
160#endif
161
162#ifdef DEBUG
163 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
164 if (ut->fRead && ut->dwBytesTransferred)
165 {
166 DPRINTF("<- Read: ");
167 hexdump(ut->pBuffer, ut->dwBytesTransferred);
168 }
169#endif
170 }
171 break;
172
173 case INT_ENABLE_OLD:
174 case INT_ENABLE:
175 DPRINTF("INT_ENABLE\n");
176 {
177 struct interrupt *it = (struct interrupt*)(wdheader->data);
178
179 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
180 it->hInterrupt, it->dwOptions,
181 it->dwCmds, it->fEnableOk, it->dwCounter,
182 it->dwLost, it->fStopped);
183
184#ifndef NO_WINDRVR
185 ret = (*ioctl_func) (fd, request, wdioctl);
186#else
187 ret = xpcu_int_state(xpcu, it, ENABLE_INTERRUPT);
188#endif
189
190 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
191 it->hInterrupt, it->dwOptions,
192 it->dwCmds, it->fEnableOk, it->dwCounter,
193 it->dwLost, it->fStopped);
194 }
195
196 break;
197
198 case INT_DISABLE:
199 DPRINTF("INT_DISABLE\n");
200 {
201 struct interrupt *it = (struct interrupt*)(wdheader->data);
202
203 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
204 it->hInterrupt, it->dwOptions,
205 it->dwCmds, it->fEnableOk, it->dwCounter,
206 it->dwLost, it->fStopped);
207#ifndef NO_WINDRVR
208 ret = (*ioctl_func) (fd, request, wdioctl);
209#else
210 ret = xpcu_int_state(xpcu, it, DISABLE_INTERRUPT);
211#endif
212 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
213 it->hInterrupt, it->dwOptions,
214 it->dwCmds, it->fEnableOk, it->dwCounter,
215 it->dwLost, it->fStopped);
216 }
217 break;
218
219 case USB_SET_INTERFACE:
220 DPRINTF("USB_SET_INTERFACE\n");
221 {
222 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
223
224 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
225 usi->dwUniqueID, usi->dwInterfaceNum,
226 usi->dwAlternateSetting, usi->dwOptions);
227#ifndef NO_WINDRVR
228 ret = (*ioctl_func) (fd, request, wdioctl);
229#else
230 ret = xpcu_set_interface(xpcu, usi);
231#endif
232 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
233 usi->dwUniqueID, usi->dwInterfaceNum,
234 usi->dwAlternateSetting, usi->dwOptions);
235 }
236 break;
237
238 case USB_GET_DEVICE_DATA_OLD:
239 case USB_GET_DEVICE_DATA:
240 DPRINTF("USB_GET_DEVICE_DATA\n");
241 {
242 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
243
244 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
245 ugdd->dwUniqueID, ugdd->dwBytes,
246 ugdd->dwOptions);
247
248 ret = xpcu_deviceinfo(xpcu, ugdd);
249
250 }
251 break;
252
253 case EVENT_REGISTER_OLD:
254 case EVENT_REGISTER:
255 DPRINTF("EVENT_REGISTER\n");
256 {
257 struct event *e = (struct event*)(wdheader->data);
258#ifdef DEBUG
259 int i;
260#endif
261
262 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",
263 e->handle, e->dwAction,
264 e->dwStatus, e->dwEventId, e->dwCardType,
265 e->hKernelPlugIn, e->dwOptions,
266 e->u.Usb.deviceId.dwVendorId,
267 e->u.Usb.deviceId.dwProductId,
268 e->u.Usb.dwUniqueID, e->dwEventVer,
269 e->dwNumMatchTables);
270
271#ifndef NO_WINDRVR
272 ret = (*ioctl_func) (fd, request, wdioctl);
273#else
274 xpcu = xpcu_find(e);
275#endif
276
277#ifdef DEBUG
278 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",
279 e->handle, e->dwAction,
280 e->dwStatus, e->dwEventId, e->dwCardType,
281 e->hKernelPlugIn, e->dwOptions,
282 e->u.Usb.deviceId.dwVendorId,
283 e->u.Usb.deviceId.dwProductId,
284 e->u.Usb.dwUniqueID, e->dwEventVer,
285 e->dwNumMatchTables);
286
287 for (i = 0; i < e->dwNumMatchTables; i++)
288 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
289 e->matchTables[i].VendorId,
290 e->matchTables[i].ProductId,
291 e->matchTables[i].bDeviceClass,
292 e->matchTables[i].bDeviceSubClass,
293 e->matchTables[i].bInterfaceClass,
294 e->matchTables[i].bInterfaceSubClass,
295 e->matchTables[i].bInterfaceProtocol);
296#endif
297 }
298 break;
299
300 case TRANSFER_OLD:
301 case TRANSFER:
302 DPRINTF("TRANSFER\n");
303 {
304 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
305
306#ifndef NO_WINDRVR
307 ret = (*ioctl_func) (fd, request, wdioctl);
308#else
309 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1);
310#endif
311 }
312 break;
313
314 case MULTI_TRANSFER_OLD:
315 case MULTI_TRANSFER:
316 DPRINTF("MULTI_TRANSFER\n");
317 {
318 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
319 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
320#ifndef NO_WINDRVR
321 ret = (*ioctl_func) (fd, request, wdioctl);
322#else
323 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num);
324#endif
325 }
326 break;
327
328 case EVENT_UNREGISTER:
329 {
330 struct event *e = (struct event*)(wdheader->data);
331
332 DPRINTF("EVENT_UNREGISTER\n");
333#ifndef NO_WINDRVR
334 ret = (*ioctl_func) (fd, request, wdioctl);
335#else
336 ret = xpcu_close(xpcu, e);
337#endif
338 }
339 break;
340
341 case INT_WAIT:
342 DPRINTF("INT_WAIT\n");
343 {
344 struct interrupt *it = (struct interrupt*)(wdheader->data);
345
346 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
347 it->hInterrupt, it->dwOptions,
348 it->dwCmds, it->fEnableOk, it->dwCounter,
349 it->dwLost, it->fStopped);
350
351#ifndef NO_WINDRVR
352 ret = (*ioctl_func) (fd, request, wdioctl);
353#else
354 ret = xpcu_int_wait(xpcu, it);
355#endif
356
357 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
358 it->hInterrupt, it->dwOptions, it->dwCmds,
359 it->fEnableOk, it->dwCounter, it->dwLost,
360 it->fStopped);
361 }
362 break;
363
364 case CARD_UNREGISTER:
365 DPRINTF("CARD_UNREGISTER\n");
366 {
367 struct card_register* cr = (struct card_register*)(wdheader->data);
368
369 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
370 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
371 cr->Card.Item[0].I.IO.dwBytes,
372 cr->Card.Item[0].I.IO.dwBar);
373
374 DPRINTF("-> hCard: %lu\n", cr->hCard);
375
376#ifndef NO_WINDRVR
377 ret = (*ioctl_func) (fd, request, wdioctl);
378#else
379 if (pport)
380 pport->close(cr->hCard);
381
382 pport = NULL;
383#endif
384 }
385 break;
386
387 case EVENT_PULL:
388 DPRINTF("EVENT_PULL\n");
389 {
390 struct event *e = (struct event*)(wdheader->data);
391#ifdef DEBUG
392 int i;
393
394 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",
395 e->handle, e->dwAction, e->dwStatus,
396 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
397 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
398 e->u.Usb.deviceId.dwProductId,
399 e->u.Usb.dwUniqueID, e->dwEventVer,
400 e->dwNumMatchTables);
401
402 for (i = 0; i < e->dwNumMatchTables; i++)
403 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
404 e->matchTables[i].VendorId,
405 e->matchTables[i].ProductId,
406 e->matchTables[i].bDeviceClass,
407 e->matchTables[i].bDeviceSubClass,
408 e->matchTables[i].bInterfaceClass,
409 e->matchTables[i].bInterfaceSubClass,
410 e->matchTables[i].bInterfaceProtocol);
411#endif
412
413#ifndef NO_WINDRVR
414 ret = (*ioctl_func) (fd, request, wdioctl);
415#else
416 ret = xpcu_found(xpcu, e);
417#endif
418
419#ifdef DEBUG
420 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",
421 e->handle, e->dwAction, e->dwStatus,
422 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
423 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
424 e->u.Usb.deviceId.dwProductId,
425 e->u.Usb.dwUniqueID, e->dwEventVer,
426 e->dwNumMatchTables);
427
428 for (i = 0; i < e->dwNumMatchTables; i++)
429 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
430 e->matchTables[i].VendorId,
431 e->matchTables[i].ProductId,
432 e->matchTables[i].bDeviceClass,
433 e->matchTables[i].bDeviceSubClass,
434 e->matchTables[i].bInterfaceClass,
435 e->matchTables[i].bInterfaceSubClass,
436 e->matchTables[i].bInterfaceProtocol);
437#endif
438
439 }
440 break;
441
442 default:
443 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
444#ifndef NO_WINDRVR
445 ret = (*ioctl_func) (fd, request, wdioctl);
446#endif
447 break;
448 }
449
450 return ret;
451}
452
453int ioctl(int fd, unsigned long int request, ...) {
454 va_list args;
455 void *argp;
456 int ret;
457
458 if (!ioctl_func)
459 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
460
461 va_start (args, request);
462 argp = va_arg (args, void *);
463 va_end (args);
464
465 if (fd == windrvrfd)
466 ret = do_wdioctl(fd, request, argp);
467 else
468 ret = (*ioctl_func) (fd, request, argp);
469
470 return ret;
471}
472
473int open (const char *pathname, int flags, ...) {
474 static int (*func) (const char *, int, mode_t) = NULL;
475 mode_t mode = 0;
476 va_list args;
477 int fd;
478
479 if (!func)
480 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
481
482 if (flags & O_CREAT) {
483 va_start(args, flags);
484 mode = va_arg(args, mode_t);
485 va_end(args);
486 }
487
488 if (!strcmp (pathname, "/dev/windrvr6")) {
489 DPRINTF("opening windrvr6\n");
490#ifdef NO_WINDRVR
491 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
492#else
493 windrvrfd = fd = (*func) (pathname, flags, mode);
494#endif
495
496 return fd;
497 }
498
499 return (*func) (pathname, flags, mode);
500}
501
502int close(int fd) {
503 static int (*func) (int) = NULL;
504
505 if (!func)
506 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
507
508 if (fd == windrvrfd && windrvrfd >= 0) {
509 DPRINTF("close windrvrfd\n");
510
511 xpcu = NULL;
512 windrvrfd = -1;
513 }
514
515 return (*func) (fd);
516}
517
518FILE *fopen(const char *path, const char *mode) {
519 FILE *ret;
520 static FILE* (*func) (const char*, const char*) = NULL;
521 char buf[256];
522 int i;
523
524 if (!func)
525 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
526
527 for (i = 0; i < 4; i++) {
528 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
529 if (!strcmp(path, buf)) {
530 DPRINTF("open base-addr of parport%d\n", i);
531 if (config_is_real_pport(i)) {
532 ret = (*func) (path, mode);
533 } else {
534 ret = (*func) ("/dev/null", mode);
535 }
536
537 if (ret) {
538 baseaddrfp = ret;
539 baseaddrnum = i;
540 }
541
542 return ret;
543 }
544 }
545
546 ret = (*func) (path, mode);
547
548 if (!strcmp(path, "/proc/modules")) {
549 DPRINTF("opening /proc/modules\n");
550#ifdef NO_WINDRVR
551 modulesfp = ret;
552 modules_read = 0;
553#endif
554 }
555
556 return ret;
557}
558
559char *fgets(char *s, int size, FILE *stream) {
560 static char* (*func) (char*, int, FILE*) = NULL;
561 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
562 char buf[256];
563 char *ret = NULL;
564
565
566 if (!func)
567 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
568
569 if (modulesfp == stream) {
570 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
571 strcpy(s, modules[modules_read]);
572 ret = s;
573 modules_read++;
574 }
575 } else if (baseaddrfp == stream) {
576 snprintf(s, sizeof(buf), "%d\t%d\n",
577 (baseaddrnum) * 0x10,
578 ((baseaddrnum) * 0x10) + 0x400);
579 ret = s;
580 } else {
581 ret = (*func)(s,size,stream);
582 }
583
584 return ret;
585}
586
587int fclose(FILE *fp) {
588 static int (*func) (FILE*) = NULL;
589
590 if (!func)
591 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
592
593 if (fp == modulesfp) {
594 modulesfp = NULL;
595 }
596
597 if (fp == baseaddrfp) {
598 baseaddrfp = NULL;
599 }
600
601 return (*func)(fp);
602}
603
604int access(const char *pathname, int mode) {
605 static int (*func) (const char*, int);
606
607 if (!func)
608 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
609
610 if (pathname && !strcmp(pathname, "/dev/windrvr6")) {
611 return 0;
612 } else {
613 return (*func)(pathname, mode);
614 }
615}
616
617#if __WORDSIZE == 32
618int uname (struct utsname *__name) {
619 static int (*func) (struct utsname*);
620 int ret;
621
622 if (!func)
623 func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname");
624
625 ret = (*func)(__name);
626
627 if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) {
628 strcpy(__name->machine, "i686");
629 }
630
631 return ret;
632}
633#endif
Impressum, Datenschutz