1. 程式人生 > >Usb Composite Device (audio+hid) Descriptor

Usb Composite Device (audio+hid) Descriptor

近期開發USB Composite Device 撰寫的USB 描述符 ,支援 USB speaker + USB Mic,同時支援USB HID 自定義裝置。可以作為
開發裝置的參考。(此描述符已經經過實戰驗證,支援WINDOWS)
AUDIO支援:48KHZ,16BIT,MONO 錄放 (2個 ISO endpoints)
HID支援: 8位元組 雙向(2 interrupt endpoints)

uint8_t g_device_descriptor[DEVICE_DESCRIPTOR_SIZE] =
{
DEVICE_DESCRIPTOR_SIZE, /* (18) “Device Descriptor Size */
USB_DEVICE_DESCRIPTOR, /* “Device” Type of descriptor */
0x10,0x01, /* BCD USB version */
0x00, /* Device Class is indicated in
the interface descriptors */
0x00, /* Device Subclass is indicated
in the interface descriptors */
0x00, /* Device Protocol */
CONTROL_MAX_PACKET_SIZE, /* Max Packet size */
0xa2, 0x15, /* Vendor ID */
0x00, 0x03, /* Product ID */
0x00, 0x02, /* BCD Device version */
0x01, /* Manufacturer string index */
0x02, /* Product string index */
0x00, /* Serial number string index */
0x01 /* Number of configurations */
};

/* 1個 AUDIO CONTROL介面(SPEAKER和MICROPHONE的控制)
2個 AUDIO STREAMING介面
1 個SPEAKER ( 1 channels 16bit 48khz, 96bytes)
1 個MICROPHONE ( 1 channel 16bit 48khz, 96bytes )
1個HID 介面
device host
microphone : IT -> FU -> OT USB IN-ENDPOINT
speaker : OT <- FU <- IT USB OUT-ENDPOINT

config :
config header

   audio control interface 0
   audio control header
   audio microphone control IT
   audio microphone control FU
   audio microphone control OT
   audio speaker control OT
   audio speaker control FU
   audio speaker control IT

   audio microphone interface
   audio microphone endpoint

   audio speaker interface
   audio speaker endpoint

*/

