It assumes that the reader is familiar with the USB 2.0 and USB 1.1 specifications, as well as the EHCI and OHCI specifications. It also assumes the reader has access to the AHB sub-system hardware specifications. Please see the list of references for links to these documents. Only details peculiar to our system are described in this document. It does not try to explain the USB specifications. The sections are arranged in the order they should be read. OHCI0 is the simpler RM and was written first. The EHCI RM is based on the OHCI0 RM. However, the transfer mechanisms and so on are done very differently in EHCI.
Please note that even though port 3 is wired to BRCM2045, it is organized as a companion host-controller to EHCI. The EHCI RM needs to explicitly transfer the port to OHCI for the port to be accessible to OHCI1. See the section about port transfer in EHCI for details.
Also, as defined by the OHCI specifications, there are no hardware interlocks for the data structures operated on by the host-controller as well as the host-controller driver. Procedures to enqueue and dequeue transfer and endpoint descriptors described in the OHCI specifications must be followed, in order to avoid race conditions in the processing of queues.
In the sources, you will find 3 kinds of OHCI resource managers and 1 EHCI resource manager. The 3 OHCI RMs are:
The USB resource managers (OHCI1, OHCI0, EHCI) can be accessed via the standard IOP OS resource manager interface. However, a "shim layer" library is provided on the IOP as well as the PPC, in order to allow easier access to them. This API is also referred to as the "IUSB API".
The IOP version of this library is described in the "IOP USB API" document and can be found in "libusb.a" included in the IOP SDK. The PPC version is described in the "PPC USB API" document and can be found in "usb.a" or "usbD.a" in the PPC SDK.
This section describes some of the internals of the OHCI0 resource manager. A description of the individual files can be found in a later section.
typedef struct ohci_opregs {
u32 rev; /* HcRevision */
u32 ctrl; /* HcControl */
u32 cmd_stat; /* HcCommandStatus */
u32 int_stat; /* HcInterruptStatus */
u32 int_en; /* HcInterruptEnable */
u32 int_dis; /* HcInterruptDisable */
u32 hcca; /* HcHCCA */
u32 prd_curr; /* HcPeriodCurrentED */
u32 ctrl_head; /* HcControlHeadED */
u32 ctrl_curr; /* HcControlCurrentED */
u32 blk_head; /* HcBulkHeadED */
u32 blk_curr; /* HcBulkCurrentED */
u32 done_head; /* HcDoneHead */
u32 frm_int; /* HcFmInterval */
u32 frm_rem; /* HcFmRemaining */
u32 frm_num; /* HcFmNumber */
u32 prd_start; /* HcPeriodicStart */
u32 ls_thres; /* HcLSThreshold */
u32 rhub_desca; /* HcRhDescriptorA */
u32 rhub_descb; /* HcRhDescriptorB */
u32 rhub_stat; /* HcRhStatus */
u32 rhub_pstat[USB_RHUB_PORT_MAX]; /* HcRhPortStatus */
} ohci_opregs __attribute__((aligned(32)));
#define OHCI_HCCA_ALIGN 256
typedef struct ohci_hcca {
u32 inttbl[OHCI_HCCA_NINT];
u32 frmnum; /* includes 2 byte pad */
u32 donehead;
u8 hcrsvd[116]; /* reserved for HC */
u8 pad[4]; /* undefined bytes in spec */
} ohci_hcca __attribute__((aligned(OHCI_HCCA_ALIGN)));
#define OHCI_ED_ALIGN 16
typedef struct ohci_ed {
u32 flags;
u32 tailP;
u32 headP;
u32 nextED;
} ohci_ed __attribute__((aligned(OHCI_ED_ALIGN)));
#define OHCI_TD_ALIGN 32 /* at least 16 for general td, 32 for itd */
typedef struct ohci_td {
u32 flags; /* Transfer flags */
u32 cbp; /* Current buffer pointer or bp0*/
u32 nextTD; /* Next TD */
u32 be; /* Buffer end */
u32 psw[4]; /* Packet status word for iTD only */
/* Stuff for HCD's use */
OHCIrp *irp; /* IRP containing this transfer */
u16 len; /* Transfer length */
} ohci_td __attribute((aligned(OHCI_TD_ALIGN)));
typedef struct ohci_hcd {
struct ohci_opregs *opregs; /* Operational registers in IO space */
struct ohci_hcca *hcca; /* Memory shared with HC (sec ) */
/* For HCD use */
u32 frm_int; /* Save frame interval */
ohci_ed *intr_list; /* Interrupt endpoint list */
IOSTimerId tmr; /* Timer used by main thread */
IOSTimerId tmrEv; /* Timer used by event thread */
IOSMessageQueueId qid; /* QueueId used by main thread */
IOSMessageQueueId qidEv; /* QueueId used by event thread */
IOSMessageQueueId rm_qid; /* QueueId used for RM loop */
IOSMessage mq[1]; /* Queue used by main thread */
IOSMessage mqEv[1]; /* Queue used by event thread */
IOSEvent event; /* Constant: IOS_EVENT_OH0 for OHCI0 RM */
u8 ndp; /* Number of downstream ports for root-hub */
u32 flags; /* HCD initialization flag */
u32 openid; /* Used to assign device numbers */
OHCCtrlReq *req; /* Used for internal control requests */
IOSThreadId rmId; /* Main thread id */
u8 rwc; /* Save remote wakeup connected bit */
u8 hc_rdb; /* Constant: IOS_RB_OHCI0 for OHCI0 RM */
u8 hc_wb; /* Constant: IOS_WB_OHCI0 for OHCI0 RM */
u32 load_ns; /* Periodic transfer load */
} ohci_hcd;
typedef struct usb_hub {
u8 devnum; /* Index into device nodes list */
u8 ndp; /* Number of downstream ports */
u8 ndp_stat; /* Status */
u8 potpgt; /* Power-up to power-good time */
u8 data[4] __attribute__((aligned(32))); /* Status data */
} usb_hub;
typedef struct usb_dev {
u8 hub; /* Hub for current device */
u8 port; /* Port for current device */
u16 spd; /* High, full or low speed */
u8 maxPower; /* Max power */
u16 vid; /* Vendor id */
u16 pid; /* Product id */
u8 devClass; /* Device class */
u8 subClass; /* Device subclass */
u8 protocol; /* Protocol */
u8 nintf; /* Number of interfaces */
IOSResourceRequest *notifyRemoval; /* Notify removal request */
usb_intf intf[OHCI_DEV_MAX_INTF]; /* Interface data */
struct usb_dev_ep ep0; /* Device endpoint 0 */
struct usb_dev_ep ep[OHCI_DEV_MAX_EP]; /* Other device endpoints */
struct usb_hub *hubp; /* Hub node used for hubs */
} usb_dev;
typedef struct ohci_device {
u8 type; /* Device or hub */
struct usb_dev dev; /* usb device structure */
u32 openid; /* Id for repeated open/close */
u32 opened; /* Device currently opened? */
} ohci_device;
#define OHCI_CC_ERROR_SHIFT 28
#define OHCI_CC_NOERROR 0x00000000 /* No error */
#define OHCI_CC_CRC 0x10000000 /* CRC */
#define OHCI_CC_BITSTUFF 0x20000000 /* Bitstuffing */
#define OHCI_CC_DTMISMATCH 0x30000000 /* DataToggleMismatch */
#define OHCI_CC_STALL 0x40000000 /* Stall */
#define OHCI_CC_DEVNOTRESP 0x50000000 /* DeviceNotResponding */
#define OHCI_CC_PIDCHK 0x60000000 /* PidCheckFailure */
#define OHCI_CC_PID 0x70000000 /* UnexpectedPid */
#define OHCI_CC_DOVERRUN 0x80000000 /* DataOverrun */
#define OHCI_CC_DUNDERRUN 0x90000000 /* DataUnderrun */
#define IUSB_CC_ERR(X) (-(7000 + (X >>OHCI_CC_ERROR_SHIFT)))
#define IUSB_ERROR_CRC IUSB_CC_ERR(OHCI_CC_CRC)
#define IUSB_ERROR_BITSTUFF IUSB_CC_ERR(OHCI_CC_BITSTUFF)
#define IUSB_ERROR_DTMISMATCH IUSB_CC_ERR(OHCI_CC_DTMISMATCH)
#define IUSB_ERROR_STALL IUSB_CC_ERR(OHCI_CC_STALL)
#define IUSB_ERROR_DEVNOTRESP IUSB_CC_ERR(OHCI_CC_DEVNOTRESP)
#define IUSB_ERROR_PIDCHK IUSB_CC_ERR(OHCI_CC_PICCHK)
#define IUSB_ERROR_PID IUSB_CC_ERR(OHCI_CC_PID)
#define IUSB_ERROR_DOVERRUN IUSB_CC_ERR(OHCI_CC_DOVERRUN)
#define IUSB_ERROR_DUNDERRUN IUSB_CC_ERR(OHCI_CC_DUNDERRUN)
#define IUSB_ERROR_CC_ABORT IUSB_CC_ERR(OHCI_CC_ABORT)
#define IUSB_ERROR_BOVERRUN IUSB_CC_ERR(OHCI_CC_BOVERRUN)
#define IUSB_ERROR_BUNDERRUN IUSB_CC_ERR(OHCI_CC_BUNDERRUN)
#define IUSB_ERROR_NOTACCESSED IUSB_CC_ERR(OHCI_CC_NOTACC)
#define IUSB_ERROR_NOTACCESSED2 IUSB_CC_ERR(OHCI_CC_NOTACC2)
#define IUSB_ERROR_CTRL_XFER -7021
#define IUSB_ERROR_CANCELLED -7022
#define IUSB_ERROR_TIMEOUT -7023
#define IUSB_TIMEOUT_MESSAGE 0xdeadbeef
The resource manager thread starts as the main thread at priority IOS_PRIORITY_OH0 (72). After the host-controller is initialized, the event thread is started at priority IOS_PRIORITY_OH0 and the main thread lowers its own priority to (IOS_PRIORITY_OH0 - 1) and becomes the resource manager thread.
The main thread initializes the RM and the host-controller hardware. As the resource manager thread, it handles user requests for transfers, etc.
The event handler thread handles the event, IOS_EVENT_OH0.
The OHCI0 RM has separate memory regions allocated for the HCCA (1K) and interrupt ED space (4K).
Certain pathological devices randomly fail to respond to requests during enumeration. It was found that retrying a few times always works. So we make up to 3 attempts to enumerate any attached device.
Enumerated devices are assigned an address as required. However, this device address is not exposed to users directly. Users specify devices via a handle obtained by using the IOS_Open call.
Opening the host-controller driver /dev/usb/oh0 returns the reserved handle OHC_RSVD_HANDLE.
Control transfers are used internally by the OHCI0 RM in device enumeration and to control hubs. The OHCI0 RM uses interrupt transfers to detect changes in hub status. An internal message queue is used by the RM thread to get completion replies from the event handler thread.
The endpoints in the interrupt transfer tree are allocated statically and enqueued into a tree structure as specified by the OHCI specifications. Control, bulk and isochronous endpoints are allocated and enqueued, when they are found in connected devices.
OHCI imposes constraints on the size and number of packets per isochronous transfer.
Please see
The EHCI RM is based on the OHCI0 RM. However, the EHCI host-controller is based on UHCI (another implementation of hardware for USB 1.1). Hence transfer mechanisms and related data structures in the EHCI RM are substantially different from OHCI. EHCI also needs to handle the case, when USB 1.1 devices are connected to USB 2.0 hubs. A special mechanism, "split transactions", is used to support this case. It is useful to read the sections about OHCI0 before reading this section. A description of the individual files can be found in a later section.
typedef struct ehci_capregs {
u8 caplength; /* Capability register length */
u8 rsvd; /* Reserved */
u16 hciversion; /* Interface version number */
u32 hcsparams; /* Structural parameters */
u32 hccparams; /* Capability parameters */
u32 hcsp; /* Companion port route description */
} ehci_capregs __attribute__((aligned(32)));
typedef struct ehci_opregs {
u32 usbcmd; /* Command */
u32 usbsts; /* Status */
u32 usbintr; /* Interrupt enable */
u32 fridx; /* Frame index */
u32 ctrldss; /* 4G segment */
u32 plbase; /* Frame list base */
u32 aladdr; /* Async list address */
u32 rsvd[9]; /* Reserved */
u32 cfgflg; /* Configured flag */
u32 portsc[4]; /* Port status/control*/
} ehci_opregs __attribute__((aligned(32)));
#define EHC_QTD_ALIGN 32
typedef struct ehci_qtd {
u32 nextQTD;
u32 altNextQTD;
u32 token;
u32 buf[5];
/* Stuff for HCD's use */
EHCIrp *irp;
u32 len;
} ehci_qtd __attribute__((aligned(EHC_QTD_ALIGN)));
#define EHC_QH_ALIGN 32
typedef struct ehci_qh {
u32 qhlp;
u32 ecap1;
u32 ecap2;
u32 qtd;
/* xfer overlay */
u32 nextQTD;
u32 altNextQTD;
u32 token;
u32 buf0;
u32 buf1;
u32 buf2;
u32 buf3;
u32 buf4;
/* HCD use only */
u32 ep; /* devEp pointer */
} ehci_qh __attribute__((aligned(EHC_QH_ALIGN)));
#define EHC_ITD_ALIGN 32
typedef struct ehci_itd {
u32 next;
u32 stat[8];
u32 buf[7];
/* Stuff for HCD's use */
EHCIrp *irp;
u32 len;
u32 ep;
} ehci_itd __attribute__((aligned(EHC_ITD_ALIGN)));
#define EHC_SITD_ALIGN 32
typedef struct ehci_sitd {
u32 next;
u32 ecap;
u32 sched;
u32 stat;
u32 buf0;
u32 buf1;
u32 back;
/* Stuff for HCD's use */
EHCIrp *irp;
u32 len;
u32 ep;
} ehci_sitd __attribute__((aligned(EHC_SITD_ALIGN)));
typedef struct ehci_hcd {
ehci_opregs *opregs; /* Operational registers in IO space */
ehci_capregs *capregs; /* Capability registers */
u32 *pflist; /* Periodic frame list */
/* For HCD use */
u32 frm_int; /* need to save frame interval */
IOSTimerId tmr;
IOSTimerId tmrEv;
IOSMessageQueueId qid;
IOSMessageQueueId qidEv;
IOSMessageQueueId rm_qid;
IOSMessage mq[1];
IOSMessage mqEv[1];
IOSEvent event;
u8 ndp;
u8 ncc;
u32 flags;
u8 hc_rdb;
u8 hc_wb;
IOSThreadId rmId;
u32 loadns;
u32 openid;
EHCCtrlReq *req;
IRPList irp;
u32 asyncH;
DBList db;
u32 split_us; /* periodic split txn us allocated */
} ehci_hcd;
typedef struct usb20_hub {
u8 devnum;
u8 ndp;
u8 ndp_stat;
u8 potpgt;
u8 data[4] __attribute__((aligned(32)));
} usb20_hub;
typedef struct usb20_dev {
u8 hub;
u8 port;
u16 spd;
u8 maxPower;
u16 vid;
u16 pid;
u8 devClass;
u8 subClass;
u8 protocol;
u8 nintf;
IOSResourceRequest *notifyRemoval;
usb_intf intf[EHCI_DEV_MAX_INTF];
/* XXX Assumes 1 configuration for now */
usb20_dev_ep ep[EHCI_DEV_MAX_EP];
struct usb20_hub *hubp;
} usb20_dev;
typedef struct ehci_device {
u8 type;
usb20_dev dev;
u32 openid;
u32 opened;
} ehci_device;
typedef struct EHCIrp {
IOSResourceRequest *req;
s8 ntd; /* Number of TDs in this request */
u8 err; /* Number of TDs which reported errors */
s32 done; /* Bytes transferred */
void *data; /* Data to be read or written */
u32 datalen; /* Size of transferred data */
struct ehci_qh *qh;
struct EHCIrp *nxt;
struct ehci_qtd *qtd;
struct ehci_itd *itd;
struct ehci_sitd **sitd;
IOSMessageQueueId qid; /* for internal use only */
EHCCtrlReq *clean1;
u16 *size; /* isoc packet sizes */
u8 numPkts; /* number of isoc packets */
u8 devnum; /* Destination device number */
} EHCIrp;
#define IUSB_ERROR_NOERROR 0
#define IUSB_ERROR_DBUFFER -7100 /* Data buffer error */
#define IUSB_ERROR_BABBLE -7101 /* Babble detected */
#define IUSB_ERROR_HALT -7102 /* Endpoint halted */
#define IUSB_ERROR_DEVERR -7103 /* Transaction error. Invalid
* response from device - CRC,
* timeout, bad PID, etc.
*/
#define IUSB_ERROR_TT_ERR -7104 /* Transaction translator error */
#define IUSB_ERROR_XACT_ERR -7105 /* XactErr */
#define IUSB_ERROR_MUF_ERR -7106 /* Missed uframe */
#define IUSB_ERROR_CTRL_XFER -7021
#define IUSB_ERROR_CANCELLED -7022
#define IUSB_ERROR_SCHED -7024 /* EHCI scheduling error */
The resource manager thread starts as the main thread at priority IOS_PRIORITY_EHC (97). As described in a section, after port transfer, the priority is lowered to IOS_PRIORITY_EHC_RUN (73). After the host-controller is initialized, the event thread is started at priority IOS_PRIORITY_EHC_RUN and the main thread lowers its own priority to (IOS_PRIORITY_EHC_RUN - 1) and becomes the resource manager thread.
The main thread initializes the RM and the host-controller hardware. As the resource manager thread, it handles user requests for transfers, etc.
The event handler thread handles the event, IOS_EVENT_EHC.
The handling of port routing in our system is somewhat complicated. OH0 needs to run at a higher priority that EHCI or OH1 RMs, in order for the wiimote to be responsive. However, OH0 cannot be started before EHC, because EHC needs to hand off port 3 to OH0. If the OH1 RM were running and handling the wired Bluetooth device, initializing EHC would cause the device to be disconnected from OH1 and reconnected. However, OH1 and the Bluetooth software do not handle hotplug.
The EHCI RM is started by the IOP OS at priority IOS_PRIORITY_EHC, which is higher than the priority of the OHCI RMs. It transfers port 3 to OH1 and lowers its priority to IOS_PRIORITY_EHC_RUN. At this point, OH1 gets to run. Later, when EHC runs again, it transfers any low and full speed devices connected to port 1 or port 2 to OH0. Port 4 is not accessible and cannot be used.
As in OHCI0, internal blocking control transfers are used to enumerate devices and control hubs. Internal non-blocking interrupt transfers are used to detect changes in hub status.
EHCI uses a frame list size of 1024. Interrupt QHs are allocated, enqueued and dequeued dynamically in the EHCI RM.