欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

system.c

WinCE 3.0 BSP, 包含Inter SA1110, Intel_815E, Advantech_PCM9574 等
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:  
    system.c
    
Abstract:  
    Device dependant part of the USB Universal Host Controller Driver (UHCD).

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

// NOTENOTE: for x86 platforms
//
// CEPC device drivers should get their IRQ number from the registry and use
// MapIrq2SysIntr to get the appropriate SYSINTR_* number to use.
#ifdef x86
_inline
DWORD
MapIrq2SysIntr(DWORD _Irq)
{
    if( _Irq<=15 )
        return ( SYSINTR_FIRMWARE + _Irq );
    else
        return (0xffffffff);
}
#endif

// Registry key and value names
#define UHCI_DRIVER_KEY         TEXT("Drivers\\BuiltIn\\UHCI")
#define USE_EXISTING_VALUE_NAME TEXT("UseExistingSettings")
#define SYSINTR_VALUE_NAME      TEXT("SysIntr")
#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 SysIntr 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 * lpdwSysIntr,    // OUT- Receives SysIntr 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;
    }

#ifdef x86
    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;
#endif

    dwSize = sizeof(dwData);
    dwRet = RegQueryValueEx(hKey,SYSINTR_VALUE_NAME,0,&dwType,(PUCHAR)&dwData,&dwSize);
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR, (TEXT("!UHCD:GetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
                              SYSINTR_VALUE_NAME, dwRet));
        goto GetRegistryConfig_exit;
    }
    *lpdwSysIntr = 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 SysIntr 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 dwSysIntr,    // IN - SysIntr 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,SYSINTR_VALUE_NAME,0,REG_DWORD,(PUCHAR)&dwSysIntr,sizeof(DWORD));
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR, (TEXT("!UHCD:SetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
                              SYSINTR_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
    DWORD *pSysIntr)      // IN - contains SYSINTR from registry
                           // OUT - new SYSINTR if fUseExistingSettings & x86

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

    ULONG               portBase;
    PHYSICAL_ADDRESS    ioPhysicalBase;
    ULONG               inIoSpace = 0;      // memory space is default
    ULONG               portRange = 0x70; // cDefaultUhcdPortRange;

// when detecting a PIIX4, these are the values which appear
#define UHCD_VENDOR_ID_INTEL ((USHORT)0x8086)
#define UHCD_DEVICE_ID_PIIX4 ((USHORT)0x7112)

// values for DSB-500
#define UHCD_VENDOR_ID_DLINK ((USHORT)0x1106)
#define UHCD_DEVICE_ID_DSB500 ((USHORT)0x3038)

// 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.
// DSB-500 does this too.
#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;

                if ((pciConfig.BaseClass == PCI_CLASS_SERIAL_BUS_CTLR) &&
                    (pciConfig.SubClass  == PCI_SUBCLASS_SB_USB) &&
                    (pciConfig.ProgIf    == 0x00)) { // UHCI 

                    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 ]) );
                              
#ifdef x86
                    // Update current config
                    if (fUseExistingSettings) {
                        *pSysIntr = MapIrq2SysIntr(pciConfig.u.type0.InterruptLine);
                    }
                    else if ((MapIrq2SysIntr(pciConfig.u.type0.InterruptLine) != (UCHAR) *pSysIntr) ||
                        (pciConfig.u.type0.BaseAddresses[ UHCD_PIIX4_ADDRESS_REG ] != (DWORD)*pioPortBase)) {
                        RETAILMSG(1,(TEXT("!UHCD:  Configuration different from registry: IRQ: %u(%u), PortBase: 0x%X (reg: %u, 0x%X)\r\n"),
                                     pciConfig.u.type0.InterruptLine,MapIrq2SysIntr(pciConfig.u.type0.InterruptLine),pciConfig.u.type0.BaseAddresses[ UHCD_PIIX4_ADDRESS_REG ],
                                     (UCHAR)*pSysIntr,(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 (MapIrq2SysIntr(pciConfig.u.type0.InterruptLine) != (UCHAR) *pSysIntr) {
                            bFoundIt = FALSE;
                            break;
                        }
                    }
#endif
                    // Save off SysIntr and Port Base to registry
                    *pioPortBase = (PUCHAR)pciConfig.u.type0.BaseAddresses[ UHCD_PIIX4_ADDRESS_REG ];

⌨️ 快捷键说明

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