some cleanups
[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
2c2119eb 91 fprintf(stderr," Handle: %lu, Options: %lx, ncmds: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds);
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
2c2119eb 104 fprintf(stderr," Handle: %lu, Options: %lx, ncmds: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds);
9c9fd67c 105
106 hexdump(wdheader->data, wdheader->size);
107 //it->dwCounter = 0;
108 //it->fStopped = 1;
109 ret = (*ioctl_func) (fd, request, wdioctl);
110 fprintf(stderr,"\n\n");
111 hexdump(wdheader->data, wdheader->size);
da3ba95a 112 fprintf(stderr,"\n");
da3ba95a 113 }
da3ba95a 114 break;
da3ba95a 115
9c9fd67c 116 case USB_SET_INTERFACE:
117 fprintf(stderr,"USB_SET_INTERFACE\n");
da3ba95a 118 {
9c9fd67c 119 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
120
2c2119eb 121 fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions);
9c9fd67c 122 ret = (*ioctl_func) (fd, request, wdioctl);
da3ba95a 123 }
cdc711dc 124 break;
da3ba95a 125
9c9fd67c 126 case USB_GET_DEVICE_DATA:
127 fprintf(stderr,"USB_GET_DEVICE_DATA\n");
128 {
129 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
130 int pSize;
131
2c2119eb 132 fprintf(stderr, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions);
9c9fd67c 133 pSize = ugdd->dwBytes;
134 ret = (*ioctl_func) (fd, request, wdioctl);
135 if (pSize) {
136 hexdump(ugdd->pBuf, pSize);
137 fprintf(stderr, "\n");
da3ba95a 138 }
139 }
9c9fd67c 140 break;
141
da3ba95a 142 case TRANSFER:
9c9fd67c 143 fprintf(stderr,"TRANSFER\n");
144 ret = (*ioctl_func) (fd, request, wdioctl);
145 break;
146
da3ba95a 147 case EVENT_UNREGISTER:
9c9fd67c 148 fprintf(stderr,"EVENT_UNREGISTER\n");
149 ret = (*ioctl_func) (fd, request, wdioctl);
150 break;
151
da3ba95a 152 case INT_WAIT:
9c9fd67c 153 fprintf(stderr,"INT_WAIT\n");
154 ret = (*ioctl_func) (fd, request, wdioctl);
155 break;
156
da3ba95a 157 case CARD_UNREGISTER:
9c9fd67c 158 fprintf(stderr,"CARD_UNREGISTER\n");
159 ret = (*ioctl_func) (fd, request, wdioctl);
160 break;
161
da3ba95a 162 case EVENT_PULL:
9c9fd67c 163 fprintf(stderr,"EVENT_PULL\n");
164 ret = (*ioctl_func) (fd, request, wdioctl);
165 break;
166
da3ba95a 167 case EVENT_REGISTER:
9c9fd67c 168 fprintf(stderr,"EVENT_REGISTER\n");
169 ret = (*ioctl_func) (fd, request, wdioctl);
170 break;
171
da3ba95a 172 default:
9c9fd67c 173 ret = (*ioctl_func) (fd, request, wdioctl);
cdc711dc 174 }
da3ba95a 175
9c9fd67c 176 return ret;
cdc711dc 177}
178
179
180typedef int (*open_funcptr_t) (const char *, int, mode_t);
181
cdc711dc 182int open (const char *pathname, int flags, ...)
183{
184 static open_funcptr_t func = NULL;
185 mode_t mode = 0;
186 va_list args;
187 int fd;
188
189 if (!func)
190 func = (open_funcptr_t) dlsym (REAL_LIBC, "open");
191
192 if (flags & O_CREAT) {
193 va_start(args, flags);
194 mode = va_arg(args, mode_t);
195 va_end(args);
196 }
197
198 fd = (*func) (pathname, flags, mode);
199
200 if (!strcmp (pathname, "/dev/windrvr6")) {
201 fprintf(stderr,"opening windrvr6\n");
202 windrvrfd = fd;
203 }
204
205 return fd;
206}
207
208void diff(unsigned char *buf1, unsigned char *buf2, int len) {
209 int i;
210
211 for(i=0; i<len; i++) {
212 if (buf1[i] != buf2[i]) {
213 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]:'.'));
214 }
215 }
216}
217
218void hexdump(unsigned char *buf, int len) {
219 int i;
220
221 for(i=0; i<len; i++) {
222 fprintf(stderr,"%02x ", buf[i]);
223 if ((i % 16) == 15)
224 fprintf(stderr,"\n");
225 }
226}
227
228int ioctl(int fd, int request, ...)
229{
cdc711dc 230 va_list args;
231 void *argp;
cdc711dc 232 int ret;
233
9c9fd67c 234 if (!ioctl_func)
235 ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");
cdc711dc 236
237 va_start (args, request);
238 argp = va_arg (args, void *);
239 va_end (args);
240
9c9fd67c 241 if (fd == windrvrfd)
242 ret = do_wdioctl(fd, request, argp);
243 else
244 ret = (*ioctl_func) (fd, request, argp);
cdc711dc 245
cdc711dc 246 return ret;
247}
248
da3ba95a 249#if 0
cdc711dc 250void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
251{
252 static void* (*func) (void *, size_t, int, int, int, off_t) = NULL;
253 void *ret;
254
255 if (!func)
256 func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap");
257
258 ret = (*func) (start, length, prot, flags, fd, offset);
259 fprintf(stderr,"MMAP: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start, length, prot, flags, fd, offset, (unsigned int)ret);
260 mmapped = ret;
261 mmapplen = length;
262
263 return ret;
264}
265
266void *mmap64(void *start, size_t length, int prot, int flags, int fd, off64_t offset)
267{
268 static void* (*func) (void *, size_t, int, int, int, off64_t) = NULL;
269 void *ret;
270
271 if (!func)
272 func = (void* (*) (void *, size_t, int, int, int, off64_t)) dlsym (REAL_LIBC, "mmap64");
273
274 ret = (*func) (start, length, prot, flags, fd, offset);
275 fprintf(stderr,"MMAP64: %x, %d, %d, %d, %d, %lld -> %x\n", (unsigned int)start, length, prot, flags, fd, offset, (unsigned int)ret);
276 mmapped = ret;
277 mmapplen = length;
278
279 return ret;
280}
281
282void *mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
283{
284 static void* (*func) (void *, size_t, int, int, int, off_t) = NULL;
285 void *ret;
286
287 if (!func)
288 func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap2");
289
290 ret = (*func) (start, length, prot, flags, fd, pgoffset);
291 fprintf(stderr,"MMAP2: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start, length, prot, flags, fd, pgoffset, (unsigned int)ret);
292 mmapped = ret;
293 mmapplen = length;
294
295 return ret;
296}
297
298void *malloc(size_t size)
299{
300 static void* (*func) (size_t) = NULL;
301 void *ret;
302
303 if (!func)
304 func = (void* (*) (size_t)) dlsym(REAL_LIBC, "malloc");
305
306 ret = (*func) (size);
307
308 //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret);
309
310 return ret;
311}
da3ba95a 312#endif
cdc711dc 313
314
315#endif
Impressum, Datenschutz