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