USB Control Transfer to get AudioStreaming Descriptor with libusb

Viewed 985

I am currently working on USB microphone (Samson Q1U) support for my Android app using libusb (1.0.9). I have gotten data streaming from the device working no problem, but I would like to read information about the data format (bit depth, sample rates, channels). So far, USB has been really confusing, so forgive my attempt at its usage below. The problem is the control transfer, I don't believe it is setup correctly. I have tried many different transfer parameter combinations, but all have return error codes -1, -7, & -9. Any help would be great.

Essentially, I want to get this descriptor:

AudioStreaming Interface Descriptor:
    bLength                23
    bDescriptorType        36
    bDescriptorSubtype      2 (FORMAT_TYPE)
    bFormatType             1 (FORMAT_TYPE_I)
    bNrChannels             1
    bSubframeSize           2
    bBitResolution         16
    bSamFreqType            5 Discrete
    tSamFreq[ 0]         8000
    tSamFreq[ 1]        11025
    tSamFreq[ 2]        22050
    tSamFreq[ 3]        44100
    tSamFreq[ 4]        48000

This is my code for the control transfer:

extern "C" void Java_com_example_test_LibUsb_nativeGetAudioInfo(JNIEnv *env, jobject obj){

libusb_init(NULL);
devh = libusb_open_device_with_vid_pid(NULL,VID,PID);
libusb_kernel_driver_active(devh, 0);
libusb_detach_kernel_driver(devh, 0);
libusb_claim_interface(devh, 0);
libusb_set_interface_alt_setting(devh,0,0);

uint8_t requestType = 0b1 | 0b01 | 0b00001;
uint8_t bRequest = 0x06;
uint16_t wValue = 0x24;
uint16_t wIndex = 0;
unsigned char data[23];
unsigned int timeout = 1000;

int rc = libusb_control_transfer(devh, requestType, bRequest, wValue, wIndex, data, sizeof(data), timeout);
LOGD("%d",rc);

int cnt = 0;
if(rc > 0){

    char bLength = data[cnt++];
    char bDescriptorType = data[cnt++];
    char bDescriptorSubtype = data[cnt++];
    char bFormatType = data[cnt++];
    char bNrChannels = data[cnt++];
    char bSubframeSize = data[cnt++];
    char bBitResolution = data[cnt++];
    char bSamFreqType = data[cnt++];

    int rates[bSamFreqType];

    LOGD("%d",bNrChannels);
    LOGD("%d",bBitResolution);

    for(int i = 0;i < bSamFreqType;i++){
        char byte1 = data[cnt++];
        char byte2 = data[cnt++];
        char byte3 = data[cnt++];

        rates[i] = ((byte1 & 0xFF) << 8 | (byte2 & 0xFF) << 16 | (byte3 & 0xFF) << 24);
        LOGD("%d",rates[i]);
    }

}

libusb_exit(NULL);

}

Here is the full device descriptor:

