]>
Commit | Line | Data |
---|---|---|
1 | /* libusb/ppdev connector for XILINX impact | |
2 | * | |
3 | * Copyright (c) 2007 Michael Gernoth <michael@gernoth.net> | |
4 | * | |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to | |
7 | * deal in the Software without restriction, including without limitation the | |
8 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
9 | * sell copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | */ | |
23 | ||
24 | #define _GNU_SOURCE 1 | |
25 | ||
26 | #include <dlfcn.h> | |
27 | #include <stdarg.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <unistd.h> | |
31 | #include <fcntl.h> | |
32 | #include <sys/types.h> | |
33 | #include <sys/stat.h> | |
34 | #include <sys/time.h> | |
35 | #include <stdio.h> | |
36 | #include <usb.h> | |
37 | #include <signal.h> | |
38 | #include <pthread.h> | |
39 | #include <errno.h> | |
40 | #include <inttypes.h> | |
41 | #include <sys/ioctl.h> | |
42 | #include <sys/utsname.h> | |
43 | #include <bits/wordsize.h> | |
44 | #include "usb-driver.h" | |
45 | #include "config.h" | |
46 | #include "xpcu.h" | |
47 | ||
48 | static int (*ioctl_func) (int, int, void *) = NULL; | |
49 | static int windrvrfd = -1; | |
50 | static unsigned long ppbase = 0; | |
51 | static unsigned long ecpbase = 0; | |
52 | static struct parport_config *pport = NULL; | |
53 | static struct xpcu_s *xpcu = NULL; | |
54 | static FILE *modulesfp = NULL; | |
55 | static FILE *baseaddrfp = NULL; | |
56 | static int baseaddrnum = 0; | |
57 | static int modules_read = 0; | |
58 | static unsigned long card_type; | |
59 | static int ints_enabled = 0; | |
60 | static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER; | |
61 | ||
62 | #define NO_WINDRVR 1 | |
63 | ||
64 | void hexdump(unsigned char *buf, int len) { | |
65 | int i; | |
66 | ||
67 | for(i=0; i<len; i++) { | |
68 | fprintf(stderr,"%02x ", buf[i]); | |
69 | if ((i % 16) == 15) | |
70 | fprintf(stderr,"\n"); | |
71 | } | |
72 | fprintf(stderr,"\n"); | |
73 | } | |
74 | ||
75 | ||
76 | static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) { | |
77 | struct header_struct* wdheader = (struct header_struct*)wdioctl; | |
78 | struct version_struct *version; | |
79 | int ret = 0; | |
80 | ||
81 | if (wdheader->magic != MAGIC) { | |
82 | fprintf(stderr,"!!!ERROR: magic header does not match!!!\n"); | |
83 | return (*ioctl_func) (fd, request, wdioctl); | |
84 | } | |
85 | ||
86 | switch(request & ~(0xc0000000)) { | |
87 | case VERSION: | |
88 | version = (struct version_struct*)(wdheader->data); | |
89 | strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION); | |
90 | version->versionul = 802; | |
91 | DPRINTF("VERSION\n"); | |
92 | break; | |
93 | ||
94 | case LICENSE: | |
95 | DPRINTF("LICENSE\n"); | |
96 | break; | |
97 | ||
98 | case CARD_REGISTER_OLD: | |
99 | case CARD_REGISTER: | |
100 | DPRINTF("CARD_REGISTER\n"); | |
101 | { | |
102 | struct card_register* cr = (struct card_register*)(wdheader->data); | |
103 | ||
104 | DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
105 | cr->Card.dwItems, | |
106 | (unsigned long)cr->Card.Item[0].I.IO.dwAddr, | |
107 | cr->Card.Item[0].I.IO.dwBytes, | |
108 | cr->Card.Item[0].I.IO.dwBar); | |
109 | ||
110 | DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
111 | cr->Card.dwItems, | |
112 | (unsigned long)cr->Card.Item[1].I.IO.dwAddr, | |
113 | cr->Card.Item[1].I.IO.dwBytes, | |
114 | cr->Card.Item[1].I.IO.dwBar); | |
115 | #ifndef NO_WINDRVR | |
116 | ret = (*ioctl_func) (fd, request, wdioctl); | |
117 | #else | |
118 | ||
119 | pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10); | |
120 | if (!pport) | |
121 | break; | |
122 | ||
123 | ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10); | |
124 | ||
125 | ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr; | |
126 | ||
127 | if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr) | |
128 | ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr; | |
129 | ||
130 | if (ret >= 0) { | |
131 | cr->hCard = ret; | |
132 | } else { | |
133 | cr->hCard = 0; | |
134 | } | |
135 | #endif | |
136 | DPRINTF("hCard: %lu\n", cr->hCard); | |
137 | } | |
138 | break; | |
139 | ||
140 | case USB_TRANSFER: | |
141 | DPRINTF("in USB_TRANSFER"); | |
142 | { | |
143 | struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data); | |
144 | ||
145 | #ifdef DEBUG | |
146 | DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n", | |
147 | ut->dwUniqueID, ut->dwPipeNum, ut->fRead, | |
148 | ut->dwOptions, ut->dwBufferSize, ut->dwTimeout); | |
149 | if (ut->dwPipeNum == 0) { | |
150 | DPRINTF("setup packet: "); | |
151 | hexdump(ut->SetupPacket, 8); | |
152 | } | |
153 | ||
154 | if (!ut->fRead && ut->dwBufferSize) | |
155 | { | |
156 | hexdump(ut->pBuffer, ut->dwBufferSize); | |
157 | } | |
158 | #endif | |
159 | ||
160 | #ifndef NO_WINDRVR | |
161 | ret = (*ioctl_func) (fd, request, wdioctl); | |
162 | #else | |
163 | xpcu_transfer(xpcu, ut); | |
164 | #endif | |
165 | ||
166 | #ifdef DEBUG | |
167 | DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write")); | |
168 | if (ut->fRead && ut->dwBytesTransferred) | |
169 | { | |
170 | DPRINTF("Read: "); | |
171 | hexdump(ut->pBuffer, ut->dwBytesTransferred); | |
172 | } | |
173 | #endif | |
174 | } | |
175 | break; | |
176 | ||
177 | case INT_ENABLE_OLD: | |
178 | case INT_ENABLE: | |
179 | DPRINTF("INT_ENABLE\n"); | |
180 | { | |
181 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
182 | ||
183 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
184 | it->hInterrupt, it->dwOptions, | |
185 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
186 | it->dwLost, it->fStopped); | |
187 | ||
188 | it->fEnableOk = 1; | |
189 | it->fStopped = 0; | |
190 | ints_enabled = 1; | |
191 | pthread_mutex_trylock(&int_wait); | |
192 | } | |
193 | ||
194 | break; | |
195 | ||
196 | case INT_DISABLE: | |
197 | DPRINTF("INT_DISABLE\n"); | |
198 | { | |
199 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
200 | ||
201 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
202 | it->hInterrupt, it->dwOptions, | |
203 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
204 | it->dwLost, it->fStopped); | |
205 | #ifndef NO_WINDRVR | |
206 | ret = (*ioctl_func) (fd, request, wdioctl); | |
207 | #else | |
208 | it->dwCounter = 0; | |
209 | it->fStopped = 1; | |
210 | ints_enabled = 0; | |
211 | if (pthread_mutex_trylock(&int_wait) == EBUSY) | |
212 | pthread_mutex_unlock(&int_wait); | |
213 | #endif | |
214 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
215 | it->hInterrupt, it->dwOptions, | |
216 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
217 | it->dwLost, it->fStopped); | |
218 | } | |
219 | break; | |
220 | ||
221 | case USB_SET_INTERFACE: | |
222 | DPRINTF("USB_SET_INTERFACE\n"); | |
223 | { | |
224 | struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data); | |
225 | ||
226 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", | |
227 | usi->dwUniqueID, usi->dwInterfaceNum, | |
228 | usi->dwAlternateSetting, usi->dwOptions); | |
229 | #ifndef NO_WINDRVR | |
230 | ret = (*ioctl_func) (fd, request, wdioctl); | |
231 | #else | |
232 | xpcu_set_interface(xpcu, usi); | |
233 | #endif | |
234 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", | |
235 | usi->dwUniqueID, usi->dwInterfaceNum, | |
236 | usi->dwAlternateSetting, usi->dwOptions); | |
237 | } | |
238 | break; | |
239 | ||
240 | case USB_GET_DEVICE_DATA_OLD: | |
241 | case USB_GET_DEVICE_DATA: | |
242 | DPRINTF("USB_GET_DEVICE_DATA\n"); | |
243 | { | |
244 | struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data); | |
245 | int pSize; | |
246 | ||
247 | DPRINTF("unique: %lu, bytes: %lu, options: %lx\n", | |
248 | ugdd->dwUniqueID, ugdd->dwBytes, | |
249 | ugdd->dwOptions); | |
250 | ||
251 | pSize = ugdd->dwBytes; | |
252 | if (!ugdd->dwBytes) { | |
253 | if (xpcu->dev) { | |
254 | ugdd->dwBytes = xpcu_deviceinfo(xpcu, NULL); | |
255 | } | |
256 | } else { | |
257 | xpcu_deviceinfo(xpcu, (unsigned char*)ugdd->pBuf); | |
258 | } | |
259 | } | |
260 | break; | |
261 | ||
262 | case EVENT_REGISTER_OLD: | |
263 | case EVENT_REGISTER: | |
264 | DPRINTF("EVENT_REGISTER\n"); | |
265 | { | |
266 | struct event *e = (struct event*)(wdheader->data); | |
267 | struct usb_bus *bus; | |
268 | char* devpos; | |
269 | int busnum = -1, devnum = -1; | |
270 | int i; | |
271 | ||
272 | DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", | |
273 | e->handle, e->dwAction, | |
274 | e->dwStatus, e->dwEventId, e->dwCardType, | |
275 | e->hKernelPlugIn, e->dwOptions, | |
276 | e->u.Usb.deviceId.dwVendorId, | |
277 | e->u.Usb.deviceId.dwProductId, | |
278 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
279 | e->dwNumMatchTables); | |
280 | ||
281 | devpos = getenv("XILINX_USB_DEV"); | |
282 | if (devpos != NULL) { | |
283 | int j; | |
284 | char *devstr = NULL, *remainder; | |
285 | ||
286 | DPRINTF("XILINX_USB_DEV=%s\n", devpos); | |
287 | ||
288 | for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) { | |
289 | if (devpos[j] == ':') { | |
290 | devpos[j] = 0; | |
291 | devstr = &(devpos[j+1]); | |
292 | } | |
293 | } | |
294 | ||
295 | if (devstr && strlen(devstr) > 0) { | |
296 | busnum = strtol(devpos, &remainder, 10); | |
297 | if (devpos == remainder) { | |
298 | busnum = -1; | |
299 | } else { | |
300 | devnum = strtol(devstr, &remainder, 10); | |
301 | if (devstr == remainder) { | |
302 | busnum = -1; | |
303 | devnum = -1; | |
304 | } else { | |
305 | fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n", | |
306 | busnum, devnum); | |
307 | } | |
308 | } | |
309 | } | |
310 | } | |
311 | ||
312 | for (i = 0; i < e->dwNumMatchTables; i++) { | |
313 | ||
314 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
315 | e->matchTables[i].VendorId, | |
316 | e->matchTables[i].ProductId, | |
317 | e->matchTables[i].bDeviceClass, | |
318 | e->matchTables[i].bDeviceSubClass, | |
319 | e->matchTables[i].bInterfaceClass, | |
320 | e->matchTables[i].bInterfaceSubClass, | |
321 | e->matchTables[i].bInterfaceProtocol); | |
322 | ||
323 | for (bus = xpcu->busses; bus; bus = bus->next) { | |
324 | struct usb_device *dev; | |
325 | ||
326 | if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum)) | |
327 | continue; | |
328 | ||
329 | for (dev = bus->devices; dev; dev = dev->next) { | |
330 | struct usb_device_descriptor *desc = &(dev->descriptor); | |
331 | ||
332 | if((desc->idVendor == e->matchTables[i].VendorId) && | |
333 | (desc->idProduct == e->matchTables[i].ProductId) && | |
334 | (desc->bDeviceClass == e->matchTables[i].bDeviceClass) && | |
335 | (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) && | |
336 | ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) { | |
337 | int ac; | |
338 | for (ac = 0; ac < desc->bNumConfigurations; ac++) { | |
339 | struct usb_interface *interface = dev->config[ac].interface; | |
340 | int ai; | |
341 | ||
342 | for (ai = 0; ai < interface->num_altsetting; ai++) { | |
343 | ||
344 | DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n", | |
345 | interface->altsetting[i].bInterfaceClass, | |
346 | interface->altsetting[i].bInterfaceSubClass, | |
347 | interface->altsetting[i].bInterfaceProtocol); | |
348 | ||
349 | if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) && | |
350 | (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){ | |
351 | /* TODO: check interfaceClass! */ | |
352 | DPRINTF("found device with libusb\n"); | |
353 | xpcu->dev = dev; | |
354 | card_type = e->dwCardType; | |
355 | } | |
356 | } | |
357 | } | |
358 | } | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | #ifndef NO_WINDRVR | |
364 | ret = (*ioctl_func) (fd, request, wdioctl); | |
365 | #else | |
366 | e->handle++; | |
367 | #endif | |
368 | ||
369 | #ifdef DEBUG | |
370 | DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", | |
371 | e->handle, e->dwAction, | |
372 | e->dwStatus, e->dwEventId, e->dwCardType, | |
373 | e->hKernelPlugIn, e->dwOptions, | |
374 | e->u.Usb.deviceId.dwVendorId, | |
375 | e->u.Usb.deviceId.dwProductId, | |
376 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
377 | e->dwNumMatchTables); | |
378 | ||
379 | for (i = 0; i < e->dwNumMatchTables; i++) | |
380 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
381 | e->matchTables[i].VendorId, | |
382 | e->matchTables[i].ProductId, | |
383 | e->matchTables[i].bDeviceClass, | |
384 | e->matchTables[i].bDeviceSubClass, | |
385 | e->matchTables[i].bInterfaceClass, | |
386 | e->matchTables[i].bInterfaceSubClass, | |
387 | e->matchTables[i].bInterfaceProtocol); | |
388 | #endif | |
389 | } | |
390 | break; | |
391 | ||
392 | case TRANSFER_OLD: | |
393 | case TRANSFER: | |
394 | DPRINTF("TRANSFER\n"); | |
395 | { | |
396 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
397 | ||
398 | #ifndef NO_WINDRVR | |
399 | ret = (*ioctl_func) (fd, request, wdioctl); | |
400 | #else | |
401 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1); | |
402 | #endif | |
403 | } | |
404 | break; | |
405 | ||
406 | case MULTI_TRANSFER_OLD: | |
407 | case MULTI_TRANSFER: | |
408 | DPRINTF("MULTI_TRANSFER\n"); | |
409 | { | |
410 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
411 | unsigned long num = wdheader->size/sizeof(WD_TRANSFER); | |
412 | #ifndef NO_WINDRVR | |
413 | ret = (*ioctl_func) (fd, request, wdioctl); | |
414 | #else | |
415 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num); | |
416 | #endif | |
417 | } | |
418 | break; | |
419 | ||
420 | case EVENT_UNREGISTER: | |
421 | DPRINTF("EVENT_UNREGISTER\n"); | |
422 | #ifndef NO_WINDRVR | |
423 | ret = (*ioctl_func) (fd, request, wdioctl); | |
424 | #endif | |
425 | break; | |
426 | ||
427 | case INT_WAIT: | |
428 | DPRINTF("INT_WAIT\n"); | |
429 | { | |
430 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
431 | ||
432 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
433 | it->hInterrupt, it->dwOptions, | |
434 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
435 | it->dwLost, it->fStopped); | |
436 | ||
437 | #ifndef NO_WINDRVR | |
438 | ret = (*ioctl_func) (fd, request, wdioctl); | |
439 | #else | |
440 | if (xpcu->dev) { | |
441 | if (it->dwCounter == 0) { | |
442 | it->dwCounter = 1; | |
443 | } else { | |
444 | pthread_mutex_lock(&int_wait); | |
445 | pthread_mutex_unlock(&int_wait); | |
446 | } | |
447 | } else { | |
448 | pthread_mutex_lock(&int_wait); | |
449 | pthread_mutex_unlock(&int_wait); | |
450 | } | |
451 | #endif | |
452 | ||
453 | DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
454 | it->hInterrupt, it->dwOptions, it->dwCmds, | |
455 | it->fEnableOk, it->dwCounter, it->dwLost, | |
456 | it->fStopped); | |
457 | } | |
458 | break; | |
459 | ||
460 | case CARD_UNREGISTER: | |
461 | DPRINTF("CARD_UNREGISTER\n"); | |
462 | { | |
463 | struct card_register* cr = (struct card_register*)(wdheader->data); | |
464 | ||
465 | DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
466 | (unsigned long)cr->Card.Item[0].I.IO.dwAddr, | |
467 | cr->Card.Item[0].I.IO.dwBytes, | |
468 | cr->Card.Item[0].I.IO.dwBar); | |
469 | ||
470 | DPRINTF("hCard: %lu\n", cr->hCard); | |
471 | ||
472 | #ifndef NO_WINDRVR | |
473 | ret = (*ioctl_func) (fd, request, wdioctl); | |
474 | #else | |
475 | if (pport) | |
476 | pport->close(cr->hCard); | |
477 | ||
478 | pport = NULL; | |
479 | #endif | |
480 | } | |
481 | break; | |
482 | ||
483 | case EVENT_PULL: | |
484 | DPRINTF("EVENT_PULL\n"); | |
485 | { | |
486 | struct event *e = (struct event*)(wdheader->data); | |
487 | #ifdef DEBUG | |
488 | int i; | |
489 | ||
490 | DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", | |
491 | e->handle, e->dwAction, e->dwStatus, | |
492 | e->dwEventId, e->dwCardType, e->hKernelPlugIn, | |
493 | e->dwOptions, e->u.Usb.deviceId.dwVendorId, | |
494 | e->u.Usb.deviceId.dwProductId, | |
495 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
496 | e->dwNumMatchTables); | |
497 | ||
498 | for (i = 0; i < e->dwNumMatchTables; i++) | |
499 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
500 | e->matchTables[i].VendorId, | |
501 | e->matchTables[i].ProductId, | |
502 | e->matchTables[i].bDeviceClass, | |
503 | e->matchTables[i].bDeviceSubClass, | |
504 | e->matchTables[i].bInterfaceClass, | |
505 | e->matchTables[i].bInterfaceSubClass, | |
506 | e->matchTables[i].bInterfaceProtocol); | |
507 | #endif | |
508 | ||
509 | #ifndef NO_WINDRVR | |
510 | ret = (*ioctl_func) (fd, request, wdioctl); | |
511 | #else | |
512 | if (xpcu->dev) { | |
513 | struct usb_interface *interface = xpcu->dev->config->interface; | |
514 | ||
515 | e->dwCardType = card_type; | |
516 | e->dwAction = 1; | |
517 | e->dwEventId = 109; | |
518 | e->u.Usb.dwUniqueID = 110; | |
519 | e->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor; | |
520 | e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct; | |
521 | e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass; | |
522 | e->matchTables[0].bDeviceSubClass = xpcu->dev->descriptor.bDeviceSubClass; | |
523 | e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass; | |
524 | e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass; | |
525 | e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol; | |
526 | } | |
527 | #endif | |
528 | ||
529 | #ifdef DEBUG | |
530 | DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", | |
531 | e->handle, e->dwAction, e->dwStatus, | |
532 | e->dwEventId, e->dwCardType, e->hKernelPlugIn, | |
533 | e->dwOptions, e->u.Usb.deviceId.dwVendorId, | |
534 | e->u.Usb.deviceId.dwProductId, | |
535 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
536 | e->dwNumMatchTables); | |
537 | ||
538 | for (i = 0; i < e->dwNumMatchTables; i++) | |
539 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
540 | e->matchTables[i].VendorId, | |
541 | e->matchTables[i].ProductId, | |
542 | e->matchTables[i].bDeviceClass, | |
543 | e->matchTables[i].bDeviceSubClass, | |
544 | e->matchTables[i].bInterfaceClass, | |
545 | e->matchTables[i].bInterfaceSubClass, | |
546 | e->matchTables[i].bInterfaceProtocol); | |
547 | #endif | |
548 | ||
549 | } | |
550 | break; | |
551 | ||
552 | default: | |
553 | fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request); | |
554 | #ifndef NO_WINDRVR | |
555 | ret = (*ioctl_func) (fd, request, wdioctl); | |
556 | #endif | |
557 | break; | |
558 | } | |
559 | ||
560 | return ret; | |
561 | } | |
562 | ||
563 | int ioctl(int fd, unsigned long int request, ...) { | |
564 | va_list args; | |
565 | void *argp; | |
566 | int ret; | |
567 | ||
568 | if (!ioctl_func) | |
569 | ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl"); | |
570 | ||
571 | va_start (args, request); | |
572 | argp = va_arg (args, void *); | |
573 | va_end (args); | |
574 | ||
575 | if (fd == windrvrfd) | |
576 | ret = do_wdioctl(fd, request, argp); | |
577 | else | |
578 | ret = (*ioctl_func) (fd, request, argp); | |
579 | ||
580 | return ret; | |
581 | } | |
582 | ||
583 | int open (const char *pathname, int flags, ...) { | |
584 | static int (*func) (const char *, int, mode_t) = NULL; | |
585 | mode_t mode = 0; | |
586 | va_list args; | |
587 | int fd; | |
588 | ||
589 | if (!func) | |
590 | func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open"); | |
591 | ||
592 | if (flags & O_CREAT) { | |
593 | va_start(args, flags); | |
594 | mode = va_arg(args, mode_t); | |
595 | va_end(args); | |
596 | } | |
597 | ||
598 | if (!strcmp (pathname, "/dev/windrvr6")) { | |
599 | DPRINTF("opening windrvr6\n"); | |
600 | #ifdef NO_WINDRVR | |
601 | windrvrfd = fd = (*func) ("/dev/null", flags, mode); | |
602 | #else | |
603 | windrvrfd = fd = (*func) (pathname, flags, mode); | |
604 | #endif | |
605 | if (!xpcu) | |
606 | xpcu = xpcu_open(); | |
607 | ||
608 | return fd; | |
609 | } | |
610 | ||
611 | return (*func) (pathname, flags, mode); | |
612 | } | |
613 | ||
614 | int close(int fd) { | |
615 | static int (*func) (int) = NULL; | |
616 | ||
617 | if (!func) | |
618 | func = (int (*) (int)) dlsym(RTLD_NEXT, "close"); | |
619 | ||
620 | if (fd == windrvrfd && windrvrfd >= 0) { | |
621 | DPRINTF("close windrvrfd\n"); | |
622 | ||
623 | if (xpcu) | |
624 | xpcu_close(xpcu); | |
625 | ||
626 | xpcu = NULL; | |
627 | windrvrfd = -1; | |
628 | } | |
629 | ||
630 | return (*func) (fd); | |
631 | } | |
632 | ||
633 | FILE *fopen(const char *path, const char *mode) { | |
634 | FILE *ret; | |
635 | static FILE* (*func) (const char*, const char*) = NULL; | |
636 | char buf[256]; | |
637 | int i; | |
638 | ||
639 | if (!func) | |
640 | func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen"); | |
641 | ||
642 | for (i = 0; i < 4; i++) { | |
643 | snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i); | |
644 | if (!strcmp(path, buf)) { | |
645 | DPRINTF("open base-addr of parport%d\n", i); | |
646 | if (config_is_real_pport(i)) { | |
647 | ret = (*func) (path, mode); | |
648 | } else { | |
649 | ret = (*func) ("/dev/null", mode); | |
650 | } | |
651 | ||
652 | if (ret) { | |
653 | baseaddrfp = ret; | |
654 | baseaddrnum = i; | |
655 | } | |
656 | ||
657 | return ret; | |
658 | } | |
659 | } | |
660 | ||
661 | ret = (*func) (path, mode); | |
662 | ||
663 | if (!strcmp(path, "/proc/modules")) { | |
664 | DPRINTF("opening /proc/modules\n"); | |
665 | #ifdef NO_WINDRVR | |
666 | modulesfp = ret; | |
667 | modules_read = 0; | |
668 | #endif | |
669 | } | |
670 | ||
671 | return ret; | |
672 | } | |
673 | ||
674 | char *fgets(char *s, int size, FILE *stream) { | |
675 | static char* (*func) (char*, int, FILE*) = NULL; | |
676 | const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"}; | |
677 | char buf[256]; | |
678 | char *ret = NULL; | |
679 | ||
680 | ||
681 | if (!func) | |
682 | func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets"); | |
683 | ||
684 | if (modulesfp == stream) { | |
685 | if (modules_read < sizeof(modules) / sizeof(modules[0])) { | |
686 | strcpy(s, modules[modules_read]); | |
687 | ret = s; | |
688 | modules_read++; | |
689 | } | |
690 | } else if (baseaddrfp == stream) { | |
691 | snprintf(s, sizeof(buf), "%d\t%d\n", | |
692 | (baseaddrnum) * 0x10, | |
693 | ((baseaddrnum) * 0x10) + 0x400); | |
694 | ret = s; | |
695 | } else { | |
696 | ret = (*func)(s,size,stream); | |
697 | } | |
698 | ||
699 | return ret; | |
700 | } | |
701 | ||
702 | int fclose(FILE *fp) { | |
703 | static int (*func) (FILE*) = NULL; | |
704 | ||
705 | if (!func) | |
706 | func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose"); | |
707 | ||
708 | if (fp == modulesfp) { | |
709 | modulesfp = NULL; | |
710 | } | |
711 | ||
712 | if (fp == baseaddrfp) { | |
713 | baseaddrfp = NULL; | |
714 | } | |
715 | ||
716 | return (*func)(fp); | |
717 | } | |
718 | ||
719 | int access(const char *pathname, int mode) { | |
720 | static int (*func) (const char*, int); | |
721 | ||
722 | if (!func) | |
723 | func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access"); | |
724 | ||
725 | if (pathname && !strcmp(pathname, "/dev/windrvr6")) { | |
726 | return 0; | |
727 | } else { | |
728 | return (*func)(pathname, mode); | |
729 | } | |
730 | } | |
731 | ||
732 | #if __WORDSIZE == 32 | |
733 | int uname (struct utsname *__name) { | |
734 | static int (*func) (struct utsname*); | |
735 | int ret; | |
736 | ||
737 | if (!func) | |
738 | func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname"); | |
739 | ||
740 | ret = (*func)(__name); | |
741 | ||
742 | if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) { | |
743 | strcpy(__name->machine, "i686"); | |
744 | } | |
745 | ||
746 | return ret; | |
747 | } | |
748 | #endif |