]> git.zerfleddert.de Git - usb-driver/blame - usb-driver.c
find 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;
cdc711dc 28
29void hexdump(unsigned char *buf, int len);
da3ba95a 30void diff(unsigned char *buf1, unsigned char *buf2, int len);
cdc711dc 31
9c9fd67c 32int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 33 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 34 struct version_struct *version;
35 int ret = 0;
cdc711dc 36
da3ba95a 37 if (wdheader->magic != MAGIC) {
2c2119eb 38 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
39 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 40 }
41
cdc711dc 42 switch(request) {
da3ba95a 43 case VERSION:
9c9fd67c 44 version = (struct version_struct*)(wdheader->data);
45 strcpy(version->version, "WinDriver no more");
46 version->versionul = 999;
47 fprintf(stderr,"faking VERSION\n");
cdc711dc 48 break;
2c2119eb 49
50 case LICENSE:
51 fprintf(stderr,"faking LICENSE\n");
52 break;
53
9c9fd67c 54 case CARD_REGISTER:
55 {
2c2119eb 56 //struct card_register* cr = (struct card_register*)(wdheader->data);
9c9fd67c 57 /* Todo: LPT-Port already in use */
58 }
59 fprintf(stderr,"faking CARD_REGISTER\n");
da3ba95a 60 break;
2c2119eb 61
da3ba95a 62 case USB_TRANSFER:
9c9fd67c 63 fprintf(stderr,"in USB_TRANSFER");
da3ba95a 64 {
65 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
66
2c2119eb 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);
9c9fd67c 68 fprintf(stderr,"setup packet: ");
69 hexdump(ut->SetupPacket, 8);
70 fprintf(stderr,"\n");
71 if (!ut->fRead && ut->dwBufferSize)
72 {
73 hexdump(ut->pBuffer, ut->dwBufferSize);
74 fprintf(stderr,"\n");
75 }
76
77 ret = (*ioctl_func) (fd, request, wdioctl);
78
2c2119eb 79 fprintf(stderr,"Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 80 if (ut->fRead && ut->dwBytesTransferred)
81 {
82 fprintf(stderr,"Read: ");
83 hexdump(ut->pBuffer, ut->dwBytesTransferred);
84 }
da3ba95a 85 fprintf(stderr,"\n");
86 }
cdc711dc 87 break;
2c2119eb 88
da3ba95a 89 case INT_ENABLE:
9c9fd67c 90 fprintf(stderr,"faking INT_ENABLE");
da3ba95a 91 {
9c9fd67c 92 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 93
53f639f2 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);
cdc711dc 95
9c9fd67c 96 it->fEnableOk = 1;
97 //ret = (*ioctl_func) (fd, request, wdioctl);
98 }
cdc711dc 99
cdc711dc 100 break;
9c9fd67c 101
da3ba95a 102 case INT_DISABLE:
9c9fd67c 103 fprintf(stderr,"INT_DISABLE\n");
da3ba95a 104 {
9c9fd67c 105 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 106
53f639f2 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);
9c9fd67c 108 //it->dwCounter = 0;
109 //it->fStopped = 1;
110 ret = (*ioctl_func) (fd, request, wdioctl);
53f639f2 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);
da3ba95a 112 }
da3ba95a 113 break;
da3ba95a 114
9c9fd67c 115 case USB_SET_INTERFACE:
116 fprintf(stderr,"USB_SET_INTERFACE\n");
da3ba95a 117 {
9c9fd67c 118 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
119
2c2119eb 120 fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions);
9c9fd67c 121 ret = (*ioctl_func) (fd, request, wdioctl);
da3ba95a 122 }
cdc711dc 123 break;
da3ba95a 124
9c9fd67c 125 case USB_GET_DEVICE_DATA:
126 fprintf(stderr,"USB_GET_DEVICE_DATA\n");
127 {
128 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
129 int pSize;
130
2c2119eb 131 fprintf(stderr, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions);
9c9fd67c 132 pSize = ugdd->dwBytes;
133 ret = (*ioctl_func) (fd, request, wdioctl);
134 if (pSize) {
135 hexdump(ugdd->pBuf, pSize);
136 fprintf(stderr, "\n");
da3ba95a 137 }
138 }
9c9fd67c 139 break;
140
b0f621dd 141 case EVENT_REGISTER:
142 fprintf(stderr,"EVENT_REGISTER\n");
143 {
144 struct event *e = (struct event*)(wdheader->data);
ac22d975 145 struct usb_bus *bus;
b0f621dd 146 int i;
147
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);
ac22d975 149 for (i = 0; i < e->dwNumMatchTables; i++) {
b0f621dd 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);
151
ac22d975 152 for (bus = busses; bus; bus = bus->next) {
153 struct usb_device *dev;
154
155 for (dev = bus->devices; dev; dev = dev->next) {
156 struct usb_device_descriptor *desc = &(dev->descriptor);
157
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;
163 int ai;
164
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");
171 usb_cable = dev;
172 }
173 }
174 }
175 }
176 }
177 }
178
b0f621dd 179 ret = (*ioctl_func) (fd, request, wdioctl);
180
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);
184 }
185 break;
186
da3ba95a 187 case TRANSFER:
9c9fd67c 188 fprintf(stderr,"TRANSFER\n");
189 ret = (*ioctl_func) (fd, request, wdioctl);
190 break;
191
da3ba95a 192 case EVENT_UNREGISTER:
9c9fd67c 193 fprintf(stderr,"EVENT_UNREGISTER\n");
194 ret = (*ioctl_func) (fd, request, wdioctl);
195 break;
196
da3ba95a 197 case INT_WAIT:
9c9fd67c 198 fprintf(stderr,"INT_WAIT\n");
b0f621dd 199 {
200 struct interrupt *it = (struct interrupt*)(wdheader->data);
201
53f639f2 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);
b0f621dd 203
b0f621dd 204 ret = (*ioctl_func) (fd, request, wdioctl);
53f639f2 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);
b0f621dd 206 }
9c9fd67c 207 break;
208
da3ba95a 209 case CARD_UNREGISTER:
9c9fd67c 210 fprintf(stderr,"CARD_UNREGISTER\n");
211 ret = (*ioctl_func) (fd, request, wdioctl);
212 break;
213
da3ba95a 214 case EVENT_PULL:
9c9fd67c 215 fprintf(stderr,"EVENT_PULL\n");
b1831983 216 {
217 struct event *e = (struct event*)(wdheader->data);
218 int i;
219
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);
223
224 ret = (*ioctl_func) (fd, request, wdioctl);
225
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);
229 }
9c9fd67c 230 break;
231
da3ba95a 232 default:
9c9fd67c 233 ret = (*ioctl_func) (fd, request, wdioctl);
cdc711dc 234 }
da3ba95a 235
9c9fd67c 236 return ret;
cdc711dc 237}
238
239
240typedef int (*open_funcptr_t) (const char *, int, mode_t);
241
cdc711dc 242int open (const char *pathname, int flags, ...)
243{
244 static open_funcptr_t func = NULL;
245 mode_t mode = 0;
246 va_list args;
247 int fd;
248
249 if (!func)
250 func = (open_funcptr_t) dlsym (REAL_LIBC, "open");
251
252 if (flags & O_CREAT) {
253 va_start(args, flags);
254 mode = va_arg(args, mode_t);
255 va_end(args);
256 }
257
258 fd = (*func) (pathname, flags, mode);
259
260 if (!strcmp (pathname, "/dev/windrvr6")) {
261 fprintf(stderr,"opening windrvr6\n");
262 windrvrfd = fd;
f10480d1 263 if (!busses) {
264 usb_init();
265 usb_find_busses();
266 usb_find_devices();
267
268 busses = usb_get_busses();
269 }
cdc711dc 270 }
271
272 return fd;
273}
274
275void diff(unsigned char *buf1, unsigned char *buf2, int len) {
276 int i;
277
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]:'.'));
281 }
282 }
283}
284
285void hexdump(unsigned char *buf, int len) {
286 int i;
287
288 for(i=0; i<len; i++) {
289 fprintf(stderr,"%02x ", buf[i]);
290 if ((i % 16) == 15)
291 fprintf(stderr,"\n");
292 }
293}
294
295int ioctl(int fd, int request, ...)
296{
cdc711dc 297 va_list args;
298 void *argp;
cdc711dc 299 int ret;
300
9c9fd67c 301 if (!ioctl_func)
302 ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");
cdc711dc 303
304 va_start (args, request);
305 argp = va_arg (args, void *);
306 va_end (args);
307
9c9fd67c 308 if (fd == windrvrfd)
309 ret = do_wdioctl(fd, request, argp);
310 else
311 ret = (*ioctl_func) (fd, request, argp);
cdc711dc 312
cdc711dc 313 return ret;
314}
315
da3ba95a 316#if 0
cdc711dc 317void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
318{
319 static void* (*func) (void *, size_t, int, int, int, off_t) = NULL;
320 void *ret;
321
322 if (!func)
323 func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap");
324
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);
327 mmapped = ret;
328 mmapplen = length;
329
330 return ret;
331}
332
333void *mmap64(void *start, size_t length, int prot, int flags, int fd, off64_t offset)
334{
335 static void* (*func) (void *, size_t, int, int, int, off64_t) = NULL;
336 void *ret;
337
338 if (!func)
339 func = (void* (*) (void *, size_t, int, int, int, off64_t)) dlsym (REAL_LIBC, "mmap64");
340
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);
343 mmapped = ret;
344 mmapplen = length;
345
346 return ret;
347}
348
349void *mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
350{
351 static void* (*func) (void *, size_t, int, int, int, off_t) = NULL;
352 void *ret;
353
354 if (!func)
355 func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap2");
356
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);
359 mmapped = ret;
360 mmapplen = length;
361
362 return ret;
363}
364
365void *malloc(size_t size)
366{
367 static void* (*func) (size_t) = NULL;
368 void *ret;
369
370 if (!func)
371 func = (void* (*) (size_t)) dlsym(REAL_LIBC, "malloc");
372
373 ret = (*func) (size);
374
375 //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret);
376
377 return ret;
378}
da3ba95a 379#endif
cdc711dc 380
381
382#endif
Impressum, Datenschutz