]>
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_claim(xpcu, XPCU_CLAIM); | |
164 | /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */ | |
165 | if (ut->dwPipeNum == 0) { /* control pipe */ | |
166 | int requesttype, request, value, index, size; | |
167 | requesttype = ut->SetupPacket[0]; | |
168 | request = ut->SetupPacket[1]; | |
169 | value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8); | |
170 | index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8); | |
171 | size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8); | |
172 | DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size); | |
173 | ret = usb_control_msg(xpcu->handle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout); | |
174 | } else { | |
175 | if (ut->fRead) { | |
176 | ret = usb_bulk_read(xpcu->handle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout); | |
177 | } else { | |
178 | ret = usb_bulk_write(xpcu->handle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout); | |
179 | } | |
180 | xpcu_claim(xpcu, XPCU_RELEASE); | |
181 | } | |
182 | ||
183 | if (ret < 0) { | |
184 | fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror()); | |
185 | } else { | |
186 | ut->dwBytesTransferred = ret; | |
187 | ret = 0; | |
188 | } | |
189 | #endif | |
190 | ||
191 | #ifdef DEBUG | |
192 | DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write")); | |
193 | if (ut->fRead && ut->dwBytesTransferred) | |
194 | { | |
195 | DPRINTF("Read: "); | |
196 | hexdump(ut->pBuffer, ut->dwBytesTransferred); | |
197 | } | |
198 | #endif | |
199 | } | |
200 | break; | |
201 | ||
202 | case INT_ENABLE_OLD: | |
203 | case INT_ENABLE: | |
204 | DPRINTF("INT_ENABLE\n"); | |
205 | { | |
206 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
207 | ||
208 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
209 | it->hInterrupt, it->dwOptions, | |
210 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
211 | it->dwLost, it->fStopped); | |
212 | ||
213 | it->fEnableOk = 1; | |
214 | it->fStopped = 0; | |
215 | ints_enabled = 1; | |
216 | pthread_mutex_trylock(&int_wait); | |
217 | } | |
218 | ||
219 | break; | |
220 | ||
221 | case INT_DISABLE: | |
222 | DPRINTF("INT_DISABLE\n"); | |
223 | { | |
224 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
225 | ||
226 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
227 | it->hInterrupt, it->dwOptions, | |
228 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
229 | it->dwLost, it->fStopped); | |
230 | #ifndef NO_WINDRVR | |
231 | ret = (*ioctl_func) (fd, request, wdioctl); | |
232 | #else | |
233 | it->dwCounter = 0; | |
234 | it->fStopped = 1; | |
235 | ints_enabled = 0; | |
236 | if (pthread_mutex_trylock(&int_wait) == EBUSY) | |
237 | pthread_mutex_unlock(&int_wait); | |
238 | #endif | |
239 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
240 | it->hInterrupt, it->dwOptions, | |
241 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
242 | it->dwLost, it->fStopped); | |
243 | } | |
244 | break; | |
245 | ||
246 | case USB_SET_INTERFACE: | |
247 | DPRINTF("USB_SET_INTERFACE\n"); | |
248 | { | |
249 | struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data); | |
250 | ||
251 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", | |
252 | usi->dwUniqueID, usi->dwInterfaceNum, | |
253 | usi->dwAlternateSetting, usi->dwOptions); | |
254 | #ifndef NO_WINDRVR | |
255 | ret = (*ioctl_func) (fd, request, wdioctl); | |
256 | #else | |
257 | if (xpcu->dev) { | |
258 | if (!xpcu->handle) { | |
259 | xpcu->handle = usb_open(xpcu->dev); | |
260 | #ifndef NO_USB_RESET | |
261 | if (xpcu->handle) { | |
262 | usb_reset(xpcu->handle); | |
263 | xpcu->handle = usb_open(xpcu->dev); | |
264 | } | |
265 | #endif | |
266 | } | |
267 | ||
268 | xpcu->interface = xpcu->dev->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber; | |
269 | xpcu->alternate = usi->dwAlternateSetting; | |
270 | } | |
271 | #endif | |
272 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", | |
273 | usi->dwUniqueID, usi->dwInterfaceNum, | |
274 | usi->dwAlternateSetting, usi->dwOptions); | |
275 | } | |
276 | break; | |
277 | ||
278 | case USB_GET_DEVICE_DATA_OLD: | |
279 | case USB_GET_DEVICE_DATA: | |
280 | DPRINTF("USB_GET_DEVICE_DATA\n"); | |
281 | { | |
282 | struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data); | |
283 | int pSize; | |
284 | ||
285 | DPRINTF("unique: %lu, bytes: %lu, options: %lx\n", | |
286 | ugdd->dwUniqueID, ugdd->dwBytes, | |
287 | ugdd->dwOptions); | |
288 | ||
289 | pSize = ugdd->dwBytes; | |
290 | if (!ugdd->dwBytes) { | |
291 | if (xpcu->dev) { | |
292 | ugdd->dwBytes = xpcu_deviceinfo(xpcu, NULL); | |
293 | } | |
294 | } else { | |
295 | xpcu_deviceinfo(xpcu, (unsigned char*)ugdd->pBuf); | |
296 | } | |
297 | } | |
298 | break; | |
299 | ||
300 | case EVENT_REGISTER_OLD: | |
301 | case EVENT_REGISTER: | |
302 | DPRINTF("EVENT_REGISTER\n"); | |
303 | { | |
304 | struct event *e = (struct event*)(wdheader->data); | |
305 | struct usb_bus *bus; | |
306 | char* devpos; | |
307 | int busnum = -1, devnum = -1; | |
308 | int i; | |
309 | ||
310 | 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", | |
311 | e->handle, e->dwAction, | |
312 | e->dwStatus, e->dwEventId, e->dwCardType, | |
313 | e->hKernelPlugIn, e->dwOptions, | |
314 | e->u.Usb.deviceId.dwVendorId, | |
315 | e->u.Usb.deviceId.dwProductId, | |
316 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
317 | e->dwNumMatchTables); | |
318 | ||
319 | devpos = getenv("XILINX_USB_DEV"); | |
320 | if (devpos != NULL) { | |
321 | int j; | |
322 | char *devstr = NULL, *remainder; | |
323 | ||
324 | DPRINTF("XILINX_USB_DEV=%s\n", devpos); | |
325 | ||
326 | for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) { | |
327 | if (devpos[j] == ':') { | |
328 | devpos[j] = 0; | |
329 | devstr = &(devpos[j+1]); | |
330 | } | |
331 | } | |
332 | ||
333 | if (devstr && strlen(devstr) > 0) { | |
334 | busnum = strtol(devpos, &remainder, 10); | |
335 | if (devpos == remainder) { | |
336 | busnum = -1; | |
337 | } else { | |
338 | devnum = strtol(devstr, &remainder, 10); | |
339 | if (devstr == remainder) { | |
340 | busnum = -1; | |
341 | devnum = -1; | |
342 | } else { | |
343 | fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n", | |
344 | busnum, devnum); | |
345 | } | |
346 | } | |
347 | } | |
348 | } | |
349 | ||
350 | for (i = 0; i < e->dwNumMatchTables; i++) { | |
351 | ||
352 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
353 | e->matchTables[i].VendorId, | |
354 | e->matchTables[i].ProductId, | |
355 | e->matchTables[i].bDeviceClass, | |
356 | e->matchTables[i].bDeviceSubClass, | |
357 | e->matchTables[i].bInterfaceClass, | |
358 | e->matchTables[i].bInterfaceSubClass, | |
359 | e->matchTables[i].bInterfaceProtocol); | |
360 | ||
361 | for (bus = xpcu->busses; bus; bus = bus->next) { | |
362 | struct usb_device *dev; | |
363 | ||
364 | if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum)) | |
365 | continue; | |
366 | ||
367 | for (dev = bus->devices; dev; dev = dev->next) { | |
368 | struct usb_device_descriptor *desc = &(dev->descriptor); | |
369 | ||
370 | if((desc->idVendor == e->matchTables[i].VendorId) && | |
371 | (desc->idProduct == e->matchTables[i].ProductId) && | |
372 | (desc->bDeviceClass == e->matchTables[i].bDeviceClass) && | |
373 | (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) && | |
374 | ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) { | |
375 | int ac; | |
376 | for (ac = 0; ac < desc->bNumConfigurations; ac++) { | |
377 | struct usb_interface *interface = dev->config[ac].interface; | |
378 | int ai; | |
379 | ||
380 | for (ai = 0; ai < interface->num_altsetting; ai++) { | |
381 | ||
382 | DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n", | |
383 | interface->altsetting[i].bInterfaceClass, | |
384 | interface->altsetting[i].bInterfaceSubClass, | |
385 | interface->altsetting[i].bInterfaceProtocol); | |
386 | ||
387 | if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) && | |
388 | (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){ | |
389 | /* TODO: check interfaceClass! */ | |
390 | DPRINTF("found device with libusb\n"); | |
391 | xpcu->dev = dev; | |
392 | card_type = e->dwCardType; | |
393 | } | |
394 | } | |
395 | } | |
396 | } | |
397 | } | |
398 | } | |
399 | } | |
400 | ||
401 | #ifndef NO_WINDRVR | |
402 | ret = (*ioctl_func) (fd, request, wdioctl); | |
403 | #else | |
404 | e->handle++; | |
405 | #endif | |
406 | ||
407 | #ifdef DEBUG | |
408 | 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", | |
409 | e->handle, e->dwAction, | |
410 | e->dwStatus, e->dwEventId, e->dwCardType, | |
411 | e->hKernelPlugIn, e->dwOptions, | |
412 | e->u.Usb.deviceId.dwVendorId, | |
413 | e->u.Usb.deviceId.dwProductId, | |
414 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
415 | e->dwNumMatchTables); | |
416 | ||
417 | for (i = 0; i < e->dwNumMatchTables; i++) | |
418 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
419 | e->matchTables[i].VendorId, | |
420 | e->matchTables[i].ProductId, | |
421 | e->matchTables[i].bDeviceClass, | |
422 | e->matchTables[i].bDeviceSubClass, | |
423 | e->matchTables[i].bInterfaceClass, | |
424 | e->matchTables[i].bInterfaceSubClass, | |
425 | e->matchTables[i].bInterfaceProtocol); | |
426 | #endif | |
427 | } | |
428 | break; | |
429 | ||
430 | case TRANSFER_OLD: | |
431 | case TRANSFER: | |
432 | DPRINTF("TRANSFER\n"); | |
433 | { | |
434 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
435 | ||
436 | #ifndef NO_WINDRVR | |
437 | ret = (*ioctl_func) (fd, request, wdioctl); | |
438 | #else | |
439 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1); | |
440 | #endif | |
441 | } | |
442 | break; | |
443 | ||
444 | case MULTI_TRANSFER_OLD: | |
445 | case MULTI_TRANSFER: | |
446 | DPRINTF("MULTI_TRANSFER\n"); | |
447 | { | |
448 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
449 | unsigned long num = wdheader->size/sizeof(WD_TRANSFER); | |
450 | #ifndef NO_WINDRVR | |
451 | ret = (*ioctl_func) (fd, request, wdioctl); | |
452 | #else | |
453 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num); | |
454 | #endif | |
455 | } | |
456 | break; | |
457 | ||
458 | case EVENT_UNREGISTER: | |
459 | DPRINTF("EVENT_UNREGISTER\n"); | |
460 | #ifndef NO_WINDRVR | |
461 | ret = (*ioctl_func) (fd, request, wdioctl); | |
462 | #endif | |
463 | break; | |
464 | ||
465 | case INT_WAIT: | |
466 | DPRINTF("INT_WAIT\n"); | |
467 | { | |
468 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
469 | ||
470 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
471 | it->hInterrupt, it->dwOptions, | |
472 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
473 | it->dwLost, it->fStopped); | |
474 | ||
475 | #ifndef NO_WINDRVR | |
476 | ret = (*ioctl_func) (fd, request, wdioctl); | |
477 | #else | |
478 | if (xpcu->dev) { | |
479 | if (it->dwCounter == 0) { | |
480 | it->dwCounter = 1; | |
481 | } else { | |
482 | pthread_mutex_lock(&int_wait); | |
483 | pthread_mutex_unlock(&int_wait); | |
484 | } | |
485 | } else { | |
486 | pthread_mutex_lock(&int_wait); | |
487 | pthread_mutex_unlock(&int_wait); | |
488 | } | |
489 | #endif | |
490 | ||
491 | DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
492 | it->hInterrupt, it->dwOptions, it->dwCmds, | |
493 | it->fEnableOk, it->dwCounter, it->dwLost, | |
494 | it->fStopped); | |
495 | } | |
496 | break; | |
497 | ||
498 | case CARD_UNREGISTER: | |
499 | DPRINTF("CARD_UNREGISTER\n"); | |
500 | { | |
501 | struct card_register* cr = (struct card_register*)(wdheader->data); | |
502 | ||
503 | DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
504 | (unsigned long)cr->Card.Item[0].I.IO.dwAddr, | |
505 | cr->Card.Item[0].I.IO.dwBytes, | |
506 | cr->Card.Item[0].I.IO.dwBar); | |
507 | ||
508 | DPRINTF("hCard: %lu\n", cr->hCard); | |
509 | ||
510 | #ifndef NO_WINDRVR | |
511 | ret = (*ioctl_func) (fd, request, wdioctl); | |
512 | #else | |
513 | if (pport) | |
514 | pport->close(cr->hCard); | |
515 | ||
516 | pport = NULL; | |
517 | #endif | |
518 | } | |
519 | break; | |
520 | ||
521 | case EVENT_PULL: | |
522 | DPRINTF("EVENT_PULL\n"); | |
523 | { | |
524 | struct event *e = (struct event*)(wdheader->data); | |
525 | #ifdef DEBUG | |
526 | int i; | |
527 | ||
528 | 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", | |
529 | e->handle, e->dwAction, e->dwStatus, | |
530 | e->dwEventId, e->dwCardType, e->hKernelPlugIn, | |
531 | e->dwOptions, e->u.Usb.deviceId.dwVendorId, | |
532 | e->u.Usb.deviceId.dwProductId, | |
533 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
534 | e->dwNumMatchTables); | |
535 | ||
536 | for (i = 0; i < e->dwNumMatchTables; i++) | |
537 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
538 | e->matchTables[i].VendorId, | |
539 | e->matchTables[i].ProductId, | |
540 | e->matchTables[i].bDeviceClass, | |
541 | e->matchTables[i].bDeviceSubClass, | |
542 | e->matchTables[i].bInterfaceClass, | |
543 | e->matchTables[i].bInterfaceSubClass, | |
544 | e->matchTables[i].bInterfaceProtocol); | |
545 | #endif | |
546 | ||
547 | #ifndef NO_WINDRVR | |
548 | ret = (*ioctl_func) (fd, request, wdioctl); | |
549 | #else | |
550 | if (xpcu->dev) { | |
551 | struct usb_interface *interface = xpcu->dev->config->interface; | |
552 | ||
553 | e->dwCardType = card_type; | |
554 | e->dwAction = 1; | |
555 | e->dwEventId = 109; | |
556 | e->u.Usb.dwUniqueID = 110; | |
557 | e->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor; | |
558 | e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct; | |
559 | e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass; | |
560 | e->matchTables[0].bDeviceSubClass = xpcu->dev->descriptor.bDeviceSubClass; | |
561 | e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass; | |
562 | e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass; | |
563 | e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol; | |
564 | } | |
565 | #endif | |
566 | ||
567 | #ifdef DEBUG | |
568 | 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", | |
569 | e->handle, e->dwAction, e->dwStatus, | |
570 | e->dwEventId, e->dwCardType, e->hKernelPlugIn, | |
571 | e->dwOptions, e->u.Usb.deviceId.dwVendorId, | |
572 | e->u.Usb.deviceId.dwProductId, | |
573 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
574 | e->dwNumMatchTables); | |
575 | ||
576 | for (i = 0; i < e->dwNumMatchTables; i++) | |
577 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
578 | e->matchTables[i].VendorId, | |
579 | e->matchTables[i].ProductId, | |
580 | e->matchTables[i].bDeviceClass, | |
581 | e->matchTables[i].bDeviceSubClass, | |
582 | e->matchTables[i].bInterfaceClass, | |
583 | e->matchTables[i].bInterfaceSubClass, | |
584 | e->matchTables[i].bInterfaceProtocol); | |
585 | #endif | |
586 | ||
587 | } | |
588 | break; | |
589 | ||
590 | default: | |
591 | fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request); | |
592 | #ifndef NO_WINDRVR | |
593 | ret = (*ioctl_func) (fd, request, wdioctl); | |
594 | #endif | |
595 | break; | |
596 | } | |
597 | ||
598 | return ret; | |
599 | } | |
600 | ||
601 | int ioctl(int fd, unsigned long int request, ...) { | |
602 | va_list args; | |
603 | void *argp; | |
604 | int ret; | |
605 | ||
606 | if (!ioctl_func) | |
607 | ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl"); | |
608 | ||
609 | va_start (args, request); | |
610 | argp = va_arg (args, void *); | |
611 | va_end (args); | |
612 | ||
613 | if (fd == windrvrfd) | |
614 | ret = do_wdioctl(fd, request, argp); | |
615 | else | |
616 | ret = (*ioctl_func) (fd, request, argp); | |
617 | ||
618 | return ret; | |
619 | } | |
620 | ||
621 | int open (const char *pathname, int flags, ...) { | |
622 | static int (*func) (const char *, int, mode_t) = NULL; | |
623 | mode_t mode = 0; | |
624 | va_list args; | |
625 | int fd; | |
626 | ||
627 | if (!func) | |
628 | func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open"); | |
629 | ||
630 | if (flags & O_CREAT) { | |
631 | va_start(args, flags); | |
632 | mode = va_arg(args, mode_t); | |
633 | va_end(args); | |
634 | } | |
635 | ||
636 | if (!strcmp (pathname, "/dev/windrvr6")) { | |
637 | DPRINTF("opening windrvr6\n"); | |
638 | #ifdef NO_WINDRVR | |
639 | windrvrfd = fd = (*func) ("/dev/null", flags, mode); | |
640 | #else | |
641 | windrvrfd = fd = (*func) (pathname, flags, mode); | |
642 | #endif | |
643 | if (!xpcu) | |
644 | xpcu = xpcu_open(); | |
645 | ||
646 | return fd; | |
647 | } | |
648 | ||
649 | return (*func) (pathname, flags, mode); | |
650 | } | |
651 | ||
652 | int close(int fd) { | |
653 | static int (*func) (int) = NULL; | |
654 | ||
655 | if (!func) | |
656 | func = (int (*) (int)) dlsym(RTLD_NEXT, "close"); | |
657 | ||
658 | if (fd == windrvrfd && windrvrfd >= 0) { | |
659 | DPRINTF("close windrvrfd\n"); | |
660 | ||
661 | if (xpcu->handle) { | |
662 | xpcu_claim(xpcu, XPCU_RELEASE); | |
663 | usb_close(xpcu->handle); | |
664 | } | |
665 | ||
666 | xpcu->handle = NULL; | |
667 | xpcu->interface = -1; | |
668 | xpcu = NULL; | |
669 | windrvrfd = -1; | |
670 | } | |
671 | ||
672 | return (*func) (fd); | |
673 | } | |
674 | ||
675 | FILE *fopen(const char *path, const char *mode) { | |
676 | FILE *ret; | |
677 | static FILE* (*func) (const char*, const char*) = NULL; | |
678 | char buf[256]; | |
679 | int i; | |
680 | ||
681 | if (!func) | |
682 | func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen"); | |
683 | ||
684 | for (i = 0; i < 4; i++) { | |
685 | snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i); | |
686 | if (!strcmp(path, buf)) { | |
687 | DPRINTF("open base-addr of parport%d\n", i); | |
688 | if (config_is_real_pport(i)) { | |
689 | ret = (*func) (path, mode); | |
690 | } else { | |
691 | ret = (*func) ("/dev/null", mode); | |
692 | } | |
693 | ||
694 | if (ret) { | |
695 | baseaddrfp = ret; | |
696 | baseaddrnum = i; | |
697 | } | |
698 | ||
699 | return ret; | |
700 | } | |
701 | } | |
702 | ||
703 | ret = (*func) (path, mode); | |
704 | ||
705 | if (!strcmp(path, "/proc/modules")) { | |
706 | DPRINTF("opening /proc/modules\n"); | |
707 | #ifdef NO_WINDRVR | |
708 | modulesfp = ret; | |
709 | modules_read = 0; | |
710 | #endif | |
711 | } | |
712 | ||
713 | return ret; | |
714 | } | |
715 | ||
716 | char *fgets(char *s, int size, FILE *stream) { | |
717 | static char* (*func) (char*, int, FILE*) = NULL; | |
718 | const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"}; | |
719 | char buf[256]; | |
720 | char *ret = NULL; | |
721 | ||
722 | ||
723 | if (!func) | |
724 | func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets"); | |
725 | ||
726 | if (modulesfp == stream) { | |
727 | if (modules_read < sizeof(modules) / sizeof(modules[0])) { | |
728 | strcpy(s, modules[modules_read]); | |
729 | ret = s; | |
730 | modules_read++; | |
731 | } | |
732 | } else if (baseaddrfp == stream) { | |
733 | snprintf(s, sizeof(buf), "%d\t%d\n", | |
734 | (baseaddrnum) * 0x10, | |
735 | ((baseaddrnum) * 0x10) + 0x400); | |
736 | ret = s; | |
737 | } else { | |
738 | ret = (*func)(s,size,stream); | |
739 | } | |
740 | ||
741 | return ret; | |
742 | } | |
743 | ||
744 | int fclose(FILE *fp) { | |
745 | static int (*func) (FILE*) = NULL; | |
746 | ||
747 | if (!func) | |
748 | func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose"); | |
749 | ||
750 | if (fp == modulesfp) { | |
751 | modulesfp = NULL; | |
752 | } | |
753 | ||
754 | if (fp == baseaddrfp) { | |
755 | baseaddrfp = NULL; | |
756 | } | |
757 | ||
758 | return (*func)(fp); | |
759 | } | |
760 | ||
761 | int access(const char *pathname, int mode) { | |
762 | static int (*func) (const char*, int); | |
763 | ||
764 | if (!func) | |
765 | func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access"); | |
766 | ||
767 | if (pathname && !strcmp(pathname, "/dev/windrvr6")) { | |
768 | return 0; | |
769 | } else { | |
770 | return (*func)(pathname, mode); | |
771 | } | |
772 | } | |
773 | ||
774 | #if __WORDSIZE == 32 | |
775 | int uname (struct utsname *__name) { | |
776 | static int (*func) (struct utsname*); | |
777 | int ret; | |
778 | ||
779 | if (!func) | |
780 | func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname"); | |
781 | ||
782 | ret = (*func)(__name); | |
783 | ||
784 | if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) { | |
785 | strcpy(__name->machine, "i686"); | |
786 | } | |
787 | ||
788 | return ret; | |
789 | } | |
790 | #endif |