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