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