usbh_core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USBH_CORE_H
  7. #define USBH_CORE_H
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include "usb_config.h"
  13. #include "usb_util.h"
  14. #include "usb_errno.h"
  15. #include "usb_def.h"
  16. #include "usb_list.h"
  17. #include "usb_log.h"
  18. #include "usb_hc.h"
  19. #include "usb_osal.h"
  20. #include "usbh_hub.h"
  21. #include "usb_memcpy.h"
  22. #include "usb_ringbuffer.h"
  23. #include "usb_mempool.h"
  24. #include "usb_dcache.h"
  25. #include "usb_version.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. enum usbh_event_type {
  30. /* USB HCD IRQ */
  31. USBH_EVENT_ERROR,
  32. USBH_EVENT_SOF,
  33. /* USB DEVICE STATUS */
  34. USBH_EVENT_DEVICE_RESET,
  35. USBH_EVENT_DEVICE_CONNECTED,
  36. USBH_EVENT_DEVICE_DISCONNECTED,
  37. USBH_EVENT_DEVICE_CONFIGURED,
  38. USBH_EVENT_DEVICE_WAKEUP,
  39. USBH_EVENT_DEVICE_SUSPEND,
  40. USBH_EVENT_DEVICE_RESUME,
  41. /* USB DEVICE INTERFACE STATUS */
  42. USBH_EVENT_INTERFACE_UNSUPPORTED,
  43. USBH_EVENT_INTERFACE_START,
  44. USBH_EVENT_INTERFACE_STOP,
  45. /* USB FRAMEWORK STATUS */
  46. USBH_EVENT_INIT,
  47. USBH_EVENT_DEINIT,
  48. USBH_EVENT_UNKNOWN,
  49. };
  50. #define USB_HUB_PORT_ANY 0
  51. #define USB_HUB_INDEX_ANY 0
  52. #define USB_INTERFACE_ANY 0xff
  53. #define USB_CLASS_MATCH_VENDOR 0x0001
  54. #define USB_CLASS_MATCH_PRODUCT 0x0002
  55. #define USB_CLASS_MATCH_INTF_CLASS 0x0004
  56. #define USB_CLASS_MATCH_INTF_SUBCLASS 0x0008
  57. #define USB_CLASS_MATCH_INTF_PROTOCOL 0x0010
  58. #define USB_CLASS_MATCH_INTF_NUM 0x0020
  59. #define USB_CLASS_MATCH_VID_PID (USB_CLASS_MATCH_VENDOR | USB_CLASS_MATCH_PRODUCT)
  60. #define CLASS_CONNECT(hport, i) ((hport)->config.intf[i].class_driver->connect(hport, i))
  61. #define CLASS_DISCONNECT(hport, i) ((hport)->config.intf[i].class_driver->disconnect(hport, i))
  62. #ifdef __ARMCC_VERSION /* ARM C Compiler */
  63. #define CLASS_INFO_DEFINE __attribute__((section("usbh_class_info"))) __USED __ALIGNED(1)
  64. #elif defined(__GNUC__)
  65. #define CLASS_INFO_DEFINE __attribute__((section(".usbh_class_info"))) __USED __ALIGNED(1)
  66. #elif defined(__ICCARM__) || defined(__ICCRX__) || defined(__ICCRISCV__)
  67. #pragma section = ".usbh_class_info"
  68. #define CLASS_INFO_DEFINE __attribute__((section(".usbh_class_info"))) __USED __ALIGNED(1)
  69. #endif
  70. #define USBH_GET_URB_INTERVAL(interval, speed) (speed < USB_SPEED_HIGH ? (interval * 1000) : ((1 << (interval - 1)) * 125))
  71. #define USBH_EP_INIT(ep, ep_desc) \
  72. do { \
  73. ep = ep_desc; \
  74. USB_LOG_INFO("Ep=%02x Attr=%02u Mps=%d Interval=%02u Mult=%02u\r\n", \
  75. ep_desc->bEndpointAddress, \
  76. ep_desc->bmAttributes, \
  77. USB_GET_MAXPACKETSIZE(ep_desc->wMaxPacketSize), \
  78. ep_desc->bInterval, \
  79. USB_GET_MULT(ep_desc->wMaxPacketSize)); \
  80. } while (0)
  81. typedef void (*usbh_event_handler_t)(uint8_t busid, uint8_t hub_index, uint8_t hub_port, uint8_t intf, uint8_t event);
  82. struct usbh_class_info {
  83. uint8_t match_flags; /* Used for product specific matches; range is inclusive */
  84. uint8_t bInterfaceClass; /* Base device class code */
  85. uint8_t bInterfaceSubClass; /* Sub-class, depends on base class. Eg. */
  86. uint8_t bInterfaceProtocol; /* Protocol, depends on base class. Eg. */
  87. uint8_t bInterfaceNumber; /* Interface number */
  88. const uint16_t (*id_table)[2]; /* List of Vendor/Product ID pairs */
  89. const struct usbh_class_driver *class_driver;
  90. };
  91. struct usbh_hubport;
  92. struct usbh_class_driver {
  93. const char *driver_name;
  94. int (*connect)(struct usbh_hubport *hport, uint8_t intf);
  95. int (*disconnect)(struct usbh_hubport *hport, uint8_t intf);
  96. };
  97. struct usbh_endpoint {
  98. struct usb_endpoint_descriptor ep_desc;
  99. };
  100. struct usbh_interface_altsetting {
  101. struct usb_interface_descriptor intf_desc;
  102. struct usbh_endpoint ep[CONFIG_USBHOST_MAX_ENDPOINTS];
  103. };
  104. struct usbh_interface {
  105. char devname[CONFIG_USBHOST_DEV_NAMELEN];
  106. struct usbh_class_driver *class_driver;
  107. void *priv;
  108. struct usbh_interface_altsetting altsetting[CONFIG_USBHOST_MAX_INTF_ALTSETTINGS];
  109. uint8_t altsetting_num;
  110. };
  111. struct usbh_configuration {
  112. struct usb_configuration_descriptor config_desc;
  113. struct usbh_interface intf[CONFIG_USBHOST_MAX_INTERFACES];
  114. };
  115. struct usbh_hubport {
  116. bool connected; /* True: device connected; false: disconnected */
  117. uint8_t port; /* Hub port index */
  118. uint8_t dev_addr; /* device address */
  119. uint8_t speed; /* device speed */
  120. uint8_t depth; /* distance from root hub */
  121. uint8_t route; /* route string */
  122. uint8_t slot_id; /* slot id */
  123. struct usb_device_descriptor device_desc;
  124. struct usbh_configuration config;
  125. const char *iManufacturer;
  126. const char *iProduct;
  127. const char *iSerialNumber;
  128. uint8_t *raw_config_desc;
  129. struct usb_setup_packet *setup;
  130. struct usbh_hub *parent;
  131. struct usbh_hub *self; /* if this hubport is a hub */
  132. struct usbh_bus *bus;
  133. struct usb_endpoint_descriptor ep0;
  134. struct usbh_urb ep0_urb;
  135. usb_osal_mutex_t mutex;
  136. };
  137. struct usbh_hub {
  138. bool connected;
  139. bool is_roothub;
  140. uint8_t index;
  141. uint8_t hub_addr;
  142. uint8_t speed;
  143. uint8_t nports;
  144. uint8_t powerdelay;
  145. uint8_t tt_think;
  146. bool ismtt;
  147. struct usb_hub_descriptor hub_desc; /* USB 2.0 only */
  148. struct usb_hub_ss_descriptor hub_ss_desc; /* USB 3.0 only */
  149. struct usbh_hubport child[CONFIG_USBHOST_MAX_EHPORTS];
  150. struct usbh_hubport *parent;
  151. struct usbh_bus *bus;
  152. struct usb_endpoint_descriptor *intin;
  153. struct usbh_urb intin_urb;
  154. uint8_t *int_buffer;
  155. struct usb_osal_timer *int_timer;
  156. };
  157. struct usbh_devaddr_map {
  158. /**
  159. * alloctab[0]:addr from 0~31
  160. * alloctab[1]:addr from 32~63
  161. * alloctab[2]:addr from 64~95
  162. * alloctab[3]:addr from 96~127
  163. *
  164. */
  165. uint8_t last; /* Last device address */
  166. uint32_t alloctab[4]; /* Bit allocation table */
  167. };
  168. struct usbh_hcd {
  169. uintptr_t reg_base;
  170. uint8_t hcd_id;
  171. uint8_t roothub_intbuf[2]; /* at most 15 roothub ports */
  172. struct usbh_hub roothub;
  173. };
  174. struct usbh_bus {
  175. usb_slist_t list;
  176. uint8_t busid;
  177. struct usbh_hcd hcd;
  178. struct usbh_devaddr_map devgen;
  179. usb_osal_thread_t hub_thread;
  180. usb_osal_mq_t hub_mq;
  181. usb_osal_mutex_t mutex;
  182. usbh_event_handler_t event_handler;
  183. };
  184. static inline void usbh_control_urb_fill(struct usbh_urb *urb,
  185. struct usbh_hubport *hport,
  186. struct usb_setup_packet *setup,
  187. uint8_t *transfer_buffer,
  188. uint32_t transfer_buffer_length,
  189. uint32_t timeout,
  190. usbh_complete_callback_t complete,
  191. void *arg)
  192. {
  193. urb->hport = hport;
  194. urb->ep = &hport->ep0;
  195. urb->setup = setup;
  196. urb->transfer_buffer = transfer_buffer;
  197. urb->transfer_buffer_length = transfer_buffer_length;
  198. urb->timeout = timeout;
  199. urb->complete = complete;
  200. urb->arg = arg;
  201. }
  202. static inline void usbh_bulk_urb_fill(struct usbh_urb *urb,
  203. struct usbh_hubport *hport,
  204. struct usb_endpoint_descriptor *ep,
  205. uint8_t *transfer_buffer,
  206. uint32_t transfer_buffer_length,
  207. uint32_t timeout,
  208. usbh_complete_callback_t complete,
  209. void *arg)
  210. {
  211. urb->hport = hport;
  212. urb->ep = ep;
  213. urb->setup = NULL;
  214. urb->transfer_buffer = transfer_buffer;
  215. urb->transfer_buffer_length = transfer_buffer_length;
  216. urb->timeout = timeout;
  217. urb->complete = complete;
  218. urb->arg = arg;
  219. }
  220. static inline void usbh_int_urb_fill(struct usbh_urb *urb,
  221. struct usbh_hubport *hport,
  222. struct usb_endpoint_descriptor *ep,
  223. uint8_t *transfer_buffer,
  224. uint32_t transfer_buffer_length,
  225. uint32_t timeout,
  226. usbh_complete_callback_t complete,
  227. void *arg)
  228. {
  229. urb->hport = hport;
  230. urb->ep = ep;
  231. urb->setup = NULL;
  232. urb->transfer_buffer = transfer_buffer;
  233. urb->transfer_buffer_length = transfer_buffer_length;
  234. urb->timeout = timeout;
  235. urb->complete = complete;
  236. urb->arg = arg;
  237. urb->interval = USBH_GET_URB_INTERVAL(ep->bInterval, hport->speed);
  238. }
  239. extern struct usbh_bus g_usbhost_bus[];
  240. #ifdef USBH_IRQHandler
  241. #error USBH_IRQHandler is obsolete, please call USBH_IRQHandler(xxx) in your irq
  242. #endif
  243. /**
  244. * @brief Submit an control transfer to an endpoint.
  245. * This is a blocking method; this method will not return until the transfer has completed.
  246. * Default timeout is 500ms.
  247. *
  248. * @param pipe The control endpoint to send/receive the control request.
  249. * @param setup Setup packet to be sent.
  250. * @param buffer buffer used for sending the request and for returning any responses.
  251. * @return On success will return 0, and others indicate fail.
  252. */
  253. int usbh_control_transfer(struct usbh_hubport *hport, struct usb_setup_packet *setup, uint8_t *buffer);
  254. /**
  255. * @brief Retrieves a USB string descriptor from a specific hub port.
  256. *
  257. * This function is responsible for retrieving the USB string descriptor
  258. * with the specified index from the USB device connected to the given hub port.
  259. * The retrieved descriptor is stored in the output buffer provided.
  260. *
  261. * @param hport Pointer to the USB hub port structure.
  262. * @param index Index of the string descriptor to retrieve.
  263. * @param output Pointer to the buffer where the retrieved descriptor will be stored.
  264. * @param output_len Length of the output buffer.
  265. * @return On success will return 0, and others indicate fail.
  266. */
  267. int usbh_get_string_desc(struct usbh_hubport *hport, uint8_t index, uint8_t *output, uint16_t output_len);
  268. /**
  269. * @brief Sets the alternate setting for a USB interface on a specific hub port.
  270. *
  271. * This function is responsible for setting the alternate setting of the
  272. * specified USB interface on the USB device connected to the given hub port.
  273. * The interface and alternate setting are identified by the respective parameters.
  274. *
  275. * @param hport Pointer to the USB hub port structure.
  276. * @param intf Interface number to set the alternate setting for.
  277. * @param altsetting Alternate setting value to set for the interface.
  278. * @return On success will return 0, and others indicate fail.
  279. */
  280. int usbh_set_interface(struct usbh_hubport *hport, uint8_t intf, uint8_t altsetting);
  281. int usbh_initialize(uint8_t busid, uintptr_t reg_base, usbh_event_handler_t event_handler);
  282. int usbh_deinitialize(uint8_t busid);
  283. void *usbh_find_class_instance(const char *devname);
  284. struct usbh_hubport *usbh_find_hubport(uint8_t busid, uint8_t hub_index, uint8_t hub_port);
  285. uint8_t usbh_get_hport_active_config_index(struct usbh_hubport *hport);
  286. int lsusb(int argc, char **argv);
  287. #ifdef __cplusplus
  288. }
  289. #endif
  290. #endif /* USBH_CORE_H */