usb_helpers.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stdbool.h>
  8. #include <stdlib.h>
  9. #include <assert.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "usb/usb_helpers.h"
  13. #include "usb/usb_types_ch9.h"
  14. #include "esp_check.h"
  15. #include "usb/usb_host.h"
  16. static const char *TAG = "usb_helper";
  17. // ---------------------------------------- Configuration Descriptor Parsing -------------------------------------------
  18. const usb_standard_desc_t *usb_parse_next_descriptor(const usb_standard_desc_t *cur_desc, uint16_t wTotalLength, int *offset)
  19. {
  20. assert(cur_desc != NULL && offset != NULL);
  21. if (*offset >= wTotalLength) {
  22. return NULL; //We have traversed the entire configuration descriptor
  23. }
  24. if (*offset + cur_desc->bLength >= wTotalLength) {
  25. return NULL; //Next descriptor is out of bounds
  26. }
  27. //Return the next descriptor, update offset
  28. const usb_standard_desc_t *ret_desc = (const usb_standard_desc_t *)(((uint32_t)cur_desc) + cur_desc->bLength);
  29. *offset += cur_desc->bLength;
  30. return ret_desc;
  31. }
  32. const usb_standard_desc_t *usb_parse_next_descriptor_of_type(const usb_standard_desc_t *cur_desc, uint16_t wTotalLength, uint8_t bDescriptorType, int *offset)
  33. {
  34. assert(cur_desc != NULL && offset != NULL);
  35. int offset_temp = *offset; //We only want to update offset if we've actually found a descriptor
  36. //Keep stepping over descriptors until we find one of bDescriptorType or until we go out of bounds
  37. const usb_standard_desc_t *ret_desc = usb_parse_next_descriptor(cur_desc, wTotalLength, &offset_temp);
  38. while (ret_desc != NULL) {
  39. if (ret_desc->bDescriptorType == bDescriptorType) {
  40. break;
  41. }
  42. ret_desc = usb_parse_next_descriptor(ret_desc, wTotalLength, &offset_temp);
  43. }
  44. if (ret_desc != NULL) {
  45. //We've found a descriptor. Update the offset
  46. *offset = offset_temp;
  47. }
  48. return ret_desc;
  49. }
  50. int usb_parse_interface_number_of_alternate(const usb_config_desc_t *config_desc, uint8_t bInterfaceNumber)
  51. {
  52. assert(config_desc != NULL);
  53. int offset = 0;
  54. //Find the first interface descriptor of bInterfaceNumber
  55. const usb_intf_desc_t *first_intf_desc = usb_parse_interface_descriptor(config_desc, bInterfaceNumber, 0, &offset);
  56. if (first_intf_desc == NULL) {
  57. return -1; //bInterfaceNumber not found
  58. }
  59. int num_alt_setting = 0;
  60. const usb_intf_desc_t *next_intf_desc = (const usb_intf_desc_t *)usb_parse_next_descriptor_of_type((const usb_standard_desc_t *)first_intf_desc, config_desc->wTotalLength, USB_B_DESCRIPTOR_TYPE_INTERFACE, &offset);
  61. while (next_intf_desc != NULL) {
  62. if (next_intf_desc->bInterfaceNumber != bInterfaceNumber) {
  63. break;
  64. }
  65. num_alt_setting++;
  66. next_intf_desc = (const usb_intf_desc_t *)usb_parse_next_descriptor_of_type((const usb_standard_desc_t *)next_intf_desc, config_desc->wTotalLength, USB_B_DESCRIPTOR_TYPE_INTERFACE, &offset);
  67. }
  68. return num_alt_setting;
  69. }
  70. const usb_intf_desc_t *usb_parse_interface_descriptor(const usb_config_desc_t *config_desc, uint8_t bInterfaceNumber, uint8_t bAlternateSetting, int *offset)
  71. {
  72. assert(config_desc != NULL);
  73. if (bInterfaceNumber >= config_desc->bNumInterfaces) {
  74. return NULL; //bInterfaceNumber is out of range
  75. }
  76. //Walk to first interface descriptor of bInterfaceNumber
  77. int offset_temp = 0;
  78. const usb_intf_desc_t *next_intf_desc = (const usb_intf_desc_t *)usb_parse_next_descriptor_of_type((const usb_standard_desc_t *)config_desc, config_desc->wTotalLength, USB_B_DESCRIPTOR_TYPE_INTERFACE, &offset_temp);
  79. while (next_intf_desc != NULL) {
  80. if (next_intf_desc->bInterfaceNumber == bInterfaceNumber) {
  81. break; //We found the first interface descriptor with matching bInterfaceNumber
  82. }
  83. next_intf_desc = (const usb_intf_desc_t *)usb_parse_next_descriptor_of_type((const usb_standard_desc_t *)next_intf_desc, config_desc->wTotalLength, USB_B_DESCRIPTOR_TYPE_INTERFACE, &offset_temp);
  84. }
  85. if (next_intf_desc == NULL) {
  86. return NULL; //Couldn't find a interface with bInterfaceNumber
  87. }
  88. //Keep walking until an interface descriptor matching bInterfaceNumber and bAlternateSetting is found
  89. while (next_intf_desc != NULL) {
  90. if (next_intf_desc->bInterfaceNumber == bInterfaceNumber + 1) {
  91. //We've walked past our target bInterfaceNumber
  92. next_intf_desc = NULL;
  93. break;
  94. }
  95. if (next_intf_desc->bAlternateSetting == bAlternateSetting) {
  96. //We've found our target interface descriptor
  97. break;
  98. }
  99. //Get the next interface descriptor
  100. next_intf_desc = (const usb_intf_desc_t *)usb_parse_next_descriptor_of_type((const usb_standard_desc_t *)next_intf_desc, config_desc->wTotalLength, USB_B_DESCRIPTOR_TYPE_INTERFACE, &offset_temp);
  101. }
  102. if (next_intf_desc != NULL && offset != NULL) {
  103. *offset = offset_temp;
  104. }
  105. return next_intf_desc;
  106. }
  107. const usb_ep_desc_t *usb_parse_endpoint_descriptor_by_index(const usb_intf_desc_t *intf_desc, int index, uint16_t wTotalLength, int *offset)
  108. {
  109. assert(intf_desc != NULL && offset != NULL);
  110. if (index >= intf_desc->bNumEndpoints) {
  111. return NULL; //Index is out of range
  112. }
  113. //Walk to the Nth endpoint descriptor we find
  114. int offset_temp = *offset;
  115. bool ep_found = true;
  116. const usb_standard_desc_t *next_desc = (const usb_standard_desc_t *)intf_desc;
  117. for (int i = 0; i <= index; i++) {
  118. next_desc = usb_parse_next_descriptor_of_type((const usb_standard_desc_t *)next_desc, wTotalLength, USB_B_DESCRIPTOR_TYPE_ENDPOINT, &offset_temp);
  119. if (next_desc == NULL) {
  120. ep_found = false;
  121. break;
  122. }
  123. }
  124. if (ep_found) {
  125. *offset = offset_temp;
  126. return (const usb_ep_desc_t *)next_desc;
  127. }
  128. return NULL;
  129. }
  130. const usb_ep_desc_t *usb_parse_endpoint_descriptor_by_address(const usb_config_desc_t *config_desc, uint8_t bInterfaceNumber, uint8_t bAlternateSetting, uint8_t bEndpointAddress, int *offset)
  131. {
  132. assert(config_desc != NULL);
  133. //Find the interface descriptor
  134. int offset_intf;
  135. const usb_intf_desc_t *intf_desc = usb_parse_interface_descriptor(config_desc, bInterfaceNumber, bAlternateSetting, &offset_intf);
  136. if (intf_desc == NULL) {
  137. return NULL;
  138. }
  139. //Walk endpoint descriptors until one matching bEndpointAddress is found
  140. int offset_ep;
  141. bool ep_found = false;
  142. const usb_ep_desc_t *ep_desc = NULL;
  143. for (int index = 0; index < intf_desc->bNumEndpoints; index++) {
  144. offset_ep = offset_intf;
  145. ep_desc = usb_parse_endpoint_descriptor_by_index(intf_desc, index, config_desc->wTotalLength, &offset_ep);
  146. if (ep_desc == NULL) {
  147. break;
  148. }
  149. if (ep_desc->bEndpointAddress == bEndpointAddress) {
  150. ep_found = true;
  151. break;
  152. }
  153. }
  154. if (ep_found && offset != NULL) {
  155. *offset = offset_ep;
  156. }
  157. return ep_desc;
  158. }
  159. // ------------------------------------------ Descriptor printing ---------------------------------------------
  160. static void print_ep_desc(const usb_ep_desc_t *ep_desc)
  161. {
  162. const char *ep_type_str;
  163. int type = ep_desc->bmAttributes & USB_BM_ATTRIBUTES_XFERTYPE_MASK;
  164. switch (type) {
  165. case USB_BM_ATTRIBUTES_XFER_CONTROL:
  166. ep_type_str = "CTRL";
  167. break;
  168. case USB_BM_ATTRIBUTES_XFER_ISOC:
  169. ep_type_str = "ISOC";
  170. break;
  171. case USB_BM_ATTRIBUTES_XFER_BULK:
  172. ep_type_str = "BULK";
  173. break;
  174. case USB_BM_ATTRIBUTES_XFER_INT:
  175. ep_type_str = "INT";
  176. break;
  177. default:
  178. ep_type_str = NULL;
  179. break;
  180. }
  181. printf("\t\t*** Endpoint descriptor ***\n");
  182. printf("\t\tbLength %d\n", ep_desc->bLength);
  183. printf("\t\tbDescriptorType %d\n", ep_desc->bDescriptorType);
  184. printf("\t\tbEndpointAddress 0x%x\tEP %d %s\n", ep_desc->bEndpointAddress,
  185. USB_EP_DESC_GET_EP_NUM(ep_desc),
  186. USB_EP_DESC_GET_EP_DIR(ep_desc) ? "IN" : "OUT");
  187. printf("\t\tbmAttributes 0x%x\t%s\n", ep_desc->bmAttributes, ep_type_str);
  188. printf("\t\twMaxPacketSize %d\n", ep_desc->wMaxPacketSize);
  189. printf("\t\tbInterval %d\n", ep_desc->bInterval);
  190. }
  191. static void usbh_print_intf_desc(const usb_intf_desc_t *intf_desc)
  192. {
  193. printf("\t*** Interface descriptor ***\n");
  194. printf("\tbLength %d\n", intf_desc->bLength);
  195. printf("\tbDescriptorType %d\n", intf_desc->bDescriptorType);
  196. printf("\tbInterfaceNumber %d\n", intf_desc->bInterfaceNumber);
  197. printf("\tbAlternateSetting %d\n", intf_desc->bAlternateSetting);
  198. printf("\tbNumEndpoints %d\n", intf_desc->bNumEndpoints);
  199. printf("\tbInterfaceClass 0x%x\n", intf_desc->bInterfaceProtocol);
  200. printf("\tiInterface %d\n", intf_desc->iInterface);
  201. }
  202. static void usbh_print_cfg_desc(const usb_config_desc_t *cfg_desc)
  203. {
  204. printf("*** Configuration descriptor ***\n");
  205. printf("bLength %d\n", cfg_desc->bLength);
  206. printf("bDescriptorType %d\n", cfg_desc->bDescriptorType);
  207. printf("wTotalLength %d\n", cfg_desc->wTotalLength);
  208. printf("bNumInterfaces %d\n", cfg_desc->bNumInterfaces);
  209. printf("bConfigurationValue %d\n", cfg_desc->bConfigurationValue);
  210. printf("iConfiguration %d\n", cfg_desc->iConfiguration);
  211. printf("bmAttributes 0x%x\n", cfg_desc->bmAttributes);
  212. printf("bMaxPower %dmA\n", cfg_desc->bMaxPower * 2);
  213. }
  214. static void print_device_descriptor(const usb_device_desc_t *devc_desc)
  215. {
  216. printf("*** Device descriptor ***\n");
  217. printf("bLength %d\n", devc_desc->bLength);
  218. printf("bDescriptorType %d\n", devc_desc->bDescriptorType);
  219. printf("bcdUSB %d.%d0\n", ((devc_desc->bcdUSB >> 8) & 0xF), ((devc_desc->bcdUSB >> 4) & 0xF));
  220. printf("bDeviceClass 0x%x\n", devc_desc->bDeviceClass);
  221. printf("bDeviceSubClass 0x%x\n", devc_desc->bDeviceSubClass);
  222. printf("bDeviceProtocol 0x%x\n", devc_desc->bDeviceProtocol);
  223. printf("bMaxPacketSize0 %d\n", devc_desc->bMaxPacketSize0);
  224. printf("idVendor 0x%x\n", devc_desc->idVendor);
  225. printf("idProduct 0x%x\n", devc_desc->idProduct);
  226. printf("bcdDevice %d.%d0\n", ((devc_desc->bcdDevice >> 8) & 0xF), ((devc_desc->bcdDevice >> 4) & 0xF));
  227. printf("iManufacturer %d\n", devc_desc->iManufacturer);
  228. printf("iProduct %d\n", devc_desc->iProduct);
  229. printf("iSerialNumber %d\n", devc_desc->iSerialNumber);
  230. printf("bNumConfigurations %d\n", devc_desc->bNumConfigurations);
  231. }
  232. static void print_config_descriptor(const usb_config_desc_t *cfg_desc, print_class_descriptor_cb class_specific_cb)
  233. {
  234. int offset = 0;
  235. uint16_t wTotalLength = cfg_desc->wTotalLength;
  236. const usb_standard_desc_t *next_desc = (const usb_standard_desc_t *)cfg_desc;
  237. do {
  238. switch (next_desc->bDescriptorType) {
  239. case USB_W_VALUE_DT_CONFIG:
  240. usbh_print_cfg_desc((const usb_config_desc_t *)next_desc);
  241. break;
  242. case USB_W_VALUE_DT_INTERFACE:
  243. usbh_print_intf_desc((const usb_intf_desc_t *)next_desc);
  244. break;
  245. case USB_W_VALUE_DT_ENDPOINT:
  246. print_ep_desc((const usb_ep_desc_t *)next_desc);
  247. break;
  248. default:
  249. if(class_specific_cb) {
  250. class_specific_cb(next_desc);
  251. }
  252. break;
  253. }
  254. next_desc = usb_parse_next_descriptor(next_desc, wTotalLength, &offset);
  255. } while (next_desc != NULL);
  256. }
  257. esp_err_t usb_print_descriptors(usb_device_handle_t device, print_class_descriptor_cb class_specific_cb)
  258. {
  259. if (device == NULL) {
  260. return ESP_ERR_INVALID_ARG;
  261. }
  262. const usb_config_desc_t *config_desc;
  263. const usb_device_desc_t *device_desc;
  264. ESP_RETURN_ON_ERROR( usb_host_get_device_descriptor(device, &device_desc), TAG, "Failed to get devices descriptor" );
  265. ESP_RETURN_ON_ERROR( usb_host_get_active_config_descriptor(device, &config_desc), TAG, "Failed to get config descriptor" );
  266. print_device_descriptor(device_desc);
  267. print_config_descriptor(config_desc, class_specific_cb);
  268. return ESP_OK;
  269. }
  270. // ------------------------------------------------------ Misc ---------------------------------------------------------