usb_host.h 6.9 KB

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