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