test_hcd_port.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/semphr.h"
  9. #include "unity.h"
  10. #include "esp_rom_sys.h"
  11. #include "test_utils.h"
  12. #include "test_hcd_common.h"
  13. #define TEST_DEV_ADDR 0
  14. #define NUM_URBS 3
  15. #define TRANSFER_MAX_BYTES 256
  16. #define URB_DATA_BUFF_SIZE (sizeof(usb_setup_packet_t) + TRANSFER_MAX_BYTES) //256 is worst case size for configuration descriptors
  17. #define POST_ENQUEUE_DELAY_US 10
  18. /*
  19. Test a port sudden disconnect and port recovery
  20. Purpose: Test that when sudden disconnection happens on an HCD port, the port will
  21. - Generate the HCD_PORT_EVENT_SUDDEN_DISCONN and be put into the HCD_PORT_STATE_RECOVERY state
  22. - Pipes can be halted and flushed after a port error
  23. Procedure:
  24. - Setup the HCD and a port
  25. - Trigger a port connection
  26. - Create a default pipe
  27. - Start transfers but trigger a disconnect after a short delay
  28. - Check that HCD_PORT_EVENT_SUDDEN_DISCONN event is generated. Handle that port event.
  29. - Check that the default pipe remains in the HCD_PIPE_STATE_ACTIVE after the port error.
  30. - Check that the default pipe can be halted, a HCD_PIPE_EVENT_URB_DONE event should be generated
  31. - Check that the default pipe can be flushed, a HCD_PIPE_EVENT_URB_DONE event should be generated
  32. - Check that all URBs can be dequeued.
  33. - Free default pipe
  34. - Recover the port
  35. - Trigger connection and disconnection again (to make sure the port works post recovery)
  36. - Teardown port and HCD
  37. */
  38. TEST_CASE("Test HCD port sudden disconnect", "[hcd][ignore]")
  39. {
  40. hcd_port_handle_t port_hdl = test_hcd_setup(); //Setup the HCD and port
  41. usb_speed_t port_speed = test_hcd_wait_for_conn(port_hdl); //Trigger a connection
  42. vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
  43. //Allocate some URBs and initialize their data buffers with control transfers
  44. hcd_pipe_handle_t default_pipe = test_hcd_pipe_alloc(port_hdl, NULL, TEST_DEV_ADDR, port_speed); //Create a default pipe (using a NULL EP descriptor)
  45. urb_t *urb_list[NUM_URBS];
  46. for (int i = 0; i < NUM_URBS; i++) {
  47. urb_list[i] = test_hcd_alloc_urb(0, URB_DATA_BUFF_SIZE);
  48. //Initialize with a "Get Config Descriptor request"
  49. urb_list[i]->transfer.num_bytes = sizeof(usb_setup_packet_t) + TRANSFER_MAX_BYTES;
  50. USB_SETUP_PACKET_INIT_GET_CONFIG_DESC((usb_setup_packet_t *)urb_list[i]->transfer.data_buffer, 0, TRANSFER_MAX_BYTES);
  51. urb_list[i]->transfer.context = (void *)0xDEADBEEF;
  52. }
  53. //Enqueue URBs but immediately trigger a disconnect
  54. printf("Enqueuing URBs\n");
  55. for (int i = 0; i < NUM_URBS; i++) {
  56. TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(default_pipe, urb_list[i]));
  57. }
  58. //Add a short delay to let the transfers run for a bit
  59. esp_rom_delay_us(POST_ENQUEUE_DELAY_US);
  60. test_hcd_force_conn_state(false, 0);
  61. //Disconnect event should have occurred. Handle the event
  62. test_hcd_expect_port_event(port_hdl, HCD_PORT_EVENT_DISCONNECTION);
  63. TEST_ASSERT_EQUAL(HCD_PORT_EVENT_DISCONNECTION, hcd_port_handle_event(port_hdl));
  64. TEST_ASSERT_EQUAL(HCD_PORT_STATE_RECOVERY, hcd_port_get_state(port_hdl));
  65. printf("Sudden disconnect\n");
  66. //We should be able to halt then flush the pipe
  67. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_ACTIVE, hcd_pipe_get_state(default_pipe));
  68. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_HALT));
  69. test_hcd_expect_pipe_event(default_pipe, HCD_PIPE_EVENT_URB_DONE);
  70. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_HALTED, hcd_pipe_get_state(default_pipe));
  71. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_FLUSH));
  72. test_hcd_expect_pipe_event(default_pipe, HCD_PIPE_EVENT_URB_DONE);
  73. printf("Pipe halted and flushed\n");
  74. //Dequeue URBs
  75. for (int i = 0; i < NUM_URBS; i++) {
  76. urb_t *urb = hcd_urb_dequeue(default_pipe);
  77. TEST_ASSERT_EQUAL(urb_list[i], urb);
  78. TEST_ASSERT(urb->transfer.status == USB_TRANSFER_STATUS_COMPLETED || urb->transfer.status == USB_TRANSFER_STATUS_CANCELED);
  79. if (urb->transfer.status == USB_TRANSFER_STATUS_COMPLETED) {
  80. //We must have transmitted at least the setup packet, but device may return less than bytes requested
  81. TEST_ASSERT_GREATER_OR_EQUAL(sizeof(usb_setup_packet_t), urb->transfer.actual_num_bytes);
  82. TEST_ASSERT_LESS_OR_EQUAL(urb->transfer.num_bytes, urb->transfer.actual_num_bytes);
  83. } else {
  84. //A failed transfer should 0 actual number of bytes transmitted
  85. TEST_ASSERT_EQUAL(0, urb->transfer.actual_num_bytes);
  86. }
  87. TEST_ASSERT_EQUAL(0xDEADBEEF, urb->transfer.context);
  88. }
  89. //Free URB list and pipe
  90. for (int i = 0; i < NUM_URBS; i++) {
  91. test_hcd_free_urb(urb_list[i]);
  92. }
  93. test_hcd_pipe_free(default_pipe);
  94. //Recover the port should return to the to NOT POWERED state
  95. TEST_ASSERT_EQUAL(ESP_OK, hcd_port_recover(port_hdl));
  96. TEST_ASSERT_EQUAL(HCD_PORT_STATE_NOT_POWERED, hcd_port_get_state(port_hdl));
  97. //Recovered port should be able to connect and disconnect again
  98. test_hcd_wait_for_conn(port_hdl);
  99. test_hcd_wait_for_disconn(port_hdl, false);
  100. test_hcd_teardown(port_hdl);
  101. }
  102. /*
  103. Test port suspend and resume with active pipes
  104. Purpose:
  105. - Test port suspend and resume procedure
  106. - When suspending, the pipes should be halted before suspending the port. Any pending transfers should remain pending
  107. - When resuming, the pipes should remain in the halted state
  108. - Pipes on being cleared of the halt should resume transferring the pending transfers
  109. Procedure:
  110. - Setup the HCD and a port
  111. - Trigger a port connection
  112. - Create a default pipe
  113. - Start transfers
  114. - Halt the default pipe after a short delay
  115. - Suspend the port
  116. - Resume the port
  117. - Check that all the URBs have either completed successfully or been canceled by the pipe halt
  118. - Cleanup URBs and default pipe
  119. - Trigger disconnection and teardown
  120. */
  121. TEST_CASE("Test HCD port suspend and resume", "[hcd][ignore]")
  122. {
  123. hcd_port_handle_t port_hdl = test_hcd_setup(); //Setup the HCD and port
  124. usb_speed_t port_speed = test_hcd_wait_for_conn(port_hdl); //Trigger a connection
  125. vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
  126. //Allocate some URBs and initialize their data buffers with control transfers
  127. hcd_pipe_handle_t default_pipe = test_hcd_pipe_alloc(port_hdl, NULL, TEST_DEV_ADDR, port_speed); //Create a default pipe (using a NULL EP descriptor)
  128. urb_t *urb_list[NUM_URBS];
  129. for (int i = 0; i < NUM_URBS; i++) {
  130. urb_list[i] = test_hcd_alloc_urb(0, URB_DATA_BUFF_SIZE);
  131. //Initialize with a "Get Config Descriptor request"
  132. urb_list[i]->transfer.num_bytes = sizeof(usb_setup_packet_t) + TRANSFER_MAX_BYTES;
  133. USB_SETUP_PACKET_INIT_GET_CONFIG_DESC((usb_setup_packet_t *)urb_list[i]->transfer.data_buffer, 0, TRANSFER_MAX_BYTES);
  134. urb_list[i]->transfer.context = (void *)0xDEADBEEF;
  135. }
  136. //Enqueue URBs but immediately suspend the port
  137. printf("Enqueuing URBs\n");
  138. for (int i = 0; i < NUM_URBS; i++) {
  139. TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(default_pipe, urb_list[i]));
  140. }
  141. //Add a short delay to let the transfers run for a bit
  142. esp_rom_delay_us(POST_ENQUEUE_DELAY_US);
  143. //Halt the default pipe before suspending
  144. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_ACTIVE, hcd_pipe_get_state(default_pipe));
  145. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_HALT));
  146. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_HALTED, hcd_pipe_get_state(default_pipe));
  147. //Suspend the port
  148. TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_SUSPEND));
  149. TEST_ASSERT_EQUAL(HCD_PORT_STATE_SUSPENDED, hcd_port_get_state(port_hdl));
  150. printf("Suspended\n");
  151. vTaskDelay(pdMS_TO_TICKS(100)); //Give some time for bus to remain suspended
  152. //Resume the port
  153. TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_RESUME));
  154. TEST_ASSERT_EQUAL(HCD_PORT_STATE_ENABLED, hcd_port_get_state(port_hdl));
  155. printf("Resumed\n");
  156. //Clear the default pipe's halt
  157. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_CLEAR));
  158. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_ACTIVE, hcd_pipe_get_state(default_pipe));
  159. vTaskDelay(pdMS_TO_TICKS(100)); //Give some time for resumed URBs to complete
  160. //Dequeue URBs
  161. for (int i = 0; i < NUM_URBS; i++) {
  162. urb_t *urb = hcd_urb_dequeue(default_pipe);
  163. TEST_ASSERT_EQUAL(urb_list[i], urb);
  164. TEST_ASSERT(urb->transfer.status == USB_TRANSFER_STATUS_COMPLETED || urb->transfer.status == USB_TRANSFER_STATUS_CANCELED);
  165. if (urb->transfer.status == USB_TRANSFER_STATUS_COMPLETED) {
  166. //We must have transmitted at least the setup packet, but device may return less than bytes requested
  167. TEST_ASSERT_GREATER_OR_EQUAL(sizeof(usb_setup_packet_t), urb->transfer.actual_num_bytes);
  168. TEST_ASSERT_LESS_OR_EQUAL(urb->transfer.num_bytes, urb->transfer.actual_num_bytes);
  169. } else {
  170. //A failed transfer should 0 actual number of bytes transmitted
  171. TEST_ASSERT_EQUAL(0, urb->transfer.actual_num_bytes);
  172. }
  173. TEST_ASSERT_EQUAL(0xDEADBEEF, urb->transfer.context);
  174. }
  175. //Free URB list and pipe
  176. for (int i = 0; i < NUM_URBS; i++) {
  177. test_hcd_free_urb(urb_list[i]);
  178. }
  179. test_hcd_pipe_free(default_pipe);
  180. //Cleanup
  181. test_hcd_wait_for_disconn(port_hdl, false);
  182. test_hcd_teardown(port_hdl);
  183. }
  184. /*
  185. Test HCD port disable and disconnection
  186. Purpose:
  187. - Test that the port disable command works correctly
  188. - Check that port can only be disabled when pipes have been halted
  189. - Check that a disconnection after port disable still triggers a HCD_PORT_EVENT_DISCONNECTION event
  190. Procedure:
  191. - Setup HCD, a default pipe, and multiple URBs
  192. - Start transfers
  193. - Halt the default pipe after a short delay
  194. - Check that port can be disabled
  195. - Flush the default pipe and cleanup the default pipe
  196. - Check that a disconnection still works after disable
  197. - Teardown
  198. */
  199. TEST_CASE("Test HCD port disable", "[hcd][ignore]")
  200. {
  201. hcd_port_handle_t port_hdl = test_hcd_setup(); //Setup the HCD and port
  202. usb_speed_t port_speed = test_hcd_wait_for_conn(port_hdl); //Trigger a connection
  203. vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
  204. //Allocate some URBs and initialize their data buffers with control transfers
  205. hcd_pipe_handle_t default_pipe = test_hcd_pipe_alloc(port_hdl, NULL, TEST_DEV_ADDR, port_speed); //Create a default pipe (using a NULL EP descriptor)
  206. urb_t *urb_list[NUM_URBS];
  207. for (int i = 0; i < NUM_URBS; i++) {
  208. urb_list[i] = test_hcd_alloc_urb(0, URB_DATA_BUFF_SIZE);
  209. //Initialize with a "Get Config Descriptor request"
  210. urb_list[i]->transfer.num_bytes = sizeof(usb_setup_packet_t) + TRANSFER_MAX_BYTES;
  211. USB_SETUP_PACKET_INIT_GET_CONFIG_DESC((usb_setup_packet_t *)urb_list[i]->transfer.data_buffer, 0, TRANSFER_MAX_BYTES);
  212. urb_list[i]->transfer.context = (void *)0xDEADBEEF;
  213. }
  214. //Enqueue URBs but immediately disable the port
  215. printf("Enqueuing URBs\n");
  216. for (int i = 0; i < NUM_URBS; i++) {
  217. TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(default_pipe, urb_list[i]));
  218. }
  219. //Add a short delay to let the transfers run for a bit
  220. esp_rom_delay_us(POST_ENQUEUE_DELAY_US);
  221. //Halt the default pipe before suspending
  222. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_ACTIVE, hcd_pipe_get_state(default_pipe));
  223. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_HALT));
  224. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_HALTED, hcd_pipe_get_state(default_pipe));
  225. //Check that port can be disabled
  226. TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_DISABLE));
  227. TEST_ASSERT_EQUAL(HCD_PORT_STATE_DISABLED, hcd_port_get_state(port_hdl));
  228. printf("Disabled\n");
  229. //Flush pipe
  230. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_FLUSH));
  231. //Dequeue URBs
  232. for (int i = 0; i < NUM_URBS; i++) {
  233. urb_t *urb = hcd_urb_dequeue(default_pipe);
  234. TEST_ASSERT_EQUAL(urb_list[i], urb);
  235. TEST_ASSERT(urb->transfer.status == USB_TRANSFER_STATUS_COMPLETED || urb->transfer.status == USB_TRANSFER_STATUS_CANCELED);
  236. if (urb->transfer.status == USB_TRANSFER_STATUS_COMPLETED) {
  237. //We must have transmitted at least the setup packet, but device may return less than bytes requested
  238. TEST_ASSERT_GREATER_OR_EQUAL(sizeof(usb_setup_packet_t), urb->transfer.actual_num_bytes);
  239. TEST_ASSERT_LESS_OR_EQUAL(urb->transfer.num_bytes, urb->transfer.actual_num_bytes);
  240. } else {
  241. //A failed transfer should 0 actual number of bytes transmitted
  242. TEST_ASSERT_EQUAL(0, urb->transfer.actual_num_bytes);
  243. }
  244. TEST_ASSERT_EQUAL(0xDEADBEEF, urb->transfer.context);
  245. }
  246. //Free URB list and pipe
  247. for (int i = 0; i < NUM_URBS; i++) {
  248. test_hcd_free_urb(urb_list[i]);
  249. }
  250. test_hcd_pipe_free(default_pipe);
  251. //Trigger a disconnection and cleanup
  252. test_hcd_wait_for_disconn(port_hdl, true);
  253. test_hcd_teardown(port_hdl);
  254. }
  255. /*
  256. Test HCD port command bailout
  257. Purpose:
  258. - Test that if the a port's state changes whilst a command is being executed, the port command should return
  259. ESP_ERR_INVALID_RESPONSE
  260. Procedure:
  261. - Setup HCD and wait for connection
  262. - Suspend the port
  263. - Resume the port but trigger a disconnect from another thread during the resume command
  264. - Check that port command returns ESP_ERR_INVALID_RESPONSE
  265. */
  266. static void concurrent_task(void *arg)
  267. {
  268. SemaphoreHandle_t sync_sem = (SemaphoreHandle_t) arg;
  269. xSemaphoreTake(sync_sem, portMAX_DELAY);
  270. vTaskDelay(pdMS_TO_TICKS(10)); //Give a short delay let reset command start in main thread
  271. //Force a disconnection
  272. test_hcd_force_conn_state(false, 0);
  273. vTaskDelay(portMAX_DELAY); //Block forever and wait to be deleted
  274. }
  275. TEST_CASE("Test HCD port command bailout", "[hcd][ignore]")
  276. {
  277. hcd_port_handle_t port_hdl = test_hcd_setup(); //Setup the HCD and port
  278. test_hcd_wait_for_conn(port_hdl); //Trigger a connection
  279. vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
  280. //Create task to run port commands concurrently
  281. SemaphoreHandle_t sync_sem = xSemaphoreCreateBinary();
  282. TaskHandle_t task_handle;
  283. TEST_ASSERT_NOT_EQUAL(NULL, sync_sem);
  284. TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(concurrent_task, "tsk", 4096, (void *) sync_sem, UNITY_FREERTOS_PRIORITY + 1, &task_handle, 0));
  285. //Suspend the device
  286. printf("Suspending\n");
  287. TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_SUSPEND));
  288. vTaskDelay(pdMS_TO_TICKS(20)); //Short delay for device to enter suspend state
  289. //Attempt to resume the port. But the concurrent task should override this with a disconnection event
  290. printf("Attempting to resume\n");
  291. xSemaphoreGive(sync_sem); //Trigger concurrent task
  292. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_RESPONSE, hcd_port_command(port_hdl, HCD_PORT_CMD_RESUME));
  293. //Check that concurrent task triggered a sudden disconnection
  294. test_hcd_expect_port_event(port_hdl, HCD_PORT_EVENT_DISCONNECTION);
  295. TEST_ASSERT_EQUAL(HCD_PORT_EVENT_DISCONNECTION, hcd_port_handle_event(port_hdl));
  296. TEST_ASSERT_EQUAL(HCD_PORT_STATE_RECOVERY, hcd_port_get_state(port_hdl));
  297. //Cleanup task and semaphore
  298. vTaskDelay(pdMS_TO_TICKS(10)); //Short delay for concurrent task finish running
  299. vTaskDelete(task_handle);
  300. vSemaphoreDelete(sync_sem);
  301. test_hcd_teardown(port_hdl);
  302. }