usbh.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <sys/queue.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/portmacro.h"
  13. #include "freertos/task.h"
  14. #include "esp_err.h"
  15. #include "esp_log.h"
  16. #include "esp_heap_caps.h"
  17. #include "hcd.h"
  18. #include "usbh.h"
  19. #include "usb/usb_helpers.h"
  20. #include "usb/usb_types_ch9.h"
  21. //Device action flags. Listed in the order they should handled in. Some actions are mutually exclusive
  22. #define DEV_FLAG_ACTION_SEND_GONE_EVENT 0x01 //Send a USB_HOST_CLIENT_EVENT_DEV_GONE event
  23. #define DEV_FLAG_ACTION_DEFAULT_PIPE_FLUSH 0x02 //Retire all URBS in the default pipe
  24. #define DEV_FLAG_ACTION_DEFAULT_PIPE_DEQUEUE 0x04 //Dequeue all URBs from default pipe
  25. #define DEV_FLAG_ACTION_DEFAULT_PIPE_CLEAR 0x08 //Move the default pipe to the active state
  26. #define DEV_FLAG_ACTION_FREE 0x10 //Free the device object
  27. #define DEV_FLAG_ACTION_PORT_DISABLE 0x20
  28. #define DEV_FLAG_ACTION_SEND_NEW 0x40 //Send a new device event
  29. #define DEV_ENUM_TODO_FLAG_DEV_ADDR 0x01
  30. #define DEV_ENUM_TODO_FLAG_DEV_DESC 0x02
  31. #define DEV_ENUM_TODO_FLAG_CONFIG_DESC 0x04
  32. #define EP_NUM_MIN 1
  33. #define EP_NUM_MAX 16
  34. typedef struct device_s device_t;
  35. struct device_s {
  36. //Dynamic members require a critical section
  37. struct {
  38. TAILQ_ENTRY(device_s) tailq_entry;
  39. union {
  40. struct {
  41. uint32_t actions: 8;
  42. uint32_t in_pending_list: 1;
  43. uint32_t is_gone: 1;
  44. uint32_t waiting_close: 1;
  45. uint32_t waiting_port_disable: 1;
  46. uint32_t waiting_free: 1;
  47. uint32_t reserved19: 19;
  48. };
  49. uint32_t val;
  50. } flags;
  51. int num_ctrl_xfers_inflight;
  52. usb_device_state_t state;
  53. uint32_t ref_count;
  54. usb_config_desc_t *config_desc;
  55. hcd_pipe_handle_t ep_in[EP_NUM_MAX - 1]; //IN EP owner contexts. -1 to exclude the default endpoint
  56. hcd_pipe_handle_t ep_out[EP_NUM_MAX - 1]; //OUT EP owner contexts. -1 to exclude the default endpoint
  57. } dynamic;
  58. //Constant members do no change after device allocation and enumeration thus do not require a critical section
  59. struct {
  60. hcd_pipe_handle_t default_pipe;
  61. hcd_port_handle_t port_hdl;
  62. uint8_t address;
  63. usb_speed_t speed;
  64. const usb_device_desc_t *desc;
  65. uint32_t enum_todo_flags;
  66. } constant;
  67. };
  68. typedef struct {
  69. //Dynamic members require a critical section
  70. struct {
  71. TAILQ_HEAD(tailhead_devs, device_s) devs_idle_tailq; //Tailq of all enum and configured devices
  72. TAILQ_HEAD(tailhead_devs_cb, device_s) devs_pending_tailq; //Tailq of devices that need to have their cb called
  73. uint8_t num_device; //Number of enumerated devices
  74. } dynamic;
  75. //Constant members do no change after installation thus do not require a critical section
  76. struct {
  77. usb_notif_cb_t notif_cb;
  78. void *notif_cb_arg;
  79. usbh_hub_cb_t hub_cb;
  80. void *hub_cb_arg;
  81. usbh_event_cb_t event_cb;
  82. void *event_cb_arg;
  83. usbh_ctrl_xfer_cb_t ctrl_xfer_cb;
  84. void *ctrl_xfer_cb_arg;
  85. } constant;
  86. } usbh_t;
  87. static usbh_t *p_usbh_obj = NULL;
  88. static portMUX_TYPE usbh_lock = portMUX_INITIALIZER_UNLOCKED;
  89. const char *USBH_TAG = "USBH";
  90. #define USBH_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&usbh_lock)
  91. #define USBH_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&usbh_lock)
  92. #define USBH_ENTER_CRITICAL() portENTER_CRITICAL(&usbh_lock)
  93. #define USBH_EXIT_CRITICAL() portEXIT_CRITICAL(&usbh_lock)
  94. #define USBH_ENTER_CRITICAL_SAFE() portENTER_CRITICAL_SAFE(&usbh_lock)
  95. #define USBH_EXIT_CRITICAL_SAFE() portEXIT_CRITICAL_SAFE(&usbh_lock)
  96. #define USBH_CHECK(cond, ret_val) ({ \
  97. if (!(cond)) { \
  98. return (ret_val); \
  99. } \
  100. })
  101. #define USBH_CHECK_FROM_CRIT(cond, ret_val) ({ \
  102. if (!(cond)) { \
  103. USBH_EXIT_CRITICAL(); \
  104. return ret_val; \
  105. } \
  106. })
  107. // --------------------------------------------------- Allocation ------------------------------------------------------
  108. static esp_err_t device_alloc(hcd_port_handle_t port_hdl, usb_speed_t speed, device_t **dev_obj_ret)
  109. {
  110. esp_err_t ret;
  111. device_t *dev_obj = heap_caps_calloc(1, sizeof(device_t), MALLOC_CAP_DEFAULT);
  112. usb_device_desc_t *dev_desc = heap_caps_calloc(1, sizeof(usb_device_desc_t), MALLOC_CAP_DEFAULT);
  113. if (dev_obj == NULL || dev_desc == NULL) {
  114. ret = ESP_ERR_NO_MEM;
  115. goto err;
  116. }
  117. //Allocate default pipe. We set the pipe callback to NULL for now
  118. hcd_pipe_config_t pipe_config = {
  119. .callback = NULL,
  120. .callback_arg = NULL,
  121. .context = (void *)dev_obj,
  122. .ep_desc = NULL, //No endpoint descriptor means we're allocating a default pipe
  123. .dev_speed = speed,
  124. .dev_addr = 0,
  125. };
  126. hcd_pipe_handle_t default_pipe_hdl;
  127. ret = hcd_pipe_alloc(port_hdl, &pipe_config, &default_pipe_hdl);
  128. if (ret != ESP_OK) {
  129. goto err;
  130. }
  131. //Initialize device object
  132. dev_obj->dynamic.state = USB_DEVICE_STATE_DEFAULT;
  133. dev_obj->constant.default_pipe = default_pipe_hdl;
  134. dev_obj->constant.port_hdl = port_hdl;
  135. //Note: dev_obj->constant.address is assigned later during enumeration
  136. dev_obj->constant.speed = speed;
  137. dev_obj->constant.desc = dev_desc;
  138. dev_obj->constant.enum_todo_flags = (DEV_ENUM_TODO_FLAG_DEV_ADDR | DEV_ENUM_TODO_FLAG_DEV_DESC | DEV_ENUM_TODO_FLAG_CONFIG_DESC);
  139. *dev_obj_ret = dev_obj;
  140. ret = ESP_OK;
  141. return ret;
  142. err:
  143. heap_caps_free(dev_desc);
  144. heap_caps_free(dev_obj);
  145. return ret;
  146. }
  147. static void device_free(device_t *dev_obj)
  148. {
  149. if (dev_obj == NULL) {
  150. return;
  151. }
  152. //Configuration must be freed
  153. assert(dev_obj->dynamic.config_desc == NULL);
  154. ESP_ERROR_CHECK(hcd_pipe_free(dev_obj->constant.default_pipe));
  155. heap_caps_free((usb_device_desc_t *)dev_obj->constant.desc);
  156. heap_caps_free(dev_obj);
  157. }
  158. // -------------------------------------------------- Event Related ----------------------------------------------------
  159. static bool _dev_set_actions(device_t *dev_obj, uint32_t action_flags)
  160. {
  161. if (action_flags == 0) {
  162. return false;
  163. }
  164. bool call_notif_cb;
  165. //Check if device is already on the callback list
  166. if (!dev_obj->dynamic.flags.in_pending_list) {
  167. //Move device form idle device list to callback device list
  168. TAILQ_REMOVE(&p_usbh_obj->dynamic.devs_idle_tailq, dev_obj, dynamic.tailq_entry);
  169. TAILQ_INSERT_TAIL(&p_usbh_obj->dynamic.devs_pending_tailq, dev_obj, dynamic.tailq_entry);
  170. dev_obj->dynamic.flags.actions |= action_flags;
  171. dev_obj->dynamic.flags.in_pending_list = 1;
  172. call_notif_cb = true;
  173. } else {
  174. call_notif_cb = false;
  175. }
  176. return call_notif_cb;
  177. }
  178. static bool default_pipe_callback(hcd_pipe_handle_t pipe_hdl, hcd_pipe_event_t pipe_event, void *user_arg, bool in_isr)
  179. {
  180. uint32_t action_flags;
  181. device_t *dev_obj = (device_t *)user_arg;
  182. switch (pipe_event) {
  183. case HCD_PIPE_EVENT_URB_DONE:
  184. //A control transfer completed on the default pipe. We need to dequeue it
  185. action_flags = DEV_FLAG_ACTION_DEFAULT_PIPE_DEQUEUE;
  186. break;
  187. case HCD_PIPE_EVENT_ERROR_XFER:
  188. case HCD_PIPE_EVENT_ERROR_URB_NOT_AVAIL:
  189. case HCD_PIPE_EVENT_ERROR_OVERFLOW:
  190. //The default pipe has encountered an error. We need to retire all URBs, dequeue them, then make the pipe active again
  191. action_flags = DEV_FLAG_ACTION_DEFAULT_PIPE_FLUSH |
  192. DEV_FLAG_ACTION_DEFAULT_PIPE_DEQUEUE |
  193. DEV_FLAG_ACTION_DEFAULT_PIPE_CLEAR;
  194. if (in_isr) {
  195. ESP_EARLY_LOGE(USBH_TAG, "Dev %d EP 0 Error", dev_obj->constant.address);
  196. } else {
  197. ESP_LOGE(USBH_TAG, "Dev %d EP 0 Error", dev_obj->constant.address);
  198. }
  199. break;
  200. case HCD_PIPE_EVENT_ERROR_STALL:
  201. //The default pipe encountered a "protocol stall". We just need to dequeue URBs then make the pipe active again
  202. action_flags = DEV_FLAG_ACTION_DEFAULT_PIPE_DEQUEUE | DEV_FLAG_ACTION_DEFAULT_PIPE_CLEAR;
  203. if (in_isr) {
  204. ESP_EARLY_LOGE(USBH_TAG, "Dev %d EP 0 STALL", dev_obj->constant.address);
  205. } else {
  206. ESP_LOGE(USBH_TAG, "Dev %d EP 0 STALL", dev_obj->constant.address);
  207. }
  208. break;
  209. default:
  210. action_flags = 0;
  211. break;
  212. }
  213. USBH_ENTER_CRITICAL_SAFE();
  214. bool call_notif_cb = _dev_set_actions(dev_obj, action_flags);
  215. USBH_EXIT_CRITICAL_SAFE();
  216. bool yield = false;
  217. if (call_notif_cb) {
  218. yield = p_usbh_obj->constant.notif_cb(USB_NOTIF_SOURCE_USBH, in_isr, p_usbh_obj->constant.notif_cb_arg);
  219. }
  220. return yield;
  221. }
  222. static bool handle_dev_free(device_t *dev_obj)
  223. {
  224. USBH_ENTER_CRITICAL();
  225. //Remove the device object for it's containing list
  226. if (dev_obj->dynamic.flags.in_pending_list) {
  227. dev_obj->dynamic.flags.in_pending_list = 0;
  228. TAILQ_REMOVE(&p_usbh_obj->dynamic.devs_pending_tailq, dev_obj, dynamic.tailq_entry);
  229. } else {
  230. TAILQ_REMOVE(&p_usbh_obj->dynamic.devs_idle_tailq, dev_obj, dynamic.tailq_entry);
  231. }
  232. p_usbh_obj->dynamic.num_device--;
  233. bool all_free = (p_usbh_obj->dynamic.num_device == 0);
  234. USBH_EXIT_CRITICAL();
  235. heap_caps_free(dev_obj->dynamic.config_desc);
  236. dev_obj->dynamic.config_desc = NULL;
  237. device_free(dev_obj);
  238. return all_free;
  239. }
  240. // ------------------------------------------------- USBH Functions ----------------------------------------------------
  241. esp_err_t usbh_install(const usbh_config_t *usbh_config)
  242. {
  243. USBH_CHECK(usbh_config != NULL, ESP_ERR_INVALID_ARG);
  244. USBH_ENTER_CRITICAL();
  245. USBH_CHECK_FROM_CRIT(p_usbh_obj == NULL, ESP_ERR_INVALID_STATE);
  246. USBH_EXIT_CRITICAL();
  247. usbh_t *usbh_obj = heap_caps_calloc(1, sizeof(usbh_t), MALLOC_CAP_DEFAULT);
  248. if (usbh_obj == NULL) {
  249. return ESP_ERR_NO_MEM;
  250. }
  251. esp_err_t ret;
  252. //Install HCD
  253. ret = hcd_install(&usbh_config->hcd_config);
  254. if (ret != ESP_OK) {
  255. goto hcd_install_err;
  256. }
  257. //Initialize usbh object
  258. TAILQ_INIT(&usbh_obj->dynamic.devs_idle_tailq);
  259. TAILQ_INIT(&usbh_obj->dynamic.devs_pending_tailq);
  260. usbh_obj->constant.notif_cb = usbh_config->notif_cb;
  261. usbh_obj->constant.notif_cb_arg = usbh_config->notif_cb_arg;
  262. usbh_obj->constant.event_cb = usbh_config->event_cb;
  263. usbh_obj->constant.event_cb_arg = usbh_config->event_cb_arg;
  264. usbh_obj->constant.ctrl_xfer_cb = usbh_config->ctrl_xfer_cb;
  265. usbh_obj->constant.ctrl_xfer_cb_arg = usbh_config->ctrl_xfer_cb_arg;
  266. //Assign usbh object pointer
  267. USBH_ENTER_CRITICAL();
  268. if (p_usbh_obj != NULL) {
  269. USBH_EXIT_CRITICAL();
  270. ret = ESP_ERR_INVALID_STATE;
  271. goto assign_err;
  272. }
  273. p_usbh_obj = usbh_obj;
  274. USBH_EXIT_CRITICAL();
  275. ret = ESP_OK;
  276. return ret;
  277. assign_err:
  278. ESP_ERROR_CHECK(hcd_uninstall());
  279. hcd_install_err:
  280. heap_caps_free(usbh_obj);
  281. return ret;
  282. }
  283. esp_err_t usbh_uninstall(void)
  284. {
  285. USBH_ENTER_CRITICAL();
  286. USBH_CHECK_FROM_CRIT(p_usbh_obj != NULL, ESP_ERR_INVALID_STATE);
  287. //Check that USBH is in a state to be uninstalled
  288. USBH_CHECK_FROM_CRIT(p_usbh_obj->dynamic.num_device == 0, ESP_ERR_INVALID_STATE);
  289. usbh_t *usbh_obj = p_usbh_obj;
  290. p_usbh_obj = NULL;
  291. USBH_EXIT_CRITICAL();
  292. //Uninstall HCD
  293. ESP_ERROR_CHECK(hcd_uninstall());
  294. heap_caps_free(usbh_obj);
  295. return ESP_OK;
  296. }
  297. esp_err_t usbh_process(void)
  298. {
  299. USBH_ENTER_CRITICAL();
  300. USBH_CHECK_FROM_CRIT(p_usbh_obj != NULL, ESP_ERR_INVALID_STATE);
  301. //Keep clearing devices with events
  302. while (!TAILQ_EMPTY(&p_usbh_obj->dynamic.devs_pending_tailq)){
  303. //Move the device back into the idle device list,
  304. device_t *dev_obj = TAILQ_FIRST(&p_usbh_obj->dynamic.devs_pending_tailq);
  305. TAILQ_REMOVE(&p_usbh_obj->dynamic.devs_pending_tailq, dev_obj, dynamic.tailq_entry);
  306. TAILQ_INSERT_TAIL(&p_usbh_obj->dynamic.devs_idle_tailq, dev_obj, dynamic.tailq_entry);
  307. //Clear the device's flags
  308. uint32_t action_flags = dev_obj->dynamic.flags.actions;
  309. dev_obj->dynamic.flags.actions = 0;
  310. dev_obj->dynamic.flags.in_pending_list = 0;
  311. USBH_EXIT_CRITICAL();
  312. ESP_LOGD(USBH_TAG, "Processing actions 0x%x", action_flags);
  313. //Sanity check. If the device is being freed, there must not be any other action flags set
  314. assert(!(action_flags & DEV_FLAG_ACTION_FREE) || action_flags == DEV_FLAG_ACTION_FREE);
  315. if (action_flags & DEV_FLAG_ACTION_SEND_GONE_EVENT) {
  316. //Flush the default pipe. Then do an event gone
  317. ESP_LOGE(USBH_TAG, "Device %d gone", dev_obj->constant.address);
  318. p_usbh_obj->constant.event_cb((usb_device_handle_t)dev_obj, USBH_EVENT_DEV_GONE, p_usbh_obj->constant.event_cb_arg);
  319. }
  320. if (action_flags & DEV_FLAG_ACTION_DEFAULT_PIPE_FLUSH) {
  321. ESP_ERROR_CHECK(hcd_pipe_command(dev_obj->constant.default_pipe, HCD_PIPE_CMD_HALT));
  322. ESP_ERROR_CHECK(hcd_pipe_command(dev_obj->constant.default_pipe, HCD_PIPE_CMD_FLUSH));
  323. }
  324. if (action_flags & DEV_FLAG_ACTION_DEFAULT_PIPE_DEQUEUE) {
  325. //Empty URBs from default pipe and trigger a control transfer callback
  326. ESP_LOGD(USBH_TAG, "Default pipe device %d", dev_obj->constant.address);
  327. int num_urbs = 0;
  328. urb_t *urb = hcd_urb_dequeue(dev_obj->constant.default_pipe);
  329. while (urb != NULL) {
  330. num_urbs++;
  331. p_usbh_obj->constant.ctrl_xfer_cb((usb_device_handle_t)dev_obj, urb, p_usbh_obj->constant.ctrl_xfer_cb_arg);
  332. urb = hcd_urb_dequeue(dev_obj->constant.default_pipe);
  333. }
  334. USBH_ENTER_CRITICAL();
  335. dev_obj->dynamic.num_ctrl_xfers_inflight -= num_urbs;
  336. USBH_EXIT_CRITICAL();
  337. }
  338. if (action_flags & DEV_FLAG_ACTION_DEFAULT_PIPE_CLEAR) {
  339. //We allow the pipe command to fail just in case the pipe becomes invalid mid command
  340. hcd_pipe_command(dev_obj->constant.default_pipe, HCD_PIPE_CMD_CLEAR);
  341. }
  342. /*
  343. Note: We make these action flags mutually exclusive in case they happen in rapid succession. They are handled
  344. in the order of precedence
  345. For example
  346. - New device event is requested followed immediately by a disconnection
  347. - Port disable requested followed immediately by a disconnection
  348. */
  349. if (action_flags & DEV_FLAG_ACTION_FREE) {
  350. ESP_LOGD(USBH_TAG, "Freeing device %d", dev_obj->constant.address);
  351. if (handle_dev_free(dev_obj)) {
  352. ESP_LOGD(USBH_TAG, "Device all free");
  353. p_usbh_obj->constant.event_cb((usb_device_handle_t)NULL, USBH_EVENT_DEV_ALL_FREE, p_usbh_obj->constant.event_cb_arg);
  354. }
  355. } else if (action_flags & DEV_FLAG_ACTION_PORT_DISABLE) {
  356. //Request that the HUB disables this device's port
  357. ESP_LOGD(USBH_TAG, "Disable device port %d", dev_obj->constant.address);
  358. p_usbh_obj->constant.hub_cb(dev_obj->constant.port_hdl, USBH_HUB_EVENT_DISABLE_PORT, p_usbh_obj->constant.hub_cb_arg);
  359. } else if (action_flags & DEV_FLAG_ACTION_SEND_NEW) {
  360. ESP_LOGD(USBH_TAG, "New device %d", dev_obj->constant.address);
  361. p_usbh_obj->constant.event_cb((usb_device_handle_t)dev_obj, USBH_EVENT_DEV_NEW, p_usbh_obj->constant.event_cb_arg);
  362. }
  363. USBH_ENTER_CRITICAL();
  364. }
  365. USBH_EXIT_CRITICAL();
  366. return ESP_OK;
  367. }
  368. // ------------------------------------------------ Device Functions ---------------------------------------------------
  369. // --------------------- Device Pool -----------------------
  370. esp_err_t usbh_dev_open(uint8_t dev_addr, usb_device_handle_t *dev_hdl)
  371. {
  372. USBH_CHECK(dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  373. esp_err_t ret;
  374. USBH_ENTER_CRITICAL();
  375. //Go through the device lists to find the device with the specified address
  376. device_t *found_dev_obj = NULL;
  377. device_t *dev_obj;
  378. TAILQ_FOREACH(dev_obj, &p_usbh_obj->dynamic.devs_idle_tailq, dynamic.tailq_entry) {
  379. if (dev_obj->constant.address == dev_addr) {
  380. found_dev_obj = dev_obj;
  381. goto exit;
  382. }
  383. }
  384. TAILQ_FOREACH(dev_obj, &p_usbh_obj->dynamic.devs_idle_tailq, dynamic.tailq_entry) {
  385. if (dev_obj->constant.address == dev_addr) {
  386. found_dev_obj = dev_obj;
  387. goto exit;
  388. }
  389. }
  390. exit:
  391. if (found_dev_obj != NULL) {
  392. //The device is not in a state to be referenced
  393. if (dev_obj->dynamic.flags.is_gone || dev_obj->dynamic.flags.waiting_port_disable || dev_obj->dynamic.flags.waiting_free) {
  394. ret = ESP_ERR_INVALID_STATE;
  395. } else {
  396. dev_obj->dynamic.ref_count++;
  397. *dev_hdl = (usb_device_handle_t)found_dev_obj;
  398. ret = ESP_OK;
  399. }
  400. } else {
  401. ret = ESP_ERR_NOT_FOUND;
  402. }
  403. USBH_EXIT_CRITICAL();
  404. return ret;
  405. }
  406. esp_err_t usbh_dev_close(usb_device_handle_t dev_hdl)
  407. {
  408. USBH_CHECK(dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  409. device_t *dev_obj = (device_t *)dev_hdl;
  410. USBH_ENTER_CRITICAL();
  411. USBH_CHECK_FROM_CRIT(dev_obj->dynamic.num_ctrl_xfers_inflight == 0, ESP_ERR_INVALID_STATE);
  412. dev_obj->dynamic.ref_count--;
  413. bool call_notif_cb = false;
  414. if (dev_obj->dynamic.ref_count == 0) {
  415. //Sanity check. This can only be set when ref count reaches 0
  416. assert(!dev_obj->dynamic.flags.waiting_free);
  417. if (dev_obj->dynamic.flags.is_gone) {
  418. //Device is already gone so it's port is already disabled. Trigger the USBH process to free the device
  419. dev_obj->dynamic.flags.waiting_free = 1;
  420. call_notif_cb = _dev_set_actions(dev_obj, DEV_FLAG_ACTION_FREE);
  421. } else if (dev_obj->dynamic.flags.waiting_close) {
  422. //Device is still connected but is no longer needed. Trigger the USBH process to request device's port be disabled
  423. dev_obj->dynamic.flags.waiting_port_disable = 1;
  424. call_notif_cb = _dev_set_actions(dev_obj, DEV_FLAG_ACTION_PORT_DISABLE);
  425. }
  426. //Else, there's nothing to do. Leave the device allocated
  427. }
  428. USBH_EXIT_CRITICAL();
  429. if (call_notif_cb) {
  430. p_usbh_obj->constant.notif_cb(USB_NOTIF_SOURCE_USBH, false, p_usbh_obj->constant.notif_cb_arg);
  431. }
  432. return ESP_OK;
  433. }
  434. esp_err_t usbh_dev_mark_all_free(void)
  435. {
  436. USBH_ENTER_CRITICAL();
  437. /*
  438. Go through the device list and mark each device as waiting to be closed. If the device is not opened at all, we can
  439. disable it immediately.
  440. Note: We manually traverse the list because we need to add/remove items while traversing
  441. */
  442. bool call_notif_cb = false;
  443. for (int i = 0; i < 2; i++) {
  444. device_t *dev_obj_cur;
  445. device_t *dev_obj_next;
  446. //Go through pending list first as it's more efficient
  447. if (i == 0) {
  448. dev_obj_cur = TAILQ_FIRST(&p_usbh_obj->dynamic.devs_pending_tailq);
  449. } else {
  450. dev_obj_cur = TAILQ_FIRST(&p_usbh_obj->dynamic.devs_idle_tailq);
  451. }
  452. while (dev_obj_cur != NULL) {
  453. assert(!dev_obj_cur->dynamic.flags.waiting_close); //Sanity check
  454. //Keep a copy of the next item first in case we remove the current item
  455. dev_obj_next = TAILQ_NEXT(dev_obj_cur, dynamic.tailq_entry);
  456. if (dev_obj_cur->dynamic.ref_count == 0 && !dev_obj_cur->dynamic.flags.is_gone) {
  457. //Device is not opened as is not gone, so we can disable it now
  458. dev_obj_cur->dynamic.flags.waiting_port_disable = 1;
  459. call_notif_cb |= _dev_set_actions(dev_obj_cur, DEV_FLAG_ACTION_PORT_DISABLE);
  460. } else {
  461. //Device is still opened. Just mark it as waiting to be closed
  462. dev_obj_cur->dynamic.flags.waiting_close = 1;
  463. }
  464. dev_obj_cur = dev_obj_next;
  465. }
  466. }
  467. USBH_EXIT_CRITICAL();
  468. if (call_notif_cb) {
  469. p_usbh_obj->constant.notif_cb(USB_NOTIF_SOURCE_USBH, false, p_usbh_obj->constant.notif_cb_arg);
  470. }
  471. return ESP_OK;
  472. }
  473. // ------------------- Single Device ----------------------
  474. esp_err_t usbh_dev_get_addr(usb_device_handle_t dev_hdl, uint8_t *dev_addr)
  475. {
  476. USBH_CHECK(dev_hdl != NULL && dev_addr != NULL, ESP_ERR_INVALID_ARG);
  477. device_t *dev_obj = (device_t *)dev_hdl;
  478. USBH_ENTER_CRITICAL();
  479. USBH_CHECK_FROM_CRIT(dev_obj->constant.address > 0, ESP_ERR_INVALID_STATE);
  480. *dev_addr = dev_obj->constant.address;
  481. USBH_EXIT_CRITICAL();
  482. return ESP_OK;
  483. }
  484. esp_err_t usbh_dev_get_info(usb_device_handle_t dev_hdl, usb_device_info_t *dev_info)
  485. {
  486. USBH_CHECK(dev_hdl != NULL && dev_info != NULL, ESP_ERR_INVALID_ARG);
  487. device_t *dev_obj = (device_t *)dev_hdl;
  488. USBH_ENTER_CRITICAL();
  489. USBH_CHECK_FROM_CRIT(dev_obj->dynamic.state == USB_DEVICE_STATE_CONFIGURED || dev_obj->dynamic.state == USB_DEVICE_STATE_NOT_ATTACHED, ESP_ERR_INVALID_STATE);
  490. dev_info->speed = dev_obj->constant.speed;
  491. dev_info->dev_addr = dev_obj->constant.address;
  492. dev_info->bMaxPacketSize0 = dev_obj->constant.desc->bMaxPacketSize0;
  493. if (dev_obj->dynamic.config_desc == NULL) {
  494. dev_info->bConfigurationValue = 0;
  495. } else {
  496. dev_info->bConfigurationValue = dev_obj->dynamic.config_desc->bConfigurationValue;
  497. }
  498. USBH_EXIT_CRITICAL();
  499. return ESP_OK;
  500. }
  501. esp_err_t usbh_dev_get_desc(usb_device_handle_t dev_hdl, const usb_device_desc_t **dev_desc_ret)
  502. {
  503. USBH_CHECK(dev_hdl != NULL && dev_desc_ret != NULL, ESP_ERR_INVALID_ARG);
  504. device_t *dev_obj = (device_t *)dev_hdl;
  505. USBH_ENTER_CRITICAL();
  506. USBH_CHECK_FROM_CRIT(dev_obj->dynamic.state == USB_DEVICE_STATE_CONFIGURED, ESP_ERR_INVALID_STATE);
  507. USBH_EXIT_CRITICAL();
  508. *dev_desc_ret = dev_obj->constant.desc;
  509. return ESP_OK;
  510. }
  511. esp_err_t usbh_dev_get_config_desc(usb_device_handle_t dev_hdl, const usb_config_desc_t **config_desc_ret)
  512. {
  513. USBH_CHECK(dev_hdl != NULL && config_desc_ret != NULL, ESP_ERR_INVALID_ARG);
  514. device_t *dev_obj = (device_t *)dev_hdl;
  515. USBH_ENTER_CRITICAL();
  516. USBH_CHECK_FROM_CRIT(dev_obj->dynamic.state == USB_DEVICE_STATE_CONFIGURED, ESP_ERR_INVALID_STATE);
  517. *config_desc_ret = dev_obj->dynamic.config_desc;
  518. USBH_EXIT_CRITICAL();
  519. return ESP_OK;
  520. }
  521. esp_err_t usbh_dev_submit_ctrl_urb(usb_device_handle_t dev_hdl, urb_t *urb)
  522. {
  523. USBH_CHECK(dev_hdl != NULL && urb != NULL, ESP_ERR_INVALID_ARG);
  524. device_t *dev_obj = (device_t *)dev_hdl;
  525. USBH_ENTER_CRITICAL();
  526. USBH_CHECK_FROM_CRIT(dev_obj->dynamic.state == USB_DEVICE_STATE_CONFIGURED, ESP_ERR_INVALID_STATE);
  527. //Increment the control transfer count first
  528. dev_obj->dynamic.num_ctrl_xfers_inflight++;
  529. USBH_EXIT_CRITICAL();
  530. esp_err_t ret;
  531. if (hcd_pipe_get_state(dev_obj->constant.default_pipe) != HCD_PIPE_STATE_ACTIVE) {
  532. ret = ESP_ERR_INVALID_STATE;
  533. goto hcd_err;
  534. }
  535. ret = hcd_urb_enqueue(dev_obj->constant.default_pipe, urb);
  536. if (ret != ESP_OK) {
  537. goto hcd_err;
  538. }
  539. ret = ESP_OK;
  540. return ret;
  541. hcd_err:
  542. USBH_ENTER_CRITICAL();
  543. dev_obj->dynamic.num_ctrl_xfers_inflight--;
  544. USBH_EXIT_CRITICAL();
  545. return ret;
  546. }
  547. // ----------------------------------------------- Interface Functions -------------------------------------------------
  548. esp_err_t usbh_ep_alloc(usb_device_handle_t dev_hdl, usbh_ep_config_t *ep_config, hcd_pipe_handle_t *pipe_hdl_ret)
  549. {
  550. USBH_CHECK(dev_hdl != NULL && ep_config != NULL && pipe_hdl_ret != NULL, ESP_ERR_INVALID_ARG);
  551. device_t *dev_obj = (device_t *)dev_hdl;
  552. USBH_ENTER_CRITICAL();
  553. USBH_CHECK_FROM_CRIT(dev_obj->dynamic.state == USB_DEVICE_STATE_CONFIGURED, ESP_ERR_INVALID_STATE);
  554. dev_obj->dynamic.ref_count++; //Increase the ref_count to keep the device alive while allocating the endpoint
  555. USBH_EXIT_CRITICAL();
  556. esp_err_t ret;
  557. //Allocate HCD pipe
  558. hcd_pipe_config_t pipe_config = {
  559. .callback = ep_config->pipe_cb,
  560. .callback_arg = ep_config->pipe_cb_arg,
  561. .context = ep_config->context,
  562. .ep_desc = ep_config->ep_desc,
  563. .dev_speed = dev_obj->constant.speed,
  564. .dev_addr = dev_obj->constant.address,
  565. };
  566. hcd_pipe_handle_t pipe_hdl;
  567. ret = hcd_pipe_alloc(dev_obj->constant.port_hdl, &pipe_config, &pipe_hdl);
  568. if (ret != ESP_OK) {
  569. goto pipe_alloc_err;
  570. }
  571. USBH_ENTER_CRITICAL();
  572. //Check that endpoint has not be allocated yet
  573. bool is_in = ep_config->ep_desc->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK;
  574. uint8_t addr = ep_config->ep_desc->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK;
  575. //Assign the pipe handle
  576. bool assigned = false;
  577. if (is_in && dev_obj->dynamic.ep_in[addr - 1] == NULL) { //Is an IN EP
  578. dev_obj->dynamic.ep_in[addr - 1] = pipe_hdl;
  579. assigned = true;
  580. } else {
  581. dev_obj->dynamic.ep_out[addr - 1] = pipe_hdl;
  582. assigned = true;
  583. }
  584. dev_obj->dynamic.ref_count--; //Restore ref_count
  585. USBH_EXIT_CRITICAL();
  586. if (!assigned) {
  587. ret = ESP_ERR_INVALID_STATE;
  588. goto assign_err;
  589. }
  590. //Write back output
  591. *pipe_hdl_ret = pipe_hdl;
  592. ret = ESP_OK;
  593. return ret;
  594. assign_err:
  595. ESP_ERROR_CHECK(hcd_pipe_free(pipe_hdl));
  596. pipe_alloc_err:
  597. return ret;
  598. }
  599. esp_err_t usbh_ep_free(usb_device_handle_t dev_hdl, uint8_t bEndpointAddress)
  600. {
  601. USBH_CHECK(dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  602. device_t *dev_obj = (device_t *)dev_hdl;
  603. USBH_ENTER_CRITICAL();
  604. //Un-assign the pipe handle from the endpoint
  605. bool is_in = bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK;
  606. uint8_t addr = bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK;
  607. hcd_pipe_handle_t pipe_hdl;
  608. if (is_in) {
  609. USBH_CHECK_FROM_CRIT(dev_obj->dynamic.ep_in[addr - 1] != NULL, ESP_ERR_INVALID_STATE);
  610. pipe_hdl = dev_obj->dynamic.ep_in[addr - 1];
  611. dev_obj->dynamic.ep_in[addr - 1] = NULL;
  612. } else {
  613. USBH_CHECK_FROM_CRIT(dev_obj->dynamic.ep_out[addr - 1] != NULL, ESP_ERR_INVALID_STATE);
  614. pipe_hdl = dev_obj->dynamic.ep_out[addr - 1];
  615. dev_obj->dynamic.ep_out[addr - 1] = NULL;
  616. }
  617. USBH_EXIT_CRITICAL();
  618. ESP_ERROR_CHECK(hcd_pipe_free(pipe_hdl));
  619. return ESP_OK;
  620. }
  621. esp_err_t usbh_ep_get_context(usb_device_handle_t dev_hdl, uint8_t bEndpointAddress, void **context_ret)
  622. {
  623. bool is_in = bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK;
  624. uint8_t addr = bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK;
  625. USBH_CHECK(dev_hdl != NULL &&
  626. addr >= EP_NUM_MIN && //Control endpoints are owned by the USBH
  627. addr <= EP_NUM_MAX &&
  628. context_ret != NULL,
  629. ESP_ERR_INVALID_ARG);
  630. device_t *dev_obj = (device_t *)dev_hdl;
  631. USBH_ENTER_CRITICAL();
  632. //Get the endpoint's corresponding pipe
  633. hcd_pipe_handle_t pipe_hdl;
  634. if (is_in) {
  635. pipe_hdl = dev_obj->dynamic.ep_in[addr - 1];
  636. } else {
  637. pipe_hdl = dev_obj->dynamic.ep_out[addr - 1];
  638. }
  639. esp_err_t ret;
  640. if (pipe_hdl == NULL) {
  641. USBH_EXIT_CRITICAL();
  642. ret = ESP_ERR_NOT_FOUND;
  643. goto exit;
  644. }
  645. //Return the context of the pipe
  646. void *context = hcd_pipe_get_context(pipe_hdl);
  647. *context_ret = context;
  648. USBH_EXIT_CRITICAL();
  649. ret = ESP_OK;
  650. exit:
  651. return ret;
  652. }
  653. // -------------------------------------------------- Hub Functions ----------------------------------------------------
  654. // ------------------- Device Related ----------------------
  655. esp_err_t usbh_hub_is_installed(usbh_hub_cb_t hub_callback, void *callback_arg)
  656. {
  657. USBH_CHECK(hub_callback != NULL, ESP_ERR_INVALID_ARG);
  658. USBH_ENTER_CRITICAL();
  659. //Check that USBH is already installed
  660. USBH_CHECK_FROM_CRIT(p_usbh_obj != NULL, ESP_ERR_INVALID_STATE);
  661. //Check that Hub has not be installed yet
  662. USBH_CHECK_FROM_CRIT(p_usbh_obj->constant.hub_cb == NULL, ESP_ERR_INVALID_STATE);
  663. p_usbh_obj->constant.hub_cb = hub_callback;
  664. p_usbh_obj->constant.hub_cb_arg = callback_arg;
  665. USBH_EXIT_CRITICAL();
  666. return ESP_OK;
  667. }
  668. esp_err_t usbh_hub_add_dev(hcd_port_handle_t port_hdl, usb_speed_t dev_speed, usb_device_handle_t *new_dev_hdl, hcd_pipe_handle_t *default_pipe_hdl)
  669. {
  670. //Note: Parent device handle can be NULL if it's connected to the root hub
  671. USBH_CHECK(new_dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  672. esp_err_t ret;
  673. device_t *dev_obj;
  674. ret = device_alloc(port_hdl, dev_speed, &dev_obj);
  675. if (ret != ESP_OK) {
  676. return ret;
  677. }
  678. //Write-back device handle
  679. *new_dev_hdl = (usb_device_handle_t)dev_obj;
  680. *default_pipe_hdl = dev_obj->constant.default_pipe;
  681. ret = ESP_OK;
  682. return ret;
  683. }
  684. esp_err_t usbh_hub_mark_dev_gone(usb_device_handle_t dev_hdl)
  685. {
  686. USBH_CHECK(dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  687. device_t *dev_obj = (device_t *)dev_hdl;
  688. USBH_ENTER_CRITICAL();
  689. dev_obj->dynamic.flags.is_gone = 1;
  690. bool call_notif_cb;
  691. //Check if the device can be freed now
  692. if (dev_obj->dynamic.ref_count == 0) {
  693. dev_obj->dynamic.flags.waiting_free = 1;
  694. call_notif_cb = _dev_set_actions(dev_obj, DEV_FLAG_ACTION_FREE);
  695. } else {
  696. call_notif_cb = _dev_set_actions(dev_obj, DEV_FLAG_ACTION_SEND_GONE_EVENT |
  697. DEV_FLAG_ACTION_DEFAULT_PIPE_FLUSH |
  698. DEV_FLAG_ACTION_DEFAULT_PIPE_DEQUEUE);
  699. }
  700. USBH_EXIT_CRITICAL();
  701. if (call_notif_cb) {
  702. p_usbh_obj->constant.notif_cb(USB_NOTIF_SOURCE_USBH, false, p_usbh_obj->constant.notif_cb_arg);
  703. }
  704. return ESP_OK;
  705. }
  706. esp_err_t usbh_hub_dev_port_disabled(usb_device_handle_t dev_hdl)
  707. {
  708. USBH_CHECK(dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  709. device_t *dev_obj = (device_t *)dev_hdl;
  710. USBH_ENTER_CRITICAL();
  711. assert(dev_obj->dynamic.ref_count == 0); //At this stage, the device should have been closed by all users
  712. dev_obj->dynamic.flags.waiting_free = 1;
  713. bool call_notif_cb = _dev_set_actions(dev_obj, DEV_FLAG_ACTION_FREE);
  714. USBH_EXIT_CRITICAL();
  715. if (call_notif_cb) {
  716. ESP_LOGD(USBH_TAG, "Notif free");
  717. p_usbh_obj->constant.notif_cb(USB_NOTIF_SOURCE_USBH, false, p_usbh_obj->constant.notif_cb_arg);
  718. }
  719. return ESP_OK;
  720. }
  721. // ----------------- Enumeration Related -------------------
  722. esp_err_t usbh_hub_enum_fill_dev_addr(usb_device_handle_t dev_hdl, uint8_t dev_addr)
  723. {
  724. USBH_CHECK(dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  725. device_t *dev_obj = (device_t *)dev_hdl;
  726. USBH_ENTER_CRITICAL();
  727. USBH_CHECK_FROM_CRIT(dev_obj->constant.enum_todo_flags & DEV_ENUM_TODO_FLAG_DEV_ADDR, ESP_ERR_INVALID_STATE);
  728. dev_obj->dynamic.state = USB_DEVICE_STATE_ADDRESS;
  729. USBH_EXIT_CRITICAL();
  730. //We can modify the info members outside the critical section
  731. dev_obj->constant.enum_todo_flags &= ~DEV_ENUM_TODO_FLAG_DEV_ADDR;
  732. dev_obj->constant.address = dev_addr;
  733. return ESP_OK;
  734. }
  735. esp_err_t usbh_hub_enum_fill_dev_desc(usb_device_handle_t dev_hdl, const usb_device_desc_t *device_desc)
  736. {
  737. USBH_CHECK(dev_hdl != NULL && device_desc != NULL, ESP_ERR_INVALID_ARG);
  738. device_t *dev_obj = (device_t *)dev_hdl;
  739. //We can modify the info members outside the critical section
  740. USBH_CHECK(dev_obj->constant.enum_todo_flags & DEV_ENUM_TODO_FLAG_DEV_DESC, ESP_ERR_INVALID_STATE);
  741. dev_obj->constant.enum_todo_flags &= ~DEV_ENUM_TODO_FLAG_DEV_DESC;
  742. memcpy((usb_device_desc_t *)dev_obj->constant.desc, device_desc, sizeof(usb_device_desc_t));
  743. return ESP_OK;
  744. }
  745. esp_err_t usbh_hub_enum_fill_config_desc(usb_device_handle_t dev_hdl, const usb_config_desc_t *config_desc_full)
  746. {
  747. USBH_CHECK(dev_hdl != NULL && config_desc_full != NULL, ESP_ERR_INVALID_ARG);
  748. device_t *dev_obj = (device_t *)dev_hdl;
  749. esp_err_t ret;
  750. //Allocate memory to store the configuration descriptor
  751. usb_config_desc_t *config_desc = heap_caps_malloc(config_desc_full->wTotalLength, MALLOC_CAP_DEFAULT); //Buffer to copy over full configuration descriptor (wTotalLength)
  752. if (config_desc == NULL) {
  753. ret = ESP_ERR_NO_MEM;
  754. goto err;
  755. }
  756. //Copy the configuration descriptor
  757. memcpy(config_desc, config_desc_full, config_desc_full->wTotalLength);
  758. //Assign the config object to the device object
  759. if (!(dev_obj->constant.enum_todo_flags & DEV_ENUM_TODO_FLAG_CONFIG_DESC)) {
  760. ret = ESP_ERR_INVALID_STATE;
  761. goto assign_err;
  762. }
  763. USBH_ENTER_CRITICAL();
  764. assert(dev_obj->dynamic.config_desc == NULL);
  765. dev_obj->dynamic.config_desc = config_desc;
  766. USBH_EXIT_CRITICAL();
  767. //We can modify the info members outside the critical section
  768. dev_obj->constant.enum_todo_flags &= ~DEV_ENUM_TODO_FLAG_CONFIG_DESC;
  769. ret = ESP_OK;
  770. return ret;
  771. assign_err:
  772. heap_caps_free(config_desc);
  773. err:
  774. return ret;
  775. }
  776. esp_err_t usbh_hub_enum_done(usb_device_handle_t dev_hdl)
  777. {
  778. USBH_CHECK(dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  779. device_t *dev_obj = (device_t *)dev_hdl;
  780. USBH_CHECK(dev_obj->constant.enum_todo_flags == 0, ESP_ERR_INVALID_STATE); //All enumeration stages to be done
  781. USBH_ENTER_CRITICAL();
  782. dev_obj->dynamic.state = USB_DEVICE_STATE_CONFIGURED;
  783. //Add the device to list of devices, then trigger a device event
  784. TAILQ_INSERT_TAIL(&p_usbh_obj->dynamic.devs_idle_tailq, dev_obj, dynamic.tailq_entry); //Add it to the idle device list first
  785. p_usbh_obj->dynamic.num_device++;
  786. bool call_notif_cb = _dev_set_actions(dev_obj, DEV_FLAG_ACTION_SEND_NEW);
  787. USBH_EXIT_CRITICAL();
  788. //Update the default pipe callback
  789. ESP_ERROR_CHECK(hcd_pipe_update_callback(dev_obj->constant.default_pipe, default_pipe_callback, (void *)dev_obj));
  790. //Call the notification callback
  791. if (call_notif_cb) {
  792. p_usbh_obj->constant.notif_cb(USB_NOTIF_SOURCE_USBH, false, p_usbh_obj->constant.notif_cb_arg);
  793. }
  794. return ESP_OK;
  795. }
  796. esp_err_t usbh_hub_enum_failed(usb_device_handle_t dev_hdl)
  797. {
  798. USBH_CHECK(dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  799. device_t *dev_obj = (device_t *)dev_hdl;
  800. USBH_ENTER_CRITICAL();
  801. usb_config_desc_t *config_desc = dev_obj->dynamic.config_desc;
  802. dev_obj->dynamic.config_desc = NULL;
  803. USBH_EXIT_CRITICAL();
  804. if (config_desc) {
  805. heap_caps_free(config_desc);
  806. }
  807. device_free(dev_obj);
  808. return ESP_OK;
  809. }