usb_host.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * File : usb_host.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-3-12 Yi Qiu first version
  13. */
  14. #ifndef __RT_USB_HOST_H__
  15. #define __RT_USB_HOST_H__
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #include <rtthread.h>
  20. #include "usb_common.h"
  21. #define USB_MAX_DEVICE 0x20
  22. #define USB_MAX_INTERFACE 0x08
  23. #define USB_HUB_PORT_NUM 0x04
  24. #define SIZEOF_USB_REQUEST 0x08
  25. #define UINST_STATUS_IDLE 0x00
  26. #define UINST_STATUS_BUSY 0x01
  27. #define UINST_STATUS_ERROR 0x02
  28. #define UPIPE_STATUS_OK 0x00
  29. #define UPIPE_STATUS_STALL 0x01
  30. #define UPIPE_STATUS_ERROR 0x02
  31. struct uhcd;
  32. struct uifinst;
  33. struct uhubinst;
  34. struct uclass_driver
  35. {
  36. rt_list_t list;
  37. int class_code;
  38. int subclass_code;
  39. rt_err_t (*run)(void* arg);
  40. rt_err_t (*stop)(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 udevice_descriptor dev_desc;
  55. ucfg_desc_t cfg_desc;
  56. struct uhcd *hcd;
  57. rt_uint8_t status;
  58. rt_uint8_t type;
  59. rt_uint8_t index;
  60. rt_uint8_t address;
  61. rt_uint8_t speed;
  62. rt_uint8_t max_packet_size;
  63. rt_uint8_t port;
  64. struct uhubinst* parent;
  65. struct uifinst* ifinst[USB_MAX_INTERFACE];
  66. };
  67. typedef struct uinstance* uinst_t;
  68. struct uifinst
  69. {
  70. uinst_t uinst;
  71. uintf_desc_t intf_desc;
  72. ucd_t drv;
  73. void *user_data;
  74. };
  75. typedef struct uifinst* uifinst_t;
  76. struct upipe
  77. {
  78. rt_uint32_t status;
  79. struct uendpoint_descriptor ep;
  80. uifinst_t ifinst;
  81. func_callback callback;
  82. void* user_data;
  83. };
  84. typedef struct upipe* upipe_t;
  85. struct uhubinst
  86. {
  87. struct uhub_descriptor hub_desc;
  88. rt_uint8_t num_ports;
  89. rt_uint32_t port_status[USB_HUB_PORT_NUM];
  90. struct uinstance* child[USB_HUB_PORT_NUM];
  91. rt_bool_t is_roothub;
  92. upipe_t pipe_in;
  93. rt_uint8_t buffer[8];
  94. struct uinstance* self;
  95. struct uhcd *hcd;
  96. };
  97. typedef struct uhubinst* uhubinst_t;
  98. struct uhcd_ops
  99. {
  100. int (*ctl_xfer)(uinst_t inst, ureq_t setup, void* buffer, int nbytes,
  101. int timeout);
  102. int (*bulk_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  103. int (*int_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  104. int (*iso_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  105. rt_err_t (*alloc_pipe)(struct upipe** pipe, uifinst_t ifinst, uep_desc_t ep,
  106. func_callback callback);
  107. rt_err_t (*free_pipe)(upipe_t pipe);
  108. rt_err_t (*hub_ctrl)(rt_uint16_t port, rt_uint8_t cmd, void *args);
  109. };
  110. struct uhcd
  111. {
  112. struct rt_device parent;
  113. struct uhcd_ops* ops;
  114. };
  115. typedef struct uhcd* uhcd_t;
  116. enum uhost_msg_type
  117. {
  118. USB_MSG_CONNECT_CHANGE,
  119. USB_MSG_CALLBACK,
  120. };
  121. typedef enum uhost_msg_type uhost_msg_type;
  122. struct uhost_msg
  123. {
  124. uhost_msg_type type;
  125. union
  126. {
  127. struct uhubinst* uhub;
  128. struct
  129. {
  130. func_callback function;
  131. void *context;
  132. }cb;
  133. }content;
  134. };
  135. typedef struct uhost_msg* uhost_msg_t;
  136. /* usb host system interface */
  137. void rt_usb_host_init(void);
  138. void rt_usb_hub_thread(void);
  139. /* usb host core interface */
  140. uinst_t rt_usb_alloc_instance(void);
  141. rt_err_t rt_usb_attatch_instance(uinst_t uinst);
  142. rt_err_t rt_usb_detach_instance(uinst_t uinst);
  143. rt_err_t rt_usb_get_descriptor(uinst_t uinst, rt_uint8_t type, void* buffer,
  144. int nbytes);
  145. rt_err_t rt_usb_set_configure(uinst_t uinst, int config);
  146. rt_err_t rt_usb_set_address(uinst_t uinst);
  147. rt_err_t rt_usb_set_interface(uinst_t uinst, int intf);
  148. rt_err_t rt_usb_clear_feature(uinst_t uinst, int endpoint, int feature);
  149. rt_err_t rt_usb_get_interface_descriptor(ucfg_desc_t cfg_desc, int num,
  150. uintf_desc_t* intf_desc);
  151. rt_err_t rt_usb_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
  152. uep_desc_t* ep_desc);
  153. /* usb class driver interface */
  154. rt_err_t rt_usb_class_driver_init(void);
  155. rt_err_t rt_usb_class_driver_register(ucd_t drv);
  156. rt_err_t rt_usb_class_driver_unregister(ucd_t drv);
  157. rt_err_t rt_usb_class_driver_run(ucd_t drv, void* args);
  158. rt_err_t rt_usb_class_driver_stop(ucd_t drv, void* args);
  159. ucd_t rt_usb_class_driver_find(int class_code, int subclass_code);
  160. /* usb class driver implement */
  161. ucd_t rt_usb_class_driver_hid(void);
  162. ucd_t rt_usb_class_driver_hub(void);
  163. ucd_t rt_usb_class_driver_storage(void);
  164. ucd_t rt_usb_class_driver_adk(void);
  165. /* usb hid protocal implement */
  166. uprotocal_t rt_usb_hid_protocal_kbd(void);
  167. uprotocal_t rt_usb_hid_protocal_mouse(void);
  168. /* usb adk class driver interface */
  169. rt_err_t rt_usb_adk_set_string(const char* manufacturer, const char* model,
  170. const char* description, const char* version, const char* uri,
  171. const char* serial);
  172. /* usb hub interface */
  173. rt_err_t rt_usb_hub_get_descriptor(uinst_t uinst, rt_uint8_t *buffer,
  174. rt_size_t size);
  175. rt_err_t rt_usb_hub_get_status(uinst_t uinst, rt_uint8_t* buffer);
  176. rt_err_t rt_usb_hub_get_port_status(uhubinst_t uhub, rt_uint16_t port,
  177. rt_uint8_t* buffer);
  178. rt_err_t rt_usb_hub_clear_port_feature(uhubinst_t uhub, rt_uint16_t port,
  179. rt_uint16_t feature);
  180. rt_err_t rt_usb_hub_set_port_feature(uhubinst_t uhub, rt_uint16_t port,
  181. rt_uint16_t feature);
  182. rt_err_t rt_usb_hub_reset_port(uhubinst_t uhub, rt_uint16_t port);
  183. rt_err_t rt_usb_post_event(struct uhost_msg* msg, rt_size_t size);
  184. /* usb host controller driver interface */
  185. rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd, upipe_t* pipe,
  186. uifinst_t ifinst, uep_desc_t ep, func_callback callback)
  187. {
  188. if(ifinst == RT_NULL) return -RT_EIO;
  189. return hcd->ops->alloc_pipe(pipe, ifinst, ep, callback);
  190. }
  191. rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
  192. {
  193. RT_ASSERT(pipe != RT_NULL);
  194. return hcd->ops->free_pipe(pipe);
  195. }
  196. rt_inline int rt_usb_hcd_bulk_xfer(uhcd_t hcd, upipe_t pipe, void* buffer,
  197. int nbytes, int timeout)
  198. {
  199. if(pipe == RT_NULL) return -1;
  200. if(pipe->ifinst == RT_NULL) return -1;
  201. if(pipe->ifinst->uinst == RT_NULL) return -1;
  202. if(pipe->ifinst->uinst->status == UINST_STATUS_IDLE)
  203. return -1;
  204. return hcd->ops->bulk_xfer(pipe, buffer, nbytes, timeout);
  205. }
  206. rt_inline int rt_usb_hcd_control_xfer(uhcd_t hcd, uinst_t uinst, ureq_t setup,
  207. void* buffer, int nbytes, int timeout)
  208. {
  209. if(uinst->status == UINST_STATUS_IDLE) return -1;
  210. return hcd->ops->ctl_xfer(uinst, setup, buffer, nbytes, timeout);
  211. }
  212. rt_inline int rt_usb_hcd_int_xfer(uhcd_t hcd, upipe_t pipe, void* buffer,
  213. int nbytes, int timeout)
  214. {
  215. if(pipe == RT_NULL) return -1;
  216. if(pipe->ifinst == RT_NULL) return -1;
  217. if(pipe->ifinst->uinst == RT_NULL) return -1;
  218. if(pipe->ifinst->uinst->status == UINST_STATUS_IDLE)
  219. return -1;
  220. return hcd->ops->int_xfer(pipe, buffer, nbytes, timeout);
  221. }
  222. rt_inline rt_err_t rt_usb_hcd_hub_control(uhcd_t hcd, rt_uint16_t port,
  223. rt_uint8_t cmd, void *args)
  224. {
  225. return hcd->ops->hub_ctrl(port, cmd, args);
  226. }
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif