]> git.zerfleddert.de Git - usb-driver/blame - xpcu.c
add include stdint.h
[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 28static struct usb_bus *busses = NULL;
501f1c21 29static pthread_mutex_t dummy_interrupt = PTHREAD_MUTEX_INITIALIZER;
f92c0fbc 30
19acdb82
MG
31int xpcu_deviceinfo(struct usb_get_device_data *ugdd) {
32 struct xpcu_s *xpcu = (struct xpcu_s*)ugdd->dwUniqueID;
cbfa0ac6
MG
33 int i,j,k,l;
34 int len = 0;
f92c0fbc 35 unsigned char *buf = NULL;
cbfa0ac6
MG
36 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
37 WDU_INTERFACE **pActiveInterface;
38
8af4d910
MG
39 if (!xpcu)
40 return -ENODEV;
f92c0fbc
MG
41
42 if (ugdd->dwBytes)
43 buf = ugdd->pBuf;
44
cbfa0ac6
MG
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
8af4d910
MG
196 ugdd->dwBytes = len;
197
198 return 0;
cbfa0ac6
MG
199}
200
6234190b 201static int xpcu_claim(struct xpcu_s *xpcu, int claim) {
cbfa0ac6
MG
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
19acdb82
MG
234int xpcu_transfer(struct usb_transfer *ut) {
235 struct xpcu_s *xpcu = (struct xpcu_s*)ut->dwUniqueID;
6c235d59
MG
236 int ret = 0;
237
8af4d910
MG
238 if (!xpcu)
239 return -ENODEV;
f92c0fbc 240
6c235d59
MG
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);
501f1c21 250 DPRINTF("-> requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
6c235d59
MG
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
19acdb82
MG
271int xpcu_set_interface(struct usb_set_interface *usi) {
272 struct xpcu_s *xpcu = (struct xpcu_s*)usi->dwUniqueID;
273
8af4d910
MG
274 if (!xpcu)
275 return -ENODEV;
f92c0fbc 276
0c2db148
MG
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 }
8af4d910
MG
291
292 return 0;
0c2db148
MG
293}
294
f92c0fbc
MG
295static 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
19acdb82 307int xpcu_find(struct event *e) {
9bfaca62 308 struct xpcu_event_s *xpcu_event = NULL;
19acdb82 309 struct xpcu_s *xpcu = NULL;
8949e420 310 char* usbdev;
f92c0fbc
MG
311 struct usb_bus *bus;
312 int busnum = -1, devnum = -1;
313 int i;
cbfa0ac6 314
9bfaca62
MG
315 e->handle = (unsigned long)NULL;
316
f92c0fbc
MG
317 xpcu_init();
318
8949e420
MG
319 usbdev = getenv("XILINX_USB_DEV");
320 if (usbdev != NULL) {
f92c0fbc
MG
321 int j;
322 char *devstr = NULL, *remainder;
8949e420
MG
323 char *devpos;
324
325 devpos = strdup(usbdev);
326 if (!devpos)
327 return -ENOMEM;
f92c0fbc
MG
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 }
8949e420 353 free(devpos);
f92c0fbc
MG
354 }
355
8949e420
MG
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
f92c0fbc
MG
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)){
9bfaca62
MG
405 int n = xpcu_event->count;
406
f92c0fbc
MG
407 /* TODO: check interfaceClass! */
408 DPRINTF("found device with libusb\n");
501f1c21 409
9bfaca62
MG
410 xpcu = realloc(xpcu, sizeof(struct xpcu_s) * (++xpcu_event->count));
411 if (!xpcu) {
412 free(xpcu_event);
19acdb82 413 return -ENOMEM;
9bfaca62
MG
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;
19acdb82 421
9bfaca62 422 xpcu_event->xpcu = xpcu;
f92c0fbc
MG
423 }
424 }
425 }
426 }
427 }
428 }
429 }
cbfa0ac6 430
9bfaca62 431 e->handle = (unsigned long)xpcu_event;
cbfa0ac6 432
19acdb82 433 return 0;
cbfa0ac6 434}
6234190b 435
19acdb82 436int xpcu_found(struct event *e) {
9bfaca62
MG
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]);
19acdb82
MG
442
443 if (xpcu && xpcu->dev) {
f92c0fbc
MG
444 struct usb_interface *interface = xpcu->dev->config->interface;
445
446 e->dwCardType = xpcu->card_type;
447 e->dwAction = 1;
448 e->dwEventId = 1;
9bfaca62 449 e->u.Usb.dwUniqueID = (unsigned long)xpcu;
f92c0fbc
MG
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;
6234190b 457 }
8af4d910
MG
458
459 return 0;
f92c0fbc
MG
460}
461
19acdb82 462int xpcu_close(struct event *e) {
9bfaca62 463 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)e->handle;
19acdb82 464
9bfaca62 465 if (!xpcu_event)
8af4d910 466 return -ENODEV;
6234190b 467
9bfaca62
MG
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 }
f92c0fbc
MG
478 }
479
9bfaca62
MG
480 if (xpcu_event->xpcu)
481 free(xpcu_event->xpcu);
482
f92c0fbc 483 busses = NULL;
9bfaca62 484 free(xpcu_event);
f92c0fbc 485 }
8af4d910
MG
486
487 return 0;
6234190b 488}
501f1c21 489
19acdb82 490int xpcu_int_state(struct interrupt *it, int enable) {
9bfaca62 491 struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)it->hInterrupt;
19acdb82 492 pthread_mutex_t *interrupt = &dummy_interrupt;
501f1c21 493
9bfaca62
MG
494 if (xpcu_event)
495 interrupt = &xpcu_event->interrupt;
501f1c21
MG
496
497 if (enable == ENABLE_INTERRUPT) {
498 it->fEnableOk = 1;
499 it->fStopped = 0;
9bfaca62 500 it->dwCounter = 0;
501f1c21
MG
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 }
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
9bfaca62
MG
515 if (xpcu_event) {
516 if (it->dwCounter < xpcu_event->count) {
517 it->dwCounter++;
501f1c21 518 } else {
9bfaca62
MG
519 pthread_mutex_lock(&xpcu_event->interrupt);
520 pthread_mutex_unlock(&xpcu_event->interrupt);
501f1c21 521 }
9bfaca62 522 xpcu_event->interrupt_count++;
501f1c21
MG
523 } else {
524 pthread_mutex_lock(&dummy_interrupt);
525 pthread_mutex_unlock(&dummy_interrupt);
526 }
8af4d910
MG
527
528 return 0;
501f1c21 529}
Impressum, Datenschutz