usb_host.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. Warning: The USB Host Library API is still a beta version and may be subject to change
  8. */
  9. #include <stdlib.h>
  10. #include <stdint.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/queue.h"
  14. #include "freertos/semphr.h"
  15. #include "esp_err.h"
  16. #include "esp_log.h"
  17. #include "esp_heap_caps.h"
  18. #include "hub.h"
  19. #include "usbh.h"
  20. #include "usb/usb_host.h"
  21. static portMUX_TYPE host_lock = portMUX_INITIALIZER_UNLOCKED;
  22. #define HOST_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&host_lock)
  23. #define HOST_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&host_lock)
  24. #define HOST_ENTER_CRITICAL() portENTER_CRITICAL(&host_lock)
  25. #define HOST_EXIT_CRITICAL() portEXIT_CRITICAL(&host_lock)
  26. #define HOST_ENTER_CRITICAL_SAFE() portENTER_CRITICAL_SAFE(&host_lock)
  27. #define HOST_EXIT_CRITICAL_SAFE() portEXIT_CRITICAL_SAFE(&host_lock)
  28. #define HOST_CHECK(cond, ret_val) ({ \
  29. if (!(cond)) { \
  30. return (ret_val); \
  31. } \
  32. })
  33. #define HOST_CHECK_FROM_CRIT(cond, ret_val) ({ \
  34. if (!(cond)) { \
  35. HOST_EXIT_CRITICAL(); \
  36. return ret_val; \
  37. } \
  38. })
  39. #define PROCESS_PENDING_FLAG_USBH 0x01
  40. #define PROCESS_PENDING_FLAG_HUB 0x02
  41. #define PROCESS_PENDING_FLAG_EVENT 0x04
  42. typedef struct endpoint_s endpoint_t;
  43. typedef struct interface_s interface_t;
  44. typedef struct client_s client_t;
  45. struct endpoint_s {
  46. //Dynamic members require a critical section
  47. struct {
  48. TAILQ_ENTRY(endpoint_s) tailq_entry;
  49. union {
  50. struct {
  51. uint32_t pending: 1;
  52. uint32_t reserved31:31;
  53. };
  54. } flags;
  55. uint32_t num_urb_inflight;
  56. hcd_pipe_event_t last_event;
  57. } dynamic;
  58. //Constant members do no change after claiming the interface thus do not require a critical section
  59. struct {
  60. hcd_pipe_handle_t pipe_hdl;
  61. const usb_ep_desc_t *ep_desc;
  62. interface_t *intf_obj;
  63. } constant;
  64. };
  65. struct interface_s {
  66. //Dynamic members require a critical section
  67. struct {
  68. TAILQ_ENTRY(interface_s) tailq_entry;
  69. } mux_protected;
  70. //Constant members do no change after claiming the interface thus do not require a critical section
  71. struct {
  72. const usb_intf_desc_t *intf_desc;
  73. usb_device_handle_t dev_hdl;
  74. client_t *client_obj;
  75. endpoint_t *endpoints[0];
  76. } constant;
  77. };
  78. struct client_s {
  79. //Dynamic members require a critical section
  80. struct {
  81. TAILQ_ENTRY(client_s) tailq_entry;
  82. TAILQ_HEAD(tailhead_pending_ep, endpoint_s) pending_ep_tailq;
  83. TAILQ_HEAD(tailhead_idle_ep, endpoint_s) idle_ep_tailq;
  84. TAILQ_HEAD(tailhead_done_ctrl_xfers, urb_s) done_ctrl_xfer_tailq;
  85. union {
  86. struct {
  87. uint32_t events_pending: 1;
  88. uint32_t handling_events: 1;
  89. uint32_t blocked: 1;
  90. uint32_t taking_mux: 1;
  91. uint32_t reserved4: 4;
  92. uint32_t num_intf_claimed: 8;
  93. uint32_t reserved16: 16;
  94. };
  95. uint32_t val;
  96. } flags;
  97. uint32_t num_done_ctrl_xfer;
  98. uint32_t opened_dev_addr_map;
  99. } dynamic;
  100. //Mux protected members must be protected by host library the mux_lock when accessed
  101. struct {
  102. TAILQ_HEAD(tailhead_interfaces, interface_s) interface_tailq;
  103. } mux_protected;
  104. //Constant members do no change after registration thus do not require a critical section
  105. struct {
  106. SemaphoreHandle_t event_sem;
  107. usb_host_client_event_cb_t event_callback;
  108. void *callback_arg;
  109. QueueHandle_t event_msg_queue;
  110. } constant;
  111. };
  112. typedef struct {
  113. //Dynamic members require a critical section
  114. struct {
  115. //Access to these should be done in a critical section
  116. uint32_t process_pending_flags;
  117. uint32_t lib_event_flags;
  118. union {
  119. struct {
  120. uint32_t process_pending: 1;
  121. uint32_t handling_events: 1;
  122. uint32_t blocked: 1;
  123. uint32_t reserved5: 5;
  124. uint32_t num_clients: 8;
  125. uint32_t reserved16: 16;
  126. };
  127. uint32_t val;
  128. } flags;
  129. } dynamic;
  130. //Mux protected members must be protected by host library the mux_lock when accessed
  131. struct {
  132. TAILQ_HEAD(tailhead_clients, client_s) client_tailq; //List of all clients registered
  133. } mux_protected;
  134. //Constant members do no change after installation thus do not require a critical section
  135. struct {
  136. SemaphoreHandle_t event_sem;
  137. SemaphoreHandle_t mux_lock;
  138. } constant;
  139. } host_lib_t;
  140. static host_lib_t *p_host_lib_obj = NULL;
  141. const char *USB_HOST_TAG = "USB HOST";
  142. // ----------------------------------------------------- Helpers -------------------------------------------------------
  143. static inline void _record_client_opened_device(client_t *client_obj, uint8_t dev_addr)
  144. {
  145. assert(dev_addr != 0);
  146. client_obj->dynamic.opened_dev_addr_map |= (1 << (dev_addr - 1));
  147. }
  148. static inline void _clear_client_opened_device(client_t *client_obj, uint8_t dev_addr)
  149. {
  150. assert(dev_addr != 0);
  151. client_obj->dynamic.opened_dev_addr_map &= ~(1 << (dev_addr - 1));
  152. }
  153. static inline bool _check_client_opened_device(client_t *client_obj, uint8_t dev_addr)
  154. {
  155. assert(dev_addr != 0);
  156. return (client_obj->dynamic.opened_dev_addr_map & (1 << (dev_addr - 1)));
  157. }
  158. static bool _unblock_client(client_t *client_obj, bool in_isr)
  159. {
  160. bool send_sem;
  161. if (!client_obj->dynamic.flags.events_pending && !client_obj->dynamic.flags.handling_events) {
  162. client_obj->dynamic.flags.events_pending = 1;
  163. send_sem = true;
  164. } else {
  165. send_sem = false;
  166. }
  167. HOST_EXIT_CRITICAL_SAFE();
  168. bool yield = false;
  169. if (send_sem) {
  170. if (in_isr) {
  171. BaseType_t xTaskWoken = pdFALSE;
  172. xSemaphoreGiveFromISR(client_obj->constant.event_sem, &xTaskWoken);
  173. yield = (xTaskWoken == pdTRUE);
  174. } else {
  175. xSemaphoreGive(client_obj->constant.event_sem);
  176. }
  177. }
  178. HOST_ENTER_CRITICAL_SAFE();
  179. return yield;
  180. }
  181. static bool _unblock_lib(bool in_isr)
  182. {
  183. bool send_sem;
  184. if (!p_host_lib_obj->dynamic.flags.process_pending && !p_host_lib_obj->dynamic.flags.handling_events) {
  185. p_host_lib_obj->dynamic.flags.process_pending = 1;
  186. send_sem = true;
  187. } else {
  188. send_sem = false;
  189. }
  190. HOST_EXIT_CRITICAL_SAFE();
  191. bool yield = false;
  192. if (send_sem) {
  193. if (in_isr) {
  194. BaseType_t xTaskWoken = pdFALSE;
  195. xSemaphoreGiveFromISR(p_host_lib_obj->constant.event_sem, &xTaskWoken);
  196. yield = (xTaskWoken == pdTRUE);
  197. } else {
  198. xSemaphoreGive(p_host_lib_obj->constant.event_sem);
  199. }
  200. }
  201. HOST_ENTER_CRITICAL_SAFE();
  202. return yield;
  203. }
  204. static void send_event_msg_to_clients(const usb_host_client_event_msg_t *event_msg, bool send_to_all, uint8_t opened_dev_addr)
  205. {
  206. //Lock client list
  207. xSemaphoreTake(p_host_lib_obj->constant.mux_lock, portMAX_DELAY);
  208. //Send event message to relevant or all clients
  209. client_t *client_obj;
  210. TAILQ_FOREACH(client_obj, &p_host_lib_obj->mux_protected.client_tailq, dynamic.tailq_entry) {
  211. if (!send_to_all) {
  212. //Check if client opened the device
  213. HOST_ENTER_CRITICAL();
  214. bool send = _check_client_opened_device(client_obj, opened_dev_addr);
  215. HOST_EXIT_CRITICAL();
  216. if (!send) {
  217. continue;
  218. }
  219. }
  220. //Send the event message
  221. if (xQueueSend(client_obj->constant.event_msg_queue, event_msg, 0) == pdTRUE) {
  222. HOST_ENTER_CRITICAL();
  223. _unblock_client(client_obj, false);
  224. HOST_EXIT_CRITICAL();
  225. } else {
  226. ESP_LOGE(USB_HOST_TAG, "Client event message queue full");
  227. }
  228. }
  229. //Unlock client list
  230. xSemaphoreGive(p_host_lib_obj->constant.mux_lock);
  231. }
  232. // ---------------------------------------------------- Callbacks ------------------------------------------------------
  233. // ------------------- Library Related ---------------------
  234. static bool notif_callback(usb_notif_source_t source, bool in_isr, void *arg)
  235. {
  236. HOST_ENTER_CRITICAL_SAFE();
  237. //Store notification source
  238. switch (source) {
  239. case USB_NOTIF_SOURCE_USBH:
  240. p_host_lib_obj->dynamic.process_pending_flags |= PROCESS_PENDING_FLAG_USBH;
  241. break;
  242. case USB_NOTIF_SOURCE_HUB:
  243. p_host_lib_obj->dynamic.process_pending_flags |= PROCESS_PENDING_FLAG_HUB;
  244. break;
  245. }
  246. bool yield = _unblock_lib(in_isr);
  247. HOST_EXIT_CRITICAL_SAFE();
  248. return yield;
  249. }
  250. static void ctrl_xfer_callback(usb_device_handle_t dev_hdl, urb_t *urb, void *arg)
  251. {
  252. assert(urb->usb_host_client != NULL);
  253. //Redistribute done control transfer to the clients that submitted them
  254. client_t *client_obj = (client_t *)urb->usb_host_client;
  255. HOST_ENTER_CRITICAL();
  256. TAILQ_INSERT_TAIL(&client_obj->dynamic.done_ctrl_xfer_tailq, urb, tailq_entry);
  257. client_obj->dynamic.num_done_ctrl_xfer++;
  258. _unblock_client(client_obj, false);
  259. HOST_EXIT_CRITICAL();
  260. }
  261. static void dev_event_callback(usb_device_handle_t dev_hdl, usbh_event_t usbh_event, void *arg)
  262. {
  263. //Check usbh_event. The data type of event_arg depends on the type of event
  264. switch (usbh_event) {
  265. case USBH_EVENT_DEV_NEW: {
  266. //Prepare a NEW_DEV client event message, the send it to all clients
  267. uint8_t dev_addr;
  268. ESP_ERROR_CHECK(usbh_dev_get_addr(dev_hdl, &dev_addr));
  269. usb_host_client_event_msg_t event_msg = {
  270. .event = USB_HOST_CLIENT_EVENT_NEW_DEV,
  271. .new_dev.address = dev_addr,
  272. };
  273. send_event_msg_to_clients(&event_msg, true, 0);
  274. break;
  275. }
  276. case USBH_EVENT_DEV_GONE: {
  277. //Prepare event msg, send only to clients that have opened the device
  278. uint8_t dev_addr;
  279. ESP_ERROR_CHECK(usbh_dev_get_addr(dev_hdl, &dev_addr));
  280. usb_host_client_event_msg_t event_msg = {
  281. .event = USB_HOST_CLIENT_EVENT_DEV_GONE,
  282. .dev_gone.dev_hdl = dev_hdl,
  283. };
  284. send_event_msg_to_clients(&event_msg, false, dev_addr);
  285. break;
  286. }
  287. case USBH_EVENT_DEV_ALL_FREE: {
  288. //Notify the lib handler that all devices are free
  289. HOST_ENTER_CRITICAL();
  290. p_host_lib_obj->dynamic.lib_event_flags |= USB_HOST_LIB_EVENT_FLAGS_ALL_FREE;
  291. _unblock_lib(false);
  292. HOST_EXIT_CRITICAL();
  293. break;
  294. }
  295. default:
  296. abort(); //Should never occur
  297. break;
  298. }
  299. }
  300. // ------------------- Client Related ----------------------
  301. static bool pipe_callback(hcd_pipe_handle_t pipe_hdl, hcd_pipe_event_t pipe_event, void *user_arg, bool in_isr)
  302. {
  303. endpoint_t *ep_obj = (endpoint_t *)user_arg;
  304. client_t *client_obj = (client_t *)ep_obj->constant.intf_obj->constant.client_obj;
  305. HOST_ENTER_CRITICAL_SAFE();
  306. //Store the event to be handled later. Note that we allow overwriting of events because more severe will halt the pipe prevent any further events.
  307. ep_obj->dynamic.last_event = pipe_event;
  308. //Add the EP to the client's pending list if it's not in the list already
  309. if (!ep_obj->dynamic.flags.pending) {
  310. ep_obj->dynamic.flags.pending = 1;
  311. TAILQ_REMOVE(&client_obj->dynamic.idle_ep_tailq, ep_obj, dynamic.tailq_entry);
  312. TAILQ_INSERT_TAIL(&client_obj->dynamic.pending_ep_tailq, ep_obj, dynamic.tailq_entry);
  313. }
  314. bool yield = _unblock_client(client_obj, in_isr);
  315. HOST_EXIT_CRITICAL_SAFE();
  316. return yield;
  317. }
  318. // ------------------------------------------------ Library Functions --------------------------------------------------
  319. // ----------------------- Public --------------------------
  320. esp_err_t usb_host_install(const usb_host_config_t *config)
  321. {
  322. HOST_CHECK(config != NULL, ESP_ERR_INVALID_ARG);
  323. HOST_ENTER_CRITICAL();
  324. HOST_CHECK_FROM_CRIT(p_host_lib_obj == NULL, ESP_ERR_INVALID_STATE);
  325. HOST_EXIT_CRITICAL();
  326. esp_err_t ret;
  327. host_lib_t *host_lib_obj = heap_caps_calloc(1, sizeof(host_lib_t), MALLOC_CAP_DEFAULT);
  328. SemaphoreHandle_t event_sem = xSemaphoreCreateBinary();
  329. SemaphoreHandle_t mux_lock = xSemaphoreCreateMutex();
  330. if (host_lib_obj == NULL || event_sem == NULL || mux_lock == NULL) {
  331. ret = ESP_ERR_NO_MEM;
  332. goto alloc_err;
  333. }
  334. //Initialize host library object
  335. TAILQ_INIT(&host_lib_obj->mux_protected.client_tailq);
  336. host_lib_obj->constant.event_sem = event_sem;
  337. host_lib_obj->constant.mux_lock = mux_lock;
  338. //Install USBH
  339. usbh_config_t usbh_config = {
  340. .notif_cb = notif_callback,
  341. .notif_cb_arg = NULL,
  342. .ctrl_xfer_cb = ctrl_xfer_callback,
  343. .ctrl_xfer_cb_arg = NULL,
  344. .event_cb = dev_event_callback,
  345. .event_cb_arg = NULL,
  346. .hcd_config = {
  347. .intr_flags = config->intr_flags,
  348. },
  349. };
  350. ret = usbh_install(&usbh_config);
  351. if (ret != ESP_OK) {
  352. goto usbh_err;
  353. }
  354. //Install Hub
  355. hub_config_t hub_config = {
  356. .notif_cb = notif_callback,
  357. .notif_cb_arg = NULL,
  358. };
  359. ret = hub_install(&hub_config);
  360. if (ret != ESP_OK) {
  361. goto hub_err;
  362. }
  363. //Assign host library object
  364. HOST_ENTER_CRITICAL();
  365. if (p_host_lib_obj != NULL) {
  366. HOST_EXIT_CRITICAL();
  367. ret = ESP_ERR_INVALID_STATE;
  368. goto assign_err;
  369. }
  370. p_host_lib_obj = host_lib_obj;
  371. HOST_EXIT_CRITICAL();
  372. //Start the root hub
  373. ESP_ERROR_CHECK(hub_root_start());
  374. ret = ESP_OK;
  375. return ret;
  376. assign_err:
  377. ESP_ERROR_CHECK(hub_uninstall());
  378. hub_err:
  379. ESP_ERROR_CHECK(usbh_uninstall());
  380. usbh_err:
  381. alloc_err:
  382. if (mux_lock) {
  383. vSemaphoreDelete(mux_lock);
  384. }
  385. if (event_sem) {
  386. vSemaphoreDelete(event_sem);
  387. }
  388. heap_caps_free(host_lib_obj);
  389. return ret;
  390. }
  391. esp_err_t usb_host_uninstall(void)
  392. {
  393. //All devices must have been freed at this point
  394. HOST_ENTER_CRITICAL();
  395. HOST_CHECK_FROM_CRIT(p_host_lib_obj != NULL, ESP_ERR_INVALID_STATE);
  396. HOST_CHECK_FROM_CRIT(p_host_lib_obj->dynamic.process_pending_flags == 0 &&
  397. p_host_lib_obj->dynamic.lib_event_flags == 0 &&
  398. p_host_lib_obj->dynamic.flags.val == 0,
  399. ESP_ERR_INVALID_STATE);
  400. HOST_EXIT_CRITICAL();
  401. //Stop the root hub
  402. ESP_ERROR_CHECK(hub_root_stop());
  403. //Uninstall Hub and USBH
  404. ESP_ERROR_CHECK(hub_uninstall());
  405. ESP_ERROR_CHECK(usbh_uninstall());
  406. HOST_ENTER_CRITICAL();
  407. host_lib_t *host_lib_obj = p_host_lib_obj;
  408. p_host_lib_obj = NULL;
  409. HOST_EXIT_CRITICAL();
  410. //Free memory objects
  411. vSemaphoreDelete(host_lib_obj->constant.mux_lock);
  412. vSemaphoreDelete(host_lib_obj->constant.event_sem);
  413. heap_caps_free(host_lib_obj);
  414. return ESP_OK;
  415. }
  416. esp_err_t usb_host_lib_handle_events(TickType_t timeout_ticks, uint32_t *event_flags_ret)
  417. {
  418. esp_err_t ret;
  419. uint32_t event_flags = 0;
  420. HOST_ENTER_CRITICAL();
  421. if (!p_host_lib_obj->dynamic.flags.process_pending) {
  422. //There is currently processing that needs to be done. Wait for some processing
  423. HOST_EXIT_CRITICAL();
  424. BaseType_t sem_ret = xSemaphoreTake(p_host_lib_obj->constant.event_sem, timeout_ticks);
  425. if (sem_ret == pdFALSE) {
  426. ret = ESP_ERR_TIMEOUT;
  427. goto exit;
  428. }
  429. HOST_ENTER_CRITICAL();
  430. }
  431. //Read and clear process pending flags
  432. uint32_t process_pending_flags = p_host_lib_obj->dynamic.process_pending_flags;
  433. p_host_lib_obj->dynamic.process_pending_flags = 0;
  434. p_host_lib_obj->dynamic.flags.handling_events = 1;
  435. while (process_pending_flags) {
  436. HOST_EXIT_CRITICAL();
  437. if (process_pending_flags & PROCESS_PENDING_FLAG_USBH) {
  438. ESP_ERROR_CHECK(usbh_process());
  439. }
  440. if (process_pending_flags & PROCESS_PENDING_FLAG_HUB) {
  441. ESP_ERROR_CHECK(hub_process());
  442. }
  443. HOST_ENTER_CRITICAL();
  444. //Read and clear process pending flags again, and loop back if there is more to process
  445. process_pending_flags = p_host_lib_obj->dynamic.process_pending_flags;
  446. p_host_lib_obj->dynamic.process_pending_flags = 0;
  447. }
  448. p_host_lib_obj->dynamic.flags.process_pending = 0;
  449. p_host_lib_obj->dynamic.flags.handling_events = 0;
  450. event_flags = p_host_lib_obj->dynamic.lib_event_flags;
  451. p_host_lib_obj->dynamic.lib_event_flags = 0;
  452. HOST_EXIT_CRITICAL();
  453. ret = ESP_OK;
  454. exit:
  455. if (event_flags_ret != NULL) {
  456. *event_flags_ret = event_flags;
  457. }
  458. return ret;
  459. }
  460. // ------------------------------------------------ Client Functions ---------------------------------------------------
  461. // ----------------------- Private -------------------------
  462. static void _handle_pending_ep(client_t *client_obj)
  463. {
  464. //Handle each EP on the pending list
  465. while (!TAILQ_EMPTY(&client_obj->dynamic.pending_ep_tailq)) {
  466. //Get the next pending EP.
  467. endpoint_t *ep_obj = TAILQ_FIRST(&client_obj->dynamic.pending_ep_tailq);
  468. TAILQ_REMOVE(&client_obj->dynamic.pending_ep_tailq, ep_obj, dynamic.tailq_entry);
  469. TAILQ_INSERT_TAIL(&client_obj->dynamic.idle_ep_tailq, ep_obj, dynamic.tailq_entry);
  470. ep_obj->dynamic.flags.pending = 0;
  471. hcd_pipe_event_t last_event = ep_obj->dynamic.last_event;
  472. uint32_t num_urb_dequeued = 0;
  473. HOST_EXIT_CRITICAL();
  474. //Handle pipe event
  475. switch (last_event) {
  476. case HCD_PIPE_EVENT_ERROR_XFER:
  477. case HCD_PIPE_EVENT_ERROR_URB_NOT_AVAIL:
  478. case HCD_PIPE_EVENT_ERROR_OVERFLOW:
  479. case HCD_PIPE_EVENT_ERROR_STALL:
  480. //The pipe is now stalled. Flush all pending URBs
  481. ESP_ERROR_CHECK(hcd_pipe_command(ep_obj->constant.pipe_hdl, HCD_PIPE_CMD_FLUSH));
  482. //All URBs in this pipe are now retired waiting to be dequeued. Fall through to dequeue them
  483. __attribute__((fallthrough));
  484. case HCD_PIPE_EVENT_URB_DONE: {
  485. //Dequeue all URBs and run their transfer callback
  486. urb_t *urb = hcd_urb_dequeue(ep_obj->constant.pipe_hdl);
  487. while (urb != NULL) {
  488. urb->transfer.callback(&urb->transfer);
  489. num_urb_dequeued++;
  490. urb = hcd_urb_dequeue(ep_obj->constant.pipe_hdl);
  491. }
  492. break;
  493. }
  494. default:
  495. abort(); //Should never occur
  496. break;
  497. }
  498. HOST_ENTER_CRITICAL();
  499. //Update the endpoint's number of URB's inflight
  500. assert(num_urb_dequeued <= ep_obj->dynamic.num_urb_inflight);
  501. ep_obj->dynamic.num_urb_inflight -= num_urb_dequeued;
  502. }
  503. }
  504. // ----------------------- Public --------------------------
  505. esp_err_t usb_host_client_register(const usb_host_client_config_t *client_config, usb_host_client_handle_t *client_hdl_ret)
  506. {
  507. HOST_CHECK(client_config != NULL && client_hdl_ret != NULL, ESP_ERR_INVALID_ARG);
  508. HOST_CHECK(client_config->client_event_callback != NULL && client_config->max_num_event_msg > 0, ESP_ERR_INVALID_ARG);
  509. esp_err_t ret;
  510. //Create client object
  511. client_t *client_obj = heap_caps_calloc(1, sizeof(client_t), MALLOC_CAP_DEFAULT);
  512. SemaphoreHandle_t event_sem = xSemaphoreCreateBinary();
  513. QueueHandle_t event_msg_queue = xQueueCreate(client_config->max_num_event_msg, sizeof(usb_host_client_event_msg_t));
  514. if (client_obj == NULL || event_sem == NULL || event_msg_queue == NULL) {
  515. ret = ESP_ERR_NO_MEM;
  516. goto alloc_err;
  517. }
  518. //Initialize client object
  519. TAILQ_INIT(&client_obj->dynamic.pending_ep_tailq);
  520. TAILQ_INIT(&client_obj->dynamic.idle_ep_tailq);
  521. TAILQ_INIT(&client_obj->mux_protected.interface_tailq);
  522. TAILQ_INIT(&client_obj->dynamic.done_ctrl_xfer_tailq);
  523. client_obj->constant.event_sem = event_sem;
  524. client_obj->constant.event_callback = client_config->client_event_callback;
  525. client_obj->constant.callback_arg = client_config->callback_arg;
  526. client_obj->constant.event_msg_queue = event_msg_queue;
  527. //Add client to the host library's list of clients
  528. xSemaphoreTake(p_host_lib_obj->constant.mux_lock, portMAX_DELAY);
  529. HOST_ENTER_CRITICAL();
  530. p_host_lib_obj->dynamic.flags.num_clients++;
  531. HOST_EXIT_CRITICAL();
  532. TAILQ_INSERT_TAIL(&p_host_lib_obj->mux_protected.client_tailq, client_obj, dynamic.tailq_entry);
  533. xSemaphoreGive(p_host_lib_obj->constant.mux_lock);
  534. //Write back client handle
  535. *client_hdl_ret = (usb_host_client_handle_t)client_obj;
  536. ret = ESP_OK;
  537. return ret;
  538. alloc_err:
  539. if (event_msg_queue) {
  540. vQueueDelete(event_msg_queue);
  541. }
  542. if (event_sem) {
  543. vSemaphoreDelete(event_sem);
  544. }
  545. heap_caps_free(client_obj);
  546. return ESP_OK;
  547. }
  548. esp_err_t usb_host_client_deregister(usb_host_client_handle_t client_hdl)
  549. {
  550. HOST_CHECK(client_hdl != NULL, ESP_ERR_INVALID_ARG);
  551. client_t *client_obj = (client_t *)client_hdl;
  552. esp_err_t ret;
  553. //We take the mux_lock because we need to access the host library's client_tailq
  554. xSemaphoreTake(p_host_lib_obj->constant.mux_lock, portMAX_DELAY);
  555. HOST_ENTER_CRITICAL();
  556. //Check that client can currently deregistered
  557. bool can_deregister;
  558. if (!TAILQ_EMPTY(&client_obj->dynamic.pending_ep_tailq) ||
  559. !TAILQ_EMPTY(&client_obj->dynamic.idle_ep_tailq) ||
  560. !TAILQ_EMPTY(&client_obj->dynamic.done_ctrl_xfer_tailq) ||
  561. client_obj->dynamic.flags.handling_events ||
  562. client_obj->dynamic.flags.blocked ||
  563. client_obj->dynamic.flags.taking_mux ||
  564. client_obj->dynamic.flags.num_intf_claimed != 0 ||
  565. client_obj->dynamic.num_done_ctrl_xfer != 0 ||
  566. client_obj->dynamic.opened_dev_addr_map != 0) {
  567. can_deregister = false;
  568. } else {
  569. can_deregister = true;
  570. }
  571. HOST_EXIT_CRITICAL();
  572. if (!can_deregister) {
  573. ret = ESP_ERR_INVALID_STATE;
  574. goto exit;
  575. }
  576. //Remove client object from the library's list of clients
  577. TAILQ_REMOVE(&p_host_lib_obj->mux_protected.client_tailq, client_obj, dynamic.tailq_entry);
  578. HOST_ENTER_CRITICAL();
  579. p_host_lib_obj->dynamic.flags.num_clients--;
  580. if (p_host_lib_obj->dynamic.flags.num_clients == 0) {
  581. //This is the last client being deregistered. Notify the lib handler
  582. p_host_lib_obj->dynamic.lib_event_flags |= USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS;
  583. _unblock_lib(false);
  584. }
  585. HOST_EXIT_CRITICAL();
  586. //Free client object
  587. vQueueDelete(client_obj->constant.event_msg_queue);
  588. vSemaphoreDelete(client_obj->constant.event_sem);
  589. heap_caps_free(client_obj);
  590. ret = ESP_OK;
  591. exit:
  592. xSemaphoreGive(p_host_lib_obj->constant.mux_lock);
  593. return ret;
  594. }
  595. esp_err_t usb_host_client_handle_events(usb_host_client_handle_t client_hdl, TickType_t timeout_ticks)
  596. {
  597. HOST_CHECK(client_hdl != NULL, ESP_ERR_INVALID_ARG);
  598. esp_err_t ret;
  599. client_t *client_obj = (client_t *)client_hdl;
  600. HOST_ENTER_CRITICAL();
  601. if (!client_obj->dynamic.flags.events_pending) {
  602. //There are currently no events, wait for one to occur
  603. client_obj->dynamic.flags.blocked = 1;
  604. HOST_EXIT_CRITICAL();
  605. BaseType_t sem_ret = xSemaphoreTake(client_obj->constant.event_sem, timeout_ticks);
  606. HOST_ENTER_CRITICAL();
  607. client_obj->dynamic.flags.blocked = 0;
  608. if (sem_ret == pdFALSE) {
  609. HOST_EXIT_CRITICAL();
  610. //Timed out waiting for semaphore
  611. ret = ESP_ERR_TIMEOUT;
  612. goto exit;
  613. }
  614. }
  615. //Mark that we're processing events
  616. client_obj->dynamic.flags.handling_events = 1;
  617. while (client_obj->dynamic.flags.handling_events) {
  618. //Handle pending endpoints
  619. if (!TAILQ_EMPTY(&client_obj->dynamic.pending_ep_tailq)) {
  620. _handle_pending_ep(client_obj);
  621. }
  622. //Handle any done control transfers
  623. while (client_obj->dynamic.num_done_ctrl_xfer > 0) {
  624. urb_t *urb = TAILQ_FIRST(&client_obj->dynamic.done_ctrl_xfer_tailq);
  625. TAILQ_REMOVE(&client_obj->dynamic.done_ctrl_xfer_tailq, urb, tailq_entry);
  626. client_obj->dynamic.num_done_ctrl_xfer--;
  627. HOST_EXIT_CRITICAL();
  628. //Call the transfer's callback
  629. urb->transfer.callback(&urb->transfer);
  630. HOST_ENTER_CRITICAL();
  631. }
  632. //Handle event messages
  633. while (uxQueueMessagesWaiting(client_obj->constant.event_msg_queue) > 0) {
  634. HOST_EXIT_CRITICAL();
  635. //Dequeue the event message and call the client event callback
  636. usb_host_client_event_msg_t event_msg;
  637. BaseType_t queue_ret = xQueueReceive(client_obj->constant.event_msg_queue, &event_msg, 0);
  638. assert(queue_ret == pdTRUE);
  639. client_obj->constant.event_callback(&event_msg, client_obj->constant.callback_arg);
  640. HOST_ENTER_CRITICAL();
  641. }
  642. //Check each event again to see any new events occurred
  643. if (TAILQ_EMPTY(&client_obj->dynamic.pending_ep_tailq) &&
  644. client_obj->dynamic.num_done_ctrl_xfer == 0 &&
  645. uxQueueMessagesWaiting(client_obj->constant.event_msg_queue) == 0) {
  646. //All pending endpoints and event messages handled
  647. client_obj->dynamic.flags.events_pending = 0;
  648. client_obj->dynamic.flags.handling_events = 0;
  649. }
  650. }
  651. HOST_EXIT_CRITICAL();
  652. ret = ESP_OK;
  653. exit:
  654. return ret;
  655. }
  656. esp_err_t usb_host_client_unblock(usb_host_client_handle_t client_hdl)
  657. {
  658. HOST_CHECK(client_hdl != NULL, ESP_ERR_INVALID_ARG);
  659. client_t *client_obj = (client_t *)client_hdl;
  660. HOST_ENTER_CRITICAL();
  661. _unblock_client(client_obj, false);
  662. HOST_EXIT_CRITICAL();
  663. return ESP_OK;
  664. }
  665. // ------------------------------------------------- Device Handling ---------------------------------------------------
  666. esp_err_t usb_host_device_open(usb_host_client_handle_t client_hdl, uint8_t dev_addr, usb_device_handle_t *dev_hdl_ret)
  667. {
  668. HOST_CHECK(dev_addr > 0 && client_hdl != NULL && dev_hdl_ret != NULL, ESP_ERR_INVALID_ARG);
  669. client_t *client_obj = (client_t *)client_hdl;
  670. esp_err_t ret;
  671. usb_device_handle_t dev_hdl;
  672. ret = usbh_dev_open(dev_addr, &dev_hdl);
  673. if (ret != ESP_OK) {
  674. goto exit;
  675. }
  676. HOST_ENTER_CRITICAL();
  677. if (_check_client_opened_device(client_obj, dev_addr)) {
  678. //Client has already opened the device. Close it and return an error
  679. ret = ESP_ERR_INVALID_STATE;
  680. HOST_EXIT_CRITICAL();
  681. goto already_opened;
  682. }
  683. //Record in client object that we have opened the device of this address
  684. _record_client_opened_device(client_obj, dev_addr);
  685. HOST_EXIT_CRITICAL();
  686. *dev_hdl_ret = dev_hdl;
  687. ret = ESP_OK;
  688. return ret;
  689. already_opened:
  690. ESP_ERROR_CHECK(usbh_dev_close(dev_hdl));
  691. exit:
  692. return ret;
  693. }
  694. esp_err_t usb_host_device_close(usb_host_client_handle_t client_hdl, usb_device_handle_t dev_hdl)
  695. {
  696. HOST_CHECK(dev_hdl != NULL && client_hdl != NULL, ESP_ERR_INVALID_ARG);
  697. client_t *client_obj = (client_t *)client_hdl;
  698. //We take the lock because we need to walk the interface list
  699. xSemaphoreTake(p_host_lib_obj->constant.mux_lock, portMAX_DELAY);
  700. esp_err_t ret;
  701. //Check that all interfaces claimed by this client do not belong to this device
  702. bool all_released = true;
  703. interface_t *intf_obj;
  704. TAILQ_FOREACH(intf_obj, &client_obj->mux_protected.interface_tailq, mux_protected.tailq_entry) {
  705. if (intf_obj->constant.dev_hdl == dev_hdl) {
  706. all_released = false;
  707. break;
  708. }
  709. }
  710. if (!all_released) {
  711. ret = ESP_ERR_INVALID_STATE;
  712. goto exit;
  713. }
  714. //Check that client actually opened the device in the first place
  715. HOST_ENTER_CRITICAL();
  716. uint8_t dev_addr;
  717. ESP_ERROR_CHECK(usbh_dev_get_addr(dev_hdl, &dev_addr));
  718. HOST_CHECK_FROM_CRIT(_check_client_opened_device(client_obj, dev_addr), ESP_ERR_NOT_FOUND);
  719. if (!_check_client_opened_device(client_obj, dev_addr)) {
  720. //Client never opened this device
  721. ret = ESP_ERR_INVALID_STATE;
  722. HOST_EXIT_CRITICAL();
  723. goto exit;
  724. }
  725. //Proceed to clear the record of the device form the client
  726. _clear_client_opened_device(client_obj, dev_addr);
  727. HOST_EXIT_CRITICAL();
  728. ESP_ERROR_CHECK(usbh_dev_close(dev_hdl));
  729. ret = ESP_OK;
  730. exit:
  731. xSemaphoreGive(p_host_lib_obj->constant.mux_lock);
  732. return ret;
  733. }
  734. esp_err_t usb_host_device_free_all(void)
  735. {
  736. HOST_ENTER_CRITICAL();
  737. HOST_CHECK_FROM_CRIT(p_host_lib_obj->dynamic.flags.num_clients == 0, ESP_ERR_INVALID_STATE); //All clients must have been deregistered
  738. HOST_EXIT_CRITICAL();
  739. esp_err_t ret;
  740. ret = usbh_dev_mark_all_free();
  741. //Wait for USB_HOST_LIB_EVENT_FLAGS_ALL_FREE to confirm all devices free
  742. return ret;
  743. }
  744. // ------------------------------------------------- Device Requests ---------------------------------------------------
  745. // ------------------- Cached Requests ---------------------
  746. esp_err_t usb_host_device_info(usb_device_handle_t dev_hdl, usb_device_info_t *dev_info)
  747. {
  748. HOST_CHECK(dev_hdl != NULL && dev_info != NULL, ESP_ERR_INVALID_ARG);
  749. return usbh_dev_get_info(dev_hdl, dev_info);
  750. }
  751. // ----------------------------------------------- Descriptor Requests -------------------------------------------------
  752. // ----------------- Cached Descriptors --------------------
  753. esp_err_t usb_host_get_device_descriptor(usb_device_handle_t dev_hdl, const usb_device_desc_t **device_desc)
  754. {
  755. HOST_CHECK(dev_hdl != NULL && device_desc != NULL, ESP_ERR_INVALID_ARG);
  756. return usbh_dev_get_desc(dev_hdl, device_desc);
  757. }
  758. esp_err_t usb_host_get_active_config_descriptor(usb_device_handle_t dev_hdl, const usb_config_desc_t **config_desc)
  759. {
  760. HOST_CHECK(dev_hdl != NULL && config_desc != NULL, ESP_ERR_INVALID_ARG);
  761. return usbh_dev_get_config_desc(dev_hdl, config_desc);
  762. }
  763. // ----------------------------------------------- Interface Functions -------------------------------------------------
  764. // ----------------------- Private -------------------------
  765. static esp_err_t endpoint_alloc(usb_device_handle_t dev_hdl, const usb_ep_desc_t *ep_desc, interface_t *intf_obj, endpoint_t **ep_obj_ret)
  766. {
  767. endpoint_t *ep_obj = heap_caps_calloc(1, sizeof(endpoint_t), MALLOC_CAP_DEFAULT);
  768. if (ep_obj == NULL) {
  769. return ESP_ERR_NO_MEM;
  770. }
  771. esp_err_t ret;
  772. usbh_ep_config_t ep_config = {
  773. .ep_desc = ep_desc,
  774. .pipe_cb = pipe_callback,
  775. .pipe_cb_arg = (void *)ep_obj,
  776. .context = (void *)ep_obj,
  777. };
  778. hcd_pipe_handle_t pipe_hdl;
  779. ret = usbh_ep_alloc(dev_hdl, &ep_config, &pipe_hdl);
  780. if (ret != ESP_OK) {
  781. goto ep_alloc_err;
  782. }
  783. //Initialize endpoint object
  784. ep_obj->constant.pipe_hdl = pipe_hdl;
  785. ep_obj->constant.ep_desc = ep_desc;
  786. ep_obj->constant.intf_obj = intf_obj;
  787. //Write back result
  788. *ep_obj_ret = ep_obj;
  789. ret = ESP_OK;
  790. return ret;
  791. ep_alloc_err:
  792. heap_caps_free(ep_obj);
  793. return ret;
  794. }
  795. static void endpoint_free(usb_device_handle_t dev_hdl, endpoint_t *ep_obj)
  796. {
  797. if (ep_obj == NULL) {
  798. return;
  799. }
  800. //Free the underlying endpoint
  801. ESP_ERROR_CHECK(usbh_ep_free(dev_hdl, ep_obj->constant.ep_desc->bEndpointAddress));
  802. //Free the endpoint object
  803. heap_caps_free(ep_obj);
  804. }
  805. static interface_t *interface_alloc(client_t *client_obj, usb_device_handle_t dev_hdl, const usb_intf_desc_t *intf_desc)
  806. {
  807. interface_t *intf_obj = heap_caps_calloc(1, sizeof(interface_t) + (sizeof(endpoint_t *) * intf_desc->bNumEndpoints), MALLOC_CAP_DEFAULT);
  808. if (intf_obj == NULL) {
  809. return NULL;
  810. }
  811. intf_obj->constant.intf_desc = intf_desc;
  812. intf_obj->constant.client_obj = client_obj;
  813. intf_obj->constant.dev_hdl = dev_hdl;
  814. return intf_obj;
  815. }
  816. static void interface_free(interface_t *intf_obj)
  817. {
  818. if (intf_obj == NULL) {
  819. return;
  820. }
  821. for (int i = 0; i < intf_obj->constant.intf_desc->bNumEndpoints; i++) {
  822. assert(intf_obj->constant.endpoints[i] == NULL);
  823. }
  824. heap_caps_free(intf_obj);
  825. }
  826. static esp_err_t interface_claim(client_t *client_obj, usb_device_handle_t dev_hdl, const usb_config_desc_t *config_desc, uint8_t bInterfaceNumber, uint8_t bAlternateSetting, interface_t **intf_obj_ret)
  827. {
  828. esp_err_t ret;
  829. //We need to walk to configuration descriptor to find the correct interface descriptor, and each of its constituent endpoint descriptors
  830. //Find the interface descriptor and allocate the interface object
  831. int offset_intf;
  832. const usb_intf_desc_t *intf_desc = usb_parse_interface_descriptor(config_desc, bInterfaceNumber, bAlternateSetting, &offset_intf);
  833. if (intf_desc == NULL) {
  834. ret = ESP_ERR_NOT_FOUND;
  835. goto exit;
  836. }
  837. //Allocate interface object
  838. interface_t *intf_obj = interface_alloc(client_obj, dev_hdl, intf_desc);
  839. if (intf_obj == NULL) {
  840. ret = ESP_ERR_NO_MEM;
  841. goto exit;
  842. }
  843. //Find each endpoint descriptor in the interface by index, and allocate those endpoints
  844. for (int i = 0; i < intf_desc->bNumEndpoints; i++) {
  845. int offset_ep = offset_intf;
  846. const usb_ep_desc_t *ep_desc = usb_parse_endpoint_descriptor_by_index(intf_desc, i, config_desc->wTotalLength, &offset_ep);
  847. if (ep_desc == NULL) {
  848. ret = ESP_ERR_NOT_FOUND;
  849. goto ep_alloc_err;
  850. }
  851. //Allocate the endpoint
  852. endpoint_t *ep_obj;
  853. ret = endpoint_alloc(dev_hdl, ep_desc, intf_obj, &ep_obj);
  854. if (ret != ESP_OK) {
  855. goto ep_alloc_err;
  856. }
  857. //Store endpoint object into interface object
  858. intf_obj->constant.endpoints[i] = ep_obj;
  859. }
  860. //Add interface object to client (safe because we have already taken the mutex)
  861. TAILQ_INSERT_TAIL(&client_obj->mux_protected.interface_tailq, intf_obj, mux_protected.tailq_entry);
  862. //Add each endpoint to the client's endpoint list
  863. HOST_ENTER_CRITICAL();
  864. for (int i = 0; i < intf_obj->constant.intf_desc->bNumEndpoints; i++) {
  865. TAILQ_INSERT_TAIL(&client_obj->dynamic.idle_ep_tailq, intf_obj->constant.endpoints[i], dynamic.tailq_entry);
  866. }
  867. HOST_EXIT_CRITICAL();
  868. //Write back result
  869. *intf_obj_ret = intf_obj;
  870. ret = ESP_OK;
  871. return ret;
  872. ep_alloc_err:
  873. for (int i = 0; i < intf_obj->constant.intf_desc->bNumEndpoints; i++) {
  874. endpoint_free(dev_hdl, intf_obj->constant.endpoints[i]);
  875. intf_obj->constant.endpoints[i] = NULL;
  876. }
  877. interface_free(intf_obj);
  878. exit:
  879. return ret;
  880. }
  881. static esp_err_t interface_release(client_t *client_obj, usb_device_handle_t dev_hdl, uint8_t bInterfaceNumber)
  882. {
  883. esp_err_t ret;
  884. //Find the interface object
  885. interface_t *intf_obj_iter;
  886. interface_t *intf_obj = NULL;
  887. TAILQ_FOREACH(intf_obj_iter, &client_obj->mux_protected.interface_tailq, mux_protected.tailq_entry) {
  888. if (intf_obj_iter->constant.dev_hdl == dev_hdl && intf_obj_iter->constant.intf_desc->bInterfaceNumber == bInterfaceNumber) {
  889. intf_obj = intf_obj_iter;
  890. break;
  891. }
  892. }
  893. if (intf_obj == NULL) {
  894. ret = ESP_ERR_NOT_FOUND;
  895. goto exit;
  896. }
  897. //Check that all endpoints in the interface are in a state to be freed
  898. HOST_ENTER_CRITICAL();
  899. bool can_free = true;
  900. for (int i = 0; i < intf_obj->constant.intf_desc->bNumEndpoints; i++) {
  901. endpoint_t *ep_obj = intf_obj->constant.endpoints[i];
  902. //Endpoint must not be on the pending list and must not have inflight URBs
  903. if (ep_obj->dynamic.num_urb_inflight != 0 || ep_obj->dynamic.flags.pending) {
  904. can_free = false;
  905. break;
  906. }
  907. }
  908. if (!can_free) {
  909. HOST_EXIT_CRITICAL();
  910. ret = ESP_ERR_INVALID_STATE;
  911. goto exit;
  912. }
  913. //Proceed to remove all endpoint objects from list
  914. for (int i = 0; i < intf_obj->constant.intf_desc->bNumEndpoints; i++) {
  915. TAILQ_REMOVE(&client_obj->dynamic.idle_ep_tailq, intf_obj->constant.endpoints[i], dynamic.tailq_entry);
  916. }
  917. HOST_EXIT_CRITICAL();
  918. //Remove the interface object from the list (safe because we have already taken the mutex)
  919. TAILQ_REMOVE(&client_obj->mux_protected.interface_tailq, intf_obj, mux_protected.tailq_entry);
  920. //Free each endpoint in the interface
  921. for (int i = 0; i < intf_obj->constant.intf_desc->bNumEndpoints; i++) {
  922. endpoint_free(dev_hdl, intf_obj->constant.endpoints[i]);
  923. intf_obj->constant.endpoints[i] = NULL;
  924. }
  925. //Free the interface object itself
  926. interface_free(intf_obj);
  927. ret = ESP_OK;
  928. exit:
  929. return ret;
  930. }
  931. // ----------------------- Public --------------------------
  932. esp_err_t usb_host_interface_claim(usb_host_client_handle_t client_hdl, usb_device_handle_t dev_hdl, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
  933. {
  934. HOST_CHECK(client_hdl != NULL && dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  935. client_t *client_obj = (client_t *)client_hdl;
  936. HOST_ENTER_CRITICAL();
  937. uint8_t dev_addr;
  938. ESP_ERROR_CHECK(usbh_dev_get_addr(dev_hdl, &dev_addr));
  939. //Check if client actually opened device
  940. HOST_CHECK_FROM_CRIT(_check_client_opened_device(client_obj, dev_addr), ESP_ERR_INVALID_STATE);
  941. client_obj->dynamic.flags.taking_mux = 1;
  942. HOST_EXIT_CRITICAL();
  943. //Take mux lock. This protects the client being released or other clients from claiming interfaces
  944. xSemaphoreTake(p_host_lib_obj->constant.mux_lock, portMAX_DELAY);
  945. esp_err_t ret;
  946. const usb_config_desc_t *config_desc;
  947. ESP_ERROR_CHECK(usbh_dev_get_config_desc(dev_hdl, &config_desc));
  948. interface_t *intf_obj;
  949. //Claim interface
  950. ret = interface_claim(client_obj, dev_hdl, config_desc, bInterfaceNumber, bAlternateSetting, &intf_obj);
  951. if (ret != ESP_OK) {
  952. goto exit;
  953. }
  954. ret = ESP_OK;
  955. exit:
  956. xSemaphoreGive(p_host_lib_obj->constant.mux_lock);
  957. HOST_ENTER_CRITICAL();
  958. if (ret == ESP_OK) {
  959. client_obj->dynamic.flags.num_intf_claimed++;
  960. }
  961. client_obj->dynamic.flags.taking_mux = 0;
  962. HOST_EXIT_CRITICAL();
  963. return ret;
  964. }
  965. esp_err_t usb_host_interface_release(usb_host_client_handle_t client_hdl, usb_device_handle_t dev_hdl, uint8_t bInterfaceNumber)
  966. {
  967. HOST_CHECK(client_hdl != NULL && dev_hdl != NULL, ESP_ERR_INVALID_ARG);
  968. client_t *client_obj = (client_t *)client_hdl;
  969. HOST_ENTER_CRITICAL();
  970. uint8_t dev_addr;
  971. ESP_ERROR_CHECK(usbh_dev_get_addr(dev_hdl, &dev_addr));
  972. //Check if client actually opened device
  973. HOST_CHECK_FROM_CRIT(_check_client_opened_device(client_obj, dev_addr), ESP_ERR_INVALID_STATE);
  974. client_obj->dynamic.flags.taking_mux = 1;
  975. HOST_EXIT_CRITICAL();
  976. //Take mux lock. This protects the client being released or other clients from claiming interfaces
  977. xSemaphoreTake(p_host_lib_obj->constant.mux_lock, portMAX_DELAY);
  978. esp_err_t ret = interface_release(client_obj, dev_hdl, bInterfaceNumber);
  979. xSemaphoreGive(p_host_lib_obj->constant.mux_lock);
  980. HOST_ENTER_CRITICAL();
  981. if (ret == ESP_OK) {
  982. client_obj->dynamic.flags.num_intf_claimed--;
  983. }
  984. client_obj->dynamic.flags.taking_mux = 0;
  985. HOST_EXIT_CRITICAL();
  986. return ret;
  987. }
  988. esp_err_t usb_host_endpoint_halt(usb_device_handle_t dev_hdl, uint8_t bEndpointAddress)
  989. {
  990. esp_err_t ret;
  991. endpoint_t *ep_obj = NULL;
  992. ret = usbh_ep_get_context(dev_hdl, bEndpointAddress, (void **)&ep_obj);
  993. if (ret != ESP_OK) {
  994. goto exit;
  995. }
  996. assert(ep_obj != NULL);
  997. ret = hcd_pipe_command(ep_obj->constant.pipe_hdl, HCD_PIPE_CMD_HALT);
  998. exit:
  999. return ret;
  1000. }
  1001. esp_err_t usb_host_endpoint_flush(usb_device_handle_t dev_hdl, uint8_t bEndpointAddress)
  1002. {
  1003. esp_err_t ret;
  1004. endpoint_t *ep_obj = NULL;
  1005. ret = usbh_ep_get_context(dev_hdl, bEndpointAddress, (void **)&ep_obj);
  1006. if (ret != ESP_OK) {
  1007. goto exit;
  1008. }
  1009. assert(ep_obj != NULL);
  1010. ret = hcd_pipe_command(ep_obj->constant.pipe_hdl, HCD_PIPE_CMD_FLUSH);
  1011. exit:
  1012. return ret;
  1013. }
  1014. esp_err_t usb_host_endpoint_clear(usb_device_handle_t dev_hdl, uint8_t bEndpointAddress)
  1015. {
  1016. esp_err_t ret;
  1017. endpoint_t *ep_obj = NULL;
  1018. ret = usbh_ep_get_context(dev_hdl, bEndpointAddress, (void **)&ep_obj);
  1019. if (ret != ESP_OK) {
  1020. goto exit;
  1021. }
  1022. assert(ep_obj != NULL);
  1023. ret = hcd_pipe_command(ep_obj->constant.pipe_hdl, HCD_PIPE_CMD_CLEAR);
  1024. exit:
  1025. return ret;
  1026. }
  1027. // ------------------------------------------------ Asynchronous I/O ---------------------------------------------------
  1028. // ----------------------- Private -------------------------
  1029. static bool transfer_check(usb_transfer_t *transfer, usb_transfer_type_t type, int mps, bool is_in)
  1030. {
  1031. if (transfer->callback == NULL) {
  1032. ESP_LOGE(USB_HOST_TAG, "Transfer callback is NULL");
  1033. return false;
  1034. }
  1035. //Check that the total transfer length does not exceed data buffer size
  1036. if (transfer->num_bytes > transfer->data_buffer_size) {
  1037. ESP_LOGE(USB_HOST_TAG, "Transfer num_bytes > data_buffer_size");
  1038. return false;
  1039. }
  1040. if (type == USB_TRANSFER_TYPE_CTRL) {
  1041. //Check that num_bytes and wLength are set correctly
  1042. usb_setup_packet_t *setup_pkt = (usb_setup_packet_t *)transfer->data_buffer;
  1043. if (transfer->num_bytes != sizeof(usb_setup_packet_t) + setup_pkt->wLength) {
  1044. ESP_LOGE(USB_HOST_TAG, "Control transfer num_bytes wLength mismatch");
  1045. return false;
  1046. }
  1047. } else if (type == USB_TRANSFER_TYPE_ISOCHRONOUS) {
  1048. //Check that there is at least one isochronous packet descriptor
  1049. if (transfer->num_isoc_packets <= 0) {
  1050. ESP_LOGE(USB_HOST_TAG, "ISOC transfer has 0 packet descriptors");
  1051. return false;
  1052. }
  1053. //Check that sum of all packet lengths add up to transfer length
  1054. //If IN, check that each packet length is integer multiple of MPS
  1055. int total_num_bytes = 0;
  1056. bool mod_mps_all_zero = true;
  1057. for (int i = 0; i < transfer->num_isoc_packets; i++) {
  1058. total_num_bytes += transfer->isoc_packet_desc[i].num_bytes;
  1059. if (transfer->isoc_packet_desc[i].num_bytes % mps != 0) {
  1060. mod_mps_all_zero = false;
  1061. }
  1062. }
  1063. if (transfer->num_bytes != total_num_bytes) {
  1064. ESP_LOGE(USB_HOST_TAG, "ISOC transfer num_bytes not equal to total num_bytes of all packets");
  1065. return false;
  1066. }
  1067. if (is_in && !mod_mps_all_zero) {
  1068. ESP_LOGE(USB_HOST_TAG, "ISOC IN transfer all packets num_bytes must be integer multiple of MPS");
  1069. return false;
  1070. }
  1071. } else {
  1072. //Check that IN transfers are integer multiple of MPS
  1073. if (is_in && (transfer->num_bytes % mps != 0)) {
  1074. ESP_LOGE(USB_HOST_TAG, "IN transfer num_bytes must be integer multiple of MPS");
  1075. return false;
  1076. }
  1077. }
  1078. return true;
  1079. }
  1080. // ----------------------- Public --------------------------
  1081. esp_err_t usb_host_transfer_alloc(size_t data_buffer_size, int num_isoc_packets, usb_transfer_t **transfer)
  1082. {
  1083. urb_t *urb = urb_alloc(data_buffer_size, 0, num_isoc_packets);
  1084. if (urb == NULL) {
  1085. return ESP_ERR_NO_MEM;
  1086. }
  1087. *transfer = &urb->transfer;
  1088. return ESP_OK;
  1089. }
  1090. esp_err_t usb_host_transfer_free(usb_transfer_t *transfer)
  1091. {
  1092. HOST_CHECK(transfer != NULL, ESP_ERR_INVALID_ARG);
  1093. urb_t *urb = __containerof(transfer, urb_t, transfer);
  1094. urb_free(urb);
  1095. return ESP_OK;
  1096. }
  1097. esp_err_t usb_host_transfer_submit(usb_transfer_t *transfer)
  1098. {
  1099. HOST_CHECK(transfer != NULL, ESP_ERR_INVALID_ARG);
  1100. //Check that transfer and target endpoint are valid
  1101. HOST_CHECK(transfer->device_handle != NULL, ESP_ERR_INVALID_ARG); //Target device must be set
  1102. HOST_CHECK((transfer->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK) != 0, ESP_ERR_INVALID_ARG);
  1103. endpoint_t *ep_obj = NULL;
  1104. urb_t *urb_obj = __containerof(transfer, urb_t, transfer);
  1105. esp_err_t ret;
  1106. ret = usbh_ep_get_context(transfer->device_handle, transfer->bEndpointAddress, (void **)&ep_obj);
  1107. if (ret != ESP_OK) {
  1108. goto err;
  1109. }
  1110. assert(ep_obj != NULL);
  1111. HOST_CHECK(transfer_check(transfer,
  1112. USB_EP_DESC_GET_XFERTYPE(ep_obj->constant.ep_desc),
  1113. USB_EP_DESC_GET_MPS(ep_obj->constant.ep_desc),
  1114. transfer->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK), ESP_ERR_INVALID_ARG);
  1115. HOST_ENTER_CRITICAL();
  1116. ep_obj->dynamic.num_urb_inflight++;
  1117. HOST_EXIT_CRITICAL();
  1118. //Check if pipe is in a state to enqueue URBs
  1119. if (hcd_pipe_get_state(ep_obj->constant.pipe_hdl) != HCD_PIPE_STATE_ACTIVE) {
  1120. ret = ESP_ERR_INVALID_STATE;
  1121. goto hcd_err;
  1122. }
  1123. ret = hcd_urb_enqueue(ep_obj->constant.pipe_hdl, urb_obj);
  1124. if (ret != ESP_OK) {
  1125. goto hcd_err;
  1126. }
  1127. ret = ESP_OK;
  1128. return ret;
  1129. hcd_err:
  1130. HOST_ENTER_CRITICAL();
  1131. ep_obj->dynamic.num_urb_inflight--;
  1132. HOST_EXIT_CRITICAL();
  1133. err:
  1134. return ret;
  1135. }
  1136. esp_err_t usb_host_transfer_submit_control(usb_host_client_handle_t client_hdl, usb_transfer_t *transfer)
  1137. {
  1138. HOST_CHECK(client_hdl != NULL && transfer != NULL, ESP_ERR_INVALID_ARG);
  1139. //Check that control transfer is valid
  1140. HOST_CHECK(transfer->device_handle != NULL, ESP_ERR_INVALID_ARG); //Target device must be set
  1141. usb_device_handle_t dev_hdl = transfer->device_handle;
  1142. bool xfer_is_in = ((usb_setup_packet_t *)transfer->data_buffer)->bmRequestType & USB_BM_REQUEST_TYPE_DIR_OUT;
  1143. usb_device_info_t dev_info;
  1144. ESP_ERROR_CHECK(usbh_dev_get_info(dev_hdl, &dev_info));
  1145. HOST_CHECK(transfer_check(transfer, USB_TRANSFER_TYPE_CTRL, dev_info.bMaxPacketSize0, xfer_is_in), ESP_ERR_INVALID_ARG);
  1146. HOST_CHECK(transfer->bEndpointAddress == 0, ESP_ERR_INVALID_ARG);
  1147. //Save client handle into URB
  1148. urb_t *urb_obj = __containerof(transfer, urb_t, transfer);
  1149. urb_obj->usb_host_client = (void *)client_hdl;
  1150. return usbh_dev_submit_ctrl_urb(dev_hdl, urb_obj);
  1151. }