יום שלישי, 11 ביוני 2013

Enumerate USB Device interfaces

I wanted to enumerate the USB FX2 device interface endpoints.
I used the following code:

  1: struct fx2_device_type
  2: {
  3:   struct usb_interface *interface;   
  4:   struct usb_device * usbdev ;
  5: };
  6: 
  7: struct fx2_device_type *fx2_device;
  8: 
  9: static struct usb_device_id ml_table [] = {
 10:     { USB_DEVICE(ML_VENDOR_ID, ML_PRODUCT_ID) },
 11:     { }
 12: };
 13: 
 14: MODULE_DEVICE_TABLE(usb, ml_table);
 15: 
 16: void printHelper (char *pValueToPrint , char * description )
 17: {
 18: if (pValueToPrint != NULL )
 19:   {
 20:     printk(KERN_INFO "%s :%s",description,pValueToPrint);
 21:   }
 22: }
 23: static int ml_probe(struct usb_interface *interface, const struct usb_device_id  *id)
 24: {
 25:   char *product;
 26:   char *manufacturer;
 27:   char *serial;
 28:     /* called when a USB device is connected to the computer. */
 29:     printk(KERN_INFO "ml_probe");
 30:     struct usb_device *udev = interface_to_usbdev(interface);
 31:     
 32:     /* static strings from the device */
 33:   product = udev->product;
 34:   printHelper (product , "product");
 35:   
 36:   manufacturer = udev->manufacturer;
 37:   printHelper (manufacturer , "manufacturer");
 38:   
 39:   serial = udev->serial;
 40:   printHelper (serial , "serial");
 41:     
 42:   struct usb_host_interface *iface_desc;
 43: 
 44:   struct usb_endpoint_descriptor *endpoint;
 45: 
 46:   int retval = -ENOMEM;
 47: 
 48:   fx2_device = kzalloc(sizeof(struct fx2_device_type), GFP_KERNEL);
 49: 
 50:   fx2_device->interface = interface;
 51: 
 52:   fx2_device->usbdev = usb_get_dev(interface_to_usbdev(interface));
 53: 
 54:   iface_desc = interface->cur_altsetting;
 55: 
 56:   printk("The no of endpoints %d \n" , iface_desc->desc.bNumEndpoints);
 57:    
 58:   int i = 0; 
 59: 
 60:   for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
 61: 
 62:   endpoint = &iface_desc->endpoint[i].desc;
 63: 
 64:   int  theEnpointType = usb_endpoint_type(endpoint);
 65: 
 66:     switch ( theEnpointType )
 67:     {
 68:       case USB_ENDPOINT_XFER_CONTROL:
 69:         printk("Endpoint %d Control\n" , i);    
 70:       break;
 71:       case USB_ENDPOINT_XFER_ISOC:
 72:         printk("Endpoint %d ISOC\n" , i);    
 73:       break;
 74:       case USB_ENDPOINT_XFER_BULK:
 75:         printk("Endpoint %d BULK\n" , i);    
 76:       break;
 77:       case USB_ENDPOINT_XFER_INT  :
 78:         printk("Endpoint %d INT\n" , i);    
 79:       break;
 80:       default:
 81:         printk("Endpoint %d out of range\n" , i);  
 82:       break ;
 83:     }
 84: }

And the Result :
[ 7277.566609] ml_probe
[ 7277.566613] product :OSR USB-FX2 LK
[ 7277.566616] manufacturer :OSR.COM
                      The no of endpoints 3
[ 7277.566621] Endpoint 0 INT
[ 7277.566623] Endpoint 1 BULK
[ 7277.566626] Endpoint 2 BULK

אין תגובות:

הוסף רשומת תגובה