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