impact finds the device!
[usb-driver] / usb-driver.c
CommitLineData
cdc711dc 1#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
2
3#define _GNU_SOURCE 1
4
5#if defined(RTLD_NEXT)
6#define REAL_LIBC RTLD_NEXT
7#else
8#define REAL_LIBC ((void *) -1L)
9#endif
10
11#include <dlfcn.h>
12#include <stdarg.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <sys/time.h>
20#include <stdio.h>
f10480d1 21#include <usb.h>
da3ba95a 22#include "xilinx.h"
cdc711dc 23
9c9fd67c 24static int (*ioctl_func) (int, int, void *) = NULL;
2c2119eb 25static int windrvrfd = 0;
f10480d1 26static struct usb_bus *busses = NULL;
ac22d975 27static struct usb_device *usb_cable;
292160ed 28static unsigned long card_type;
29
30#define USE_LIBUSB 1
cdc711dc 31
32void hexdump(unsigned char *buf, int len);
da3ba95a 33void diff(unsigned char *buf1, unsigned char *buf2, int len);
cdc711dc 34
9c9fd67c 35int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 36 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 37 struct version_struct *version;
38 int ret = 0;
cdc711dc 39
da3ba95a 40 if (wdheader->magic != MAGIC) {
2c2119eb 41 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
42 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 43 }
44
cdc711dc 45 switch(request) {
da3ba95a 46 case VERSION:
9c9fd67c 47 version = (struct version_struct*)(wdheader->data);
48 strcpy(version->version, "WinDriver no more");
49 version->versionul = 999;
50 fprintf(stderr,"faking VERSION\n");
cdc711dc 51 break;
2c2119eb 52
53 case LICENSE:
54 fprintf(stderr,"faking LICENSE\n");
55 break;
56
9c9fd67c 57 case CARD_REGISTER:
58 {
2c2119eb 59 //struct card_register* cr = (struct card_register*)(wdheader->data);
9c9fd67c 60 /* Todo: LPT-Port already in use */
61 }
62 fprintf(stderr,"faking CARD_REGISTER\n");
da3ba95a 63 break;
2c2119eb 64
da3ba95a 65 case USB_TRANSFER:
9c9fd67c 66 fprintf(stderr,"in USB_TRANSFER");
da3ba95a 67 {
68 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
69
2c2119eb 70 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);
9c9fd67c 71 fprintf(stderr,"setup packet: ");
72 hexdump(ut->SetupPacket, 8);
73 fprintf(stderr,"\n");
74 if (!ut->fRead && ut->dwBufferSize)
75 {
76 hexdump(ut->pBuffer, ut->dwBufferSize);
77 fprintf(stderr,"\n");
78 }
79
292160ed 80#ifndef USE_LIBUSB
9c9fd67c 81 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 82#endif
9c9fd67c 83
2c2119eb 84 fprintf(stderr,"Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 85 if (ut->fRead && ut->dwBytesTransferred)
86 {
87 fprintf(stderr,"Read: ");
88 hexdump(ut->pBuffer, ut->dwBytesTransferred);
89 }
da3ba95a 90 fprintf(stderr,"\n");
91 }
cdc711dc 92 break;
2c2119eb 93
da3ba95a 94 case INT_ENABLE:
9c9fd67c 95 fprintf(stderr,"faking INT_ENABLE");
da3ba95a 96 {
9c9fd67c 97 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 98
53f639f2 99 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);
cdc711dc 100
9c9fd67c 101 it->fEnableOk = 1;
102 //ret = (*ioctl_func) (fd, request, wdioctl);
103 }
cdc711dc 104
cdc711dc 105 break;
9c9fd67c 106
da3ba95a 107 case INT_DISABLE:
9c9fd67c 108 fprintf(stderr,"INT_DISABLE\n");
da3ba95a 109 {
9c9fd67c 110 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 111
53f639f2 112 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);
9c9fd67c 113 //it->dwCounter = 0;
114 //it->fStopped = 1;
292160ed 115#ifndef USE_LIBUSB
9c9fd67c 116 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 117#endif
53f639f2 118 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);
da3ba95a 119 }
da3ba95a 120 break;
da3ba95a 121
9c9fd67c 122 case USB_SET_INTERFACE:
123 fprintf(stderr,"USB_SET_INTERFACE\n");
da3ba95a 124 {
9c9fd67c 125 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
126
2c2119eb 127 fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions);
292160ed 128#ifndef USE_LIBUSB
9c9fd67c 129 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 130#endif
da3ba95a 131 }
cdc711dc 132 break;
da3ba95a 133
9c9fd67c 134 case USB_GET_DEVICE_DATA:
135 fprintf(stderr,"USB_GET_DEVICE_DATA\n");
136 {
137 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
138 int pSize;
139
2c2119eb 140 fprintf(stderr, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions);
9c9fd67c 141 pSize = ugdd->dwBytes;
292160ed 142#ifndef USE_LIBUSB
9c9fd67c 143 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 144#endif
9c9fd67c 145 if (pSize) {
146 hexdump(ugdd->pBuf, pSize);
147 fprintf(stderr, "\n");
da3ba95a 148 }
149 }
9c9fd67c 150 break;
151
b0f621dd 152 case EVENT_REGISTER:
153 fprintf(stderr,"EVENT_REGISTER\n");
154 {
155 struct event *e = (struct event*)(wdheader->data);
ac22d975 156 struct usb_bus *bus;
b0f621dd 157 int i;
158
159 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);
ac22d975 160 for (i = 0; i < e->dwNumMatchTables; i++) {
b0f621dd 161 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);
162
ac22d975 163 for (bus = busses; bus; bus = bus->next) {
164 struct usb_device *dev;
165
166 for (dev = bus->devices; dev; dev = dev->next) {
167 struct usb_device_descriptor *desc = &(dev->descriptor);
168
169 if((desc->idVendor == e->matchTables[i].VendorId) &&
170 (desc->idProduct == e->matchTables[i].ProductId) &&
171 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
172 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
173 struct usb_interface *interface = dev->config->interface;
174 int ai;
175
176 for (ai = 0; ai < interface->num_altsetting; ai++) {
177 fprintf(stderr, "intclass: %x, intsubclass: %x, intproto: %x\n", interface->altsetting[i].bInterfaceClass, interface->altsetting[i].bInterfaceSubClass, interface->altsetting[i].bInterfaceProtocol);
178 if ((interface->altsetting[i].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
179 (interface->altsetting[i].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
180 /* TODO: check interfaceClass! */
181 fprintf(stderr,"!!!FOUND DEVICE WITH LIBUSB!!!\n");
182 usb_cable = dev;
292160ed 183 card_type = e->dwCardType;
ac22d975 184 }
185 }
186 }
187 }
188 }
189 }
190
292160ed 191#ifndef USE_LIBUSB
b0f621dd 192 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 193#endif
b0f621dd 194
195 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);
196 for (i = 0; i < e->dwNumMatchTables; i++)
197 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);
198 }
199 break;
200
da3ba95a 201 case TRANSFER:
9c9fd67c 202 fprintf(stderr,"TRANSFER\n");
292160ed 203#ifndef USE_LIBUSB
9c9fd67c 204 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 205#endif
9c9fd67c 206 break;
207
da3ba95a 208 case EVENT_UNREGISTER:
9c9fd67c 209 fprintf(stderr,"EVENT_UNREGISTER\n");
292160ed 210#ifndef USE_LIBUSB
9c9fd67c 211 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 212#endif
9c9fd67c 213 break;
214
da3ba95a 215 case INT_WAIT:
9c9fd67c 216 fprintf(stderr,"INT_WAIT\n");
b0f621dd 217 {
218 struct interrupt *it = (struct interrupt*)(wdheader->data);
219
53f639f2 220 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);
b0f621dd 221
292160ed 222#ifndef USE_LIBUSB
b0f621dd 223 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 224#else
225 if (usb_cable)
226 it->dwCounter++;
227#endif
228
53f639f2 229 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);
b0f621dd 230 }
9c9fd67c 231 break;
232
da3ba95a 233 case CARD_UNREGISTER:
9c9fd67c 234 fprintf(stderr,"CARD_UNREGISTER\n");
292160ed 235#ifndef USE_LIBUSB
9c9fd67c 236 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 237#endif
9c9fd67c 238 break;
239
da3ba95a 240 case EVENT_PULL:
9c9fd67c 241 fprintf(stderr,"EVENT_PULL\n");
b1831983 242 {
243 struct event *e = (struct event*)(wdheader->data);
244 int i;
245
246 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);
247 for (i = 0; i < e->dwNumMatchTables; i++)
248 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);
249
292160ed 250#ifndef USE_LIBUSB
b1831983 251 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 252#else
253//EVENT_PULL
254//handle: 1, action: 0, status: 0, eventid: 0, cardtype: 0, kplug: 0, options: 0, dev: 0:0, unique: 0, ver: 1, nummatch: 1
255//match: dev: 0:0, class: 0, subclass: 0, intclass: 0, intsubclass: 0, intproto: 0
256//handle: 1, action: 1, status: 0, eventid: 109, cardtype: 4294967294, kplug: 0, options: 0, dev: 0:0, unique: 90, ver: 1, nummatch: 1
257//match: dev: 3fd:8, class: 0, subclass: 0, intclass: ff, intsubclass: 0, intproto: 0
258 if (usb_cable) {
259 struct usb_interface *interface = usb_cable->config->interface;
260
261 e->dwCardType = card_type;
262 e->dwAction = 1;
263 e->dwEventId = 109;
264 e->u.Usb.dwUniqueID = 4711;
265 e->matchTables[0].VendorId = usb_cable->descriptor.idVendor;
266 e->matchTables[0].ProductId = usb_cable->descriptor.idProduct;
267 e->matchTables[0].bDeviceClass = usb_cable->descriptor.bDeviceClass;
268 e->matchTables[0].bDeviceSubClass = usb_cable->descriptor.bDeviceSubClass;
269 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
270 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
271 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
272 }
273#endif
b1831983 274
275 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);
276 for (i = 0; i < e->dwNumMatchTables; i++)
277 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);
278 }
9c9fd67c 279 break;
280
da3ba95a 281 default:
292160ed 282 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
283#ifndef USE_LIBUSB
9c9fd67c 284 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 285#endif
286 break;
cdc711dc 287 }
da3ba95a 288
9c9fd67c 289 return ret;
cdc711dc 290}
291
292
293typedef int (*open_funcptr_t) (const char *, int, mode_t);
294
cdc711dc 295int open (const char *pathname, int flags, ...)
296{
297 static open_funcptr_t func = NULL;
298 mode_t mode = 0;
299 va_list args;
300 int fd;
301
302 if (!func)
303 func = (open_funcptr_t) dlsym (REAL_LIBC, "open");
304
305 if (flags & O_CREAT) {
306 va_start(args, flags);
307 mode = va_arg(args, mode_t);
308 va_end(args);
309 }
310
311 fd = (*func) (pathname, flags, mode);
312
313 if (!strcmp (pathname, "/dev/windrvr6")) {
314 fprintf(stderr,"opening windrvr6\n");
315 windrvrfd = fd;
f10480d1 316 if (!busses) {
317 usb_init();
318 usb_find_busses();
319 usb_find_devices();
320
321 busses = usb_get_busses();
322 }
cdc711dc 323 }
324
325 return fd;
326}
327
328void diff(unsigned char *buf1, unsigned char *buf2, int len) {
329 int i;
330
331 for(i=0; i<len; i++) {
332 if (buf1[i] != buf2[i]) {
333 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]:'.'));
334 }
335 }
336}
337
338void hexdump(unsigned char *buf, int len) {
339 int i;
340
341 for(i=0; i<len; i++) {
342 fprintf(stderr,"%02x ", buf[i]);
343 if ((i % 16) == 15)
344 fprintf(stderr,"\n");
345 }
346}
347
348int ioctl(int fd, int request, ...)
349{
cdc711dc 350 va_list args;
351 void *argp;
cdc711dc 352 int ret;
353
9c9fd67c 354 if (!ioctl_func)
355 ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");
cdc711dc 356
357 va_start (args, request);
358 argp = va_arg (args, void *);
359 va_end (args);
360
9c9fd67c 361 if (fd == windrvrfd)
362 ret = do_wdioctl(fd, request, argp);
363 else
364 ret = (*ioctl_func) (fd, request, argp);
cdc711dc 365
cdc711dc 366 return ret;
367}
368
da3ba95a 369#if 0
cdc711dc 370void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
371{
372 static void* (*func) (void *, size_t, int, int, int, off_t) = NULL;
373 void *ret;
374
375 if (!func)
376 func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap");
377
378 ret = (*func) (start, length, prot, flags, fd, offset);
379 fprintf(stderr,"MMAP: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start, length, prot, flags, fd, offset, (unsigned int)ret);
380 mmapped = ret;
381 mmapplen = length;
382
383 return ret;
384}
385
386void *mmap64(void *start, size_t length, int prot, int flags, int fd, off64_t offset)
387{
388 static void* (*func) (void *, size_t, int, int, int, off64_t) = NULL;
389 void *ret;
390
391 if (!func)
392 func = (void* (*) (void *, size_t, int, int, int, off64_t)) dlsym (REAL_LIBC, "mmap64");
393
394 ret = (*func) (start, length, prot, flags, fd, offset);
395 fprintf(stderr,"MMAP64: %x, %d, %d, %d, %d, %lld -> %x\n", (unsigned int)start, length, prot, flags, fd, offset, (unsigned int)ret);
396 mmapped = ret;
397 mmapplen = length;
398
399 return ret;
400}
401
402void *mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
403{
404 static void* (*func) (void *, size_t, int, int, int, off_t) = NULL;
405 void *ret;
406
407 if (!func)
408 func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap2");
409
410 ret = (*func) (start, length, prot, flags, fd, pgoffset);
411 fprintf(stderr,"MMAP2: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start, length, prot, flags, fd, pgoffset, (unsigned int)ret);
412 mmapped = ret;
413 mmapplen = length;
414
415 return ret;
416}
417
418void *malloc(size_t size)
419{
420 static void* (*func) (size_t) = NULL;
421 void *ret;
422
423 if (!func)
424 func = (void* (*) (size_t)) dlsym(REAL_LIBC, "malloc");
425
426 ret = (*func) (size);
427
428 //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret);
429
430 return ret;
431}
da3ba95a 432#endif
cdc711dc 433
434
435#endif
Impressum, Datenschutz