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
) 
  32                 struct usb_device_info 
*udi 
= (struct usb_device_info
*)(buf
+len
); 
  34                 udi
->Descriptor
.bLength 
= sizeof(WDU_DEVICE_DESCRIPTOR
); 
  35                 udi
->Descriptor
.bDescriptorType 
= xpcu
->dev
->descriptor
.bDescriptorType
; 
  36                 udi
->Descriptor
.bcdUSB 
= xpcu
->dev
->descriptor
.bcdUSB
; 
  37                 udi
->Descriptor
.bDeviceClass 
= xpcu
->dev
->descriptor
.bDeviceClass
; 
  38                 udi
->Descriptor
.bDeviceSubClass 
= xpcu
->dev
->descriptor
.bDeviceSubClass
; 
  39                 udi
->Descriptor
.bDeviceProtocol 
= xpcu
->dev
->descriptor
.bDeviceProtocol
; 
  40                 udi
->Descriptor
.bMaxPacketSize0 
= xpcu
->dev
->descriptor
.bMaxPacketSize0
; 
  41                 udi
->Descriptor
.idVendor 
= xpcu
->dev
->descriptor
.idVendor
; 
  42                 udi
->Descriptor
.idProduct 
= xpcu
->dev
->descriptor
.idProduct
; 
  43                 udi
->Descriptor
.bcdDevice 
= xpcu
->dev
->descriptor
.bcdDevice
; 
  44                 udi
->Descriptor
.iManufacturer 
= xpcu
->dev
->descriptor
.iManufacturer
; 
  45                 udi
->Descriptor
.iProduct 
= xpcu
->dev
->descriptor
.iProduct
; 
  46                 udi
->Descriptor
.iSerialNumber 
= xpcu
->dev
->descriptor
.iSerialNumber
; 
  47                 udi
->Descriptor
.bNumConfigurations 
= xpcu
->dev
->descriptor
.bNumConfigurations
; 
  49                 /* TODO: Fix Pipe0! */ 
  50                 udi
->Pipe0
.dwNumber 
= 0x00; 
  51                 udi
->Pipe0
.dwMaximumPacketSize 
= xpcu
->dev
->descriptor
.bMaxPacketSize0
; 
  53                 udi
->Pipe0
.direction 
= WDU_DIR_IN_OUT
; 
  54                 udi
->Pipe0
.dwInterval 
= 0; 
  56                 pConfigs 
= &(udi
->pConfigs
); 
  57                 pActiveConfig 
= &(udi
->pActiveConfig
); 
  58                 pActiveInterface 
= &(udi
->pActiveInterface
[0]); 
  61         len 
= sizeof(struct usb_device_info
); 
  63         for (i
=0; i
<xpcu
->dev
->descriptor
.bNumConfigurations
; i
++) 
  65                 struct usb_config_descriptor 
*conf_desc 
= &xpcu
->dev
->config
[i
]; 
  66                 WDU_INTERFACE 
**pInterfaces
; 
  67                 WDU_ALTERNATE_SETTING 
**pAlternateSettings
[conf_desc
->bNumInterfaces
]; 
  68                 WDU_ALTERNATE_SETTING 
**pActiveAltSetting
[conf_desc
->bNumInterfaces
]; 
  71                         WDU_CONFIGURATION 
*cfg 
= (WDU_CONFIGURATION
*)(buf
+len
); 
  76                         cfg
->Descriptor
.bLength 
= conf_desc
->bLength
; 
  77                         cfg
->Descriptor
.bDescriptorType 
= conf_desc
->bDescriptorType
; 
  78                         cfg
->Descriptor
.wTotalLength 
= conf_desc
->wTotalLength
; 
  79                         cfg
->Descriptor
.bNumInterfaces 
= conf_desc
->bNumInterfaces
; 
  80                         cfg
->Descriptor
.bConfigurationValue 
= conf_desc
->bConfigurationValue
; 
  81                         cfg
->Descriptor
.iConfiguration 
= conf_desc
->iConfiguration
; 
  82                         cfg
->Descriptor
.bmAttributes 
= conf_desc
->bmAttributes
; 
  83                         cfg
->Descriptor
.MaxPower 
= conf_desc
->MaxPower
; 
  85                         cfg
->dwNumInterfaces 
= conf_desc
->bNumInterfaces
; 
  87                         pInterfaces 
= &(cfg
->pInterfaces
); 
  89                 len 
+= sizeof(WDU_CONFIGURATION
); 
  92                         *pInterfaces 
= (WDU_INTERFACE
*)(buf
+len
); 
  93                         for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) { 
  94                                 WDU_INTERFACE 
*iface 
= (WDU_INTERFACE
*)(buf
+len
); 
  96                                 pActiveInterface
[j
] = iface
; 
  98                                 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
); 
  99                                 iface
