]> git.zerfleddert.de Git - usb-driver/blame - usb-driver.c
a bit 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];
9c9fd67c 25static int (*ioctl_func) (int, int, void *) = NULL;
cdc711dc 26
27void hexdump(unsigned char *buf, int len);
da3ba95a 28void diff(unsigned char *buf1, unsigned char *buf2, int len);
cdc711dc 29
9c9fd67c 30int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 31 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 32 struct version_struct *version;
33 int ret = 0;
cdc711dc 34
da3ba95a 35 if (wdheader->magic != MAGIC) {
cdc711dc 36 fprintf(stderr,"!!!ERROR: Header does not match!!!\n");
37 return;
38 }
39
cdc711dc 40 switch(request) {
da3ba95a 41 case VERSION:
9c9fd67c 42 version = (struct version_struct*)(wdheader->data);
43 strcpy(version->version, "WinDriver no more");
44 version->versionul = 999;
45 fprintf(stderr,"faking VERSION\n");
cdc711dc 46 break;
9c9fd67c 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");
da3ba95a 53 break;
54 case USB_TRANSFER:
9c9fd67c 55 fprintf(stderr,"in USB_TRANSFER");
da3ba95a 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);
9c9fd67c 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 }
da3ba95a 77 fprintf(stderr,"\n");
78 }
cdc711dc 79 break;
da3ba95a 80 case INT_ENABLE:
9c9fd67c 81 fprintf(stderr,"faking INT_ENABLE");
da3ba95a 82 {
9c9fd67c 83 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 84
9c9fd67c 85 fprintf(stderr," Handle: %d, Options: %x, ncmds: %d\n", it->hInterrupt, it->dwOptions, it->dwCmds);
cdc711dc 86
9c9fd67c 87 it->fEnableOk = 1;
88 //ret = (*ioctl_func) (fd, request, wdioctl);
89 }
cdc711dc 90
cdc711dc 91 break;
9c9fd67c 92
da3ba95a 93 case INT_DISABLE:
9c9fd67c 94 fprintf(stderr,"INT_DISABLE\n");
da3ba95a 95 {
9c9fd67c 96 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 97
9c9fd67c 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);
da3ba95a 106 fprintf(stderr,"\n");
da3ba95a 107 }
da3ba95a 108 break;
da3ba95a 109
9c9fd67c 110 case USB_SET_INTERFACE:
111 fprintf(stderr,"USB_SET_INTERFACE\n");
da3ba95a 112 {
9c9fd67c 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);
da3ba95a 117 }
cdc711dc 118 break;
da3ba95a 119
9c9fd67c 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");
da3ba95a 132 }
133 }
9c9fd67c 134 break;
135
da3ba95a 136 case LICENSE:
9c9fd67c 137 fprintf(stderr,"faking LICENSE\n");
138 break;
139
da3ba95a 140 case TRANSFER:
9c9fd67c 141 fprintf(stderr,"TRANSFER\n");
142 ret = (*ioctl_func) (fd, request, wdioctl);
143 break;
144
da3ba95a 145 case EVENT_UNREGISTER:
9c9fd67c 146 fprintf(stderr,"EVENT_UNREGISTER\n");
147 ret = (*ioctl_func) (fd, request, wdioctl);
148 break;
149
da3ba95a 150 case INT_WAIT:
9c9fd67c 151 fprintf(stderr,"INT_WAIT\n");
152 ret = (*ioctl_func) (fd, request, wdioctl);
153 break;
154
da3ba95a 155 case CARD_UNREGISTER:
9c9fd67c 156 fprintf(stderr,"CARD_UNREGISTER\n");
157 ret = (*ioctl_func) (fd, request, wdioctl);
158 break;
159
da3ba95a 160 case EVENT_PULL:
9c9fd67c 161 fprintf(stderr,"EVENT_PULL\n");
162 ret = (*ioctl_func) (fd, request, wdioctl);
163 break;
164
da3ba95a 165 case EVENT_REGISTER:
9c9fd67c 166 fprintf(stderr,"EVENT_REGISTER\n");
167 ret = (*ioctl_func) (fd, request, wdioctl);
168 break;
169
da3ba95a 170 default:
9c9fd67c 171 ret = (*ioctl_func) (fd, request, wdioctl);
cdc711dc 172 }
da3ba95a 173
9c9fd67c 174 return ret;
cdc711dc 175}
176
177
178typedef int (*open_funcptr_t) (const char *, int, mode_t);
179
180static windrvrfd = 0;
181static void* mmapped = NULL;
182static size_t mmapplen = 0;
183
184int 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
210void 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
220void 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
230int ioctl(int fd, int request, ...)
231{
cdc711dc 232 va_list args;
233 void *argp;
cdc711dc 234 int ret;
235
9c9fd67c 236 if (!ioctl_func)
237 ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");
cdc711dc 238
239 va_start (args, request);
240 argp = va_arg (args, void *);
241 va_end (args);
242
9c9fd67c 243 if (fd == windrvrfd)
244 ret = do_wdioctl(fd, request, argp);
245 else
246 ret = (*ioctl_func) (fd, request, argp);
cdc711dc 247
cdc711dc 248 return ret;
249}
250
da3ba95a 251#if 0
cdc711dc 252void *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
268void *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
284void *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
300void *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}
da3ba95a 314#endif
cdc711dc 315
316
317#endif
Impressum, Datenschutz