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