usb_host.h 7.7 KB

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