Page 1 of 2

USB problem with Configuration Descriptor

Posted: Tue Nov 27, 2012 10:15 am
by Spider
Hi!

I'm trying to write HID device on STM32F103. But I have problem with Configuration descriptor. When I sniffing USB packets on Host i receive next:

Code: Select all

00000000  09 02 6D 00 04 01 00 C0 32 09 04 00 00 01 03 01  ..m....À2.......
00000010  01 04 09 21 11 01 00 01 22 3F 00 07 05 81 03 08  ...!...."?.....
00000020  00 0A 09 04 01 00 01 03 00 00 04 09 21 11 01 00  ............!...
00000030  01 22 75 00 07 05 82 03 04 00 32 09 04 02 00 01  ."u...‚...2.....
00000040  11 01 00 01 22 75 00 07 05 82 03 04 00 32 09 04  ...."u...‚...2..
00000050  02 00 01 03 FF FF 06 09 21 11 01 00 01 22 36 00  ....ÿÿ..!...."6.
00000060  07 05 83 03 08 00 0A 09 04 03 00 01 03           ..Æ’..........   

when must be

Code: Select all

static const uint8_t hid_configuration_descriptor_data[] = {
  /* Configuration Descriptor.*/
  USB_DESC_CONFIGURATION(      0x6D,
                           0x04,          /* bNumInterfaces.                  */
                         0x01,          /* bConfigurationValue.             */
                         0,             /* iConfiguration.                  */
                         0xC0,          /* bmAttributes (self powered).     */
                         50),           /* bMaxPower (100mA).               */
  /* Keyboard Interface Descriptor.*/
  USB_DESC_INTERFACE    (0x00,          /* bInterfaceNumber.                */
                         0x00,          /* bAlternateSetting.               */
                         0x01,          /* bNumEndpoints.                   */
                         0x03,          /* bInterfaceClass (HID).           */
                         0x01,          /* bInterfaceSubClass (Boot).       */
                         0x01,          /* bInterfaceProtocol (Boot Kbd).   */
                         0x04),         /* iInterface.                      */
  /* Header Functional Descriptor (HID).*/
  USB_HID_DESCRIPTOR    (0x0111,
                           0x00,
                           0x01,
                          HID_DTYPE_Report,
                           sizeof(KeyboardReport)),
  /* In Endpoint Descriptor.*/
  USB_DESC_ENDPOINT     (KEYBOARD_IN_EPNUM|0x80,
                           USB_EP_MODE_TYPE_INTR,          /* bmAttributes (Interrupt).        */
                          EPSIZE_KEYBOARD_IN,             /* wMaxPacketSize.                  */
                         0x0A),         /* bInterval.                       */
  /* Keyboard Ex Interface Descriptor.*/
  USB_DESC_INTERFACE    (0x01,          /* bInterfaceNumber.                */
                         0x00,          /* bAlternateSetting.               */
                         0x01,          /* bNumEndpoints.                   */
                         0x03,          /* bInterfaceClass (HID).     */
                         0x00,          /* bInterfaceSubClass (No Boot).    */
                         0x00,          /* bInterfaceProtocol (No Boot).    */
                         0x04),         /* iInterface.                      */
  /* Header Functional Descriptor (HID).*/
  USB_HID_DESCRIPTOR    (0x0111,
                           0x00,
                         0x01,
                         HID_DTYPE_Report,
                         sizeof(KeyboardExReport)),
  /* In Endpoint Descriptor.*/
  USB_DESC_ENDPOINT     (KEYBOARD_EX_IN_EPNUM|0x80,     /* bEndpointAddress.*/
                           USB_EP_MODE_TYPE_INTR,          /* bmAttributes (Interrupt)         */
                          EPSIZE_KEYBOARD_EX_IN,        /* wMaxPacketSize.                  */
                         50),           /* bInterval.                       */
  /* Mouse Interface Descriptor.*/
  USB_DESC_INTERFACE    (0x02,          /* bInterfaceNumber.                */
                           0x00,          /* bAlternateSetting.               */
                         0x01,          /* bNumEndpoints.                   */
                         0x03,          /* bInterfaceClass (HID).     */
                         0xFF,          /* bInterfaceSubClass (No Boot).    */
                         0xFF,          /* bInterfaceProtocol (No Boot).    */
                         0x06),         /* iInterface.                      */
  /* Header Functional Descriptor (HID).*/
  USB_HID_DESCRIPTOR    (0x0111,
                          0x00,
                         0x01,
                         HID_DTYPE_Report,
                         sizeof(MouseReport)),
  /* In Endpoint Descriptor.*/
  USB_DESC_ENDPOINT     (MOUSE_IN_EPNUM|0x80,     /* bEndpointAddress.*/
                           USB_EP_MODE_TYPE_INTR,          /* bmAttributes (Interrupt)         */
                          EPSIZE_MOUSE_IN,        /* wMaxPacketSize.                  */
                         10),           /* bInterval.                       */
  /* HID Interface Descriptor.*/
  USB_DESC_INTERFACE    (0x03,          /* bInterfaceNumber.                */
                             0x00,          /* bAlternateSetting.               */
                         0x01,          /* bNumEndpoints.                   */
                         0x03,          /* bInterfaceClass (HID).     */
                         0x00,          /* bInterfaceSubClass (No Boot).    */
                         0x00,          /* bInterfaceProtocol (No Boot).    */
                         0x03),         /* iInterface.                      */
  /* Header Functional Descriptor (HID).*/
  USB_HID_DESCRIPTOR    (0x0111,
                            0x00,
                         0x01,
                         HID_DTYPE_Report,
                         sizeof(GenericReport)),
  /* In Endpoint Descriptor.*/
  USB_DESC_ENDPOINT     (GENERIC_IN_EPNUM|0x80,     /* bEndpointAddress.*/
                           USB_EP_MODE_TYPE_INTR,          /* bmAttributes (Interrupt)         */
                          EPSIZE_GENERIC_IN,        /* wMaxPacketSize.                  */
                         200),
}


