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