]> cvs.zerfleddert.de Git - usb-driver/blame - xpcu.c
fix prototype for _Z14isModuleLoadedPci
[usb-driver] / xpcu.c
CommitLineData
cbfa0ac6
MG
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
f92c0fbc 4#include <string.h>
cbfa0ac6
MG
5#include <strings.h>
6#include <usb.h>
f92c0fbc 7#include <errno.h>
501f1c21 8#include <pthread.h>
787300f6 9#include <stdint.h>
cbfa0ac6 10#include "usb-driver.h"
6c235d59 11#include "xpcu.h"
cbfa0ac6 12
19acdb82
MG
13struct xpcu_s {
14 struct usb_device *dev;
15 usb_dev_handle *handle;
16 int interface;
17 int alternate;
18 unsigned long card_type;
9bfaca62
MG
19};
20
21struct xpcu_event_s {
22 struct xpcu_s *xpcu;
23 int count;
24 int interrupt_count;
19acdb82
MG
25 pthread_mutex_t interrupt;
26};
27
f92c0fbc
MG
28static struct usb_bus *busses = NULL;
29
19acdb82
MG
30int xpcu_deviceinfo(struct usb_get_device_data *ugdd) {
31 struct xpcu_s *xpcu = (struct xpcu_s*)ugdd->dwUniqueID;
cbfa0ac6
MG
32 int i,j,k,l;
33 int len = 0;
f92c0fbc 34 unsigned char *buf = NULL;
cbfa0ac6
MG
35 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
36 WDU_INTERFACE **pActiveInterface;
37
8af4d910
MG
38 if (!xpcu)
39 return -ENODEV;
f92c0fbc
MG
40
41 if (ugdd->dwBytes)
42 buf = ugdd->pBuf;
43
cbfa0ac6
MG
44 if (buf) {
45 struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
46
47 udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR);
48 udi->Descriptor.bDescriptorType = xpcu->dev->descriptor.bDescriptorType;
49 udi->Descriptor.bcdUSB = xpcu->dev->descriptor.bcdUSB;
50 udi->Descriptor.bDeviceClass = xpcu->dev->descriptor.bDeviceClass;
51 udi->Descriptor.bDeviceSubClass = xpcu->dev->descriptor.bDeviceSubClass;
52 udi->Descriptor.bDeviceProtocol = xpcu->dev->descriptor.bDeviceProtocol;
53 udi->Descriptor.bMaxPacketSize0 = xpcu->dev->descriptor.bMaxPacketSize0;
54 udi->Descriptor.idVendor = xpcu->dev->descriptor.idVendor;
55 udi->Descriptor.idProduct = xpcu->dev->descriptor.idProduct;
56 udi->Descriptor.bcdDevice = xpcu->dev->descriptor.bcdDevice;
57 udi->Descriptor.iManufacturer = xpcu->dev->descriptor.iManufacturer;
58 udi->Descriptor.iProduct = xpcu->dev->descriptor.iProduct;
59 udi->Descriptor.iSerialNumber = xpcu->dev->descriptor.iSerialNumber;
60 udi->Descriptor.bNumConfigurations = xpcu->dev->descriptor.bNumConfigurations;
61
62 /* TODO: Fix Pipe0! */
63 udi->Pipe0.dwNumber = 0x00;
64 udi->Pipe0.dwMaximumPacketSize = xpcu->dev->descriptor.bMaxPacketSize0;
65 udi->Pipe0.type = 0;
66 udi->Pipe0.direction = WDU_DIR_IN_OUT;
67 udi->Pipe0.dwInterval = 0;
68
69 pConfigs = &(udi->pConfigs);
70 pActiveConfig = &(udi->pActiveConfig);
71 pActiveInterface = &(udi->pActiveInterface[0]);
72 }
73
74 len = sizeof(struct usb_device_info);
75
76 for (i=0; i<xpcu->dev->descriptor.bNumConfigurations; i++)
77 {
78 struct usb_config_descriptor *conf_desc = &xpcu->dev->config[i];
79 WDU_INTERFACE **pInterfaces;
80 WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces];
81 WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces];
82
83 if (buf) {
84 WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len);
85
86 *pConfigs = cfg;
87 *pActiveConfig = cfg;
88
89 cfg->Descriptor.bLength = conf_desc->bLength;
90 cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType;
91 cfg->Descriptor.wTotalLength = conf_desc->wTotalLength;
92 cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces;
93 cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue;
94 cfg->Descriptor.iConfiguration = conf_desc->iConfiguration;
95 cfg->Descriptor.bmAttributes = conf_desc->bmAttributes;
96 cfg->Descriptor.MaxPower = conf_desc->MaxPower;
97
98 cfg->dwNumInterfaces = conf_desc->bNumInterfaces;
99
100 pInterfaces = &(cfg->pInterfaces);
101 }
102 len += sizeof(WDU_CONFIGURATION);
103
104 if (buf) {
105 *pInterfaces = (WDU_INTERFACE*)(buf+len);
106 for (j=0; j<conf_desc->bNumInterfaces; j++) {
107 WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len);
108
109 pActiveInterface[j] = iface;
110
111 pAlternateSettings[j] = &(iface->pAlternateSettings);
112 iface->dwNumAltSettings = xpcu->dev->config[i].interface[j].num_altsetting;
113 pActiveAltSetting[j] = &(iface->pActiveAltSetting);
114
115 len += sizeof(WDU_INTERFACE);
116 }
117 } else {
118 len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces;
119 }
120
121 for (j=0; j<conf_desc->bNumInterfaces; j++)
122 {
123 struct usb_interface *interface = &xpcu->dev->config[i].interface[j];
124
125 if (buf) {
126 *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
127 /* FIXME: */
128 *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
129 }
130
131 for(k=0; k<interface->num_altsetting; k++)
132 {
133 unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints;
134 WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors;
135 WDU_PIPE_INFO **pPipes;
136
137 if (buf) {
138 WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len);
139
140 altset->Descriptor.bLength = interface->altsetting[k].bLength;
141 altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType;
142 altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber;
143 altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting;
144 altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints;
145 altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass;
146 altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass;
147 altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol;
148 altset->Descriptor.iInterface = interface->altsetting[k].iInterface;
149 pEndpointDescriptors = &(altset->pEndpointDescriptors);
150 pPipes = &(altset->pPipes);
151
152 }
153 len +=sizeof(WDU_ALTERNATE_SETTING);
154
155 if (buf) {
156 *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
157 for (l = 0; l < bNumEndpoints; l++) {
158 WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
159
160 ed->bLength = interface->altsetting[k].endpoint[l].bLength;
161 ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType;
162 ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress;
163 ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes;
164 ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize;
165 ed->bInterval = interface->altsetting[k].endpoint[l].bInterval;
166
167 len += sizeof(WDU_ENDPOINT_DESCRIPTOR);
168 }
169
170 *pPipes = (WDU_PIPE_INFO*)(buf+len);
171 for (l = 0; l < bNumEndpoints; l++) {
172 WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len);
173
174 pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress;
175 pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize);
176 pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK;
177 if (pi->type == PIPE_TYPE_CONTROL)
178 pi->direction = WDU_DIR_IN_OUT;
179 else
180 {
181 pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT;
182 }
183
184 pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval;
185
186 len += sizeof(WDU_PIPE_INFO);
187 }
188 } else {
189 len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints;
190 }
191 }
192 }
193 }
194
8af4d910
MG
195 ugdd->dwBytes = len;
196
197 return 0;
cbfa0ac6
MG
198}
199
6234190b 200static int xpcu_claim(struct xpcu_s *xpcu, int claim) {
cbfa0ac6
MG
201 int ret = 0;
202 static int claimed = 0;
203
204 if (xpcu->interface < 0)
205 return -1;
206
207 if (claim == XPCU_CLAIM) {
208 if (claimed)
209 return 0;
210
211 ret = usb_claim_interface(xpcu->handle, xpcu->interface);
212 if (!ret) {
213 claimed = 1;
214 ret = usb_set_altinterface(xpcu->handle, xpcu->alternate);
215 if (ret)
216 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
217 } else {
218 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
219 xpcu->interface, ret, usb_strerror());
220 }
221 } else {
222 if (!claimed)
223 return 0;
224
23138e94 225#if 0
cbfa0ac6
MG
226 ret = usb_release_interface(xpcu->handle, xpcu->interface);
227 if (!ret)
228 claimed = 0;
23138e94 229#endif
cbfa0ac6
MG
230 }
231
232 return ret;
233}
234
19acdb82
MG
235int xpcu_transfer(struct usb_transfer *ut) {
236 struct xpcu_s *xpcu = (struct xpcu_s*)ut->dwUniqueID;
6c235d59
MG
237 int ret = 0;
238
8af4d910
MG
239 if (!xpcu)
240 return -ENODEV;
f92c0fbc 241
6c235d59
MG
242 xpcu_claim(xpcu, XPCU_CLAIM);
243 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
244 if (ut->dwPipeNum == 0) { /* control pipe */
245 int requesttype, request, value, index, size;
246 requesttype = ut->SetupPacket[0];
247 request = ut->SetupPacket[1];
248 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
249 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
250 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
501f1c21 251 DPRINTF("-> requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
6c235d59
MG
252 ret = usb_control_msg(xpcu->handle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
253 } else {
254 if (ut->fRead) {
255 ret = usb_bulk_read(xpcu->handle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
256 } else {
257 ret = usb_bulk_write(xpcu->handle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
258 }
259 xpcu_claim(xpcu, XPCU_RELEASE);
260 }
261
262 if (ret < 0) {
263 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
264 } else {
265 ut->dwBytesTransferred = ret;
266 ret = 0;
267 }
268
269 return ret;
270}
271
19acdb82
MG
272int xpcu_set_interface(struct usb_set_interface *usi) {
273 struct xpcu_s *xpcu = (struct xpcu_s*)usi->dwUniqueID;
274
8af4d910
MG
275 if (!xpcu)
276 return -ENODEV;
f92c0fbc 277
0c2db148
MG
278 if (xpcu->dev) {
279 if (!xpcu->handle) {
280 xpcu->handle = usb_open(xpcu->dev);
281#ifndef NO_USB_RESET
282 if (xpcu->handle) {
283 usb_reset(xpcu->handle);
284 xpcu->handle = usb_open(xpcu->dev);
285 }
286#endif
287 }
288
289 xpcu->interface = xpcu->dev->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
290 xpcu->alternate = usi->dwAlternateSetting;
291 }
8af4d910
MG
292
293 return 0;
0c2db148
MG
294}
295
f92c0fbc
MG
296static void xpcu_init(void) {
297 if (busses)
298 return;
299
300 usb_init();
301 usb_find_busses();
302 usb_find_devices();
303
304 busses = usb_get_busses();
305}
306
307
19acdb82 308int xpcu_find(struct event *e) {
9bfaca62 309 struct xpcu_event_s *xpcu_event = NULL;
19acdb82 310 struct xpcu_s *xpcu = NULL;
8949e420 311 char* usbdev;
f92c0fbc
MG
312 struct usb_bus *bus;
313 int busnum = -1, devnum = -1;
314 int i;
cbfa0ac6 315
9bfaca62
MG
316 e->handle = (unsigned long)NULL;
317
f92c0fbc
MG
318 xpcu_init();
319
8949e420
MG
320 usbdev = getenv("XILINX_USB_DEV");
321 if (usbdev != NULL) {
f92c0fbc
MG
322 int j;
323 char *devstr = NULL, *remainder;
8949e420
MG
324 char *devpos;
325
326 devpos = strdup(usbdev);
327 if (!devpos)
328 return -ENOMEM;
f92c0fbc
MG
329
330 DPRINTF("XILINX_USB_DEV=%s\n", devpos);
331
332 for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) {
333 if (devpos[j] == ':') {
334 devpos[j] = 0;
335 devstr = &(devpos[j+1]);
336 }
337 }
338
339 if (devstr && strlen(devstr) > 0) {
340 busnum = strtol(devpos, &remainder, 10);
341 if (devpos == remainder) {
342 busnum = -1;
343 } else {
344 devnum = strtol(devstr, &remainder, 10);
345 if (devstr == remainder) {
346 busnum = -1;
347 devnum = -1;
348 } else {
349 fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n",
350 busnum, devnum);
351 }
352 }
353 }
8949e420 354 free(devpos);
f92c0fbc
MG
355 }
356
8949e420
MG
357 xpcu_event = malloc(sizeof(struct xpcu_event_s));
358 if (!xpcu_event)
359 return -ENOMEM;
360
361 bzero(xpcu_event, sizeof(struct xpcu_event_s));
362 xpcu_event->xpcu = NULL;
363 xpcu_event->count = 0;
364 xpcu_event->interrupt_count = 0;
365 pthread_mutex_init(&xpcu_event->interrupt, NULL);
366
f92c0fbc
MG
367 for (i = 0; i < e->dwNumMatchTables; i++) {
368
369 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
370 e->matchTables[i].VendorId,
371 e->matchTables[i].ProductId,
372 e->matchTables[i].bDeviceClass,
373 e->matchTables[i].bDeviceSubClass,
374 e->matchTables[i].bInterfaceClass,
375 e->matchTables[i].bInterfaceSubClass,
376 e->matchTables[i].bInterfaceProtocol);
377
378 for (bus = busses; bus; bus = bus->next) {
379 struct usb_device *dev;
380
381 if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum))
382 continue;
383
384 for (dev = bus->devices; dev; dev = dev->next) {
385 struct usb_device_descriptor *desc = &(dev->descriptor);
386
387 if((desc->idVendor == e->matchTables[i].VendorId) &&
388 (desc->idProduct == e->matchTables[i].ProductId) &&
389 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
390 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) &&
391 ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) {
392 int ac;
393 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
394 struct usb_interface *interface = dev->config[ac].interface;
395 int ai;
396
397 for (ai = 0; ai < interface->num_altsetting; ai++) {
398
399 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
400 interface->altsetting[i].bInterfaceClass,
401 interface->altsetting[i].bInterfaceSubClass,
402 interface->altsetting[i].bInterfaceProtocol);
403
404 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
405 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
9bfaca62
MG
406 int n = xpcu_event->count;
407
f92c0fbc
MG
408 /* TODO: check interfaceClass! */
409 DPRINTF("found device with libusb\n");
501f1c21 410
9bfaca62
MG
411 xpcu = realloc(xpcu, sizeof(struct xpcu_s) * (++xpcu_event->count));
412 if (!xpcu) {
413 free(xpcu_event);
19acdb82 414 return -ENOMEM;
9bfaca62
MG
415 }
416
417 bzero(&(xpcu[n]), sizeof(struct xpcu_s));
418 xpcu[n].interface = -1;
419 xpcu[n].alternate = -1;
420 xpcu[n].dev = dev;
421 xpcu[n].card_type = e->dwCardType;
19acdb82 422
9bfaca62 423 xpcu_event->xpcu = xpcu;
f92c0fbc
MG
424 }
425 }
426 }
427 }
428 }
429 }
430 }
cbfa0ac6 431
9bfaca62 432 e->handle = (unsigned long)xpcu_event;
cbfa0ac6 433
19acdb82 434 return 0;
cbfa0ac6 435}
6234190b 436
19acdb82 437int xpcu_found(struct event *e) {
9bfaca62
MG
438 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)e->handle;
439 struct xpcu_s *xpcu = NULL;
440
441 if (xpcu_event && xpcu_event->count && (xpcu_event->interrupt_count <= xpcu_event->count))
442 xpcu = &(xpcu_event->xpcu[xpcu_event->interrupt_count-1]);
19acdb82
MG
443
444 if (xpcu && xpcu->dev) {
f92c0fbc
MG
445 struct usb_interface *interface = xpcu->dev->config->interface;
446
447 e->dwCardType = xpcu->card_type;
448 e->dwAction = 1;
449 e->dwEventId = 1;
9bfaca62 450 e->u.Usb.dwUniqueID = (unsigned long)xpcu;
f92c0fbc
MG
451 e->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor;
452 e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct;
453 e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass;
454 e->matchTables[0].bDeviceSubClass = xpcu->dev->descriptor.bDeviceSubClass;
455 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
456 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
457 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
6234190b 458 }
8af4d910
MG
459
460 return 0;
f92c0fbc
MG
461}
462
19acdb82 463int xpcu_close(struct event *e) {
9bfaca62 464 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)e->handle;
19acdb82 465
9bfaca62 466 if (!xpcu_event)
8af4d910 467 return -ENODEV;
6234190b 468
9bfaca62
MG
469 if(xpcu_event) {
470 struct xpcu_s *xpcu;
471 int i;
472
473 for (i = 0; i < xpcu_event->count; i++) {
474 xpcu = &(xpcu_event->xpcu[i]);
475 if (xpcu->handle) {
476 xpcu_claim(xpcu, XPCU_RELEASE);
477 usb_close(xpcu->handle);
478 }
f92c0fbc
MG
479 }
480
9bfaca62
MG
481 if (xpcu_event->xpcu)
482 free(xpcu_event->xpcu);
483
f92c0fbc 484 busses = NULL;
9bfaca62 485 free(xpcu_event);
f92c0fbc 486 }
8af4d910
MG
487
488 return 0;
6234190b 489}
501f1c21 490
19acdb82 491int xpcu_int_state(struct interrupt *it, int enable) {
9bfaca62 492 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)it->hInterrupt;
501f1c21 493
614a4da1
MG
494 if (!xpcu_event)
495 return -ENODEV;
501f1c21
MG
496
497 if (enable == ENABLE_INTERRUPT) {
498 it->fEnableOk = 1;
499 it->fStopped = 0;
9bfaca62 500 it->dwCounter = 0;
614a4da1 501 pthread_mutex_trylock(&xpcu_event->interrupt);
501f1c21
MG
502 } else {
503 it->dwCounter = 0;
504 it->fStopped = 1;
614a4da1
MG
505 if (pthread_mutex_trylock(&xpcu_event->interrupt) == EBUSY)
506 pthread_mutex_unlock(&xpcu_event->interrupt);
501f1c21 507 }
8af4d910
MG
508
509 return 0;
501f1c21
MG
510}
511
19acdb82 512int xpcu_int_wait(struct interrupt *it) {
9bfaca62 513 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)it->hInterrupt;
19acdb82 514
614a4da1
MG
515 if (!xpcu_event)
516 return -ENODEV;
517
518 if (it->dwCounter < xpcu_event->count) {
519 it->dwCounter++;
501f1c21 520 } else {
614a4da1
MG
521 pthread_mutex_lock(&xpcu_event->interrupt);
522 pthread_mutex_unlock(&xpcu_event->interrupt);
501f1c21 523 }
614a4da1 524 xpcu_event->interrupt_count++;
8af4d910
MG
525
526 return 0;
501f1c21 527}
Impressum, Datenschutz