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