As we can see in packet dump at 0x00000040 address beginig wrong data. Why?
I'm trying to change hid_configuration_descriptor_data length and content, but BUG always exist in different place.
What I can do wrong?

PS. gcc version 4.6.3 (Sourcery CodeBench Lite 2012.03-56)

Re: USB problem with Configuration Descriptor

Posted: Tue Nov 27, 2012 12:47 pm
by Giovanni
Hi,

If I understand well, you see on the analyzer a configuration descriptor different from the one you specified. 0x40 is on a packet boundary, is this incidental or the problem moves when changing the descriptor?

You should also verify if the descriptor as compiled is equal to the one you see in the analyzer, you could see the .lst file and compare with your dump.

Giovanni

Re: USB problem with Configuration Descriptor

Posted: Tue Nov 27, 2012 1:08 pm
by Spider
Giovanni wrote:0x40 is on a packet boundary, is this incidental or the problem moves when changing the descriptor?

Randomly changes but changed with changing descriptor size. And always in 0x000000-0 address. For example can be 0x90 or 0xC0. And after this address may be anything. Sometime it was repeat of description from begin.
Giovanni wrote:You should also verify if the descriptor as compiled is equal to the one you see in the analyzer, you could see the .lst file and compare with your dump.

Code: Select all

 975                 hid_configuration_descriptor_data:
 976 0000 09             .byte   9
 977 0001 02             .byte   2
 978 0002 6D             .byte   109
 979 0003 00             .byte   0
 980 0004 04             .byte   4
 981 0005 01             .byte   1
 982 0006 00             .byte   0
 983 0007 C0             .byte   -64
 984 0008 32             .byte   50
 985 0009 09             .byte   9
 986 000a 04             .byte   4
 987 000b 00             .byte   0
 988 000c 00             .byte   0
 989 000d 01             .byte   1
 990 000e 03             .byte   3
 991 000f 01             .byte   1
 992 0010 01             .byte   1
 993 0011 04             .byte   4
 994 0012 09             .byte   9
 995 0013 21             .byte   33
 996 0014 11             .byte   17
 997 0015 01             .byte   1
 998 0016 00             .byte   0
 999 0017 01             .byte   1
 1000 0018 22             .byte   34
 1001 0019 3F             .byte   63
 1002 001a 00             .byte   0
 1003 001b 07             .byte   7
 1004 001c 05             .byte   5
 1005 001d 81             .byte   -127
 1006 001e 03             .byte   3
 1007 001f 08             .byte   8
 1008 0020 00             .byte   0
 1009 0021 0A             .byte   10
 1010 0022 09             .byte   9
 1011 0023 04             .byte   4
 1012 0024 01             .byte   1
 1013 0025 00             .byte   0
 1014 0026 01             .byte   1
 1015 0027 03             .byte   3
 1016 0028 00             .byte   0
