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