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