usbh_core.h 10 KB

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