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(); 
 300 int xpcu_find(struct event 
*e
) { 
 301         struct xpcu_s 
*xpcu 
= NULL
; 
 304         int busnum 
= -1, devnum 
= -1; 
 309         devpos 
= getenv("XILINX_USB_DEV"); 
 310         if (devpos 
!= NULL
) { 
 312                 char *devstr 
= NULL
, *remainder
; 
 314                 DPRINTF("XILINX_USB_DEV=%s\n", devpos
); 
 316                 for (j 
= 0; j 
< strlen(devpos
) && devpos
[j
] != 0; j
++) { 
 317                         if (devpos
[j
] == ':') { 
 319                                 devstr 
= &(devpos
[j
+1]); 
 323                 if (devstr 
&& strlen(devstr
) > 0) { 
 324                         busnum 
= strtol(devpos
, &remainder
, 10); 
 325                         if (devpos 
== remainder
) { 
 328                                 devnum 
= strtol(devstr
, &remainder
, 10); 
 329                                 if (devstr 
== remainder
) { 
 333                                         fprintf(stderr
,"Using XILINX platform cable USB at %03d:%03d\n", 
 340         for (i 
= 0; i 
< e
->dwNumMatchTables
; i
++) { 
 342                 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", 
 343                                 e
->matchTables
[i
].VendorId
, 
 344                                 e
->matchTables
[i
].ProductId
, 
 345                                 e
->matchTables
[i
].bDeviceClass
, 
 346                                 e
->matchTables
[i
].bDeviceSubClass
, 
 347                                 e
->matchTables
[i
].bInterfaceClass
, 
 348                                 e
->matchTables
[i
].bInterfaceSubClass
, 
 349                                 e
->matchTables
[i
].bInterfaceProtocol
); 
 351                 for (bus 
= busses
; bus
; bus 
= bus
->next
) { 
 352                         struct usb_device 
*dev
; 
 354                         if ((devnum 
!= -1) && (strtol(bus
->dirname
, NULL
, 10) != busnum
)) 
 357                         for (dev 
= bus
->devices
; dev
; dev 
= dev
->next
) { 
 358                                 struct usb_device_descriptor 
*desc 
= &(dev
->descriptor
); 
 360                                 if((desc
->idVendor 
== e
->matchTables
[i
].VendorId
) && 
 361                                                 (desc
->idProduct 
== e
->matchTables
[i
].ProductId
) && 
 362                                                 (desc
->bDeviceClass 
== e
->matchTables
[i
].bDeviceClass
) && 
 363                                                 (desc
->bDeviceSubClass 
== e
->matchTables
[i
].bDeviceSubClass
) && 
 364                                                 ((devnum 
== -1) || (strtol(dev
->filename
, NULL
, 10) == devnum
)) ) { 
 366                                         for (ac 
= 0; ac 
< desc
->bNumConfigurations
; ac
++) { 
 367                                                 struct usb_interface 
*interface 
= dev
->config
[ac
].interface
; 
 370                                                 for (ai 
= 0; ai 
< interface
->num_altsetting
; ai
++) { 
 372                                                         DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n", 
 373                                                                         interface
->altsetting
[i
].bInterfaceClass
, 
 374                                                                         interface
->altsetting
[i
].bInterfaceSubClass
, 
 375                                                                         interface
->altsetting
[i
].bInterfaceProtocol
); 
 377                                                         if ((interface
->altsetting
[ai
].bInterfaceSubClass 
== e
->matchTables
[i
].bInterfaceSubClass
) && 
 378                                                                         (interface
->altsetting
[ai
].bInterfaceProtocol 
== e
->matchTables
[i
].bInterfaceProtocol
)){ 
 379                                                                 /* TODO: check interfaceClass! */ 
 380                                                                 DPRINTF("found device with libusb\n"); 
 382                                                                 xpcu 
= malloc(sizeof(struct xpcu_s
)); 
 386                                                                 bzero(xpcu
, sizeof(struct xpcu_s
)); 
 387                                                                 xpcu
->interface 
= -1; 
 388                                                                 xpcu
->alternate 
= -1; 
 390                                                                 xpcu
->card_type 
= e
->dwCardType
; 
 391                                                                 pthread_mutex_init(&xpcu
->interrupt
, NULL
); 
 392                                                                 e
->handle 
= (unsigned long)&xpcu
; 
 401         e
->handle 
= (unsigned long)xpcu
; 
 406 int xpcu_found(struct event 
*e
) { 
 407         struct xpcu_s 
*xpcu 
= (struct xpcu_s
*)e
->handle
; 
 409         if (xpcu 
&& xpcu
->dev
) { 
 410                 struct usb_interface 
*interface 
= xpcu
->dev
->config
->interface
; 
 412                 e
->dwCardType 
= xpcu
->card_type
; 
 415                 e
->u
.Usb
.dwUniqueID 
= e
->handle
; 
 416                 e
->matchTables
[0].VendorId 
= xpcu
->dev
->descriptor
.idVendor
; 
 417                 e
->matchTables
[0].ProductId 
= xpcu
->dev
->descriptor
.idProduct
; 
 418                 e
->matchTables
[0].bDeviceClass 
= xpcu
->dev
->descriptor
.bDeviceClass
; 
 419                 e
->matchTables
[0].bDeviceSubClass 
= xpcu
->dev
->descriptor
.bDeviceSubClass
; 
 420                 e
->matchTables
[0].bInterfaceClass 
= interface
->altsetting
[0].bInterfaceClass
; 
 421                 e
->matchTables
[0].bInterfaceSubClass 
= interface
->altsetting
[0].bInterfaceSubClass
; 
 422                 e
->matchTables
[0].bInterfaceProtocol 
= interface
->altsetting
[0].bInterfaceProtocol
; 
 428 int xpcu_close(struct event 
*e
) { 
 429         struct xpcu_s 
*xpcu 
= (struct xpcu_s
*)e
->handle
; 
 436                         xpcu_claim(xpcu
, XPCU_RELEASE
); 
 437                         usb_close(xpcu
->handle
); 
 447 int xpcu_int_state(struct interrupt 
*it
, int enable
) { 
 448         struct xpcu_s 
*xpcu 
= (struct xpcu_s
*)it
->hInterrupt
; 
 449         pthread_mutex_t 
*interrupt 
= &dummy_interrupt
; 
 452                 interrupt 
= &xpcu
->interrupt
; 
 454         if (enable 
== ENABLE_INTERRUPT
) { 
 457                 pthread_mutex_trylock(interrupt
); 
 461                 if (pthread_mutex_trylock(interrupt
) == EBUSY
) 
 462                         pthread_mutex_unlock(interrupt
); 
 468 int xpcu_int_wait(struct interrupt 
*it
) { 
 469         struct xpcu_s 
*xpcu 
= (struct xpcu_s
*)it
->hInterrupt
; 
 471         if (it
->hInterrupt 
!= (unsigned long)xpcu
) 
 475                 if (it
->dwCounter 
== 0) { 
 478                         pthread_mutex_lock(&xpcu
->interrupt
); 
 479                         pthread_mutex_unlock(&xpcu
->interrupt
); 
 482                 pthread_mutex_lock(&dummy_interrupt
); 
 483                 pthread_mutex_unlock(&dummy_interrupt
);