usbh_core.h 11 KB

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