usb_host.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 DEV_STATUS_IDLE 0x00
  36. #define DEV_STATUS_BUSY 0x01
  37. #define DEV_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 uintf;
  43. struct uhub;
  44. struct uclass_driver
  45. {
  46. rt_list_t list;
  47. int class_code;
  48. int subclass_code;
  49. rt_err_t (*enable)(void* arg);
  50. rt_err_t (*disable)(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 uhub* parent;
  75. struct uintf* intf[USB_MAX_INTERFACE];
  76. };
  77. typedef struct uinstance* uinst_t;
  78. struct uintf
  79. {
  80. struct uinstance* device;
  81. uintf_desc_t intf_desc;
  82. ucd_t drv;
  83. void *user_data;
  84. };
  85. struct upipe
  86. {
  87. rt_uint32_t status;
  88. struct uendpoint_descriptor ep;
  89. struct uintf* intf;
  90. func_callback callback;
  91. void* user_data;
  92. };
  93. typedef struct upipe* upipe_t;
  94. struct uhub
  95. {
  96. struct uhub_descriptor hub_desc;
  97. rt_uint8_t num_ports;
  98. rt_uint32_t port_status[USB_HUB_PORT_NUM];
  99. struct uinstance* child[USB_HUB_PORT_NUM];
  100. rt_bool_t is_roothub;
  101. upipe_t pipe_in;
  102. rt_uint8_t buffer[8];
  103. struct uinstance* self;
  104. struct uhcd *hcd;
  105. };
  106. typedef struct uhub* uhub_t;
  107. struct uhcd_ops
  108. {
  109. int (*ctl_xfer)(struct uinstance* inst, ureq_t setup, void* buffer, int nbytes,
  110. int timeout);
  111. int (*bulk_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  112. int (*int_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  113. int (*iso_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  114. rt_err_t (*alloc_pipe)(struct upipe** pipe, struct uintf* intf, uep_desc_t ep,
  115. func_callback callback);
  116. rt_err_t (*free_pipe)(upipe_t pipe);
  117. rt_err_t (*hub_ctrl)(rt_uint16_t port, rt_uint8_t cmd, void *args);
  118. };
  119. struct uhcd
  120. {
  121. struct rt_device parent;
  122. struct uhcd_ops* ops;
  123. struct uhub* roothub;
  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 uhub* hub;
  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. rt_err_t rt_usb_host_init(void);
  148. void rt_usbh_hub_init(void);
  149. /* usb host core interface */
  150. struct uinstance* rt_usbh_alloc_instance(void);
  151. rt_err_t rt_usbh_attatch_instance(struct uinstance* device);
  152. rt_err_t rt_usbh_detach_instance(struct uinstance* device);
  153. rt_err_t rt_usbh_get_descriptor(struct uinstance* device, rt_uint8_t type, void* buffer,
  154. int nbytes);
  155. rt_err_t rt_usbh_set_configure(struct uinstance* device, int config);
  156. rt_err_t rt_usbh_set_address(struct uinstance* device);
  157. rt_err_t rt_usbh_set_interface(struct uinstance* device, int intf);
  158. rt_err_t rt_usbh_clear_feature(struct uinstance* device, int endpoint, int feature);
  159. rt_err_t rt_usbh_get_interface_descriptor(ucfg_desc_t cfg_desc, int num,
  160. uintf_desc_t* intf_desc);
  161. rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
  162. uep_desc_t* ep_desc);
  163. /* usb class driver interface */
  164. rt_err_t rt_usbh_class_driver_init(void);
  165. rt_err_t rt_usbh_class_driver_register(ucd_t drv);
  166. rt_err_t rt_usbh_class_driver_unregister(ucd_t drv);
  167. rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args);
  168. rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args);
  169. ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code);
  170. /* usb class driver implement */
  171. ucd_t rt_usbh_class_driver_hid(void);
  172. ucd_t rt_usbh_class_driver_hub(void);
  173. ucd_t rt_usbh_class_driver_storage(void);
  174. ucd_t rt_usbh_class_driver_adk(void);
  175. /* usb hid protocal implement */
  176. uprotocal_t rt_usbh_hid_protocal_kbd(void);
  177. uprotocal_t rt_usbh_hid_protocal_mouse(void);
  178. /* usb adk class driver interface */
  179. rt_err_t rt_usbh_adk_set_string(const char* manufacturer, const char* model,
  180. const char* description, const char* version, const char* uri,
  181. const char* serial);
  182. /* usb hub interface */
  183. rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer,
  184. rt_size_t size);
  185. rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint8_t* buffer);
  186. rt_err_t rt_usbh_hub_get_port_status(uhub_t uhub, rt_uint16_t port,
  187. rt_uint8_t* buffer);
  188. rt_err_t rt_usbh_hub_clear_port_feature(uhub_t uhub, rt_uint16_t port,
  189. rt_uint16_t feature);
  190. rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port,
  191. rt_uint16_t feature);
  192. rt_err_t rt_usbh_hub_reset_port(uhub_t uhub, rt_uint16_t port);
  193. rt_err_t rt_usbh_event_signal(struct uhost_msg* msg);
  194. /* usb host controller driver interface */
  195. rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd, upipe_t* pipe,
  196. struct uintf* intf, uep_desc_t ep, func_callback callback)
  197. {
  198. if(intf == RT_NULL) return -RT_EIO;
  199. return hcd->ops->alloc_pipe(pipe, intf, ep, callback);
  200. }
  201. rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
  202. {
  203. RT_ASSERT(pipe != RT_NULL);
  204. return hcd->ops->free_pipe(pipe);
  205. }
  206. rt_inline int rt_usb_hcd_bulk_xfer(uhcd_t hcd, upipe_t pipe, void* buffer,
  207. int nbytes, int timeout)
  208. {
  209. if(pipe == RT_NULL) return -1;
  210. if(pipe->intf == RT_NULL) return -1;
  211. if(pipe->intf->device == RT_NULL) return -1;
  212. if(pipe->intf->device->status == DEV_STATUS_IDLE)
  213. return -1;
  214. return hcd->ops->bulk_xfer(pipe, buffer, nbytes, timeout);
  215. }
  216. rt_inline int rt_usb_hcd_control_xfer(uhcd_t hcd, struct uinstance* device, ureq_t setup,
  217. void* buffer, int nbytes, int timeout)
  218. {
  219. if(device->status == DEV_STATUS_IDLE) return -1;
  220. return hcd->ops->ctl_xfer(device, setup, buffer, nbytes, timeout);
  221. }
  222. rt_inline int rt_usb_hcd_int_xfer(uhcd_t hcd, upipe_t pipe, void* buffer,
  223. int nbytes, int timeout)
  224. {
  225. if(pipe == RT_NULL) return -1;
  226. if(pipe->intf == RT_NULL) return -1;
  227. if(pipe->intf->device == RT_NULL) return -1;
  228. if(pipe->intf->device->status == DEV_STATUS_IDLE)
  229. return -1;
  230. return hcd->ops->int_xfer(pipe, buffer, nbytes, timeout);
  231. }
  232. rt_inline rt_err_t rt_usb_hcd_hub_control(uhcd_t hcd, rt_uint16_t port,
  233. rt_uint8_t cmd, void *args)
  234. {
  235. return hcd->ops->hub_ctrl(port, cmd, args);
  236. }
  237. #ifdef __cplusplus
  238. }
  239. #endif
  240. #endif