]> cvs.zerfleddert.de Git - usb-driver/blame - xpcu.c
implement support for multiple XPCU cables for ISE 10.1
[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>
cbfa0ac6 9#include "usb-driver.h"
6c235d59 10#include "xpcu.h"
cbfa0ac6 11
19acdb82
MG
12struct xpcu_s {
13 struct usb_device *dev;
14 usb_dev_handle *handle;
15 int interface;
16 int alternate;
17 unsigned long card_type;
9bfaca62
MG
18};
19
20struct xpcu_event_s {
21 struct xpcu_s *xpcu;
22 int count;
23 int interrupt_count;
19acdb82
MG
24 pthread_mutex_t interrupt;
25};
26
f92c0fbc 27static struct usb_bus *busses = NULL;
501f1c21 28static pthread_mutex_t dummy_interrupt = PTHREAD_MUTEX_INITIALIZER;
f92c0fbc 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
225 ret = usb_release_interface(xpcu->handle, xpcu->interface);
226 if (!ret)
227 claimed = 0;
228 }
229
230 return ret;
231}
232
19acdb82
MG
233int xpcu_transfer(struct usb_transfer *ut) {
234 struct xpcu_s *xpcu = (struct xpcu_s*)ut->dwUniqueID;
6c235d59
MG
235 int ret = 0;
236
8af4d910
MG
237 if (!xpcu)
238 return -ENODEV;
f92c0fbc 239
6c235d59
MG
240 xpcu_claim(xpcu, XPCU_CLAIM);
241 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
242 if (ut->dwPipeNum == 0) { /* control pipe */
243 int requesttype, request, value, index, size;
244 requesttype = ut->SetupPacket[0];
245 request = ut->SetupPacket[1];
246 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
247 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
248 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
501f1c21 249 DPRINTF("-> requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
6c235d59
MG
250 ret = usb_control_msg(xpcu->handle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
251 } else {
252 if (ut->fRead) {
253 ret = usb_bulk_read(xpcu->handle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
254 } else {
255 ret = usb_bulk_write(xpcu->handle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
256 }
257 xpcu_claim(xpcu, XPCU_RELEASE);
258 }
259
260 if (ret < 0) {
261 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
262 } else {
263 ut->dwBytesTransferred = ret;
264 ret = 0;
265 }
266
267 return ret;
268}
269
19acdb82
MG
270int xpcu_set_interface(struct usb_set_interface *usi) {
271 struct xpcu_s *xpcu = (struct xpcu_s*)usi->dwUniqueID;
272
8af4d910
MG
273 if (!xpcu)
274 return -ENODEV;
f92c0fbc 275
0c2db148
MG
276 if (xpcu->dev) {
277 if (!xpcu->handle) {
278 xpcu->handle = usb_open(xpcu->dev);
279#ifndef NO_USB_RESET
280 if (xpcu->handle) {
281 usb_reset(xpcu->handle);
282 xpcu->handle = usb_open(xpcu->dev);
283 }
284#endif
285 }
286
287 xpcu->interface = xpcu->dev->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
288 xpcu->alternate = usi->dwAlternateSetting;
289 }
8af4d910
MG
290
291 return 0;
0c2db148
MG
292}
293
f92c0fbc
MG
294static void xpcu_init(void) {
295 if (busses)
296 return;
297
298 usb_init();
299 usb_find_busses();
300 usb_find_devices();
301
302 busses = usb_get_busses();
0a2341bb
MG
303
304 pthread_mutex_init(&dummy_interrupt, NULL);
f92c0fbc
MG
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;
f92c0fbc
MG
311 char* devpos;
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
9bfaca62
MG
320 xpcu_event = malloc(sizeof(struct xpcu_event_s));
321 if (!xpcu_event)
322 return -ENOMEM;
323
324 bzero(xpcu_event, sizeof(struct xpcu_event_s));
325 xpcu_event->xpcu = NULL;
326 xpcu_event->count = 0;
327 xpcu_event->interrupt_count = 0;
328 pthread_mutex_init(&xpcu_event->interrupt, NULL);
329
f92c0fbc
MG
330 devpos = getenv("XILINX_USB_DEV");
331 if (devpos != NULL) {
332 int j;
333 char *devstr = NULL, *remainder;
334
335 DPRINTF("XILINX_USB_DEV=%s\n", devpos);
336
337 for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) {
338 if (devpos[j] == ':') {
339 devpos[j] = 0;
340 devstr = &(devpos[j+1]);
341 }
342 }
343
344 if (devstr && strlen(devstr) > 0) {
345 busnum = strtol(devpos, &remainder, 10);
346 if (devpos == remainder) {
347 busnum = -1;
348 } else {
349 devnum = strtol(devstr, &remainder, 10);
350 if (devstr == remainder) {
351 busnum = -1;
352 devnum = -1;
353 } else {
354 fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n",
355 busnum, devnum);
356 }
357 }
358 }
359 }
360
361 for (i = 0; i < e->dwNumMatchTables; i++) {
362
363 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
364 e->matchTables[i].VendorId,
365 e->matchTables[i].ProductId,
366 e->matchTables[i].bDeviceClass,
367 e->matchTables[i].bDeviceSubClass,
368 e->matchTables[i].bInterfaceClass,
369 e->matchTables[i].bInterfaceSubClass,
370 e->matchTables[i].bInterfaceProtocol);
371
372 for (bus = busses; bus; bus = bus->next) {
373 struct usb_device *dev;
374
375 if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum))
376 continue;
377
378 for (dev = bus->devices; dev; dev = dev->next) {
379 struct usb_device_descriptor *desc = &(dev->descriptor);
380
381 if((desc->idVendor == e->matchTables[i].VendorId) &&
382 (desc->idProduct == e->matchTables[i].ProductId) &&
383 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
384 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) &&
385 ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) {
386 int ac;
387 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
388 struct usb_interface *interface = dev->config[ac].interface;
389 int ai;
390
391 for (ai = 0; ai < interface->num_altsetting; ai++) {
392
393 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
394 interface->altsetting[i].bInterfaceClass,
395 interface->altsetting[i].bInterfaceSubClass,
396 interface->altsetting[i].bInterfaceProtocol);
397
398 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
399 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
9bfaca62
MG
400 int n = xpcu_event->count;
401
f92c0fbc
MG
402 /* TODO: check interfaceClass! */
403 DPRINTF("found device with libusb\n");
501f1c21 404
9bfaca62
MG
405 xpcu = realloc(xpcu, sizeof(struct xpcu_s) * (++xpcu_event->count));
406 if (!xpcu) {
407 free(xpcu_event);
19acdb82 408 return -ENOMEM;
9bfaca62
MG
409 }
410
411 bzero(&(xpcu[n]), sizeof(struct xpcu_s));
412 xpcu[n].interface = -1;
413 xpcu[n].alternate = -1;
414 xpcu[n].dev = dev;
415 xpcu[n].card_type = e->dwCardType;
19acdb82 416
9bfaca62 417 xpcu_event->xpcu = xpcu;
f92c0fbc
MG
418 }
419 }
420 }
421 }
422 }
423 }
424 }
cbfa0ac6 425
9bfaca62 426 e->handle = (unsigned long)xpcu_event;
cbfa0ac6 427
19acdb82 428 return 0;
cbfa0ac6 429}
6234190b 430
19acdb82 431int xpcu_found(struct event *e) {
9bfaca62
MG
432 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)e->handle;
433 struct xpcu_s *xpcu = NULL;
434
435 if (xpcu_event && xpcu_event->count && (xpcu_event->interrupt_count <= xpcu_event->count))
436 xpcu = &(xpcu_event->xpcu[xpcu_event->interrupt_count-1]);
19acdb82
MG
437
438 if (xpcu && xpcu->dev) {
f92c0fbc
MG
439 struct usb_interface *interface = xpcu->dev->config->interface;
440
441 e->dwCardType = xpcu->card_type;
442 e->dwAction = 1;
443 e->dwEventId = 1;
9bfaca62 444 e->u.Usb.dwUniqueID = (unsigned long)xpcu;
f92c0fbc
MG
445 e->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor;
446 e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct;
447 e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass;
448 e->matchTables[0].bDeviceSubClass = xpcu->dev->descriptor.bDeviceSubClass;
449 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
450 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
451 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
6234190b 452 }
8af4d910
MG
453
454 return 0;
f92c0fbc
MG
455}
456
19acdb82 457int xpcu_close(struct event *e) {
9bfaca62 458 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)e->handle;
19acdb82 459
9bfaca62 460 if (!xpcu_event)
8af4d910 461 return -ENODEV;
6234190b 462
9bfaca62
MG
463 if(xpcu_event) {
464 struct xpcu_s *xpcu;
465 int i;
466
467 for (i = 0; i < xpcu_event->count; i++) {
468 xpcu = &(xpcu_event->xpcu[i]);
469 if (xpcu->handle) {
470 xpcu_claim(xpcu, XPCU_RELEASE);
471 usb_close(xpcu->handle);
472 }
f92c0fbc
MG
473 }
474
9bfaca62
MG
475 if (xpcu_event->xpcu)
476 free(xpcu_event->xpcu);
477
f92c0fbc 478 busses = NULL;
9bfaca62 479 free(xpcu_event);
f92c0fbc 480 }
8af4d910
MG
481
482 return 0;
6234190b 483}
501f1c21 484
19acdb82 485int xpcu_int_state(struct interrupt *it, int enable) {
9bfaca62 486 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)it->hInterrupt;
19acdb82 487 pthread_mutex_t *interrupt = &dummy_interrupt;
501f1c21 488
9bfaca62
MG
489 if (xpcu_event)
490 interrupt = &xpcu_event->interrupt;
501f1c21
MG
491
492 if (enable == ENABLE_INTERRUPT) {
493 it->fEnableOk = 1;
494 it->fStopped = 0;
9bfaca62 495 it->dwCounter = 0;
501f1c21
MG
496 pthread_mutex_trylock(interrupt);
497 } else {
498 it->dwCounter = 0;
499 it->fStopped = 1;
500 if (pthread_mutex_trylock(interrupt) == EBUSY)
501 pthread_mutex_unlock(interrupt);
502 }
8af4d910
MG
503
504 return 0;
501f1c21
MG
505}
506
19acdb82 507int xpcu_int_wait(struct interrupt *it) {
9bfaca62 508 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)it->hInterrupt;
19acdb82 509
9bfaca62
MG
510 if (xpcu_event) {
511 if (it->dwCounter < xpcu_event->count) {
512 it->dwCounter++;
501f1c21 513 } else {
9bfaca62
MG
514 pthread_mutex_lock(&xpcu_event->interrupt);
515 pthread_mutex_unlock(&xpcu_event->interrupt);
501f1c21 516 }
9bfaca62 517 xpcu_event->interrupt_count++;
501f1c21
MG
518 } else {
519 pthread_mutex_lock(&dummy_interrupt);
520 pthread_mutex_unlock(&dummy_interrupt);
521 }
8af4d910
MG
522
523 return 0;
501f1c21 524}
Impressum, Datenschutz