1 /*
2 * Copyright (c) 2006-2018, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2011-3-12 Yi Qiu first version
9 */
10
11 #ifndef __RT_USB_HOST_H__
12 #define __RT_USB_HOST_H__
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #include <rtthread.h>
19 #include "usb_common.h"
20
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
26 #define DEV_STATUS_IDLE 0x00
27 #define DEV_STATUS_BUSY 0x01
28 #define DEV_STATUS_ERROR 0x02
29
30 #define UPIPE_STATUS_OK 0x00
31 #define UPIPE_STATUS_STALL 0x01
32 #define UPIPE_STATUS_ERROR 0x02
33
34 #define USBH_PID_SETUP 0x00
35 #define USBH_PID_DATA 0x01
36
37 struct uhcd;
38 struct uhintf;
39 struct uhub;
40 struct upipe;
41
42 struct uclass_driver
43 {
44 rt_list_t list;
45 int class_code;
46 int subclass_code;
47
48 rt_err_t (*enable)(void* arg);
49 rt_err_t (*disable)(void* arg);
50
51 void* user_data;
52 };
53 typedef struct uclass_driver* ucd_t;
54
55 struct uprotocal
56 {
57 rt_list_t list;
58 int pro_id;
59
60 rt_err_t (*init)(void* arg);
61 rt_err_t (*callback)(void* arg);
62 };
63 typedef struct uprotocal* uprotocal_t;
64
65 struct uinstance
66 {
67 struct rt_device parent;
68
69 struct udevice_descriptor dev_desc;
70 ucfg_desc_t cfg_desc;
71 struct uhcd *hcd;
72
73 struct upipe * pipe_ep0_out;
74 struct upipe * pipe_ep0_in;
75 rt_list_t pipe;
76
77 rt_uint8_t status;
78 rt_uint8_t type;
79 rt_uint8_t index;
80 rt_uint8_t address;
81 rt_uint8_t speed;
82 rt_uint8_t max_packet_size;
83 rt_uint8_t port;
84
85 struct uhub* parent_hub;
86 struct uhintf* intf[USB_MAX_INTERFACE];
87 };
88 typedef struct uinstance* uinst_t;
89
90 struct uhintf
91 {
92 struct uinstance* device;
93 uintf_desc_t intf_desc;
94
95 ucd_t drv;
96 void *user_data;
97 };
98
99 struct upipe
100 {
101 rt_list_t list;
102 rt_uint8_t pipe_index;
103 rt_uint32_t status;
104 struct uendpoint_descriptor ep;
105 uinst_t inst;
106 func_callback callback;
107 void* user_data;
108 };
109 typedef struct upipe* upipe_t;
110
111 struct uhub
112 {
113 struct uhub_descriptor hub_desc;
114 rt_uint8_t num_ports;
115 rt_uint32_t port_status[USB_HUB_PORT_NUM];
116 struct uinstance* child[USB_HUB_PORT_NUM];
117
118 rt_bool_t is_roothub;
119
120 rt_uint8_t buffer[8];
121 struct uinstance* self;
122 struct uhcd *hcd;
123 };
124 typedef struct uhub* uhub_t;
125
126 struct uhcd_ops
127 {
128 rt_err_t (*reset_port) (rt_uint8_t port);
129 int (*pipe_xfer) (upipe_t pipe, rt_uint8_t token, void* buffer, int nbytes, int timeout);
130 rt_err_t (*open_pipe) (upipe_t pipe);
131 rt_err_t (*close_pipe) (upipe_t pipe);
132 };
133 typedef struct uhcd_ops* uhcd_ops_t;
134 struct uhcd
135 {
136 struct rt_device parent;
137 uhcd_ops_t ops;
138 rt_uint8_t num_ports;
139 uhub_t roothub;
140 };
141 typedef struct uhcd* uhcd_t;
142
143 enum uhost_msg_type
144 {
145 USB_MSG_CONNECT_CHANGE,
146 USB_MSG_CALLBACK,
147 };
148 typedef enum uhost_msg_type uhost_msg_type;
149
150 struct uhost_msg
151 {
152 uhost_msg_type type;
153 union
154 {
155 struct uhub* hub;
156 struct
157 {
158 func_callback function;
159 void *context;
160 }cb;
161 }content;
162 };
163 typedef struct uhost_msg* uhost_msg_t;
164
165 /* usb host system interface */
166 rt_err_t rt_usb_host_init(void);
167 void rt_usbh_hub_init(struct uhcd *hcd);
168
169 /* usb host core interface */
170 struct uinstance* rt_usbh_alloc_instance(uhcd_t uhcd);
171 rt_err_t rt_usbh_attatch_instance(struct uinstance* device);
172 rt_err_t rt_usbh_detach_instance(struct uinstance* device);
173 rt_err_t rt_usbh_get_descriptor(struct uinstance* device, rt_uint8_t type, void* buffer, int nbytes);
174 rt_err_t rt_usbh_set_configure(struct uinstance* device, int config);
175 rt_err_t rt_usbh_set_address(struct uinstance* device);
176 rt_err_t rt_usbh_set_interface(struct uinstance* device, int intf);
177 rt_err_t rt_usbh_clear_feature(struct uinstance* device, int endpoint, int feature);
178 rt_err_t rt_usbh_get_interface_descriptor(ucfg_desc_t cfg_desc, int num, uintf_desc_t* intf_desc);
179 rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num, uep_desc_t* ep_desc);
180
181 /* usb class driver interface */
182 rt_err_t rt_usbh_class_driver_init(void);
183 rt_err_t rt_usbh_class_driver_register(ucd_t drv);
184 rt_err_t rt_usbh_class_driver_unregister(ucd_t drv);
185 rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args);
186 rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args);
187 ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code);
188
189 /* usb class driver implement */
190 ucd_t rt_usbh_class_driver_hub(void);
191 ucd_t rt_usbh_class_driver_storage(void);
192
193
194
195 /* usb hub interface */
196 rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer,
197 rt_size_t size);
198 rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer);
199 rt_err_t rt_usbh_hub_get_port_status(uhub_t uhub, rt_uint16_t port,
200 rt_uint32_t* buffer);
201 rt_err_t rt_usbh_hub_clear_port_feature(uhub_t uhub, rt_uint16_t port,
202 rt_uint16_t feature);
203 rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port,
204 rt_uint16_t feature);
205 rt_err_t rt_usbh_hub_reset_port(uhub_t uhub, rt_uint16_t port);
206 rt_err_t rt_usbh_event_signal(struct uhost_msg* msg);
207
208
209 void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS);
210 void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port);
211
212 /* usb host controller driver interface */
rt_usb_instance_add_pipe(uinst_t inst,upipe_t pipe)213 rt_inline rt_err_t rt_usb_instance_add_pipe(uinst_t inst, upipe_t pipe)
214 {
215 RT_ASSERT(inst != RT_NULL);
216 RT_ASSERT(pipe != RT_NULL);
217 rt_list_insert_before(&inst->pipe, &pipe->list);
218 return RT_EOK;
219 }
rt_usb_instance_find_pipe(uinst_t inst,rt_uint8_t ep_address)220 rt_inline upipe_t rt_usb_instance_find_pipe(uinst_t inst,rt_uint8_t ep_address)
221 {
222 rt_list_t * l;
223 for(l = inst->pipe.next;l != &inst->pipe;l = l->next)
224 {
225 if(rt_list_entry(l,struct upipe,list)->ep.bEndpointAddress == ep_address)
226 {
227 return rt_list_entry(l,struct upipe,list);
228 }
229 }
230 return RT_NULL;
231 }
rt_usb_hcd_alloc_pipe(uhcd_t hcd,upipe_t * pipe,uinst_t inst,uep_desc_t ep)232 rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd, upipe_t* pipe, uinst_t inst, uep_desc_t ep)
233 {
234 *pipe = (upipe_t)rt_malloc(sizeof(struct upipe));
235 if(*pipe == RT_NULL)
236 {
237 return RT_ERROR;
238 }
239 rt_memset(*pipe,0,sizeof(struct upipe));
240 (*pipe)->inst = inst;
241 rt_memcpy(&(*pipe)->ep,ep,sizeof(struct uendpoint_descriptor));
242 return hcd->ops->open_pipe(*pipe);
243 }
rt_usb_pipe_add_callback(upipe_t pipe,func_callback callback)244 rt_inline void rt_usb_pipe_add_callback(upipe_t pipe, func_callback callback)
245 {
246 pipe->callback = callback;
247 }
248
rt_usb_hcd_free_pipe(uhcd_t hcd,upipe_t pipe)249 rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
250 {
251 RT_ASSERT(pipe != RT_NULL);
252 hcd->ops->close_pipe(pipe);
253 rt_free(pipe);
254 return RT_EOK;
255 }
256
257 int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout);
rt_usb_hcd_setup_xfer(uhcd_t hcd,upipe_t pipe,ureq_t setup,int timeout)258 rt_inline int rt_usb_hcd_setup_xfer(uhcd_t hcd, upipe_t pipe, ureq_t setup, int timeout)
259 {
260 return hcd->ops->pipe_xfer(pipe, USBH_PID_SETUP, (void *)setup, 8, timeout);
261 }
262
263 #ifdef __cplusplus
264 }
265 #endif
266
267 #endif
268
269
270