]> git.zerfleddert.de Git - usb-driver/blame_incremental - usb-driver.c
find the device!
[usb-driver] / usb-driver.c
... / ...
CommitLineData
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>
21#include <usb.h>
22#include "xilinx.h"
23
24static int (*ioctl_func) (int, int, void *) = NULL;
25static int windrvrfd = 0;
26static struct usb_bus *busses = NULL;
27static struct usb_device *usb_cable;
28
29void hexdump(unsigned char *buf, int len);
30void diff(unsigned char *buf1, unsigned char *buf2, int len);
31
32int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
33 struct header_struct* wdheader = (struct header_struct*)wdioctl;
34 struct version_struct *version;
35 int ret = 0;
36
37 if (wdheader->magic != MAGIC) {
38 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
39 return (*ioctl_func) (fd, request, wdioctl);
40 }
41
42 switch(request) {
43 case VERSION:
44 version = (struct version_struct*)(wdheader->data);
45 strcpy(version->version, "WinDriver no more");
46 version->versionul = 999;
47 fprintf(stderr,"faking VERSION\n");
48 break;
49
50 case LICENSE:
51 fprintf(stderr,"faking LICENSE\n");
52 break;
53
54 case CARD_REGISTER:
55 {
56 //struct card_register* cr = (struct card_register*)(wdheader->data);
57 /* Todo: LPT-Port already in use */
58 }
59 fprintf(stderr,"faking CARD_REGISTER\n");
60 break;
61
62 case USB_TRANSFER:
63 fprintf(stderr,"in USB_TRANSFER");
64 {
65 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
66
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);
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
79 fprintf(stderr,"Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
80 if (ut->fRead && ut->dwBytesTransferred)
81 {
82 fprintf(stderr,"Read: ");
83 hexdump(ut->pBuffer, ut->dwBytesTransferred);
84 }
85 fprintf(stderr,"\n");
86 }
87 break;
88
89 case INT_ENABLE:
90 fprintf(stderr,"faking INT_ENABLE");
91 {
92 struct interrupt *it = (struct interrupt*)(wdheader->data);
93
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);
95
96 it->fEnableOk = 1;
97 //ret = (*ioctl_func) (fd, request, wdioctl);
98 }
99
100 break;
101
102 case INT_DISABLE:
103 fprintf(stderr,"INT_DISABLE\n");
104 {
105 struct interrupt *it = (struct interrupt*)(wdheader->data);
106
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);
108 //it->dwCounter = 0;
109 //it->fStopped = 1;
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);
112 }
113 break;
114
115 case USB_SET_INTERFACE:
116 fprintf(stderr,"USB_SET_INTERFACE\n");
117 {
118 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
119
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);
122 }
123 break;
124
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
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);
134 if (pSize) {
135 hexdump(ugdd->pBuf, pSize);
136 fprintf(stderr, "\n");
137 }
138 }
139 break;
140
141 case EVENT_REGISTER:
142 fprintf(stderr,"EVENT_REGISTER\n");
143 {
144 struct event *e = (struct event*)(wdheader->data);
145 struct usb_bus *bus;
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);
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);
151
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
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
187 case TRANSFER:
188 fprintf(stderr,"TRANSFER\n");
189 ret = (*ioctl_func) (fd, request, wdioctl);
190 break;
191
192 case EVENT_UNREGISTER:
193 fprintf(stderr,"EVENT_UNREGISTER\n");
194 ret = (*ioctl_func) (fd, request, wdioctl);
195 break;
196
197 case INT_WAIT:
198 fprintf(stderr,"INT_WAIT\n");
199 {
200 struct interrupt *it = (struct interrupt*)(wdheader->data);
201
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);
203
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);
206 }
207 break;
208
209 case CARD_UNREGISTER:
210 fprintf(stderr,"CARD_UNREGISTER\n");
211 ret = (*ioctl_func) (fd, request, wdioctl);
212 break;
213
214 case EVENT_PULL:
215 fprintf(stderr,"EVENT_PULL\n");
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 }
230 break;
231
232 default:
233 ret = (*ioctl_func) (fd, request, wdioctl);
234 }
235
236 return ret;
237}
238
239
240typedef int (*open_funcptr_t) (const char *, int, mode_t);
241
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;
263 if (!busses) {
264 usb_init();
265 usb_find_busses();
266 usb_find_devices();
267
268 busses = usb_get_busses();
269 }
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{
297 va_list args;
298 void *argp;
299 int ret;
300
301 if (!ioctl_func)
302 ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");
303
304 va_start (args, request);
305 argp = va_arg (args, void *);
306 va_end (args);
307
308 if (fd == windrvrfd)
309 ret = do_wdioctl(fd, request, argp);
310 else
311 ret = (*ioctl_func) (fd, request, argp);
312
313 return ret;
314}
315
316#if 0
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}
379#endif
380
381
382#endif
Impressum, Datenschutz