]>
Commit | Line | Data |
---|---|---|
54357994 | 1 | /* libusb/ppdev connector for XILINX impact |
f1405f13 | 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 | */ | |
cdc711dc | 23 | |
24 | #define _GNU_SOURCE 1 | |
25 | ||
cdc711dc | 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> | |
f10480d1 | 36 | #include <usb.h> |
2a7af812 | 37 | #include <signal.h> |
533f4b68 | 38 | #include <pthread.h> |
9ba1e383 | 39 | #include <errno.h> |
11d01742 | 40 | #include <inttypes.h> |
54357994 | 41 | #include <sys/ioctl.h> |
c42237a3 MG |
42 | #include <sys/utsname.h> |
43 | #include <bits/wordsize.h> | |
2c0c1255 | 44 | #include "usb-driver.h" |
3e670223 | 45 | #include "config.h" |
cbfa0ac6 | 46 | #include "xpcu.h" |
cdc711dc | 47 | |
9c9fd67c | 48 | static int (*ioctl_func) (int, int, void *) = NULL; |
332ced7a | 49 | static int windrvrfd = -1; |
54357994 | 50 | static unsigned long ppbase = 0; |
51 | static unsigned long ecpbase = 0; | |
25ba7a49 | 52 | static struct parport_config *pport = NULL; |
cbfa0ac6 | 53 | static struct xpcu_s *xpcu = NULL; |
be59993b MG |
54 | static FILE *modulesfp = NULL; |
55 | static FILE *baseaddrfp = NULL; | |
56 | static int baseaddrnum = 0; | |
723d9aa0 | 57 | static int modules_read = 0; |
b72b86b4 | 58 | static unsigned long card_type; |
ca18111b | 59 | static int ints_enabled = 0; |
533f4b68 | 60 | static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER; |
292160ed | 61 | |
795992ad | 62 | #define NO_WINDRVR 1 |
723d9aa0 | 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 | } | |
f1405f13 | 72 | fprintf(stderr,"\n"); |
723d9aa0 | 73 | } |
cdc711dc | 74 | |
cf55ed64 | 75 | |
be59993b | 76 | static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) { |
cdc711dc | 77 | struct header_struct* wdheader = (struct header_struct*)wdioctl; |
9c9fd67c | 78 | struct version_struct *version; |
79 | int ret = 0; | |
cdc711dc | 80 | |
da3ba95a | 81 | if (wdheader->magic != MAGIC) { |
2c2119eb | 82 | fprintf(stderr,"!!!ERROR: magic header does not match!!!\n"); |
83 | return (*ioctl_func) (fd, request, wdioctl); | |
cdc711dc | 84 | } |
85 | ||
bdc65937 | 86 | switch(request & ~(0xc0000000)) { |
da3ba95a | 87 | case VERSION: |
9c9fd67c | 88 | version = (struct version_struct*)(wdheader->data); |
81cf3658 | 89 | strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION); |
90831fba | 90 | version->versionul = 802; |
f1405f13 | 91 | DPRINTF("VERSION\n"); |
cdc711dc | 92 | break; |
2c2119eb | 93 | |
94 | case LICENSE: | |
f1405f13 | 95 | DPRINTF("LICENSE\n"); |
2c2119eb | 96 | break; |
97 | ||
f152c048 | 98 | case CARD_REGISTER_OLD: |
9c9fd67c | 99 | case CARD_REGISTER: |
54357994 | 100 | DPRINTF("CARD_REGISTER\n"); |
9c9fd67c | 101 | { |
533f4b68 | 102 | struct card_register* cr = (struct card_register*)(wdheader->data); |
54357994 | 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); | |
576995a8 | 115 | #ifndef NO_WINDRVR |
116 | ret = (*ioctl_func) (fd, request, wdioctl); | |
ac9e3f59 | 117 | #else |
bccac33b | 118 | |
25ba7a49 | 119 | pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10); |
61e0de7a | 120 | if (!pport) |
121 | break; | |
122 | ||
25ba7a49 | 123 | ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10); |
3e670223 | 124 | |
5e3d963b | 125 | ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr; |
bccac33b | 126 | |
5e3d963b | 127 | if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr) |
128 | ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr; | |
19b2e286 | 129 | |
5e3d963b | 130 | if (ret >= 0) { |
131 | cr->hCard = ret; | |
132 | } else { | |
19b2e286 | 133 | cr->hCard = 0; |
5e3d963b | 134 | } |
576995a8 | 135 | #endif |
ac9e3f59 | 136 | DPRINTF("hCard: %lu\n", cr->hCard); |
9c9fd67c | 137 | } |
da3ba95a | 138 | break; |
2c2119eb | 139 | |
da3ba95a | 140 | case USB_TRANSFER: |
f1405f13 | 141 | DPRINTF("in USB_TRANSFER"); |
da3ba95a | 142 | { |
143 | struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data); | |
144 | ||
723d9aa0 | 145 | #ifdef DEBUG |
f1405f13 | 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); | |
cf55ed64 MG |
149 | if (ut->dwPipeNum == 0) { |
150 | DPRINTF("setup packet: "); | |
151 | hexdump(ut->SetupPacket, 8); | |
152 | } | |
723d9aa0 | 153 | |
9c9fd67c | 154 | if (!ut->fRead && ut->dwBufferSize) |
155 | { | |
156 | hexdump(ut->pBuffer, ut->dwBufferSize); | |
9c9fd67c | 157 | } |
723d9aa0 | 158 | #endif |
9c9fd67c | 159 | |
795992ad | 160 | #ifndef NO_WINDRVR |
9c9fd67c | 161 | ret = (*ioctl_func) (fd, request, wdioctl); |
411af373 | 162 | #else |
cbfa0ac6 | 163 | xpcu_claim(xpcu, XPCU_CLAIM); |
411af373 | 164 | /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */ |
a70f4f38 | 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); | |
f1405f13 | 172 | DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size); |
cbfa0ac6 | 173 | ret = usb_control_msg(xpcu->handle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout); |
a70f4f38 | 174 | } else { |
175 | if (ut->fRead) { | |
cbfa0ac6 | 176 | ret = usb_bulk_read(xpcu->handle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout); |
a70f4f38 | 177 | } else { |
cbfa0ac6 | 178 | ret = usb_bulk_write(xpcu->handle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout); |
a70f4f38 | 179 | } |
cbfa0ac6 | 180 | xpcu_claim(xpcu, XPCU_RELEASE); |
a70f4f38 | 181 | } |
182 | ||
411af373 | 183 | if (ret < 0) { |
a70f4f38 | 184 | fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror()); |
411af373 | 185 | } else { |
186 | ut->dwBytesTransferred = ret; | |
187 | ret = 0; | |
188 | } | |
292160ed | 189 | #endif |
9c9fd67c | 190 | |
723d9aa0 | 191 | #ifdef DEBUG |
f1405f13 | 192 | DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write")); |
9c9fd67c | 193 | if (ut->fRead && ut->dwBytesTransferred) |
194 | { | |
f1405f13 | 195 | DPRINTF("Read: "); |
9c9fd67c | 196 | hexdump(ut->pBuffer, ut->dwBytesTransferred); |
197 | } | |
723d9aa0 | 198 | #endif |
da3ba95a | 199 | } |
cdc711dc | 200 | break; |
2c2119eb | 201 | |
f152c048 | 202 | case INT_ENABLE_OLD: |
da3ba95a | 203 | case INT_ENABLE: |
f1405f13 | 204 | DPRINTF("INT_ENABLE\n"); |
da3ba95a | 205 | { |
9c9fd67c | 206 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
cdc711dc | 207 | |
f1405f13 | 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); | |
cdc711dc | 212 | |
9c9fd67c | 213 | it->fEnableOk = 1; |
9ba1e383 | 214 | it->fStopped = 0; |
ca18111b | 215 | ints_enabled = 1; |
533f4b68 | 216 | pthread_mutex_trylock(&int_wait); |
9c9fd67c | 217 | } |
cdc711dc | 218 | |
cdc711dc | 219 | break; |
9c9fd67c | 220 | |
da3ba95a | 221 | case INT_DISABLE: |
f1405f13 | 222 | DPRINTF("INT_DISABLE\n"); |
da3ba95a | 223 | { |
9c9fd67c | 224 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
da3ba95a | 225 | |
f1405f13 | 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); | |
795992ad | 230 | #ifndef NO_WINDRVR |
9c9fd67c | 231 | ret = (*ioctl_func) (fd, request, wdioctl); |
e71b6bf3 | 232 | #else |
233 | it->dwCounter = 0; | |
234 | it->fStopped = 1; | |
ca18111b | 235 | ints_enabled = 0; |
9ba1e383 | 236 | if (pthread_mutex_trylock(&int_wait) == EBUSY) |
237 | pthread_mutex_unlock(&int_wait); | |
292160ed | 238 | #endif |
f1405f13 | 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); | |
da3ba95a | 243 | } |
da3ba95a | 244 | break; |
da3ba95a | 245 | |
9c9fd67c | 246 | case USB_SET_INTERFACE: |
f1405f13 | 247 | DPRINTF("USB_SET_INTERFACE\n"); |
da3ba95a | 248 | { |
9c9fd67c | 249 | struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data); |
250 | ||
f1405f13 | 251 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", |
252 | usi->dwUniqueID, usi->dwInterfaceNum, | |
253 | usi->dwAlternateSetting, usi->dwOptions); | |
795992ad | 254 | #ifndef NO_WINDRVR |
9c9fd67c | 255 | ret = (*ioctl_func) (fd, request, wdioctl); |
2a7af812 | 256 | #else |
cbfa0ac6 MG |
257 | if (xpcu->dev) { |
258 | if (!xpcu->handle) { | |
259 | xpcu->handle = usb_open(xpcu->dev); | |
adb51cf1 | 260 | #ifndef NO_USB_RESET |
cbfa0ac6 MG |
261 | if (xpcu->handle) { |
262 | usb_reset(xpcu->handle); | |
263 | xpcu->handle = usb_open(xpcu->dev); | |
adb51cf1 MG |
264 | } |
265 | #endif | |
266 | } | |
d0676964 | 267 | |
cbfa0ac6 MG |
268 | xpcu->interface = xpcu->dev->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber; |
269 | xpcu->alternate = usi->dwAlternateSetting; | |
2a7af812 | 270 | } |
292160ed | 271 | #endif |
f1405f13 | 272 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", |
273 | usi->dwUniqueID, usi->dwInterfaceNum, | |
274 | usi->dwAlternateSetting, usi->dwOptions); | |
da3ba95a | 275 | } |
cdc711dc | 276 | break; |
da3ba95a | 277 | |
f152c048 | 278 | case USB_GET_DEVICE_DATA_OLD: |
9c9fd67c | 279 | case USB_GET_DEVICE_DATA: |
f1405f13 | 280 | DPRINTF("USB_GET_DEVICE_DATA\n"); |
9c9fd67c | 281 | { |
282 | struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data); | |
283 | int pSize; | |
284 | ||
f1405f13 | 285 | DPRINTF("unique: %lu, bytes: %lu, options: %lx\n", |
286 | ugdd->dwUniqueID, ugdd->dwBytes, | |
287 | ugdd->dwOptions); | |
288 | ||
9c9fd67c | 289 | pSize = ugdd->dwBytes; |
e71b6bf3 | 290 | if (!ugdd->dwBytes) { |
cbfa0ac6 MG |
291 | if (xpcu->dev) { |
292 | ugdd->dwBytes = xpcu_deviceinfo(xpcu, NULL); | |
e71b6bf3 | 293 | } |
294 | } else { | |
cbfa0ac6 | 295 | xpcu_deviceinfo(xpcu, (unsigned char*)ugdd->pBuf); |
e71b6bf3 | 296 | } |
da3ba95a | 297 | } |
9c9fd67c | 298 | break; |
299 | ||
f152c048 | 300 | case EVENT_REGISTER_OLD: |
b0f621dd | 301 | case EVENT_REGISTER: |
f1405f13 | 302 | DPRINTF("EVENT_REGISTER\n"); |
b0f621dd | 303 | { |
304 | struct event *e = (struct event*)(wdheader->data); | |
ac22d975 | 305 | struct usb_bus *bus; |
3a2a22bc MG |
306 | char* devpos; |
307 | int busnum = -1, devnum = -1; | |
b0f621dd | 308 | int i; |
309 | ||
f1405f13 | 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 | ||
3a2a22bc MG |
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 | } | |
d377df04 | 332 | |
3a2a22bc MG |
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) { | |
77683ab6 | 340 | busnum = -1; |
3a2a22bc MG |
341 | devnum = -1; |
342 | } else { | |
343 | fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n", | |
344 | busnum, devnum); | |
345 | } | |
346 | } | |
347 | } | |
348 | } | |
d377df04 | 349 | |
ac22d975 | 350 | for (i = 0; i < e->dwNumMatchTables; i++) { |
f1405f13 | 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); | |
b0f621dd | 360 | |
cbfa0ac6 | 361 | for (bus = xpcu->busses; bus; bus = bus->next) { |
ac22d975 | 362 | struct usb_device *dev; |
363 | ||
3a2a22bc MG |
364 | if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum)) |
365 | continue; | |
366 | ||
ac22d975 | 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) && | |
d377df04 | 373 | (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) && |
a2a952c0 | 374 | ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) { |
2a7af812 | 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++) { | |
f1405f13 | 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 | ||
2a7af812 | 387 | if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) && |
388 | (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){ | |
389 | /* TODO: check interfaceClass! */ | |
f1405f13 | 390 | DPRINTF("found device with libusb\n"); |
cbfa0ac6 | 391 | xpcu->dev = dev; |
2a7af812 | 392 | card_type = e->dwCardType; |
393 | } | |
394 | } | |
395 | } | |
ac22d975 | 396 | } |
397 | } | |
398 | } | |
399 | } | |
400 | ||
795992ad | 401 | #ifndef NO_WINDRVR |
b0f621dd | 402 | ret = (*ioctl_func) (fd, request, wdioctl); |
2a7af812 | 403 | #else |
2a7af812 | 404 | e->handle++; |
292160ed | 405 | #endif |
b0f621dd | 406 | |
723d9aa0 | 407 | #ifdef DEBUG |
f1405f13 | 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 | ||
b0f621dd | 417 | for (i = 0; i < e->dwNumMatchTables; i++) |
f1405f13 | 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); | |
723d9aa0 | 426 | #endif |
b0f621dd | 427 | } |
428 | break; | |
429 | ||
f152c048 | 430 | case TRANSFER_OLD: |
da3ba95a | 431 | case TRANSFER: |
f1405f13 | 432 | DPRINTF("TRANSFER\n"); |
ac9e3f59 | 433 | { |
434 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
435 | ||
1dac5195 | 436 | #ifndef NO_WINDRVR |
437 | ret = (*ioctl_func) (fd, request, wdioctl); | |
438 | #else | |
25ba7a49 | 439 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1); |
1dac5195 | 440 | #endif |
ac9e3f59 | 441 | } |
9c9fd67c | 442 | break; |
443 | ||
54357994 | 444 | case MULTI_TRANSFER_OLD: |
576995a8 | 445 | case MULTI_TRANSFER: |
446 | DPRINTF("MULTI_TRANSFER\n"); | |
ac9e3f59 | 447 | { |
448 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
449 | unsigned long num = wdheader->size/sizeof(WD_TRANSFER); | |
1dac5195 | 450 | #ifndef NO_WINDRVR |
451 | ret = (*ioctl_func) (fd, request, wdioctl); | |
452 | #else | |
25ba7a49 | 453 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num); |
ac9e3f59 | 454 | #endif |
ac9e3f59 | 455 | } |
576995a8 | 456 | break; |
457 | ||
da3ba95a | 458 | case EVENT_UNREGISTER: |
f1405f13 | 459 | DPRINTF("EVENT_UNREGISTER\n"); |
795992ad | 460 | #ifndef NO_WINDRVR |
9c9fd67c | 461 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed | 462 | #endif |
9c9fd67c | 463 | break; |
464 | ||
da3ba95a | 465 | case INT_WAIT: |
f1405f13 | 466 | DPRINTF("INT_WAIT\n"); |
b0f621dd | 467 | { |
468 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
469 | ||
f1405f13 | 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); | |
b0f621dd | 474 | |
795992ad | 475 | #ifndef NO_WINDRVR |
b0f621dd | 476 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed | 477 | #else |
cbfa0ac6 | 478 | if (xpcu->dev) { |
2a7af812 | 479 | if (it->dwCounter == 0) { |
480 | it->dwCounter = 1; | |
481 | } else { | |
533f4b68 | 482 | pthread_mutex_lock(&int_wait); |
be452175 | 483 | pthread_mutex_unlock(&int_wait); |
2a7af812 | 484 | } |
94038a57 | 485 | } else { |
533f4b68 | 486 | pthread_mutex_lock(&int_wait); |
be452175 | 487 | pthread_mutex_unlock(&int_wait); |
2a7af812 | 488 | } |
292160ed | 489 | #endif |
490 | ||
f1405f13 | 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); | |
b0f621dd | 495 | } |
9c9fd67c | 496 | break; |
497 | ||
da3ba95a | 498 | case CARD_UNREGISTER: |
f1405f13 | 499 | DPRINTF("CARD_UNREGISTER\n"); |
54357994 | 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 | ||
795992ad | 510 | #ifndef NO_WINDRVR |
54357994 | 511 | ret = (*ioctl_func) (fd, request, wdioctl); |
512 | #else | |
72ce448b MG |
513 | if (pport) |
514 | pport->close(cr->hCard); | |
515 | ||
516 | pport = NULL; | |
292160ed | 517 | #endif |
54357994 | 518 | } |
9c9fd67c | 519 | break; |
520 | ||
da3ba95a | 521 | case EVENT_PULL: |
f1405f13 | 522 | DPRINTF("EVENT_PULL\n"); |
b1831983 | 523 | { |
524 | struct event *e = (struct event*)(wdheader->data); | |
723d9aa0 | 525 | #ifdef DEBUG |
b1831983 | 526 | int i; |
527 | ||
f1405f13 | 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 | ||
b1831983 | 536 | for (i = 0; i < e->dwNumMatchTables; i++) |
f1405f13 | 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); | |
723d9aa0 | 545 | #endif |
b1831983 | 546 | |
795992ad | 547 | #ifndef NO_WINDRVR |
b1831983 | 548 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed | 549 | #else |
cbfa0ac6 MG |
550 | if (xpcu->dev) { |
551 | struct usb_interface *interface = xpcu->dev->config->interface; | |
292160ed | 552 | |
553 | e->dwCardType = card_type; | |
554 | e->dwAction = 1; | |
555 | e->dwEventId = 109; | |
ca18111b | 556 | e->u.Usb.dwUniqueID = 110; |
cbfa0ac6 MG |
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; | |
292160ed | 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 | |
b1831983 | 566 | |
723d9aa0 | 567 | #ifdef DEBUG |
f1405f13 | 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 | ||
b1831983 | 576 | for (i = 0; i < e->dwNumMatchTables; i++) |
f1405f13 | 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); | |
723d9aa0 | 585 | #endif |
ca18111b | 586 | |
b1831983 | 587 | } |
9c9fd67c | 588 | break; |
589 | ||
da3ba95a | 590 | default: |
292160ed | 591 | fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request); |
795992ad | 592 | #ifndef NO_WINDRVR |
9c9fd67c | 593 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed | 594 | #endif |
595 | break; | |
cdc711dc | 596 | } |
da3ba95a | 597 | |
9c9fd67c | 598 | return ret; |
cdc711dc | 599 | } |
600 | ||
54357994 | 601 | int ioctl(int fd, unsigned long int request, ...) { |
dbda1264 | 602 | va_list args; |
603 | void *argp; | |
604 | int ret; | |
605 | ||
606 | if (!ioctl_func) | |
f1405f13 | 607 | ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl"); |
dbda1264 | 608 | |
609 | va_start (args, request); | |
610 | argp = va_arg (args, void *); | |
611 | va_end (args); | |
612 | ||
332ced7a | 613 | if (fd == windrvrfd) |
dbda1264 | 614 | ret = do_wdioctl(fd, request, argp); |
615 | else | |
616 | ret = (*ioctl_func) (fd, request, argp); | |
617 | ||
618 | return ret; | |
619 | } | |
cdc711dc | 620 | |
be452175 | 621 | int open (const char *pathname, int flags, ...) { |
622 | static int (*func) (const char *, int, mode_t) = NULL; | |
cdc711dc | 623 | mode_t mode = 0; |
624 | va_list args; | |
625 | int fd; | |
626 | ||
627 | if (!func) | |
f1405f13 | 628 | func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open"); |
cdc711dc | 629 | |
630 | if (flags & O_CREAT) { | |
631 | va_start(args, flags); | |
632 | mode = va_arg(args, mode_t); | |
633 | va_end(args); | |
634 | } | |
635 | ||
cdc711dc | 636 | if (!strcmp (pathname, "/dev/windrvr6")) { |
f1405f13 | 637 | DPRINTF("opening windrvr6\n"); |
795992ad | 638 | #ifdef NO_WINDRVR |
01b99d52 | 639 | windrvrfd = fd = (*func) ("/dev/null", flags, mode); |
640 | #else | |
641 | windrvrfd = fd = (*func) (pathname, flags, mode); | |
642 | #endif | |
cbfa0ac6 MG |
643 | if (!xpcu) |
644 | xpcu = xpcu_open(); | |
723d9aa0 | 645 | |
646 | return fd; | |
cdc711dc | 647 | } |
648 | ||
723d9aa0 | 649 | return (*func) (pathname, flags, mode); |
cdc711dc | 650 | } |
651 | ||
723d9aa0 | 652 | int close(int fd) { |
653 | static int (*func) (int) = NULL; | |
cdc711dc | 654 | |
723d9aa0 | 655 | if (!func) |
f1405f13 | 656 | func = (int (*) (int)) dlsym(RTLD_NEXT, "close"); |
723d9aa0 | 657 | |
11d01742 | 658 | if (fd == windrvrfd && windrvrfd >= 0) { |
f1405f13 | 659 | DPRINTF("close windrvrfd\n"); |
010cbaa6 | 660 | |
cbfa0ac6 MG |
661 | if (xpcu->handle) { |
662 | xpcu_claim(xpcu, XPCU_RELEASE); | |
663 | usb_close(xpcu->handle); | |
74ed4fbd | 664 | } |
010cbaa6 | 665 | |
cbfa0ac6 MG |
666 | xpcu->handle = NULL; |
667 | xpcu->interface = -1; | |
668 | xpcu = NULL; | |
332ced7a | 669 | windrvrfd = -1; |
cdc711dc | 670 | } |
cdc711dc | 671 | |
723d9aa0 | 672 | return (*func) (fd); |
cdc711dc | 673 | } |
674 | ||
723d9aa0 | 675 | FILE *fopen(const char *path, const char *mode) { |
676 | FILE *ret; | |
677 | static FILE* (*func) (const char*, const char*) = NULL; | |
0dca330e | 678 | char buf[256]; |
679 | int i; | |
ca18111b | 680 | |
681 | if (!func) | |
f1405f13 | 682 | func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen"); |
ca18111b | 683 | |
3e670223 | 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); | |
ca18111b | 704 | |
0dca330e | 705 | if (!strcmp(path, "/proc/modules")) { |
f1405f13 | 706 | DPRINTF("opening /proc/modules\n"); |
723d9aa0 | 707 | #ifdef NO_WINDRVR |
dbda1264 | 708 | modulesfp = ret; |
723d9aa0 | 709 | modules_read = 0; |
710 | #endif | |
711 | } | |
cdc711dc | 712 | |
713 | return ret; | |
714 | } | |
715 | ||
dbda1264 | 716 | char *fgets(char *s, int size, FILE *stream) { |
717 | static char* (*func) (char*, int, FILE*) = NULL; | |
16f6b164 | 718 | const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"}; |
0dca330e | 719 | char buf[256]; |
dbda1264 | 720 | char *ret = NULL; |
721 | ||
cdc711dc | 722 | |
723 | if (!func) | |
f1405f13 | 724 | func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets"); |
723d9aa0 | 725 | |
dbda1264 | 726 | if (modulesfp == stream) { |
54357994 | 727 | if (modules_read < sizeof(modules) / sizeof(modules[0])) { |
16f6b164 | 728 | strcpy(s, modules[modules_read]); |
dbda1264 | 729 | ret = s; |
16f6b164 | 730 | modules_read++; |
dbda1264 | 731 | } |
0dca330e | 732 | } else if (baseaddrfp == stream) { |
733 | snprintf(s, sizeof(buf), "%d\t%d\n", | |
734 | (baseaddrnum) * 0x10, | |
735 | ((baseaddrnum) * 0x10) + 0x400); | |
736 | ret = s; | |
723d9aa0 | 737 | } else { |
dbda1264 | 738 | ret = (*func)(s,size,stream); |
723d9aa0 | 739 | } |
cdc711dc | 740 | |
741 | return ret; | |
742 | } | |
743 | ||
dbda1264 | 744 | int fclose(FILE *fp) { |
745 | static int (*func) (FILE*) = NULL; | |
cdc711dc | 746 | |
dbda1264 | 747 | if (!func) |
f1405f13 | 748 | func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose"); |
cdc711dc | 749 | |
dbda1264 | 750 | if (fp == modulesfp) { |
751 | modulesfp = NULL; | |
752 | } | |
0dca330e | 753 | |
754 | if (fp == baseaddrfp) { | |
755 | baseaddrfp = NULL; | |
756 | } | |
dbda1264 | 757 | |
758 | return (*func)(fp); | |
cdc711dc | 759 | } |
419f2c98 | 760 | |
761 | int access(const char *pathname, int mode) { | |
762 | static int (*func) (const char*, int); | |
763 | ||
764 | if (!func) | |
f1405f13 | 765 | func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access"); |
3eee002c | 766 | |
b8b756f5 | 767 | if (pathname && !strcmp(pathname, "/dev/windrvr6")) { |
419f2c98 | 768 | return 0; |
769 | } else { | |
770 | return (*func)(pathname, mode); | |
771 | } | |
772 | } | |
c42237a3 MG |
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 |