USB Device - Composite - HID + LibUSB

Post
PTAV
New Member
2010/03/06 18:32:32
Hi to all,

I'm trying to make a composite firmware to PIC18F4550 that will communicate to a PC application using LibUSB (Generic Device). Plus, it will also control the mouse cursor (HID Device).

Both Microchip examples (mouse and LibUSB) works perfectly separatedly, but when I try to make them as an unique firmware, the devices are not recognized by the OS (Windows). I saw an example on the forum that works as a Composite - HID device, but for my case is different since I'm trying to use different class devices.

Are there any code examples with this kind of software?
If someone could point me some light regarding this subject I'll be very pleased.

PTAV
New Member
RE: USB Device - Composite - HID + LibUSB 2010/03/06 18:34:47
My descriptors on <usb_descriptors.c>:


/* Device Descriptor */

ROM USB_DEVICE_DESCRIPTOR device_dsc=
{
    0x12,            // Size of this descriptor in bytes
    USB_DESCRIPTOR_DEVICE,  // DEVICE descriptor type
    0x0200,                 // USB Spec Release Number in BCD format
    0x00,                   // Class Code
    0x00,                   // Subclass code
    0x00,                   // Protocol code
    USB_EP0_BUFF_SIZE,      // Max packet size for EP0, see usb_config.h
    MY_VID,                 // Vendor ID
    MY_PID,                 // Product ID: Mouse in a circle fw demo
    0x0003,                 // Device release number in BCD format
    0x01,                   // Manufacturer string index
    0x02,                   // Product string index
    0x00,                   // Device serial number string index
    0x01                    // Number of possible configurations
};

/* Configuration 1 Descriptor */
ROM BYTE configDescriptor1[]={

    /* ----------- Common Descriptor ----------- */
    /* Configuration Descriptor */
    0x09,                           // Size of this descriptor in bytes
    USB_DESCRIPTOR_CONFIGURATION,   // CONFIGURATION descriptor type
    DESC_CONFIG_WORD(0x0039),       // Total length of data for this cfg
    2,                              // Number of interfaces in this cfg
    1,                              // Index value of this configuration
    0,                              // Configuration string index
    _DEFAULT | _SELF,               // Attributes, see usb_device.h
    50,                             // Max power consumption (2X mA)

    /* --------- HID - Mouse Descriptor --------- */

    /* Interface Descriptor */
    0x09,                // Size of this descriptor in bytes
    USB_DESCRIPTOR_INTERFACE,    
      // INTERFACE descriptor type
    0,                              // Interface Number
    0,                              // Alternate Setting Number
    1,                              // Number of endpoints in this intf
    HID_INTF,                       // Class code
    BOOT_INTF_SUBCLASS,             // Subclass code
    HID_PROTOCOL_MOUSE,             // Protocol code
    0,                              // Interface string index

    /* HID Class-Specific Descriptor */
    0x09,                                 // Size of this descriptor in bytes RRoj hack
    DSC_HID,                              // HID descriptor type
    DESC_CONFIG_WORD(0x0111),             // HID Spec Release Number in BCD format (1.11)
    0x00,                                 // Country Code (0x00 for Not supported)
    HID_NUM_OF_DSC,                       // Number of class descriptors, see usbcfg.h
    DSC_RPT,                              // Report descriptor type
    DESC_CONFIG_WORD(HID_RPT01_SIZE),     // Size of the report descriptor

    /* Endpoint Descriptor */
    0x07,
    USB_DESCRIPTOR_ENDPOINT,              //Endpoint Descriptor
    HID_EP | _EP_IN,                      //EndpointAddress
    _INTERRUPT,                           //Attributes
    DESC_CONFIG_WORD(HID_INT_IN_EP_SIZE), //Size
    0x01,                                 //Interval (ms)

    /* --------- LibUSB Descriptor --------- */

    /* Interface Descriptor */
    0x09,                           // Size of this descriptor in bytes
    USB_DESCRIPTOR_INTERFACE,       // INTERFACE descriptor type
    1,                              // Interface Number                  
    0,                              // Alternate Setting Number
    2,                              // Number of endpoints in this intf
    0xFF,                           // Class code
    0xFF,                           // Subclass code
    0xFF,                           // Protocol code
    0,                              // Interface string index              

    /* Endpoint Descriptors */
    0x07,                     
    USB_DESCRIPTOR_ENDPOINT,             //Endpoint Descriptor
    _EP01_OUT,                           //EndpointAddress                      
    _BULK,                               //Attributes
    DESC_CONFIG_WORD(USBGEN_EP_SIZE),    //Size                                  
    1,                                   //Interval (ms)

    0x07,                     
    USB_DESCRIPTOR_ENDPOINT,             //Endpoint Descriptor
    _EP01_IN,                            //EndpointAddress                      
    _BULK,                               //Attributes
    DESC_CONFIG_WORD(USBGEN_EP_SIZE),    //Size                              
    1                                    //Interval (ms)

};

