usb_device.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * File : usb_device.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, 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. * 2012-10-01 Yi Qiu first version
  23. * 2012-12-12 heyuanjie87 change endpoint and class handler
  24. */
  25. #ifndef __USB_DEVICE_H__
  26. #define __USB_DEVICE_H__
  27. #include <rtthread.h>
  28. #include "usb_common.h"
  29. /* Vendor ID */
  30. #ifdef USB_VENDOR_ID
  31. #define _VENDOR_ID USB_VENDOR_ID
  32. #else
  33. #define _VENDOR_ID 0x0EFF
  34. #endif
  35. /* Product ID */
  36. #ifdef USB_PRODUCT_ID
  37. #define _PRODUCT_ID USB_PRODUCT_ID
  38. #else
  39. #define _PRODUCT_ID 0x0001
  40. #endif
  41. #define USB_BCD_DEVICE 0x0200 /* USB Specification Release Number in Binary-Coded Decimal */
  42. #define USB_BCD_VERSION 0x0200 /* USB 2.0 */
  43. struct uclass;
  44. struct udevice;
  45. struct uendpoint;
  46. struct udcd_ops
  47. {
  48. rt_err_t (*set_address)(rt_uint8_t value);
  49. rt_err_t (*clear_feature)(rt_uint16_t value, rt_uint16_t index);
  50. rt_err_t (*set_feature)(rt_uint16_t value, rt_uint16_t index);
  51. rt_err_t (*ep_alloc)(struct uendpoint* ep);
  52. rt_err_t (*ep_free)(struct uendpoint* ep);
  53. rt_err_t (*ep_stall)(struct uendpoint* ep);
  54. rt_err_t (*ep_run)(struct uendpoint* ep);
  55. rt_err_t (*ep_stop)(struct uendpoint* ep);
  56. rt_err_t (*ep_read)(struct uendpoint* ep, void *buffer, rt_size_t size);
  57. rt_size_t (*ep_write)(struct uendpoint* ep, void *buffer, rt_size_t size);
  58. rt_err_t (*send_status)(void);
  59. };
  60. struct udcd
  61. {
  62. struct rt_device parent;
  63. struct udcd_ops* ops;
  64. struct rt_completion completion;
  65. };
  66. typedef struct udcd* udcd_t;
  67. typedef rt_err_t (*udep_handler_t)(struct udevice* device, struct uclass* cls, rt_size_t size);
  68. struct uendpoint
  69. {
  70. rt_list_t list;
  71. rt_uint8_t* buffer;
  72. uep_desc_t ep_desc;
  73. udep_handler_t handler;
  74. rt_bool_t is_stall;
  75. };
  76. typedef struct uendpoint* uep_t;
  77. struct ualtsetting
  78. {
  79. rt_list_t list;
  80. uintf_desc_t intf_desc;
  81. void* desc;
  82. rt_size_t desc_size;
  83. rt_list_t ep_list;
  84. };
  85. typedef struct ualtsetting* ualtsetting_t;
  86. typedef rt_err_t (*uintf_handler_t)(struct udevice* device, struct uclass* cls, ureq_t setup);
  87. struct uinterface
  88. {
  89. rt_list_t list;
  90. rt_uint8_t intf_num;
  91. ualtsetting_t curr_setting;
  92. rt_list_t setting_list;
  93. uintf_handler_t handler;
  94. };
  95. typedef struct uinterface* uintf_t;
  96. struct uclass_ops
  97. {
  98. rt_err_t (*run)(struct udevice* device, struct uclass* cls);
  99. rt_err_t (*stop)(struct udevice* device, struct uclass* cls);
  100. rt_err_t (*sof_handler)(struct udevice* device, struct uclass* cls);
  101. };
  102. typedef struct uclass_ops* uclass_ops_t;
  103. struct uclass
  104. {
  105. rt_list_t list;
  106. uclass_ops_t ops;
  107. void* eps;
  108. struct udevice* device;
  109. udev_desc_t dev_desc;
  110. void* user_data;
  111. rt_list_t intf_list;
  112. };
  113. typedef struct uclass* uclass_t;
  114. struct uconfig
  115. {
  116. rt_list_t list;
  117. struct uconfig_descriptor cfg_desc;
  118. rt_list_t cls_list;
  119. };
  120. typedef struct uconfig* uconfig_t;
  121. struct udevice
  122. {
  123. rt_list_t list;
  124. struct udevice_descriptor dev_desc;
  125. const char** str;
  126. udevice_state_t state;
  127. rt_list_t cfg_list;
  128. uconfig_t curr_cfg;
  129. rt_uint8_t nr_intf;
  130. udcd_t dcd;
  131. };
  132. typedef struct udevice* udevice_t;
  133. enum udev_msg_type
  134. {
  135. USB_MSG_SETUP_NOTIFY,
  136. USB_MSG_DATA_NOTIFY,
  137. USB_MSG_SOF,
  138. USB_MSG_RESET,
  139. /* we don't need to add a "PLUG_IN" event because after the cable is
  140. * plugged in(before any SETUP) the classed have nothing to do. If the host
  141. * is ready, it will send RESET and we will have USB_MSG_RESET. So, a RESET
  142. * should reset and run the class while plug_in is not. */
  143. USB_MSG_PLUG_OUT,
  144. };
  145. typedef enum udev_msg_type udev_msg_type;
  146. struct udev_msg
  147. {
  148. udev_msg_type type;
  149. udcd_t dcd;
  150. union
  151. {
  152. struct
  153. {
  154. rt_size_t size;
  155. rt_uint8_t ep_addr;
  156. } ep_msg;
  157. struct
  158. {
  159. rt_uint32_t* packet;
  160. } setup_msg;
  161. } content;
  162. };
  163. typedef struct udev_msg* udev_msg_t;
  164. udevice_t rt_usbd_device_create(void);
  165. uconfig_t rt_usbd_config_create(void);
  166. uclass_t rt_usbd_class_create(udevice_t device,
  167. udev_desc_t dev_desc,
  168. uclass_ops_t ops);
  169. uintf_t rt_usbd_interface_create(udevice_t device, uintf_handler_t handler);
  170. uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler);
  171. ualtsetting_t rt_usbd_altsetting_create(rt_size_t desc_size);
  172. rt_err_t rt_usbd_core_init(void);
  173. rt_err_t rt_usb_device_init(const char *udc_name);
  174. rt_err_t rt_usbd_post_event(struct udev_msg *msg, rt_size_t size);
  175. rt_err_t rt_usbd_free_device(udevice_t device);
  176. rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd);
  177. rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc);
  178. rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring);
  179. rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg);
  180. rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls);
  181. rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf);
  182. rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting);
  183. rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep);
  184. rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting,
  185. const void *desc,
  186. rt_off_t intf_pos);
  187. rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value);
  188. rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value);
  189. udevice_t rt_usbd_find_device(udcd_t dcd);
  190. uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value);
  191. uintf_t rt_usbd_find_interface(udevice_t device,
  192. rt_uint8_t value,
  193. uclass_t *pcls);
  194. uep_t rt_usbd_find_endpoint(udevice_t device,
  195. uclass_t *pcls,
  196. rt_uint8_t ep_addr);
  197. uclass_t rt_usbd_class_mstorage_create(udevice_t device);
  198. uclass_t rt_usbd_class_cdc_create(udevice_t device);
  199. uclass_t rt_usbd_class_rndis_create(udevice_t device);
  200. uclass_t rt_usbd_class_dap_create(udevice_t device);
  201. #ifdef RT_USB_DEVICE_COMPOSITE
  202. rt_err_t rt_usbd_class_set_iad(uclass_t cls, uiad_desc_t iad_desc);
  203. #endif
  204. rt_inline rt_err_t dcd_set_address(udcd_t dcd, rt_uint8_t value)
  205. {
  206. RT_ASSERT(dcd != RT_NULL);
  207. return dcd->ops->set_address(value);
  208. }
  209. rt_inline rt_err_t dcd_clear_feature(udcd_t dcd,
  210. rt_uint16_t value,
  211. rt_uint16_t index)
  212. {
  213. RT_ASSERT(dcd != RT_NULL);
  214. return dcd->ops->clear_feature(value, index);
  215. }
  216. rt_inline rt_err_t dcd_set_feature(udcd_t dcd,
  217. rt_uint8_t value,
  218. rt_uint16_t index)
  219. {
  220. RT_ASSERT(dcd != RT_NULL);
  221. return dcd->ops->set_feature(value, index);
  222. }
  223. rt_inline rt_err_t dcd_ep_stall(udcd_t dcd, uep_t ep)
  224. {
  225. RT_ASSERT(dcd != RT_NULL);
  226. return dcd->ops->ep_stall(ep);
  227. }
  228. rt_inline rt_uint8_t dcd_ep_alloc(udcd_t dcd, uep_t ep)
  229. {
  230. RT_ASSERT(dcd != RT_NULL);
  231. return dcd->ops->ep_alloc(ep);
  232. }
  233. rt_inline rt_err_t dcd_ep_free(udcd_t dcd, uep_t ep)
  234. {
  235. RT_ASSERT(dcd != RT_NULL);
  236. return dcd->ops->ep_free(ep);
  237. }
  238. rt_inline rt_err_t dcd_ep_run(udcd_t dcd, uep_t ep)
  239. {
  240. RT_ASSERT(dcd != RT_NULL);
  241. return dcd->ops->ep_run(ep);
  242. }
  243. rt_inline rt_err_t dcd_ep_stop(udcd_t dcd, uep_t ep)
  244. {
  245. RT_ASSERT(dcd != RT_NULL);
  246. return dcd->ops->ep_stop(ep);
  247. }
  248. rt_inline rt_err_t dcd_ep_read(udcd_t dcd, uep_t ep, void *buffer,
  249. rt_size_t size)
  250. {
  251. RT_ASSERT(dcd != RT_NULL);
  252. return dcd->ops->ep_read(ep, buffer, size);
  253. }
  254. rt_inline rt_size_t dcd_ep_write(udcd_t dcd,
  255. uep_t ep,
  256. void *buffer,
  257. rt_size_t size)
  258. {
  259. RT_ASSERT(dcd != RT_NULL);
  260. return dcd->ops->ep_write(ep, buffer, size);
  261. }
  262. rt_inline rt_err_t dcd_send_status(udcd_t dcd)
  263. {
  264. RT_ASSERT(dcd != RT_NULL);
  265. return dcd->ops->send_status();
  266. }
  267. #endif