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

📄 phcd.c

📁 ISP1161 USB Driver under WinCE for StrongARM processor implementation
💻 C
字号:
/*++
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:  
    phcd.c
    
Abstract:  
    Platform dependant part of the USB Open Host Controller Driver (PHCD).

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

// Registry key and value names
#define PHCI_DRIVER_KEY         TEXT("Drivers\\BuiltIn\\PHCI")
#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 _SPhcdPdd
{
    LPVOID lpvMemoryObject;
    LPVOID lpvPhcdMddObject;
} SPhcdPdd;

#define UnusedParameter(x)  x = x

/* PhcdPdd_DllMain
 * 
 *  DLL Entry point.
 *
 * Return Value:
 */
extern BOOL PhcdPdd_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,PHCI_DRIVER_KEY,0,0,&hKey);
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR,(TEXT("!PHCD:GetRegistryConfig RegOpenKeyEx(%s) failed %d\r\n"),
                             PHCI_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("!PHCD: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("!PHCD: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("!PHCD: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,PHCI_DRIVER_KEY,0,0,&hKey);
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR,(TEXT("!PHCD:SetRegistryConfig RegOpenKeyEx(%s) failed %d\r\n"),
                             PHCI_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("!PHCD: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("!PHCD:SetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
                             IOBASE_VALUE_NAME, dwRet));
        goto SetRegistryConfig_exit;
    }
    fRet = TRUE;

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

/* ConfigurePHCICard
 * 
 *    Configure PHCI 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
ConfigurePHCICard(
    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

{
    const USHORT cDefaultPhcdPortRange = 0x70;

    ULONG               portRange = cDefaultPhcdPortRange;
    PHYSICAL_ADDRESS    ioPhysicalBase;

	/*
	
	PULONG *pioArmRegBase = 0;

	ioPhysicalBase.LowPart = (ULONG)0x90040000;
	ioPhysicalBase.HighPart = 0;

    if ((*pioArmRegBase = (PULONG)MmMapIoSpace(ioPhysicalBase, 0x4, FALSE)) == NULL) 
	{
        // We may as well not continue
		DEBUGMSG(ZONE_INIT | ZONE_ERROR, 
                         (TEXT("PHCD: Error mapping I/O Ports.\r\n")));
		return FALSE;
	}

	RETAILMSG(1, (TEXT("PHCD: ulGPLR 0x%08X\r\n"), READ_REGISTER_ULONG((PULONG)*pioArmRegBase)));
*/

	/////////////
	
    ioPhysicalBase.LowPart = (ULONG)*pioPortBase;
	ioPhysicalBase.HighPart = 0;

    if ((*pioPortBase = (PUCHAR)MmMapIoSpace(ioPhysicalBase, portRange, FALSE)) == NULL) 
	{
        // We may as well not continue
		DEBUGMSG(ZONE_INIT | ZONE_ERROR, 
                         (TEXT("PHCD: Error mapping I/O Ports.\r\n")));
		return FALSE;

	}

	

//  DEBUGMSG(ZONE_PCI, 
	RETAILMSG(1, 
             (TEXT("PHCD: ioPhysicalBase 0x%X, ioPortBase 0x%X\r\n"),
              ioPhysicalBase.LowPart,  *pioPortBase));

    
    return TRUE;
}


/* InitializePHCI
 *
 *  Configure and initialize PHCI card
 *
 * Return Value:
 *  Return TRUE if card could be located and configured, otherwise FALSE
 */
static BOOL 
InitializePHCI(
    SPhcdPdd * pPddObject,    // IN - Pointer to PDD structure
    LPCWSTR szDriverRegKey)   // IN - Pointer to active registry key string
{
    DWORD dwUseExistingSettings;
    PUCHAR ioPortBase = NULL;
    DWORD dwSysIntr, dwIRQ;
    BOOL fResult = FALSE;
    LPVOID pobMem = NULL;
    LPVOID pobPhcd = NULL;

    if (!GetRegistryConfig(&dwUseExistingSettings, &dwIRQ, (DWORD *)&ioPortBase)) 
	{
        RETAILMSG(1,(TEXT("!PHCD: Error reading registry settings\r\n")));
        return FALSE;
    }
    
	DEBUGMSG(ZONE_INIT,(TEXT("PHCD: Read config from registry: Use existing: %u, IRQ: %u, I/O base: %X\r\n"),
                        dwUseExistingSettings,dwIRQ,ioPortBase));

    fResult   = ConfigurePHCICard(!!dwUseExistingSettings,&ioPortBase, &dwIRQ);

    if(fResult)
    {
       // dwSysIntr = MapIrq2SysIntr(dwIRQ);
		dwSysIntr = dwIRQ;

        DEBUGMSG(ZONE_INIT,(TEXT("PHCD: MapIrq2SysIntr(%u): %u\r\n"),dwIRQ,dwSysIntr));

        // The PDD can supply a buffer of contiguous physical memory here, or can let the 
        // MDD try to allocate the memory from system RAM.  In our case, let the MDD do it.
        pobMem = PhcdMdd_CreateMemoryObject(gcTotalAvailablePhysicalMemory, 
                                            gcHighPriorityPhysicalMemory, NULL,NULL); 

        if(pobMem)
        {
            pobPhcd = PhcdMdd_CreatePhcdObject(pPddObject, pobMem,
                    szDriverRegKey, ioPortBase, dwSysIntr);

            fResult = pobPhcd ? TRUE : FALSE;
        }
        else
            fResult = FALSE;

        if(!fResult)
        {
            if(pobPhcd)
                PhcdMdd_DestroyPhcdObject(pobPhcd);
            if(pobMem)
                PhcdMdd_DestroyMemoryObject(pobMem);

            pobPhcd = NULL;
            pobMem = NULL;
        }
    }

    pPddObject->lpvMemoryObject = pobMem;
    pPddObject->lpvPhcdMddObject = pobPhcd;

    return fResult;
}

/* PhcdPdd_Init
 *
 *   PDD Entry point - called at system init to detect and configure PHCI card.
 *
 * Return Value:
 *   Return pointer to PDD specific data structure, or NULL if error.
 */
extern DWORD 
PhcdPdd_Init(
    DWORD dwContext)  // IN - Pointer to context value. For device.exe, this is a string 
                      //      indicating our active registry key.
{
    SPhcdPdd *  pPddObject = malloc(sizeof(SPhcdPdd));
    BOOL        fRet = FALSE;

    fRet = InitializePHCI(pPddObject, (LPCWSTR)dwContext);

    if(!fRet)
    {
        free(pPddObject);
        pPddObject = NULL;
    }

    return (DWORD)pPddObject;
}

/* PhcdPdd_CheckConfigPower
 *
 *    Check power required by specific device configuration and return whether it
 *    can be supported on this platform.  For CEPC, this is trivial, just limit to
 *    the 500mA requirement of USB.  For battery powered devices, this could be 
 *    more sophisticated, taking into account current battery status or other info.
 *
 * Return Value:
 *    Return TRUE if configuration can be supported, FALSE if not.
 */
extern BOOL PhcdPdd_CheckConfigPower(
    UCHAR bPort,         // IN - Port number
    DWORD dwCfgPower,    // IN - Power required by configuration
    DWORD dwTotalPower)  // IN - Total power currently in use on port
{
    return ((dwCfgPower + dwTotalPower) > 500) ? FALSE : TRUE;
}

extern void PhcdPdd_PowerUp(DWORD hDeviceContext)
{
    SPhcdPdd * pPddObject = (SPhcdPdd *)hDeviceContext;
    PhcdMdd_PowerUp(pPddObject->lpvPhcdMddObject);

    return;
}


extern void PhcdPdd_PowerDown(DWORD hDeviceContext)
{
    SPhcdPdd * pPddObject = (SPhcdPdd *)hDeviceContext;

    PhcdMdd_PowerDown(pPddObject->lpvPhcdMddObject);

    return;
}


extern BOOL PhcdPdd_Deinit(DWORD hDeviceContext)
{
    SPhcdPdd * pPddObject = (SPhcdPdd *)hDeviceContext;

    if(pPddObject->lpvPhcdMddObject)
        PhcdMdd_DestroyPhcdObject(pPddObject->lpvPhcdMddObject);
    if(pPddObject->lpvMemoryObject)
        PhcdMdd_DestroyMemoryObject(pPddObject->lpvMemoryObject);

    return TRUE;
}


extern DWORD PhcdPdd_Open(DWORD hDeviceContext, DWORD AccessCode,
        DWORD ShareMode)
{
    UnusedParameter(hDeviceContext);
    UnusedParameter(AccessCode);
    UnusedParameter(ShareMode);

    return 1; // we can be opened, but only once!
}


extern BOOL PhcdPdd_Close(DWORD hOpenContext)
{
    UnusedParameter(hOpenContext);

    return TRUE;
}


extern DWORD PhcdPdd_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
{
    UnusedParameter(hOpenContext);
    UnusedParameter(pBuffer);
    UnusedParameter(Count);

    return (DWORD)-1; // an error occured
}


extern DWORD PhcdPdd_Write(DWORD hOpenContext, LPCVOID pSourceBytes,
        DWORD NumberOfBytes)
{
    UnusedParameter(hOpenContext);
    UnusedParameter(pSourceBytes);
    UnusedParameter(NumberOfBytes);

    return (DWORD)-1;
}


extern DWORD PhcdPdd_Seek(DWORD hOpenContext, LONG Amount, DWORD Type)
{
    UnusedParameter(hOpenContext);
    UnusedParameter(Amount);
    UnusedParameter(Type);

    return (DWORD)-1;
}


extern BOOL PhcdPdd_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn,
        DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut)
{
    UnusedParameter(hOpenContext);
    UnusedParameter(dwCode);
    UnusedParameter(pBufIn);
    UnusedParameter(dwLenIn);
    UnusedParameter(pBufOut);
    UnusedParameter(dwLenOut);
    UnusedParameter(pdwActualOut);

    return FALSE;
}

⌨️ 快捷键说明

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