4fc6522a562f089642ce1af059a9a1688b3fdbd0
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
;
27 static struct usb_device
*usb_cable
;
28 static unsigned long card_type
;
32 void hexdump(unsigned char *buf
, int len
);
33 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
);
35 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
36 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
37 struct version_struct
*version
;
40 if (wdheader
->magic
!= MAGIC
) {
41 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
42 return (*ioctl_func
) (fd
, request
, wdioctl
);
47 version
= (struct version_struct
*)(wdheader
->data
);
48 strcpy(version
->version
, "WinDriver no more");
49 version
->versionul
= 999;
50 fprintf(stderr
,"faking VERSION\n");
54 fprintf(stderr
,"faking LICENSE\n");
59 //struct card_register* cr = (struct card_register*)(wdheader->data);
60 /* Todo: LPT-Port already in use */
62 fprintf(stderr
,"faking CARD_REGISTER\n");
66 fprintf(stderr
,"in USB_TRANSFER");
68 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
70 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
);
71 fprintf(stderr
,"setup packet: ");
72 hexdump(ut
->SetupPacket
, 8);
74 if (!ut
->fRead
&& ut
->dwBufferSize
)
76 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
81 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
84 fprintf(stderr
,"Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
85 if (ut
->fRead
&& ut
->dwBytesTransferred
)
87 fprintf(stderr
,"Read: ");
88 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
95 fprintf(stderr
,"faking INT_ENABLE");
97 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
99 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
);
102 //ret = (*ioctl_func) (fd, request, wdioctl);
108 fprintf(stderr
,"INT_DISABLE\n");
110 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
112 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
);
116 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
118 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
);
122 case USB_SET_INTERFACE
:
123 fprintf(stderr
,"USB_SET_INTERFACE\n");
125 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
127 fprintf(stderr
,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi
->dwUniqueID
, usi
->dwInterfaceNum
, usi
->dwAlternateSetting
, usi
->dwOptions
);
129 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
134 case USB_GET_DEVICE_DATA
:
135 fprintf(stderr
,"USB_GET_DEVICE_DATA\n");
137 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
140 fprintf(stderr
, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd
->dwUniqueID
, ugdd
->dwBytes
, ugdd
->dwOptions
);
141 pSize
= ugdd
->dwBytes
;
143 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
146 hexdump(ugdd
->pBuf
, pSize
);
147 fprintf(stderr
, "\n");
153 fprintf(stderr
,"EVENT_REGISTER\n");
155 struct event
*e
= (struct event
*)(wdheader
->data
);
159 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
);
160 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
161 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
);
163 for (bus
= busses
; bus
; bus
= bus
->next
) {
164 struct usb_device
*dev
;
166 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
167 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
169 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
170 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
171 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
172 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
173 struct usb_interface
*interface
= dev
->config
->interface
;
176 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
177 fprintf(stderr
, "intclass: %x, intsubclass: %x, intproto: %x\n", interface
->altsetting
[i
].bInterfaceClass
, interface
->altsetting
[i
].bInterfaceSubClass
, interface
->altsetting
[i
].bInterfaceProtocol
);
178 if ((interface
->altsetting
[i
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
179 (interface
->altsetting
[i
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
180 /* TODO: check interfaceClass! */
181 fprintf(stderr
,"!!!FOUND DEVICE WITH LIBUSB!!!\n");
183 card_type
= e
->dwCardType
;
192 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
195 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
);
196 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
197 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
);
202 fprintf(stderr
,"TRANSFER\n");
204 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
208 case EVENT_UNREGISTER
:
209 fprintf(stderr
,"EVENT_UNREGISTER\n");
211 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
216 fprintf(stderr
,"INT_WAIT\n");
218 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
220 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
);
223 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
229 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
);
233 case CARD_UNREGISTER
:
234 fprintf(stderr
,"CARD_UNREGISTER\n");
236 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
241 fprintf(stderr
,"EVENT_PULL\n");
243 struct event
*e
= (struct event
*)(wdheader
->data
);
246 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
);
247 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
248 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
);
251 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
254 //handle: 1, action: 0, status: 0, eventid: 0, cardtype: 0, kplug: 0, options: 0, dev: 0:0, unique: 0, ver: 1, nummatch: 1
255 //match: dev: 0:0, class: 0, subclass: 0, intclass: 0, intsubclass: 0, intproto: 0
256 //handle: 1, action: 1, status: 0, eventid: 109, cardtype: 4294967294, kplug: 0, options: 0, dev: 0:0, unique: 90, ver: 1, nummatch: 1
257 //match: dev: 3fd:8, class: 0, subclass: 0, intclass: ff, intsubclass: 0, intproto: 0
259 struct usb_interface
*interface
= usb_cable
->config
->interface
;
261 e
->dwCardType
= card_type
;
264 e
->u
.Usb
.dwUniqueID
= 4711;
265 e
->matchTables
[0].VendorId
= usb_cable
->descriptor
.idVendor
;
266 e
->matchTables
[0].ProductId
= usb_cable
->descriptor
.idProduct
;
267 e
->matchTables
[0].bDeviceClass
= usb_cable
->descriptor
.bDeviceClass
;
268 e
->matchTables
[0].bDeviceSubClass
= usb_cable
->descriptor
.bDeviceSubClass
;
269 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
270 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
271 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
275 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
);
276 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
277 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
);
282 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
284 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
293 typedef int (*open_funcptr_t
) (const char *, int, mode_t
);
295 int open (const char *pathname
, int flags
, ...)
297 static open_funcptr_t func
= NULL
;
303 func
= (open_funcptr_t
) dlsym (REAL_LIBC
, "open");
305 if (flags
& O_CREAT
) {
306 va_start(args
, flags
);
307 mode
= va_arg(args
, mode_t
);
311 fd
= (*func
) (pathname
, flags
, mode
);
313 if (!strcmp (pathname
, "/dev/windrvr6")) {
314 fprintf(stderr
,"opening windrvr6\n");
321 busses
= usb_get_busses();
328 void diff(unsigned char *buf1
, unsigned char *buf2
, int len
) {
331 for(i
=0; i
<len
; i
++) {
332 if (buf1
[i
] != buf2
[i
]) {
333 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
]:'.'));
338 void hexdump(unsigned char *buf
, int len
) {
341 for(i
=0; i
<len
; i
++) {
342 fprintf(stderr
,"%02x ", buf
[i
]);
344 fprintf(stderr
,"\n");
348 int ioctl(int fd
, int request
, ...)
355 ioctl_func
= (int (*) (int, int, void *)) dlsym (REAL_LIBC
, "ioctl");
357 va_start (args
, request
);
358 argp
= va_arg (args
, void *);
362 ret
= do_wdioctl(fd
, request
, argp
);
364 ret
= (*ioctl_func
) (fd
, request
, argp
);
370 void *mmap(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t offset
)
372 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
376 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap");
378 ret
= (*func
) (start
, length
, prot
, flags
, fd
, offset
);
379 fprintf(stderr
,"MMAP: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, offset
, (unsigned int)ret
);
386 void *mmap64(void *start
, size_t length
, int prot
, int flags
, int fd
, off64_t offset
)
388 static void* (*func
) (void *, size_t, int, int, int, off64_t
) = NULL
;
392 func
= (void* (*) (void *, size_t, int, int, int, off64_t
)) dlsym (REAL_LIBC
, "mmap64");
394 ret
= (*func
) (start
, length
, prot
, flags
, fd
, offset
);
395 fprintf(stderr
,"MMAP64: %x, %d, %d, %d, %d, %lld -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, offset
, (unsigned int)ret
);
402 void *mmap2(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t pgoffset
)
404 static void* (*func
) (void *, size_t, int, int, int, off_t
) = NULL
;
408 func
= (void* (*) (void *, size_t, int, int, int, off_t
)) dlsym (REAL_LIBC
, "mmap2");
410 ret
= (*func
) (start
, length
, prot
, flags
, fd
, pgoffset
);
411 fprintf(stderr
,"MMAP2: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start
, length
, prot
, flags
, fd
, pgoffset
, (unsigned int)ret
);
418 void *malloc(size_t size
)
420 static void* (*func
) (size_t) = NULL
;
424 func
= (void* (*) (size_t)) dlsym(REAL_LIBC
, "malloc");
426 ret
= (*func
) (size
);
428 //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret);