usb.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #pragma once
  2. #include <common/sys/types.h>
  3. // usb设备在pci总线上的class
  4. #define USB_CLASS 0xC
  5. #define USB_SUBCLASS 0x3
  6. // 不同的usb设备在pci总线上的prog IF
  7. #define USB_TYPE_UHCI 0x0
  8. #define USB_TYPE_OHCI 0x10
  9. #define USB_TYPE_EHCI 0x20
  10. #define USB_TYPE_XHCI 0x30
  11. #define USB_TYPE_UNSPEC 0x80 // Unspecified
  12. #define USB_TYPE_DEVICE 0xfe // USB Device(Not controller)
  13. // Reset wait times(milliseconds) ,USB 2.0 specs, page 153, section 7.1.7.5, paragraph 3
  14. #define USB_TIME_RST_RH 50 // reset on a root hub
  15. #define USB_TIME_RST_MIN 10 // minimum delay for a reset
  16. #define USB_TIME_RST_NOMORE 3 // No more than this between resets for root hubs
  17. #define USB_TIME_RST_REC 10 // reset recovery
  18. /**
  19. * @brief usb描述符的头部
  20. *
  21. * String Descriptor:
  22. * String Language Descriptor:
  23. * 先获取头部,然后根据长度申请空间,再获取整个string desc
  24. */
  25. struct usb_desc_header
  26. {
  27. uint8_t len; // 整个描述符的大小(字节)
  28. uint8_t type;
  29. } __attribute__((packed));
  30. /**
  31. * @brief usb 设备描述符
  32. *
  33. */
  34. struct usb_device_desc
  35. {
  36. uint8_t len;
  37. uint8_t type;
  38. uint16_t usb_version;
  39. uint8_t _class;
  40. uint8_t subclass;
  41. uint8_t protocol;
  42. uint8_t max_packet_size;
  43. uint16_t vendor_id;
  44. uint16_t product_id;
  45. uint16_t device_rel;
  46. uint8_t manufacturer_index;
  47. uint8_t procuct_index;
  48. uint8_t serial_index;
  49. uint8_t config; // number of configurations
  50. } __attribute__((packed));
  51. /**
  52. * @brief usb设备配置信息描述符
  53. *
  54. */
  55. struct usb_config_desc
  56. {
  57. uint8_t len; // 当前描述符的大小(字节)
  58. uint8_t type; // USB_DT_CONFIG
  59. uint16_t total_len; /*
  60. Total length of data returned for this
  61. configuration. Includes the combined length
  62. of all descriptors (configuration, interface,
  63. endpoint, and class- or vendor-specific)
  64. returned for this configuration
  65. */
  66. uint8_t num_interfaces; // 当前conf对应的接口数量
  67. uint8_t value; /*
  68. Value to use as an argument to the
  69. SetConfiguration() request to select this
  70. configuration
  71. */
  72. uint8_t index; // Index of string descriptor describing this configuration
  73. uint8_t bmAttr; /*
  74. Configuration characteristics:
  75. D7: Reserved (要设置为1)
  76. D6: Self-powered
  77. D5: Remote Wakeup
  78. D4...0: Reserved (设置为0)
  79. */
  80. uint8_t max_power; /*
  81. 当这个设备满载时,为在这个conf上提供对应的功能,需要消耗的电流值。
  82. 当设备是在High-speed时,这里的单位是2mA (也就是说,值为50,代表最大消耗100mA的电流)
  83. 当设备运行在Gen X speed时,这里的单位是8mA
  84. */
  85. } __attribute__((packed));
  86. /**
  87. * @brief usb接口描述符
  88. *
  89. */
  90. struct usb_interface_desc
  91. {
  92. uint8_t len;
  93. uint8_t type; // USB_DT_INTERFACE
  94. uint8_t interface_number; // 当前接口序号(从0开始的)
  95. uint8_t alternate_setting; // used to select alt. setting
  96. uint8_t num_endpoints; // 当前interface的端点数量
  97. uint8_t interface_class; // Class code
  98. uint8_t interface_sub_class; // Sub class code
  99. uint8_t interface_protocol; // 协议 These codes are qualified by the value of thebInterfaceClass and the
  100. // bInterfaceSubClass fields.
  101. uint8_t index; // index of String Descriptor describing this interface
  102. } __attribute__((packed));
  103. /**
  104. * @brief usb端点描述符
  105. *
  106. * 详见usb3.2 Specification Table 9-26
  107. */
  108. struct usb_endpoint_desc
  109. {
  110. uint8_t len;
  111. uint8_t type; // descriptor type
  112. uint8_t endpoint_addr; /* Bit 3...0: The endpoint number
  113. Bit 6...4: Reserved, reset to zero
  114. Bit 7: Direction, ignored for
  115. control endpoints
  116. 0 = OUT endpoint
  117. 1 = IN endpoint
  118. */
  119. uint8_t attributes;
  120. uint16_t max_packet;
  121. uint8_t interval;
  122. };
  123. // 从endpoint描述符中获取max burst size大小
  124. #define usb_get_max_burst_from_ep(__ep_desc) (((__ep_desc)->max_packet & 0x1800) >> 11)
  125. /**
  126. * @brief usb设备请求包
  127. *
  128. */
  129. struct usb_request_packet_t
  130. {
  131. uint8_t request_type;
  132. uint8_t request;
  133. uint16_t value;
  134. uint16_t index;
  135. uint16_t length;
  136. } __attribute__((packed));
  137. // usb设备请求包的request_type字段的值
  138. #define __USB_REQ_TYPE_H2D 0x00
  139. #define __USB_REQ_TYPE_D2H 0x80
  140. #define __USB_REQ_TYPE_STANDARD 0x00
  141. #define __USB_REQ_TYPE_CLASS 0x20
  142. #define __USB_REQ_TYPE_VENDOR 0x40
  143. #define __USB_REQ_TYPE_RSVD 0x60
  144. #define __USB_REQ_TYPE_DEVICE 0x00
  145. #define __USB_REQ_TYPE_INTERFACE 0x01
  146. #define __USB_REQ_TYPE_ENDPOINT 0x02
  147. #define __USB_REQ_TYPE_OTHER 0x03
  148. #define USB_REQ_TYPE_GET_REQUEST (__USB_REQ_TYPE_D2H | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_DEVICE)
  149. #define USB_REQ_TYPE_SET_REQUEST (__USB_REQ_TYPE_H2D | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_DEVICE)
  150. #define USB_REQ_TYPE_GET_INTERFACE_REQUEST (__USB_REQ_TYPE_D2H | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_INTERFACE)
  151. #define USB_REQ_TYPE_SET_INTERFACE (__USB_REQ_TYPE_H2D | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_INTERFACE)
  152. #define USB_REQ_TYPE_SET_CLASS_INTERFACE (__USB_REQ_TYPE_H2D | __USB_REQ_TYPE_CLASS | __USB_REQ_TYPE_INTERFACE)
  153. // device requests
  154. enum
  155. {
  156. USB_REQ_GET_STATUS = 0,
  157. USB_REQ_CLEAR_FEATURE,
  158. USB_REQ_SET_FEATURE = 3,
  159. USB_REQ_SET_ADDRESS = 5,
  160. USB_REQ_GET_DESCRIPTOR = 6,
  161. USB_REQ_SET_DESCRIPTOR,
  162. USB_REQ_GET_CONFIGURATION,
  163. USB_REQ_SET_CONFIGURATION,
  164. // interface requests
  165. USB_REQ_GET_INTERFACE,
  166. USB_REQ_SET_INTERFACE,
  167. // standard endpoint requests
  168. USB_REQ_SYNCH_FRAME,
  169. USB_REQ_SET_ENCRYPTION,
  170. USB_REQ_GET_ENCRYPTION,
  171. USB_REQ_SET_HANDSHAKE,
  172. USB_REQ_GET_HANDSHAKE,
  173. USB_REQ_SET_CONNECTION,
  174. USB_REQ_SET_SECURITY_DATA,
  175. USB_REQ_GET_SECURITY_DATA,
  176. USB_REQ_SET_WUSB_DATA,
  177. USB_REQ_LOOPBACK_DATA_WRITE,
  178. USB_REQ_LOOPBACK_DATA_READ,
  179. USB_REQ_SET_INTERFACE_DS,
  180. USB_REQ_GET_FW_STATUS = 26,
  181. USB_REQ_SET_FW_STATUS,
  182. USB_REQ_SET_SEL = 48,
  183. USB_REQ_SET_ISOCH_DELAY,
  184. // Device specific
  185. USB_REQ_GET_MAX_LUNS = 0xFE,
  186. USB_REQ_BULK_ONLY_RESET
  187. };
  188. // Descriptor types
  189. enum
  190. {
  191. USB_DT_DEVICE = 1,
  192. USB_DT_CONFIG,
  193. USB_DT_STRING,
  194. USB_DT_INTERFACE,
  195. USB_DT_ENDPOINT,
  196. USB_DT_DEVICE_QUALIFIER,
  197. USB_DT_OTHER_SPEED_CONFIG,
  198. USB_DT_INTERFACE_POWER,
  199. USB_DT_OTG,
  200. USB_DT_DEBUG,
  201. USB_DT_INTERFACE_ASSOSIATION,
  202. USB_DT_BOS = 15,
  203. USB_DT_DEVICE_CAPABILITY,
  204. USB_DT_HID = 0x21,
  205. USB_DT_HID_REPORT,
  206. USB_DT_HID_PHYSICAL,
  207. USB_DT_INTERFACE_FUNCTION = 0x24,
  208. USB_DT_ENDPOINT_FUNCTION,
  209. // HUB = 0x29
  210. USB_DT_SUPERSPEED_USB_ENDPOINT_COMPANION = 48,
  211. USB_DT_SUPERSPEEDPLUS_ISOCHRONOUS_ENDPOINT_COMPANION,
  212. };
  213. // transfer types (Endpoint types) (USB 2.0 page 270)
  214. enum
  215. {
  216. USB_EP_CONTROL = 0,
  217. USB_EP_ISOCHRONOUS,
  218. USB_EP_BULK,
  219. USB_EP_INTERRUPT
  220. };
  221. /**
  222. * @brief 该宏定义用于声明usb请求包,并初始化其中的各个字段
  223. *
  224. */
  225. #define DECLARE_USB_PACKET(pak_name, _trans_req_type, _trans_request, _trans_value, _trans_index, _transfer_length) \
  226. struct usb_request_packet_t pak_name = {0}; \
  227. pak_name.request_type = (_trans_req_type); \
  228. pak_name.request = (_trans_request); \
  229. pak_name.value = (_trans_value); \
  230. pak_name.index = (_trans_index); \
  231. pak_name.length = (_transfer_length);
  232. /*
  233. usb class codes
  234. refs: https://www.usb.org/defined-class-codes
  235. */
  236. enum
  237. {
  238. USB_CLASS_IF = 0x00,
  239. USB_CLASS_AUDIO,
  240. USB_CLASS_CDC,
  241. USB_CLASS_HID,
  242. USB_CLASS_PHYSICAL = 0x05,
  243. USB_CLASS_IMAGE,
  244. USB_CLASS_PRINTER,
  245. USB_CLASS_MASS_STORAGE,
  246. USB_CLASS_HUB,
  247. USB_CLASS_CDC_DATA,
  248. USB_CLASS_SMART_CARD,
  249. USB_CLASS_CONTENT_SEC = 0x0d,
  250. USB_CLASS_VIDEO,
  251. USB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
  252. USB_CLASS_AV,
  253. USB_CLASS_BILLBOARD,
  254. USB_CLASS_TYPEC_BRIDGE,
  255. USB_CLASS_I3C = 0x3c,
  256. USB_CLASS_DIAGNOSTIC = 0xdc,
  257. USB_CLASS_WIRELESS_CTRL = 0xe0,
  258. USB_CLASS_MISC = 0xef,
  259. USB_CLASS_APP_SPEC = 0xfe,
  260. USB_CLASS_VENDOR_SPEC = 0XFF,
  261. };
  262. /**
  263. * @brief usb hid descriptor的结构体
  264. *
  265. */
  266. struct usb_hid_desc
  267. {
  268. uint8_t len;
  269. uint8_t type; // USB_DT_HID
  270. uint16_t bcdHID; // 标识HIDClass规范版本的数字表达式。
  271. uint8_t country_code;
  272. uint8_t descriptors_num; // the number of class descriptors
  273. uint8_t desc_type; // Constant name identifying type of class descriptor
  274. uint16_t report_desc_len; // Report descriptor的大小
  275. };
  276. /**
  277. * @brief 初始化usb驱动程序
  278. *
  279. */
  280. int usb_init();