]> git.zerfleddert.de Git - usb-driver/blame - usb-driver.c
pass MULTI_TRANSFER to jtagkey_transfer, so it can be optimized there
[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>
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>
42#include <linux/parport.h>
43#include <linux/ppdev.h>
2c0c1255 44#include "usb-driver.h"
19b2e286 45#ifdef JTAGKEY
46#include "jtagkey.h"
47#endif
cdc711dc 48
9c9fd67c 49static int (*ioctl_func) (int, int, void *) = NULL;
332ced7a 50static int windrvrfd = -1;
54357994 51static int parportfd = -1;
54357994 52static unsigned long ppbase = 0;
53static unsigned long ecpbase = 0;
332ced7a 54FILE *modulesfp = NULL;
0dca330e 55FILE *baseaddrfp = NULL;
56int baseaddrnum = 0;
723d9aa0 57static int modules_read = 0;
f10480d1 58static struct usb_bus *busses = NULL;
2a7af812 59static struct usb_device *usbdevice;
60static usb_dev_handle *usb_devhandle = NULL;
010cbaa6 61static int usbinterface = -1;
b72b86b4 62static unsigned long card_type;
ca18111b 63static int ints_enabled = 0;
533f4b68 64static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
292160ed 65
795992ad 66#define NO_WINDRVR 1
723d9aa0 67
be452175 68#ifdef DEBUG
723d9aa0 69void hexdump(unsigned char *buf, int len) {
70 int i;
71
72 for(i=0; i<len; i++) {
73 fprintf(stderr,"%02x ", buf[i]);
74 if ((i % 16) == 15)
75 fprintf(stderr,"\n");
76 }
f1405f13 77 fprintf(stderr,"\n");
723d9aa0 78}
be452175 79#endif
cdc711dc 80
f95f1d2e 81int usb_deviceinfo(unsigned char *buf) {
82 int i,j,k,l;
83 int len = 0;
3664a3e3 84 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
85 WDU_INTERFACE **pActiveInterface;
f95f1d2e 86
87 if (buf) {
88 struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
89
90 udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR);
2a7af812 91 udi->Descriptor.bDescriptorType = usbdevice->descriptor.bDescriptorType;
92 udi->Descriptor.bcdUSB = usbdevice->descriptor.bcdUSB;
93 udi->Descriptor.bDeviceClass = usbdevice->descriptor.bDeviceClass;
94 udi->Descriptor.bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
95 udi->Descriptor.bDeviceProtocol = usbdevice->descriptor.bDeviceProtocol;
96 udi->Descriptor.bMaxPacketSize0 = usbdevice->descriptor.bMaxPacketSize0;
97 udi->Descriptor.idVendor = usbdevice->descriptor.idVendor;
98 udi->Descriptor.idProduct = usbdevice->descriptor.idProduct;
99 udi->Descriptor.bcdDevice = usbdevice->descriptor.bcdDevice;
100 udi->Descriptor.iManufacturer = usbdevice->descriptor.iManufacturer;
101 udi->Descriptor.iProduct = usbdevice->descriptor.iProduct;
102 udi->Descriptor.iSerialNumber = usbdevice->descriptor.iSerialNumber;
103 udi->Descriptor.bNumConfigurations = usbdevice->descriptor.bNumConfigurations;
f95f1d2e 104
105 /* TODO: Fix Pipe0! */
106 udi->Pipe0.dwNumber = 0x00;
2a7af812 107 udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0;
f95f1d2e 108 udi->Pipe0.type = 0;
8923df66 109 udi->Pipe0.direction = WDU_DIR_IN_OUT;
f95f1d2e 110 udi->Pipe0.dwInterval = 0;
3664a3e3 111
112 pConfigs = &(udi->pConfigs);
113 pActiveConfig = &(udi->pActiveConfig);
114 pActiveInterface = &(udi->pActiveInterface[0]);
f95f1d2e 115 }
116
117 len = sizeof(struct usb_device_info);
118
2a7af812 119 for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++)
f95f1d2e 120 {
2a7af812 121 struct usb_config_descriptor *conf_desc = &usbdevice->config[i];
3664a3e3 122 WDU_INTERFACE **pInterfaces;
123 WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces];
124 WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces];
125
f95f1d2e 126 if (buf) {
127 WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len);
128
3664a3e3 129 *pConfigs = cfg;
130 *pActiveConfig = cfg;
131
f95f1d2e 132 cfg->Descriptor.bLength = conf_desc->bLength;
133 cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType;
134 cfg->Descriptor.wTotalLength = conf_desc->wTotalLength;
135 cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces;
136 cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue;
137 cfg->Descriptor.iConfiguration = conf_desc->iConfiguration;
138 cfg->Descriptor.bmAttributes = conf_desc->bmAttributes;
139 cfg->Descriptor.MaxPower = conf_desc->MaxPower;
140
141 cfg->dwNumInterfaces = conf_desc->bNumInterfaces;
3664a3e3 142
143 pInterfaces = &(cfg->pInterfaces);
f95f1d2e 144 }
145 len += sizeof(WDU_CONFIGURATION);
3664a3e3 146
f95f1d2e 147 if (buf) {
3664a3e3 148 *pInterfaces = (WDU_INTERFACE*)(buf+len);
f95f1d2e 149 for (j=0; j<conf_desc->bNumInterfaces; j++) {
150 WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len);
3664a3e3 151
152 pActiveInterface[j] = iface;
153
154 pAlternateSettings[j] = &(iface->pAlternateSettings);
2a7af812 155 iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting;
3664a3e3 156 pActiveAltSetting[j] = &(iface->pActiveAltSetting);
f95f1d2e 157
158 len += sizeof(WDU_INTERFACE);
159 }
160 } else {
161 len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces;
162 }
163
164 for (j=0; j<conf_desc->bNumInterfaces; j++)
165 {
2a7af812 166 struct usb_interface *interface = &usbdevice->config[i].interface[j];
3664a3e3 167
168 if (buf) {
169 *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
170 /* FIXME: */
171 *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
172 }
173
f95f1d2e 174 for(k=0; k<interface->num_altsetting; k++)
175 {
3664a3e3 176 unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints;
177 WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors;
178 WDU_PIPE_INFO **pPipes;
179
f95f1d2e 180 if (buf) {
181 WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len);
182
183 altset->Descriptor.bLength = interface->altsetting[k].bLength;
184 altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType;
185 altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber;
186 altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting;
187 altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints;
188 altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass;
189 altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass;
190 altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol;
191 altset->Descriptor.iInterface = interface->altsetting[k].iInterface;
3664a3e3 192 pEndpointDescriptors = &(altset->pEndpointDescriptors);
193 pPipes = &(altset->pPipes);
f95f1d2e 194
195 }
196 len +=sizeof(WDU_ALTERNATE_SETTING);
197
198 if (buf) {
3664a3e3 199 *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
f95f1d2e 200 for (l = 0; l < bNumEndpoints; l++) {
201 WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
f95f1d2e 202
203 ed->bLength = interface->altsetting[k].endpoint[l].bLength;
204 ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType;
205 ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress;
206 ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes;
207 ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize;
208 ed->bInterval = interface->altsetting[k].endpoint[l].bInterval;
209
210 len += sizeof(WDU_ENDPOINT_DESCRIPTOR);
792bf5f2 211 }
f95f1d2e 212
3664a3e3 213 *pPipes = (WDU_PIPE_INFO*)(buf+len);
792bf5f2 214 for (l = 0; l < bNumEndpoints; l++) {
215 WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len);
f95f1d2e 216
217 pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress;
218 pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize);
219 pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK;
220 if (pi->type == PIPE_TYPE_CONTROL)
221 pi->direction = WDU_DIR_IN_OUT;
222 else
223 {
224 pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT;
225 }
226
227 pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval;
228
229 len += sizeof(WDU_PIPE_INFO);
230 }
231 } else {
232 len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints;
233 }
234 }
235 }
236 }
237
238 return len;
239}
240
1dac5195 241int pp_transfer(WD_TRANSFER *tr, int fd, unsigned int request) {
ac9e3f59 242 int ret = 0;
28e7c368 243 unsigned long port = (unsigned long)tr->dwPort;
54357994 244 unsigned char val;
19b2e286 245 static unsigned char last_pp_write = 0;
246
ac9e3f59 247 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
248 (unsigned long)tr->dwPort, tr->cmdTrans, tr->dwBytes,
249 tr->fAutoinc, tr->dwOptions);
54357994 250
251 val = tr->Data.Byte;
ac9e3f59 252
253#ifdef DEBUG
254 if (tr->cmdTrans == 13)
54357994 255 DPRINTF("write byte: %d\n", val);
ac9e3f59 256#endif
257
54357994 258 if (parportfd < 0)
259 return ret;
260
28e7c368 261 if (port == ppbase + PP_DATA) {
262 DPRINTF("data port\n");
263 switch(tr->cmdTrans) {
264 case PP_READ:
265 ret = 0; /* We don't support reading of the data port */
266 break;
267
268 case PP_WRITE:
269 ret = ioctl(parportfd, PPWDATA, &val);
cdc9c5bf 270 last_pp_write = val;
28e7c368 271 break;
272
273 default:
274 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
275 ret = -1;
276 break;
277 }
278 } else if (port == ppbase + PP_STATUS) {
cdc9c5bf 279 DPRINTF("status port (last write: %d)\n", last_pp_write);
28e7c368 280 switch(tr->cmdTrans) {
281 case PP_READ:
282 ret = ioctl(parportfd, PPRSTATUS, &val);
05e1bfcd 283#ifdef FORCE_PC3_IDENT
19b2e286 284 val &= 0x5f;
285 if (last_pp_write & 0x40)
286 val |= 0x20;
cdc9c5bf 287 else
19b2e286 288 val |= 0x80;
cdc9c5bf 289#endif
28e7c368 290 break;
291
292 case PP_WRITE:
293 ret = 0; /* Status Port is readonly */
294 break;
295
296 default:
297 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
298 ret = -1;
299 break;
300 }
301 } else if (port == ppbase + PP_CONTROL) {
302 DPRINTF("control port\n");
303 switch(tr->cmdTrans) {
304 case PP_READ:
305 ret = ioctl(parportfd, PPRCONTROL, &val);
306 break;
307
308 case PP_WRITE:
309 ret = ioctl(parportfd, PPWCONTROL, &val);
310 break;
311
312 default:
313 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
314 ret = -1;
315 break;
316 }
54fc4508 317 } else if ((port == ecpbase + PP_ECP_CFGA) && ecpbase) {
318 DPRINTF("ECP_CFGA port\n");
319 } else if ((port == ecpbase + PP_ECP_CFGB) && ecpbase) {
320 DPRINTF("ECP_CFGB port\n");
321 } else if ((port == ecpbase + PP_ECP_ECR) && ecpbase) {
322 DPRINTF("ECP_ECR port\n");
28e7c368 323 } else {
54fc4508 324 DPRINTF("access to unsupported address range!\n");
28e7c368 325 ret = 0;
54357994 326 }
327
328 tr->Data.Byte = val;
ac9e3f59 329
330 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
331 (unsigned long)tr->dwPort, tr->cmdTrans, tr->dwBytes,
332 tr->fAutoinc, tr->dwOptions);
333#ifdef DEBUG
334 if (tr->cmdTrans == 10)
335 DPRINTF("read byte: %d\n", tr->Data.Byte);
336#endif
337
338 return ret;
339}
340
9c9fd67c 341int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 342 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 343 struct version_struct *version;
344 int ret = 0;
cdc711dc 345
da3ba95a 346 if (wdheader->magic != MAGIC) {
2c2119eb 347 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
348 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 349 }
350
bdc65937 351 switch(request & ~(0xc0000000)) {
da3ba95a 352 case VERSION:
9c9fd67c 353 version = (struct version_struct*)(wdheader->data);
1dac5195 354 strcpy(version->version, "libusb-driver.so $Revision: 1.63 $");
90831fba 355 version->versionul = 802;
f1405f13 356 DPRINTF("VERSION\n");
cdc711dc 357 break;
2c2119eb 358
359 case LICENSE:
f1405f13 360 DPRINTF("LICENSE\n");
2c2119eb 361 break;
362
f152c048 363 case CARD_REGISTER_OLD:
9c9fd67c 364 case CARD_REGISTER:
54357994 365 DPRINTF("CARD_REGISTER\n");
9c9fd67c 366 {
533f4b68 367 struct card_register* cr = (struct card_register*)(wdheader->data);
54357994 368 char ppdev[32];
369
370 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
371 cr->Card.dwItems,
372 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
373 cr->Card.Item[0].I.IO.dwBytes,
374 cr->Card.Item[0].I.IO.dwBar);
375
376 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
377 cr->Card.dwItems,
378 (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
379 cr->Card.Item[1].I.IO.dwBytes,
380 cr->Card.Item[1].I.IO.dwBar);
576995a8 381#ifndef NO_WINDRVR
382 ret = (*ioctl_func) (fd, request, wdioctl);
ac9e3f59 383#else
19b2e286 384
385 /* FIXME: Ugly hack which maps amontec JtagKey to 4. parallel port */
386#ifdef JTAGKEY
bccac33b 387 if ((unsigned long)cr->Card.Item[0].I.IO.dwAddr == 0x30) {
388 ret=jtagkey_init(0x0403, 0xcff8); /* I need a config file... */
389 cr->hCard = 0xff;
390 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
391 if (ret < 0)
392 cr->hCard = 0;
393
394 break;
395 }
19b2e286 396#endif
bccac33b 397 if (parportfd < 0) {
398 snprintf(ppdev, sizeof(ppdev), "/dev/parport%lu",
399 (unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
400 DPRINTF("opening %s\n", ppdev);
401 parportfd = open(ppdev, O_RDWR|O_EXCL);
402
403 if (parportfd < 0)
404 fprintf(stderr,"Can't open %s: %s\n", ppdev, strerror(errno));
405 }
19b2e286 406
bccac33b 407 if (parportfd >= 0) {
408 int pmode;
19b2e286 409
bccac33b 410 if (ioctl(parportfd, PPCLAIM) == -1)
411 return ret;
54357994 412
bccac33b 413 ecpbase = 0;
414 pmode = IEEE1284_MODE_COMPAT;
415 if (ioctl(parportfd, PPNEGOT, &pmode) == -1)
416 return ret;
19b2e286 417
bccac33b 418 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr) {
419 DPRINTF("ECP mode requested\n");
420 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
421 /* TODO: Implement ECP mode */
54357994 422#if 0
bccac33b 423 pmode = IEEE1284_MODE_ECP;
54357994 424
bccac33b 425 if (ioctl(parportfd, PPNEGOT, &pmode) == -1) {
426 ecpbase = 0;
427 pmode = IEEE1284_MODE_COMPAT;
428 if (ioctl(parportfd, PPNEGOT, &pmode) == -1)
429 return ret;
430 }
19b2e286 431#endif
bccac33b 432 }
433
434 cr->hCard = parportfd;
54357994 435 }
19b2e286 436
437 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
438 if (ret < 0)
439 cr->hCard = 0;
576995a8 440#endif
ac9e3f59 441 DPRINTF("hCard: %lu\n", cr->hCard);
9c9fd67c 442 }
da3ba95a 443 break;
2c2119eb 444
da3ba95a 445 case USB_TRANSFER:
f1405f13 446 DPRINTF("in USB_TRANSFER");
da3ba95a 447 {
448 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
449
723d9aa0 450#ifdef DEBUG
f1405f13 451 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
452 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
453 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
454 DPRINTF("setup packet: ");
9c9fd67c 455 hexdump(ut->SetupPacket, 8);
723d9aa0 456
9c9fd67c 457 if (!ut->fRead && ut->dwBufferSize)
458 {
459 hexdump(ut->pBuffer, ut->dwBufferSize);
9c9fd67c 460 }
723d9aa0 461#endif
9c9fd67c 462
795992ad 463#ifndef NO_WINDRVR
9c9fd67c 464 ret = (*ioctl_func) (fd, request, wdioctl);
411af373 465#else
466 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
a70f4f38 467 if (ut->dwPipeNum == 0) { /* control pipe */
468 int requesttype, request, value, index, size;
469 requesttype = ut->SetupPacket[0];
470 request = ut->SetupPacket[1];
471 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
472 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
473 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
f1405f13 474 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
a70f4f38 475 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
476 } else {
477 if (ut->fRead) {
478 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
479
480 } else {
481 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
482 }
483 }
484
411af373 485 if (ret < 0) {
a70f4f38 486 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
411af373 487 } else {
488 ut->dwBytesTransferred = ret;
489 ret = 0;
490 }
292160ed 491#endif
9c9fd67c 492
723d9aa0 493#ifdef DEBUG
f1405f13 494 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 495 if (ut->fRead && ut->dwBytesTransferred)
496 {
f1405f13 497 DPRINTF("Read: ");
9c9fd67c 498 hexdump(ut->pBuffer, ut->dwBytesTransferred);
499 }
723d9aa0 500#endif
da3ba95a 501 }
cdc711dc 502 break;
2c2119eb 503
f152c048 504 case INT_ENABLE_OLD:
da3ba95a 505 case INT_ENABLE:
f1405f13 506 DPRINTF("INT_ENABLE\n");
da3ba95a 507 {
9c9fd67c 508 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 509
f1405f13 510 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
511 it->hInterrupt, it->dwOptions,
512 it->dwCmds, it->fEnableOk, it->dwCounter,
513 it->dwLost, it->fStopped);
cdc711dc 514
9c9fd67c 515 it->fEnableOk = 1;
9ba1e383 516 it->fStopped = 0;
ca18111b 517 ints_enabled = 1;
533f4b68 518 pthread_mutex_trylock(&int_wait);
9c9fd67c 519 }
cdc711dc 520
cdc711dc 521 break;
9c9fd67c 522
da3ba95a 523 case INT_DISABLE:
f1405f13 524 DPRINTF("INT_DISABLE\n");
da3ba95a 525 {
9c9fd67c 526 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 527
f1405f13 528 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
529 it->hInterrupt, it->dwOptions,
530 it->dwCmds, it->fEnableOk, it->dwCounter,
531 it->dwLost, it->fStopped);
795992ad 532#ifndef NO_WINDRVR
9c9fd67c 533 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 534#else
535 it->dwCounter = 0;
536 it->fStopped = 1;
ca18111b 537 ints_enabled = 0;
9ba1e383 538 if (pthread_mutex_trylock(&int_wait) == EBUSY)
539 pthread_mutex_unlock(&int_wait);
292160ed 540#endif
f1405f13 541 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
542 it->hInterrupt, it->dwOptions,
543 it->dwCmds, it->fEnableOk, it->dwCounter,
544 it->dwLost, it->fStopped);
da3ba95a 545 }
da3ba95a 546 break;
da3ba95a 547
9c9fd67c 548 case USB_SET_INTERFACE:
f1405f13 549 DPRINTF("USB_SET_INTERFACE\n");
da3ba95a 550 {
9c9fd67c 551 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
552
f1405f13 553 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
554 usi->dwUniqueID, usi->dwInterfaceNum,
555 usi->dwAlternateSetting, usi->dwOptions);
795992ad 556#ifndef NO_WINDRVR
9c9fd67c 557 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 558#else
559 if (usbdevice) {
2a7af812 560 if (!usb_devhandle)
561 usb_devhandle = usb_open(usbdevice);
d0676964 562
563 /* FIXME: Select right interface! */
564 ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
565 if (!ret) {
411af373 566 if(!ret) {
010cbaa6 567 usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
411af373 568 ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
569 if (ret)
570 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
571 } else {
572 fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
573 }
d0676964 574 } else {
f1405f13 575 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
576 usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber,
577 ret, usb_strerror());
d0676964 578 }
2a7af812 579 }
292160ed 580#endif
f1405f13 581 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
582 usi->dwUniqueID, usi->dwInterfaceNum,
583 usi->dwAlternateSetting, usi->dwOptions);
da3ba95a 584 }
cdc711dc 585 break;
da3ba95a 586
f152c048 587 case USB_GET_DEVICE_DATA_OLD:
9c9fd67c 588 case USB_GET_DEVICE_DATA:
f1405f13 589 DPRINTF("USB_GET_DEVICE_DATA\n");
9c9fd67c 590 {
591 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
592 int pSize;
593
f1405f13 594 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
595 ugdd->dwUniqueID, ugdd->dwBytes,
596 ugdd->dwOptions);
597
9c9fd67c 598 pSize = ugdd->dwBytes;
e71b6bf3 599 if (!ugdd->dwBytes) {
2a7af812 600 if (usbdevice) {
f95f1d2e 601 ugdd->dwBytes = usb_deviceinfo(NULL);
e71b6bf3 602 }
603 } else {
f95f1d2e 604 usb_deviceinfo((unsigned char*)ugdd->pBuf);
e71b6bf3 605 }
da3ba95a 606 }
9c9fd67c 607 break;
608
f152c048 609 case EVENT_REGISTER_OLD:
b0f621dd 610 case EVENT_REGISTER:
f1405f13 611 DPRINTF("EVENT_REGISTER\n");
b0f621dd 612 {
613 struct event *e = (struct event*)(wdheader->data);
ac22d975 614 struct usb_bus *bus;
b0f621dd 615 int i;
616
f1405f13 617 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",
618 e->handle, e->dwAction,
619 e->dwStatus, e->dwEventId, e->dwCardType,
620 e->hKernelPlugIn, e->dwOptions,
621 e->u.Usb.deviceId.dwVendorId,
622 e->u.Usb.deviceId.dwProductId,
623 e->u.Usb.dwUniqueID, e->dwEventVer,
624 e->dwNumMatchTables);
625
ac22d975 626 for (i = 0; i < e->dwNumMatchTables; i++) {
f1405f13 627
628 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
629 e->matchTables[i].VendorId,
630 e->matchTables[i].ProductId,
631 e->matchTables[i].bDeviceClass,
632 e->matchTables[i].bDeviceSubClass,
633 e->matchTables[i].bInterfaceClass,
634 e->matchTables[i].bInterfaceSubClass,
635 e->matchTables[i].bInterfaceProtocol);
b0f621dd 636
ac22d975 637 for (bus = busses; bus; bus = bus->next) {
638 struct usb_device *dev;
639
640 for (dev = bus->devices; dev; dev = dev->next) {
641 struct usb_device_descriptor *desc = &(dev->descriptor);
642
643 if((desc->idVendor == e->matchTables[i].VendorId) &&
644 (desc->idProduct == e->matchTables[i].ProductId) &&
645 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
646 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
2a7af812 647 int ac;
648 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
649 struct usb_interface *interface = dev->config[ac].interface;
650 int ai;
651
652 for (ai = 0; ai < interface->num_altsetting; ai++) {
f1405f13 653
654 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
655 interface->altsetting[i].bInterfaceClass,
656 interface->altsetting[i].bInterfaceSubClass,
657 interface->altsetting[i].bInterfaceProtocol);
658
2a7af812 659 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
660 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
661 /* TODO: check interfaceClass! */
f1405f13 662 DPRINTF("found device with libusb\n");
2a7af812 663 usbdevice = dev;
664 card_type = e->dwCardType;
665 }
666 }
667 }
ac22d975 668 }
669 }
670 }
671 }
672
795992ad 673#ifndef NO_WINDRVR
b0f621dd 674 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 675#else
2a7af812 676 e->handle++;
292160ed 677#endif
b0f621dd 678
723d9aa0 679#ifdef DEBUG
f1405f13 680 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",
681 e->handle, e->dwAction,
682 e->dwStatus, e->dwEventId, e->dwCardType,
683 e->hKernelPlugIn, e->dwOptions,
684 e->u.Usb.deviceId.dwVendorId,
685 e->u.Usb.deviceId.dwProductId,
686 e->u.Usb.dwUniqueID, e->dwEventVer,
687 e->dwNumMatchTables);
688
b0f621dd 689 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 690 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
691 e->matchTables[i].VendorId,
692 e->matchTables[i].ProductId,
693 e->matchTables[i].bDeviceClass,
694 e->matchTables[i].bDeviceSubClass,
695 e->matchTables[i].bInterfaceClass,
696 e->matchTables[i].bInterfaceSubClass,
697 e->matchTables[i].bInterfaceProtocol);
723d9aa0 698#endif
b0f621dd 699 }
700 break;
701
f152c048 702 case TRANSFER_OLD:
da3ba95a 703 case TRANSFER:
f1405f13 704 DPRINTF("TRANSFER\n");
ac9e3f59 705 {
706 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
707
1dac5195 708#ifndef NO_WINDRVR
709 ret = (*ioctl_func) (fd, request, wdioctl);
710#else
711
712#ifdef JTAGKEY
713 if (ppbase == 0x30) {
714 ret = jtagkey_transfer(tr, fd, request, ppbase, ecpbase, 1);
715 break;
716 }
717#endif /* JTAGKEY */
718 ret = pp_transfer(tr, fd, request);
719#endif
ac9e3f59 720 }
9c9fd67c 721 break;
722
54357994 723 case MULTI_TRANSFER_OLD:
576995a8 724 case MULTI_TRANSFER:
725 DPRINTF("MULTI_TRANSFER\n");
ac9e3f59 726 {
727 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
728 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
729 int i;
1dac5195 730#ifndef NO_WINDRVR
731 ret = (*ioctl_func) (fd, request, wdioctl);
732#else
733
734#ifdef JTAGKEY
735 /* FIXME: Config file and mor intelligent mapping! */
736 if (ppbase == 0x30) {
737 ret = jtagkey_transfer(tr, fd, request, ppbase, ecpbase, num);
738 break;
739 }
740#endif /* JTAGKEY */
ac9e3f59 741
ac9e3f59 742 for (i = 0; i < num; i++) {
743 DPRINTF("Transfer %d:\n", i+1);
1dac5195 744 ret = pp_transfer(tr + i, fd, request);
ac9e3f59 745 }
ac9e3f59 746#endif
ac9e3f59 747 }
576995a8 748 break;
749
da3ba95a 750 case EVENT_UNREGISTER:
f1405f13 751 DPRINTF("EVENT_UNREGISTER\n");
795992ad 752#ifndef NO_WINDRVR
9c9fd67c 753 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 754#endif
9c9fd67c 755 break;
756
da3ba95a 757 case INT_WAIT:
f1405f13 758 DPRINTF("INT_WAIT\n");
b0f621dd 759 {
760 struct interrupt *it = (struct interrupt*)(wdheader->data);
761
f1405f13 762 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
763 it->hInterrupt, it->dwOptions,
764 it->dwCmds, it->fEnableOk, it->dwCounter,
765 it->dwLost, it->fStopped);
b0f621dd 766
795992ad 767#ifndef NO_WINDRVR
b0f621dd 768 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 769#else
2a7af812 770 if (usbdevice) {
771 if (it->dwCounter == 0) {
772 it->dwCounter = 1;
773 } else {
533f4b68 774 pthread_mutex_lock(&int_wait);
be452175 775 pthread_mutex_unlock(&int_wait);
2a7af812 776 }
94038a57 777 } else {
533f4b68 778 pthread_mutex_lock(&int_wait);
be452175 779 pthread_mutex_unlock(&int_wait);
2a7af812 780 }
292160ed 781#endif
782
f1405f13 783 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
784 it->hInterrupt, it->dwOptions, it->dwCmds,
785 it->fEnableOk, it->dwCounter, it->dwLost,
786 it->fStopped);
b0f621dd 787 }
9c9fd67c 788 break;
789
da3ba95a 790 case CARD_UNREGISTER:
f1405f13 791 DPRINTF("CARD_UNREGISTER\n");
54357994 792 {
793 struct card_register* cr = (struct card_register*)(wdheader->data);
794
795 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
796 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
797 cr->Card.Item[0].I.IO.dwBytes,
798 cr->Card.Item[0].I.IO.dwBar);
799
800 DPRINTF("hCard: %lu\n", cr->hCard);
801
795992ad 802#ifndef NO_WINDRVR
54357994 803 ret = (*ioctl_func) (fd, request, wdioctl);
804#else
805 if (parportfd == cr->hCard && parportfd >= 0) {
806 ioctl(parportfd, PPRELEASE);
807 close(parportfd);
808 parportfd = -1;
54357994 809 }
292160ed 810#endif
54357994 811 }
9c9fd67c 812 break;
813
da3ba95a 814 case EVENT_PULL:
f1405f13 815 DPRINTF("EVENT_PULL\n");
b1831983 816 {
817 struct event *e = (struct event*)(wdheader->data);
723d9aa0 818#ifdef DEBUG
b1831983 819 int i;
820
f1405f13 821 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",
822 e->handle, e->dwAction, e->dwStatus,
823 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
824 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
825 e->u.Usb.deviceId.dwProductId,
826 e->u.Usb.dwUniqueID, e->dwEventVer,
827 e->dwNumMatchTables);
828
b1831983 829 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 830 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
831 e->matchTables[i].VendorId,
832 e->matchTables[i].ProductId,
833 e->matchTables[i].bDeviceClass,
834 e->matchTables[i].bDeviceSubClass,
835 e->matchTables[i].bInterfaceClass,
836 e->matchTables[i].bInterfaceSubClass,
837 e->matchTables[i].bInterfaceProtocol);
723d9aa0 838#endif
b1831983 839
795992ad 840#ifndef NO_WINDRVR
b1831983 841 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 842#else
2a7af812 843 if (usbdevice) {
844 struct usb_interface *interface = usbdevice->config->interface;
292160ed 845
846 e->dwCardType = card_type;
847 e->dwAction = 1;
848 e->dwEventId = 109;
ca18111b 849 e->u.Usb.dwUniqueID = 110;
2a7af812 850 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
851 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
852 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
853 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
292160ed 854 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
855 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
856 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
857 }
858#endif
b1831983 859
723d9aa0 860#ifdef DEBUG
f1405f13 861 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",
862 e->handle, e->dwAction, e->dwStatus,
863 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
864 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
865 e->u.Usb.deviceId.dwProductId,
866 e->u.Usb.dwUniqueID, e->dwEventVer,
867 e->dwNumMatchTables);
868
b1831983 869 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 870 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
871 e->matchTables[i].VendorId,
872 e->matchTables[i].ProductId,
873 e->matchTables[i].bDeviceClass,
874 e->matchTables[i].bDeviceSubClass,
875 e->matchTables[i].bInterfaceClass,
876 e->matchTables[i].bInterfaceSubClass,
877 e->matchTables[i].bInterfaceProtocol);
723d9aa0 878#endif
ca18111b 879
b1831983 880 }
9c9fd67c 881 break;
882
da3ba95a 883 default:
292160ed 884 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
795992ad 885#ifndef NO_WINDRVR
9c9fd67c 886 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 887#endif
888 break;
cdc711dc 889 }
da3ba95a 890
9c9fd67c 891 return ret;
cdc711dc 892}
893
54357994 894int ioctl(int fd, unsigned long int request, ...) {
dbda1264 895 va_list args;
896 void *argp;
897 int ret;
898
899 if (!ioctl_func)
f1405f13 900 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
dbda1264 901
902 va_start (args, request);
903 argp = va_arg (args, void *);
904 va_end (args);
905
332ced7a 906 if (fd == windrvrfd)
dbda1264 907 ret = do_wdioctl(fd, request, argp);
908 else
909 ret = (*ioctl_func) (fd, request, argp);
910
911 return ret;
912}
cdc711dc 913
be452175 914int open (const char *pathname, int flags, ...) {
915 static int (*func) (const char *, int, mode_t) = NULL;
cdc711dc 916 mode_t mode = 0;
917 va_list args;
918 int fd;
919
920 if (!func)
f1405f13 921 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
cdc711dc 922
923 if (flags & O_CREAT) {
924 va_start(args, flags);
925 mode = va_arg(args, mode_t);
926 va_end(args);
927 }
928
cdc711dc 929 if (!strcmp (pathname, "/dev/windrvr6")) {
f1405f13 930 DPRINTF("opening windrvr6\n");
795992ad 931#ifdef NO_WINDRVR
01b99d52 932 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
933#else
934 windrvrfd = fd = (*func) (pathname, flags, mode);
935#endif
f10480d1 936 if (!busses) {
937 usb_init();
938 usb_find_busses();
939 usb_find_devices();
940
941 busses = usb_get_busses();
942 }
723d9aa0 943
944 return fd;
cdc711dc 945 }
946
723d9aa0 947 return (*func) (pathname, flags, mode);
cdc711dc 948}
949
723d9aa0 950int close(int fd) {
951 static int (*func) (int) = NULL;
cdc711dc 952
723d9aa0 953 if (!func)
f1405f13 954 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
723d9aa0 955
11d01742 956 if (fd == windrvrfd && windrvrfd >= 0) {
f1405f13 957 DPRINTF("close windrvrfd\n");
010cbaa6 958 if (usbinterface >= 0)
959 usb_release_interface(usb_devhandle, usbinterface);
960
961 if (usb_devhandle)
962 usb_close(usb_devhandle);
963
964 usb_devhandle = NULL;
965 usbinterface = -1;
332ced7a 966 windrvrfd = -1;
cdc711dc 967 }
cdc711dc 968
723d9aa0 969 return (*func) (fd);
cdc711dc 970}
971
723d9aa0 972FILE *fopen(const char *path, const char *mode) {
973 FILE *ret;
974 static FILE* (*func) (const char*, const char*) = NULL;
0dca330e 975 char buf[256];
976 int i;
ca18111b 977
978 if (!func)
f1405f13 979 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
ca18111b 980
19b2e286 981#ifdef JTAGKEY
982 /* FIXME: Hack for parport mapping */
983 if (!strcmp(path, "/proc/sys/dev/parport/parport3/base-addr")) {
984 ret = (*func) ("/dev/null", mode);
985 } else
986#endif
987 ret = (*func) (path, mode);
ca18111b 988
0dca330e 989 if (!strcmp(path, "/proc/modules")) {
f1405f13 990 DPRINTF("opening /proc/modules\n");
723d9aa0 991#ifdef NO_WINDRVR
dbda1264 992 modulesfp = ret;
723d9aa0 993 modules_read = 0;
994#endif
995 }
0dca330e 996
997 if (ret) {
998 for (i = 0; i < 4; i++) {
999 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
1000 if (!strcmp(path, buf)) {
1001 DPRINTF("open base-addr of parport%d\n", i);
1002 baseaddrfp = ret;
1003 baseaddrnum = i;
1004 }
1005 }
1006 }
cdc711dc 1007
1008 return ret;
1009}
1010
dbda1264 1011char *fgets(char *s, int size, FILE *stream) {
1012 static char* (*func) (char*, int, FILE*) = NULL;
16f6b164 1013 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
0dca330e 1014 char buf[256];
dbda1264 1015 char *ret = NULL;
1016
cdc711dc 1017
1018 if (!func)
f1405f13 1019 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
723d9aa0 1020
dbda1264 1021 if (modulesfp == stream) {
54357994 1022 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
16f6b164 1023 strcpy(s, modules[modules_read]);
dbda1264 1024 ret = s;
16f6b164 1025 modules_read++;
dbda1264 1026 }
0dca330e 1027 } else if (baseaddrfp == stream) {
1028 snprintf(s, sizeof(buf), "%d\t%d\n",
1029 (baseaddrnum) * 0x10,
1030 ((baseaddrnum) * 0x10) + 0x400);
1031 ret = s;
723d9aa0 1032 } else {
dbda1264 1033 ret = (*func)(s,size,stream);
723d9aa0 1034 }
cdc711dc 1035
1036 return ret;
1037}
1038
dbda1264 1039int fclose(FILE *fp) {
1040 static int (*func) (FILE*) = NULL;
cdc711dc 1041
dbda1264 1042 if (!func)
f1405f13 1043 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
cdc711dc 1044
dbda1264 1045 if (fp == modulesfp) {
1046 modulesfp = NULL;
1047 }
0dca330e 1048
1049 if (fp == baseaddrfp) {
1050 baseaddrfp = NULL;
1051 }
dbda1264 1052
1053 return (*func)(fp);
cdc711dc 1054}
419f2c98 1055
1056int access(const char *pathname, int mode) {
1057 static int (*func) (const char*, int);
1058
1059 if (!func)
f1405f13 1060 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
419f2c98 1061
1062 if (!strcmp(pathname, "/dev/windrvr6")) {
1063 return 0;
1064 } else {
1065 return (*func)(pathname, mode);
1066 }
1067}
Impressum, Datenschutz