usbh_core.h 11 KB

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