uint8_t g_config_descriptor[CONFIG_DESC_SIZE] =
{
CONFIG_ONLY_DESC_SIZE, /* Configuration Descriptor Size ( 9 )bytes*/
USB_CONFIG_DESCRIPTOR, /* “Configuration” type of descriptor */
// CONFIG_DESC_SIZE :注意大小需要計算
CONFIG_DESC_SIZE, 0x00, /* Total length of the Configuration descriptor (191 + 25 +7)*/
// 介面的個數要更改: 3(2個AUDIO+1個HID) -> 4 (3個AUDIO+1個HID)
0x04, /* NumInterfaces */
0x01, /* Configuration Value */
0, /* Configuration Description String Index*/
/* Attributes.support RemoteWakeup and self power */
(USB_DESC_CFG_ATTRIBUTES_D7_POS) | (USBCFG_DEV_SELF_POWER << USB_DESC_CFG_ATTRIBUTES_SELF_POWERED_SHIFT) | (USBCFG_DEV_REMOTE_WAKEUP << USB_DESC_CFG_ATTRIBUTES_REMOTE_WAKEUP_SHIFT),
/* S08/CFv1 are both self powered (its compulsory to set bus powered)*/
/Attributes.support RemoteWakeup and self power/
0xFA, /* Current draw from bus */

/********** Audio Control interface 0 :不定義端點,使用EP0************************/
/* AUDIO CONTROL INTERFACE DISCRIPTOR */
IFACE_ONLY_DESC_SIZE, /(9) Size of this descriptor/
USB_IFACE_DESCRIPTOR, /INTERFACE descriptor /
0x00, /* interface number = 0*/
0x00, /Index of this setting/
0x00, /0 endpoint (control端點使用EP0)/
USB_DEVICE_CLASS_AUDIO, /AUDIO/
USB_SUBCLASS_AUDIOCONTROL, /AUDIO_CONTROL/
0x00, /Unused/
0x00, /* Unused*/

//class-specific AC interface descriptor,audio interface(0x24),audio control header(0x01),
//Total Length 0x0048,Number of streaming interface 2,interfaceNr 1,1
/* Audio class-specific interface header */
EX_HEADER_ONLY_DESC_SIZE, /* bLength (10) */
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_CONTROL_HEADER, /* bDescriptorSubtype (HEADER) */
0x00,0x01, /* bcdADC (1.0) */
// HEADER SIZE 要重新計算
0x45,0x00, /* wTotalLength (69) */
0x02, /* bInCollection :2 AudioStreaming interface*/ //lzq:0->2
0x02, /* 編號2 :InterfaceNr(2) - AS #1 id AudioStreaming interface 2 belongs to this AudioControl interface */
0x01, /* 編號1 :InterfaceNr(1) - AS #2 id AudioStreaming interface 1 belongs to this AudioControl interface */

/* Audio microphone control unit */
//Microphone(0x0201),Input Terminal,1 channel
/* Audio class-specific input terminal */
INPUT_TERMINAL_ONLY_DESC_SIZE, /* bLength (12) */
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_CONTROL_INPUT_TERMINAL, /* bDescriptorSubtype (INPUT_TERMINAL) */
0x01, /* bTerminalID (1) */
0x01,0x02, /* wTerminalType ( microphone) */
0x00, /* bAssocTerminal (none) */
//channel 個數
0x01, /* bNrChannels (1) */
0x00,0x00, /* wChannelConfig (left, right) */
0x00, /* iChannelNames (none) */
0x00, /* iTerminal (none) */

//Audio Feature Unit Descriptor:audio interface descriptor,feature_unit(0x06),terminal id 0x02,
//SourceId 0x01,ControlSize 0x01,Mute,Volume,
/* Audio class-specific feature unit */
//注意:由於INPUT 為單聲道錄音,所以這個地方FEATURE_UNIT_ONLY_DESC_SIZE=9,如果是雙聲道錄音則FEATURE_UNIT_ONLY_DESC_SIZE=10
FEATURE_UNIT_ONLY_DESC_SIZE, /* bLength (9) */
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_CONTROL_FEATURE_UNIT, /* bDescriptorSubtype (FEATURE_UNIT) */
0x02, /* bUnitID (2) */
0x01, /* bSourceID (input terminal 1 : bTerminalID 1 #Microphone IT) */
0x01, /* bControlSize (1 bytes) */
0x03, // CONTROL :MUTE
0x00, /* Master controls :volume*/
0x00, /* Channel 0 controls */

/* Audio class-specific output terminal */
//USB Streaming OT:audio interface descriptor,audio control output terminal(0x03),terminal id 0x03,
//USB Streaming(0x0101),Output Terminal(0x03),SourceId 0x02,

OUTPUT_TERMINAL_ONLY_DESC_SIZE, /* bLength (9)*/
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_CONTROL_OUTPUT_TERMINAL, /* bDescriptorSubtype (OUTPUT_TERMINAL)*/
0x03, /* bTerminalID (3) */
0x01,0x01, /* wTerminalType (USB streaming) */
0x00, /* bAssocTerminal (none) */
0x02, /* bSourceID (feature unit 2) */
0x00, /* iTerminal (none)*/

/* Audio speaker control unit */
//streaming (0x0101),Input Terminal(0x02),1 channel
INPUT_TERMINAL_ONLY_DESC_SIZE, /* bLength (12) */
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_CONTROL_INPUT_TERMINAL, /* bDescriptorSubtype (INPUT_TERMINAL) */
0x04, /* bTerminalID (4) */
0x01,0x01, /* wTerminalType (USB Streaming) */
0x00, /* bAssocTerminal (none) */
0x01, /* bNrChannels (2) */ //only one channel: 2->1
0x00,0x00, /* wChannelConfig (left, right) */
0x00, /* iChannelNames (none) */
0x00, /* iTerminal (none) */

/* Audio class-specific feature unit */
//Audio Feature Unit Descriptor:audio interface descriptor,feature_unit,terminal id 0x05,
//SourceId 0x01,ControlSize 0x01,Mute,Volume,
//FU 和IT關聯,如果是1個CHANNEL :FEATURE_UNIT2_ONLY_DESC_SIZE是9;如果是2個CHN,那麼FEATURE_UNIT2_ONLY_DESC_SIZE=10
FEATURE_UNIT_ONLY_DESC_SIZE, /* bLength (9) */
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_CONTROL_FEATURE_UNIT, /* bDescriptorSubtype (FEATURE_UNIT) */
0x05, /* bTerminalID (5) */
0x04, /* bSourceID (input terminal 4) */
0x01, /* bControlSize (1 bytes) */
0x03, // controls:mute
0x02, /* controls:volume(0) */
0x00, /* Feature String */

/* Audio class-specific output terminal */
//USB Speaker OT:audio interface descriptor,audio control output terminal(0x03),terminal id 0x06,
//USB Speaker(0x0301),Output Terminal(0x03),SourceId 0x05,
OUTPUT_TERMINAL_ONLY_DESC_SIZE, /* bLength (9)*/
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_CONTROL_OUTPUT_TERMINAL, /* bDescriptorSubtype (OUTPUT_TERMINAL)*/
0x06, /* bTerminalID (6) */
0x01,0x03, /* wTerminalType (USB speaker) */
0x00, /* bAssocTerminal (none) */
0x05, /* bSourceID (feature unit 5) */
0x00, /* iTerminal (none)*/

//——————-Microphone streaming interface 1———————//
/* USB microphone standard AS interface descriptor - audio streaming operational
(Interface 1, Alternate Setting 0) */
IFACE_ONLY_DESC_SIZE, /* bLength (9)*/
USB_IFACE_DESCRIPTOR, /* bDescriptorType (CS_INTERFACE) */
0x01, /* interface Number: 1 */
0x00, /* Alternate Setting: 0 */
0x00, /* not used (Zero Bandwidth) */
USB_DEVICE_CLASS_AUDIO, /* USB DEVICE CLASS AUDIO */
USB_SUBCLASS_AUDIOSTREAM, /* AUDIO SUBCLASS AUDIOSTREAMING */
0x00, /* AUDIO PROTOCOL UNDEFINED */
0x00, /* Unused */

/* USB microphone standard AS interface descriptor - audio streaming operational

(Interface 1, Alternate Setting 1) */
IFACE_ONLY_DESC_SIZE, /* bLength (9) */
USB_IFACE_DESCRIPTOR, /* bDescriptorType (CS_INTERFACE) */
0x01, /* interface Number: 1 */
0x01, /* Alternate Setting: 1 */
0x01, /* One Endpoint. */
USB_DEVICE_CLASS_AUDIO, /* USB DEVICE CLASS AUDIO */
USB_SUBCLASS_AUDIOSTREAM, /* AUDIO SUBCLASS AUDIOSTREAMING */
0x00, /* AUDIO PROTOCOL UNDEFINED */
0x00, /* Unused */

/* USB microphone standard General AS interface descriptor */

AUDIO_STREAMING_IFACE_DESC_SIZE, /* bLength (7) */
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_STREAMING_GENERAL, /* GENERAL subtype */
0x03, /* Unit ID of microphone output terminal : terminal ID = 3 */
0x00, /* Interface delay */
0x01,0x00, /* PCM format */ // 02,01 –> 01,00

/* USB microphone audio type I format interface descriptor */

AUDIO_STREAMING_TYPE_I_DESC_SIZE,/* bLength (11) */
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_STREAMING_FORMAT_TYPE, /* DescriptorSubtype: AUDIO STREAMING FORMAT TYPE */
AUDIO_FORMAT_TYPE_I, /* Format Type: Type I */
AUDIO_FORMAT_CHANNELS, /* Number of Channels: one channel */
AUDIO_FORMAT_SIZE, /* SubFrame Size: one byte per audio subframe */
AUDIO_FORMAT_BITS, /* Bit Resolution: 8 bits per sample */
0x01, /* One frequency supported */
// 0x40, 0x1F,0x00, /* 8 kHz */
// 0x80, 0x3E,0x00, /* 16 kHz */
0x80,0xBB,0x00, /* 48 kHz */
// 0x00, 0xFA,0x00, /* 72 kHz */

/Endpoint 1 - standard descriptor/
ENDP_ONLY_DESC_SIZE, /* bLength (9) */
USB_ENDPOINT_DESCRIPTOR, /* Descriptor type (endpoint descriptor) */
AUDIO_MIC_ENDPOINT|(USB_SEND << 7), /* OUT endpoint address = 1 */ //endpoint address must be set
0x05, /* Isochronous endpoint */ //lzq 1->5
USB_uint_16_low(FS_ISO_OUT_ENDP_PACKET_SIZE),
USB_uint_16_high(FS_ISO_OUT_ENDP_PACKET_SIZE), /* 16 bytes */
FS_ISO_OUT_ENDP_INTERVAL, /* 1 ms */
0x00, /* Unused */
0x00, /* Unused */

/* Endpoint 1 - Audio streaming descriptor */
AUDIO_STREAMING_ENDP_DESC_SIZE, /* bLength (7) */
USB_AUDIO_DESCRIPTOR, /* AUDIO ENDPOINT DESCRIPTOR TYPE */
AUDIO_ENDPOINT_GENERAL, /* AUDIO ENDPOINT GENERAL */
0x00, /* bmAttributes: 0x00 */
0x00, /* unused */
0x00,0x00, /* unused */

//——————-Speaker streaming interface 2———————//
/* USB speaker standard AS interface descriptor - audio streaming operational
(Interface 2, Alternate Setting 0) */
IFACE_ONLY_DESC_SIZE, /* bLength (9) */
USB_IFACE_DESCRIPTOR, /* bDescriptorType (CS_INTERFACE) */
0x02, /* interface Number: 2 */
0x00, /* Alternate Setting: 0 */
0x00, /* not used (Zero Bandwidth) */
USB_DEVICE_CLASS_AUDIO, /* USB DEVICE CLASS AUDIO */
USB_SUBCLASS_AUDIOSTREAM, /* AUDIO SUBCLASS AUDIOSTREAMING */
0x00, /* AUDIO PROTOCOL UNDEFINED */
0x00, /* Unused */

 /* USB speaker standard AS interface descriptor - audio streaming operational

(Interface 2, Alternate Setting 1) */
IFACE_ONLY_DESC_SIZE, /* bLength (9) */
USB_IFACE_DESCRIPTOR, /* bDescriptorType (CS_INTERFACE) */
0x02, /* interface Number: 1 */
0x01, /* Alternate Setting: 1 */
0x01, /* One Endpoint. */
USB_DEVICE_CLASS_AUDIO, /* USB DEVICE CLASS AUDIO */
USB_SUBCLASS_AUDIOSTREAM, /* AUDIO SUBCLASS AUDIOSTREAMING */
0x00, /* AUDIO PROTOCOL UNDEFINED */
0x00, /* Unused */

 /* USB speaker standard General AS interface descriptor */
AUDIO_STREAMING_IFACE_DESC_SIZE, /* bLength (7) */
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_STREAMING_GENERAL,         /* GENERAL subtype */
0x04,                            /* Unit ID of USB Streaming IT   */    
0x01,                            /* Interface delay */
0x01,0x00,                       /* PCM format */

/* USB speaker audio type I format interface descriptor */
AUDIO_STREAMING_TYPE_I_DESC_SIZE,   /* bLength (11) */
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType (CS_INTERFACE) */
AUDIO_STREAMING_FORMAT_TYPE,     /* DescriptorSubtype: AUDIO STREAMING FORMAT TYPE */
AUDIO_FORMAT_TYPE_I,             /* Format Type: Type I */
AUDIO_FORMAT_CHANNELS,           /* Number of Channels: one channel */
AUDIO_FORMAT_SIZE,               /* SubFrame Size: one byte per audio subframe */
AUDIO_FORMAT_BITS,               /* Bit Resolution: 8 bits per sample */
0x01,                            /* One frequency supported */

// 0x40, 0x1F,0x00, /* 8 kHz */
// 0x80, 0x3E,0x00, /* 16 kHz */
0x80,0xBB,0x00, /* 48 kHz */
// 0x00, 0xFA,0x00, /* 72 kHz */

/*Endpoint 1 - standard descriptor*/
ENDP_ONLY_DESC_SIZE,             /* bLength (9) */
USB_ENDPOINT_DESCRIPTOR,         /* Descriptor type (endpoint descriptor) */
AUDIO_SPK_ENDPOINT,              /* OUT endpoint address 2 */       //輸出的ENDPOINT = 2
0x05,                            /* Isochronous endpoint */  //lzq 1->5
USB_uint_16_low(FS_ISO_OUT_ENDP_PACKET_SIZE),
USB_uint_16_high(FS_ISO_OUT_ENDP_PACKET_SIZE),  /* size of packet: 64 Bytes */

#if HIGH_SPEED
HS_ISO_OUT_ENDP_INTERVAL, /* bInterval(0x04): 2^x ms */
#else
FS_ISO_OUT_ENDP_INTERVAL, /* bInterval(0x01): 2^x ms */
#endif
0x00, /* Unused */
0x00, /* Unused */

/* Endpoint 1 - Audio streaming descriptor */
AUDIO_STREAMING_ENDP_DESC_SIZE,  /* bLength (7) */
USB_AUDIO_DESCRIPTOR,            /* AUDIO ENDPOINT DESCRIPTOR TYPE */
AUDIO_ENDPOINT_GENERAL,          /* AUDIO ENDPOINT GENERAL */
0x00,                            /* bmAttributes: 0x80 */
0x00,                            /* unused */
0x00,0x00,                       /* unused */

//AUDIO DESCRIPTOR TOTAL :191 BYTES

//——————-HID interface 3———————//
/* HID specific Interface Descriptor */
IFACE_ONLY_DESC_SIZE, //(9)
USB_IFACE_DESCRIPTOR, ///* bDescriptorType (CS_INTERFACE) */
0x03, // interface number
0x00, /* Alternate Setting: 0 */
HID_DESC_ENDPOINT_COUNT,
0x03, /* USB DEVICE class : HID */
0x00, /* USB SUBCLASS :注意:非 boot device*/
0x00, //介面所遵循的協議 0-NONE 1-KEYBOARD 2-MOUSE 3-255:RSV 2->0xFF(VENDOR DEFINED)
0x00, //描述該介面的字串索引值

  /* HID descriptor */
  HID_ONLY_DESC_SIZE,               //(9)
  USB_HID_DESCRIPTOR,
  0x10,0x01,                        //bcd
  0x00,
  0x01,
  0x22,                             //report descriptor support
  0x23,0x00,                        //size of(report descriptor)

  /*Endpoint descriptor */
  0x07,                             //(7)
  USB_ENDPOINT_DESCRIPTOR,
  HID_IN_ENDPOINT|(USB_SEND << 7),      //:endpoint number
  USB_INTERRUPT_PIPE,
  USB_uint_16_low(FS_INTERRUPT_OUT_ENDP_PACKET_SIZE),       //8->64 (FS_INTERRUPT_OUT_ENDP_PACKET_SIZE)
  USB_uint_16_high(FS_INTERRUPT_OUT_ENDP_PACKET_SIZE),
  FS_INTERRUPT_OUT_ENDP_INTERVAL,

  /*Endpoint descriptor */
  0x07,                             //(7)
  USB_ENDPOINT_DESCRIPTOR,
  HID_OUT_ENDPOINT,     //endpoint number
  USB_INTERRUPT_PIPE,
  USB_uint_16_low(FS_INTERRUPT_OUT_ENDP_PACKET_SIZE),       //8->64 (FS_INTERRUPT_OUT_ENDP_PACKET_SIZE)
  USB_uint_16_high(FS_INTERRUPT_OUT_ENDP_PACKET_SIZE),
  FS_INTERRUPT_OUT_ENDP_INTERVAL

};

