9 #include "usb-driver.h"
13 struct usb_device
*dev
;
14 usb_dev_handle
*handle
;
17 unsigned long card_type
;
24 pthread_mutex_t interrupt
;
27 static struct usb_bus
*busses
= NULL
;
28 static pthread_mutex_t dummy_interrupt
= PTHREAD_MUTEX_INITIALIZER
;
30 int xpcu_deviceinfo(struct usb_get_device_data
*ugdd
) {
31 struct xpcu_s
*xpcu
= (struct xpcu_s
*)ugdd
->dwUniqueID
;
34 unsigned char *buf
= NULL
;
35 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
36 WDU_INTERFACE
**pActiveInterface
;
45 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
47 udi
->Descriptor
.bLength
= sizeof(WDU_DEVICE_DESCRIPTOR
);
48 udi
->Descriptor
.bDescriptorType
= xpcu
->dev
->descriptor
.bDescriptorType
;
49 udi
->Descriptor
.bcdUSB
= xpcu
->dev
->descriptor
.bcdUSB
;
50 udi
->Descriptor
.bDeviceClass
= xpcu
->dev
->descriptor
.bDeviceClass
;
51 udi
->Descriptor
.bDeviceSubClass
= xpcu
->dev
->descriptor
.bDeviceSubClass
;
52 udi
->Descriptor
.bDeviceProtocol
= xpcu
->dev
->descriptor
.bDeviceProtocol
;
53 udi
->Descriptor
.bMaxPacketSize0
= xpcu
->dev
->descriptor
.bMaxPacketSize0
;
54 udi
->Descriptor
.idVendor
= xpcu
->dev
->descriptor
.idVendor
;
55 udi
->Descriptor
.idProduct
= xpcu
->dev
->descriptor
.idProduct
;
56 udi
->Descriptor
.bcdDevice
= xpcu
->dev
->descriptor
.bcdDevice
;
57 udi
->Descriptor
.iManufacturer
= xpcu
->dev
->descriptor
.iManufacturer
;
58 udi
->Descriptor
.iProduct
= xpcu
->dev
->descriptor
.iProduct
;
59 udi
->Descriptor
.iSerialNumber
= xpcu
->dev
->descriptor
.iSerialNumber
;
60 udi
->Descriptor
.bNumConfigurations
= xpcu
->dev
->descriptor
.bNumConfigurations
;
62 /* TODO: Fix Pipe0! */
63 udi
->Pipe0
.dwNumber
= 0x00;
64 udi
->Pipe0
.dwMaximumPacketSize
= xpcu
->dev
->descriptor
.bMaxPacketSize0
;
66 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
67 udi
->Pipe0
.dwInterval
= 0;
69 pConfigs
= &(udi
->pConfigs
);
70 pActiveConfig
= &(udi
->pActiveConfig
);
71 pActiveInterface
= &(udi
->pActiveInterface
[0]);
74 len
= sizeof(struct usb_device_info
);
76 for (i
=0; i
<xpcu
->dev
->descriptor
.bNumConfigurations
; i
++)
78 struct usb_config_descriptor
*conf_desc
= &xpcu
->dev
->config
[i
];
79 WDU_INTERFACE
**pInterfaces
;
80 WDU_ALTERNATE_SETTING
**pAlternateSettings
[conf_desc
->bNumInterfaces
];
81 WDU_ALTERNATE_SETTING
**pActiveAltSetting
[conf_desc
->bNumInterfaces
];
84 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
89 cfg
->Descriptor
.bLength
= conf_desc
->bLength
;
90 cfg
->Descriptor
.bDescriptorType
= conf_desc
->bDescriptorType
;
91 cfg
->Descriptor
.wTotalLength
= conf_desc
->wTotalLength
;
92 cfg
->Descriptor
.bNumInterfaces
= conf_desc
->bNumInterfaces
;
93 cfg
->Descriptor
.bConfigurationValue
= conf_desc
->bConfigurationValue
;
94 cfg
->Descriptor
.iConfiguration
= conf_desc
->iConfiguration
;
95 cfg
->Descriptor
.bmAttributes
= conf_desc
->bmAttributes
;
96 cfg
->Descriptor
.MaxPower
= conf_desc
->MaxPower
;
98 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
100 pInterfaces
= &(cfg
->pInterfaces
);
102 len
+= sizeof(WDU_CONFIGURATION
);
105 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
106 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
107 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
109 pActiveInterface
[j
] = iface
;
111 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
112 iface
->dwNumAltSettings
= xpcu
->dev
->config
[i
].interface
[j
].num_altsetting
;
113 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
115 len
+= sizeof(WDU_INTERFACE
);
118 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
121 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
123 struct usb_interface
*interface
= &xpcu
->dev
->config
[i
].interface
[j
];
126 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
128 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
131 for(k
=0; k
<interface
->num_altsetting
; k
++)
133 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
134 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
135 WDU_PIPE_INFO
**pPipes
;
138 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
140 altset
->Descriptor
.bLength
= interface
->altsetting
[k
].bLength
;
141 altset
->Descriptor
.bDescriptorType
= interface
->altsetting
[k
].bDescriptorType
;
142 altset
->Descriptor
.bInterfaceNumber
= interface
->altsetting
[k
].bInterfaceNumber
;
143 altset
->Descriptor
.bAlternateSetting
= interface
->altsetting
[k
].bAlternateSetting
;
144 altset
->Descriptor
.bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
145 altset
->Descriptor
.bInterfaceClass
= interface
->altsetting
[k
].bInterfaceClass
;
146 altset
->Descriptor
.bInterfaceSubClass
= interface
->altsetting
[k
].bInterfaceSubClass
;
147 altset
->Descriptor
.bInterfaceProtocol
= interface
->altsetting
[k
].bInterfaceProtocol
;
148 altset
->Descriptor
.iInterface
= interface
->altsetting
[k
].iInterface
;
149 pEndpointDescriptors
= &(altset
->pEndpointDescriptors
);
150 pPipes
= &(altset
->pPipes
);
153 len
+=sizeof(WDU_ALTERNATE_SETTING
);
156 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
157 for (l
= 0; l
< bNumEndpoints
; l
++) {
158 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
160 ed
->bLength
= interface
->altsetting
[k
].endpoint
[l
].bLength
;
161 ed
->bDescriptorType
= interface
->altsetting
[k
].endpoint
[l
].bDescriptorType
;
162 ed
->bEndpointAddress
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
163 ed
->bmAttributes
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
;
164 ed
->wMaxPacketSize
= interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
;
165 ed
->bInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
167 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
170 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
171 for (l
= 0; l
< bNumEndpoints
; l
++) {
172 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
174 pi
->dwNumber
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
175 pi
->dwMaximumPacketSize
= WDU_GET_MAX_PACKET_SIZE(interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
);
176 pi
->type
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
& USB_ENDPOINT_TYPE_MASK
;
177 if (pi
->type
== PIPE_TYPE_CONTROL
)
178 pi
->direction
= WDU_DIR_IN_OUT
;
181 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
184 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
186 len
+= sizeof(WDU_PIPE_INFO
);
189 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
200 static int xpcu_claim(struct xpcu_s
*xpcu
, int claim
) {
202 static int claimed
= 0;
204 if (xpcu
->interface
< 0)
207 if (claim
== XPCU_CLAIM
) {
211 ret
= usb_claim_interface(xpcu
->handle
, xpcu
->interface
);
214 ret
= usb_set_altinterface(xpcu
->handle
, xpcu
->alternate
);
216 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
218 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
219 xpcu
->interface
, ret
, usb_strerror());
225 ret
= usb_release_interface(xpcu
->handle
, xpcu
->interface
);
233 int xpcu_transfer(struct usb_transfer
*ut
) {
234 struct xpcu_s
*xpcu
= (struct xpcu_s
*)ut
->dwUniqueID
;
240 xpcu_claim(xpcu
, XPCU_CLAIM
);
241 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
242 if (ut
->dwPipeNum
== 0) { /* control pipe */
243 int requesttype
, request
, value
, index
, size
;
244 requesttype
= ut
->SetupPacket
[0];
245 request
= ut
->SetupPacket
[1];
246 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
247 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
248 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
249 DPRINTF("-> requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
250 ret
= usb_control_msg(xpcu
->handle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
253 ret
= usb_bulk_read(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
255 ret
= usb_bulk_write(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
257 xpcu_claim(xpcu
, XPCU_RELEASE
);
261 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
263 ut
->dwBytesTransferred
= ret
;
270 int xpcu_set_interface(struct usb_set_interface
*usi
) {
271 struct xpcu_s
*xpcu
= (struct xpcu_s
*)usi
->dwUniqueID
;
278 xpcu
->handle
= usb_open(xpcu
->dev
);
281 usb_reset(xpcu
->handle
);
282 xpcu
->handle
= usb_open(xpcu
->dev
);
287 xpcu
->interface
= xpcu
->dev
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
288 xpcu
->alternate
= usi
->dwAlternateSetting
;
294 static void xpcu_init(void) {
302 busses
= usb_get_busses();
306 int xpcu_find(struct event
*e
) {
307 struct xpcu_event_s
*xpcu_event
= NULL
;
308 struct xpcu_s
*xpcu
= NULL
;
311 int busnum
= -1, devnum
= -1;
314 e
->handle
= (unsigned long)NULL
;
318 usbdev
= getenv("XILINX_USB_DEV");
319 if (usbdev
!= NULL
) {
321 char *devstr
= NULL
, *remainder
;
324 devpos
= strdup(usbdev
);
328 DPRINTF("XILINX_USB_DEV=%s\n", devpos
);
330 for (j
= 0; j
< strlen(devpos
) && devpos
[j
] != 0; j
++) {
331 if (devpos
[j
] == ':') {
333 devstr
= &(devpos
[j
+1]);
337 if (devstr
&& strlen(devstr
) > 0) {
338 busnum
= strtol(devpos
, &remainder
, 10);
339 if (devpos
== remainder
) {
342 devnum
= strtol(devstr
, &remainder
, 10);
343 if (devstr
== remainder
) {
347 fprintf(stderr
,"Using XILINX platform cable USB at %03d:%03d\n",
355 xpcu_event
= malloc(sizeof(struct xpcu_event_s
));
359 bzero(xpcu_event
, sizeof(struct xpcu_event_s
));
360 xpcu_event
->xpcu
= NULL
;
361 xpcu_event
->count
= 0;
362 xpcu_event
->interrupt_count
= 0;
363 pthread_mutex_init(&xpcu_event
->interrupt
, NULL
);
365 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
367 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
368 e
->matchTables
[i
].VendorId
,
369 e
->matchTables
[i
].ProductId
,
370 e
->matchTables
[i
].bDeviceClass
,
371 e
->matchTables
[i
].bDeviceSubClass
,
372 e
->matchTables
[i
].bInterfaceClass
,
373 e
->matchTables
[i
].bInterfaceSubClass
,
374 e
->matchTables
[i
].bInterfaceProtocol
);
376 for (bus
= busses
; bus
; bus
= bus
->next
) {
377 struct usb_device
*dev
;
379 if ((devnum
!= -1) && (strtol(bus
->dirname
, NULL
, 10) != busnum
))
382 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
383 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
385 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
386 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
387 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
388 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
) &&
389 ((devnum
== -1) || (strtol(dev
->filename
, NULL
, 10) == devnum
)) ) {
391 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
392 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
395 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
397 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
398 interface
->altsetting
[i
].bInterfaceClass
,
399 interface
->altsetting
[i
].bInterfaceSubClass
,
400 interface
->altsetting
[i
].bInterfaceProtocol
);
402 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
403 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
404 int n
= xpcu_event
->count
;
406 /* TODO: check interfaceClass! */
407 DPRINTF("found device with libusb\n");
409 xpcu
= realloc(xpcu
, sizeof(struct xpcu_s
) * (++xpcu_event
->count
));
415 bzero(&(xpcu
[n
]), sizeof(struct xpcu_s
));
416 xpcu
[n
].interface
= -1;
417 xpcu
[n
].alternate
= -1;
419 xpcu
[n
].card_type
= e
->dwCardType
;
421 xpcu_event
->xpcu
= xpcu
;
430 e
->handle
= (unsigned long)xpcu_event
;
435 int xpcu_found(struct event
*e
) {
436 struct xpcu_event_s
*xpcu_event
= (struct xpcu_event_s
*)e
->handle
;
437 struct xpcu_s
*xpcu
= NULL
;
439 if (xpcu_event
&& xpcu_event
->count
&& (xpcu_event
->interrupt_count
<= xpcu_event
->count
))
440 xpcu
= &(xpcu_event
->xpcu
[xpcu_event
->interrupt_count
-1]);
442 if (xpcu
&& xpcu
->dev
) {
443 struct usb_interface
*interface
= xpcu
->dev
->config
->interface
;
445 e
->dwCardType
= xpcu
->card_type
;
448 e
->u
.Usb
.dwUniqueID
= (unsigned long)xpcu
;
449 e
->matchTables
[0].VendorId
= xpcu
->dev
->descriptor
.idVendor
;
450 e
->matchTables
[0].ProductId
= xpcu
->dev
->descriptor
.idProduct
;
451 e
->matchTables
[0].bDeviceClass
= xpcu
->dev
->descriptor
.bDeviceClass
;
452 e
->matchTables
[0].bDeviceSubClass
= xpcu
->dev
->descriptor
.bDeviceSubClass
;
453 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
454 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
455 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
461 int xpcu_close(struct event
*e
) {
462 struct xpcu_event_s
*xpcu_event
= (struct xpcu_event_s
*)e
->handle
;
471 for (i
= 0; i
< xpcu_event
->count
; i
++) {
472 xpcu
= &(xpcu_event
->xpcu
[i
]);
474 xpcu_claim(xpcu
, XPCU_RELEASE
);
475 usb_close(xpcu
->handle
);
479 if (xpcu_event
->xpcu
)
480 free(xpcu_event
->xpcu
);
489 int xpcu_int_state(struct interrupt
*it
, int enable
) {
490 struct xpcu_event_s
*xpcu_event
= (struct xpcu_event_s
*)it
->hInterrupt
;
491 pthread_mutex_t
*interrupt
= &dummy_interrupt
;
494 interrupt
= &xpcu_event
->interrupt
;
496 if (enable
== ENABLE_INTERRUPT
) {
500 pthread_mutex_trylock(interrupt
);
504 if (pthread_mutex_trylock(interrupt
) == EBUSY
)
505 pthread_mutex_unlock(interrupt
);
511 int xpcu_int_wait(struct interrupt
*it
) {
512 struct xpcu_event_s
*xpcu_event
= (struct xpcu_event_s
*)it
->hInterrupt
;
515 if (it
->dwCounter
< xpcu_event
->count
) {
518 pthread_mutex_lock(&xpcu_event
->interrupt
);
519 pthread_mutex_unlock(&xpcu_event
->interrupt
);
521 xpcu_event
->interrupt_count
++;
523 pthread_mutex_lock(&dummy_interrupt
);
524 pthread_mutex_unlock(&dummy_interrupt
);