]>
Commit | Line | Data |
---|---|---|
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> | |
f10480d1 | 36 | #include <usb.h> |
2a7af812 | 37 | #include <signal.h> |
533f4b68 | 38 | #include <pthread.h> |
9ba1e383 | 39 | #include <errno.h> |
11d01742 | 40 | #include <inttypes.h> |
54357994 | 41 | #include <sys/ioctl.h> |
c42237a3 MG |
42 | #include <sys/utsname.h> |
43 | #include <bits/wordsize.h> | |
2c0c1255 | 44 | #include "usb-driver.h" |
3e670223 | 45 | #include "config.h" |
cbfa0ac6 | 46 | #include "xpcu.h" |
cdc711dc | 47 | |
9c9fd67c | 48 | static int (*ioctl_func) (int, int, void *) = NULL; |
332ced7a | 49 | static int windrvrfd = -1; |
54357994 | 50 | static unsigned long ppbase = 0; |
51 | static unsigned long ecpbase = 0; | |
25ba7a49 | 52 | static struct parport_config *pport = NULL; |
cbfa0ac6 | 53 | static struct xpcu_s *xpcu = NULL; |
be59993b MG |
54 | static FILE *modulesfp = NULL; |
55 | static FILE *baseaddrfp = NULL; | |
56 | static int baseaddrnum = 0; | |
723d9aa0 | 57 | static int modules_read = 0; |
b72b86b4 | 58 | static unsigned long card_type; |
ca18111b | 59 | static int ints_enabled = 0; |
533f4b68 | 60 | static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER; |
292160ed | 61 | |
795992ad | 62 | #define NO_WINDRVR 1 |
723d9aa0 | 63 | |
64 | void hexdump(unsigned char *buf, int len) { | |
65 | int i; | |
66 | ||
67 | for(i=0; i<len; i++) { | |
68 | fprintf(stderr,"%02x ", buf[i]); | |
69 | if ((i % 16) == 15) | |
70 | fprintf(stderr,"\n"); | |
71 | } | |
f1405f13 | 72 | fprintf(stderr,"\n"); |
723d9aa0 | 73 | } |
cdc711dc | 74 | |
cf55ed64 | 75 | |
be59993b | 76 | static 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 | |
104 | DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
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 | ||
110 | DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
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 |
ac9e3f59 | 136 | DPRINTF("hCard: %lu\n", cr->hCard); |
9c9fd67c | 137 | } |
da3ba95a | 138 | break; |
2c2119eb | 139 | |
da3ba95a | 140 | case USB_TRANSFER: |
f1405f13 | 141 | DPRINTF("in USB_TRANSFER"); |
da3ba95a | 142 | { |
143 | struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data); | |
144 | ||
723d9aa0 | 145 | #ifdef DEBUG |
f1405f13 | 146 | DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n", |
147 | ut->dwUniqueID, ut->dwPipeNum, ut->fRead, | |
148 | ut->dwOptions, ut->dwBufferSize, ut->dwTimeout); | |
cf55ed64 MG |
149 | if (ut->dwPipeNum == 0) { |
150 | DPRINTF("setup packet: "); | |
151 | hexdump(ut->SetupPacket, 8); | |
152 | } | |
723d9aa0 | 153 | |
9c9fd67c | 154 | if (!ut->fRead && ut->dwBufferSize) |
155 | { | |
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 |
6c235d59 | 163 | xpcu_transfer(xpcu, 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 | { | |
f1405f13 | 170 | DPRINTF("Read: "); |
9c9fd67c | 171 | hexdump(ut->pBuffer, ut->dwBytesTransferred); |
172 | } | |
723d9aa0 | 173 | #endif |
da3ba95a | 174 | } |
cdc711dc | 175 | break; |
2c2119eb | 176 | |
f152c048 | 177 | case INT_ENABLE_OLD: |
da3ba95a | 178 | case INT_ENABLE: |
f1405f13 | 179 | DPRINTF("INT_ENABLE\n"); |
da3ba95a | 180 | { |
9c9fd67c | 181 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
cdc711dc | 182 | |
f1405f13 | 183 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", |
184 | it->hInterrupt, it->dwOptions, | |
185 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
186 | it->dwLost, it->fStopped); | |
cdc711dc | 187 | |
9c9fd67c | 188 | it->fEnableOk = 1; |
9ba1e383 | 189 | it->fStopped = 0; |
ca18111b | 190 | ints_enabled = 1; |
533f4b68 | 191 | pthread_mutex_trylock(&int_wait); |
9c9fd67c | 192 | } |
cdc711dc | 193 | |
cdc711dc | 194 | break; |
9c9fd67c | 195 | |
da3ba95a | 196 | case INT_DISABLE: |
f1405f13 | 197 | DPRINTF("INT_DISABLE\n"); |
da3ba95a | 198 | { |
9c9fd67c | 199 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
da3ba95a | 200 | |
f1405f13 | 201 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", |
202 | it->hInterrupt, it->dwOptions, | |
203 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
204 | it->dwLost, it->fStopped); | |
795992ad | 205 | #ifndef NO_WINDRVR |
9c9fd67c | 206 | ret = (*ioctl_func) (fd, request, wdioctl); |
e71b6bf3 | 207 | #else |
208 | it->dwCounter = 0; | |
209 | it->fStopped = 1; | |
ca18111b | 210 | ints_enabled = 0; |
9ba1e383 | 211 | if (pthread_mutex_trylock(&int_wait) == EBUSY) |
212 | pthread_mutex_unlock(&int_wait); | |
292160ed | 213 | #endif |
f1405f13 | 214 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", |
215 | it->hInterrupt, it->dwOptions, | |
216 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
217 | it->dwLost, it->fStopped); | |
da3ba95a | 218 | } |
da3ba95a | 219 | break; |
da3ba95a | 220 | |
9c9fd67c | 221 | case USB_SET_INTERFACE: |
f1405f13 | 222 | DPRINTF("USB_SET_INTERFACE\n"); |
da3ba95a | 223 | { |
9c9fd67c | 224 | struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data); |
225 | ||
f1405f13 | 226 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", |
227 | usi->dwUniqueID, usi->dwInterfaceNum, | |
228 | usi->dwAlternateSetting, usi->dwOptions); | |
795992ad | 229 | #ifndef NO_WINDRVR |
9c9fd67c | 230 | ret = (*ioctl_func) (fd, request, wdioctl); |
2a7af812 | 231 | #else |
0c2db148 | 232 | xpcu_set_interface(xpcu, usi); |
292160ed | 233 | #endif |
f1405f13 | 234 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", |
235 | usi->dwUniqueID, usi->dwInterfaceNum, | |
236 | usi->dwAlternateSetting, usi->dwOptions); | |
da3ba95a | 237 | } |
cdc711dc | 238 | break; |
da3ba95a | 239 | |
f152c048 | 240 | case USB_GET_DEVICE_DATA_OLD: |
9c9fd67c | 241 | case USB_GET_DEVICE_DATA: |
f1405f13 | 242 | DPRINTF("USB_GET_DEVICE_DATA\n"); |
9c9fd67c | 243 | { |
244 | struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data); | |
245 | int pSize; | |
246 | ||
f1405f13 | 247 | DPRINTF("unique: %lu, bytes: %lu, options: %lx\n", |
248 | ugdd->dwUniqueID, ugdd->dwBytes, | |
249 | ugdd->dwOptions); | |
250 | ||
9c9fd67c | 251 | pSize = ugdd->dwBytes; |
e71b6bf3 | 252 | if (!ugdd->dwBytes) { |
cbfa0ac6 MG |
253 | if (xpcu->dev) { |
254 | ugdd->dwBytes = xpcu_deviceinfo(xpcu, NULL); | |
e71b6bf3 | 255 | } |
256 | } else { | |
cbfa0ac6 | 257 | xpcu_deviceinfo(xpcu, (unsigned char*)ugdd->pBuf); |
e71b6bf3 | 258 | } |
da3ba95a | 259 | } |
9c9fd67c | 260 | break; |
261 | ||
f152c048 | 262 | case EVENT_REGISTER_OLD: |
b0f621dd | 263 | case EVENT_REGISTER: |
f1405f13 | 264 | DPRINTF("EVENT_REGISTER\n"); |
b0f621dd | 265 | { |
266 | struct event *e = (struct event*)(wdheader->data); | |
ac22d975 | 267 | struct usb_bus *bus; |
3a2a22bc MG |
268 | char* devpos; |
269 | int busnum = -1, devnum = -1; | |
b0f621dd | 270 | int i; |
271 | ||
f1405f13 | 272 | DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", |
273 | e->handle, e->dwAction, | |
274 | e->dwStatus, e->dwEventId, e->dwCardType, | |
275 | e->hKernelPlugIn, e->dwOptions, | |
276 | e->u.Usb.deviceId.dwVendorId, | |
277 | e->u.Usb.deviceId.dwProductId, | |
278 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
279 | e->dwNumMatchTables); | |
280 | ||
3a2a22bc MG |
281 | devpos = getenv("XILINX_USB_DEV"); |
282 | if (devpos != NULL) { | |
283 | int j; | |
284 | char *devstr = NULL, *remainder; | |
285 | ||
286 | DPRINTF("XILINX_USB_DEV=%s\n", devpos); | |
287 | ||
288 | for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) { | |
289 | if (devpos[j] == ':') { | |
290 | devpos[j] = 0; | |
291 | devstr = &(devpos[j+1]); | |
292 | } | |
293 | } | |
d377df04 | 294 | |
3a2a22bc MG |
295 | if (devstr && strlen(devstr) > 0) { |
296 | busnum = strtol(devpos, &remainder, 10); | |
297 | if (devpos == remainder) { | |
298 | busnum = -1; | |
299 | } else { | |
300 | devnum = strtol(devstr, &remainder, 10); | |
301 | if (devstr == remainder) { | |
77683ab6 | 302 | busnum = -1; |
3a2a22bc MG |
303 | devnum = -1; |
304 | } else { | |
305 | fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n", | |
306 | busnum, devnum); | |
307 | } | |
308 | } | |
309 | } | |
310 | } | |
d377df04 | 311 | |
ac22d975 | 312 | for (i = 0; i < e->dwNumMatchTables; i++) { |
f1405f13 | 313 | |
314 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", | |
315 | e->matchTables[i].VendorId, | |
316 | e->matchTables[i].ProductId, | |
317 | e->matchTables[i].bDeviceClass, | |
318 | e->matchTables[i].bDeviceSubClass, | |
319 | e->matchTables[i].bInterfaceClass, | |
320 | e->matchTables[i].bInterfaceSubClass, | |
321 | e->matchTables[i].bInterfaceProtocol); | |
b0f621dd | 322 | |
cbfa0ac6 | 323 | for (bus = xpcu->busses; bus; bus = bus->next) { |
ac22d975 | 324 | struct usb_device *dev; |
325 | ||
3a2a22bc MG |
326 | if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum)) |
327 | continue; | |
328 | ||
ac22d975 | 329 | for (dev = bus->devices; dev; dev = dev->next) { |
330 | struct usb_device_descriptor *desc = &(dev->descriptor); | |
331 | ||
332 | if((desc->idVendor == e->matchTables[i].VendorId) && | |
333 | (desc->idProduct == e->matchTables[i].ProductId) && | |
334 | (desc->bDeviceClass == e->matchTables[i].bDeviceClass) && | |
d377df04 | 335 | (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) && |
a2a952c0 | 336 | ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) { |
2a7af812 | 337 | int ac; |
338 | for (ac = 0; ac < desc->bNumConfigurations; ac++) { | |
339 | struct usb_interface *interface = dev->config[ac].interface; | |
340 | int ai; | |
341 | ||
342 | for (ai = 0; ai < interface->num_altsetting; ai++) { | |
f1405f13 | 343 | |
344 | DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n", | |
345 | interface->altsetting[i].bInterfaceClass, | |
346 | interface->altsetting[i].bInterfaceSubClass, | |
347 | interface->altsetting[i].bInterfaceProtocol); | |
348 | ||
2a7af812 | 349 | if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) && |
350 | (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){ | |
351 | /* TODO: check interfaceClass! */ | |
f1405f13 | 352 | DPRINTF("found device with libusb\n"); |
cbfa0ac6 | 353 | xpcu->dev = dev; |
2a7af812 | 354 | card_type = e->dwCardType; |
355 | } | |
356 | } | |
357 | } | |
ac22d975 | 358 | } |
359 | } | |
360 | } | |
361 | } | |
362 | ||
795992ad | 363 | #ifndef NO_WINDRVR |
b0f621dd | 364 | ret = (*ioctl_func) (fd, request, wdioctl); |
2a7af812 | 365 | #else |
2a7af812 | 366 | e->handle++; |
292160ed | 367 | #endif |
b0f621dd | 368 | |
723d9aa0 | 369 | #ifdef DEBUG |
f1405f13 | 370 | DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", |
371 | e->handle, e->dwAction, | |
372 | e->dwStatus, e->dwEventId, e->dwCardType, | |
373 | e->hKernelPlugIn, e->dwOptions, | |
374 | e->u.Usb.deviceId.dwVendorId, | |
375 | e->u.Usb.deviceId.dwProductId, | |
376 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
377 | e->dwNumMatchTables); | |
378 | ||
b0f621dd | 379 | for (i = 0; i < e->dwNumMatchTables; i++) |
f1405f13 | 380 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", |
381 | e->matchTables[i].VendorId, | |
382 | e->matchTables[i].ProductId, | |
383 | e->matchTables[i].bDeviceClass, | |
384 | e->matchTables[i].bDeviceSubClass, | |
385 | e->matchTables[i].bInterfaceClass, | |
386 | e->matchTables[i].bInterfaceSubClass, | |
387 | e->matchTables[i].bInterfaceProtocol); | |
723d9aa0 | 388 | #endif |
b0f621dd | 389 | } |
390 | break; | |
391 | ||
f152c048 | 392 | case TRANSFER_OLD: |
da3ba95a | 393 | case TRANSFER: |
f1405f13 | 394 | DPRINTF("TRANSFER\n"); |
ac9e3f59 | 395 | { |
396 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
397 | ||
1dac5195 | 398 | #ifndef NO_WINDRVR |
399 | ret = (*ioctl_func) (fd, request, wdioctl); | |
400 | #else | |
25ba7a49 | 401 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1); |
1dac5195 | 402 | #endif |
ac9e3f59 | 403 | } |
9c9fd67c | 404 | break; |
405 | ||
54357994 | 406 | case MULTI_TRANSFER_OLD: |
576995a8 | 407 | case MULTI_TRANSFER: |
408 | DPRINTF("MULTI_TRANSFER\n"); | |
ac9e3f59 | 409 | { |
410 | WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data); | |
411 | unsigned long num = wdheader->size/sizeof(WD_TRANSFER); | |
1dac5195 | 412 | #ifndef NO_WINDRVR |
413 | ret = (*ioctl_func) (fd, request, wdioctl); | |
414 | #else | |
25ba7a49 | 415 | ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num); |
ac9e3f59 | 416 | #endif |
ac9e3f59 | 417 | } |
576995a8 | 418 | break; |
419 | ||
da3ba95a | 420 | case EVENT_UNREGISTER: |
f1405f13 | 421 | DPRINTF("EVENT_UNREGISTER\n"); |
795992ad | 422 | #ifndef NO_WINDRVR |
9c9fd67c | 423 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed | 424 | #endif |
9c9fd67c | 425 | break; |
426 | ||
da3ba95a | 427 | case INT_WAIT: |
f1405f13 | 428 | DPRINTF("INT_WAIT\n"); |
b0f621dd | 429 | { |
430 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
431 | ||
f1405f13 | 432 | DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", |
433 | it->hInterrupt, it->dwOptions, | |
434 | it->dwCmds, it->fEnableOk, it->dwCounter, | |
435 | it->dwLost, it->fStopped); | |
b0f621dd | 436 | |
795992ad | 437 | #ifndef NO_WINDRVR |
b0f621dd | 438 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed | 439 | #else |
cbfa0ac6 | 440 | if (xpcu->dev) { |
2a7af812 | 441 | if (it->dwCounter == 0) { |
442 | it->dwCounter = 1; | |
443 | } else { | |
533f4b68 | 444 | pthread_mutex_lock(&int_wait); |
be452175 | 445 | pthread_mutex_unlock(&int_wait); |
2a7af812 | 446 | } |
94038a57 | 447 | } else { |
533f4b68 | 448 | pthread_mutex_lock(&int_wait); |
be452175 | 449 | pthread_mutex_unlock(&int_wait); |
2a7af812 | 450 | } |
292160ed | 451 | #endif |
452 | ||
f1405f13 | 453 | DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", |
454 | it->hInterrupt, it->dwOptions, it->dwCmds, | |
455 | it->fEnableOk, it->dwCounter, it->dwLost, | |
456 | it->fStopped); | |
b0f621dd | 457 | } |
9c9fd67c | 458 | break; |
459 | ||
da3ba95a | 460 | case CARD_UNREGISTER: |
f1405f13 | 461 | DPRINTF("CARD_UNREGISTER\n"); |
54357994 | 462 | { |
463 | struct card_register* cr = (struct card_register*)(wdheader->data); | |
464 | ||
465 | DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n", | |
466 | (unsigned long)cr->Card.Item[0].I.IO.dwAddr, | |
467 | cr->Card.Item[0].I.IO.dwBytes, | |
468 | cr->Card.Item[0].I.IO.dwBar); | |
469 | ||
470 | DPRINTF("hCard: %lu\n", cr->hCard); | |
471 | ||
795992ad | 472 | #ifndef NO_WINDRVR |
54357994 | 473 | ret = (*ioctl_func) (fd, request, wdioctl); |
474 | #else | |
72ce448b MG |
475 | if (pport) |
476 | pport->close(cr->hCard); | |
477 | ||
478 | pport = NULL; | |
292160ed | 479 | #endif |
54357994 | 480 | } |
9c9fd67c | 481 | break; |
482 | ||
da3ba95a | 483 | case EVENT_PULL: |
f1405f13 | 484 | DPRINTF("EVENT_PULL\n"); |
b1831983 | 485 | { |
486 | struct event *e = (struct event*)(wdheader->data); | |
723d9aa0 | 487 | #ifdef DEBUG |
b1831983 | 488 | int i; |
489 | ||
f1405f13 | 490 | DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", |
491 | e->handle, e->dwAction, e->dwStatus, | |
492 | e->dwEventId, e->dwCardType, e->hKernelPlugIn, | |
493 | e->dwOptions, e->u.Usb.deviceId.dwVendorId, | |
494 | e->u.Usb.deviceId.dwProductId, | |
495 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
496 | e->dwNumMatchTables); | |
497 | ||
b1831983 | 498 | for (i = 0; i < e->dwNumMatchTables; i++) |
f1405f13 | 499 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", |
500 | e->matchTables[i].VendorId, | |
501 | e->matchTables[i].ProductId, | |
502 | e->matchTables[i].bDeviceClass, | |
503 | e->matchTables[i].bDeviceSubClass, | |
504 | e->matchTables[i].bInterfaceClass, | |
505 | e->matchTables[i].bInterfaceSubClass, | |
506 | e->matchTables[i].bInterfaceProtocol); | |
723d9aa0 | 507 | #endif |
b1831983 | 508 | |
795992ad | 509 | #ifndef NO_WINDRVR |
b1831983 | 510 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed | 511 | #else |
cbfa0ac6 MG |
512 | if (xpcu->dev) { |
513 | struct usb_interface *interface = xpcu->dev->config->interface; | |
292160ed | 514 | |
515 | e->dwCardType = card_type; | |
516 | e->dwAction = 1; | |
517 | e->dwEventId = 109; | |
ca18111b | 518 | e->u.Usb.dwUniqueID = 110; |
cbfa0ac6 MG |
519 | e->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor; |
520 | e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct; | |
521 | e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass; | |
522 | e->matchTables[0].bDeviceSubClass = xpcu->dev->descriptor.bDeviceSubClass; | |
292160ed | 523 | e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass; |
524 | e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass; | |
525 | e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol; | |
526 | } | |
527 | #endif | |
b1831983 | 528 | |
723d9aa0 | 529 | #ifdef DEBUG |
f1405f13 | 530 | DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", |
531 | e->handle, e->dwAction, e->dwStatus, | |
532 | e->dwEventId, e->dwCardType, e->hKernelPlugIn, | |
533 | e->dwOptions, e->u.Usb.deviceId.dwVendorId, | |
534 | e->u.Usb.deviceId.dwProductId, | |
535 | e->u.Usb.dwUniqueID, e->dwEventVer, | |
536 | e->dwNumMatchTables); | |
537 | ||
b1831983 | 538 | for (i = 0; i < e->dwNumMatchTables; i++) |
f1405f13 | 539 | DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", |
540 | e->matchTables[i].VendorId, | |
541 | e->matchTables[i].ProductId, | |
542 | e->matchTables[i].bDeviceClass, | |
543 | e->matchTables[i].bDeviceSubClass, | |
544 | e->matchTables[i].bInterfaceClass, | |
545 | e->matchTables[i].bInterfaceSubClass, | |
546 | e->matchTables[i].bInterfaceProtocol); | |
723d9aa0 | 547 | #endif |
ca18111b | 548 | |
b1831983 | 549 | } |
9c9fd67c | 550 | break; |
551 | ||
da3ba95a | 552 | default: |
292160ed | 553 | fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request); |
795992ad | 554 | #ifndef NO_WINDRVR |
9c9fd67c | 555 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed | 556 | #endif |
557 | break; | |
cdc711dc | 558 | } |
da3ba95a | 559 | |
9c9fd67c | 560 | return ret; |
cdc711dc | 561 | } |
562 | ||
54357994 | 563 | int ioctl(int fd, unsigned long int request, ...) { |
dbda1264 | 564 | va_list args; |
565 | void *argp; | |
566 | int ret; | |
567 | ||
568 | if (!ioctl_func) | |
f1405f13 | 569 | ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl"); |
dbda1264 | 570 | |
571 | va_start (args, request); | |
572 | argp = va_arg (args, void *); | |
573 | va_end (args); | |
574 | ||
332ced7a | 575 | if (fd == windrvrfd) |
dbda1264 | 576 | ret = do_wdioctl(fd, request, argp); |
577 | else | |
578 | ret = (*ioctl_func) (fd, request, argp); | |
579 | ||
580 | return ret; | |
581 | } | |
cdc711dc | 582 | |
be452175 | 583 | int open (const char *pathname, int flags, ...) { |
584 | static int (*func) (const char *, int, mode_t) = NULL; | |
cdc711dc | 585 | mode_t mode = 0; |
586 | va_list args; | |
587 | int fd; | |
588 | ||
589 | if (!func) | |
f1405f13 | 590 | func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open"); |
cdc711dc | 591 | |
592 | if (flags & O_CREAT) { | |
593 | va_start(args, flags); | |
594 | mode = va_arg(args, mode_t); | |
595 | va_end(args); | |
596 | } | |
597 | ||
cdc711dc | 598 | if (!strcmp (pathname, "/dev/windrvr6")) { |
f1405f13 | 599 | DPRINTF("opening windrvr6\n"); |
795992ad | 600 | #ifdef NO_WINDRVR |
01b99d52 | 601 | windrvrfd = fd = (*func) ("/dev/null", flags, mode); |
602 | #else | |
603 | windrvrfd = fd = (*func) (pathname, flags, mode); | |
604 | #endif | |
cbfa0ac6 MG |
605 | if (!xpcu) |
606 | xpcu = xpcu_open(); | |
723d9aa0 | 607 | |
608 | return fd; | |
cdc711dc | 609 | } |
610 | ||
723d9aa0 | 611 | return (*func) (pathname, flags, mode); |
cdc711dc | 612 | } |
613 | ||
723d9aa0 | 614 | int close(int fd) { |
615 | static int (*func) (int) = NULL; | |
cdc711dc | 616 | |
723d9aa0 | 617 | if (!func) |
f1405f13 | 618 | func = (int (*) (int)) dlsym(RTLD_NEXT, "close"); |
723d9aa0 | 619 | |
11d01742 | 620 | if (fd == windrvrfd && windrvrfd >= 0) { |
f1405f13 | 621 | DPRINTF("close windrvrfd\n"); |
010cbaa6 | 622 | |
6234190b MG |
623 | if (xpcu) |
624 | xpcu_close(xpcu); | |
010cbaa6 | 625 | |
cbfa0ac6 | 626 | xpcu = NULL; |
332ced7a | 627 | windrvrfd = -1; |
cdc711dc | 628 | } |
cdc711dc | 629 | |
723d9aa0 | 630 | return (*func) (fd); |
cdc711dc | 631 | } |
632 | ||
723d9aa0 | 633 | FILE *fopen(const char *path, const char *mode) { |
634 | FILE *ret; | |
635 | static FILE* (*func) (const char*, const char*) = NULL; | |
0dca330e | 636 | char buf[256]; |
637 | int i; | |
ca18111b | 638 | |
639 | if (!func) | |
f1405f13 | 640 | func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen"); |
ca18111b | 641 | |
3e670223 | 642 | for (i = 0; i < 4; i++) { |
643 | snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i); | |
644 | if (!strcmp(path, buf)) { | |
645 | DPRINTF("open base-addr of parport%d\n", i); | |
646 | if (config_is_real_pport(i)) { | |
647 | ret = (*func) (path, mode); | |
648 | } else { | |
649 | ret = (*func) ("/dev/null", mode); | |
650 | } | |
651 | ||
652 | if (ret) { | |
653 | baseaddrfp = ret; | |
654 | baseaddrnum = i; | |
655 | } | |
656 | ||
657 | return ret; | |
658 | } | |
659 | } | |
660 | ||
661 | ret = (*func) (path, mode); | |
ca18111b | 662 | |
0dca330e | 663 | if (!strcmp(path, "/proc/modules")) { |
f1405f13 | 664 | DPRINTF("opening /proc/modules\n"); |
723d9aa0 | 665 | #ifdef NO_WINDRVR |
dbda1264 | 666 | modulesfp = ret; |
723d9aa0 | 667 | modules_read = 0; |
668 | #endif | |
669 | } | |
cdc711dc | 670 | |
671 | return ret; | |
672 | } | |
673 | ||
dbda1264 | 674 | char *fgets(char *s, int size, FILE *stream) { |
675 | static char* (*func) (char*, int, FILE*) = NULL; | |
16f6b164 | 676 | const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"}; |
0dca330e | 677 | char buf[256]; |
dbda1264 | 678 | char *ret = NULL; |
679 | ||
cdc711dc | 680 | |
681 | if (!func) | |
f1405f13 | 682 | func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets"); |
723d9aa0 | 683 | |
dbda1264 | 684 | if (modulesfp == stream) { |
54357994 | 685 | if (modules_read < sizeof(modules) / sizeof(modules[0])) { |
16f6b164 | 686 | strcpy(s, modules[modules_read]); |
dbda1264 | 687 | ret = s; |
16f6b164 | 688 | modules_read++; |
dbda1264 | 689 | } |
0dca330e | 690 | } else if (baseaddrfp == stream) { |
691 | snprintf(s, sizeof(buf), "%d\t%d\n", | |
692 | (baseaddrnum) * 0x10, | |
693 | ((baseaddrnum) * 0x10) + 0x400); | |
694 | ret = s; | |
723d9aa0 | 695 | } else { |
dbda1264 | 696 | ret = (*func)(s,size,stream); |
723d9aa0 | 697 | } |
cdc711dc | 698 | |
699 | return ret; | |
700 | } | |
701 | ||
dbda1264 | 702 | int fclose(FILE *fp) { |
703 | static int (*func) (FILE*) = NULL; | |
cdc711dc | 704 | |
dbda1264 | 705 | if (!func) |
f1405f13 | 706 | func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose"); |
cdc711dc | 707 | |
dbda1264 | 708 | if (fp == modulesfp) { |
709 | modulesfp = NULL; | |
710 | } | |
0dca330e | 711 | |
712 | if (fp == baseaddrfp) { | |
713 | baseaddrfp = NULL; | |
714 | } | |
dbda1264 | 715 | |
716 | return (*func)(fp); | |
cdc711dc | 717 | } |
419f2c98 | 718 | |
719 | int access(const char *pathname, int mode) { | |
720 | static int (*func) (const char*, int); | |
721 | ||
722 | if (!func) | |
f1405f13 | 723 | func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access"); |
3eee002c | 724 | |
b8b756f5 | 725 | if (pathname && !strcmp(pathname, "/dev/windrvr6")) { |
419f2c98 | 726 | return 0; |
727 | } else { | |
728 | return (*func)(pathname, mode); | |
729 | } | |
730 | } | |
c42237a3 MG |
731 | |
732 | #if __WORDSIZE == 32 | |
733 | int uname (struct utsname *__name) { | |
734 | static int (*func) (struct utsname*); | |
735 | int ret; | |
736 | ||
737 | if (!func) | |
738 | func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname"); | |
739 | ||
740 | ret = (*func)(__name); | |
741 | ||
742 | if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) { | |
743 | strcpy(__name->machine, "i686"); | |
744 | } | |
745 | ||
746 | return ret; | |
747 | } | |
748 | #endif |