10 #include "usb-driver.h"
14 struct usb_device
*dev
;
15 usb_dev_handle
*handle
;
18 unsigned long card_type
;
25 pthread_mutex_t interrupt
;
28 static struct usb_bus
*busses
= NULL
;
29 static pthread_mutex_t dummy_interrupt
= PTHREAD_MUTEX_INITIALIZER
;
31 int xpcu_deviceinfo(struct usb_get_device_data
*ugdd
) {
32 struct xpcu_s
*xpcu
= (struct xpcu_s
*)ugdd
->dwUniqueID
;
35 unsigned char *buf
= NULL
;
36 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
37 WDU_INTERFACE
**pActiveInterface
;
46 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
48 udi
->Descriptor
.bLength
= sizeof(WDU_DEVICE_DESCRIPTOR
);
49 udi
->Descriptor
.bDescriptorType
= xpcu
->dev
->descriptor
.bDescriptorType
;
50 udi
->Descriptor
.bcdUSB
= xpcu
->dev
->descriptor
.bcdUSB
;
51 udi
->Descriptor
.bDeviceClass
= xpcu
->dev
->descriptor
.bDeviceClass
;
52 udi
->Descriptor
.bDeviceSubClass
= xpcu
->dev
->descriptor
.bDeviceSubClass
;
53 udi
->Descriptor
.bDeviceProtocol
= xpcu
->dev
->descriptor
.bDeviceProtocol
;
54 udi
->Descriptor
.bMaxPacketSize0
= xpcu
->dev
->descriptor
.bMaxPacketSize0
;
55 udi
->Descriptor
.idVendor
= xpcu
->dev
->descriptor
.idVendor
;
56 udi
->Descriptor
.idProduct
= xpcu
->dev
->descriptor
.idProduct
;
57 udi
->Descriptor
.bcdDevice
= xpcu
->dev
->descriptor
.bcdDevice
;
58 udi
->Descriptor
.iManufacturer
= xpcu
->dev
->descriptor
.iManufacturer
;
59 udi
->Descriptor
.iProduct
= xpcu
->dev
->descriptor
.iProduct
;
60 udi
->Descriptor
.iSerialNumber
= xpcu
->dev
->descriptor
.iSerialNumber
;
61 udi
->Descriptor
.bNumConfigurations
= xpcu
->dev
->descriptor
.bNumConfigurations
;
63 /* TODO: Fix Pipe0! */
64 udi
->Pipe0
.dwNumber
= 0x00;
65 udi
->Pipe0
.dwMaximumPacketSize
= xpcu
->dev
->descriptor
.bMaxPacketSize0
;
67 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
68 udi
->Pipe0
.dwInterval
= 0;
70 pConfigs
= &(udi
->pConfigs
);
71 pActiveConfig
= &(udi
->pActiveConfig
);
72 pActiveInterface
= &(udi
->pActiveInterface
[0]);
75 len
= sizeof(struct usb_device_info
);
77 for (i
=0; i
<xpcu
->dev
->descriptor
.bNumConfigurations
; i
++)
79 struct usb_config_descriptor
*conf_desc
= &xpcu
->dev
->config
[i
];
80 WDU_INTERFACE
**pInterfaces
;
81 WDU_ALTERNATE_SETTING
**pAlternateSettings
[conf_desc
->bNumInterfaces
];
82 WDU_ALTERNATE_SETTING
**pActiveAltSetting
[conf_desc
->bNumInterfaces
];
85 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
90 cfg
->Descriptor
.bLength
= conf_desc
->bLength
;
91 cfg
->Descriptor
.bDescriptorType
= conf_desc
->bDescriptorType
;
92 cfg
->Descriptor
.wTotalLength
= conf_desc
->wTotalLength
;
93 cfg
->Descriptor
.bNumInterfaces
= conf_desc
->bNumInterfaces
;
94 cfg
->Descriptor
.bConfigurationValue
= conf_desc
->bConfigurationValue
;
95 cfg
->Descriptor
.iConfiguration
= conf_desc
->iConfiguration
;
96 cfg
->Descriptor
.bmAttributes
= conf_desc
->bmAttributes
;
97 cfg
->Descriptor
.MaxPower
= conf_desc
->MaxPower
;
99 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
101 pInterfaces
= &(cfg
->pInterfaces
);
103 len
+= sizeof(WDU_CONFIGURATION
);
106 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
107 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
108 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
110 pActiveInterface
[j
] = iface
;
112 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
113 iface
->dwNumAltSettings
= xpcu
->dev
->config
[i
].interface
[j
].num_altsetting
;
114 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
116 len
+= sizeof(WDU_INTERFACE
);
119 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
122 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
124 struct usb_interface
*interface
= &xpcu
->dev
->config
[i
].interface
[j
];
127 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
129 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
132 for(k
=0; k
<interface
->num_altsetting
; k
++)
134 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
135 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
136 WDU_PIPE_INFO
**pPipes
;
139 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
141 altset
->Descriptor
.bLength
= interface
->altsetting
[k
].bLength
;
142 altset
->Descriptor
.bDescriptorType
= interface
->altsetting
[k
].bDescriptorType
;
143 altset
->Descriptor
.bInterfaceNumber
= interface
->altsetting
[k
].bInterfaceNumber
;
144 altset
->Descriptor
.bAlternateSetting
= interface
->altsetting
[k
].bAlternateSetting
;
145 altset
->Descriptor
.bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
146 altset
->Descriptor
.bInterfaceClass
= interface
->altsetting
[k
].bInterfaceClass
;
147 altset
->Descriptor
.bInterfaceSubClass
= interface
->altsetting
[k
].bInterfaceSubClass
;
148 altset
->Descriptor
.bInterfaceProtocol
= interface
->altsetting
[k
].bInterfaceProtocol
;
149 altset
->Descriptor
.iInterface
= interface
->altsetting
[k
].iInterface
;
150 pEndpointDescriptors
= &(altset
->pEndpointDescriptors
);
151 pPipes
= &(altset
->pPipes
);
154 len
+=sizeof(WDU_ALTERNATE_SETTING
);
157 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
158 for (l
= 0; l
< bNumEndpoints
; l
++) {
159 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
161 ed
->bLength
= interface
->altsetting
[k
].endpoint
[l
].bLength
;
162 ed
->bDescriptorType
= interface
->altsetting
[k
].endpoint
[l
].bDescriptorType
;
163 ed
->bEndpointAddress
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
164 ed
->bmAttributes
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
;
165 ed
->wMaxPacketSize
= interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
;
166 ed
->bInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
168 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
171 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
172 for (l
= 0; l
< bNumEndpoints
; l
++) {
173 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
175 pi
->dwNumber
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
176 pi
->dwMaximumPacketSize
= WDU_GET_MAX_PACKET_SIZE(interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
);
177 pi
->type
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
& USB_ENDPOINT_TYPE_MASK
;
178 if (pi
->type
== PIPE_TYPE_CONTROL
)
179 pi
->direction
= WDU_DIR_IN_OUT
;
182 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
185 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
187 len
+= sizeof(WDU_PIPE_INFO
);
190 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
201 static int xpcu_claim(struct xpcu_s
*xpcu
, int claim
) {
203 static int claimed
= 0;
205 if (xpcu
->interface
< 0)
208 if (claim
== XPCU_CLAIM
) {
212 ret
= usb_claim_interface(xpcu
->handle
, xpcu
->interface
);
215 ret
= usb_set_altinterface(xpcu
->handle
, xpcu
->alternate
);
217 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
219 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
220 xpcu
->interface
, ret
, usb_strerror());
226 ret
= usb_release_interface(xpcu
->handle
, xpcu
->interface
);
234 int xpcu_transfer(struct usb_transfer
*ut
) {
235 struct xpcu_s
*xpcu
= (struct xpcu_s
*)ut
->dwUniqueID
;
241 xpcu_claim(xpcu
, XPCU_CLAIM
);
242 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
243 if (ut
->dwPipeNum
== 0) { /* control pipe */
244 int requesttype
, request
, value
, index
, size
;
245 requesttype
= ut
->SetupPacket
[0];
246 request
= ut
->SetupPacket
[1];
247 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
248 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
249 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
250 DPRINTF("-> requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
251 ret
= usb_control_msg(xpcu
->handle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
254 ret
= usb_bulk_read(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
256 ret
= usb_bulk_write(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
258 xpcu_claim(xpcu
, XPCU_RELEASE
);
262 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
264 ut
->dwBytesTransferred
= ret
;
271 int xpcu_set_interface(struct usb_set_interface
*usi
) {
272 struct xpcu_s
*xpcu
= (struct xpcu_s
*)usi
->dwUniqueID
;
279 xpcu
->handle
= usb_open(xpcu
->dev
);
282 usb_reset(xpcu
->handle
);
283 xpcu
->handle
= usb_open(xpcu
->dev
);
288 xpcu
->interface
= xpcu
->dev
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
289 xpcu
->alternate
= usi
->dwAlternateSetting
;
295 static void xpcu_init(void) {
303 busses
= usb_get_busses();
307 int xpcu_find(struct event
*e
) {
308 struct xpcu_event_s
*xpcu_event
= NULL
;
309 struct xpcu_s
*xpcu
= NULL
;
312 int busnum
= -1, devnum
= -1;
315 e
->handle
= (unsigned long)NULL
;
319 usbdev
= getenv("XILINX_USB_DEV");
320 if (usbdev
!= NULL
) {
322 char *devstr
= NULL
, *remainder
;
325 devpos
= strdup(usbdev
);
329 DPRINTF("XILINX_USB_DEV=%s\n", devpos
);
331 for (j
= 0; j
< strlen(devpos
) && devpos
[j
] != 0; j
++) {
332 if (devpos
[j
] == ':') {
334 devstr
= &(devpos
[j
+1]);
338 if (devstr
&& strlen(devstr
) > 0) {
339 busnum
= strtol(devpos
, &remainder
, 10);
340 if (devpos
== remainder
) {
343 devnum
= strtol(devstr
, &remainder
, 10);
344 if (devstr
== remainder
) {
348 fprintf(stderr
,"Using XILINX platform cable USB at %03d:%03d\n",
356 xpcu_event
= malloc(sizeof(struct xpcu_event_s
));
360 bzero(xpcu_event
, sizeof(struct xpcu_event_s
));
361 xpcu_event
->xpcu
= NULL
;
362 xpcu_event
->count
= 0;
363 xpcu_event
->interrupt_count
= 0;
364 pthread_mutex_init(&xpcu_event
->interrupt
, NULL
);
366 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
368 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
369 e
->matchTables
[i
].VendorId
,
370 e
->matchTables
[i
].ProductId
,
371 e
->matchTables
[i
].bDeviceClass
,
372 e
->matchTables
[i
].bDeviceSubClass
,
373 e
->matchTables
[i
].bInterfaceClass
,
374 e
->matchTables
[i
].bInterfaceSubClass
,
375 e
->matchTables
[i
].bInterfaceProtocol
);
377 for (bus
= busses
; bus
; bus
= bus
->next
) {
378 struct usb_device
*dev
;
380 if ((devnum
!= -1) && (strtol(bus
->dirname
, NULL
, 10) != busnum
))
383 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
384 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
386 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
387 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
388 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
389 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
) &&
390 ((devnum
== -1) || (strtol(dev
->filename
, NULL
, 10) == devnum
)) ) {
392 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
393 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
396 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
398 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
399 interface
->altsetting
[i
].bInterfaceClass
,
400 interface
->altsetting
[i
].bInterfaceSubClass
,
401 interface
->altsetting
[i
].bInterfaceProtocol
);
403 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
404 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
405 int n
= xpcu_event
->count
;
407 /* TODO: check interfaceClass! */
408 DPRINTF("found device with libusb\n");
410 xpcu
= realloc(xpcu
, sizeof(struct xpcu_s
) * (++xpcu_event
->count
));
416 bzero(&(xpcu
[n
]), sizeof(struct xpcu_s
));
417 xpcu
[n
].interface
= -1;
418 xpcu
[n
].alternate
= -1;
420 xpcu
[n
].card_type
= e
->dwCardType
;
422 xpcu_event
->xpcu
= xpcu
;
431 e
->handle
= (unsigned long)xpcu_event
;
436 int xpcu_found(struct event
*e
) {
437 struct xpcu_event_s
*xpcu_event
= (struct xpcu_event_s
*)e
->handle
;
438 struct xpcu_s
*xpcu
= NULL
;
440 if (xpcu_event
&& xpcu_event
->count
&& (xpcu_event
->interrupt_count
<= xpcu_event
->count
))
441 xpcu
= &(xpcu_event
->xpcu
[xpcu_event
->interrupt_count
-1]);
443 if (xpcu
&& xpcu
->dev
) {
444 struct usb_interface
*interface
= xpcu
->dev
->config
->interface
;
446 e
->dwCardType
= xpcu
->card_type
;
449 e
->u
.Usb
.dwUniqueID
= (unsigned long)xpcu
;
450 e
->matchTables
[0].VendorId
= xpcu
->dev
->descriptor
.idVendor
;
451 e
->matchTables
[0].ProductId
= xpcu
->dev
->descriptor
.idProduct
;
452 e
->matchTables
[0].bDeviceClass
= xpcu
->dev
->descriptor
.bDeviceClass
;
453 e
->matchTables
[0].bDeviceSubClass
= xpcu
->dev
->descriptor
.bDeviceSubClass
;
454 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
455 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
456 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
462 int xpcu_close(struct event
*e
) {
463 struct xpcu_event_s
*xpcu_event
= (struct xpcu_event_s
*)e
->handle
;
472 for (i
= 0; i
< xpcu_event
->count
; i
++) {
473 xpcu
= &(xpcu_event
->xpcu
[i
]);
475 xpcu_claim(xpcu
, XPCU_RELEASE
);
476 usb_close(xpcu
->handle
);
480 if (xpcu_event
->xpcu
)
481 free(xpcu_event
->xpcu
);
490 int xpcu_int_state(struct interrupt
*it
, int enable
) {
491 struct xpcu_event_s
*xpcu_event
= (struct xpcu_event_s
*)it
->hInterrupt
;
492 pthread_mutex_t
*interrupt
= &dummy_interrupt
;
495 interrupt
= &xpcu_event
->interrupt
;
497 if (enable
== ENABLE_INTERRUPT
) {
501 pthread_mutex_trylock(interrupt
);
505 if (pthread_mutex_trylock(interrupt
) == EBUSY
)
506 pthread_mutex_unlock(interrupt
);
512 int xpcu_int_wait(struct interrupt
*it
) {
513 struct xpcu_event_s
*xpcu_event
= (struct xpcu_event_s
*)it
->hInterrupt
;
516 if (it
->dwCounter
< xpcu_event
->count
) {
519 pthread_mutex_lock(&xpcu_event
->interrupt
);
520 pthread_mutex_unlock(&xpcu_event
->interrupt
);
522 xpcu_event
->interrupt_count
++;
524 pthread_mutex_lock(&dummy_interrupt
);
525 pthread_mutex_unlock(&dummy_interrupt
);