]>
Commit | Line | Data |
---|---|---|
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 | ||
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; | |
28 | static unsigned long card_type; | |
29 | ||
30 | #define USE_LIBUSB 1 | |
31 | ||
32 | void hexdump(unsigned char *buf, int len); | |
33 | void diff(unsigned char *buf1, unsigned char *buf2, int len); | |
34 | ||
35 | int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) { | |
36 | struct header_struct* wdheader = (struct header_struct*)wdioctl; | |
37 | struct version_struct *version; | |
38 | int ret = 0; | |
39 | ||
40 | if (wdheader->magic != MAGIC) { | |
41 | fprintf(stderr,"!!!ERROR: magic header does not match!!!\n"); | |
42 | return (*ioctl_func) (fd, request, wdioctl); | |
43 | } | |
44 | ||
45 | switch(request) { | |
46 | case VERSION: | |
47 | version = (struct version_struct*)(wdheader->data); | |
48 | strcpy(version->version, "WinDriver no more"); | |
49 | version->versionul = 999; | |
50 | fprintf(stderr,"faking VERSION\n"); | |
51 | break; | |
52 | ||
53 | case LICENSE: | |
54 | fprintf(stderr,"faking LICENSE\n"); | |
55 | break; | |
56 | ||
57 | case CARD_REGISTER: | |
58 | { | |
59 | //struct card_register* cr = (struct card_register*)(wdheader->data); | |
60 | /* Todo: LPT-Port already in use */ | |
61 | } | |
62 | fprintf(stderr,"faking CARD_REGISTER\n"); | |
63 | break; | |
64 | ||
65 | case USB_TRANSFER: | |
66 | fprintf(stderr,"in USB_TRANSFER"); | |
67 | { | |
68 | struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data); | |
69 | ||
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); | |
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 | ||
80 | #ifndef USE_LIBUSB | |
81 | ret = (*ioctl_func) (fd, request, wdioctl); | |
82 | #endif | |
83 | ||
84 | fprintf(stderr,"Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write")); | |
85 | if (ut->fRead && ut->dwBytesTransferred) | |
86 | { | |
87 | fprintf(stderr,"Read: "); | |
88 | hexdump(ut->pBuffer, ut->dwBytesTransferred); | |
89 | } | |
90 | fprintf(stderr,"\n"); | |
91 | } | |
92 | break; | |
93 | ||
94 | case INT_ENABLE: | |
95 | fprintf(stderr,"faking INT_ENABLE"); | |
96 | { | |
97 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
98 | ||
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); | |
100 | ||
101 | it->fEnableOk = 1; | |
102 | //ret = (*ioctl_func) (fd, request, wdioctl); | |
103 | } | |
104 | ||
105 | break; | |
106 | ||
107 | case INT_DISABLE: | |
108 | fprintf(stderr,"INT_DISABLE\n"); | |
109 | { | |
110 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
111 | ||
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); | |
113 | //it->dwCounter = 0; | |
114 | //it->fStopped = 1; | |
115 | #ifndef USE_LIBUSB | |
116 | ret = (*ioctl_func) (fd, request, wdioctl); | |
117 | #endif | |
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); | |
119 | } | |
120 | break; | |
121 | ||
122 | case USB_SET_INTERFACE: | |
123 | fprintf(stderr,"USB_SET_INTERFACE\n"); | |
124 | { | |
125 | struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data); | |
126 | ||
127 | fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions); | |
128 | #ifndef USE_LIBUSB | |
129 | ret = (*ioctl_func) (fd, request, wdioctl); | |
130 | #endif | |
131 | } | |
132 | break; | |
133 | ||
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 | ||
140 | fprintf(stderr, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions); | |
141 | pSize = ugdd->dwBytes; | |
142 | #ifndef USE_LIBUSB | |
143 | ret = (*ioctl_func) (fd, request, wdioctl); | |
144 | #endif | |
145 | if (pSize) { | |
146 | hexdump(ugdd->pBuf, pSize); | |
147 | fprintf(stderr, "\n"); | |
148 | } | |
149 | } | |
150 | break; | |
151 | ||
152 | case EVENT_REGISTER: | |
153 | fprintf(stderr,"EVENT_REGISTER\n"); | |
154 | { | |
155 | struct event *e = (struct event*)(wdheader->data); | |
156 | struct usb_bus *bus; | |
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); | |
160 | for (i = 0; i < e->dwNumMatchTables; i++) { | |
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 | ||
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; | |
183 | card_type = e->dwCardType; | |
184 | } | |
185 | } | |
186 | } | |
187 | } | |
188 | } | |
189 | } | |
190 | ||
191 | #ifndef USE_LIBUSB | |
192 | ret = (*ioctl_func) (fd, request, wdioctl); | |
193 | #endif | |
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 | ||
201 | case TRANSFER: | |
202 | fprintf(stderr,"TRANSFER\n"); | |
203 | #ifndef USE_LIBUSB | |
204 | ret = (*ioctl_func) (fd, request, wdioctl); | |
205 | #endif | |
206 | break; | |
207 | ||
208 | case EVENT_UNREGISTER: | |
209 | fprintf(stderr,"EVENT_UNREGISTER\n"); | |
210 | #ifndef USE_LIBUSB | |
211 | ret = (*ioctl_func) (fd, request, wdioctl); | |
212 | #endif | |
213 | break; | |
214 | ||
215 | case INT_WAIT: | |
216 | fprintf(stderr,"INT_WAIT\n"); | |
217 | { | |
218 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
219 | ||
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); | |
221 | ||
222 | #ifndef USE_LIBUSB | |
223 | ret = (*ioctl_func) (fd, request, wdioctl); | |
224 | #else | |
225 | if (usb_cable) | |
226 | it->dwCounter++; | |
227 | #endif | |
228 | ||
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); | |
230 | } | |
231 | break; | |
232 | ||
233 | case CARD_UNREGISTER: | |
234 | fprintf(stderr,"CARD_UNREGISTER\n"); | |
235 | #ifndef USE_LIBUSB | |
236 | ret = (*ioctl_func) (fd, request, wdioctl); | |
237 | #endif | |
238 | break; | |
239 | ||
240 | case EVENT_PULL: | |
241 | fprintf(stderr,"EVENT_PULL\n"); | |
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 | ||
250 | #ifndef USE_LIBUSB | |
251 | ret = (*ioctl_func) (fd, request, wdioctl); | |
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 | |
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 | } | |
279 | break; | |
280 | ||
281 | default: | |
282 | fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request); | |
283 | #ifndef USE_LIBUSB | |
284 | ret = (*ioctl_func) (fd, request, wdioctl); | |
285 | #endif | |
286 | break; | |
287 | } | |
288 | ||
289 | return ret; | |
290 | } | |
291 | ||
292 | ||
293 | typedef int (*open_funcptr_t) (const char *, int, mode_t); | |
294 | ||
295 | int 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; | |
316 | if (!busses) { | |
317 | usb_init(); | |
318 | usb_find_busses(); | |
319 | usb_find_devices(); | |
320 | ||
321 | busses = usb_get_busses(); | |
322 | } | |
323 | } | |
324 | ||
325 | return fd; | |
326 | } | |
327 | ||
328 | void 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 | ||
338 | void 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 | ||
348 | int ioctl(int fd, int request, ...) | |
349 | { | |
350 | va_list args; | |
351 | void *argp; | |
352 | int ret; | |
353 | ||
354 | if (!ioctl_func) | |
355 | ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl"); | |
356 | ||
357 | va_start (args, request); | |
358 | argp = va_arg (args, void *); | |
359 | va_end (args); | |
360 | ||
361 | if (fd == windrvrfd) | |
362 | ret = do_wdioctl(fd, request, argp); | |
363 | else | |
364 | ret = (*ioctl_func) (fd, request, argp); | |
365 | ||
366 | return ret; | |
367 | } | |
368 | ||
369 | #if 0 | |
370 | void *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 | ||
386 | void *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 | ||
402 | void *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 | ||
418 | void *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 | } | |
432 | #endif | |
433 | ||
434 | ||
435 | #endif |