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