]>
git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
f49cb163ec35c26547cd28147519c9489d304118
1 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
6 #define REAL_LIBC RTLD_NEXT
8 #define REAL_LIBC ((void *) -1L)
17 #include <sys/types.h>
24 static int (*ioctl_func
) (int, int, void *) = NULL
;
25 static int windrvrfd
= 0;
26 static struct usb_bus
*busses
= NULL
;
27 static struct usb_device
*usb_cable
;
29 void hexdump(unsigned char *buf
, int len
);
30 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
);
32 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
33 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
34 struct version_struct
*version
;
37 if (wdheader
->magic
!= MAGIC
) {
38 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
39 return (*ioctl_func
) (fd
, request
, wdioctl
);
44 version
= (struct version_struct
*)(wdheader
->data
);
45 strcpy(version
->version
, "WinDriver no more");
46 version
->versionul
= 999;
47 fprintf(stderr
,"faking VERSION\n");
51 fprintf(stderr
,"faking LICENSE\n");
56 //struct card_register* cr = (struct card_register*)(wdheader->data);
57 /* Todo: LPT-Port already in use */
59 fprintf(stderr
,"faking CARD_REGISTER\n");
63 fprintf(stderr
,"in USB_TRANSFER");
65 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
67 fprintf(stderr
," unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n", ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
, ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
68 fprintf(stderr
,"setup packet: ");
69 hexdump(ut
->SetupPacket
, 8);
71 if (!ut
->fRead
&& ut
->dwBufferSize
)
73 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
77 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
79 fprintf(stderr
,"Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
80 if (ut
->fRead
&& ut
->dwBytesTransferred
)
82 fprintf(stderr
,"Read: ");
83 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
90 fprintf(stderr
,"faking INT_ENABLE");
92 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
94 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
97 //ret = (*ioctl_func) (fd, request, wdioctl);
103 fprintf(stderr
,"INT_DISABLE\n");
105 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
107 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
110 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
111 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
115 case USB_SET_INTERFACE
:
116 fprintf(stderr
,"USB_SET_INTERFACE\n");
118 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
120 fprintf(stderr
,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi
->dwUniqueID
, usi
->dwInterfaceNum
, usi
->dwAlternateSetting
, usi
->dwOptions
);
121 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
125 case USB_GET_DEVICE_DATA
:
126 fprintf(stderr
,"USB_GET_DEVICE_DATA\n");
128 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
131 fprintf(stderr
, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd
->dwUniqueID
, ugdd
->dwBytes
, ugdd
->dwOptions
);
132 pSize
= ugdd
->dwBytes
;
133 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
135 hexdump(ugdd
->pBuf
, pSize
);
136 fprintf(stderr
, "\n");
142 fprintf(stderr
,"EVENT_REGISTER\n");
144 struct event
*e
= (struct event
*)(wdheader
->data
);
148 fprintf(stderr
,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e
->handle
, e
->dwAction
, e
->dwStatus
, e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
, e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
, e
->u
.Usb
.deviceId
.dwProductId
, e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
, e
->dwNumMatchTables
);
149 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
150 fprintf(stderr
,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e
->matchTables
[i
].VendorId
, e
->matchTables
[i
].ProductId
, e
->matchTables
[i
].bDeviceClass
, e
->matchTables
[i
].bDeviceSubClass
, e
->matchTables
[i
].bInterfaceClass
, e
->matchTables
[i
].bInterfaceSubClass
, e
->matchTables
[i
].bInterfaceProtocol
);
152 for (bus
= busses
; bus
; bus
= bus
->next
) {
153 struct usb_device
*dev
;
155 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
156 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
158 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
159 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
160 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
161 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
162 struct usb_interface
*interface
= dev
->config
->interface
;
165 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
166 fprintf(stderr
, "intclass: %x, intsubclass: %x, intproto: %x\n", interface
->altsetting
[i
].bInterfaceClass
, interface
->altsetting
[i
].bInterfaceSubClass
, interface
->altsetting
[i
].bInterfaceProtocol
);
167 if ((interface
->altsetting
[i
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
168 (interface
->altsetting
[i
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
169 /* TODO: check interfaceClass! */
170 fprintf(stderr
,"!!!FOUND DEVICE WITH LIBUSB!!!\n");
179 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
181 fprintf(stderr
,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e
->handle
, e
->dwAction
, e
->dwStatus
, e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
, e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
, e
->u
.Usb
.deviceId
.dwProductId
, e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
, e
->dwNumMatchTables
);
182 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
183 fprintf(stderr
,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e
->matchTables
[i
].VendorId
, e
->matchTables
[i
].ProductId
, e
->matchTables
[i
].bDeviceClass
, e
->matchTables
[i
].bDeviceSubClass
, e
->matchTables
[i
].bInterfaceClass
, e
->matchTables
[i
].bInterfaceSubClass
, e
->matchTables
[i
].bInterfaceProtocol
);
188 fprintf(stderr
,"TRANSFER\n");
189 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
192 case EVENT_UNREGISTER
:
193 fprintf(stderr
,"EVENT_UNREGISTER\n");
194 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
198 fprintf(stderr
,"INT_WAIT\n");
200 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
202 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
204 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
205 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
209 case CARD_UNREGISTER
:
210 fprintf(stderr
,"CARD_UNREGISTER\n");
211 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
215 fprintf(stderr
,"EVENT_PULL\n");
217 struct event
*e
= (struct event
*)(wdheader
->data
);
220 fprintf(stderr
,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e
->handle
, e
->dwAction
, e
->dwStatus
, e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
, e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
, e
->u
.Usb
.deviceId
.dwProductId
, e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
, e
->dwNumMatchTables
);
221 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
222 fprintf(stderr
,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e
->matchTables
[i
].VendorId
, e
->matchTables
[i
].ProductId
, e
->matchTables
[i
].bDeviceClass
, e
->matchTables
[i
].bDeviceSubClass
, e
->matchTables
[i
].bInterfaceClass
, e
->matchTables
[i
].bInterfaceSubClass
, e
->matchTables
[i
].bInterfaceProtocol
);
224 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
226 fprintf(stderr
,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e
->handle
, e
->dwAction
, e
->dwStatus
, e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
, e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
, e
->u
.Usb
.deviceId
.dwProductId
, e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
, e
->dwNumMatchTables
);
227 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
228 fprintf(stderr
,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e
->matchTables
[i
].VendorId
, e
->matchTables
[i
].ProductId
, e
->matchTables
[i
].bDeviceClass
, e
->matchTables
[i
].bDeviceSubClass
, e
->matchTables
[i
].bInterfaceClass
, e
->matchTables
[i
].bInterfaceSubClass
, e
->matchTables
[i
].bInterfaceProtocol
);
233 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
240 typedef int (*open_funcptr_t
) (const char *, int, mode_t
);
242 int open (const char *pathname
, int flags
, ...)
244 static open_funcptr_t func
= NULL
;
250 func
= (open_funcptr_t
) dlsym (REAL_LIBC
, "open");
252 if (flags
& O_CREAT
) {
253 va_start(args
, flags
);
254 mode
= va_arg(args
, mode_t
);
258 fd
= (*func
) (pathname
, flags
, mode
);
260 if (!strcmp (pathname
, "/dev/windrvr6")) {
261 fprintf(stderr
,"opening windrvr6\n");
268 busses
= usb_get_busses();
275 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
) {
278 for(i
=0; i
<len
; i
++) {
279 if (buf1
[i
] != buf2
[i
]) {
280 fprintf(stderr
,"Diff at %d: %02x(%c)->%02x(%c)\n", i
, buf1
[i
], ((buf1
[i
] >= 31 && buf1
[i
] <= 126)?buf1
[i
]:'.'), buf2
[i
], ((buf2
[i
] >= 31 && buf2
[i
] <= 126)?buf2
[i
]:'.'));
285 void hexdump(unsigned char *buf
, int len
) {
288 for(i
=0; i
<len
; i
++) {
289 fprintf(stderr
,"%02x ", buf
[i
]);
291 fprintf(stderr
,"\n");
295 int ioctl(int fd
, int request
, ...)
302 ioctl_func
= (int (*) (int, int, void *)) dlsym (REAL_LIBC
, "ioctl");
304 va_start (args
, request
);
305 argp
= va_arg (args
, void *);
309 ret
= do_wdioctl(fd
, request
, argp
);
311 ret
= (*ioctl_func
) (fd
, request
, argp
);
317 void *mmap(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t offset
)
319 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
323 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap");
325 ret
= (*func
) (start
, length
, prot
, flags
, fd
, offset
);
326 fprintf(stderr
,"MMAP: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, offset
, (unsigned int)ret
);
333 void *mmap64(void *start
, size_t length
, int prot
, int flags
, int fd
, off64_t offset
)
335 static void* (*func
) (void *, size_t, int, int, int, off64_t
) = NULL
;
339 func
= (void* (*) (void *, size_t, int, int, int, off64_t
)) dlsym (REAL_LIBC
, "mmap64");
341 ret
= (*func
) (start
, length
, prot
, flags
, fd
, offset
);
342 fprintf(stderr
,"MMAP64: %x, %d, %d, %d, %d, %lld -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, offset
, (unsigned int)ret
);
349 void *mmap2(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t pgoffset
)
351 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
355 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap2");
357 ret
= (*func
) (start
, length
, prot
, flags
, fd
, pgoffset
);
358 fprintf(stderr
,"MMAP2: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, pgoffset
, (unsigned int)ret
);
365 void *malloc(size_t size
)
367 static void* (*func
) (size_t) = NULL
;
371 func
= (void* (*) (size_t)) dlsym(REAL_LIBC
, "malloc");
373 ret
= (*func
) (size
);
375 //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret);