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