test_usb_mock_classes.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. This header contains bare-bone mock implementations of some device classes in order to test various layers of the USB
  8. Host stack.
  9. */
  10. #pragma once
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include "usb/usb_types_ch9.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. // ---------------------------------------------------- MSC SCSI -------------------------------------------------------
  18. extern const char *MSC_CLIENT_TAG;
  19. /*
  20. Note: The mock MSC SCSI tests requires that USB flash drive be connected. The flash drive should...
  21. - Be implement the Mass Storage class supporting BULK only transfers using SCSI commands
  22. - It's configuration 1 should have the following endpoints
  23. Device Descriptor:
  24. bLength 18
  25. bDescriptorType 1
  26. bcdUSB 2.00
  27. bDeviceClass 0
  28. bDeviceSubClass 0
  29. bDeviceProtocol 0
  30. bMaxPacketSize0 64
  31. idVendor 0x125f
  32. idProduct 0xc08a
  33. bcdDevice 1.00
  34. iManufacturer 1
  35. iProduct 2
  36. iSerial 3
  37. bNumConfigurations 1
  38. Configuration Descriptor:
  39. bLength 9
  40. bDescriptorType 2
  41. wTotalLength 0x0020
  42. bNumInterfaces 1
  43. bConfigurationValue 1
  44. iConfiguration 0
  45. bmAttributes 0x80
  46. (Bus Powered)
  47. MaxPower 480mA
  48. Interface Descriptor:
  49. bLength 9
  50. bDescriptorType 4
  51. bInterfaceNumber 0
  52. bAlternateSetting 0
  53. bNumEndpoints 2
  54. bInterfaceClass 8 Mass Storage
  55. bInterfaceSubClass 6 SCSI
  56. bInterfaceProtocol 80 Bulk-Only
  57. iInterface 0
  58. Endpoint Descriptor:
  59. bLength 7
  60. bDescriptorType 5
  61. bEndpointAddress 0x01 EP 1 OUT
  62. bmAttributes 2
  63. Transfer Type Bulk
  64. Synch Type None
  65. Usage Type Data
  66. wMaxPacketSize 0x0040 1x 64 bytes
  67. bInterval 1
  68. Endpoint Descriptor:
  69. bLength 7
  70. bDescriptorType 5
  71. bEndpointAddress 0x82 EP 2 IN
  72. bmAttributes 2
  73. Transfer Type Bulk
  74. Synch Type None
  75. Usage Type Data
  76. wMaxPacketSize 0x0040 1x 64 bytes
  77. bInterval 1
  78. If you're using a flash driver with different endpoints, modify the endpoint descriptors below.
  79. */
  80. //Constant descriptors
  81. extern const uint8_t mock_msc_scsi_dev_desc[];
  82. extern const uint8_t mock_msc_scsi_config_desc[];
  83. extern const uint8_t mock_msc_scsi_str_desc_manu[];
  84. extern const uint8_t mock_msc_scsi_str_desc_prod[];
  85. extern const uint8_t mock_msc_scsi_str_desc_ser_num[];
  86. extern const usb_ep_desc_t mock_msc_scsi_bulk_out_ep_desc;
  87. extern const usb_ep_desc_t mock_msc_scsi_bulk_in_ep_desc;
  88. #define MOCK_MSC_SCSI_DEV_ID_VENDOR 0x125F
  89. #define MOCK_MSC_SCSI_DEV_ID_PRODUCT 0xc08A
  90. #define MOCK_MSC_SCSI_DEV_DFLT_EP_MPS 64
  91. #define MOCK_MSC_SCSI_SECTOR_SIZE 512
  92. #define MOCK_MSC_SCSI_LUN 0
  93. #define MOCK_MSC_SCSI_INTF_NUMBER 0
  94. #define MOCK_MSC_SCSI_INTF_ALT_SETTING 0
  95. #define MOCK_MSC_SCSI_BULK_OUT_EP_ADDR 0x01
  96. #define MOCK_MSC_SCSI_BULK_IN_EP_ADDR 0x82
  97. #define MOCK_MSC_SCSI_BULK_EP_MPS 64
  98. #define MOCK_MSC_SCSI_REQ_INIT_RESET(setup_pkt_ptr, intf_num) ({ \
  99. (setup_pkt_ptr)->bmRequestType = USB_BM_REQUEST_TYPE_DIR_OUT | USB_BM_REQUEST_TYPE_TYPE_CLASS | USB_BM_REQUEST_TYPE_RECIP_INTERFACE; \
  100. (setup_pkt_ptr)->bRequest = 0xFF; \
  101. (setup_pkt_ptr)->wValue = 0; \
  102. (setup_pkt_ptr)->wIndex = (intf_num); \
  103. (setup_pkt_ptr)->wLength = 0; \
  104. })
  105. typedef struct __attribute__((packed)) {
  106. uint8_t opcode; //0x28 = read(10), 0x2A=write(10)
  107. uint8_t flags;
  108. uint8_t lba_3;
  109. uint8_t lba_2;
  110. uint8_t lba_1;
  111. uint8_t lba_0;
  112. uint8_t group;
  113. uint8_t len_1;
  114. uint8_t len_0;
  115. uint8_t control;
  116. } mock_scsi_cmd10_t;
  117. typedef struct __attribute__((packed)) {
  118. uint32_t dCBWSignature;
  119. uint32_t dCBWTag;
  120. uint32_t dCBWDataTransferLength;
  121. uint8_t bmCBWFlags;
  122. uint8_t bCBWLUN;
  123. uint8_t bCBWCBLength;
  124. mock_scsi_cmd10_t CBWCB;
  125. uint8_t padding[6];
  126. } mock_msc_bulk_cbw_t;
  127. // USB Bulk Transfer Command Status Wrapper data
  128. typedef struct __attribute__((packed)) {
  129. uint32_t dCSWSignature;
  130. uint32_t dCSWTag;
  131. uint32_t dCSWDataResidue;
  132. uint8_t bCSWStatus;
  133. } mock_msc_bulk_csw_t;
  134. /**
  135. * @brief Initialize a MSC Command Block Wrapper (CBW) as an SCSI command
  136. *
  137. * @param cbw CBW structure
  138. * @param is_read Is a read command
  139. * @param offset Block offset
  140. * @param num_sectors Number of sectors to read
  141. * @param tag Tag (this is simply echoed back
  142. */
  143. void mock_msc_scsi_init_cbw(mock_msc_bulk_cbw_t *cbw, bool is_read, int offset, int num_sectors, uint32_t tag);
  144. /**
  145. * @brief Check that returned Command Status Wrapper (CSW) is valid
  146. *
  147. * @param csw CSW structure
  148. * @param tag_expect Expected tag
  149. * @return true CSW is valid
  150. * @return false CSW is not valid
  151. */
  152. bool mock_msc_scsi_check_csw(mock_msc_bulk_csw_t *csw, uint32_t tag_expect);
  153. // ---------------------------------------------------- HID Mouse ------------------------------------------------------
  154. /*
  155. Note: The mock HID mouse tests require that USB low speed mouse be connected. The mouse should...
  156. - Be implement the HID with standard report format used by mice
  157. - It's configuration 1 should have the following endpoint
  158. Device Descriptor:
  159. bLength 18
  160. bDescriptorType 1
  161. bcdUSB 2.00
  162. bDeviceClass 0
  163. bDeviceSubClass 0
  164. bDeviceProtocol 0
  165. bMaxPacketSize0 8
  166. idVendor 0x413c Dell Computer Corp.
  167. idProduct 0x301a
  168. bcdDevice 1.00
  169. iManufacturer 1
  170. iProduct 2
  171. iSerial 0
  172. bNumConfigurations 1
  173. Configuration Descriptor:
  174. bLength 9
  175. bDescriptorType 2
  176. wTotalLength 0x0022
  177. bNumInterfaces 1
  178. bConfigurationValue 1
  179. iConfiguration 0
  180. bmAttributes 0xa0
  181. (Bus Powered)
  182. Remote Wakeup
  183. MaxPower 100mA
  184. Interface Descriptor:
  185. bLength 9
  186. bDescriptorType 4
  187. bInterfaceNumber 0
  188. bAlternateSetting 0
  189. bNumEndpoints 1
  190. bInterfaceClass 3 Human Interface Device
  191. bInterfaceSubClass 1 Boot Interface Subclass
  192. bInterfaceProtocol 2 Mouse
  193. iInterface 0
  194. HID Device Descriptor:
  195. bLength 9
  196. bDescriptorType 33
  197. bcdHID 1.11
  198. bCountryCode 0 Not supported
  199. bNumDescriptors 1
  200. bDescriptorType 34 Report
  201. wDescriptorLength 46
  202. Report Descriptors:
  203. ** UNAVAILABLE **
  204. Endpoint Descriptor:
  205. bLength 7
  206. bDescriptorType 5
  207. bEndpointAddress 0x81 EP 1 IN
  208. bmAttributes 3
  209. Transfer Type Interrupt
  210. Synch Type None
  211. Usage Type Data
  212. wMaxPacketSize 0x0004 1x 4 bytes
  213. bInterval 10
  214. If you're using another mice with different endpoints, modify the endpoint descriptor below
  215. */
  216. extern const usb_ep_desc_t mock_hid_mouse_in_ep_desc;
  217. #define MOCK_HID_MOUSE_DEV_ID_VENDOR 0x413C
  218. #define MOCK_HID_MOUSE_DEV_ID_PRODUCT 0x301A
  219. #define MOCK_HID_MOUSE_DEV_DFLT_EP_MPS 8
  220. #define MOCK_HID_MOUSE_INTF_NUMBER 0
  221. #define MOCK_HID_MOUSE_INTF_ALT_SETTING 0
  222. #define MOCK_HID_MOUSE_INTR_IN_EP_ADDR 0x81
  223. #define MOCK_HID_MOUSE_INTR_IN_MPS 0x04
  224. typedef union {
  225. struct {
  226. uint32_t left_button: 1;
  227. uint32_t right_button: 1;
  228. uint32_t middle_button: 1;
  229. uint32_t reserved5: 5;
  230. uint8_t x_movement;
  231. uint8_t y_movement;
  232. } __attribute__((packed));
  233. uint8_t val[3];
  234. } mock_hid_mouse_report_t;
  235. _Static_assert(sizeof(mock_hid_mouse_report_t) == 3, "Size of HID mouse report incorrect");
  236. void mock_hid_process_report(mock_hid_mouse_report_t *report, int iter);
  237. // ---------------------------------------------------- Mock ISOC ------------------------------------------------------
  238. /*
  239. Note: ISOC test rely on communicating with a non existent endpoint using ISOC OUT transfers. Since no ACK is given for
  240. ISOC, transferring to a non-existent endpoint should work. The non-existent endpoint descriptor is described below:
  241. */
  242. #define MOCK_ISOC_EP_NUM 2
  243. #define MOCK_ISOC_EP_MPS 512
  244. static const usb_ep_desc_t mock_isoc_out_ep_desc = {
  245. .bLength = sizeof(usb_ep_desc_t),
  246. .bDescriptorType = USB_B_DESCRIPTOR_TYPE_ENDPOINT,
  247. .bEndpointAddress = MOCK_ISOC_EP_NUM,
  248. .bmAttributes = USB_BM_ATTRIBUTES_XFER_ISOC,
  249. .wMaxPacketSize = MOCK_ISOC_EP_MPS, //MPS of 512 bytes
  250. .bInterval = 1, //Isoc interval is (2 ^ (bInterval - 1)) which means an interval of 1ms
  251. };
  252. #ifdef __cplusplus
  253. }
  254. #endif