usb_host.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-3-12 Yi Qiu first version
  23. */
  24. #ifndef __RT_USB_HOST_H__
  25. #define __RT_USB_HOST_H__
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include <rtthread.h>
  30. #include "usb_common.h"
  31. #define USB_MAX_DEVICE 0x20
  32. #define USB_MAX_INTERFACE 0x08
  33. #define USB_HUB_PORT_NUM 0x04
  34. #define SIZEOF_USB_REQUEST 0x08
  35. #define UINST_STATUS_IDLE 0x00
  36. #define UINST_STATUS_BUSY 0x01
  37. #define UINST_STATUS_ERROR 0x02
  38. #define UPIPE_STATUS_OK 0x00
  39. #define UPIPE_STATUS_STALL 0x01
  40. #define UPIPE_STATUS_ERROR 0x02
  41. struct uhcd;
  42. struct uifinst;
  43. struct uhubinst;
  44. struct uclass_driver
  45. {
  46. rt_list_t list;
  47. int class_code;
  48. int subclass_code;
  49. rt_err_t (*run)(void* arg);
  50. rt_err_t (*stop)(void* arg);
  51. void* user_data;
  52. };
  53. typedef struct uclass_driver* ucd_t;
  54. struct uprotocal
  55. {
  56. rt_list_t list;
  57. int pro_id;
  58. rt_err_t (*init)(void* arg);
  59. rt_err_t (*callback)(void* arg);
  60. };
  61. typedef struct uprotocal* uprotocal_t;
  62. struct uinstance
  63. {
  64. struct udevice_descriptor dev_desc;
  65. ucfg_desc_t cfg_desc;
  66. struct uhcd *hcd;
  67. rt_uint8_t status;
  68. rt_uint8_t type;
  69. rt_uint8_t index;
  70. rt_uint8_t address;
  71. rt_uint8_t speed;
  72. rt_uint8_t max_packet_size;
  73. rt_uint8_t port;
  74. struct uhubinst* parent;
  75. struct uifinst* ifinst[USB_MAX_INTERFACE];
  76. };
  77. typedef struct uinstance* uinst_t;
  78. struct uifinst
  79. {
  80. uinst_t uinst;
  81. uintf_desc_t intf_desc;
  82. ucd_t drv;
  83. void *user_data;
  84. };
  85. typedef struct uifinst* uifinst_t;
  86. struct upipe
  87. {
  88. rt_uint32_t status;
  89. struct uendpoint_descriptor ep;
  90. uifinst_t ifinst;
  91. func_callback callback;
  92. void* user_data;
  93. };
  94. typedef struct upipe* upipe_t;
  95. struct uhubinst
  96. {
  97. struct uhub_descriptor hub_desc;
  98. rt_uint8_t num_ports;
  99. rt_uint32_t port_status[USB_HUB_PORT_NUM];
  100. struct uinstance* child[USB_HUB_PORT_NUM];
  101. rt_bool_t is_roothub;
  102. upipe_t pipe_in;
  103. rt_uint8_t buffer[8];
  104. struct uinstance* self;
  105. struct uhcd *hcd;
  106. };
  107. typedef struct uhubinst* uhubinst_t;
  108. struct uhcd_ops
  109. {
  110. int (*ctl_xfer)(uinst_t inst, ureq_t setup, void* buffer, int nbytes,
  111. int timeout);
  112. int (*bulk_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  113. int (*int_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  114. int (*iso_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  115. rt_err_t (*alloc_pipe)(struct upipe** pipe, uifinst_t ifinst, uep_desc_t ep,
  116. func_callback callback);
  117. rt_err_t (*free_pipe)(upipe_t pipe);
  118. rt_err_t (*hub_ctrl)(rt_uint16_t port, rt_uint8_t cmd, void *args);
  119. };
  120. struct uhcd
  121. {
  122. struct rt_device parent;
  123. struct uhcd_ops* ops;
  124. };
  125. typedef struct uhcd* uhcd_t;
  126. enum uhost_msg_type
  127. {
  128. USB_MSG_CONNECT_CHANGE,
  129. USB_MSG_CALLBACK,
  130. };
  131. typedef enum uhost_msg_type uhost_msg_type;
  132. struct uhost_msg
  133. {
  134. uhost_msg_type type;
  135. union
  136. {
  137. struct uhubinst* uhub;
  138. struct
  139. {
  140. func_callback function;
  141. void *context;
  142. }cb;
  143. }content;
  144. };
  145. typedef struct uhost_msg* uhost_msg_t;
  146. /* usb host system interface */
  147. void rt_usb_host_init(void);
  148. void rt_usb_hub_thread(void);
  149. /* usb host core interface */
  150. uinst_t rt_usb_alloc_instance(void);
  151. rt_err_t rt_usb_attatch_instance(uinst_t uinst);
  152. rt_err_t rt_usb_detach_instance(uinst_t uinst);
  153. rt_err_t rt_usb_get_descriptor(uinst_t uinst,
  154. rt_uint8_t type,
  155. void *buffer,
  156. int nbytes);
  157. rt_err_t rt_usb_set_configure(uinst_t uinst, int config);
  158. rt_err_t rt_usb_set_address(uinst_t uinst);
  159. rt_err_t rt_usb_set_interface(uinst_t uinst, int intf);
  160. rt_err_t rt_usb_clear_feature(uinst_t uinst, int endpoint, int feature);
  161. rt_err_t rt_usb_get_interface_descriptor(ucfg_desc_t cfg_desc,
  162. int num,
  163. uintf_desc_t *intf_desc);
  164. rt_err_t rt_usb_get_endpoint_descriptor(uintf_desc_t intf_desc,
  165. int num,
  166. uep_desc_t *ep_desc);
  167. /* usb class driver interface */
  168. rt_err_t rt_usb_class_driver_init(void);
  169. rt_err_t rt_usb_class_driver_register(ucd_t drv);
  170. rt_err_t rt_usb_class_driver_unregister(ucd_t drv);
  171. rt_err_t rt_usb_class_driver_run(ucd_t drv, void *args);
  172. rt_err_t rt_usb_class_driver_stop(ucd_t drv, void *args);
  173. ucd_t rt_usb_class_driver_find(int class_code, int subclass_code);
  174. /* usb class driver implement */
  175. ucd_t rt_usb_class_driver_hid(void);
  176. ucd_t rt_usb_class_driver_hub(void);
  177. ucd_t rt_usb_class_driver_storage(void);
  178. ucd_t rt_usb_class_driver_adk(void);
  179. /* usb hid protocal implement */
  180. uprotocal_t rt_usb_hid_protocal_kbd(void);
  181. uprotocal_t rt_usb_hid_protocal_mouse(void);
  182. /* usb adk class driver interface */
  183. rt_err_t rt_usb_adk_set_string(const char *manufacturer,
  184. const char *model,
  185. const char *description,
  186. const char *version,
  187. const char *uri,
  188. const char *serial);
  189. /* usb hub interface */
  190. rt_err_t rt_usb_hub_get_descriptor(uinst_t uinst,
  191. rt_uint8_t *buffer,
  192. rt_size_t size);
  193. rt_err_t rt_usb_hub_get_status(uinst_t uinst, rt_uint8_t *buffer);
  194. rt_err_t rt_usb_hub_get_port_status(uhubinst_t uhub,
  195. rt_uint16_t port,
  196. rt_uint8_t *buffer);
  197. rt_err_t rt_usb_hub_clear_port_feature(uhubinst_t uhub,
  198. rt_uint16_t port,
  199. rt_uint16_t feature);
  200. rt_err_t rt_usb_hub_set_port_feature(uhubinst_t uhub,
  201. rt_uint16_t port,
  202. rt_uint16_t feature);
  203. rt_err_t rt_usb_hub_reset_port(uhubinst_t uhub, rt_uint16_t port);
  204. rt_err_t rt_usb_post_event(struct uhost_msg* msg, rt_size_t size);
  205. /* usb host controller driver interface */
  206. rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd,
  207. upipe_t *pipe,
  208. uifinst_t ifinst,
  209. uep_desc_t ep,
  210. func_callback callback)
  211. {
  212. if (ifinst == RT_NULL)
  213. return -RT_EIO;
  214. return hcd->ops->alloc_pipe(pipe, ifinst, ep, callback);
  215. }
  216. rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
  217. {
  218. RT_ASSERT(pipe != RT_NULL);
  219. return hcd->ops->free_pipe(pipe);
  220. }
  221. rt_inline int rt_usb_hcd_bulk_xfer(uhcd_t hcd,
  222. upipe_t pipe,
  223. void *buffer,
  224. int nbytes,
  225. int timeout)
  226. {
  227. if (pipe == RT_NULL)
  228. return -1;
  229. if (pipe->ifinst == RT_NULL)
  230. return -1;
  231. if (pipe->ifinst->uinst == RT_NULL)
  232. return -1;
  233. if (pipe->ifinst->uinst->status == UINST_STATUS_IDLE)
  234. return -1;
  235. return hcd->ops->bulk_xfer(pipe, buffer, nbytes, timeout);
  236. }
  237. rt_inline int rt_usb_hcd_control_xfer(uhcd_t hcd,
  238. uinst_t uinst,
  239. ureq_t setup,
  240. void *buffer,
  241. int nbytes,
  242. int timeout)
  243. {
  244. if (uinst->status == UINST_STATUS_IDLE)
  245. return -1;
  246. return hcd->ops->ctl_xfer(uinst, setup, buffer, nbytes, timeout);
  247. }
  248. rt_inline int rt_usb_hcd_int_xfer(uhcd_t hcd,
  249. upipe_t pipe,
  250. void *buffer,
  251. int nbytes,
  252. int timeout)
  253. {
  254. if (pipe == RT_NULL)
  255. return -1;
  256. if (pipe->ifinst == RT_NULL)
  257. return -1;
  258. if (pipe->ifinst->uinst == RT_NULL)
  259. return -1;
  260. if (pipe->ifinst->uinst->status == UINST_STATUS_IDLE)
  261. return -1;
  262. return hcd->ops->int_xfer(pipe, buffer, nbytes, timeout);
  263. }
  264. rt_inline rt_err_t rt_usb_hcd_hub_control(uhcd_t hcd,
  265. rt_uint16_t port,
  266. rt_uint8_t cmd,
  267. void *args)
  268. {
  269. return hcd->ops->hub_ctrl(port, cmd, args);
  270. }
  271. #ifdef __cplusplus
  272. }
  273. #endif
  274. #endif