| NUC472_NUC442_BSP V3.03.003 The Board Support Package for NUC472/NUC442

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