9 #include "usb-driver.h"
13 struct usb_device
*dev
;
14 usb_dev_handle
*handle
;
17 unsigned long card_type
;
18 pthread_mutex_t interrupt
;
21 static struct usb_bus
*busses
= NULL
;
22 static pthread_mutex_t dummy_interrupt
= PTHREAD_MUTEX_INITIALIZER
;
24 int xpcu_deviceinfo(struct usb_get_device_data
*ugdd
) {
25 struct xpcu_s
*xpcu
= (struct xpcu_s
*)ugdd
->dwUniqueID
;
28 unsigned char *buf
= NULL
;
29 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
30 WDU_INTERFACE
**pActiveInterface
;
39 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
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
;
56 /* TODO: Fix Pipe0! */
57 udi
->Pipe0
.dwNumber
= 0x00;
58 udi
->Pipe0
.dwMaximumPacketSize
= xpcu
->dev
->descriptor
.bMaxPacketSize0
;
60 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
61 udi
->Pipe0
.dwInterval
= 0;
63 pConfigs
= &(udi
->pConfigs
);
64 pActiveConfig
= &(udi
->pActiveConfig
);
65 pActiveInterface
= &(udi
->pActiveInterface
[0]);
68 len
= sizeof(struct usb_device_info
);
70 for (i
=0; i
<xpcu
->dev
->descriptor
.bNumConfigurations
; i
++)
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
];
78 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
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
;
92 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
94 pInterfaces
= &(cfg
->pInterfaces
);
96 len
+= sizeof(WDU_CONFIGURATION
);
99 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
100 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
101 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
103 pActiveInterface
[j
] = iface
;
105 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
106 iface
->dwNumAltSettings
= xpcu
->dev
->config
[i
].interface
[j
].num_altsetting
;
107 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
109 len
+= sizeof(WDU_INTERFACE
);
112 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
115 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
117 struct usb_interface
*interface
= &xpcu
->dev
->config
[i
].interface
[j
];
120 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
122 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
125 for(k
=0; k
<interface
->num_altsetting
; k
++)
127 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
128 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
129 WDU_PIPE_INFO
**pPipes
;
132 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
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
);
147 len
+=sizeof(WDU_ALTERNATE_SETTING
);
150 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
151 for (l
= 0; l
< bNumEndpoints
; l
++) {
152 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
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
;
161 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
164 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
165 for (l
= 0; l
< bNumEndpoints
; l
++) {
166 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
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
;
175 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
178 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
180 len
+= sizeof(WDU_PIPE_INFO
);
183 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
194 static int xpcu_claim(struct xpcu_s
*xpcu
, int claim
) {
196 static int claimed
= 0;
198 if (xpcu
->interface
< 0)
201 if (claim
== XPCU_CLAIM
) {
205 ret
= usb_claim_interface(xpcu
->handle
, xpcu
->interface
);
208 ret
= usb_set_altinterface(xpcu
->handle
, xpcu
->alternate
);
210 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
212 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
213 xpcu
->interface
, ret
, usb_strerror());
219 ret
= usb_release_interface(xpcu
->handle
, xpcu
->interface
);
227 int xpcu_transfer(struct usb_transfer
*ut
) {
228 struct xpcu_s
*xpcu
= (struct xpcu_s
*)ut
->dwUniqueID
;
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
);
247 ret
= usb_bulk_read(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
249 ret
= usb_bulk_write(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
251 xpcu_claim(xpcu
, XPCU_RELEASE
);
255 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
257 ut
->dwBytesTransferred
= ret
;
264 int xpcu_set_interface(struct usb_set_interface
*usi
) {
265 struct xpcu_s
*xpcu
= (struct xpcu_s
*)usi
->dwUniqueID
;
272 xpcu
->handle
= usb_open(xpcu
->dev
);
275 usb_reset(xpcu
->handle
);
276 xpcu
->handle
= usb_open(xpcu
->dev
);
281 xpcu
->interface
= xpcu
->dev
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
282 xpcu
->alternate
= usi
->dwAlternateSetting
;
288 static void xpcu_init(void) {
296 busses
= usb_get_busses();
298 pthread_mutex_init(&dummy_interrupt
, NULL
);
302 int xpcu_find(struct event
*e
) {
303 struct xpcu_s
*xpcu
= NULL
;
306 int busnum
= -1, devnum
= -1;
311 devpos
= getenv("XILINX_USB_DEV");
312 if (devpos
!= NULL
) {
314 char *devstr
= NULL
, *remainder
;
316 DPRINTF("XILINX_USB_DEV=%s\n", devpos
);
318 for (j
= 0; j
< strlen(devpos
) && devpos
[j
] != 0; j
++) {
319 if (devpos
[j
] == ':') {
321 devstr
= &(devpos
[j
+1]);
325 if (devstr
&& strlen(devstr
) > 0) {
326 busnum
= strtol(devpos
, &remainder
, 10);
327 if (devpos
== remainder
) {
330 devnum
= strtol(devstr
, &remainder
, 10);
331 if (devstr
== remainder
) {
335 fprintf(stderr
,"Using XILINX platform cable USB at %03d:%03d\n",
342 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
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
);
353 for (bus
= busses
; bus
; bus
= bus
->next
) {
354 struct usb_device
*dev
;
356 if ((devnum
!= -1) && (strtol(bus
->dirname
, NULL
, 10) != busnum
))
359 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
360 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
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
)) ) {
368 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
369 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
372 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
374 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
375 interface
->altsetting
[i
].bInterfaceClass
,
376 interface
->altsetting
[i
].bInterfaceSubClass
,
377 interface
->altsetting
[i
].bInterfaceProtocol
);
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");
384 xpcu
= malloc(sizeof(struct xpcu_s
));
388 bzero(xpcu
, sizeof(struct xpcu_s
));
389 xpcu
->interface
= -1;
390 xpcu
->alternate
= -1;
392 xpcu
->card_type
= e
->dwCardType
;
393 pthread_mutex_init(&xpcu
->interrupt
, NULL
);
394 e
->handle
= (unsigned long)&xpcu
;
403 e
->handle
= (unsigned long)xpcu
;
408 int xpcu_found(struct event
*e
) {
409 struct xpcu_s
*xpcu
= (struct xpcu_s
*)e
->handle
;
411 if (xpcu
&& xpcu
->dev
) {
412 struct usb_interface
*interface
= xpcu
->dev
->config
->interface
;
414 e
->dwCardType
= xpcu
->card_type
;
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
;
430 int xpcu_close(struct event
*e
) {
431 struct xpcu_s
*xpcu
= (struct xpcu_s
*)e
->handle
;
438 xpcu_claim(xpcu
, XPCU_RELEASE
);
439 usb_close(xpcu
->handle
);
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
;
454 interrupt
= &xpcu
->interrupt
;
456 if (enable
== ENABLE_INTERRUPT
) {
459 pthread_mutex_trylock(interrupt
);
463 if (pthread_mutex_trylock(interrupt
) == EBUSY
)
464 pthread_mutex_unlock(interrupt
);
470 int xpcu_int_wait(struct interrupt
*it
) {
471 struct xpcu_s
*xpcu
= (struct xpcu_s
*)it
->hInterrupt
;
473 if (it
->hInterrupt
!= (unsigned long)xpcu
)
477 if (it
->dwCounter
== 0) {
480 pthread_mutex_lock(&xpcu
->interrupt
);
481 pthread_mutex_unlock(&xpcu
->interrupt
);
484 pthread_mutex_lock(&dummy_interrupt
);
485 pthread_mutex_unlock(&dummy_interrupt
);