usb_desc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "include.h"
  2. #include "usb_com.h"
  3. #include "usb_desc.h"
  4. AT(.rodata.usb.desc)
  5. const uint8_t usb_device_desc[] = {
  6. 0x12, /* Num bytes of the descriptor */
  7. DEVICE_DESCRIPTOR, /* Device Descriptor type */
  8. 0x10, /* Revision of USB Spec. LSB (in BCD) */
  9. 0x01, /* Revision of USB Spec. MSB (in BCD) */
  10. 0x00, /* Device class code */
  11. 0x00, /* Subclass code */
  12. 0x00, /* Device protocol */
  13. 0x40, /* Max packet size of Endpoint 0: only valid 8, 16, 32 or 64 */
  14. BYTE0(USB_DEV_VID), /* idVendor */
  15. BYTE1(USB_DEV_VID), /* idVendor */
  16. BYTE0(USB_DEV_PID), /* idProduct */
  17. BYTE1(USB_DEV_PID), /* idProduct */
  18. 0x00, /* Device Revision LSB (in BCD) */
  19. 0x01, /* Device Revision MSB (in BCD) */
  20. STR_MANUFACTURER, /* Index of Manufacture string descriptor */
  21. STR_PRODUCT, /* Index of Product string descriptor */
  22. STR_SERIAL_NUM, /* Index of Serial No. string descriptor */
  23. 0x01, /* NumConfigurations*/
  24. };
  25. AT(.rodata.usb.desc)
  26. const uint8_t usb_cfg_desc[] = {
  27. 0x09, /* Num bytes of this descriptor */
  28. CONFIGURATION_DESCRIPTOR, /* Configuration descriptor type */
  29. 0x00, /* Total size of configuration LSB*/
  30. 0x00, /* Total size of configuration MSB*/
  31. USB_ITF_NUM, /* Total Interface number*/
  32. 0x01, /* Configuration number */
  33. 0x00, /* Index of Configuration string descriptor */
  34. 0xA0, /* Configuration characteristics: BusPowerd */
  35. /* D7: reserved and should be one in historical version
  36. D6: self-powered device
  37. D5: remote wakeup
  38. D4...D0: reserved and should be set to zero */
  39. 0x32, /* Max current, unit is 2mA */
  40. };
  41. AT(.rodata.usb.desc)
  42. const uint8_t usb_lang_desc[] = {
  43. 4,
  44. 3,
  45. BYTE0(USB_DEV_LANG_ID),
  46. BYTE1(USB_DEV_LANG_ID),
  47. };
  48. AT(.usb_buf.desc)
  49. uint8_t usb_str_desc_buf[USB_MAX_STR_DESC_SIZE];
  50. static void usb_desc_swapper(u8 *dest, char *src, u8 *length)
  51. {
  52. u8 str_length = 0;
  53. u8 idx = 0;
  54. if (src != NULL) {
  55. while (src[str_length] != '\0' && str_length < USB_MAX_STR_DESC_SIZE) {
  56. str_length++;
  57. }
  58. *length = str_length * 2 + 2;
  59. dest[idx++] = *length;
  60. dest[idx++] = 0x03;
  61. while (*src != '\0') {
  62. dest[idx++] = *src++;
  63. dest[idx++] = 0x00;
  64. }
  65. }
  66. }
  67. uint8_t *usb_get_device_descriptor(uint8_t *length)
  68. {
  69. *length = sizeof(usb_device_desc);
  70. return (u8 *)usb_device_desc;
  71. }
  72. uint8_t *usb_get_cfg_descriptor(uint8_t *length)
  73. {
  74. *length = sizeof(usb_cfg_desc);
  75. return (u8 *)usb_cfg_desc;
  76. }
  77. uint8_t *usb_get_lang_id_str_descriptor(uint8_t *length)
  78. {
  79. *length = sizeof(usb_lang_desc);
  80. return (u8 *)usb_lang_desc;
  81. }
  82. uint8_t *usb_get_manufacturer_str_descriptor(uint8_t *length)
  83. {
  84. usb_desc_swapper(usb_str_desc_buf, USB_DESC_MANUF, length);
  85. return usb_str_desc_buf;
  86. }
  87. uint8_t *usb_get_product_str_descriptor(uint8_t *length)
  88. {
  89. usb_desc_swapper(usb_str_desc_buf, USB_DESC_PRODUCT, length);
  90. return usb_str_desc_buf;
  91. }
  92. uint8_t *usb_get_serial_str_descriptor(uint8_t *length)
  93. {
  94. usb_desc_swapper(usb_str_desc_buf, USB_DESC_SERIAL, length);
  95. return usb_str_desc_buf;
  96. }