->dwNumAltSettings 
= xpcu
->dev
->config
[i
].interface
[j
].num_altsetting
; 
 100                                 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
); 
 102                                 len 
+= sizeof(WDU_INTERFACE
); 
 105                         len 
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
; 
 108                 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) 
 110                         struct usb_interface 
*interface 
= &xpcu
->dev
->config
[i
].interface
[j
]; 
 113                                 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
); 
 115                                 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
); 
 118                         for(k
=0; k
<interface
->num_altsetting
; k
++) 
 120                                 unsigned char bNumEndpoints 
= interface
->altsetting
[k
].bNumEndpoints
; 
 121                                 WDU_ENDPOINT_DESCRIPTOR 
**pEndpointDescriptors
; 
 122                                 WDU_PIPE_INFO 
**pPipes
; 
 125                                         WDU_ALTERNATE_SETTING 
*altset 
= (WDU_ALTERNATE_SETTING
*)(buf
+len
); 
 127                                         altset
->Descriptor
.bLength 
= interface
->altsetting
[k
].bLength
; 
 128                                         altset
->Descriptor
.bDescriptorType 
= interface
->altsetting
[k
].bDescriptorType
; 
 129                                         altset
->Descriptor
.bInterfaceNumber 
= interface
->altsetting
[k
].bInterfaceNumber
; 
 130                                         altset
->Descriptor
.bAlternateSetting 
= interface
->altsetting
[k
].bAlternateSetting
; 
 131                                         altset
->Descriptor
.bNumEndpoints 
= interface
->altsetting
[k
].bNumEndpoints
; 
 132                                         altset
->Descriptor
.bInterfaceClass 
= interface
->altsetting
[k
].bInterfaceClass
; 
 133                                         altset
->Descriptor
.bInterfaceSubClass 
= interface
->altsetting
[k
].bInterfaceSubClass
; 
 134                                         altset
->Descriptor
.bInterfaceProtocol 
= interface
->altsetting
[k
].bInterfaceProtocol
; 
 135                                         altset
->Descriptor
.iInterface 
= interface
->altsetting
[k
].iInterface
; 
 136                                         pEndpointDescriptors 
= &(altset
->pEndpointDescriptors
); 
 137                                         pPipes 
= &(altset
->pPipes
); 
 140                                 len 
+=sizeof(WDU_ALTERNATE_SETTING
); 
 143                                         *pEndpointDescriptors 
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
); 
 144                                         for (l 
= 0; l 
< bNumEndpoints
; l
++) { 
 145                                                 WDU_ENDPOINT_DESCRIPTOR 
*ed 
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
); 
 147                                                 ed
->bLength 
= interface
->altsetting
[k
].endpoint
[l
].bLength
; 
 148                                                 ed
->bDescriptorType 
= interface
->altsetting
[k
].endpoint
[l
].bDescriptorType
; 
 149                                                 ed
->bEndpointAddress 
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
; 
 150                                                 ed
->bmAttributes 
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
; 
 151                                                 ed
->wMaxPacketSize 
= interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
; 
 152                                                 ed
->bInterval 
= interface
->altsetting
[k
].endpoint
[l
].bInterval
; 
 154                                                 len 
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
); 
 157                                         *pPipes 
= (WDU_PIPE_INFO
*)(buf
+len
); 
 158                                         for (l 
= 0; l 
< bNumEndpoints
; l
++) { 
 159                                                 WDU_PIPE_INFO 
*pi 
= (WDU_PIPE_INFO
*)(buf
+len
); 
 161                                                 pi
->dwNumber 
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
; 
 162                                                 pi
->dwMaximumPacketSize 
= WDU_GET_MAX_PACKET_SIZE(interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
); 
 163                                                 pi
->type 
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes 
& USB_ENDPOINT_TYPE_MASK
; 
 164                                                 if (pi
->type 
== PIPE_TYPE_CONTROL
) 
 165                                                         pi
->direction 
= WDU_DIR_IN_OUT
; 
 168                                                         pi
->direction 
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress 
& USB_ENDPOINT_DIR_MASK 
?  WDU_DIR_IN 
: WDU_DIR_OUT
; 
 171                                                 pi
->dwInterval 
= interface
->altsetting
[k
].endpoint
[l
].bInterval
; 
 173                                                 len 
+= sizeof(WDU_PIPE_INFO
); 
 176                                         len 
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
; 
 187 static int xpcu_claim(struct xpcu_s 
*xpcu
, int claim
) { 
 189         static int claimed 
= 0; 
 191         if (xpcu
->interface 
< 0) 
 194         if (claim 
== XPCU_CLAIM
) { 
 198                 ret 
= usb_claim_interface(xpcu
->handle
, xpcu
->interface
); 
 201                         ret 
= usb_set_altinterface(xpcu
->handle
, xpcu
->alternate
); 
 203                                 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
); 
 205                         fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n", 
 206                                         xpcu
->interface
, ret
, usb_strerror()); 
 212                 ret 
= usb_release_interface(xpcu
->handle
, xpcu
->interface
); 
 220 int xpcu_transfer(struct xpcu_s 
*xpcu
, struct usb_transfer 
*ut
) { 
 223         if (ut
->dwUniqueID 
!= (unsigned long)xpcu
) 
 229         xpcu_claim(xpcu
, XPCU_CLAIM
); 
 230         /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */ 
 231         if (ut
->dwPipeNum 
== 0) { /* control pipe */ 
 232                 int requesttype
, request
, value
, index
, size
; 
 233                 requesttype 
= ut
->SetupPacket
[0]; 
 234                 request 
= ut
->SetupPacket
[1]; 
 235                 value 
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8); 
 236                 index 
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8); 
 237                 size 
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8); 
 238                 DPRINTF("-> requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
); 
 239                 ret 