Device Descriptor:
bLength                18
bDescriptorType         1
bcdUSB               1.10
bDeviceClass            0 (Defined at Interface level)
bDeviceSubClass         0 
bDeviceProtocol         0 
bMaxPacketSize0         8
idVendor           0x17a0 Samson Technologies Corp.
idProduct          0x0002 Q1U dynamic microphone
bcdDevice            0.01
iManufacturer           1 
iProduct                2 
iSerial                 0 
bNumConfigurations      1
  Configuration Descriptor:
  bLength                 9
  bDescriptorType         2
  wTotalLength          177
  bNumInterfaces          2
  bConfigurationValue     1
  iConfiguration          0 
  bmAttributes         0x80
    (Bus Powered)
  MaxPower               90mA
  Interface Descriptor:
    bLength                 9
    bDescriptorType         4
    bInterfaceNumber        0
    bAlternateSetting       0
    bNumEndpoints           0
    bInterfaceClass         1 Audio
    bInterfaceSubClass      1 Control Device
    bInterfaceProtocol      0 
    iInterface              0 
    AudioControl Interface Descriptor:
      bLength                 9
      bDescriptorType        36
      bDescriptorSubtype      1 (HEADER)
      bcdADC               1.00
      wTotalLength           40
      bInCollection           1
      baInterfaceNr( 0)       1
    AudioControl Interface Descriptor:
      bLength                12
      bDescriptorType        36
      bDescriptorSubtype      2 (INPUT_TERMINAL)
      bTerminalID             1
      wTerminalType      0x0201 Microphone
      bAssocTerminal          2
      bNrChannels             2
      wChannelConfig     0x0003
        Left Front (L)
        Right Front (R)
      iChannelNames           0 
      iTerminal               0 
    AudioControl Interface Descriptor:
      bLength                 9
      bDescriptorType        36
      bDescriptorSubtype      3 (OUTPUT_TERMINAL)
      bTerminalID             2
      wTerminalType      0x0101 USB Streaming
      bAssocTerminal          1
      bSourceID               3
      iTerminal               0 
    AudioControl Interface Descriptor:
      bLength                10
      bDescriptorType        36
      bDescriptorSubtype      6 (FEATURE_UNIT)
      bUnitID                 3
      bSourceID               1
      bControlSize            1
      bmaControls( 0)      0x01
        Mute Control
      bmaControls( 1)      0x02
        Volume Control
      bmaControls( 2)      0x02
        Volume Control
      iFeature                0 
  Interface Descriptor:
    bLength                 9
    bDescriptorType         4
    bInterfaceNumber        1
    bAlternateSetting       0
    bNumEndpoints           0
    bInterfaceClass         1 Audio
    bInterfaceSubClass      2 Streaming
    bInterfaceProtocol      0 
    iInterface              0 
  Interface Descriptor:
    bLength                 9
    bDescriptorType         4
    bInterfaceNumber        1
    bAlternateSetting       1
    bNumEndpoints           1
    bInterfaceClass         1 Audio
    bInterfaceSubClass      2 Streaming
    bInterfaceProtocol      0 
    iInterface              0 
    AudioStreaming Interface Descriptor:
      bLength                 7
      bDescriptorType        36
      bDescriptorSubtype      1 (AS_GENERAL)
      bTerminalLink           2
      bDelay                  1 frames
      wFormatTag              1 PCM
    AudioStreaming Interface Descriptor:
      bLength                23
      bDescriptorType        36
      bDescriptorSubtype      2 (FORMAT_TYPE)
      bFormatType             1 (FORMAT_TYPE_I)
      bNrChannels             1
      bSubframeSize           2
      bBitResolution         16
      bSamFreqType            5 Discrete
      tSamFreq[ 0]         8000
      tSamFreq[ 1]        11025
      tSamFreq[ 2]        22050
      tSamFreq[ 3]        44100
      tSamFreq[ 4]        48000
    Endpoint Descriptor:
      bLength                 9
      bDescriptorType         5
      bEndpointAddress     0x81  EP 1 IN
      bmAttributes            5
        Transfer Type            Isochronous
        Synch Type               Asynchronous
        Usage Type               Data
      wMaxPacketSize     0x0064  1x 100 bytes
      bInterval               1
      bRefresh                0
      bSynchAddress           0
      AudioControl Endpoint Descriptor:
        bLength                 7
        bDescriptorType        37
        bDescriptorSubtype      1 (EP_GENERAL)
        bmAttributes         0x01
        Sampling Frequency
        bLockDelayUnits         0 Undefined
        wLockDelay              0 Undefined
  Interface Descriptor:
    bLength                 9
    bDescriptorType         4
    bInterfaceNumber        1
    bAlternateSetting       2
    bNumEndpoints           1
    bInterfaceClass         1 Audio
    bInterfaceSubClass      2 Streaming
    bInterfaceProtocol      0 
    iInterface              0 
    AudioStreaming Interface Descriptor:
      bLength                 7
      bDescriptorType        36
      bDescriptorSubtype      1 (AS_GENERAL)
      bTerminalLink           2
      bDelay                  1 frames
      wFormatTag              1 PCM
    AudioStreaming Interface Descriptor:
      bLength                23
      bDescriptorType        36
      bDescriptorSubtype      2 (FORMAT_TYPE)
      bFormatType             1 (FORMAT_TYPE_I)
      bNrChannels             2
      bSubframeSize           2
      bBitResolution         16
      bSamFreqType            5 Discrete
      tSamFreq[ 0]         8000
      tSamFreq[ 1]        11025
      tSamFreq[ 2]        22050
      tSamFreq[ 3]        44100
      tSamFreq[ 4]        48000
    Endpoint Descriptor:
      bLength                 9
      bDescriptorType         5
      bEndpointAddress     0x81  EP 1 IN
      bmAttributes            5
        Transfer Type            Isochronous
        Synch Type               Asynchronous
        Usage Type               Data
      wMaxPacketSize     0x00c8  1x 200 bytes
      bInterval               1
      bRefresh                0
      bSynchAddress           0
      AudioControl Endpoint Descriptor:
        bLength                 7
        bDescriptorType        37
        bDescriptorSubtype      1 (EP_GENERAL)
        bmAttributes         0x01
          Sampling Frequency
        bLockDelayUnits         0 Undefined
        wLockDelay              0 Undefined
1 Answers
Related