usbh_core.h 9.6 KB

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