OHCI Resource Managers

Contents

Introduction

This document describes the current implementation of USB resource managers on the IOP.

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.

Overview of USB hardware

Host-controllers and ports

The Wii has an EHCI host-controller with 3 ports. 2 companion 1.1 OHCI controllers are included. Port 3 is wired to a USB 1.1 Bluetooth host-controller, BRCM2045 and ports 1, 2 are externally accessible.

Memory access

The USB host-controllers are on the DMA AHB and can access buffers in DDR and Napa in normal mode. In compat mode, OHCI1 can access SRAM. Interrupts generated by the the host-controllers are received by the IOP and are handled by the appropriate resource managers. The PPC does not have direct access to the USB host-controllers.

Coherence

Proper flushing and invalidation must be done to maintain cache coherence between the OHCI0 RM and the OHCI0 hardware. There is a data cache in the IOP, in addition to read and write AHB caches for the IOP and OH0 blocks. Please see the document "AHB sub-system specification", section 1.5.

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.

Endianness

USB host-controllers are always little-endian. However, the rest of our system is big-endian. The hardware supports data-lane steering in some cases. It it important to understand how endian-ness works in the system. It is explained in the document "AHB sub-system specification", section 1.6 .

Initialization

Initialization of USB hardware (like USB PLLs) is done by the IOP kernel, when the system boots up. However, some of the chicken-bits in ACR related to USB need to be initialized after the host-controller is reset. This is handled by the USB host-controller resource managers.

Overview of USB resource managers

In the sources, you will find 3 kinds of OHCI resource managers and 1 EHCI resource manager. See the README included in the EHCI sources for a description of the EHCI RM. The 3 OHCI RMs are:

Accessing USB resource managers

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.

USB features supported by the OHCI0 RM

Internals of the OHCI0 resource manager

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.

Data structures

This section describes some of the data structures used by the OHCI0 RM. All sections referenced are from the OHCI specifications.

Operational registers structure

Host-controller operational registers [Section 7.1].
 
            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)));
         

HCCA structure

Host-controller communications area [Section 4.4].
            #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)));
        

Transfer and endpoint descriptors

The following transfer descriptors and endpoint descriptors are defined. Elements for the RM's internal use are noted. [Section 4]
            #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)));

        

Host-controller structure

             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;
         

Device nodes

Each inserted device or hub is allocated a device node. The following structures describe device and hub nodes.
            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;
        

Operational model

This section provides an overview of the operation of the OHCI0 resource manager.

Please see the EHCI README for information about port routing (handover of ports from EHCI to OHCI, if full or low speed devices are connected to the root hub). Port routing is transparent to the OHCI0 resource manager and is entirely handled by the EHCI resource manager (and the hardware).

Threads and priorities

The OHCI0 RM has 2 threads
  1. Resource manager thread
  2. Event thread

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.

Memory

The OHCI0 resource manager is allocated space in the secure region of DDR. It allocates TDs, EDs and other data structures for its internal use from a heap of size 16KB. OHCI restricts data transfers per TD to 8K. This means depending upon connected devices, on-going transfers, etc the maximum buffer that can be transferred per user request is constrained. In practice, this is found to be about 1M per request. Larger transfers can be accomplished by breaking up the request into smaller chunks.

The OHCI0 RM has separate memory regions allocated for the HCCA (1K) and interrupt ED space (4K).

Initialization

When the IOP OS starts, it loads and runs the OHCI0 RM. As described above, the resource manager thread is started first. The following initialization steps are performed The event handler thread handles interrupts from

Device enumeration

The device enumeration process is described in Chapter 9 of the USB specifications. When the root-hub is initialized, any connected devices are enumerated. Devices are also enumerated, when the event-handler thread receives an event corresponding to a root-hub status change and informs the resource manager thread about it. If the device enumerated is a hub, all devices connected to the hub are also enumerated.

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.

Resource manager

List of commands

The resource manager loop handles The following is a list of commands handled by the main resource manager loop The following IOCTLs are supported The following IOCTLVs are supported

Device handles

The value returned to IOS_Open() for a specific device, (VID, PID), is a combination of the index of the device node in the device node list, __ohci_dev in bits [7:0] and openid, which assigns a unique identifier for the current instance of this device in bits [31:8]. IOP OS maintains the mapping between handles assigned by resource managers and the actual file-descriptors returned to the callers of IOS_Open.

Opening the host-controller driver /dev/usb/oh0 returns the reserved handle OHC_RSVD_HANDLE.

Notes

List of RM files and their description

Public headers

IOP USB API library

Resource manager headers

Resource manager sources

References

  1. USB 2.0 specification
  2. Errata for USB 2.0 specification
  3. USB 1.1 specifications (included in our tree). No longer available on USB Forum's website.
  4. EHCI specifications
  5. OHCI specifications
  6. UHCI specifications
  7. Device class documents
  8. USB Language ID codes
  9. AHB sub-system specification [ATI (AMD) document]