usb_host.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-3-12 Yi Qiu first version
  9. * 2021-02-23 Leslie Lee provide possibility for multi usb host
  10. */
  11. #ifndef __RT_USB_HOST_H__
  12. #define __RT_USB_HOST_H__
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #include <rtthread.h>
  17. #include "usb_common.h"
  18. #define USB_MAX_DEVICE 0x20
  19. #define USB_MAX_INTERFACE 0x08
  20. #define USB_HUB_PORT_NUM 0x04
  21. #define SIZEOF_USB_REQUEST 0x08
  22. #define DEV_STATUS_IDLE 0x00
  23. #define DEV_STATUS_BUSY 0x01
  24. #define DEV_STATUS_ERROR 0x02
  25. #define UPIPE_STATUS_OK 0x00
  26. #define UPIPE_STATUS_STALL 0x01
  27. #define UPIPE_STATUS_ERROR 0x02
  28. #define USBH_PID_SETUP 0x00
  29. #define USBH_PID_DATA 0x01
  30. struct uhcd;
  31. struct uhintf;
  32. struct uhub;
  33. struct upipe;
  34. struct uclass_driver
  35. {
  36. rt_list_t list;
  37. int class_code;
  38. int subclass_code;
  39. rt_err_t (*enable)(void* arg);
  40. rt_err_t (*disable)(void* arg);
  41. void* user_data;
  42. };
  43. typedef struct uclass_driver* ucd_t;
  44. struct uprotocal
  45. {
  46. rt_list_t list;
  47. int pro_id;
  48. rt_err_t (*init)(void* arg);
  49. rt_err_t (*callback)(void* arg);
  50. };
  51. typedef struct uprotocal* uprotocal_t;
  52. struct uinstance
  53. {
  54. struct rt_device parent;
  55. struct udevice_descriptor dev_desc;
  56. ucfg_desc_t cfg_desc;
  57. struct uhcd *hcd;
  58. struct upipe * pipe_ep0_out;
  59. struct upipe * pipe_ep0_in;
  60. rt_list_t pipe;
  61. rt_uint8_t status;
  62. rt_uint8_t type;
  63. rt_uint8_t index;
  64. rt_uint8_t address;
  65. rt_uint8_t speed;
  66. rt_uint8_t max_packet_size;
  67. rt_uint8_t port;
  68. struct uhub* parent_hub;
  69. struct uhintf* intf[USB_MAX_INTERFACE];
  70. };
  71. typedef struct uinstance* uinst_t;
  72. struct uhintf
  73. {
  74. struct uinstance* device;
  75. uintf_desc_t intf_desc;
  76. ucd_t drv;
  77. void *user_data;
  78. };
  79. struct upipe
  80. {
  81. rt_list_t list;
  82. rt_uint8_t pipe_index;
  83. rt_uint32_t status;
  84. struct uendpoint_descriptor ep;
  85. uinst_t inst;
  86. func_callback callback;
  87. void* user_data;
  88. };
  89. typedef struct upipe* upipe_t;
  90. struct uhub
  91. {
  92. struct uhub_descriptor hub_desc;
  93. rt_uint8_t num_ports;
  94. rt_uint32_t port_status[USB_HUB_PORT_NUM];
  95. struct uinstance* child[USB_HUB_PORT_NUM];
  96. rt_bool_t is_roothub;
  97. rt_uint8_t buffer[8];
  98. struct uinstance* self;
  99. struct uhcd *hcd;
  100. };
  101. typedef struct uhub* uhub_t;
  102. struct uhcd_ops
  103. {
  104. rt_err_t (*reset_port) (rt_uint8_t port);
  105. int (*pipe_xfer) (upipe_t pipe, rt_uint8_t token, void* buffer, int nbytes, int timeout);
  106. rt_err_t (*open_pipe) (upipe_t pipe);
  107. rt_err_t (*close_pipe) (upipe_t pipe);
  108. };
  109. typedef struct uhcd_ops* uhcd_ops_t;
  110. struct uhcd
  111. {
  112. struct rt_device parent;
  113. uhcd_ops_t ops;
  114. rt_uint8_t num_ports;
  115. uhub_t roothub;
  116. struct rt_messagequeue *usb_mq;
  117. };
  118. typedef struct uhcd* uhcd_t;
  119. enum uhost_msg_type
  120. {
  121. USB_MSG_CONNECT_CHANGE,
  122. USB_MSG_CALLBACK,
  123. };
  124. typedef enum uhost_msg_type uhost_msg_type;
  125. struct uhost_msg
  126. {
  127. uhost_msg_type type;
  128. union
  129. {
  130. struct uhub* hub;
  131. struct
  132. {
  133. func_callback function;
  134. void *context;
  135. }cb;
  136. }content;
  137. };
  138. typedef struct uhost_msg* uhost_msg_t;
  139. /* usb host system interface */
  140. rt_err_t rt_usb_host_init(const char *name);
  141. void rt_usbh_hub_init(struct uhcd *hcd);
  142. /* usb host core interface */
  143. struct uinstance* rt_usbh_alloc_instance(uhcd_t uhcd);
  144. rt_err_t rt_usbh_attatch_instance(struct uinstance* device);
  145. rt_err_t rt_usbh_detach_instance(struct uinstance* device);
  146. rt_err_t rt_usbh_get_descriptor(struct uinstance* device, rt_uint8_t type, void* buffer, int nbytes);
  147. rt_err_t rt_usbh_set_configure(struct uinstance* device, int config);
  148. rt_err_t rt_usbh_set_address(struct uinstance* device);
  149. rt_err_t rt_usbh_set_interface(struct uinstance* device, int intf);
  150. rt_err_t rt_usbh_clear_feature(struct uinstance* device, int endpoint, int feature);
  151. rt_err_t rt_usbh_get_interface_descriptor(ucfg_desc_t cfg_desc, int num, uintf_desc_t* intf_desc);
  152. rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num, uep_desc_t* ep_desc);
  153. /* usb class driver interface */
  154. rt_err_t rt_usbh_class_driver_init(void);
  155. rt_err_t rt_usbh_class_driver_register(ucd_t drv);
  156. rt_err_t rt_usbh_class_driver_unregister(ucd_t drv);
  157. rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args);
  158. rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args);
  159. ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code);
  160. /* usb class driver implement */
  161. ucd_t rt_usbh_class_driver_hub(void);
  162. ucd_t rt_usbh_class_driver_storage(void);
  163. /* usb hub interface */
  164. rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer,
  165. rt_size_t size);
  166. rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer);
  167. rt_err_t rt_usbh_hub_get_port_status(uhub_t uhub, rt_uint16_t port,
  168. rt_uint32_t* buffer);
  169. rt_err_t rt_usbh_hub_clear_port_feature(uhub_t uhub, rt_uint16_t port,
  170. rt_uint16_t feature);
  171. rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port,
  172. rt_uint16_t feature);
  173. rt_err_t rt_usbh_hub_reset_port(uhub_t uhub, rt_uint16_t port);
  174. rt_err_t rt_usbh_event_signal(uhcd_t uhcd, struct uhost_msg* msg);
  175. void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS);
  176. void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port);
  177. /* usb host controller driver interface */
  178. rt_inline rt_err_t rt_usb_instance_add_pipe(uinst_t inst, upipe_t pipe)
  179. {
  180. RT_ASSERT(inst != RT_NULL);
  181. RT_ASSERT(pipe != RT_NULL);
  182. rt_list_insert_before(&inst->pipe, &pipe->list);
  183. return RT_EOK;
  184. }
  185. rt_inline upipe_t rt_usb_instance_find_pipe(uinst_t inst,rt_uint8_t ep_address)
  186. {
  187. rt_list_t * l;
  188. for(l = inst->pipe.next;l != &inst->pipe;l = l->next)
  189. {
  190. if(rt_list_entry(l,struct upipe,list)->ep.bEndpointAddress == ep_address)
  191. {
  192. return rt_list_entry(l,struct upipe,list);
  193. }
  194. }
  195. return RT_NULL;
  196. }
  197. rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd, upipe_t* pipe, uinst_t inst, uep_desc_t ep)
  198. {
  199. *pipe = (upipe_t)rt_malloc(sizeof(struct upipe));
  200. if(*pipe == RT_NULL)
  201. {
  202. return -RT_ERROR;
  203. }
  204. rt_memset(*pipe,0,sizeof(struct upipe));
  205. (*pipe)->inst = inst;
  206. rt_memcpy(&(*pipe)->ep,ep,sizeof(struct uendpoint_descriptor));
  207. return hcd->ops->open_pipe(*pipe);
  208. }
  209. rt_inline void rt_usb_pipe_add_callback(upipe_t pipe, func_callback callback)
  210. {
  211. pipe->callback = callback;
  212. }
  213. rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
  214. {
  215. RT_ASSERT(pipe != RT_NULL);
  216. hcd->ops->close_pipe(pipe);
  217. rt_free(pipe);
  218. return RT_EOK;
  219. }
  220. int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout);
  221. rt_inline int rt_usb_hcd_setup_xfer(uhcd_t hcd, upipe_t pipe, ureq_t setup, int timeout)
  222. {
  223. return hcd->ops->pipe_xfer(pipe, USBH_PID_SETUP, (void *)setup, 8, timeout);
  224. }
  225. #ifdef __cplusplus
  226. }
  227. #endif
  228. #endif