usbh_core.h 10 KB

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