]>
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 <signal.h> | |
37 | #include <errno.h> | |
38 | #include <inttypes.h> | |
39 | #include <sys/ioctl.h> | |
40 | #include <sys/utsname.h> | |
41 | #include <bits/wordsize.h> | |
42 | #include <sys/ipc.h> | |
43 | #include <sys/sem.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 *windrvrfds = NULL; | |
50 | static int windrvrfds_count = 0; | |
51 | static unsigned long ppbase = 0; | |
52 | static unsigned long ecpbase = 0; | |
53 | static struct parport_config *pport = NULL; | |
54 | static FILE *modulesfp = NULL; | |
55 | static FILE *baseaddrfp = NULL; | |
56 | static int baseaddrnum = 0; | |
57 | static int modules_read = 0; | |
58 | ||
59 | #define NO_WINDRVR 1 | |
60 | ||
61 | void hexdump(unsigned char *buf, int len, char *prefix) { | |
62 | int i; | |
63 | ||
64 | fprintf(stderr, "%s ", prefix); | |
65 | for(i=0; i<len; i++) { | |
66 | fprintf(stderr,"%02x ", buf[i]); | |
67 | if ((i % 16) == 15) | |
68 | fprintf(stderr,"\n%s ", prefix); | |
69 | } | |
70 | fprintf(stderr,"\n"); | |
71 | } | |
72 | ||
73 | ||
74 | static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) { | |
75 | struct header_struct* wdheader = (struct header_struct*)wdioctl; | |
76 | struct version_struct *version; | |
77 | int ret = 0; | |
78 | ||
79 | if (wdheader->magic != MAGIC) { | |
80 | fprintf(stderr,"!!!ERROR: magic header does not match!!!\n"); | |
81 | return (*ioctl_func) (fd, request, wdioctl); | |
82 | } | |
83 | ||
84 | switch(request & ~(0xc0000000)) { | |
85 | case VERSION: | |
86 | version = (struct version_struct*)(wdheader->data); | |
87 | strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION); | |
88 | version->versionul = 802; | |
89 | DPRINTF("VERSION\n"); | |
90 | break; | |
91 | ||
92 | case LICENSE: | |
93 | DPRINTF("LICENSE\n"); | |
94 | break; | |
95 | ||
96 | case CARD_REGISTER_OLD: | |
97 | case CARD_REGISTER: | |
98 | DPRINTF("CARD_REGISTER\n"); | |
99 | { | |
100 | struct card_register* cr = (struct card_register*)(wdheader->data); | |
101 | ||
102 | DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
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 | ||
108 | DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
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); | |
113 | #ifndef NO_WINDRVR | |
114 | ret = (*ioctl_func) (fd, request, wdioctl); | |
115 | #else | |
116 | ||
117 | pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10); | |
118 | if (!pport) | |
119 | break; | |
120 | ||
121 | ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10); | |
122 | ||
123 | ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr; | |
124 | ||
125 | if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr) | |
126 | ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr; | |
127 | ||
128 | if (ret >= 0) { | |
129 | cr->hCard = ret; | |
130 | } else { | |
131 | cr->hCard = 0; | |
132 | } | |
133 | #endif | |
134 | DPRINTF("<-hCard: %lu\n", cr->hCard); | |
135 | } | |
136 | break; | |
137 | ||
138 | case USB_TRANSFER: | |
139 | DPRINTF("USB_TRANSFER\n"); | |
140 | { | |
141 | struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data); | |
142 | ||
143 | #ifdef DEBUG | |
144 | DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n", | |
145 | ut->dwUniqueID, ut->dwPipeNum, ut->fRead, | |
146 | ut->dwOptions, ut->dwBufferSize, ut->dwTimeout); | |
147 | if (ut->dwPipeNum == 0) { | |
148 | DPRINTF("-> setup packet:"); | |
149 | hexdump(ut->SetupPacket, 8, ""); | |
150 | } | |
151 | ||
152 | if (!ut->fRead && ut->dwBufferSize) | |
153 | { | |
154 | hexdump(ut->pBuffer, ut->dwBufferSize, "->"); | |
155 | } | |
156 | #endif | |
157 | ||
158 | #ifndef NO_WINDRVR | |
159 | ret = (*ioctl_func) (fd, request, wdioctl); | |
160 | #else | |
161 | ret = xpcu_transfer(ut); | |
162 | #endif | |
163 | ||
164 | #ifdef DEBUG | |
165 | DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write")); | |
166 | if (ut->fRead && ut->dwBytesTransferred) | |
167 | { | |
168 | hexdump(ut->pBuffer, ut->dwBytesTransferred, "<-"); | |
169 | } | |
170 | #endif | |
171 | } | |
172 | break; | |
173 | ||
174 | case INT_ENABLE_OLD: | |
175 | case INT_ENABLE: | |
176 | DPRINTF("INT_ENABLE\n"); | |
177 | { | |
178 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
179 | ||
180 | DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
181 | it->hInterrupt, it->dwOptions, | |
182 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
183 | it->dwLost, it->fStopped); | |
184 | ||
185 | #ifndef NO_WINDRVR | |
186 | ret = (*ioctl_func) (fd, request, wdioctl); | |
187 | #else | |
188 | ret = xpcu_int_state(it, ENABLE_INTERRUPT); | |
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); | |
195 | } | |
196 | ||
197 | break; | |
198 | ||
199 | case INT_DISABLE: | |
200 | DPRINTF("INT_DISABLE\n"); | |
201 | { | |
202 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
203 | ||
204 | DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
205 | it->hInterrupt, it->dwOptions, | |
206 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
207 | it->dwLost, it->fStopped); | |
208 | #ifndef NO_WINDRVR | |
209 | ret = (*ioctl_func) (fd, request, wdioctl); | |
210 | #else | |
211 | ret = xpcu_int_state(it, DISABLE_INTERRUPT); | |
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 | ret = xpcu_set_interface(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 | ret = xpcu_deviceinfo(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 | #ifndef NO_WINDRVR | |
273 | ret = (*ioctl_func) (fd, request, wdioctl); | |
274 | #else | |
275 | ret = xpcu_find(e); | |
276 | #endif | |
277 | ||
278 | #ifdef DEBUG | |
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", | |
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 | ||
288 | for (i = 0; i < e->dwNumMatchTables; i++) | |
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); | |
297 | #endif | |
298 | } | |
299 | break; | |
300 | ||
301 | case TRANSFER_OLD: | |
302 | case TRANSFER: | |
303 | DPRINTF("TRANSFER\n"); | |
304 | { | |
305 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
306 | ||
307 | #ifndef NO_WINDRVR | |
308 | ret = (*ioctl_func) (fd, request, wdioctl); | |
309 | #else | |
310 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1); | |
311 | #endif | |
312 | } | |
313 | break; | |
314 | ||
315 | case MULTI_TRANSFER_OLD: | |
316 | case MULTI_TRANSFER: | |
317 | DPRINTF("MULTI_TRANSFER\n"); | |
318 | { | |
319 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
320 | unsigned long num = wdheader->size/sizeof(WD_TRANSFER); | |
321 | #ifndef NO_WINDRVR | |
322 | ret = (*ioctl_func) (fd, request, wdioctl); | |
323 | #else | |
324 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num); | |
325 | #endif | |
326 | } | |
327 | break; | |
328 | ||
329 | case EVENT_UNREGISTER: | |
330 | { | |
331 | struct event *e = (struct event*)(wdheader->data); | |
332 | ||
333 | DPRINTF("EVENT_UNREGISTER\n"); | |
334 | #ifndef NO_WINDRVR | |
335 | ret = (*ioctl_func) (fd, request, wdioctl); | |
336 | #else | |
337 | ret = xpcu_close(e); | |
338 | #endif | |
339 | } | |
340 | break; | |
341 | ||
342 | case INT_WAIT: | |
343 | DPRINTF("INT_WAIT\n"); | |
344 | { | |
345 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
346 | ||
347 | DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
348 | it->hInterrupt, it->dwOptions, | |
349 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
350 | it->dwLost, it->fStopped); | |
351 | ||
352 | #ifndef NO_WINDRVR | |
353 | ret = (*ioctl_func) (fd, request, wdioctl); | |
354 | #else | |
355 | ret = xpcu_int_wait(it); | |
356 | #endif | |
357 | ||
358 | DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", | |
359 | it->hInterrupt, it->dwOptions, it->dwCmds, | |
360 | it->fEnableOk, it->dwCounter, it->dwLost, | |
361 | it->fStopped); | |
362 | } | |
363 | break; | |
364 | ||
365 | case CARD_UNREGISTER: | |
366 | DPRINTF("CARD_UNREGISTER\n"); | |
367 | { | |
368 | struct card_register* cr = (struct card_register*)(wdheader->data); | |
369 | ||
370 | DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
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 | ||
375 | DPRINTF("-> hCard: %lu\n", cr->hCard); | |
376 | ||
377 | #ifndef NO_WINDRVR | |
378 | ret = (*ioctl_func) (fd, request, wdioctl); | |
379 | #else | |
380 | if (pport) | |
381 | pport->close(cr->hCard); | |
382 | ||
383 | pport = NULL; | |
384 | #endif | |
385 | } | |
386 | break; | |
387 | ||
388 | case EVENT_PULL: | |
389 | DPRINTF("EVENT_PULL\n"); | |
390 | { | |
391 | struct event *e = (struct event*)(wdheader->data); | |
392 | #ifdef DEBUG | |
393 | int i; | |
394 | ||
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", | |
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 | ||
403 | for (i = 0; i < e->dwNumMatchTables; i++) | |
404 | DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
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); | |
412 | #endif | |
413 | ||
414 | #ifndef NO_WINDRVR | |
415 | ret = (*ioctl_func) (fd, request, wdioctl); | |
416 | #else | |
417 | ret = xpcu_found(e); | |
418 | #endif | |
419 | ||
420 | #ifdef DEBUG | |
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", | |
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 | ||
429 | for (i = 0; i < e->dwNumMatchTables; i++) | |
430 | DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
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); | |
438 | #endif | |
439 | ||
440 | } | |
441 | break; | |
442 | ||
443 | default: | |
444 | fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request); | |
445 | #ifndef NO_WINDRVR | |
446 | ret = (*ioctl_func) (fd, request, wdioctl); | |
447 | #endif | |
448 | break; | |
449 | } | |
450 | ||
451 | return ret; | |
452 | } | |
453 | ||
454 | int ioctl(int fd, unsigned long int request, ...) { | |
455 | va_list args; | |
456 | void *argp; | |
457 | int i; | |
458 | ||
459 | if (!ioctl_func) | |
460 | ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl"); | |
461 | ||
462 | va_start (args, request); | |
463 | argp = va_arg (args, void *); | |
464 | va_end (args); | |
465 | ||
466 | for (i = 0; i < windrvrfds_count; i++) { | |
467 | if (fd == windrvrfds[i]) | |
468 | return do_wdioctl(fd, request, argp); | |
469 | } | |
470 | ||
471 | return (*ioctl_func) (fd, request, argp); | |
472 | } | |
473 | ||
474 | int open (const char *pathname, int flags, ...) { | |
475 | static int (*func) (const char *, int, mode_t) = NULL; | |
476 | mode_t mode = 0; | |
477 | va_list args; | |
478 | int fd; | |
479 | ||
480 | if (!func) | |
481 | func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open"); | |
482 | ||
483 | if (flags & O_CREAT) { | |
484 | va_start(args, flags); | |
485 | mode = va_arg(args, mode_t); | |
486 | va_end(args); | |
487 | } | |
488 | ||
489 | if (!strcmp (pathname, "/dev/windrvr6")) { | |
490 | DPRINTF("opening windrvr6 (%d)\n", windrvrfds_count); | |
491 | windrvrfds = realloc(windrvrfds, sizeof(int) * (++windrvrfds_count)); | |
492 | if (!windrvrfds) | |
493 | return -ENOMEM; | |
494 | ||
495 | #ifdef NO_WINDRVR | |
496 | windrvrfds[windrvrfds_count-1] = fd = (*func) ("/dev/null", flags, mode); | |
497 | #else | |
498 | windrvrfds[windrvrfds_count-1] = fd = (*func) (pathname, flags, mode); | |
499 | #endif | |
500 | ||
501 | return fd; | |
502 | } | |
503 | ||
504 | return (*func) (pathname, flags, mode); | |
505 | } | |
506 | ||
507 | int close(int fd) { | |
508 | static int (*func) (int) = NULL; | |
509 | int i; | |
510 | ||
511 | if (!func) | |
512 | func = (int (*) (int)) dlsym(RTLD_NEXT, "close"); | |
513 | ||
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 | } | |
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 | 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 | } | |
568 | #ifdef NO_WINDRVR | |
569 | modulesfp = ret; | |
570 | modules_read = 0; | |
571 | #endif | |
572 | } | |
573 | ||
574 | return ret; | |
575 | } | |
576 | ||
577 | char *fgets(char *s, int size, FILE *stream) { | |
578 | static char* (*func) (char*, int, FILE*) = NULL; | |
579 | const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"}; | |
580 | char buf[256]; | |
581 | char *ret = NULL; | |
582 | ||
583 | ||
584 | if (!func) | |
585 | func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets"); | |
586 | ||
587 | if (modulesfp == stream) { | |
588 | if (modules_read < sizeof(modules) / sizeof(modules[0])) { | |
589 | strcpy(s, modules[modules_read]); | |
590 | ret = s; | |
591 | modules_read++; | |
592 | } | |
593 | } else if (baseaddrfp == stream) { | |
594 | snprintf(s, sizeof(buf), "%d\t%d\n", | |
595 | (baseaddrnum) * 0x10, | |
596 | ((baseaddrnum) * 0x10) + 0x400); | |
597 | ret = s; | |
598 | } else { | |
599 | ret = (*func)(s,size,stream); | |
600 | } | |
601 | ||
602 | return ret; | |
603 | } | |
604 | ||
605 | int fclose(FILE *fp) { | |
606 | static int (*func) (FILE*) = NULL; | |
607 | ||
608 | if (!func) | |
609 | func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose"); | |
610 | ||
611 | if (fp == modulesfp) { | |
612 | modulesfp = NULL; | |
613 | } | |
614 | ||
615 | if (fp == baseaddrfp) { | |
616 | baseaddrfp = NULL; | |
617 | } | |
618 | ||
619 | return (*func)(fp); | |
620 | } | |
621 | ||
622 | int access(const char *pathname, int mode) { | |
623 | static int (*func) (const char*, int); | |
624 | ||
625 | if (!func) | |
626 | func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access"); | |
627 | ||
628 | if (pathname && !strcmp(pathname, "/dev/windrvr6")) { | |
629 | return 0; | |
630 | } else { | |
631 | return (*func)(pathname, mode); | |
632 | } | |
633 | } | |
634 | ||
635 | #if 0 | |
636 | /* USB cable sharing needs to overload semop, TODO! */ | |
637 | int 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 | ||
658 | #if __WORDSIZE == 32 | |
659 | int 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 | |
675 | ||
676 | /* Ugly hack for ISE 12. They don't seem to open /proc/modules with | |
677 | * open() anymore... */ | |
678 | int _Z14isModuleLoadedPci(void) { | |
679 | DPRINTF("Faking _Z14isModuleLoadedPci\n"); | |
680 | ||
681 | return 1; | |
682 | } |