uint8_t g_report_descriptor[35] =
{
0x05,0x81, /usage page(FF00h, vendor defined) /
0x09,0x82, /usage (vendor defined) /
0xA1,0x88, /* collection(Application) */
//輸入報告 device –>host
0x09, 0x83, /* usage (vendor defined1) */
0x09, 0x84, /* usage (vendor defined1) */
0x15, 0x00, /* LOGICAL_MINIMUM (0x0 )*/
0x26, 0x00,0xFF, /* LOGICAL_MAXIMUM (0xFF) */
0x75, 0x08, /* Report size (8位) */
0x95, 0x08, /* REPORT_SIZE (64 fields = 8 bytes,include report ID) */
0x81, 0x02, /* INPUT(data, val, absolute)*/
//輸出報告 host –> device

    0x09, 0x84,     /*       usage page (vendor defined3)*/
    0x15, 0x00,     /*       LOGICAL_MINIMUM (0x0 )*/
    0x26, 0x00,0xFF, /*       LOGICAL_MAXIMUM (0xFF) */
    0x75, 0x08,     /*       Report size (8位)              */
    0x95, 0x08,     /*       REPORT_SIZE (64 fields = 8 bytes,include report ID) */
    0x91, 0x02,     /*       OUTPUT(data, val, absolute) */
    0xC0            /*   ENDCOLLECTION(Application) */

};