PTAV
New Member
RE: USB Device - Composite - HID + LibUSB 2010/03/06 18:36:05
Definitions on <usb_config.h>:


/** DEFINITIONS ****************************************************/
#define USB_EP0_BUFF_SIZE    8                             
#define USB_MAX_NUM_INT      3   
#define USB_MAX_EP_NUMBER    3

//(...)
 
//#define USB_POLLING
#define USB_INTERRUPT
 
#define MY_VID 0x04D8
#define MY_PID 0x0000
 
#define USB_SUPPORT_DEVICE
 
#define USB_NUM_STRING_DESCRIPTORS 3
 
#define USB_ENABLE_ALL_HANDLERS
 
/** DEVICE CLASS USAGE *********************************************/
#define USB_USE_HID
#define USB_USE_GEN
 
/** ENDPOINTS ALLOCATION *******************************************/
 
/* HID */
#define HID_INTF_ID             0x00
#define HID_EP                  1
#define HID_INT_OUT_EP_SIZE     3
#define HID_INT_IN_EP_SIZE      3
#define HID_NUM_OF_DSC          1
#define HID_RPT01_SIZE          50
 
/* Generic - LibUSB */
#define USBGEN_EP_SIZE          64
#define USBGEN_EP_NUM           2

xiaofan
Super Member
RE: USB Device - Composite - HID + LibUSB 2010/03/07 00:32:09

ORIGINAL: PTAV


   /* --------- LibUSB Descriptor --------- */
   /* Endpoint Descriptors */
   0x07,                     
   USB_DESCRIPTOR_ENDPOINT,             //Endpoint Descriptor
   _EP01_OUT,                           //EndpointAddress                      
   _BULK,                               //Attributes
   DESC_CONFIG_WORD(USBGEN_EP_SIZE),    //Size                                  
   1,                                   //Interval (ms)

   0x07,                     
   USB_DESCRIPTOR_ENDPOINT,             //Endpoint Descriptor
   _EP01_IN,                            //EndpointAddress                      
   _BULK,                               //Attributes
   DESC_CONFIG_WORD(USBGEN_EP_SIZE),    //Size                              
   1                                    //Interval (ms)

};



/* HID */
#define HID_EP 1

/* Generic - LibUSB */
#define USBGEN_EP_NUM 2




Apparently the descriptor of the libusb interface conflict with the endpoint assigned. You should use _EP02_OUT and _EP02_IN for the libusb interface according to your code, right?

You can read the example code of USB composite device HID+MSD in stack V2.6.
PTAV
New Member
RE: USB Device - Composite - HID + LibUSB 2010/03/29 07:04:36
Hi Xiaofan,

