]>
git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
89269b6e80c084e6779cbabfc7990de374c8914b
1 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
6 #define REAL_LIBC RTLD_NEXT
8 #define REAL_LIBC ((void *) -1L)
17 #include <sys/types.h>
23 static int (*ioctl_func
) (int, int, void *) = NULL
;
24 static int windrvrfd
= 0;
26 void hexdump(unsigned char *buf
, int len
);
27 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
);
29 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
30 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
31 struct version_struct
*version
;
34 if (wdheader
->magic
!= MAGIC
) {
35 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
36 return (*ioctl_func
) (fd
, request
, wdioctl
);
41 version
= (struct version_struct
*)(wdheader
->data
);
42 strcpy(version
->version
, "WinDriver no more");
43 version
->versionul
= 999;
44 fprintf(stderr
,"faking VERSION\n");
48 fprintf(stderr
,"faking LICENSE\n");
53 //struct card_register* cr = (struct card_register*)(wdheader->data);
54 /* Todo: LPT-Port already in use */
56 fprintf(stderr
,"faking CARD_REGISTER\n");
60 fprintf(stderr
,"in USB_TRANSFER");
62 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
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
);
65 fprintf(stderr
,"setup packet: ");
66 hexdump(ut
->SetupPacket
, 8);
68 if (!ut
->fRead
&& ut
->dwBufferSize
)
70 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
74 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
76 fprintf(stderr
,"Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
77 if (ut
->fRead
&& ut
->dwBytesTransferred
)
79 fprintf(stderr
,"Read: ");
80 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
87 fprintf(stderr
,"faking INT_ENABLE");
89 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
91 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
);
94 //ret = (*ioctl_func) (fd, request, wdioctl);
100 fprintf(stderr
,"INT_DISABLE\n");
102 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
104 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
);
107 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
108 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
);
112 case USB_SET_INTERFACE
:
113 fprintf(stderr
,"USB_SET_INTERFACE\n");
115 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
117 fprintf(stderr
,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi
->dwUniqueID
, usi
->dwInterfaceNum
, usi
->dwAlternateSetting
, usi
->dwOptions
);
118 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
122 case USB_GET_DEVICE_DATA
:
123 fprintf(stderr
,"USB_GET_DEVICE_DATA\n");
125 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
128 fprintf(stderr
, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd
->dwUniqueID
, ugdd
->dwBytes
, ugdd
->dwOptions
);
129 pSize
= ugdd
->dwBytes
;
130 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
132 hexdump(ugdd
->pBuf
, pSize
);
133 fprintf(stderr
, "\n");
139 fprintf(stderr
,"EVENT_REGISTER\n");
141 struct event
*e
= (struct event
*)(wdheader
->data
);
144 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
);
145 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
146 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
);
148 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
150 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
);
151 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
152 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
);
157 fprintf(stderr
,"TRANSFER\n");
158 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
161 case EVENT_UNREGISTER
:
162 fprintf(stderr
,"EVENT_UNREGISTER\n");
163 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
167 fprintf(stderr
,"INT_WAIT\n");
169 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
171 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
);
173 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
174 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
);
178 case CARD_UNREGISTER
:
179 fprintf(stderr
,"CARD_UNREGISTER\n");
180 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
184 fprintf(stderr
,"EVENT_PULL\n");
185 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
189 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
196 typedef int (*open_funcptr_t
) (const char *, int, mode_t
);
198 int open (const char *pathname
, int flags
, ...)
200 static open_funcptr_t func
= NULL
;
206 func
= (open_funcptr_t
) dlsym (REAL_LIBC
, "open");
208 if (flags
& O_CREAT
) {
209 va_start(args
, flags
);
210 mode
= va_arg(args
, mode_t
);
214 fd
= (*func
) (pathname
, flags
, mode
);
216 if (!strcmp (pathname
, "/dev/windrvr6")) {
217 fprintf(stderr
,"opening windrvr6\n");
224 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
) {
227 for(i
=0; i
<len
; i
++) {
228 if (buf1
[i
] != buf2
[i
]) {
229 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
]:'.'));
234 void hexdump(unsigned char *buf
, int len
) {
237 for(i
=0; i
<len
; i
++) {
238 fprintf(stderr
,"%02x ", buf
[i
]);
240 fprintf(stderr
,"\n");
244 int ioctl(int fd
, int request
, ...)
251 ioctl_func
= (int (*) (int, int, void *)) dlsym (REAL_LIBC
, "ioctl");
253 va_start (args
, request
);
254 argp
= va_arg (args
, void *);
258 ret
= do_wdioctl(fd
, request
, argp
);
260 ret
= (*ioctl_func
) (fd
, request
, argp
);
266 void *mmap(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t offset
)
268 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
272 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap");
274 ret
= (*func
) (start
, length
, prot
, flags
, fd
, offset
);
275 fprintf(stderr
,"MMAP: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, offset
, (unsigned int)ret
);
282 void *mmap64(void *start
, size_t length
, int prot
, int flags
, int fd
, off64_t offset
)
284 static void* (*func
) (void *, size_t, int, int, int, off64_t
) = NULL
;
288 func
= (void* (*) (void *, size_t, int, int, int, off64_t
)) dlsym (REAL_LIBC
, "mmap64");
290 ret
= (*func
) (start
, length
, prot
, flags
, fd
, offset
);
291 fprintf(stderr
,"MMAP64: %x, %d, %d, %d, %d, %lld -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, offset
, (unsigned int)ret
);
298 void *mmap2(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t pgoffset
)
300 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
304 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap2");
306 ret
= (*func
) (start
, length
, prot
, flags
, fd
, pgoffset
);
307 fprintf(stderr
,"MMAP2: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, pgoffset
, (unsigned int)ret
);
314 void *malloc(size_t size
)
316 static void* (*func
) (size_t) = NULL
;
320 func
= (void* (*) (size_t)) dlsym(REAL_LIBC
, "malloc");
322 ret
= (*func
) (size
);
324 //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret);