usb_host.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2020 Actions Corporation.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief USB host core layer APIs and structures
  9. *
  10. * This file contains the USB device core layer APIs and structures.
  11. */
  12. #ifndef USB_HOST_H_
  13. #define USB_HOST_H_
  14. #include <drivers/usb/usb_hc.h>
  15. #include <usb/usbstruct.h>
  16. #include <usb/usb_common.h>
  17. #include <sys/slist.h>
  18. #define USB_CTRL_TIMEOUT 5000 /* 5s timeout */
  19. #define USB_MAXINTERFACES CONFIG_USB_HOST_MAXINTERFACES
  20. #define USB_MAXENDPOINTS CONFIG_USB_HOST_MAXENDPOINTS
  21. #define USB_RAW_DESCRIPTORS_SIZE CONFIG_USB_HOST_RAW_DESCRIPTORS_SIZE
  22. struct usb_interface {
  23. struct usb_if_descriptor desc;
  24. struct usb_ep_descriptor ep_desc[USB_MAXENDPOINTS];
  25. uint8_t no_of_ep;
  26. uint8_t num_altsetting;
  27. uint8_t act_altsetting;
  28. struct usb_driver *driver;
  29. } __packed;
  30. struct usb_config {
  31. union {
  32. struct usb_cfg_descriptor desc;
  33. uint8_t rawdescriptors[USB_RAW_DESCRIPTORS_SIZE];
  34. };
  35. uint8_t no_of_if; /* number of interfaces */
  36. struct usb_interface intf[USB_MAXINTERFACES];
  37. } __packed;
  38. struct usb_device {
  39. struct usb_device_descriptor desc;
  40. uint8_t devnum;
  41. uint8_t scanning;
  42. uint8_t state; /* enum usb_device_state */
  43. uint8_t speed; /* enum usb_device_speed */
  44. struct usb_config config;
  45. };
  46. /*
  47. * struct usb_device_id - identifies USB devices for probing and hotplugging
  48. */
  49. struct usb_device_id {
  50. /* which fields to match against? */
  51. uint16_t match_flags;
  52. /* Used for product specific matches; range is inclusive */
  53. uint16_t idVendor;
  54. uint16_t idProduct;
  55. uint16_t bcdDevice_lo;
  56. uint16_t bcdDevice_hi;
  57. /* Used for device class matches */
  58. uint8_t bDeviceClass;
  59. uint8_t bDeviceSubClass;
  60. uint8_t bDeviceProtocol;
  61. /* Used for interface class matches */
  62. uint8_t bInterfaceClass;
  63. uint8_t bInterfaceSubClass;
  64. uint8_t bInterfaceProtocol;
  65. /* Used for vendor-specific interface matches */
  66. uint8_t bInterfaceNumber;
  67. /* not matched against */
  68. unsigned long driver_info;
  69. };
  70. /* Some useful macros to use to create struct usb_device_id */
  71. #define USB_DEVICE_ID_MATCH_VENDOR 0x0001
  72. #define USB_DEVICE_ID_MATCH_PRODUCT 0x0002
  73. #define USB_DEVICE_ID_MATCH_DEV_LO 0x0004
  74. #define USB_DEVICE_ID_MATCH_DEV_HI 0x0008
  75. #define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010
  76. #define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020
  77. #define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040
  78. #define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080
  79. #define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100
  80. #define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200
  81. #define USB_DEVICE_ID_MATCH_INT_NUMBER 0x0400
  82. #define USB_DEVICE_ID_MATCH_DEVICE \
  83. (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
  84. #define USB_DEVICE_ID_MATCH_DEV_RANGE \
  85. (USB_DEVICE_ID_MATCH_DEV_LO | USB_DEVICE_ID_MATCH_DEV_HI)
  86. #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION \
  87. (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_RANGE)
  88. #define USB_DEVICE_ID_MATCH_DEV_INFO \
  89. (USB_DEVICE_ID_MATCH_DEV_CLASS | \
  90. USB_DEVICE_ID_MATCH_DEV_SUBCLASS | \
  91. USB_DEVICE_ID_MATCH_DEV_PROTOCOL)
  92. #define USB_DEVICE_ID_MATCH_INT_INFO \
  93. (USB_DEVICE_ID_MATCH_INT_CLASS | \
  94. USB_DEVICE_ID_MATCH_INT_SUBCLASS | \
  95. USB_DEVICE_ID_MATCH_INT_PROTOCOL)
  96. /* usb interface driver */
  97. struct usb_driver {
  98. sys_snode_t node;
  99. int (*probe)(struct usb_interface *intf,
  100. const struct usb_device_id *id);
  101. void (*disconnect)(struct usb_interface *intf);
  102. const struct usb_device_id *id_table;
  103. };
  104. /*
  105. * @brief turn on/off USB VBUS voltage
  106. *
  107. * @param on Set to false to turn off and to true to turn on VBUS
  108. *
  109. * @return 0 on success, negative errno code on fail
  110. */
  111. int usbh_vbus_set(bool on);
  112. /*
  113. * @brief issue an asynchronous transfer request for an endpoint
  114. *
  115. * If successful, it returns 0, otherwise a negative error number.
  116. */
  117. int usbh_submit_urb(struct usb_request *urb);
  118. /*
  119. * @brief cancel a transfer request
  120. *
  121. * If successful, it returns 0, otherwise a negative error number.
  122. */
  123. int usbh_cancel_urb(struct usb_request *urb);
  124. /*
  125. * @brief issue an synchronous transfer request for an endpoint
  126. *
  127. * If successful, it returns 0, otherwise a negative error number. The number
  128. * of actual bytes transferred will be stored in urb->actual.
  129. */
  130. int usbh_transfer_urb_sync(struct usb_request *urb);
  131. /*
  132. * @brief submits a control message and waits for comletion
  133. *
  134. * If successful, it returns the number of bytes transferred, otherwise a
  135. * negative error number.
  136. */
  137. int usbh_control_msg(unsigned char request, unsigned char requesttype,
  138. unsigned short value, unsigned short index,
  139. void *data, unsigned short size, int timeout);
  140. /*
  141. * @brief Builds a bulk urb, sends it off and waits for completion
  142. *
  143. * If successful, it returns 0, otherwise a negative error number. The number
  144. * of actual bytes transferred will be stored in the actual parameter.
  145. */
  146. int usbh_bulk_msg(uint8_t ep, void *data, int len, uint32_t *actual, int timeout);
  147. /*
  148. * @brief clear endpoint halt/stall condition
  149. *
  150. * This is used to clear halt conditions for bulk and interrupt endpoints.
  151. */
  152. int usbh_clear_halt(uint8_t ep);
  153. /*
  154. * @brief enable an endpoint for USB communications
  155. *
  156. * This is used to configure and enable endpoint.
  157. */
  158. int usbh_enable_endpoint(struct usb_ep_descriptor *desc);
  159. /*
  160. * @brief disable an endpoint by address
  161. *
  162. * Disables the endpoint for URB submission and nukes all pending URBs.
  163. */
  164. int usbh_disable_endpoint(struct usb_ep_descriptor *desc);
  165. /**
  166. * @brief enable all the endpoints for an interface
  167. *
  168. * Enables all the endpoints for the interface's current altsetting.
  169. */
  170. int usbh_enable_interface(struct usb_interface *intf);
  171. /*
  172. * @brief disable all endpoints for an interface
  173. *
  174. * Disables all the endpoints for the interface's current altsetting.
  175. */
  176. int usbh_disable_interface(struct usb_interface *intf);
  177. /*
  178. * @brief notify host core to prepare for enumeration
  179. *
  180. * If successful, it returns 0, otherwise a negative error number.
  181. */
  182. int usbh_prepare_scan(void);
  183. /*
  184. * @brief start to enumerate USB device
  185. *
  186. * If successful, it returns 0, otherwise a negative error number.
  187. */
  188. int usbh_scan_device(void);
  189. /*
  190. * @brief handle port reset
  191. *
  192. * If successful, it returns 0, otherwise a negative error number.
  193. */
  194. int usbh_reset_device(void);
  195. /*
  196. * @brief disconnect USB device (Synchronously)
  197. *
  198. * If successful, it returns 0, otherwise a negative error number.
  199. */
  200. int usbh_disconnect(void);
  201. /*
  202. * @brief poll if enumerating is in progress
  203. *
  204. * If enumerating, it returns true, otherwise false.
  205. */
  206. bool usbh_is_scanning(void);
  207. /*
  208. * @brief register a USB interface driver
  209. */
  210. int usbh_register_driver(struct usb_driver *driver);
  211. /*
  212. * @brief unregister a USB interface driver
  213. */
  214. void usbh_unregister_driver(struct usb_driver *driver);
  215. /*
  216. * @brief control the FIFO of USB controller
  217. *
  218. * @param enable Set to false to make FIFO accessible by CPU as normal RAM
  219. * and to true to make FIFO accessible by USB.
  220. *
  221. * If successful, it returns 0, otherwise a negative error number.
  222. */
  223. int usbh_fifo_control(bool enable);
  224. #endif /* USB_HOST_H_ */