ARM GAS  C:\Users\User\AppData\Local\Temp\ccTbTJls.s          page 19

 1017 0029 00             .byte   0
 1018 002a 04             .byte   4
 1019 002b 09             .byte   9
 1020 002c 21             .byte   33
 1021 002d 11             .byte   17
 1022 002e 01             .byte   1
 1023 002f 00             .byte   0
 1024 0030 01             .byte   1
 1025 0031 22             .byte   34
 1026 0032 75             .byte   117
 1027 0033 00             .byte   0
 1028 0034 07             .byte   7
 1029 0035 05             .byte   5
 1030 0036 82             .byte   -126
 1031 0037 03             .byte   3
 1032 0038 04             .byte   4
 1033 0039 00             .byte   0
 1034 003a 32             .byte   50
 1035 003b 09             .byte   9
 1036 003c 04             .byte   4
 1037 003d 02             .byte   2
 1038 003e 00             .byte   0
 1039 003f 01             .byte   1
 1040 0040 03             .byte   3
 1041 0041 FF             .byte   -1
 1042 0042 FF             .byte   -1
 1043 0043 06             .byte   6
 1044 0044 09             .byte   9
 1045 0045 21             .byte   33
 1046 0046 11             .byte   17
 1047 0047 01             .byte   1
 1048 0048 00             .byte   0
 1049 0049 01             .byte   1
 1050 004a 22             .byte   34
 1051 004b 36             .byte   54
 1052 004c 00             .byte   0
 1053 004d 07             .byte   7
 1054 004e 05             .byte   5
 1055 004f 83             .byte   -125
 1056 0050 03             .byte   3
 1057 0051 08             .byte   8
 1058 0052 00             .byte   0
 1059 0053 0A             .byte   10
 1060 0054 09             .byte   9
 1061 0055 04             .byte   4
 1062 0056 03             .byte   3
 1063 0057 00             .byte   0
 1064 0058 01             .byte   1
 1065 0059 03             .byte   3
 1066 005a 00             .byte   0
 1067 005b 00             .byte   0
 1068 005c 03             .byte   3
 1069 005d 09             .byte   9
 1070 005e 21             .byte   33
 1071 005f 11             .byte   17
 1072 0060 01             .byte   1
 1073 0061 00             .byte   0
ARM GAS  C:\Users\User\AppData\Local\Temp\ccTbTJls.s          page 20

 1074 0062 01             .byte   1
 1075 0063 22             .byte   34
 1076 0064 27             .byte   39
 1077 0065 00             .byte   0
 1078 0066 07             .byte   7
 1079 0067 05             .byte   5
 1080 0068 84             .byte   -124
 1081 0069 03             .byte   3
 1082 006a 09             .byte   9
 1083 006b 00             .byte   0
 1084 006c C8             .byte   -56

Re: USB problem with Configuration Descriptor

Posted: Tue Nov 27, 2012 1:35 pm
by Giovanni
It is different then. What ChibiOS version are you using?

Giovanni

Re: USB problem with Configuration Descriptor

Posted: Tue Nov 27, 2012 1:45 pm
by Spider
2.5.0 from git

Re: USB problem with Configuration Descriptor

Posted: Tue Nov 27, 2012 1:59 pm
by Spider
I think problem in CH_IRQ_HANDLER(STM32_USB1_LP_HANDLER) near lines

Code: Select all

        /* Transfer not completed, there are more packets to send.*/
        if (epcp->in_state->txsize > epcp->in_maxsize)
          n = epcp->in_maxsize;
        else
          n = epcp->in_state->txsize;


broken always last part of the buffer. I think if I made configure_descriptor proportionally in_maxsize all will work.

Re: USB problem with Configuration Descriptor

Posted: Tue Nov 27, 2012 2:16 pm
by Spider
Heh!
For example, I lengthened configuration_descriptor to 0x81 byte and fill all bytes after 0x6D with 0x00. Now All Work! But driver on PC warning me that wrong length of descriptor. But It work.
Configuration Descriptor 1 Self Powered
Offset Field Size Value Description
0 bLength 1 09h
1 bDescriptorType 1 02h Configuration
2 wTotalLength 2 0081h Incorrect, should be 006Dh
4 bNumInterfaces 1 04h
5 bConfigurationValue 1 01h
6 iConfiguration 1 00h
7 bmAttributes 1 C0h Self Powered
4..0: Reserved ...00000
5: Remote Wakeup ..0..... No
6: Self Powered .1...... Yes
7: Reserved (set to one)
(bus-powered for 1.0) 1.......
8 bMaxPower 1 32h 100 mA

Re: USB problem with Configuration Descriptor

Posted: Tue Nov 27, 2012 3:25 pm
by Giovanni
Can you create a small project that I can compile and test? just he minimal code to show the problem.

Giovanni

Re: USB problem with Configuration Descriptor

Posted: Tue Nov 27, 2012 4:12 pm
by Spider
I'll in future. As Example of HID keyboard.

Re: USB problem with Configuration Descriptor

Posted: Wed Nov 28, 2012 7:59 am
by Spider
This is VERY RAW code. It can have erros - I wrote it in Notepad based on USB_CDC code for STM32F1XX test_hal.
In ideal this FW must detects as "Composite Device" with four HID devices: Keyboard, Keyboard_Ex (Multimedia keys), TouchScreen (mouse), Generic HID.
All Devices detected and seems to work, but do nothing :) No any drivers need in WinXP/Win7.

BUT! In code exist line //BUG FIX, if you remove nop bytes after them and change Config Descriptor Length (Second Byte in strucutre) to 0x6D (it is real data size) your FW will work not correctly.

Thx! Sorry for my English :)