⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uhcd.c

📁 SBC2410 WinCE 5.0 BSP.绝大多数驱动已经调通。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-2000 Microsoft Corporation.  All rights reserved.

Module Name:  
    uhcd.c
    
Abstract:  
    Platform dependant part of the USB Universal Host Controller Driver (UHCD).

Notes: 
--*/
#include <windows.h>
#include <nkintr.h>
#include <oalintr.h>
#include <ceddk.h>
#include <uhcdddsi.h>

// Registry key and value names
#define UHCI_DRIVER_KEY         TEXT("Drivers\\BuiltIn\\UHCI")
#define USE_EXISTING_VALUE_NAME TEXT("UseExistingSettings")
#define IRQ_VALUE_NAME          TEXT("Irq")
#define IOBASE_VALUE_NAME       TEXT("MemBase")

// Amount of memory to use for HCD buffer
static const DWORD gcTotalAvailablePhysicalMemory = 65536; // 64K
static const DWORD gcHighPriorityPhysicalMemory = 0x4000; // 16K

typedef struct _SUhcdPdd
{
    LPVOID lpvMemoryObject;
    LPVOID lpvUhcdMddObject;
} SUhcdPdd;

#define UnusedParameter(x)  x = x

/* UhcdPdd_DllMain
 * 
 *  DLL Entry point.
 *
 * Return Value:
 */
extern BOOL UhcdPdd_DllMain(HANDLE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
{
    UnusedParameter(hinstDLL);
    UnusedParameter(dwReason);
    UnusedParameter(lpvReserved);

    return TRUE;
}


/* GetRegistryConfig
 *
 *   Function to get the IRQ and I/O port range from the registry.  
 *   Note: Will need to be changed to support multiple instances.
 *
 * Return Value:
 *   TRUE for success, FALSE for error
 */
static BOOL
GetRegistryConfig(
    DWORD * lpdwUseExistingSettings, // OUT- Receives value that indicates whether to 
                                     //      just use the resources assigned by the BIOS.  
    DWORD * lpdwIrq,    // OUT- Receives IRQ value
    DWORD * lpdwIoBase) // OUT- Receives I/O base
{
    HKEY hKey;
    DWORD dwData;
    DWORD dwSize;
    DWORD dwType;
    BOOL  fRet=FALSE;
    DWORD dwRet;

    dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,UHCI_DRIVER_KEY,0,0,&hKey);
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR,(TEXT("!UHCD:GetRegistryConfig RegOpenKeyEx(%s) failed %d\r\n"),
                             UHCI_DRIVER_KEY, dwRet));
        return FALSE;
    }
    dwSize = sizeof(dwData);
    dwRet = RegQueryValueEx(hKey,USE_EXISTING_VALUE_NAME,0,&dwType,(PUCHAR)&dwData,&dwSize);
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR, (TEXT("!UHCD:GetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
                              USE_EXISTING_VALUE_NAME, dwRet));
        goto GetRegistryConfig_exit;
    }
    *lpdwUseExistingSettings = dwData;

    dwSize = sizeof(dwData);
    dwRet = RegQueryValueEx(hKey,IRQ_VALUE_NAME,0,&dwType,(PUCHAR)&dwData,&dwSize);
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR, (TEXT("!UHCD:GetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
                              IRQ_VALUE_NAME, dwRet));
        goto GetRegistryConfig_exit;
    }
    *lpdwIrq = dwData;

    dwSize = sizeof(dwData);
    dwRet = RegQueryValueEx(hKey,IOBASE_VALUE_NAME,0,&dwType,(PUCHAR)&dwData,&dwSize);
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR,(TEXT("!UHCD:GetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
                             IOBASE_VALUE_NAME, dwRet));
        goto GetRegistryConfig_exit;
    }
    *lpdwIoBase = dwData;
    fRet = TRUE;

GetRegistryConfig_exit:
    RegCloseKey(hKey);
    return fRet;
}   // GetRegistryConfig


