M480 BSP V3.05.005
The Board Support Package for M480 Series
usb.h
Go to the documentation of this file.
1/**************************************************************************/
10#ifndef _USBH_H_
11#define _USBH_H_
12
13#include "config.h"
14#include "usbh_lib.h"
15#include "ehci.h"
16#include "ohci.h"
17
19
20struct utr_t;
21struct udev_t;
22struct hub_dev_t;
23struct iface_t;
24struct ep_info_t;
25
26/*----------------------------------------------------------------------------------*/
27/* USB device request setup packet */
28/*----------------------------------------------------------------------------------*/
29#ifdef __ICCARM__
30typedef struct
31{
32 __packed uint8_t bmRequestType;
33 __packed uint8_t bRequest;
34 __packed uint16_t wValue;
35 __packed uint16_t wIndex;
36 __packed uint16_t wLength;
37} DEV_REQ_T;
38#else
39typedef struct __attribute__((__packed__))
40{
41 uint8_t bmRequestType;
42 uint8_t bRequest;
43 uint16_t wValue;
44 uint16_t wIndex;
45 uint16_t wLength;
46}
47DEV_REQ_T;
48#endif
49
50/*
51 * bmRequestType[7] - Data transfer direction
52 */
53#define REQ_TYPE_OUT 0x00
54#define REQ_TYPE_IN 0x80
55/*
56 * bmRequestType[6:5] - Type
57 */
58#define REQ_TYPE_STD_DEV 0x00
59#define REQ_TYPE_CLASS_DEV 0x20
60#define REQ_TYPE_VENDOR_DEV 0x40
61/*
62 * bmRequestType[4:0] - Recipient
63 */
64#define REQ_TYPE_TO_DEV 0x00
65#define REQ_TYPE_TO_IFACE 0x01
66#define REQ_TYPE_TO_EP 0x02
67#define REQ_TYPE_TO_OTHER 0x03
68/*
69 * Standard Requests
70 */
71#define USB_REQ_GET_STATUS 0x00
72#define USB_REQ_CLEAR_FEATURE 0x01
73#define USB_REQ_SET_FEATURE 0x03
74#define USB_REQ_SET_ADDRESS 0x05
75#define USB_REQ_GET_DESCRIPTOR 0x06
76#define USB_REQ_SET_CONFIGURATION 0x09
77#define USB_REQ_SET_INTERFACE 0x0B
78/*
79 * Descriptor Types
80 */
81#define USB_DT_STANDARD 0x00
82#define USB_DT_CLASS 0x20
83#define USB_DT_VENDOR 0x40
84
85#define USB_DT_DEVICE 0x01
86#define USB_DT_CONFIGURATION 0x02
87#define USB_DT_STRING 0x03
88#define USB_DT_INTERFACE 0x04
89#define USB_DT_ENDPOINT 0x05
90#define USB_DT_DEVICE_QUALIFIER 0x06
91#define USB_DT_OTHER_SPEED_CONF 0x07
92#define USB_DT_IFACE_POWER 0x08
93
94
95
96/*----------------------------------------------------------------------------------*/
97/* USB standard descriptors */
98/*----------------------------------------------------------------------------------*/
99
100/* Descriptor header */
101#ifdef __ICCARM__
102typedef struct
103{
104 __packed uint8_t bLength;
105 __packed uint8_t bDescriptorType;
106} DESC_HDR_T;
107#else
108typedef struct __attribute__((__packed__))
109{
110 uint8_t bLength;
111 uint8_t bDescriptorType;
112}
113DESC_HDR_T;
114#endif
115
116/*----------------------------------------------------------------------------------*/
117/* USB device descriptor */
118/*----------------------------------------------------------------------------------*/
119#ifdef __ICCARM__
120typedef struct
121{
122 __packed uint8_t bLength;
123 __packed uint8_t bDescriptorType;
124 __packed uint16_t bcdUSB;
125 __packed uint8_t bDeviceClass;
126 __packed uint8_t bDeviceSubClass;
127 __packed uint8_t bDeviceProtocol;
128 __packed uint8_t bMaxPacketSize0;
129 __packed uint16_t idVendor;
130 __packed uint16_t idProduct;
131 __packed uint16_t bcdDevice;
132 __packed uint8_t iManufacturer;
133 __packed uint8_t iProduct;
134 __packed uint8_t iSerialNumber;
135 __packed uint8_t bNumConfigurations;
136} DESC_DEV_T;
137#else
138/*----------------------------------------------------------------------------------*/
139/* USB device descriptor */
140/*----------------------------------------------------------------------------------*/
141typedef struct __attribute__((__packed__))
142{
143 uint8_t bLength;
144 uint8_t bDescriptorType;
145 uint16_t bcdUSB;
146 uint8_t bDeviceClass;
147 uint8_t bDeviceSubClass;
148 uint8_t bDeviceProtocol;
149 uint8_t bMaxPacketSize0;
150 uint16_t idVendor;
151 uint16_t idProduct;
152 uint16_t bcdDevice;
153 uint8_t iManufacturer;
154 uint8_t iProduct;
155 uint8_t iSerialNumber;
156 uint8_t bNumConfigurations;
157}
158DESC_DEV_T;
159#endif
160
161/*
162 * Configuration Descriptor
163 */
164#ifdef __ICCARM__
165typedef struct usb_config_descriptor
166{
167 __packed uint8_t bLength;
168 __packed uint8_t bDescriptorType;
169 __packed uint16_t wTotalLength;
170 __packed uint8_t bNumInterfaces;
171 __packed uint8_t bConfigurationValue;
172 __packed uint8_t iConfiguration;
173 __packed uint8_t bmAttributes;
174 __packed uint8_t MaxPower;
175} DESC_CONF_T;
176#else
177typedef struct __attribute__((__packed__)) usb_config_descriptor
178{
179 uint8_t bLength;
180 uint8_t bDescriptorType;
181 uint16_t wTotalLength;
182 uint8_t bNumInterfaces;
183 uint8_t bConfigurationValue;
184 uint8_t iConfiguration;
185 uint8_t bmAttributes;
186 uint8_t MaxPower;
187} DESC_CONF_T;
188#endif
189
190/*
191 * Interface Descriptor
192 */
193#ifdef __ICCARM__
194typedef struct usb_interface_descriptor
195{
196 __packed uint8_t bLength;
197 __packed uint8_t bDescriptorType;
198 __packed uint8_t bInterfaceNumber;
199 __packed uint8_t bAlternateSetting;
200 __packed uint8_t bNumEndpoints;
201 __packed uint8_t bInterfaceClass;
202 __packed uint8_t bInterfaceSubClass;
203 __packed uint8_t bInterfaceProtocol;
204 __packed uint8_t iInterface;
205} DESC_IF_T;
206#else
207typedef struct __attribute__((__packed__)) usb_interface_descriptor
208{
209 uint8_t bLength;
210 uint8_t bDescriptorType;
211 uint8_t bInterfaceNumber;
212 uint8_t bAlternateSetting;
213 uint8_t bNumEndpoints;
214 uint8_t bInterfaceClass;
215 uint8_t bInterfaceSubClass;
216 uint8_t bInterfaceProtocol;
217 uint8_t iInterface;
218} DESC_IF_T;
219#endif
220
221/*
222 * Interface descriptor bInterfaceClass[7:0]
223 */
224#define USB_CLASS_AUDIO 0x01
225#define USB_CLASS_COMM 0x02
226#define USB_CLASS_HID 0x03
227#define USB_CLASS_PRINTER 0x07
228#define USB_CLASS_MASS_STORAGE 0x08
229#define USB_CLASS_HUB 0x09
230#define USB_CLASS_DATA 0x0A
231#define USB_CLASS_VIDEO 0x0E
232
233/*
234 * Endpoint Descriptor
235 */
236#ifdef __ICCARM__
237typedef struct usb_endpoint_descriptor
238{
239 __packed uint8_t bLength;
240 __packed uint8_t bDescriptorType;
241 __packed uint8_t bEndpointAddress;
242 __packed uint8_t bmAttributes;
243 __packed uint16_t wMaxPacketSize;
244 __packed uint8_t bInterval;
245 __packed uint8_t bRefresh;
246 __packed uint8_t bSynchAddress;
247} DESC_EP_T;
248#else
249typedef struct __attribute__((__packed__)) usb_endpoint_descriptor
250{
251 uint8_t bLength;
252 uint8_t bDescriptorType;
253 uint8_t bEndpointAddress;
254 uint8_t bmAttributes;
255 uint16_t wMaxPacketSize;
256 uint8_t bInterval;
257 uint8_t bRefresh;
258 uint8_t bSynchAddress;
259} DESC_EP_T;
260#endif
261
262/*
263 * Endpoint descriptor bEndpointAddress[7] - direction
264 */
265#define EP_ADDR_DIR_MASK 0x80
266#define EP_ADDR_DIR_IN 0x80
267#define EP_ADDR_DIR_OUT 0x00
268
269/*
270 * Endpoint descriptor bmAttributes[1:0] - transfer type
271 */
272#define EP_ATTR_TT_MASK 0x03
273#define EP_ATTR_TT_CTRL 0x00
274#define EP_ATTR_TT_ISO 0x01
275#define EP_ATTR_TT_BULK 0x02
276#define EP_ATTR_TT_INT 0x03
277
278
279/*----------------------------------------------------------------------------------*/
280/* USB Host controller driver */
281/*----------------------------------------------------------------------------------*/
282typedef struct
283{
284 int (*init) (void);
285 void (*shutdown) (void);
286 void (*suspend) (void);
287 void (*resume) (void);
288 int (*ctrl_xfer)(struct utr_t *utr);
289 int (*bulk_xfer)(struct utr_t *utr);
290 int (*int_xfer)(struct utr_t *utr);
291 int (*iso_xfer)(struct utr_t *utr);
292 int (*quit_xfer)(struct utr_t *utr, struct ep_info_t *ep);
293
294 /* root hub support */
295 int (*rthub_port_reset)(int port);
296 int (*rthub_polling) (void);
297} HC_DRV_T;
298
299
300/*----------------------------------------------------------------------------------*/
301/* USB device driver */
302/*----------------------------------------------------------------------------------*/
303typedef struct
304{
305 int (*probe) (struct iface_t *iface);
306 void (*disconnect) (struct iface_t *iface);
307 void (*suspend) (struct iface_t *iface);
308 void (*resume) (struct iface_t *iface);
309} UDEV_DRV_T;
310
311
312/*----------------------------------------------------------------------------------*/
313/* USB device */
314/*----------------------------------------------------------------------------------*/
315
316typedef enum
317{
318 SPEED_LOW,
319 SPEED_FULL,
320 SPEED_HIGH
321} SPEED_E;
322
323typedef struct ep_info_t
324{
325 uint8_t bEndpointAddress;
326 uint8_t bmAttributes;
327 uint8_t bInterval;
328 uint8_t bToggle;
329 uint16_t wMaxPacketSize;
330 void *hw_pipe;
331} EP_INFO_T;
332
333typedef struct udev_t
334{
335 DESC_DEV_T descriptor;
336 struct hub_dev_t *parent;
337 uint8_t port_num;
338 uint8_t dev_num;
339 int8_t cur_conf;
340 SPEED_E speed;
341 /*
342 * The followings are lightweight USB stack internal used .
343 */
344 uint8_t *cfd_buff;
345 EP_INFO_T ep0;
346 HC_DRV_T *hc_driver;
347 struct iface_t *iface_list;
348 struct udev_t *next;
349} UDEV_T;
350
351typedef struct alt_iface_t
352{
353 DESC_IF_T *ifd;
354 EP_INFO_T ep[MAX_EP_PER_IFACE];
355} ALT_IFACE_T;
356
357typedef struct iface_t
358{
359 UDEV_T *udev;
360 uint8_t if_num;
361 uint8_t num_alt;
362 ALT_IFACE_T *aif;
363 ALT_IFACE_T alt[MAX_ALT_PER_IFACE];
364 UDEV_DRV_T *driver;
365 void *context;
366 struct iface_t *next;
367} IFACE_T;
368
369
370/*----------------------------------------------------------------------------------*/
371/* URB (USB Request Block) */
372/*----------------------------------------------------------------------------------*/
373
374#define IF_PER_UTR 8 /* number of frames per UTR isochronous transfer (DO NOT modify it!) */
375
376typedef void (*FUNC_UTR_T)(struct utr_t *);
377
378typedef struct utr_t
379{
380 UDEV_T *udev;
381 DEV_REQ_T setup;
382 EP_INFO_T *ep;
383 uint8_t *buff;
384 uint8_t bIsTransferDone;
385 uint32_t data_len;
386 uint32_t xfer_len;
387 uint8_t bIsoNewSched;
388 uint16_t iso_sf;
389 uint16_t iso_xlen[IF_PER_UTR];
390 uint8_t * iso_buff[IF_PER_UTR];
391 int iso_status[IF_PER_UTR];
392 int td_cnt;
393 int status;
394 int interval;
395 void *context;
396 FUNC_UTR_T func;
397 struct utr_t *next; /* point to the next UTR of the same endpoint. \hideinitializer */
398} UTR_T;
399
400
401/*----------------------------------------------------------------------------------*/
402/* Global variables */
403/*----------------------------------------------------------------------------------*/
404extern USBH_T *_ohci;
405extern HSUSBH_T *_ehci;
406
407extern HC_DRV_T ohci_driver;
408extern HC_DRV_T ehci_driver;
409
410extern UDEV_T * g_udev_list;
411
412/*----------------------------------------------------------------------------------*/
413/* USB stack exported functions */
414/*----------------------------------------------------------------------------------*/
415extern void delay_us(int usec);
416
417extern void dump_ohci_regs(void);
418extern void dump_ohci_ports(void);
419extern void dump_ohci_int_table(void);
420extern void dump_ehci_regs(void);
421extern void dump_ehci_qtd(qTD_T *qtd);
422extern void dump_ehci_asynclist(void);
423extern void dump_ehci_period_frame_list_simple(void);
424extern void usbh_dump_buff_bytes(uint8_t *buff, int nSize);
425extern void usbh_dump_interface_descriptor(DESC_IF_T *if_desc);
426extern void usbh_dump_endpoint_descriptor(DESC_EP_T *ep_desc);
427extern void usbh_dump_iface(IFACE_T *iface);
428extern void usbh_dump_ep_info(EP_INFO_T *ep);
429
430/*
431 * Memory management functions
432 */
433extern void usbh_memory_init(void);
434extern uint32_t usbh_memory_used(void);
435extern void * usbh_alloc_mem(int size);
436extern void usbh_free_mem(void *p, int size);
437extern int alloc_dev_address(void);
438extern void free_dev_address(int dev_addr);
439extern UDEV_T * alloc_device(void);
440extern void free_device(UDEV_T *udev);
441extern UTR_T * alloc_utr(UDEV_T *udev);
442extern void free_utr(UTR_T *utr);
443extern ED_T * alloc_ohci_ED(void);
444extern void free_ohci_ED(ED_T *ed);
445extern TD_T * alloc_ohci_TD(UTR_T *utr);
446extern void free_ohci_TD(TD_T *td);
447extern QH_T * alloc_ehci_QH(void);
448extern void free_ehci_QH(QH_T *qh);
449extern qTD_T * alloc_ehci_qTD(UTR_T *utr);
450extern void free_ehci_qTD(qTD_T *qtd);
451extern iTD_T * alloc_ehci_iTD(void);
452extern void free_ehci_iTD(iTD_T *itd);
453extern siTD_T * alloc_ehci_siTD(void);
454extern void free_ehci_siTD(siTD_T *sitd);
455
456
457extern void usbh_hub_init(void);
458extern int connect_device(UDEV_T *);
459extern void disconnect_device(UDEV_T *);
460extern int usbh_register_driver(UDEV_DRV_T *driver);
461extern EP_INFO_T * usbh_iface_find_ep(IFACE_T *iface, uint8_t ep_addr, uint8_t dir_type);
462extern int usbh_reset_device(UDEV_T *);
463
464/*
465 * USB Standard Request functions
466 */
467extern int usbh_get_device_descriptor(UDEV_T *udev, DESC_DEV_T *desc_buff);
468extern int usbh_get_config_descriptor(UDEV_T *udev, uint8_t *desc_buff, int buff_len);
469extern int usbh_set_configuration(UDEV_T *udev, uint8_t conf_val);
470extern int usbh_set_interface(IFACE_T *iface, uint16_t alt_setting);
471extern int usbh_clear_halt(UDEV_T *udev, uint16_t ep_addr);
472
473extern int usbh_ctrl_xfer(UDEV_T *udev, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_t *buff, uint32_t *xfer_len, uint32_t timeout);
474extern int usbh_bulk_xfer(UTR_T *utr);
475extern int usbh_int_xfer(UTR_T *utr);
476extern int usbh_iso_xfer(UTR_T *utr);
477extern int usbh_quit_utr(UTR_T *utr);
478extern int usbh_quit_xfer(UDEV_T *udev, EP_INFO_T *ep);
479
480
482
483#endif /* _USBH_H_ */
void *__dso_handle __attribute__((weak))
Definition: _syscalls.c:35
This header file defines the configuration of USB Host library.
USB EHCI host controller driver header file.
USB OHCI host controller driver header file.
USB Host library exported header file.