= usb_control_msg(xpcu
->handle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
); 
 242                         ret 
= usb_bulk_read(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
); 
 244                         ret 
= usb_bulk_write(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
); 
 246                 xpcu_claim(xpcu
, XPCU_RELEASE
); 
 250                 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror()); 
 252                 ut
->dwBytesTransferred 
= ret
; 
 259 int xpcu_set_interface(struct xpcu_s 
*xpcu
, struct usb_set_interface 
*usi
) { 
 260         if (usi
->dwUniqueID 
!= (unsigned long)xpcu
) 
 268                         xpcu
->handle 
= usb_open(xpcu
->dev
); 
 271                                 usb_reset(xpcu
->handle
); 
 272                                 xpcu
->handle 
= usb_open(xpcu
->dev
); 
 277                 xpcu
->interface 
= xpcu
->dev
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
; 
 278                 xpcu
->alternate 
= usi
->dwAlternateSetting
; 
 284 static void xpcu_init(void) { 
 292         busses 
= usb_get_busses(); 
 296 struct xpcu_s 
*xpcu_find(struct event 
*e
) { 
 297         static struct xpcu_s xpcu
; 
 300         int busnum 
= -1, devnum 
= -1; 
 303         bzero(&xpcu
, sizeof(xpcu
)); 
 306         devpos 
= getenv("XILINX_USB_DEV"); 
 307         if (devpos 
!= NULL
) { 
 309                 char *devstr 
= NULL
, *remainder
; 
 311                 DPRINTF("XILINX_USB_DEV=%s\n", devpos
); 
 313                 for (j 
= 0; j 
< strlen(devpos
) && devpos
[j
] != 0; j
++) { 
 314                         if (devpos
[j
] == ':') { 
 316                                 devstr 
= &(devpos
[j
+1]); 
 320                 if (devstr 
&& strlen(devstr
) > 0) { 
 321                         busnum 
= strtol(devpos
, &remainder
, 10); 
 322                         if (devpos 
== remainder
) { 
 325                                 devnum 
= strtol(devstr
, &remainder
, 10); 
 326                                 if (devstr 
== remainder
) { 
 330                                         fprintf(stderr
,"Using XILINX platform cable USB at %03d:%03d\n", 
 337         for (i 
= 0; i 
< e
->dwNumMatchTables
; i
++) { 
 339                 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", 
 340                                 e
->matchTables
[i
].VendorId
, 
 341                                 e
->matchTables
[i
].ProductId
, 
 342                                 e
->matchTables
[i
].bDeviceClass
, 
 343                                 e
->matchTables
[i
].bDeviceSubClass
, 
 344                                 e
->matchTables
[i
].bInterfaceClass
, 
 345                                 e
->matchTables
[i
].bInterfaceSubClass
, 
 346                                 e
->matchTables
[i
].bInterfaceProtocol
); 
 348                 for (bus 
= busses
; bus
; bus 
= bus
->next
) { 
 349                         struct usb_device 
*dev
; 
 351                         if ((devnum 
!= -1) && (strtol(bus
->dirname
, NULL
, 10) != busnum
)) 
 354                         for (dev 
= bus
->devices
; dev
; dev 
= dev
->next
) { 
 355                                 struct usb_device_descriptor 
*desc 
= &(dev
->descriptor
); 
 357                                 if((desc
->idVendor 
== e
->matchTables
[i
].VendorId
) && 
 358                                                 (desc
->idProduct 
== e
->matchTables
[i
].ProductId
) && 
 359                                                 (desc
->bDeviceClass 
== e
->matchTables
[i
].bDeviceClass
) && 
 360                                                 (desc
->bDeviceSubClass 
== e
->matchTables
[i
].bDeviceSubClass
) && 
 361                                                 ((devnum 
== -1) || (strtol(dev
->filename
, NULL
, 10) == devnum
)) ) { 
 363                                         for (ac 
= 0; ac 
< desc
->bNumConfigurations
; ac
++) { 
 364                                                 struct usb_interface 
*interface 
= dev
->config
[ac
].interface
; 
 367                                                 for (ai 
= 0; ai 
< interface
->num_altsetting
; ai
++) { 
 369                                                         DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n", 
 370                                                                         interface
->altsetting
[i
].bInterfaceClass
, 
 371                                                                         interface
->altsetting
[i
].bInterfaceSubClass
, 
 372                                                                         interface
->altsetting
[i
].bInterfaceProtocol
); 
 374                                                         if ((interface
->altsetting
[ai
].bInterfaceSubClass 
== e
->matchTables
[i
].bInterfaceSubClass
) && 
 375                                                                         (interface
->altsetting
[ai
].bInterfaceProtocol 
== e
->matchTables
[i
].bInterfaceProtocol
)){ 
 376                                                                 /* TODO: check interfaceClass! */ 
 377                                                                 DPRINTF("found device with libusb\n"); 
 382                                                                 xpcu
.card_type 
= e
->dwCardType
; 
 383                                                                 pthread_mutex_init(&xpcu
.interrupt
, NULL
); 
 384                                                                 e
->handle 
= (unsigned long)&xpcu
; 
 401 int xpcu_found(struct xpcu_s 
*xpcu
, struct event 
*e
) { 
 402         if (e
->handle 
&& e
->handle 
== (unsigned long)xpcu 
&& xpcu
->dev
) { 
 403                 struct usb_interface 
*interface 
= xpcu
->dev
->config
->interface
; 
 405                 e
->dwCardType 
= xpcu
->card_type
; 
 408                 e
->u
.Usb
.dwUniqueID 
= e
->handle
; 
 409                 e
->matchTables
[0].VendorId 
= xpcu
->dev
->descriptor
.idVendor
; 
 410                 e
->matchTables
[0].ProductId 
= xpcu
->dev
->descriptor
.idProduct
; 
 411                 e
->matchTables
[0].bDeviceClass 
= xpcu
->dev
->descriptor
.bDeviceClass
; 
 412                 e
->matchTables
[0].bDeviceSubClass 
= xpcu
->dev
->descriptor
.bDeviceSubClass
; 
 413                 e
->matchTables
[0].bInterfaceClass 
= interface
->altsetting
[0].bInterfaceClass
; 
 414                 e
->matchTables
[0].bInterfaceSubClass 
= interface
->altsetting
[0].bInterfaceSubClass
; 
 415                 e
->matchTables
[0].bInterfaceProtocol 
= interface
->altsetting
[0].bInterfaceProtocol
; 
 421 int xpcu_close(struct xpcu_s 
*xpcu
, struct event 
*e
) { 
 422         if (e
->handle 
!= (unsigned long)xpcu
) 
 430                         xpcu_claim(xpcu
, XPCU_RELEASE
); 
 431                         usb_close(xpcu
->handle
); 
 435                 xpcu
->interface 
= -1; 
 436                 xpcu
->alternate 
= -1; 
 443 int xpcu_int_state(struct xpcu_s 
*xpcu
, struct interrupt 
*it
, int enable
) { 
 444         static pthread_mutex_t 
*interrupt 
= &dummy_interrupt
; 
 446         if (it
->hInterrupt 
!= (unsigned long)xpcu
) 
 450                 interrupt 
= &xpcu
->interrupt
; 
 452         if (enable 
== ENABLE_INTERRUPT
) { 
 455                 pthread_mutex_trylock(interrupt
); 
 459                 if (pthread_mutex_trylock(interrupt
) == EBUSY
) 
 460                         pthread_mutex_unlock(interrupt
); 
 466 int xpcu_int_wait(struct xpcu_s 
*xpcu
, struct interrupt 
*it
) { 
 467         if (it
->hInterrupt 
!= (unsigned long)xpcu
) 
 471                 if (it
->dwCounter 
== 0) { 
 474                         pthread_mutex_lock(&xpcu
->interrupt
); 
 475                         pthread_mutex_unlock(&xpcu
->interrupt
); 
 478                 pthread_mutex_lock(&dummy_interrupt
); 
 479                 pthread_mutex_unlock(&dummy_interrupt
);