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