]>
git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
02b410633f153b1013774b42163defacdc385743
1 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
6 #define REAL_LIBC RTLD_NEXT
8 #define REAL_LIBC ((void *) -1L)
17 #include <sys/types.h>
24 static unsigned char lastbuf
[4096];
25 static int (*ioctl_func
) (int, int, void *) = NULL
;
27 void hexdump(unsigned char *buf
, int len
);
28 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
);
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
;
35 if (wdheader
->magic
!= MAGIC
) {
36 fprintf(stderr
,"!!!ERROR: Header does not match!!!\n");
42 version
= (struct version_struct
*)(wdheader
->data
);
43 strcpy(version
->version
, "WinDriver no more");
44 version
->versionul
= 999;
45 fprintf(stderr
,"faking VERSION\n");
49 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
50 /* Todo: LPT-Port already in use */
52 fprintf(stderr
,"faking CARD_REGISTER\n");
55 fprintf(stderr
,"in USB_TRANSFER");
57 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
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);
63 if (!ut
->fRead
&& ut
->dwBufferSize
)
65 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
69 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
71 fprintf(stderr
,"Transferred: %d (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
72 if (ut
->fRead
&& ut
->dwBytesTransferred
)
74 fprintf(stderr
,"Read: ");
75 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
81 fprintf(stderr
,"faking INT_ENABLE");
83 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
85 fprintf(stderr
," Handle: %d, Options: %x, ncmds: %d\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
);
88 //ret = (*ioctl_func) (fd, request, wdioctl);
94 fprintf(stderr
,"INT_DISABLE\n");
96 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
98 fprintf(stderr
," Handle: %d, Options: %x, ncmds: %d\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
);
100 hexdump(wdheader
->data
, wdheader
->size
);
103 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
104 fprintf(stderr
,"\n\n");
105 hexdump(wdheader
->data
, wdheader
->size
);
106 fprintf(stderr
,"\n");
110 case USB_SET_INTERFACE
:
111 fprintf(stderr
,"USB_SET_INTERFACE\n");
113 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
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
);
120 case USB_GET_DEVICE_DATA
:
121 fprintf(stderr
,"USB_GET_DEVICE_DATA\n");
123 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
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
);
130 hexdump(ugdd
->pBuf
, pSize
);
131 fprintf(stderr
, "\n");
137 fprintf(stderr
,"faking LICENSE\n");
141 fprintf(stderr
,"TRANSFER\n");
142 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
145 case EVENT_UNREGISTER
:
146 fprintf(stderr
,"EVENT_UNREGISTER\n");
147 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
151 fprintf(stderr
,"INT_WAIT\n");
152 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
155 case CARD_UNREGISTER
:
156 fprintf(stderr
,"CARD_UNREGISTER\n");
157 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
161 fprintf(stderr
,"EVENT_PULL\n");
162 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
166 fprintf(stderr
,"EVENT_REGISTER\n");
167 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
171 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
178 typedef int (*open_funcptr_t
) (const char *, int, mode_t
);
180 static windrvrfd
= 0;
181 static void* mmapped
= NULL
;
182 static size_t mmapplen
= 0;
184 int open (const char *pathname
, int flags
, ...)
186 static open_funcptr_t func
= NULL
;
192 func
= (open_funcptr_t
) dlsym (REAL_LIBC
, "open");
194 if (flags
& O_CREAT
) {
195 va_start(args
, flags
);
196 mode
= va_arg(args
, mode_t
);
200 fd
= (*func
) (pathname
, flags
, mode
);
202 if (!strcmp (pathname
, "/dev/windrvr6")) {
203 fprintf(stderr
,"opening windrvr6\n");
210 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
) {
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
]:'.'));
220 void hexdump(unsigned char *buf
, int len
) {
223 for(i
=0; i
<len
; i
++) {
224 fprintf(stderr
,"%02x ", buf
[i
]);
226 fprintf(stderr
,"\n");
230 int ioctl(int fd
, int request
, ...)
237 ioctl_func
= (int (*) (int, int, void *)) dlsym (REAL_LIBC
, "ioctl");
239 va_start (args
, request
);
240 argp
= va_arg (args
, void *);
244 ret
= do_wdioctl(fd
, request
, argp
);
246 ret
= (*ioctl_func
) (fd
, request
, argp
);
252 void *mmap(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t offset
)
254 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
258 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap");
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
);
268 void *mmap64(void *start
, size_t length
, int prot
, int flags
, int fd
, off64_t offset
)
270 static void* (*func
) (void *, size_t, int, int, int, off64_t
) = NULL
;
274 func
= (void* (*) (void *, size_t, int, int, int, off64_t
)) dlsym (REAL_LIBC
, "mmap64");
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
);
284 void *mmap2(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t pgoffset
)
286 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
290 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap2");
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
);
300 void *malloc(size_t size
)
302 static void* (*func
) (size_t) = NULL
;
306 func
= (void* (*) (size_t)) dlsym(REAL_LIBC
, "malloc");
308 ret
= (*func
) (size
);
310 //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret);