M4521_BSP V3.02.000
The Board Support Package for M4521
usbh_core.h
Go to the documentation of this file.
1/**************************************************************************/
12#ifndef _USB_CORE_H_
13#define _USB_CORE_H_
14
15#include "usbh_config.h"
16#include "usbh_list.h"
17#include "usbh_err_code.h"
18
19/* USB Host Power auto control enable */
20#define AUTO_POWER_CONTROL
21
22/* USB constants */
23
24/*
25 * Device and/or Interface Class codes
26 */
28#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
29#define USB_CLASS_AUDIO 1
30#define USB_CLASS_COMM 2
31#define USB_CLASS_HID 3
32#define USB_CLASS_PHYSICAL 5
33#define USB_CLASS_PRINTER 7
34#define USB_CLASS_MASS_STORAGE 8
35#define USB_CLASS_HUB 9
36#define USB_CLASS_DATA 10
37#define USB_CLASS_APP_SPEC 0xfe
38#define USB_CLASS_VENDOR_SPEC 0xff
39
40/*
41 * USB types
42 */
43#define USB_TYPE_STANDARD (0x00 << 5)
44#define USB_TYPE_CLASS (0x01 << 5)
45#define USB_TYPE_VENDOR (0x02 << 5)
46#define USB_TYPE_RESERVED (0x03 << 5)
47
48/*
49 * USB recipients
50 */
51#define USB_RECIP_MASK 0x1f
52#define USB_RECIP_DEVICE 0x00
53#define USB_RECIP_INTERFACE 0x01
54#define USB_RECIP_ENDPOINT 0x02
55#define USB_RECIP_OTHER 0x03
56
57/*
58 * USB directions
59 */
60#define USB_DIR_OUT 0
61#define USB_DIR_IN 0x80
62
63/*
64 * Descriptor types
65 */
66#define USB_DT_DEVICE 0x01
67#define USB_DT_CONFIG 0x02
68#define USB_DT_STRING 0x03
69#define USB_DT_INTERFACE 0x04
70#define USB_DT_ENDPOINT 0x05
71
72#define USB_DT_HID (USB_TYPE_CLASS | 0x01)
73#define USB_DT_REPORT (USB_TYPE_CLASS | 0x02)
74#define USB_DT_PHYSICAL (USB_TYPE_CLASS | 0x03)
75#define USB_DT_HUB (USB_TYPE_CLASS | 0x09)
76
77#define USB_DT_CS_DEVICE (USB_TYPE_CLASS | USB_DT_DEVICE)
78#define USB_DT_CS_CONFIG (USB_TYPE_CLASS | USB_DT_CONFIG)
79#define USB_DT_CS_STRING (USB_TYPE_CLASS | USB_DT_STRING)
80#define USB_DT_CS_INTERFACE (USB_TYPE_CLASS | USB_DT_INTERFACE)
81#define USB_DT_CS_ENDPOINT (USB_TYPE_CLASS | USB_DT_ENDPOINT)
82
83
84/*
85 * Descriptor sizes per descriptor type
86 */
87#define USB_DT_DEVICE_SIZE 18
88#define USB_DT_CONFIG_SIZE 9
89#define USB_DT_INTERFACE_SIZE 9
90#define USB_DT_ENDPOINT_SIZE 7
91#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
92#define USB_DT_HUB_NONVAR_SIZE 7
93#define USB_DT_HID_SIZE 9
94
95/*
96 * Endpoints
97 */
98#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
99#define USB_ENDPOINT_DIR_MASK 0x80
100
101#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
102#define USB_ENDPOINT_XFER_CONTROL 0
103#define USB_ENDPOINT_XFER_ISOC 1
104#define USB_ENDPOINT_XFER_BULK 2
105#define USB_ENDPOINT_XFER_INT 3
106
107/*
108 * USB Packet IDs (PIDs)
109 */
110#define USB_PID_UNDEF_0 0xf0
111#define USB_PID_OUT 0xe1
112#define USB_PID_ACK 0xd2
113#define USB_PID_DATA0 0xc3
114#define USB_PID_PING 0xb4 /* USB 2.0 */
115#define USB_PID_SOF 0xa5
116#define USB_PID_NYET 0x96 /* USB 2.0 */
117#define USB_PID_DATA2 0x87 /* USB 2.0 */
118#define USB_PID_SPLIT 0x78 /* USB 2.0 */
119#define USB_PID_IN 0x69
120#define USB_PID_NAK 0x5a
121#define USB_PID_DATA1 0x4b
122#define USB_PID_PREAMBLE 0x3c /* Token mode */
123#define USB_PID_ERR 0x3c /* USB 2.0: handshake mode */
124#define USB_PID_SETUP 0x2d
125#define USB_PID_STALL 0x1e
126#define USB_PID_MDATA 0x0f /* USB 2.0 */
127
128/*
129 * Standard requests
130 */
131#define USB_REQ_GET_STATUS 0x00
132#define USB_REQ_CLEAR_FEATURE 0x01
133#define USB_REQ_SET_FEATURE 0x03
134#define USB_REQ_SET_ADDRESS 0x05
135#define USB_REQ_GET_DESCRIPTOR 0x06
136#define USB_REQ_SET_DESCRIPTOR 0x07
137#define USB_REQ_GET_CONFIGURATION 0x08
138#define USB_REQ_SET_CONFIGURATION 0x09
139#define USB_REQ_GET_INTERFACE 0x0A
140#define USB_REQ_SET_INTERFACE 0x0B
141#define USB_REQ_SYNCH_FRAME 0x0C
142
143/*
144 * HID requests
145 */
146#define USB_REQ_GET_REPORT 0x01
147#define USB_REQ_GET_IDLE 0x02
148#define USB_REQ_GET_PROTOCOL 0x03
149#define USB_REQ_SET_REPORT 0x09
150#define USB_REQ_SET_IDLE 0x0A
151#define USB_REQ_SET_PROTOCOL 0x0B
153
155#ifdef __ICCARM__
156typedef struct
157{
158 __packed uint8_t requesttype;
159 __packed uint8_t request;
160 __packed uint16_t value;
161 __packed uint16_t index;
162 __packed uint16_t length;
163} DEV_REQ_T;
164#else
165typedef struct __attribute__((__packed__))
166{
167 uint8_t requesttype;
168 uint8_t request;
169 uint16_t value;
170 uint16_t index;
171 uint16_t length;
172}
174#endif
175
176/*
177 * This is a USB device descriptor.
178 *
179 * USB device information
180 */
181
183/* Everything but the endpoint maximums are arbitrary */
184#define USB_MAXCONFIG 8
185#define USB_ALTSETTINGALLOC 16
186#define USB_MAXALTSETTING 128 /* Hard limit */
187#define USB_MAXINTERFACES 32
188#define USB_MAXENDPOINTS 32
189
190/* All standard descriptors have these 2 fields in common */
191#ifdef __ICCARM__
192typedef struct usb_descriptor_header
193{
194 __packed uint8_t bLength;
195 __packed uint8_t bDescriptorType;
196} USB_DESC_HDR_T;
197#else
198typedef struct __attribute__((__packed__)) usb_descriptor_header
199{
200 uint8_t bLength;
201 uint8_t bDescriptorType;
202} USB_DESC_HDR_T;
203#endif
204
206
207/*
208 * declaring data structures presented before their definition met
209 */
210struct usb_device;
211struct urb_t;
212
213
214/*-----------------------------------------------------------------------------------
215 * USB device descriptor
216 */
217#ifdef __ICCARM__
218typedef struct usb_device_descriptor
219{
220 __packed uint8_t bLength;
221 __packed uint8_t bDescriptorType;
222 __packed uint16_t bcdUSB;
223 __packed uint8_t bDeviceClass;
224 __packed uint8_t bDeviceSubClass;
225 __packed uint8_t bDeviceProtocol;
226 __packed uint8_t bMaxPacketSize0;
227 __packed uint16_t idVendor;
228 __packed uint16_t idProduct;
229 __packed uint16_t bcdDevice;
230 __packed uint8_t iManufacturer;
231 __packed uint8_t iProduct;
232 __packed uint8_t iSerialNumber;
233 __packed uint8_t bNumConfigurations;
235#else
236typedef struct __attribute__((__packed__)) usb_device_descriptor
237{
238 uint8_t bLength;
239 uint8_t bDescriptorType;
240 uint16_t bcdUSB;
241 uint8_t bDeviceClass;
242 uint8_t bDeviceSubClass;
243 uint8_t bDeviceProtocol;
244 uint8_t bMaxPacketSize0;
245 uint16_t idVendor;
246 uint16_t idProduct;
247 uint16_t bcdDevice;
248 uint8_t iManufacturer;
249 uint8_t iProduct;
250 uint8_t iSerialNumber;
251 uint8_t bNumConfigurations;
253#endif
254
255
256/*-----------------------------------------------------------------------------------
257 * USB endpoint descriptor
258 */
259#ifdef __ICCARM__
260typedef struct usb_endpoint_descriptor
261{
262 __packed uint8_t bLength;
263 __packed uint8_t bDescriptorType;
264 __packed uint8_t bEndpointAddress;
265 __packed uint8_t bmAttributes;
266 __packed uint16_t wMaxPacketSize;
267 __packed uint8_t bInterval;
268 __packed uint8_t bRefresh;
269 __packed uint8_t bSynchAddress;
271#else
272typedef struct __attribute__((__packed__)) usb_endpoint_descriptor
273{
274 uint8_t bLength;
275 uint8_t bDescriptorType;
276 uint8_t bEndpointAddress;
277 uint8_t bmAttributes;
278 uint16_t wMaxPacketSize;
279 uint8_t bInterval;
280 uint8_t bRefresh;
281 uint8_t bSynchAddress;
283#endif
284
285/*-----------------------------------------------------------------------------------
286 * USB interface descriptor
287 */
288#ifdef __ICCARM__
289typedef struct usb_interface_descriptor
290{
291 __packed uint8_t bLength;
292 __packed uint8_t bDescriptorType;
293 __packed uint8_t bInterfaceNumber;
294 __packed uint8_t bAlternateSetting;
295 __packed uint8_t bNumEndpoints;
296 __packed uint8_t bInterfaceClass;
297 __packed uint8_t bInterfaceSubClass;
298 __packed uint8_t bInterfaceProtocol;
299 __packed uint8_t iInterface;
300 USB_EP_DESC_T *endpoint;
302#else
303typedef struct __attribute__((__packed__)) usb_interface_descriptor
304{
305 uint8_t bLength;
306 uint8_t bDescriptorType;
307 uint8_t bInterfaceNumber;
308 uint8_t bAlternateSetting;
309 uint8_t bNumEndpoints;
310 uint8_t bInterfaceClass;
311 uint8_t bInterfaceSubClass;
312 uint8_t bInterfaceProtocol;
313 uint8_t iInterface;
314 USB_EP_DESC_T *endpoint;
316#endif
317
318/*-----------------------------------------------------------------------------------
319 * Configuration descriptor
320 */
321#ifdef __ICCARM__
322typedef struct usb_config_descriptor
323{
324 __packed uint8_t bLength;
325 __packed uint8_t bDescriptorType;
326 __packed uint16_t wTotalLength;
327 __packed uint8_t bNumInterfaces;
328 __packed uint8_t bConfigurationValue;
329 __packed uint8_t iConfiguration;
330 __packed uint8_t bmAttributes;
331 __packed uint8_t MaxPower;
333#else
334typedef struct __attribute__((__packed__)) usb_config_descriptor
335{
336 uint8_t bLength;
337 uint8_t bDescriptorType;
338 uint16_t wTotalLength;
339 uint8_t bNumInterfaces;
340 uint8_t bConfigurationValue;
341 uint8_t iConfiguration;
342 uint8_t bmAttributes;
343 uint8_t MaxPower;
345#endif
346
348
349/* String descriptor */
350#ifdef __ICCARM__
351typedef struct usb_string_descriptor
352{
353 __packed uint8_t bLength;
354 __packed uint8_t bDescriptorType;
355 __packed uint16_t wData[1];
356} USB_STR_DESC_T;
357#else
358typedef struct __attribute__((__packed__)) usb_string_descriptor
359{
360 uint8_t bLength;
361 uint8_t bDescriptorType;
362 uint16_t wData[1];
363} USB_STR_DESC_T;
364#endif
365
366/*
367 * Device table entry for "new style" table-driven USB drivers.
368 * User mode code can read these tables to choose which modules to load.
369 * Declare the table as __devinitdata, and as a MODULE_DEVICE_TABLE.
370 *
371 * With a device table provide bind() instead of probe(). Then the
372 * third bind() parameter will point to a matching entry from this
373 * table. (Null value reserved.)
374 *
375 * Terminate the driver's table with an all-zeroes entry.
376 * Init the fields you care about; zeroes are not used in comparisons.
377 */
378#define USB_DEVICE_ID_MATCH_VENDOR 0x0001
379#define USB_DEVICE_ID_MATCH_PRODUCT 0x0002
380#define USB_DEVICE_ID_MATCH_DEV_LO 0x0004
381#define USB_DEVICE_ID_MATCH_DEV_HI 0x0008
382#define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010
383#define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020
384#define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040
385#define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080
386#define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100
387#define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200
388
389#define USB_DEVICE_ID_MATCH_DEVICE (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
390#define USB_DEVICE_ID_MATCH_DEV_RANGE (USB_DEVICE_ID_MATCH_DEV_LO | USB_DEVICE_ID_MATCH_DEV_HI)
391#define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_RANGE)
392#define USB_DEVICE_ID_MATCH_DEV_INFO \
393 (USB_DEVICE_ID_MATCH_DEV_CLASS | USB_DEVICE_ID_MATCH_DEV_SUBCLASS | USB_DEVICE_ID_MATCH_DEV_PROTOCOL)
394#define USB_DEVICE_ID_MATCH_INT_INFO \
395 (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS | USB_DEVICE_ID_MATCH_INT_PROTOCOL)
396
397/* Some useful macros */
398#define USB_DEVICE(vend,prod) \
399 { USB_DEVICE_ID_MATCH_DEVICE, vend, prod, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
400
401#define USB_DEVICE_VER(vend,prod,lo,hi) \
402 { USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, vend, prod, lo, hi, 0, 0, 0, 0, 0, 0, 0 }
403
404#define USB_DEVICE_INFO(cl,sc,pr) \
405 { USB_DEVICE_ID_MATCH_DEV_INFO, 0, 0, 0, 0, cl, sc, pr, 0, 0, 0, 0 }
406
407#define USB_INTERFACE_INFO(cl,sc,pr) \
408 { USB_DEVICE_ID_MATCH_INT_INFO, 0, 0, 0, 0, 0, 0, 0, cl, sc, pr, 0 }
409
410typedef struct usb_device_id
411{
412 /* This bitmask is used to determine which of the following fields
413 * are to be used for matching.
414 */
415 uint16_t match_flags;
416
417 /*
418 * vendor/product codes are checked, if vendor is nonzero
419 * Range is for device revision (bcdDevice), inclusive;
420 * zero values here mean range isn't considered
421 */
422 uint16_t idVendor;
423 uint16_t idProduct;
424 uint16_t bcdDevice_lo;
425 uint16_t bcdDevice_hi;
426
427 /*
428 * if device class != 0, these can be match criteria;
429 * but only if this bDeviceClass value is nonzero
430 */
431 uint8_t bDeviceClass;
432 uint8_t bDeviceSubClass;
433 uint8_t bDeviceProtocol;
434
435 /*
436 * if interface class != 0, these can be match criteria;
437 * but only if this bInterfaceClass value is nonzero
438 */
439 uint8_t bInterfaceClass;
440 uint8_t bInterfaceSubClass;
441 uint8_t bInterfaceProtocol;
442
443 /*
444 * for driver's use; not involved in driver matching.
445 */
446 uint32_t driver_info;
447} USB_DEV_ID_T;
448
449
450typedef struct usb_driver
451{
452 const char *name;
453 int (*probe)(struct usb_device *dev, USB_IF_DESC_T *ifd, const USB_DEV_ID_T *id);
454 void (*disconnect)(struct usb_device *);
455 const USB_DEV_ID_T *id_table;
456 void (*suspend)(struct usb_device *dev);
457 void (*resume)(struct usb_device *dev);
458 USB_LIST_T driver_list;
459} USB_DRIVER_T;
461
462
463
465/*----------------------------------------------------------------------------*
466 * New USB Structures *
467 *----------------------------------------------------------------------------*/
468/*
469 * urb->transfer_flags:
470 */
471#define USB_DISABLE_SPD 0x0001
472#define URB_SHORT_NOT_OK USB_DISABLE_SPD
473#define USB_ISO_ASAP 0x0002
474#define USB_ASYNC_UNLINK 0x0008
475#define USB_QUEUE_BULK 0x0010
476#define USB_NO_FSBR 0x0020
477#define USB_ZERO_PACKET 0x0040 // Finish bulk OUTs always with zero length packet
478#define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt needed */
479/* ... less overhead for QUEUE_BULK */
480#define USB_TIMEOUT_KILLED 0x1000 // only set by HCD!
481
482#define URB_ZERO_PACKET USB_ZERO_PACKET
483#define URB_ISO_ASAP USB_ISO_ASAP
484
485
486struct ohci_ed_t;
487struct ohci_td_t;
488
489typedef struct
490{
491 struct ohci_ed_t *ed;
492 uint16_t length; /* number of tds associated with this request */
493 uint16_t td_cnt; /* number of tds already serviced */
494 int state;
495 struct ohci_td_t *td[MAX_TD_PER_OHCI_URB]; /* list pointer to all corresponding TDs associated with this request */
496} URB_PRIV_T;
498
499
500/*-----------------------------------------------------------------------------------
501 * URB isochronous descriptor structure
502 */
503typedef struct
504{
505 uint32_t offset;
506 uint32_t length;
507 uint32_t actual_length;
508 int status;
510
511/*-----------------------------------------------------------------------------------
512 * USB Request Block (URB) structure
513 */
514typedef struct urb_t
515{
516 URB_PRIV_T urb_hcpriv;
517 USB_LIST_T urb_list;
518 struct urb_t *next;
519 struct usb_device *dev;
520 uint32_t pipe;
521 int status;
522 uint32_t transfer_flags;
526 uint8_t *setup_packet;
532 void *context;
533 void (*complete)(struct urb_t *);
539
540#define FILL_CONTROL_URB(a,aa,b,c,d,e,f,g) \
541 do {\
542 (a)->dev=aa;\
543 (a)->pipe=b;\
544 (a)->setup_packet=c;\
545 (a)->transfer_buffer=d;\
546 (a)->transfer_buffer_length=e;\
547 (a)->complete=f;\
548 (a)->context=g;\
549 } while (0)
550
551
552#define FILL_BULK_URB(a,aa,b,c,d,e,f) \
553 do {\
554 (a)->dev=aa;\
555 (a)->pipe=b;\
556 (a)->transfer_buffer=c;\
557 (a)->transfer_buffer_length=d;\
558 (a)->complete=e;\
559 (a)->context=f;\
560 } while (0)
561
562
563#define FILL_INT_URB(a,aa,b,c,d,e,f,g) \
564 do {\
565 (a)->dev=aa;\
566 (a)->pipe=b;\
567 (a)->transfer_buffer=c;\
568 (a)->transfer_buffer_length=d;\
569 (a)->complete=e;\
570 (a)->context=f;\
571 (a)->interval=g;\
572 (a)->start_frame=-1;\
573 } while (0)
574
575
576#define FILL_CONTROL_URB_TO(a,aa,b,c,d,e,f,g,h) \
577 do {\
578 (a)->dev=aa;\
579 (a)->pipe=b;\
580 (a)->setup_packet=c;\
581 (a)->transfer_buffer=d;\
582 (a)->transfer_buffer_length=e;\
583 (a)->complete=f;\
584 (a)->context=g;\
585 (a)->timeout=h;\
586 } while (0)
587
588
589#define FILL_BULK_URB_TO(a,aa,b,c,d,e,f,g) \
590 do {\
591 (a)->dev=aa;\
592 (a)->pipe=b;\
593 (a)->transfer_buffer=c;\
594 (a)->transfer_buffer_length=d;\
595 (a)->complete=e;\
596 (a)->context=f;\
597 (a)->timeout=g;\
598 } while (0)
599
600
601typedef struct usb_operations
602{
603 int (*allocate)(struct usb_device *);
604 int (*deallocate)(struct usb_device *);
605 int (*get_frame_number)(struct usb_device *usb_dev);
606 int (*submit_urb)(URB_T *urb);
607 int (*unlink_urb)(URB_T *urb);
608} USB_OP_T;
609
610
611typedef struct usb_bus
612{
613 USB_OP_T *op; /* Operations (specific to the HC) */
614 struct usb_device *root_hub; /* Root hub */
615 void *hcpriv; /* Host Controller private data */
616} USB_BUS_T;
617
618
619#define USB_MAXCHILDREN (4) /* This is arbitrary */
620
621typedef struct ep_info_t
622{
623 uint8_t cfgno;
624 uint8_t ifnum;
625 uint8_t altno;
626 uint8_t bEndpointAddress;
627 uint8_t bmAttributes;
628 uint8_t bInterval;
629 short wMaxPacketSize;
630} EP_INFO_T;
631
632
633#define USB_SPEED_UNKNOWN 0
634#define USB_SPEED_LOW 1
635#define USB_SPEED_FULL 2
636#define USB_SPEED_HIGH 3
637
639
640/*-----------------------------------------------------------------------------------
641 * USB device structure
642 */
643typedef struct usb_device
644{
647 int devnum;
648 int slow;
649 int speed;
651 uint32_t toggle[2];
652 uint32_t halted[2];
653 /* [0] = IN, [1] = OUT */
654
657 USB_BUS_T *bus;
659 signed char act_config;
669 void *hcpriv;
671 /*
672 * Child devices - these can be either new devices
673 * (if this is a hub device), or different instances
674 * of this same device.
675 *
676 * Each instance needs its own set of data structures.
677 */
678
680 struct usb_device *children[USB_MAXCHILDREN];
682 USB_DRIVER_T *driver[MAX_DRIVER_PER_DEV];
688/*
689 * Calling this entity a "pipe" is glorifying it. A USB pipe
690 * is something embarrassingly simple: it basically consists
691 * of the following information:
692 * - device number (7 bits)
693 * - endpoint number (4 bits)
694 * - current Data0/1 state (1 bit)
695 * - direction (1 bit)
696 * - speed (1 bit)
697 * - max packet size (2 bits: 8, 16, 32 or 64) [Historical; now gone.]
698 * - pipe type (2 bits: control, interrupt, bulk, isochronous)
699 *
700 * That's 18 bits. Really. Nothing more. And the USB people have
701 * documented these eighteen bits as some kind of glorious
702 * virtual data structure.
703 *
704 * Let's not fall in that trap. We'll just encode it as a simple
705 * uint32_t. The encoding is:
706 *
707 * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64) [Historical; now gone.]
708 * - direction: bit 7 (0 = Host-to-Device [Out], 1 = Device-to-Host [In])
709 * - device: bits 8-14
710 * - endpoint: bits 15-18
711 * - Data0/1: bit 19
712 * - speed: bit 26 (0 = Full, 1 = Low Speed)
713 * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt, 10 = control, 11 = bulk)
714 *
715 * Why? Because it's arbitrary, and whatever encoding we select is really
716 * up to us. This one happens to share a lot of bit positions with the UHCI
717 * specification, so that much of the uhci driver can just mask the bits
718 * appropriately.
719 */
720
722
723#define PIPE_ISOCHRONOUS 0
724#define PIPE_INTERRUPT 1
725#define PIPE_CONTROL 2
726#define PIPE_BULK 3
727
728#define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : USB_PID_OUT)
729
730#define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1)
731#define usb_pipein(pipe) (((pipe) >> 7) & 1)
732#define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
733#define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff)
734#define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
735#define usb_pipedata(pipe) (((pipe) >> 19) & 1)
736#define usb_pipeslow(pipe) (((pipe) >> 26) & 1)
737#define usb_pipetype(pipe) (((pipe) >> 30) & 3)
738#define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
739#define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
740#define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
741#define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
742
743#define PIPE_DEVEP_MASK 0x0007ff00
744
745/* The D0/D1 toggle bits */
746#define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1)
747#define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << ep))
748#define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = ((dev)->toggle[out] & ~(1 << ep)) | ((bit) << ep))
749
750/* Endpoint halt control/status */
751#define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1)
752#define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep)))
753#define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))
754#define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))
755
756static __inline uint32_t __create_pipe(USB_DEV_T *dev, uint32_t endpoint)
757{
758 return (dev->devnum << 8) | (endpoint << 15) | (dev->slow << 26);
759}
760
761static __inline uint32_t __default_pipe(USB_DEV_T *dev)
762{
763 return (dev->slow << 26);
764}
765
766/* Create various pipes... */
767#define usb_sndctrlpipe(dev,endpoint) (0x80000000 | __create_pipe(dev,endpoint))
768#define usb_rcvctrlpipe(dev,endpoint) (0x80000000 | __create_pipe(dev,endpoint) | USB_DIR_IN)
769#define usb_sndisocpipe(dev,endpoint) (0x00000000 | __create_pipe(dev,endpoint))
770#define usb_rcvisocpipe(dev,endpoint) (0x00000000 | __create_pipe(dev,endpoint) | USB_DIR_IN)
771#define usb_sndbulkpipe(dev,endpoint) (0xC0000000 | __create_pipe(dev,endpoint))
772#define usb_rcvbulkpipe(dev,endpoint) (0xC0000000 | __create_pipe(dev,endpoint) | USB_DIR_IN)
773#define usb_sndintpipe(dev,endpoint) (0x40000000 | __create_pipe(dev,endpoint))
774#define usb_rcvintpipe(dev,endpoint) (0x40000000 | __create_pipe(dev,endpoint) | USB_DIR_IN)
775#define usb_snddefctrl(dev) (0x80000000 | __default_pipe(dev))
776#define usb_rcvdefctrl(dev) (0x80000000 | __default_pipe(dev) | USB_DIR_IN)
777
778
779extern USB_LIST_T usb_driver_list;
780extern USB_LIST_T usb_bus_list;
781
782/*-------------------------------------------------------------------------
783 * Global Variables
784 *-------------------------------------------------------------------------*/
785extern USB_BUS_T g_ohci_bus;
786
788
789
803extern int32_t USBH_Open(void);
804extern int USBH_ProcessHubEvents(void);
805extern URB_T * USBH_AllocUrb(void);
806extern void USBH_FreeUrb(URB_T *);
807extern int32_t USBH_SubmitUrb(URB_T *urb);
808extern int32_t USBH_UnlinkUrb(URB_T *urb);
809extern int32_t USBH_SendCtrlMsg(USB_DEV_T *dev, uint32_t pipe, uint8_t request, uint8_t requesttype, uint16_t value, uint16_t index, void *data, uint16_t size, int timeout);
810extern int32_t USBH_SendBulkMsg(USB_DEV_T *usb_dev, uint32_t pipe, void *data, int len, int *actual_length, int timeout);
811extern int32_t USBH_RegisterDriver(USB_DRIVER_T *new_driver);
812extern int32_t USBH_GetDescriptor(USB_DEV_T *dev, uint8_t type, uint8_t index, void *buf, int size);
813extern int32_t USBH_SetConfiguration(USB_DEV_T *dev, int configuration);
814extern int32_t USBH_SetInterface(USB_DEV_T *dev, char interface, char alternate);
815extern int32_t USBH_ClearHalt(USB_DEV_T *dev, int pipe);
816extern int32_t USBH_Suspend(void);
817extern int32_t USBH_Resume(void);
818extern int32_t USBH_Close(void);
819
820 /* end of group USBH_CORE_EXPORTED_FUNCTIONS */
822 /* end of group USBH_CORE_Driver */
824 /* end of group USB_Host_Driver */
826
827
829
830/* USB core library internal APIs */
831extern void usbh_init_memory(void);
832extern int usbh_init_ohci(void);
833extern int usbh_init_hub_driver(void);
834extern void ohci_irq(void);
835extern void ohci_int_timer_do(int);
836extern void usbh_mdelay(uint32_t msec);
837
838extern void usbh_connect_device(USB_DEV_T *dev);
839extern void usbh_disconnect_device(USB_DEV_T **pdev);
840extern int usbh_settle_new_device(USB_DEV_T *dev);
841
842extern int usb_maxpacket(USB_DEV_T *dev, uint32_t pipe, int out);
843
844extern USB_DEV_T *usbh_alloc_device(USB_DEV_T *parent, USB_BUS_T *bus);
845extern void usbh_free_device(USB_DEV_T *dev);
846
847extern struct ohci_ed_t * ohci_alloc_ed(void);
848extern void ohci_free_ed(struct ohci_ed_t *ed_p);
849extern struct ohci_td_t * ohci_alloc_td(USB_DEV_T *dev);
850extern void ohci_free_td(struct ohci_td_t *td_p);
851extern void ohci_free_dev_td(USB_DEV_T *dev);
852extern struct ohci_itd_t * ohci_alloc_idd(void);
853extern void ohci_free_itd(struct ohci_itd_t *itd_p);
854extern void usbh_free_dev_urbs(USB_DEV_T *dev);
855extern int usbh_translate_string(USB_DEV_T *dev, int index, char *buf, int size);
856
857extern void usbh_dump_device_descriptor(USB_DEV_DESC_T *desc);
858extern void usbh_dump_config_descriptor(USB_CONFIG_DESC_T *desc);
859extern void usbh_dump_iface_descriptor(USB_IF_DESC_T *desc);
860extern void usbh_dump_ep_descriptor(USB_EP_DESC_T *desc);
861extern void usbh_dump_urb(URB_T *purb);
862extern void usbh_print_usb_string(USB_DEV_T *dev, char *id, int index);
863
865
866#endif /* _USB_CORE_H_ */
867
USB_LIST_T urb_list
Definition: usbh_core.h:517
uint8_t * setup_packet
Definition: usbh_core.h:526
URB_T * USBH_AllocUrb(void)
Allocate an URB from USB Core driver internal URB pool.
Definition: usbh_support.c:76
int status
Definition: usbh_core.h:521
void * transfer_buffer
Definition: usbh_core.h:523
int32_t USBH_RegisterDriver(USB_DRIVER_T *new_driver)
Register a device driver to USB Host Core driver.
Definition: usbh_core.c:78
int maxchild
Definition: usbh_core.h:679
uint32_t toggle[2]
Definition: usbh_core.h:651
int32_t USBH_SubmitUrb(URB_T *urb)
Submit an URB to USB core for transfer data.
Definition: usbh_core.c:219
int32_t USBH_SendBulkMsg(USB_DEV_T *usb_dev, uint32_t pipe, void *data, int len, int *actual_length, int timeout)
Execute a bulk transfer.
Definition: usbh_core.c:413
int start_frame
Definition: usbh_core.h:527
int32_t USBH_Resume(void)
Resume USB Host controller and devices.
Definition: usbh_core.c:1377
int32_t USBH_Close(void)
Disable USB Host controller function.
Definition: usbh_core.c:1318
void * hcpriv
Definition: usbh_core.h:669
uint32_t halted[2]
Definition: usbh_core.h:652
int actual_length
Definition: usbh_core.h:525
int transfer_buffer_length
Definition: usbh_core.h:524
int have_langid
Definition: usbh_core.h:666
uint32_t pipe
Definition: usbh_core.h:520
int devnum
Definition: usbh_core.h:647
signed char act_config
Definition: usbh_core.h:659
int32_t USBH_Suspend(void)
Suspend USB Host Controller and devices.
Definition: usbh_core.c:1336
EP_INFO_T ep_list[MAX_ENDPOINTS]
Definition: usbh_core.h:663
struct usb_device * dev
Definition: usbh_core.h:519
char act_iface
Definition: usbh_core.h:660
int number_of_packets
Definition: usbh_core.h:528
int32_t USBH_UnlinkUrb(URB_T *urb)
Cancel an URB which has been submit to USB core.
Definition: usbh_core.c:237
struct usb_device * children[USB_MAXCHILDREN]
Definition: usbh_core.h:680
char iface_alternate
Definition: usbh_core.h:661
USB_DEV_DESC_T descriptor
Definition: usbh_core.h:645
int driver_cnt
Definition: usbh_core.h:683
uint32_t transfer_flags
Definition: usbh_core.h:522
URB_PRIV_T urb_hcpriv
Definition: usbh_core.h:516
int32_t USBH_Open(void)
Open USB Host controller function.
Definition: usbh_core.c:1269
int ep_list_cnt
Definition: usbh_core.h:664
int32_t USBH_SetInterface(USB_DEV_T *dev, char interface, char alternate)
Set USB device interface.
Definition: usbh_core.c:948
int error_count
Definition: usbh_core.h:530
int32_t USBH_GetDescriptor(USB_DEV_T *dev, uint8_t type, uint8_t index, void *buf, int size)
Get a descriptor from device.
Definition: usbh_core.c:822
USB_DRIVER_T * driver[MAX_DRIVER_PER_DEV]
Definition: usbh_core.h:682
ISO_PACKET_DESCRIPTOR_T iso_frame_desc[8]
Definition: usbh_core.h:534
struct urb_t * next
Definition: usbh_core.h:518
USB_BUS_T * bus
Definition: usbh_core.h:657
void USBH_FreeUrb(URB_T *)
Free the URB allocated from USBH_AllocUrb()
Definition: usbh_support.c:99
int32_t USBH_ClearHalt(USB_DEV_T *dev, int pipe)
Clear the halt state of an endpoint.
Definition: usbh_core.c:900
void(* complete)(struct urb_t *)
Definition: usbh_core.h:533
int hub_port
Definition: usbh_core.h:656
int USBH_ProcessHubEvents(void)
Processed USB hub device events. User application must invoke this routine in the main while loop....
Definition: usbh_hub.c:117
int interval
Definition: usbh_core.h:529
void * context
Definition: usbh_core.h:532
struct usb_device * parent
Definition: usbh_core.h:655
int string_langid
Definition: usbh_core.h:667
int timeout
Definition: usbh_core.h:531
int32_t USBH_SetConfiguration(USB_DEV_T *dev, int configuration)
Select USB device configuration.
Definition: usbh_core.c:973
int32_t USBH_SendCtrlMsg(USB_DEV_T *dev, uint32_t pipe, uint8_t request, uint8_t requesttype, uint16_t value, uint16_t index, void *data, uint16_t size, int timeout)
Execute a control transfer.
Definition: usbh_core.c:375
return value
Definition: semihosting.h:98
USB Host core configuration file.
#define MAX_TD_PER_OHCI_URB
Definition: usbh_config.h:39
#define MAX_ENDPOINTS
Definition: usbh_config.h:37
#define MAX_DRIVER_PER_DEV
Definition: usbh_config.h:38
USB_IF_DESC_T
Definition: usbh_core.h:315
struct urb_t URB_T
DEV_REQ_T
Definition: usbh_core.h:173
USB_CONFIG_DESC_T
Definition: usbh_core.h:344
USB_DEV_DESC_T
Definition: usbh_core.h:252
HIDDEN_SYMBOLS struct __attribute__((__packed__))
Definition: usbh_core.h:165
HIDDEN_SYMBOLS struct usb_device USB_DEV_T
USB_EP_DESC_T
Definition: usbh_core.h:282
void usbh_free_device(USB_DEV_T *dev)
Definition: usbh_support.c:252
void ohci_free_td(TD_T *td_p)
Definition: usbh_support.c:189
TD_T * ohci_alloc_td(USB_DEV_T *dev)
Definition: usbh_support.c:169
HIDDEN_SYMBOLS void usbh_init_memory()
Definition: usbh_support.c:48
void ohci_free_ed(ED_T *ed_p)
Definition: usbh_support.c:152
USB_DEV_T * usbh_alloc_device(USB_DEV_T *parent, USB_BUS_T *bus)
Definition: usbh_support.c:224
void usbh_mdelay(uint32_t msec)
Definition: usbh_support.c:330
void usbh_free_dev_urbs(USB_DEV_T *dev)
Definition: usbh_support.c:115
void ohci_free_dev_td(USB_DEV_T *dev)
Definition: usbh_support.c:205
ED_T * ohci_alloc_ed()
Definition: usbh_support.c:133