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