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