/* SetRegistryConfig
 *
 *   Function to set the IRQ and I/O port range in the registry.
 *   Note: Will need to be changed to support multiple instances.
 *
 * Return Value:
 *   TRUE for success, FALSE for error
 */
static BOOL
SetRegistryConfig(
    DWORD dwIrq,    // IN - IRQ value
    DWORD dwIoBase) // IN - I/O base
{
    HKEY hKey;
    BOOL  fRet=FALSE;
    DWORD dwRet;

    dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,UHCI_DRIVER_KEY,0,0,&hKey);
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR,(TEXT("!UHCD:SetRegistryConfig RegOpenKeyEx(%s) failed %d\r\n"),
                             UHCI_DRIVER_KEY, dwRet));
        return FALSE;
    }

    dwRet = RegSetValueEx(hKey,IRQ_VALUE_NAME,0,REG_DWORD,(PUCHAR)&dwIrq,sizeof(DWORD));
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR, (TEXT("!UHCD:SetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
                              IRQ_VALUE_NAME, dwRet));
        goto SetRegistryConfig_exit;
    }

    dwRet = RegSetValueEx(hKey,IOBASE_VALUE_NAME,0,REG_DWORD,(PUCHAR)&dwIoBase,sizeof(DWORD));
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR,(TEXT("!UHCD:SetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
                             IOBASE_VALUE_NAME, dwRet));
        goto SetRegistryConfig_exit;
    }
    fRet = TRUE;

SetRegistryConfig_exit:
    RegCloseKey(hKey);
    return fRet;
}   // SetRegistryConfig

/* ConfigureUHCICard
 * 
 *    Configure UHCI controller I/O base and IRQ based on settings read from registry.
 *    This should really call routines which access the IRQ routing and memory base through
 *    the PCI BIOS and reconfigure the card.  Since we currently don't have the routines
 *    to do this, use the following algorithm:
 *        -- If fUseExisting flag is set, read the values from the PCI config registers on the
 *           card, and update pioPortBase and pIrq.
 *        -- If fUseExisting is not set, verify that the passed in values of pioPortBase and
 *           pIrq match, and return FALSE if they do not. (change - don't fail if mem base
 *           doesn't match, since this is determined by the PCI config mechanism, it will not
 *           conflict with other cards.)
 *
 * Return Value
 *    Return TRUE if successful, FALSE if card could not be detected, or if configuration
 *    does not match what was specified in the registry, and fUseExistingSettings is not set.
 */
BOOL
ConfigureUHCICard(
    BOOL fUseExistingSettings,
    PUCHAR *pioPortBase,   // IN - contains physical address of register base
                           // OUT- contains virtual address of register base
    ULONG *pIrq)           // IN - contains IRQ value
                           // OUT- (if fUseExistingSettings) contains actual IRQ value

