9 #include "usb-driver.h"
12 static struct usb_bus
*busses
= NULL
;
13 static pthread_mutex_t dummy_interrupt
= PTHREAD_MUTEX_INITIALIZER
;
15 int xpcu_deviceinfo(struct xpcu_s
*xpcu
, struct usb_get_device_data
*ugdd
) {
18 unsigned char *buf
= NULL
;
19 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
20 WDU_INTERFACE
**pActiveInterface
;
22 if (ugdd
->dwUniqueID
!= (unsigned long)xpcu
)
29 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
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
;
46 /* TODO: Fix Pipe0! */
47 udi
->Pipe0
.dwNumber
= 0x00;
48 udi
->Pipe0
.dwMaximumPacketSize
= xpcu
->dev
->descriptor
.bMaxPacketSize0
;
50 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
51 udi
->Pipe0
.dwInterval
= 0;
53 pConfigs
= &(udi
->pConfigs
);
54 pActiveConfig
= &(udi
->pActiveConfig
);
55 pActiveInterface
= &(udi
->pActiveInterface
[0]);
58 len
= sizeof(struct usb_device_info
);
60 for (i
=0; i
<xpcu
->dev
->descriptor
.bNumConfigurations
; i
++)
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
];
68 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
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
;
82 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
84 pInterfaces
= &(cfg
->pInterfaces
);
86 len
+= sizeof(WDU_CONFIGURATION
);
89 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
90 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
91 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
93 pActiveInterface
[j
] = iface
;
95 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
96 iface
->dwNumAltSettings
= xpcu
->dev
->config
[i
].interface
[j
].num_altsetting
;
97 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
99 len
+= sizeof(WDU_INTERFACE
);
102 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
105 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
107 struct usb_interface
*interface
= &xpcu
->dev
->config
[i
].interface
[j
];
110 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
112 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
115 for(k
=0; k
<interface
->num_altsetting
; k
++)
117 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
118 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
119 WDU_PIPE_INFO
**pPipes
;
122 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
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
);
137 len
+=sizeof(WDU_ALTERNATE_SETTING
);
140 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
141 for (l
= 0; l
< bNumEndpoints
; l
++) {
142 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
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
;
151 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
154 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
155 for (l
= 0; l
< bNumEndpoints
; l
++) {
156 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
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
;
165 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
168 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
170 len
+= sizeof(WDU_PIPE_INFO
);
173 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
182 static int xpcu_claim(struct xpcu_s
*xpcu
, int claim
) {
184 static int claimed
= 0;
186 if (xpcu
->interface
< 0)
189 if (claim
== XPCU_CLAIM
) {
193 ret
= usb_claim_interface(xpcu
->handle
, xpcu
->interface
);
196 ret
= usb_set_altinterface(xpcu
->handle
, xpcu
->alternate
);
198 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
200 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
201 xpcu
->interface
, ret
, usb_strerror());
207 ret
= usb_release_interface(xpcu
->handle
, xpcu
->interface
);
215 int xpcu_transfer(struct xpcu_s
*xpcu
, struct usb_transfer
*ut
) {
218 if (ut
->dwUniqueID
!= (unsigned long)xpcu
)
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);
230 DPRINTF("-> requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
231 ret
= usb_control_msg(xpcu
->handle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
234 ret
= usb_bulk_read(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
236 ret
= usb_bulk_write(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
238 xpcu_claim(xpcu
, XPCU_RELEASE
);
242 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
244 ut
->dwBytesTransferred
= ret
;
251 void xpcu_set_interface(struct xpcu_s
*xpcu
, struct usb_set_interface
*usi
) {
252 if (usi
->dwUniqueID
!= (unsigned long)xpcu
)
257 xpcu
->handle
= usb_open(xpcu
->dev
);
260 usb_reset(xpcu
->handle
);
261 xpcu
->handle
= usb_open(xpcu
->dev
);
266 xpcu
->interface
= xpcu
->dev
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
267 xpcu
->alternate
= usi
->dwAlternateSetting
;
271 static void xpcu_init(void) {
279 busses
= usb_get_busses();
283 struct xpcu_s
*xpcu_find(struct event
*e
) {
284 static struct xpcu_s xpcu
;
287 int busnum
= -1, devnum
= -1;
290 bzero(&xpcu
, sizeof(xpcu
));
293 devpos
= getenv("XILINX_USB_DEV");
294 if (devpos
!= NULL
) {
296 char *devstr
= NULL
, *remainder
;
298 DPRINTF("XILINX_USB_DEV=%s\n", devpos
);
300 for (j
= 0; j
< strlen(devpos
) && devpos
[j
] != 0; j
++) {
301 if (devpos
[j
] == ':') {
303 devstr
= &(devpos
[j
+1]);
307 if (devstr
&& strlen(devstr
) > 0) {
308 busnum
= strtol(devpos
, &remainder
, 10);
309 if (devpos
== remainder
) {
312 devnum
= strtol(devstr
, &remainder
, 10);
313 if (devstr
== remainder
) {
317 fprintf(stderr
,"Using XILINX platform cable USB at %03d:%03d\n",
324 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
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
);
335 for (bus
= busses
; bus
; bus
= bus
->next
) {
336 struct usb_device
*dev
;
338 if ((devnum
!= -1) && (strtol(bus
->dirname
, NULL
, 10) != busnum
))
341 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
342 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
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
)) ) {
350 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
351 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
354 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
356 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
357 interface
->altsetting
[i
].bInterfaceClass
,
358 interface
->altsetting
[i
].bInterfaceSubClass
,
359 interface
->altsetting
[i
].bInterfaceProtocol
);
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");
369 xpcu
.card_type
= e
->dwCardType
;
370 pthread_mutex_init(&xpcu
.interrupt
, NULL
);
371 e
->handle
= (unsigned long)&xpcu
;
388 void 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
;
392 e
->dwCardType
= xpcu
->card_type
;
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
;
406 void xpcu_close(struct xpcu_s
*xpcu
, struct event
*e
) {
407 if (e
->handle
!= (unsigned long)xpcu
)
412 xpcu_claim(xpcu
, XPCU_RELEASE
);
413 usb_close(xpcu
->handle
);
417 xpcu
->interface
= -1;
418 xpcu
->alternate
= -1;
423 void xpcu_int_state(struct xpcu_s
*xpcu
, struct interrupt
*it
, int enable
) {
424 static pthread_mutex_t
*interrupt
= &dummy_interrupt
;
426 if (it
->hInterrupt
!= (unsigned long)xpcu
)
430 interrupt
= &xpcu
->interrupt
;
432 if (enable
== ENABLE_INTERRUPT
) {
435 pthread_mutex_trylock(interrupt
);
439 if (pthread_mutex_trylock(interrupt
) == EBUSY
)
440 pthread_mutex_unlock(interrupt
);
444 void xpcu_int_wait(struct xpcu_s
*xpcu
, struct interrupt
*it
) {
445 if (it
->hInterrupt
!= (unsigned long)xpcu
)
449 if (it
->dwCounter
== 0) {
452 pthread_mutex_lock(&xpcu
->interrupt
);
453 pthread_mutex_unlock(&xpcu
->interrupt
);
456 pthread_mutex_lock(&dummy_interrupt
);
457 pthread_mutex_unlock(&dummy_interrupt
);