usb_helpers.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 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. // ---------------------------------------- Configuration Descriptor Parsing -------------------------------------------
  17. const usb_standard_desc_t *usb_parse_next_descriptor(const usb_standard_desc_t *cur_desc, uint16_t wTotalLength, int *offset)
  18. {
  19. assert(cur_desc != NULL && offset != NULL);
  20. if (*offset >= wTotalLength) {
  21. return NULL; //We have traversed the entire configuration descriptor
  22. }
  23. if (*offset + cur_desc->bLength >= wTotalLength) {
  24. return NULL; //Next descriptor is out of bounds
  25. }
  26. //Return the next descriptor, update offset
  27. const usb_standard_desc_t *ret_desc = (const usb_standard_desc_t *)(((uint32_t)cur_desc) + cur_desc->bLength);
  28. *offset += cur_desc->bLength;
  29. return ret_desc;
  30. }
  31. 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)
  32. {
  33. assert(cur_desc != NULL && offset != NULL);
  34. int offset_temp = *offset; //We only want to update offset if we've actually found a descriptor
  35. //Keep stepping over descriptors until we find one of bDescriptorType or until we go out of bounds
  36. const usb_standard_desc_t *ret_desc = usb_parse_next_descriptor(cur_desc, wTotalLength, &offset_temp);
  37. while (ret_desc != NULL) {
  38. if (ret_desc->bDescriptorType == bDescriptorType) {
  39. break;
  40. }
  41. ret_desc = usb_parse_next_descriptor(ret_desc, wTotalLength, &offset_temp);
  42. }
  43. if (ret_desc != NULL) {
  44. //We've found a descriptor. Update the offset
  45. *offset = offset_temp;
  46. }
  47. return ret_desc;
  48. }
  49. int usb_parse_interface_number_of_alternate(const usb_config_desc_t *config_desc, uint8_t bInterfaceNumber)
  50. {
  51. assert(config_desc != NULL);
  52. int offset = 0;
  53. //Find the first interface descriptor of bInterfaceNumber
  54. const usb_intf_desc_t *first_intf_desc = usb_parse_interface_descriptor(config_desc, bInterfaceNumber, 0, &offset);
  55. if (first_intf_desc == NULL) {
  56. return -1; //bInterfaceNumber not found
  57. }
  58. int num_alt_setting = 0;
  59. 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);
  60. while (next_intf_desc != NULL) {
  61. if (next_intf_desc->bInterfaceNumber != bInterfaceNumber) {
  62. break;
  63. }
  64. num_alt_setting++;
  65. 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);
  66. }
  67. return num_alt_setting;
  68. }
  69. const usb_intf_desc_t *usb_parse_interface_descriptor(const usb_config_desc_t *config_desc, uint8_t bInterfaceNumber, uint8_t bAlternateSetting, int *offset)
  70. {
  71. assert(config_desc != NULL);
  72. if (bInterfaceNumber >= config_desc->bNumInterfaces) {
  73. return NULL; //bInterfaceNumber is out of range
  74. }
  75. //Walk to first interface descriptor of bInterfaceNumber
  76. int offset_temp = 0;
  77. 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);
  78. while (next_intf_desc != NULL) {
  79. if (next_intf_desc->bInterfaceNumber == bInterfaceNumber) {
  80. break; //We found the first interface descriptor with matching bInterfaceNumber
  81. }
  82. 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);
  83. }
  84. if (next_intf_desc == NULL) {
  85. return NULL; //Couldn't find a interface with bInterfaceNumber
  86. }
  87. //Keep walking until an interface descriptor matching bInterfaceNumber and bAlternateSetting is found
  88. while (next_intf_desc != NULL) {
  89. if (next_intf_desc->bInterfaceNumber == bInterfaceNumber + 1) {
  90. //We've walked past our target bInterfaceNumber
  91. next_intf_desc = NULL;
  92. break;
  93. }
  94. if (next_intf_desc->bAlternateSetting == bAlternateSetting) {
  95. //We've found our target interface descriptor
  96. break;
  97. }
  98. //Get the next interface descriptor
  99. 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);
  100. }
  101. if (next_intf_desc != NULL && offset != NULL) {
  102. *offset = offset_temp;
  103. }
  104. return next_intf_desc;
  105. }
  106. 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)
  107. {
  108. assert(intf_desc != NULL && offset != NULL);
  109. if (index >= intf_desc->bNumEndpoints) {
  110. return NULL; //Index is out of range
  111. }
  112. //Walk to the Nth endpoint descriptor we find
  113. int offset_temp = *offset;
  114. bool ep_found = true;
  115. const usb_standard_desc_t *next_desc = (const usb_standard_desc_t *)intf_desc;
  116. for (int i = 0; i <= index; i++) {
  117. next_desc = usb_parse_next_descriptor_of_type((const usb_standard_desc_t *)next_desc, wTotalLength, USB_B_DESCRIPTOR_TYPE_ENDPOINT, &offset_temp);
  118. if (next_desc == NULL) {
  119. ep_found = false;
  120. break;
  121. }
  122. }
  123. if (ep_found) {
  124. *offset = offset_temp;
  125. return (const usb_ep_desc_t *)next_desc;
  126. }
  127. return NULL;
  128. }
  129. 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)
  130. {
  131. assert(config_desc != NULL);
  132. //Find the interface descriptor
  133. int offset_intf;
  134. const usb_intf_desc_t *intf_desc = usb_parse_interface_descriptor(config_desc, bInterfaceNumber, bAlternateSetting, &offset_intf);
  135. if (intf_desc == NULL) {
  136. return NULL;
  137. }
  138. //Walk endpoint descriptors until one matching bEndpointAddress is found
  139. int offset_ep;
  140. bool ep_found = false;
  141. const usb_ep_desc_t *ep_desc = NULL;
  142. for (int index = 0; index < intf_desc->bNumEndpoints; index++) {
  143. offset_ep = offset_intf;
  144. ep_desc = usb_parse_endpoint_descriptor_by_index(intf_desc, index, config_desc->wTotalLength, &offset_ep);
  145. if (ep_desc == NULL) {
  146. break;
  147. }
  148. if (ep_desc->bEndpointAddress == bEndpointAddress) {
  149. ep_found = true;
  150. break;
  151. }
  152. }
  153. if (ep_found && offset != NULL) {
  154. *offset = offset_ep;
  155. }
  156. return ep_desc;
  157. }
  158. // ----------------------------------------------- Descriptor Printing -------------------------------------------------
  159. static void print_ep_desc(const usb_ep_desc_t *ep_desc)
  160. {
  161. const char *ep_type_str;
  162. int type = ep_desc->bmAttributes & USB_BM_ATTRIBUTES_XFERTYPE_MASK;
  163. switch (type) {
  164. case USB_BM_ATTRIBUTES_XFER_CONTROL:
  165. ep_type_str = "CTRL";
  166. break;
  167. case USB_BM_ATTRIBUTES_XFER_ISOC:
  168. ep_type_str = "ISOC";
  169. break;
  170. case USB_BM_ATTRIBUTES_XFER_BULK:
  171. ep_type_str = "BULK";
  172. break;
  173. case USB_BM_ATTRIBUTES_XFER_INT:
  174. ep_type_str = "INT";
  175. break;
  176. default:
  177. ep_type_str = NULL;
  178. break;
  179. }
  180. printf("\t\t*** Endpoint descriptor ***\n");
  181. printf("\t\tbLength %d\n", ep_desc->bLength);
  182. printf("\t\tbDescriptorType %d\n", ep_desc->bDescriptorType);
  183. printf("\t\tbEndpointAddress 0x%x\tEP %d %s\n", ep_desc->bEndpointAddress,
  184. USB_EP_DESC_GET_EP_NUM(ep_desc),
  185. USB_EP_DESC_GET_EP_DIR(ep_desc) ? "IN" : "OUT");
  186. printf("\t\tbmAttributes 0x%x\t%s\n", ep_desc->bmAttributes, ep_type_str);
  187. printf("\t\twMaxPacketSize %d\n", ep_desc->wMaxPacketSize);
  188. printf("\t\tbInterval %d\n", ep_desc->bInterval);
  189. }
  190. static void usbh_print_intf_desc(const usb_intf_desc_t *intf_desc)
  191. {
  192. printf("\t*** Interface descriptor ***\n");
  193. printf("\tbLength %d\n", intf_desc->bLength);
  194. printf("\tbDescriptorType %d\n", intf_desc->bDescriptorType);
  195. printf("\tbInterfaceNumber %d\n", intf_desc->bInterfaceNumber);
  196. printf("\tbAlternateSetting %d\n", intf_desc->bAlternateSetting);
  197. printf("\tbNumEndpoints %d\n", intf_desc->bNumEndpoints);
  198. printf("\tbInterfaceClass 0x%x\n", intf_desc->bInterfaceClass);
  199. printf("\tbInterfaceSubClass 0x%x\n", intf_desc->bInterfaceSubClass);
  200. printf("\tbInterfaceProtocol 0x%x\n", intf_desc->bInterfaceProtocol);
  201. printf("\tiInterface %d\n", intf_desc->iInterface);
  202. }
  203. static void usbh_print_cfg_desc(const usb_config_desc_t *cfg_desc)
  204. {
  205. printf("*** Configuration descriptor ***\n");
  206. printf("bLength %d\n", cfg_desc->bLength);
  207. printf("bDescriptorType %d\n", cfg_desc->bDescriptorType);
  208. printf("wTotalLength %d\n", cfg_desc->wTotalLength);
  209. printf("bNumInterfaces %d\n", cfg_desc->bNumInterfaces);
  210. printf("bConfigurationValue %d\n", cfg_desc->bConfigurationValue);
  211. printf("iConfiguration %d\n", cfg_desc->iConfiguration);
  212. printf("bmAttributes 0x%x\n", cfg_desc->bmAttributes);
  213. printf("bMaxPower %dmA\n", cfg_desc->bMaxPower * 2);
  214. }
  215. static void print_iad_desc(const usb_iad_desc_t *iad_desc)
  216. {
  217. printf("*** Interface Association Descriptor ***\n");
  218. printf("bLength %d\n", iad_desc->bLength);
  219. printf("bDescriptorType %d\n", iad_desc->bDescriptorType);
  220. printf("bFirstInterface %d\n", iad_desc->bFirstInterface);
  221. printf("bInterfaceCount %d\n", iad_desc->bInterfaceCount);
  222. printf("bFunctionClass 0x%x\n", iad_desc->bFunctionClass);
  223. printf("bFunctionSubClass 0x%x\n", iad_desc->bFunctionSubClass);
  224. printf("bFunctionProtocol 0x%x\n", iad_desc->bFunctionProtocol);
  225. printf("iFunction %d\n", iad_desc->iFunction);
  226. }
  227. void usb_print_device_descriptor(const usb_device_desc_t *devc_desc)
  228. {
  229. if (devc_desc == NULL) {
  230. return;
  231. }
  232. printf("*** Device descriptor ***\n");
  233. printf("bLength %d\n", devc_desc->bLength);
  234. printf("bDescriptorType %d\n", devc_desc->bDescriptorType);
  235. printf("bcdUSB %d.%d0\n", ((devc_desc->bcdUSB >> 8) & 0xF), ((devc_desc->bcdUSB >> 4) & 0xF));
  236. printf("bDeviceClass 0x%x\n", devc_desc->bDeviceClass);
  237. printf("bDeviceSubClass 0x%x\n", devc_desc->bDeviceSubClass);
  238. printf("bDeviceProtocol 0x%x\n", devc_desc->bDeviceProtocol);
  239. printf("bMaxPacketSize0 %d\n", devc_desc->bMaxPacketSize0);
  240. printf("idVendor 0x%x\n", devc_desc->idVendor);
  241. printf("idProduct 0x%x\n", devc_desc->idProduct);
  242. printf("bcdDevice %d.%d0\n", ((devc_desc->bcdDevice >> 8) & 0xF), ((devc_desc->bcdDevice >> 4) & 0xF));
  243. printf("iManufacturer %d\n", devc_desc->iManufacturer);
  244. printf("iProduct %d\n", devc_desc->iProduct);
  245. printf("iSerialNumber %d\n", devc_desc->iSerialNumber);
  246. printf("bNumConfigurations %d\n", devc_desc->bNumConfigurations);
  247. }
  248. void usb_print_config_descriptor(const usb_config_desc_t *cfg_desc, print_class_descriptor_cb class_specific_cb)
  249. {
  250. if (cfg_desc == NULL) {
  251. return;
  252. }
  253. int offset = 0;
  254. uint16_t wTotalLength = cfg_desc->wTotalLength;
  255. const usb_standard_desc_t *next_desc = (const usb_standard_desc_t *)cfg_desc;
  256. do {
  257. switch (next_desc->bDescriptorType) {
  258. case USB_B_DESCRIPTOR_TYPE_CONFIGURATION:
  259. usbh_print_cfg_desc((const usb_config_desc_t *)next_desc);
  260. break;
  261. case USB_B_DESCRIPTOR_TYPE_INTERFACE:
  262. usbh_print_intf_desc((const usb_intf_desc_t *)next_desc);
  263. break;
  264. case USB_B_DESCRIPTOR_TYPE_ENDPOINT:
  265. print_ep_desc((const usb_ep_desc_t *)next_desc);
  266. break;
  267. case USB_B_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION:
  268. print_iad_desc((const usb_iad_desc_t*)next_desc);
  269. break;
  270. default:
  271. if(class_specific_cb) {
  272. class_specific_cb(next_desc);
  273. }
  274. break;
  275. }
  276. next_desc = usb_parse_next_descriptor(next_desc, wTotalLength, &offset);
  277. } while (next_desc != NULL);
  278. }
  279. void usb_print_string_descriptor(const usb_str_desc_t *str_desc)
  280. {
  281. if (str_desc == NULL) {
  282. return;
  283. }
  284. for (int i = 0; i < str_desc->bLength/2; i++) {
  285. /*
  286. USB String descriptors of UTF-16.
  287. Right now We just skip any character larger than 0xFF to stay in BMP Basic Latin and Latin-1 Supplement range.
  288. */
  289. if (str_desc->wData[i] > 0xFF) {
  290. continue;
  291. }
  292. printf("%c", (char)str_desc->wData[i]);
  293. }
  294. printf("\n");
  295. }