1 /*
2 * Copyright (c) 2022, sakumisu
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #ifndef USBH_CORE_H
7 #define USBH_CORE_H
8
9 #include <stdbool.h>
10 #include <string.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13
14 #include "usb_config.h"
15 #include "usb_util.h"
16 #include "usb_errno.h"
17 #include "usb_def.h"
18 #include "usb_list.h"
19 #include "usb_mem.h"
20 #include "usb_log.h"
21 #include "usb_hc.h"
22 #include "usb_osal.h"
23 #include "usb_hub.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #define USB_CLASS_MATCH_VENDOR 0x0001
30 #define USB_CLASS_MATCH_PRODUCT 0x0002
31 #define USB_CLASS_MATCH_INTF_CLASS 0x0004
32 #define USB_CLASS_MATCH_INTF_SUBCLASS 0x0008
33 #define USB_CLASS_MATCH_INTF_PROTOCOL 0x0010
34
35 #define CLASS_CONNECT(hport, i) ((hport)->config.intf[i].class_driver->connect(hport, i))
36 #define CLASS_DISCONNECT(hport, i) ((hport)->config.intf[i].class_driver->disconnect(hport, i))
37
38 #ifdef __ARMCC_VERSION /* ARM C Compiler */
39 #define CLASS_INFO_DEFINE __attribute__((section("usbh_class_info"))) __USED __ALIGNED(1)
40 #elif defined(__GNUC__)
41 #define CLASS_INFO_DEFINE __attribute__((section(".usbh_class_info"))) __USED __ALIGNED(1)
42 #elif defined(__ICCARM__) || defined(__ICCRX__)
43 #pragma section = "usbh_class_info"
44 #define CLASS_INFO_DEFINE __attribute__((section("usbh_class_info"))) __USED __ALIGNED(1)
45 #endif
46
usbh_control_urb_fill(struct usbh_urb * urb,usbh_pipe_t pipe,struct usb_setup_packet * setup,uint8_t * transfer_buffer,uint32_t transfer_buffer_length,uint32_t timeout,usbh_complete_callback_t complete,void * arg)47 static inline void usbh_control_urb_fill(struct usbh_urb *urb,
48 usbh_pipe_t pipe,
49 struct usb_setup_packet *setup,
50 uint8_t *transfer_buffer,
51 uint32_t transfer_buffer_length,
52 uint32_t timeout,
53 usbh_complete_callback_t complete,
54 void *arg)
55 {
56 urb->pipe = pipe;
57 urb->setup = setup;
58 urb->transfer_buffer = transfer_buffer;
59 urb->transfer_buffer_length = transfer_buffer_length;
60 urb->timeout = timeout;
61 urb->complete = complete;
62 urb->arg = arg;
63 }
64
usbh_bulk_urb_fill(struct usbh_urb * urb,usbh_pipe_t pipe,uint8_t * transfer_buffer,uint32_t transfer_buffer_length,uint32_t timeout,usbh_complete_callback_t complete,void * arg)65 static inline void usbh_bulk_urb_fill(struct usbh_urb *urb,
66 usbh_pipe_t pipe,
67 uint8_t *transfer_buffer,
68 uint32_t transfer_buffer_length,
69 uint32_t timeout,
70 usbh_complete_callback_t complete,
71 void *arg)
72 {
73 urb->pipe = pipe;
74 urb->setup = NULL;
75 urb->transfer_buffer = transfer_buffer;
76 urb->transfer_buffer_length = transfer_buffer_length;
77 urb->timeout = timeout;
78 urb->complete = complete;
79 urb->arg = arg;
80 }
81
usbh_int_urb_fill(struct usbh_urb * urb,usbh_pipe_t pipe,uint8_t * transfer_buffer,uint32_t transfer_buffer_length,uint32_t timeout,usbh_complete_callback_t complete,void * arg)82 static inline void usbh_int_urb_fill(struct usbh_urb *urb,
83 usbh_pipe_t pipe,
84 uint8_t *transfer_buffer,
85 uint32_t transfer_buffer_length,
86 uint32_t timeout,
87 usbh_complete_callback_t complete,
88 void *arg)
89 {
90 urb->pipe = pipe;
91 urb->setup = NULL;
92 urb->transfer_buffer = transfer_buffer;
93 urb->transfer_buffer_length = transfer_buffer_length;
94 urb->timeout = timeout;
95 urb->complete = complete;
96 urb->arg = arg;
97 }
98
99 struct usbh_class_info {
100 uint8_t match_flags; /* Used for product specific matches; range is inclusive */
101 uint8_t class; /* Base device class code */
102 uint8_t subclass; /* Sub-class, depends on base class. Eg. */
103 uint8_t protocol; /* Protocol, depends on base class. Eg. */
104 uint16_t vid; /* Vendor ID (for vendor/product specific devices) */
105 uint16_t pid; /* Product ID (for vendor/product specific devices) */
106 const struct usbh_class_driver *class_driver;
107 };
108
109 struct usbh_hubport;
110 struct usbh_class_driver {
111 const char *driver_name;
112 int (*connect)(struct usbh_hubport *hport, uint8_t intf);
113 int (*disconnect)(struct usbh_hubport *hport, uint8_t intf);
114 };
115
116 struct usbh_endpoint {
117 struct usb_endpoint_descriptor ep_desc;
118 };
119
120 struct usbh_interface_altsetting {
121 struct usb_interface_descriptor intf_desc;
122 struct usbh_endpoint ep[CONFIG_USBHOST_MAX_ENDPOINTS];
123 };
124
125 struct usbh_interface {
126 char devname[CONFIG_USBHOST_DEV_NAMELEN];
127 struct usbh_class_driver *class_driver;
128 void *priv;
129 struct usbh_interface_altsetting altsetting[CONFIG_USBHOST_MAX_INTF_ALTSETTINGS];
130 uint8_t altsetting_num;
131 };
132
133 struct usbh_configuration {
134 struct usb_configuration_descriptor config_desc;
135 struct usbh_interface intf[CONFIG_USBHOST_MAX_INTERFACES];
136 };
137
138 struct usbh_hubport {
139 bool connected; /* True: device connected; false: disconnected */
140 uint8_t port; /* Hub port index */
141 uint8_t dev_addr; /* device address */
142 uint8_t speed; /* device speed */
143 usbh_pipe_t ep0; /* control ep pipe info */
144 struct usb_device_descriptor device_desc;
145 struct usbh_configuration config;
146 const char *iManufacturer;
147 const char *iProduct;
148 const char *iSerialNumber;
149 uint8_t *raw_config_desc;
150 struct usb_setup_packet *setup;
151 struct usbh_hub *parent;
152 #ifdef CONFIG_USBHOST_XHCI
153 uint32_t protocol; /* port protocol, for xhci, some ports are USB2.0, others are USB3.0 */
154 #endif
155 usb_osal_thread_t thread;
156 };
157
158 struct usbh_hub {
159 usb_slist_t list;
160 bool connected;
161 bool is_roothub;
162 uint8_t index;
163 uint8_t hub_addr;
164 usbh_pipe_t intin;
165 uint8_t *int_buffer;
166 struct usbh_urb intin_urb;
167 struct usb_hub_descriptor hub_desc;
168 struct usbh_hubport child[CONFIG_USBHOST_MAX_EHPORTS];
169 struct usbh_hubport *parent;
170 };
171
172 /**
173 * @brief Activates an endpoint for a USB host pipe on a specific hub port.
174 *
175 * This function is responsible for activating the specified endpoint
176 * described by the given endpoint descriptor on the USB host pipe.
177 * @param pipe Pointer to the USB host pipe structure.
178 * @param hport Pointer to the USB hub port structure.
179 * @param ep_desc Pointer to the USB endpoint descriptor.
180 * @return On success will return 0, and others indicate fail.
181 */
182 int usbh_hport_activate_epx(usbh_pipe_t *pipe, struct usbh_hubport *hport, struct usb_endpoint_descriptor *ep_desc);
183
184 /**
185 * @brief Submit an control transfer to an endpoint.
186 * This is a blocking method; this method will not return until the transfer has completed.
187 * Default timeout is 500ms.
188 *
189 * @param pipe The control endpoint to send/receive the control request.
190 * @param setup Setup packet to be sent.
191 * @param buffer buffer used for sending the request and for returning any responses.
192 * @return On success will return 0, and others indicate fail.
193 */
194 int usbh_control_transfer(usbh_pipe_t pipe, struct usb_setup_packet *setup, uint8_t *buffer);
195
196 /**
197 * @brief Retrieves a USB string descriptor from a specific hub port.
198 *
199 * This function is responsible for retrieving the USB string descriptor
200 * with the specified index from the USB device connected to the given hub port.
201 * The retrieved descriptor is stored in the output buffer provided.
202 *
203 * @param hport Pointer to the USB hub port structure.
204 * @param index Index of the string descriptor to retrieve.
205 * @param output Pointer to the buffer where the retrieved descriptor will be stored.
206 * @return On success will return 0, and others indicate fail.
207 */
208 int usbh_get_string_desc(struct usbh_hubport *hport, uint8_t index, uint8_t *output);
209
210 /**
211 * @brief Sets the alternate setting for a USB interface on a specific hub port.
212 *
213 * This function is responsible for setting the alternate setting of the
214 * specified USB interface on the USB device connected to the given hub port.
215 * The interface and alternate setting are identified by the respective parameters.
216 *
217 * @param hport Pointer to the USB hub port structure.
218 * @param intf Interface number to set the alternate setting for.
219 * @param altsetting Alternate setting value to set for the interface.
220 * @return On success will return 0, and others indicate fail.
221 */
222 int usbh_set_interface(struct usbh_hubport *hport, uint8_t intf, uint8_t altsetting);
223
224 int usbh_initialize(void);
225 void *usbh_find_class_instance(const char *devname);
226
227 int lsusb(int argc, char **argv);
228
229 #ifdef __cplusplus
230 }
231 #endif
232
233 #endif /* USBH_CORE_H */
234