]> cvs.zerfleddert.de Git - usb-driver/blame - usb-driver.c
init libusb
[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;
cdc711dc 27
28void hexdump(unsigned char *buf, int len);
da3ba95a 29void diff(unsigned char *buf1, unsigned char *buf2, int len);
cdc711dc 30
9c9fd67c 31int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 32 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 33 struct version_struct *version;
34 int ret = 0;
cdc711dc 35
da3ba95a 36 if (wdheader->magic != MAGIC) {
2c2119eb 37 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
38 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 39 }
40
cdc711dc 41 switch(request) {
da3ba95a 42 case VERSION:
9c9fd67c 43 version = (struct version_struct*)(wdheader->data);
44 strcpy(version->version, "WinDriver no more");
45 version->versionul = 999;
46 fprintf(stderr,"faking VERSION\n");
cdc711dc 47 break;
2c2119eb 48
49 case LICENSE:
50 fprintf(stderr,"faking LICENSE\n");
51 break;
52
9c9fd67c 53 case CARD_REGISTER:
54 {
2c2119eb 55 //struct card_register* cr = (struct card_register*)(wdheader->data);
9c9fd67c 56 /* Todo: LPT-Port already in use */
57 }
58 fprintf(stderr,"faking CARD_REGISTER\n");
da3ba95a 59 break;
2c2119eb 60
da3ba95a 61 case USB_TRANSFER:
9c9fd67c 62 fprintf(stderr,"in USB_TRANSFER");
da3ba95a 63 {
64 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
65
2c2119eb 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);
9c9fd67c 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
2c2119eb 78 fprintf(stderr,"Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 79 if (ut->fRead && ut->dwBytesTransferred)
80 {
81 fprintf(stderr,"Read: ");
82 hexdump(ut->pBuffer, ut->dwBytesTransferred);
83 }
da3ba95a 84 fprintf(stderr,"\n");
85 }
cdc711dc 86 break;
2c2119eb 87
da3ba95a 88 case INT_ENABLE:
9c9fd67c 89 fprintf(stderr,"faking INT_ENABLE");
da3ba95a 90 {
9c9fd67c 91 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 92
53f639f2 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);
cdc711dc 94
9c9fd67c 95 it->fEnableOk = 1;
96 //ret = (*ioctl_func) (fd, request, wdioctl);
97 }
cdc711dc 98
cdc711dc 99 break;
9c9fd67c 100
da3ba95a 101 case INT_DISABLE:
9c9fd67c 102 fprintf(stderr,"INT_DISABLE\n");
da3ba95a 103 {
9c9fd67c 104 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 105
53f639f2 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);
9c9fd67c 107 //it->dwCounter = 0;
108 //it->fStopped = 1;
109 ret = (*ioctl_func) (fd, request, wdioctl);
53f639f2 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);
da3ba95a 111 }
da3ba95a 112 break;
da3ba95a 113
9c9fd67c 114 case USB_SET_INTERFACE:
115 fprintf(stderr,"USB_SET_INTERFACE\n");
da3ba95a 116 {
9c9fd67c 117 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
118
2c2119eb 119 fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions);
9c9fd67c 120 ret = (*ioctl_func) (fd, request, wdioctl);
da3ba95a 121 }
cdc711dc 122 break;
da3ba95a 123
9c9fd67c 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
2c2119eb 130 fprintf(stderr, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions);
9c9fd67c 131 pSize = ugdd->dwBytes;
132 ret = (*ioctl_func) (fd, request, wdioctl);
133 if (pSize) {
134 hexdump(ugdd->pBuf, pSize);
135 fprintf(stderr, "\n");
da3ba95a 136 }
137 }
9c9fd67c 138 break;
139
b0f621dd 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
da3ba95a 158 case TRANSFER:
9c9fd67c 159 fprintf(stderr,"TRANSFER\n");
160 ret = (*ioctl_func) (fd, request, wdioctl);
161 break;
162
da3ba95a 163 case EVENT_UNREGISTER:
9c9fd67c 164 fprintf(stderr,"EVENT_UNREGISTER\n");
165 ret = (*ioctl_func) (fd, request, wdioctl);
166 break;
167
da3ba95a 168 case INT_WAIT:
9c9fd67c 169 fprintf(stderr,"INT_WAIT\n");
b0f621dd 170 {
171 struct interrupt *it = (struct interrupt*)(wdheader->data);
172
53f639f2 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);
b0f621dd 174
b0f621dd 175 ret = (*ioctl_func) (fd, request, wdioctl);
53f639f2 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);
b0f621dd 177 }
9c9fd67c 178 break;
179
da3ba95a 180 case CARD_UNREGISTER:
9c9fd67c 181 fprintf(stderr,"CARD_UNREGISTER\n");
182 ret = (*ioctl_func) (fd, request, wdioctl);
183 break;
184
da3ba95a 185 case EVENT_PULL:
9c9fd67c 186 fprintf(stderr,"EVENT_PULL\n");
b1831983 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 }
9c9fd67c 201 break;
202
da3ba95a 203 default:
9c9fd67c 204 ret = (*ioctl_func) (fd, request, wdioctl);
cdc711dc 205 }
da3ba95a 206
9c9fd67c 207 return ret;
cdc711dc 208}
209
210
211typedef int (*open_funcptr_t) (const char *, int, mode_t);
212
cdc711dc 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;
f10480d1 234 if (!busses) {
235 usb_init();
236 usb_find_busses();
237 usb_find_devices();
238
239 busses = usb_get_busses();
240 }
cdc711dc 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{
cdc711dc 268 va_list args;
269 void *argp;
cdc711dc 270 int ret;
271
9c9fd67c 272 if (!ioctl_func)
273 ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");
cdc711dc 274
275 va_start (args, request);
276 argp = va_arg (args, void *);
277 va_end (args);
278
9c9fd67c 279 if (fd == windrvrfd)
280 ret = do_wdioctl(fd, request, argp);
281 else
282 ret = (*ioctl_func) (fd, request, argp);
cdc711dc 283
cdc711dc 284 return ret;
285}
286
da3ba95a 287#if 0
cdc711dc 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}
da3ba95a 350#endif
cdc711dc 351
352
353#endif
Impressum, Datenschutz