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_log.h"
20 #include "usb_hc.h"
21 #include "usb_osal.h"
22 #include "usbh_hub.h"
23 #include "usb_memcpy.h"
24 #include "usb_dcache.h"
25 #include "usb_version.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define USB_CLASS_MATCH_VENDOR        0x0001
32 #define USB_CLASS_MATCH_PRODUCT       0x0002
33 #define USB_CLASS_MATCH_INTF_CLASS    0x0004
34 #define USB_CLASS_MATCH_INTF_SUBCLASS 0x0008
35 #define USB_CLASS_MATCH_INTF_PROTOCOL 0x0010
36 #define USB_CLASS_MATCH_VID_PID       (USB_CLASS_MATCH_VENDOR | USB_CLASS_MATCH_PRODUCT)
37 
38 #define CLASS_CONNECT(hport, i)    ((hport)->config.intf[i].class_driver->connect(hport, i))
39 #define CLASS_DISCONNECT(hport, i) ((hport)->config.intf[i].class_driver->disconnect(hport, i))
40 
41 #ifdef __ARMCC_VERSION /* ARM C Compiler */
42 #define CLASS_INFO_DEFINE __attribute__((section("usbh_class_info"))) __USED __ALIGNED(1)
43 #elif defined(__GNUC__)
44 #define CLASS_INFO_DEFINE __attribute__((section(".usbh_class_info"))) __USED __ALIGNED(1)
45 #elif defined(__ICCARM__) || defined(__ICCRX__) || defined(__ICCRISCV__)
46 #pragma section = ".usbh_class_info"
47 #define CLASS_INFO_DEFINE __attribute__((section(".usbh_class_info"))) __USED __ALIGNED(1)
48 #endif
49 
50 #define USBH_GET_URB_INTERVAL(interval, speed) (speed < USB_SPEED_HIGH ? (interval * 1000) : ((1 << (interval - 1)) * 125))
51 
52 #define USBH_EP_INIT(ep, ep_desc)                                            \
53     do {                                                                     \
54         ep = ep_desc;                                                        \
55         USB_LOG_INFO("Ep=%02x Attr=%02u Mps=%d Interval=%02u Mult=%02u\r\n", \
56                      ep_desc->bEndpointAddress,                              \
57                      ep_desc->bmAttributes,                                  \
58                      USB_GET_MAXPACKETSIZE(ep_desc->wMaxPacketSize),         \
59                      ep_desc->bInterval,                                     \
60                      USB_GET_MULT(ep_desc->wMaxPacketSize));                 \
61     } while (0)
62 
63 struct usbh_class_info {
64     uint8_t match_flags;           /* Used for product specific matches; range is inclusive */
65     uint8_t bInterfaceClass;       /* Base device class code */
66     uint8_t bInterfaceSubClass;    /* Sub-class, depends on base class. Eg. */
67     uint8_t bInterfaceProtocol;    /* Protocol, depends on base class. Eg. */
68     const uint16_t (*id_table)[2]; /* List of Vendor/Product ID pairs */
69     const struct usbh_class_driver *class_driver;
70 };
71 
72 struct usbh_hubport;
73 struct usbh_class_driver {
74     const char *driver_name;
75     int (*connect)(struct usbh_hubport *hport, uint8_t intf);
76     int (*disconnect)(struct usbh_hubport *hport, uint8_t intf);
77 };
78 
79 struct usbh_endpoint {
80     struct usb_endpoint_descriptor ep_desc;
81 };
82 
83 struct usbh_interface_altsetting {
84     struct usb_interface_descriptor intf_desc;
85     struct usbh_endpoint ep[CONFIG_USBHOST_MAX_ENDPOINTS];
86 };
87 
88 struct usbh_interface {
89     char devname[CONFIG_USBHOST_DEV_NAMELEN];
90     struct usbh_class_driver *class_driver;
91     void *priv;
92     struct usbh_interface_altsetting altsetting[CONFIG_USBHOST_MAX_INTF_ALTSETTINGS];
93     uint8_t altsetting_num;
94 };
95 
96 struct usbh_configuration {
97     struct usb_configuration_descriptor config_desc;
98     struct usbh_interface intf[CONFIG_USBHOST_MAX_INTERFACES];
99 };
100 
101 struct usbh_hubport {
102     bool connected;   /* True: device connected; false: disconnected */
103     uint8_t port;     /* Hub port index */
104     uint8_t dev_addr; /* device address */
105     uint8_t speed;    /* device speed */
106     uint8_t depth;    /* distance from root hub */
107     uint8_t route;    /* route string */
108     uint8_t slot_id;  /* slot id */
109     struct usb_device_descriptor device_desc;
110     struct usbh_configuration config;
111     const char *iManufacturer;
112     const char *iProduct;
113     const char *iSerialNumber;
114     uint8_t *raw_config_desc;
115     struct usb_setup_packet *setup;
116     struct usbh_hub *parent;
117     struct usbh_hub *self; /* if this hubport is a hub */
118     struct usbh_bus *bus;
119     struct usb_endpoint_descriptor ep0;
120     struct usbh_urb ep0_urb;
121     usb_osal_mutex_t mutex;
122 };
123 
124 struct usbh_hub {
125     bool connected;
126     bool is_roothub;
127     uint8_t index;
128     uint8_t hub_addr;
129     uint8_t speed;
130     uint8_t nports;
131     uint8_t powerdelay;
132     uint8_t tt_think;
133     bool ismtt;
134     struct usb_hub_descriptor hub_desc; /* USB 2.0 only */
135     struct usb_hub_ss_descriptor hub_ss_desc; /* USB 3.0 only */
136     struct usbh_hubport child[CONFIG_USBHOST_MAX_EHPORTS];
137     struct usbh_hubport *parent;
138     struct usbh_bus *bus;
139     struct usb_endpoint_descriptor *intin;
140     struct usbh_urb intin_urb;
141     uint8_t *int_buffer;
142     struct usb_osal_timer *int_timer;
143 };
144 
145 struct usbh_devaddr_map {
146     /**
147      * alloctab[0]:addr from 0~31
148      * alloctab[1]:addr from 32~63
149      * alloctab[2]:addr from 64~95
150      * alloctab[3]:addr from 96~127
151      *
152      */
153     uint8_t next;         /* Next device address */
154     uint32_t alloctab[4]; /* Bit allocation table */
155 };
156 
157 struct usbh_hcd {
158     uintptr_t reg_base;
159     uint8_t hcd_id;
160     uint8_t roothub_intbuf[2]; /* at most 15 roothub ports */
161     struct usbh_hub roothub;
162 };
163 
164 struct usbh_bus {
165     usb_slist_t list;
166     uint8_t busid;
167     struct usbh_hcd hcd;
168     struct usbh_devaddr_map devgen;
169     usb_osal_thread_t hub_thread;
170     usb_osal_mq_t hub_mq;
171 };
172 
usbh_control_urb_fill(struct usbh_urb * urb,struct usbh_hubport * hport,struct usb_setup_packet * setup,uint8_t * transfer_buffer,uint32_t transfer_buffer_length,uint32_t timeout,usbh_complete_callback_t complete,void * arg)173 static inline void usbh_control_urb_fill(struct usbh_urb *urb,
174                                          struct usbh_hubport *hport,
175                                          struct usb_setup_packet *setup,
176                                          uint8_t *transfer_buffer,
177                                          uint32_t transfer_buffer_length,
178                                          uint32_t timeout,
179                                          usbh_complete_callback_t complete,
180                                          void *arg)
181 {
182     urb->hport = hport;
183     urb->ep = &hport->ep0;
184     urb->setup = setup;
185     urb->transfer_buffer = transfer_buffer;
186     urb->transfer_buffer_length = transfer_buffer_length;
187     urb->timeout = timeout;
188     urb->complete = complete;
189     urb->arg = arg;
190 }
191 
usbh_bulk_urb_fill(struct usbh_urb * urb,struct usbh_hubport * hport,struct usb_endpoint_descriptor * ep,uint8_t * transfer_buffer,uint32_t transfer_buffer_length,uint32_t timeout,usbh_complete_callback_t complete,void * arg)192 static inline void usbh_bulk_urb_fill(struct usbh_urb *urb,
193                                       struct usbh_hubport *hport,
194                                       struct usb_endpoint_descriptor *ep,
195                                       uint8_t *transfer_buffer,
196                                       uint32_t transfer_buffer_length,
197                                       uint32_t timeout,
198                                       usbh_complete_callback_t complete,
199                                       void *arg)
200 {
201     urb->hport = hport;
202     urb->ep = ep;
203     urb->setup = NULL;
204     urb->transfer_buffer = transfer_buffer;
205     urb->transfer_buffer_length = transfer_buffer_length;
206     urb->timeout = timeout;
207     urb->complete = complete;
208     urb->arg = arg;
209 }
210 
usbh_int_urb_fill(struct usbh_urb * urb,struct usbh_hubport * hport,struct usb_endpoint_descriptor * ep,uint8_t * transfer_buffer,uint32_t transfer_buffer_length,uint32_t timeout,usbh_complete_callback_t complete,void * arg)211 static inline void usbh_int_urb_fill(struct usbh_urb *urb,
212                                      struct usbh_hubport *hport,
213                                      struct usb_endpoint_descriptor *ep,
214                                      uint8_t *transfer_buffer,
215                                      uint32_t transfer_buffer_length,
216                                      uint32_t timeout,
217                                      usbh_complete_callback_t complete,
218                                      void *arg)
219 {
220     urb->hport = hport;
221     urb->ep = ep;
222     urb->setup = NULL;
223     urb->transfer_buffer = transfer_buffer;
224     urb->transfer_buffer_length = transfer_buffer_length;
225     urb->timeout = timeout;
226     urb->complete = complete;
227     urb->arg = arg;
228     urb->interval = USBH_GET_URB_INTERVAL(ep->bInterval, hport->speed);
229 }
230 
231 extern struct usbh_bus g_usbhost_bus[];
232 #ifdef USBH_IRQHandler
233 #error USBH_IRQHandler is obsolete, please call USBH_IRQHandler(xxx) in your irq
234 #endif
235 
236 /**
237  * @brief Submit an control transfer to an endpoint.
238  * This is a blocking method; this method will not return until the transfer has completed.
239  * Default timeout is 500ms.
240  *
241  * @param pipe The control endpoint to send/receive the control request.
242  * @param setup Setup packet to be sent.
243  * @param buffer buffer used for sending the request and for returning any responses.
244  * @return On success will return 0, and others indicate fail.
245  */
246 int usbh_control_transfer(struct usbh_hubport *hport, struct usb_setup_packet *setup, uint8_t *buffer);
247 
248 /**
249  * @brief Retrieves a USB string descriptor from a specific hub port.
250  *
251  * This function is responsible for retrieving the USB string descriptor
252  * with the specified index from the USB device connected to the given hub port.
253  * The retrieved descriptor is stored in the output buffer provided.
254  *
255  * @param hport Pointer to the USB hub port structure.
256  * @param index Index of the string descriptor to retrieve.
257  * @param output Pointer to the buffer where the retrieved descriptor will be stored.
258  * @param output_len Length of the output buffer.
259  * @return On success will return 0, and others indicate fail.
260  */
261 int usbh_get_string_desc(struct usbh_hubport *hport, uint8_t index, uint8_t *output, uint16_t output_len);
262 
263 /**
264  * @brief Sets the alternate setting for a USB interface on a specific hub port.
265  *
266  * This function is responsible for setting the alternate setting of the
267  * specified USB interface on the USB device connected to the given hub port.
268  * The interface and alternate setting are identified by the respective parameters.
269  *
270  * @param hport Pointer to the USB hub port structure.
271  * @param intf Interface number to set the alternate setting for.
272  * @param altsetting Alternate setting value to set for the interface.
273  * @return On success will return 0, and others indicate fail.
274  */
275 int usbh_set_interface(struct usbh_hubport *hport, uint8_t intf, uint8_t altsetting);
276 
277 int usbh_initialize(uint8_t busid, uintptr_t reg_base);
278 int usbh_deinitialize(uint8_t busid);
279 void *usbh_find_class_instance(const char *devname);
280 struct usbh_hubport *usbh_find_hubport(uint8_t busid, uint8_t hub_index, uint8_t hub_port);
281 
282 int lsusb(int argc, char **argv);
283 
284 #ifdef __cplusplus
285 }
286 #endif
287 
288 #endif /* USBH_CORE_H */
289