]> cvs.zerfleddert.de Git - usb-driver/blame - usb-driver.c
fix ISE 12 support on 64bit
[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>
2a7af812 36#include <signal.h>
9ba1e383 37#include <errno.h>
11d01742 38#include <inttypes.h>
54357994 39#include <sys/ioctl.h>
c42237a3
MG
40#include <sys/utsname.h>
41#include <bits/wordsize.h>
1fc8f7a4
MG
42#include <sys/ipc.h>
43#include <sys/sem.h>
daa4fc04
MG
44#include <syscall.h>
45#include <linux/personality.h>
2c0c1255 46#include "usb-driver.h"
3e670223 47#include "config.h"
cbfa0ac6 48#include "xpcu.h"
cdc711dc 49
9c9fd67c 50static int (*ioctl_func) (int, int, void *) = NULL;
9bfaca62
MG
51static int *windrvrfds = NULL;
52static int windrvrfds_count = 0;
54357994 53static unsigned long ppbase = 0;
54static unsigned long ecpbase = 0;
25ba7a49 55static struct parport_config *pport = NULL;
be59993b
MG
56static FILE *modulesfp = NULL;
57static FILE *baseaddrfp = NULL;
58static int baseaddrnum = 0;
723d9aa0 59static int modules_read = 0;
292160ed 60
795992ad 61#define NO_WINDRVR 1
723d9aa0 62
325556c9 63void hexdump(unsigned char *buf, int len, char *prefix) {
723d9aa0 64 int i;
65
325556c9 66 fprintf(stderr, "%s ", prefix);
723d9aa0 67 for(i=0; i<len; i++) {
68 fprintf(stderr,"%02x ", buf[i]);
69 if ((i % 16) == 15)
325556c9 70 fprintf(stderr,"\n%s ", prefix);
723d9aa0 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
501f1c21 104 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
54357994 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
501f1c21 110 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
54357994 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
501f1c21 136 DPRINTF("<-hCard: %lu\n", cr->hCard);
9c9fd67c 137 }
da3ba95a 138 break;
2c2119eb 139
da3ba95a 140 case USB_TRANSFER:
501f1c21 141 DPRINTF("USB_TRANSFER\n");
da3ba95a 142 {
143 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
144
723d9aa0 145#ifdef DEBUG
501f1c21 146 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
f1405f13 147 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
148 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
cf55ed64 149 if (ut->dwPipeNum == 0) {
325556c9
MG
150 DPRINTF("-> setup packet:");
151 hexdump(ut->SetupPacket, 8, "");
cf55ed64 152 }
723d9aa0 153
9c9fd67c 154 if (!ut->fRead && ut->dwBufferSize)
155 {
325556c9 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
19acdb82 163 ret = xpcu_transfer(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 {
325556c9 170 hexdump(ut->pBuffer, ut->dwBytesTransferred, "<-");
9c9fd67c 171 }
723d9aa0 172#endif
da3ba95a 173 }
cdc711dc 174 break;
2c2119eb 175
f152c048 176 case INT_ENABLE_OLD:
da3ba95a 177 case INT_ENABLE:
f1405f13 178 DPRINTF("INT_ENABLE\n");
da3ba95a 179 {
9c9fd67c 180 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 181
501f1c21 182 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 183 it->hInterrupt, it->dwOptions,
184 it->dwCmds, it->fEnableOk, it->dwCounter,
185 it->dwLost, it->fStopped);
cdc711dc 186
501f1c21
MG
187#ifndef NO_WINDRVR
188 ret = (*ioctl_func) (fd, request, wdioctl);
189#else
19acdb82 190 ret = xpcu_int_state(it, ENABLE_INTERRUPT);
501f1c21
MG
191#endif
192
193 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
194 it->hInterrupt, it->dwOptions,
195 it->dwCmds, it->fEnableOk, it->dwCounter,
196 it->dwLost, it->fStopped);
9c9fd67c 197 }
cdc711dc 198
cdc711dc 199 break;
9c9fd67c 200
da3ba95a 201 case INT_DISABLE:
f1405f13 202 DPRINTF("INT_DISABLE\n");
da3ba95a 203 {
9c9fd67c 204 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 205
501f1c21 206 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 207 it->hInterrupt, it->dwOptions,
208 it->dwCmds, it->fEnableOk, it->dwCounter,
209 it->dwLost, it->fStopped);
795992ad 210#ifndef NO_WINDRVR
9c9fd67c 211 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 212#else
19acdb82 213 ret = xpcu_int_state(it, DISABLE_INTERRUPT);
292160ed 214#endif
501f1c21 215 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 216 it->hInterrupt, it->dwOptions,
217 it->dwCmds, it->fEnableOk, it->dwCounter,
218 it->dwLost, it->fStopped);
da3ba95a 219 }
da3ba95a 220 break;
da3ba95a 221
9c9fd67c 222 case USB_SET_INTERFACE:
f1405f13 223 DPRINTF("USB_SET_INTERFACE\n");
da3ba95a 224 {
9c9fd67c 225 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
226
501f1c21 227 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
f1405f13 228 usi->dwUniqueID, usi->dwInterfaceNum,
229 usi->dwAlternateSetting, usi->dwOptions);
795992ad 230#ifndef NO_WINDRVR
9c9fd67c 231 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 232#else
19acdb82 233 ret = xpcu_set_interface(usi);
292160ed 234#endif
501f1c21 235 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
f1405f13 236 usi->dwUniqueID, usi->dwInterfaceNum,
237 usi->dwAlternateSetting, usi->dwOptions);
da3ba95a 238 }
cdc711dc 239 break;
da3ba95a 240
f152c048 241 case USB_GET_DEVICE_DATA_OLD:
9c9fd67c 242 case USB_GET_DEVICE_DATA:
f1405f13 243 DPRINTF("USB_GET_DEVICE_DATA\n");
9c9fd67c 244 {
245 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
9c9fd67c 246
501f1c21 247 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
f1405f13 248 ugdd->dwUniqueID, ugdd->dwBytes,
249 ugdd->dwOptions);
250
19acdb82 251 ret = xpcu_deviceinfo(ugdd);
f92c0fbc 252
da3ba95a 253 }
9c9fd67c 254 break;
255
f152c048 256 case EVENT_REGISTER_OLD:
b0f621dd 257 case EVENT_REGISTER:
f1405f13 258 DPRINTF("EVENT_REGISTER\n");
b0f621dd 259 {
260 struct event *e = (struct event*)(wdheader->data);
f92c0fbc 261#ifdef DEBUG
b0f621dd 262 int i;
f92c0fbc 263#endif
b0f621dd 264
501f1c21 265 DPRINTF("-> handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 266 e->handle, e->dwAction,
267 e->dwStatus, e->dwEventId, e->dwCardType,
268 e->hKernelPlugIn, e->dwOptions,
269 e->u.Usb.deviceId.dwVendorId,
270 e->u.Usb.deviceId.dwProductId,
271 e->u.Usb.dwUniqueID, e->dwEventVer,
272 e->dwNumMatchTables);
273
795992ad 274#ifndef NO_WINDRVR
b0f621dd 275 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 276#else
19acdb82 277 ret = xpcu_find(e);
292160ed 278#endif
b0f621dd 279
723d9aa0 280#ifdef DEBUG
501f1c21 281 DPRINTF("<- handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 282 e->handle, e->dwAction,
283 e->dwStatus, e->dwEventId, e->dwCardType,
284 e->hKernelPlugIn, e->dwOptions,
285 e->u.Usb.deviceId.dwVendorId,
286 e->u.Usb.deviceId.dwProductId,
287 e->u.Usb.dwUniqueID, e->dwEventVer,
288 e->dwNumMatchTables);
289
b0f621dd 290 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 291 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
292 e->matchTables[i].VendorId,
293 e->matchTables[i].ProductId,
294 e->matchTables[i].bDeviceClass,
295 e->matchTables[i].bDeviceSubClass,
296 e->matchTables[i].bInterfaceClass,
297 e->matchTables[i].bInterfaceSubClass,
298 e->matchTables[i].bInterfaceProtocol);
723d9aa0 299#endif
b0f621dd 300 }
301 break;
302
f152c048 303 case TRANSFER_OLD:
da3ba95a 304 case TRANSFER:
f1405f13 305 DPRINTF("TRANSFER\n");
ac9e3f59 306 {
307 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
308
1dac5195 309#ifndef NO_WINDRVR
310 ret = (*ioctl_func) (fd, request, wdioctl);
311#else
25ba7a49 312 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1);
1dac5195 313#endif
ac9e3f59 314 }
9c9fd67c 315 break;
316
54357994 317 case MULTI_TRANSFER_OLD:
576995a8 318 case MULTI_TRANSFER:
319 DPRINTF("MULTI_TRANSFER\n");
ac9e3f59 320 {
321 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
322 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
1dac5195 323#ifndef NO_WINDRVR
324 ret = (*ioctl_func) (fd, request, wdioctl);
325#else
25ba7a49 326 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num);
ac9e3f59 327#endif
ac9e3f59 328 }
576995a8 329 break;
330
da3ba95a 331 case EVENT_UNREGISTER:
f92c0fbc
MG
332 {
333 struct event *e = (struct event*)(wdheader->data);
334
335 DPRINTF("EVENT_UNREGISTER\n");
795992ad 336#ifndef NO_WINDRVR
f92c0fbc
MG
337 ret = (*ioctl_func) (fd, request, wdioctl);
338#else
19acdb82 339 ret = xpcu_close(e);
292160ed 340#endif
f92c0fbc 341 }
9c9fd67c 342 break;
343
da3ba95a 344 case INT_WAIT:
f1405f13 345 DPRINTF("INT_WAIT\n");
b0f621dd 346 {
347 struct interrupt *it = (struct interrupt*)(wdheader->data);
348
501f1c21 349 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 350 it->hInterrupt, it->dwOptions,
351 it->dwCmds, it->fEnableOk, it->dwCounter,
352 it->dwLost, it->fStopped);
b0f621dd 353
795992ad 354#ifndef NO_WINDRVR
b0f621dd 355 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 356#else
19acdb82 357 ret = xpcu_int_wait(it);
292160ed 358#endif
359
501f1c21 360 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 361 it->hInterrupt, it->dwOptions, it->dwCmds,
362 it->fEnableOk, it->dwCounter, it->dwLost,
363 it->fStopped);
b0f621dd 364 }
9c9fd67c 365 break;
366
da3ba95a 367 case CARD_UNREGISTER:
f1405f13 368 DPRINTF("CARD_UNREGISTER\n");
54357994 369 {
370 struct card_register* cr = (struct card_register*)(wdheader->data);
371
501f1c21 372 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
54357994 373 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
374 cr->Card.Item[0].I.IO.dwBytes,
375 cr->Card.Item[0].I.IO.dwBar);
376
501f1c21 377 DPRINTF("-> hCard: %lu\n", cr->hCard);
54357994 378
795992ad 379#ifndef NO_WINDRVR
54357994 380 ret = (*ioctl_func) (fd, request, wdioctl);
381#else
72ce448b
MG
382 if (pport)
383 pport->close(cr->hCard);
384
385 pport = NULL;
292160ed 386#endif
54357994 387 }
9c9fd67c 388 break;
389
da3ba95a 390 case EVENT_PULL:
f1405f13 391 DPRINTF("EVENT_PULL\n");
b1831983 392 {
393 struct event *e = (struct event*)(wdheader->data);
723d9aa0 394#ifdef DEBUG
b1831983 395 int i;
396
501f1c21 397 DPRINTF("-> handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 398 e->handle, e->dwAction, e->dwStatus,
399 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
400 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
401 e->u.Usb.deviceId.dwProductId,
402 e->u.Usb.dwUniqueID, e->dwEventVer,
403 e->dwNumMatchTables);
404
b1831983 405 for (i = 0; i < e->dwNumMatchTables; i++)
501f1c21 406 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
f1405f13 407 e->matchTables[i].VendorId,
408 e->matchTables[i].ProductId,
409 e->matchTables[i].bDeviceClass,
410 e->matchTables[i].bDeviceSubClass,
411 e->matchTables[i].bInterfaceClass,
412 e->matchTables[i].bInterfaceSubClass,
413 e->matchTables[i].bInterfaceProtocol);
723d9aa0 414#endif
b1831983 415
795992ad 416#ifndef NO_WINDRVR
b1831983 417 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 418#else
19acdb82 419 ret = xpcu_found(e);
292160ed 420#endif
b1831983 421
723d9aa0 422#ifdef DEBUG
501f1c21 423 DPRINTF("<- handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 424 e->handle, e->dwAction, e->dwStatus,
425 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
426 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
427 e->u.Usb.deviceId.dwProductId,
428 e->u.Usb.dwUniqueID, e->dwEventVer,
429 e->dwNumMatchTables);
430
b1831983 431 for (i = 0; i < e->dwNumMatchTables; i++)
501f1c21 432 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
f1405f13 433 e->matchTables[i].VendorId,
434 e->matchTables[i].ProductId,
435 e->matchTables[i].bDeviceClass,
436 e->matchTables[i].bDeviceSubClass,
437 e->matchTables[i].bInterfaceClass,
438 e->matchTables[i].bInterfaceSubClass,
439 e->matchTables[i].bInterfaceProtocol);
723d9aa0 440#endif
ca18111b 441
b1831983 442 }
9c9fd67c 443 break;
444
da3ba95a 445 default:
292160ed 446 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
795992ad 447#ifndef NO_WINDRVR
9c9fd67c 448 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 449#endif
450 break;
cdc711dc 451 }
da3ba95a 452
9c9fd67c 453 return ret;
cdc711dc 454}
455
54357994 456int ioctl(int fd, unsigned long int request, ...) {
dbda1264 457 va_list args;
458 void *argp;
9bfaca62 459 int i;
dbda1264 460
461 if (!ioctl_func)
f1405f13 462 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
dbda1264 463
464 va_start (args, request);
465 argp = va_arg (args, void *);
466 va_end (args);
467
9bfaca62
MG
468 for (i = 0; i < windrvrfds_count; i++) {
469 if (fd == windrvrfds[i])
470 return do_wdioctl(fd, request, argp);
471 }
dbda1264 472
9bfaca62 473 return (*ioctl_func) (fd, request, argp);
dbda1264 474}
cdc711dc 475
be452175 476int open (const char *pathname, int flags, ...) {
477 static int (*func) (const char *, int, mode_t) = NULL;
cdc711dc 478 mode_t mode = 0;
479 va_list args;
480 int fd;
481
482 if (!func)
f1405f13 483 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
cdc711dc 484
485 if (flags & O_CREAT) {
486 va_start(args, flags);
487 mode = va_arg(args, mode_t);
488 va_end(args);
489 }
490
cdc711dc 491 if (!strcmp (pathname, "/dev/windrvr6")) {
9bfaca62
MG
492 DPRINTF("opening windrvr6 (%d)\n", windrvrfds_count);
493 windrvrfds = realloc(windrvrfds, sizeof(int) * (++windrvrfds_count));
494 if (!windrvrfds)
495 return -ENOMEM;
496
795992ad 497#ifdef NO_WINDRVR
9bfaca62 498 windrvrfds[windrvrfds_count-1] = fd = (*func) ("/dev/null", flags, mode);
01b99d52 499#else
9bfaca62 500 windrvrfds[windrvrfds_count-1] = fd = (*func) (pathname, flags, mode);
01b99d52 501#endif
723d9aa0 502
503 return fd;
cdc711dc 504 }
505
723d9aa0 506 return (*func) (pathname, flags, mode);
cdc711dc 507}
508
723d9aa0 509int close(int fd) {
510 static int (*func) (int) = NULL;
9bfaca62 511 int i;
cdc711dc 512
723d9aa0 513 if (!func)
f1405f13 514 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
723d9aa0 515
9bfaca62
MG
516 for (i = 0; i < windrvrfds_count; i++) {
517 if (fd == windrvrfds[i] && windrvrfds[i] >= 0) {
518 int remaining = windrvrfds_count - (i + 1);
519 DPRINTF("close windrvr6 (%d)\n", i);
520 if (remaining)
521 memmove(&(windrvrfds[i]), &(windrvrfds[i+1]), remaining * sizeof(int));
522 windrvrfds = realloc(windrvrfds, sizeof(int) * --windrvrfds_count);
523 if (!windrvrfds_count)
524 windrvrfds = NULL;
525 break;
526 }
cdc711dc 527 }
cdc711dc 528
723d9aa0 529 return (*func) (fd);
cdc711dc 530}
531
723d9aa0 532FILE *fopen(const char *path, const char *mode) {
533 FILE *ret;
534 static FILE* (*func) (const char*, const char*) = NULL;
0dca330e 535 char buf[256];
536 int i;
ca18111b 537
538 if (!func)
f1405f13 539 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
ca18111b 540
3e670223 541 for (i = 0; i < 4; i++) {
542 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
543 if (!strcmp(path, buf)) {
544 DPRINTF("open base-addr of parport%d\n", i);
545 if (config_is_real_pport(i)) {
546 ret = (*func) (path, mode);
547 } else {
548 ret = (*func) ("/dev/null", mode);
549 }
550
551 if (ret) {
552 baseaddrfp = ret;
553 baseaddrnum = i;
554 }
555
556 return ret;
557 }
558 }
559
560 ret = (*func) (path, mode);
ca18111b 561
0dca330e 562 if (!strcmp(path, "/proc/modules")) {
f1405f13 563 DPRINTF("opening /proc/modules\n");
aed36bb3
MG
564 if (!ret && errno == ENOENT) {
565 /* Hmm.. there appears to be no /proc/modules file
566 * fake it then */
567 ret = (*func)("/dev/null", mode);
568 DPRINTF("No /proc/modules -- faking\n");
569 }
723d9aa0 570#ifdef NO_WINDRVR
dbda1264 571 modulesfp = ret;
723d9aa0 572 modules_read = 0;
573#endif
574 }
cdc711dc 575
576 return ret;
577}
578
dbda1264 579char *fgets(char *s, int size, FILE *stream) {
580 static char* (*func) (char*, int, FILE*) = NULL;
16f6b164 581 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
0dca330e 582 char buf[256];
dbda1264 583 char *ret = NULL;
584
cdc711dc 585
586 if (!func)
f1405f13 587 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
723d9aa0 588
dbda1264 589 if (modulesfp == stream) {
54357994 590 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
16f6b164 591 strcpy(s, modules[modules_read]);
dbda1264 592 ret = s;
16f6b164 593 modules_read++;
dbda1264 594 }
0dca330e 595 } else if (baseaddrfp == stream) {
596 snprintf(s, sizeof(buf), "%d\t%d\n",
597 (baseaddrnum) * 0x10,
598 ((baseaddrnum) * 0x10) + 0x400);
599 ret = s;
723d9aa0 600 } else {
dbda1264 601 ret = (*func)(s,size,stream);
723d9aa0 602 }
cdc711dc 603
604 return ret;
605}
606
dbda1264 607int fclose(FILE *fp) {
608 static int (*func) (FILE*) = NULL;
cdc711dc 609
dbda1264 610 if (!func)
f1405f13 611 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
cdc711dc 612
dbda1264 613 if (fp == modulesfp) {
614 modulesfp = NULL;
615 }
0dca330e 616
617 if (fp == baseaddrfp) {
618 baseaddrfp = NULL;
619 }
dbda1264 620
621 return (*func)(fp);
cdc711dc 622}
419f2c98 623
624int access(const char *pathname, int mode) {
625 static int (*func) (const char*, int);
626
627 if (!func)
f1405f13 628 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
3eee002c 629
b8b756f5 630 if (pathname && !strcmp(pathname, "/dev/windrvr6")) {
419f2c98 631 return 0;
632 } else {
633 return (*func)(pathname, mode);
634 }
635}
c42237a3 636
1fc8f7a4
MG
637#if 0
638/* USB cable sharing needs to overload semop, TODO! */
639int semop (int __semid, struct sembuf *__sops, size_t __nsops) {
640 static int (*func) (int, struct sembuf*, size_t) = NULL;
641 int i;
642
643 if (!func)
644 func = (int (*) (int, struct sembuf*, size_t)) dlsym(RTLD_NEXT, "semop");
645
646 fprintf(stderr,"semop: semid: 0x%X, elements: %d\n", __semid, __nsops);
647 for (i = 0; i < __nsops; i++) {
648 fprintf(stderr, " num: %u, op: %d, flg: %d\n", __sops[i].sem_num, __sops[i].sem_op, __sops[i].sem_flg);
649 if (__sops[i].sem_op < 0) {
650 fprintf(stderr, "SEMAPHORE LOCK\n");
651 } else {
652 fprintf(stderr, "SEMAPHORE UNLOCK\n");
653 }
654 }
655
656 return (*func)(__semid, __sops, __nsops);
657}
658#endif
659
9f78065e
MG
660/*
661 * Ugly hack for ISE 12. They don't seem to open /proc/modules with
662 * open() anymore...
663 * echo '_Z14isModuleLoadedPci' | c++filt
664 */
80a14787 665long int _Z14isModuleLoadedPci(char *module_name, int i) {
9f78065e 666 DPRINTF("_Z14isModuleLoadedPci: Checking for module %s (%d)\n", module_name, i);
4ba5d6d9
MG
667
668 return 1;
669}
daa4fc04
MG
670
671static void __attribute__ ((constructor)) libusbdriver_init(void) {
672 #if __WORDSIZE == 32
673 struct utsname un;
674 int ret;
675
676 ret = uname(&un);
677
678 if (ret == 0 && (!strcmp(un.machine, "x86_64"))) {
679 DPRINTF("setting 32bit personality\n");
680 (long)syscall(SYS_personality, PER_LINUX32);
681 }
682 #endif
683}
Impressum, Datenschutz