Thanks to quick answering my question!
I took a long time to reply this message because I had to solve some problems for another project, but now I`m back to this one.

Both devices are recognized by the OS now. The first goal was achieved: I'm controlling the mouse cursor through an analog joystick connected to 2 A/D pins of my PIC.
Now, I'm trying to exchange some data through the libUSB interface. I made some small alterations on the QT Microchip software (that come with the examples) but couldn't make it work properly yet.

The problem: when I try to set the configuration (with "usb_set_configuration") it returns an error.

I found some interesting info - When I execute a "lsusb" command, to list the descriptors of the usb devices attached to my PC, here is what I get:


Bus 001 Device 008: ID 04d8:0204 Microchip Technology, Inc.
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x04d8 Microchip Technology, Inc.
idProduct 0x0204
bcdDevice 0.03
iManufacturer 1
iProduct 2
iSerial 0
bNumConfigurations 1

Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 57
bNumInterfaces 2
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 100mA

Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 3 Human Interface Device
bInterfaceSubClass 1 Boot Interface Subclass
bInterfaceProtocol 2 Mouse
iInterface 0
** UNRECOGNIZED: 09 21 11 01 00 01 22 32 00

Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0003 1x 3 bytes
bInterval 1

Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 255 Vendor Specific Class
bInterfaceSubClass 255 Vendor Specific Subclass
bInterfaceProtocol 255 Vendor Specific Protocol
iInterface 0

Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x02 EP 2 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1

Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1
Device Status: 0x0001
Self Powered


That line after the mouse interface (** UNRECOGNIZED: 09 21 11 01 00 01 22 32 00) is bothering me. Maybe that's the source of my error. Don't understand it though, since the mouse part is working properly.

Any tips?


Regards.
xiaofan
Super Member
RE: USB Device - Composite - HID + LibUSB 2010/03/29 07:14:54
ORIGINAL: PTAV
That line after the mouse interface (** UNRECOGNIZED: 09 21 11 01 00 01 22 32 00) is bothering me. Maybe that's the source of my error. Don't understand it though, since the mouse part is working properly.


That is not an issue. You have not detach the kernel HID driver so that lsusb (based on libusb) will have issues to get the report descriptor.


xiaofan
Super Member
RE: USB Device - Composite - HID + LibUSB 2010/03/29 07:16:14
ORIGINAL: PTAV
The problem: when I try to set the configuration (with "usb_set_configuration") it returns an error.


You can safely remove it under Linux and it should work. Maybe the HID interface is in the way.

The other way is to detach the kernel HID driver, but then the mouse will not work.
PTAV
New Member
RE: USB Device - Composite - HID + LibUSB 2010/03/30 05:53:42

ORIGINAL: xiaofan
The other way is to detach the kernel HID driver, but then the mouse will not work.


Yeah, when I detach the kernel HID driver the libUSB communication works fine! Smile And obviously, the mouse stops. sad

Well, as a composite device I though it was possible to both devices (HID & libUSB) to work at the same time by selecting the properly interface by software. Am I missing something here? Putting both of them in the same configuration descriptor and claiming the interface isn't enough?

Thanks.
xiaofan
Super Member
RE: USB Device - Composite - HID + LibUSB 2010/03/30 06:03:51
Did you try without detaching the kernel hid driver -- just do not call usb_set_configuration()?
xiaofan
Super Member
RE: USB Device - Composite - HID + LibUSB 2010/03/30 06:12:29
Take note now that you have a composite device, one interface is the mouse, the other interface is the custom device. So you should only claim the custom device. I think it should work.
PTAV
New Member
RE: USB Device - Composite - HID + LibUSB 2010/03/30 10:38:13
Thank you for the advices. I'll make some tests and post the results as soon as I can.
PTAV
New Member
RE: USB Device - Composite - HID + LibUSB 2010/03/31 07:21:10
Hi Xiaofan.

I'm glad to say that everything is working now!

The fact is that I'm dumb: I was using an old version o libusb (v 0.1.4). that doesn't supported composite devices.
I updated the libusb on my Ubuntu for the new and polished version (v1.0). made the necessary changes on my source codes (recoded almost everything again) and now everything is working perfectly!! Smile

As you said. I just need to claim the interface and that's it! Both devices are working smooth!

Thanks again for the support.

Next step: Try to make it work on Windows.
xiaofan
Super Member
RE: USB Device - Composite - HID + LibUSB 2010/03/31 16:06:26
ORIGINAL: PTAV
The fact is that I'm dumb: I was using an old version o libusb (v 0.1.4). that doesn't supported composite devices.


I do not think this is a correct statement. BTW, the actual version is actually 0.1.12.x but Debian/Ubuntu somehow use a different name than upstream.


I updated the libusb on my Ubuntu for the new and polished version (v1.0). made the necessary changes on my source codes (recoded almost everything again) and now everything is working perfectly!! Smile


Yes libusb 1.0 is the way to go. libusb 0.1 is no longer maintained by the developer.
xiaofan
Super Member
RE: USB Device - Composite - HID + LibUSB 2010/03/31 16:08:27
ORIGINAL: PTAV
Next step: Try to make it work on Windows.


Since you already have the libusb 1.0 program works under Linux, it should work under Windows as well with the new libusb 1.0 Windows backend. You can try the pre-release binary snapshot here.

http://www.libusb.org/wiki/windows_backend

You need to use the WinUSB driver for the libusb interface.
PTAV
New Member
RE: USB Device - Composite - HID + LibUSB 2010/04/09 13:42:16
Hi Xiaofan,

Just to let you know that my application is already working on Windows. I used mingw along with the WinUSB driver.
The thing that took most of my time was the QT cross-compilation part, but now everything is ok.

Thanks again!