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