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