]> git.zerfleddert.de Git - usb-driver/blob - xpcu.c
93d04247c86a644497f48bd753d19fb71ba65ab6
[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 "usb-driver.h"
10 #include "xpcu.h"
11
12 struct 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
21 static struct usb_bus *busses = NULL;
22 static pthread_mutex_t dummy_interrupt = PTHREAD_MUTEX_INITIALIZER;
23
24 int xpcu_deviceinfo(struct usb_get_device_data *ugdd) {
25 struct xpcu_s *xpcu = (struct xpcu_s*)ugdd->dwUniqueID;
26 int i,j,k,l;
27 int len = 0;
28 unsigned char *buf = NULL;
29 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
30 WDU_INTERFACE **pActiveInterface;
31
32 if (!xpcu)
33 return -ENODEV;
34
35 if (ugdd->dwBytes)
36 buf = ugdd->pBuf;
37
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
189 ugdd->dwBytes = len;
190
191 return 0;
192 }
193
194 static int xpcu_claim(struct xpcu_s *xpcu, int claim) {
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
227 int xpcu_transfer(struct usb_transfer *ut) {
228 struct xpcu_s *xpcu = (struct xpcu_s*)ut->dwUniqueID;
229 int ret = 0;
230
231 if (!xpcu)
232 return -ENODEV;
233
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);
243 DPRINTF("-> requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
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
264 int xpcu_set_interface(struct usb_set_interface *usi) {
265 struct xpcu_s *xpcu = (struct xpcu_s*)usi->dwUniqueID;
266
267 if (!xpcu)
268 return -ENODEV;
269
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 }
284
285 return 0;
286 }
287
288 static 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();
297
298 pthread_mutex_init(&dummy_interrupt, NULL);
299 }
300
301
302 int xpcu_find(struct event *e) {
303 struct xpcu_s *xpcu = NULL;
304 char* devpos;
305 struct usb_bus *bus;
306 int busnum = -1, devnum = -1;
307 int i;
308
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");
383
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);
394 e->handle = (unsigned long)&xpcu;
395 }
396 }
397 }
398 }
399 }
400 }
401 }
402
403 e->handle = (unsigned long)xpcu;
404
405 return 0;
406 }
407
408 int xpcu_found(struct event *e) {
409 struct xpcu_s *xpcu = (struct xpcu_s*)e->handle;
410
411 if (xpcu && xpcu->dev) {
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;
425 }
426
427 return 0;
428 }
429
430 int xpcu_close(struct event *e) {
431 struct xpcu_s *xpcu = (struct xpcu_s*)e->handle;
432
433 if (!xpcu)
434 return -ENODEV;
435
436 if(xpcu) {
437 if (xpcu->handle) {
438 xpcu_claim(xpcu, XPCU_RELEASE);
439 usb_close(xpcu->handle);
440 }
441
442 busses = NULL;
443 free(xpcu);
444 }
445
446 return 0;
447 }
448
449 int 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;
452
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 }
466
467 return 0;
468 }
469
470 int xpcu_int_wait(struct interrupt *it) {
471 struct xpcu_s *xpcu = (struct xpcu_s*)it->hInterrupt;
472
473 if (it->hInterrupt != (unsigned long)xpcu)
474 return -ENODEV;
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 }
487
488 return 0;
489 }
Impressum, Datenschutz