{
    PCI_SLOT_NUMBER     slotNumber;
    PCI_COMMON_CONFIG   pciConfig;
    int                 bus, device, function;
    int                 length;
    BOOL                bFoundIt = FALSE;

// when detecting a PIIX4, these are the values which appear
#define UHCD_VENDOR_ID_INTEL ((USHORT)0x8086)
#define UHCD_DEVICE_ID_PIIX4 ((USHORT)0x7112)
// PIIX4 is strange in that the port base address
// is stored in BaseAddresses[ 4 ]. In fact, the
// BaseAddresses[ 0-3 ] are all 0, for some reason.
#define UHCD_PIIX4_ADDRESS_REG 4

    for (bus = 0; bus < PCI_MAX_BUS; bus++) {
        for (device = 0; device < PCI_MAX_DEVICES; device++) {
            slotNumber.u.bits.DeviceNumber = device;

            for (function = 0; function < PCI_MAX_FUNCTION; function++) {
                slotNumber.u.bits.FunctionNumber = function;

                length = HalGetBusData(PCIConfiguration, bus, slotNumber.u.AsULONG,
                                       &pciConfig, sizeof(pciConfig) - 
                                       sizeof(pciConfig.DeviceSpecific));

                if (length == 0 || pciConfig.VendorID == 0xFFFF) 
                    break;

                // the only UHCI controller I've tested so far is the 
                // PIIX4. The rest of this code will need to be altered
                // to support other controllers.
                //
                // This DEBUGCHK says:
                // "halt if we find an Intel USB controller that is not a PIIX4"
                DEBUGCHK( !(pciConfig.BaseClass == PCI_CLASS_SERIAL_BUS_CTLR &&
                            pciConfig.SubClass  == PCI_SUBCLASS_SB_USB &&
                            pciConfig.VendorID == UHCD_VENDOR_ID_INTEL ) ||
                          pciConfig.DeviceID == UHCD_DEVICE_ID_PIIX4 );

                if (pciConfig.BaseClass == PCI_CLASS_SERIAL_BUS_CTLR &&
                    pciConfig.SubClass  == PCI_SUBCLASS_SB_USB &&
                    pciConfig.VendorID == UHCD_VENDOR_ID_INTEL && 
                    pciConfig.DeviceID == UHCD_DEVICE_ID_PIIX4 ) {

                    RETAILMSG(1,(TEXT("UHCD: Found PCI USB UHCI controller, VendorId: %04X, DeviceId: %04X\r\n"),
                                 pciConfig.VendorID, pciConfig.DeviceID));
                    bFoundIt = TRUE;
                    DEBUGMSG( ZONE_INIT,
                              (TEXT("UHCD: PCI config: Irq = 0x%X, Port Base = 0x%X\r\n"), 
                              pciConfig.u.type0.InterruptLine, pciConfig.u.type0.BaseAddresses[ UHCD_PIIX4_ADDRESS_REG ]) );
                
                    // Update current config
                    if (fUseExistingSettings) {
                        *pIrq = pciConfig.u.type0.InterruptLine;
                        *pioPortBase = (PUCHAR)pciConfig.u.type0.BaseAddresses[ UHCD_PIIX4_ADDRESS_REG ];
                        // Save new values in registry
                        SetRegistryConfig(*pIrq, (DWORD)*pioPortBase);
                    }
                    else if ((pciConfig.u.type0.InterruptLine != (UCHAR) *pIrq) ||
                        (pciConfig.u.type0.BaseAddresses[ UHCD_PIIX4_ADDRESS_REG ] != (DWORD)*pioPortBase)) {
                        RETAILMSG(1,(TEXT("!UHCD:  Configuration different from registry: IRQ %u, PortBase: 0x%X (reg: %u, 0x%X)\r\n"),
                                     pciConfig.u.type0.InterruptLine,pciConfig.u.type0.BaseAddresses[ UHCD_PIIX4_ADDRESS_REG ],
                                     (UCHAR)*pIrq,(DWORD)*pioPortBase));
                        // If IRQ isn't what we expect, fail init (otherwise might conflict with ISA 
                        // devices).  Shouldn't have a problem with conflicting memory addresses, so
                        // don't fail if that doesn't match.
                        if (pciConfig.u.type0.InterruptLine != (UCHAR) *pIrq)
                            bFoundIt = FALSE;
                    }
                    break;
                }
                if (function == 0 && !(pciConfig.HeaderType & 0x80)) 
                    break;
            }
            if (bFoundIt || length == 0)
                break;
        }

        if (bFoundIt || (length == 0 && device == 0))
            break;
    }

    if (!bFoundIt) {
        DEBUGMSG(ZONE_ERROR, (TEXT("UHCD: Error, can't find USB Controller\r\n")));
        return FALSE;
    }
    DEBUGMSG(ZONE_INIT, (TEXT("UHCD: Revision Stepping value = 0x%02X\r\n"), pciConfig.RevisionID));

    // "PCI System Architecture, 3rd Ed" by Shanley/Anderson,
    // chapter 17, page 349 (I/O Base Address Register)
    // states that for I/O mapping, the address register

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -