]>
git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
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 int (*ioctl_func
) (int, int, void *) = NULL
;
25 static int windrvrfd
= 0;
26 static struct usb_bus
*busses
= NULL
;
28 void hexdump(unsigned char *buf
, int len
);
29 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
);
31 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
32 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
33 struct version_struct
*version
;
36 if (wdheader
->magic
!= MAGIC
) {
37 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
38 return (*ioctl_func
) (fd
, request
, wdioctl
);
43 version
= (struct version_struct
*)(wdheader
->data
);
44 strcpy(version
->version
, "WinDriver no more");
45 version
->versionul
= 999;
46 fprintf(stderr
,"faking VERSION\n");
50 fprintf(stderr
,"faking LICENSE\n");
55 //struct card_register* cr = (struct card_register*)(wdheader->data);
56 /* Todo: LPT-Port already in use */
58 fprintf(stderr
,"faking CARD_REGISTER\n");
62 fprintf(stderr
,"in USB_TRANSFER");
64 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
66 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
);
67 fprintf(stderr
,"setup packet: ");
68 hexdump(ut
->SetupPacket
, 8);
70 if (!ut
->fRead
&& ut
->dwBufferSize
)
72 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
76 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
78 fprintf(stderr
,"Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
79 if (ut
->fRead
&& ut
->dwBytesTransferred
)
81 fprintf(stderr
,"Read: ");
82 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
89 fprintf(stderr
,"faking INT_ENABLE");
91 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
93 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
96 //ret = (*ioctl_func) (fd, request, wdioctl);
102 fprintf(stderr
,"INT_DISABLE\n");
104 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
106 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
109 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
110 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
114 case USB_SET_INTERFACE
:
115 fprintf(stderr
,"USB_SET_INTERFACE\n");
117 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
119 fprintf(stderr
,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi
->dwUniqueID
, usi
->dwInterfaceNum
, usi
->dwAlternateSetting
, usi
->dwOptions
);
120 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
124 case USB_GET_DEVICE_DATA
:
125 fprintf(stderr
,"USB_GET_DEVICE_DATA\n");
127 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
130 fprintf(stderr
, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd
->dwUniqueID
, ugdd
->dwBytes
, ugdd
->dwOptions
);
131 pSize
= ugdd
->dwBytes
;
132 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
134 hexdump(ugdd
->pBuf
, pSize
);
135 fprintf(stderr
, "\n");
141 fprintf(stderr
,"EVENT_REGISTER\n");
143 struct event
*e
= (struct event
*)(wdheader
->data
);
146 fprintf(stderr
,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e
->handle
, e
->dwAction
, e
->dwStatus
, e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
, e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
, e
->u
.Usb
.deviceId
.dwProductId
, e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
, e
->dwNumMatchTables
);
147 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
148 fprintf(stderr
,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e
->matchTables
[i
].VendorId
, e
->matchTables
[i
].ProductId
, e
->matchTables
[i
].bDeviceClass
, e
->matchTables
[i
].bDeviceSubClass
, e
->matchTables
[i
].bInterfaceClass
, e
->matchTables
[i
].bInterfaceSubClass
, e
->matchTables
[i
].bInterfaceProtocol
);
150 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
152 fprintf(stderr
,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e
->handle
, e
->dwAction
, e
->dwStatus
, e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
, e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
, e
->u
.Usb
.deviceId
.dwProductId
, e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
, e
->dwNumMatchTables
);
153 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
154 fprintf(stderr
,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e
->matchTables
[i
].VendorId
, e
->matchTables
[i
].ProductId
, e
->matchTables
[i
].bDeviceClass
, e
->matchTables
[i
].bDeviceSubClass
, e
->matchTables
[i
].bInterfaceClass
, e
->matchTables
[i
].bInterfaceSubClass
, e
->matchTables
[i
].bInterfaceProtocol
);
159 fprintf(stderr
,"TRANSFER\n");
160 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
163 case EVENT_UNREGISTER
:
164 fprintf(stderr
,"EVENT_UNREGISTER\n");
165 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
169 fprintf(stderr
,"INT_WAIT\n");
171 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
173 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
175 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
176 fprintf(stderr
,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
, it
->dwLost
, it
->fStopped
);
180 case CARD_UNREGISTER
:
181 fprintf(stderr
,"CARD_UNREGISTER\n");
182 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
186 fprintf(stderr
,"EVENT_PULL\n");
188 struct event
*e
= (struct event
*)(wdheader
->data
);
191 fprintf(stderr
,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e
->handle
, e
->dwAction
, e
->dwStatus
, e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
, e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
, e
->u
.Usb
.deviceId
.dwProductId
, e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
, e
->dwNumMatchTables
);
192 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
193 fprintf(stderr
,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e
->matchTables
[i
].VendorId
, e
->matchTables
[i
].ProductId
, e
->matchTables
[i
].bDeviceClass
, e
->matchTables
[i
].bDeviceSubClass
, e
->matchTables
[i
].bInterfaceClass
, e
->matchTables
[i
].bInterfaceSubClass
, e
->matchTables
[i
].bInterfaceProtocol
);
195 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
197 fprintf(stderr
,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e
->handle
, e
->dwAction
, e
->dwStatus
, e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
, e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
, e
->u
.Usb
.deviceId
.dwProductId
, e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
, e
->dwNumMatchTables
);
198 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
199 fprintf(stderr
,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e
->matchTables
[i
].VendorId
, e
->matchTables
[i
].ProductId
, e
->matchTables
[i
].bDeviceClass
, e
->matchTables
[i
].bDeviceSubClass
, e
->matchTables
[i
].bInterfaceClass
, e
->matchTables
[i
].bInterfaceSubClass
, e
->matchTables
[i
].bInterfaceProtocol
);
204 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
211 typedef int (*open_funcptr_t
) (const char *, int, mode_t
);
213 int open (const char *pathname
, int flags
, ...)
215 static open_funcptr_t func
= NULL
;
221 func
= (open_funcptr_t
) dlsym (REAL_LIBC
, "open");
223 if (flags
& O_CREAT
) {
224 va_start(args
, flags
);
225 mode
= va_arg(args
, mode_t
);
229 fd
= (*func
) (pathname
, flags
, mode
);
231 if (!strcmp (pathname
, "/dev/windrvr6")) {
232 fprintf(stderr
,"opening windrvr6\n");
239 busses
= usb_get_busses();
246 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
) {
249 for(i
=0; i
<len
; i
++) {
250 if (buf1
[i
] != buf2
[i
]) {
251 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
]:'.'));
256 void hexdump(unsigned char *buf
, int len
) {
259 for(i
=0; i
<len
; i
++) {
260 fprintf(stderr
,"%02x ", buf
[i
]);
262 fprintf(stderr
,"\n");
266 int ioctl(int fd
, int request
, ...)
273 ioctl_func
= (int (*) (int, int, void *)) dlsym (REAL_LIBC
, "ioctl");
275 va_start (args
, request
);
276 argp
= va_arg (args
, void *);
280 ret
= do_wdioctl(fd
, request
, argp
);
282 ret
= (*ioctl_func
) (fd
, request
, argp
);
288 void *mmap(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t offset
)
290 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
294 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap");
296 ret
= (*func
) (start
, length
, prot
, flags
, fd
, offset
);
297 fprintf(stderr
,"MMAP: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, offset
, (unsigned int)ret
);
304 void *mmap64(void *start
, size_t length
, int prot
, int flags
, int fd
, off64_t offset
)
306 static void* (*func
) (void *, size_t, int, int, int, off64_t
) = NULL
;
310 func
= (void* (*) (void *, size_t, int, int, int, off64_t
)) dlsym (REAL_LIBC
, "mmap64");
312 ret
= (*func
) (start
, length
, prot
, flags
, fd
, offset
);
313 fprintf(stderr
,"MMAP64: %x, %d, %d, %d, %d, %lld -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, offset
, (unsigned int)ret
);
320 void *mmap2(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t pgoffset
)
322 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
326 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap2");
328 ret
= (*func
) (start
, length
, prot
, flags
, fd
, pgoffset
);
329 fprintf(stderr
,"MMAP2: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, pgoffset
, (unsigned int)ret
);
336 void *malloc(size_t size
)
338 static void* (*func
) (size_t) = NULL
;
342 func
= (void* (*) (size_t)) dlsym(REAL_LIBC
, "malloc");
344 ret
= (*func
) (size
);
346 //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret);