]> cvs.zerfleddert.de Git - usb-driver/blob - xpcu.c
pass pointer to usb-driver structure through impact
[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
299
300 int xpcu_find(struct event *e) {
301 struct xpcu_s *xpcu = NULL;
302 char* devpos;
303 struct usb_bus *bus;
304 int busnum = -1, devnum = -1;
305 int i;
306
307 xpcu_init();
308
309 devpos = getenv("XILINX_USB_DEV");
310 if (devpos != NULL) {
311 int j;
312 char *devstr = NULL, *remainder;
313
314 DPRINTF("XILINX_USB_DEV=%s\n", devpos);
315
316 for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) {
317 if (devpos[j] == ':') {
318 devpos[j] = 0;
319 devstr = &(devpos[j+1]);
320 }
321 }
322
323 if (devstr && strlen(devstr) > 0) {
324 busnum = strtol(devpos, &remainder, 10);
325 if (devpos == remainder) {
326 busnum = -1;
327 } else {
328 devnum = strtol(devstr, &remainder, 10);
329 if (devstr == remainder) {
330 busnum = -1;
331 devnum = -1;
332 } else {
333 fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n",
334 busnum, devnum);
335 }
336 }
337 }
338 }
339
340 for (i = 0; i < e->dwNumMatchTables; i++) {
341
342 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
343 e->matchTables[i].VendorId,
344 e->matchTables[i].ProductId,
345 e->matchTables[i].bDeviceClass,
346 e->matchTables[i].bDeviceSubClass,
347 e->matchTables[i].bInterfaceClass,
348 e->matchTables[i].bInterfaceSubClass,
349 e->matchTables[i].bInterfaceProtocol);
350
351 for (bus = busses; bus; bus = bus->next) {
352 struct usb_device *dev;
353
354 if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum))
355 continue;
356
357 for (dev = bus->devices; dev; dev = dev->next) {
358 struct usb_device_descriptor *desc = &(dev->descriptor);
359
360 if((desc->idVendor == e->matchTables[i].VendorId) &&
361 (desc->idProduct == e->matchTables[i].ProductId) &&
362 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
363 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) &&
364 ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) {
365 int ac;
366 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
367 struct usb_interface *interface = dev->config[ac].interface;
368 int ai;
369
370 for (ai = 0; ai < interface->num_altsetting; ai++) {
371
372 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
373 interface->altsetting[i].bInterfaceClass,
374 interface->altsetting[i].bInterfaceSubClass,
375 interface->altsetting[i].bInterfaceProtocol);
376
377 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
378 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
379 /* TODO: check interfaceClass! */
380 DPRINTF("found device with libusb\n");
381
382 xpcu = malloc(sizeof(struct xpcu_s));
383 if (!xpcu)
384 return -ENOMEM;
385
386 bzero(xpcu, sizeof(struct xpcu_s));
387 xpcu->interface = -1;
388 xpcu->alternate = -1;
389 xpcu->dev = dev;
390 xpcu->card_type = e->dwCardType;
391 pthread_mutex_init(&xpcu->interrupt, NULL);
392 e->handle = (unsigned long)&xpcu;
393 }
394 }
395 }
396 }
397 }
398 }
399 }
400
401 e->handle = (unsigned long)xpcu;
402
403 return 0;
404 }
405
406 int xpcu_found(struct event *e) {
407 struct xpcu_s *xpcu = (struct xpcu_s*)e->handle;
408
409 if (xpcu && xpcu->dev) {
410 struct usb_interface *interface = xpcu->dev->config->interface;
411
412 e->dwCardType = xpcu->card_type;
413 e->dwAction = 1;
414 e->dwEventId = 1;
415 e->u.Usb.dwUniqueID = e->handle;
416 e->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor;
417 e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct;
418 e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass;
419 e->matchTables[0].bDeviceSubClass = xpcu->dev->descriptor.bDeviceSubClass;
420 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
421 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
422 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
423 }
424
425 return 0;
426 }
427
428 int xpcu_close(struct event *e) {
429 struct xpcu_s *xpcu = (struct xpcu_s*)e->handle;
430
431 if (!xpcu)
432 return -ENODEV;
433
434 if(xpcu) {
435 if (xpcu->handle) {
436 xpcu_claim(xpcu, XPCU_RELEASE);
437 usb_close(xpcu->handle);
438 }
439
440 busses = NULL;
441 free(xpcu);
442 }
443
444 return 0;
445 }
446
447 int xpcu_int_state(struct interrupt *it, int enable) {
448 struct xpcu_s *xpcu = (struct xpcu_s*)it->hInterrupt;
449 pthread_mutex_t *interrupt = &dummy_interrupt;
450
451 if (xpcu)
452 interrupt = &xpcu->interrupt;
453
454 if (enable == ENABLE_INTERRUPT) {
455 it->fEnableOk = 1;
456 it->fStopped = 0;
457 pthread_mutex_trylock(interrupt);
458 } else {
459 it->dwCounter = 0;
460 it->fStopped = 1;
461 if (pthread_mutex_trylock(interrupt) == EBUSY)
462 pthread_mutex_unlock(interrupt);
463 }
464
465 return 0;
466 }
467
468 int xpcu_int_wait(struct interrupt *it) {
469 struct xpcu_s *xpcu = (struct xpcu_s*)it->hInterrupt;
470
471 if (it->hInterrupt != (unsigned long)xpcu)
472 return -ENODEV;
473
474 if (xpcu) {
475 if (it->dwCounter == 0) {
476 it->dwCounter = 1;
477 } else {
478 pthread_mutex_lock(&xpcu->interrupt);
479 pthread_mutex_unlock(&xpcu->interrupt);
480 }
481 } else {
482 pthread_mutex_lock(&dummy_interrupt);
483 pthread_mutex_unlock(&dummy_interrupt);
484 }
485
486 return 0;
487 }
Impressum, Datenschutz