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

📄 ohcd.c

📁 EP931X系列的WinCEUSB驱动源代码
💻 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:  
    ohcd.c
    
Abstract:  
    Platform dependant part of the USB Open Host Controller Driver (OHCD).

Notes: 
--*/
#include <windows.h>
#include <nkintr.h>
#include <oalintr.h>
#include <ceddk.h>
#include <ohcdddsi.h>
#include <hwdefs.h>
#include <haluser.h>

// Registry key and value names
#define OHCI_DRIVER_KEY         TEXT("Drivers\\BuiltIn\\OHCI")
#define USE_EXISTING_VALUE_NAME TEXT("UseExistingSettings")
#define IRQ_VALUE_NAME          TEXT("Irq")
#define IOBASE_VALUE_NAME       TEXT("MemBase")
#define EGPIO2CLEAR_NAME        TEXT("Egpio2Clear")
#define EGPIO9CLEAR_NAME        TEXT("Egpio9Clear")


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

typedef struct _SOhcdPdd
{
    LPVOID lpvMemoryObject;
    LPVOID lpvOhcdMddObject;
} SOhcdPdd;

#define UnusedParameter(x)  x = x

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

    return TRUE;
}




/* 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
    BOOL *pfEGPIO2Clear,
    BOOL *pfEGPIO9Clear
) 
{
    HKEY    hKey;
    BOOL    fRet=FALSE;
    DWORD   dwRet;
    DWORD   dwSize;
    DWORD   dwType;


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

    dwSize = 0;
    dwType = REG_DWORD;
    dwRet = RegQueryValueEx(hKey, EGPIO2CLEAR_NAME, 0, &dwType, (PUCHAR)pfEGPIO2Clear, &dwSize);
    if (dwRet != ERROR_SUCCESS || dwType != REG_DWORD ) 
    {
        *pfEGPIO2Clear = TRUE;
    }

    dwSize = 0;
    dwType = REG_DWORD;
    dwRet = RegQueryValueEx(hKey, EGPIO9CLEAR_NAME, 0, &dwType, (PUCHAR)pfEGPIO9Clear, &dwSize);
    if (dwRet != ERROR_SUCCESS || dwType != REG_DWORD ) 
    {
        *pfEGPIO9Clear = TRUE;
    }

    dwRet = RegSetValueEx(hKey,IRQ_VALUE_NAME,0,REG_DWORD,(PUCHAR)&dwIrq,sizeof(DWORD));
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR, (TEXT("!OHCD: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("!OHCD:SetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
                             IOBASE_VALUE_NAME, dwRet));
        goto SetRegistryConfig_exit;
    }
    fRet = TRUE;

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



/* InitializeOHCI
 *
 *  Configure and initialize OHCI card
 *
 * Return Value:
 *  Return TRUE if card could be located and configured, otherwise FALSE
 */
static BOOL 
InitializeOHCI
(
    SOhcdPdd * pPddObject,    // IN - Pointer to PDD structure
    LPCWSTR szDriverRegKey)   // IN - Pointer to active registry key string
{
    BOOL    fResult = FALSE;
    LPVOID pobMem = NULL;
    LPVOID pobOhcd = NULL; 
    ULONG   ulTemp;
    BOOL    bEgpio2Clear = 0;
    BOOL    bEgpio9Clear = 0;

    
    DEBUGMSG(ZONE_INIT, (TEXT("OHCD Init\r\n")));
    fResult = SetRegistryConfig(SYSINTR_USB, USB_BASE, &bEgpio2Clear, &bEgpio9Clear);

    if(fResult)
    {
        
        //
        // Turn on the USB clock here.
        //
        HalWriteCommonReg(CSC_PWRCNT, PWRCNT_USB_EN, PWRCNT_USB_EN);


        //
        // On the EP9301 board, EGPIO 2 needs to be cleared so that power
        // can be supplied to the USB port.
        // Change EGPIO to an input so that the MAX891 can power the USB port.
        //
        if(bEgpio2Clear)
        {
            HalWriteCommonReg(GPIO_PADDR, 0x4, 0x4);
            HalWriteCommonReg(GPIO_PADR,  0x4, 0x0);
        }
        if(bEgpio9Clear)
        {
            HalWriteCommonReg(GPIO_PBDDR, 0x2, 0x2);
            HalWriteCommonReg(GPIO_PBDR,  0x2, 0x0);
        }

        Sleep(10);


        //
        // 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 = HcdMdd_CreateMemoryObject(gcTotalAvailablePhysicalMemory, 
                                            gcHighPriorityPhysicalMemory, NULL,NULL); 

        if(pobMem)
        {
            //dpCurSettings.ulZoneMask = 0xFFFFFFFF;
            pobOhcd = HcdMdd_CreateHcdObject
            (
                pPddObject, 
                pobMem,
                szDriverRegKey, 
                (unsigned char *)USB_BASE, 
                SYSINTR_USB
            );
            //dpCurSettings.ulZoneMask = 0xFFFFFFFF;
            

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

        if(!fResult)
        {
            if(pobOhcd)
                HcdMdd_DestroyHcdObject(pobOhcd);
            if(pobMem)
                HcdMdd_DestroyMemoryObject(pobMem);

            pobOhcd = NULL;
            pobMem = NULL;
        }
    }

    pPddObject->lpvMemoryObject = pobMem;
    pPddObject->lpvOhcdMddObject = pobOhcd;

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

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

    return (DWORD)pPddObject;
}

/* OhcdPdd_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 HcdPdd_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 HcdPdd_PowerUp(DWORD hDeviceContext)
{
    SOhcdPdd * pPddObject = (SOhcdPdd *)hDeviceContext;
    HcdMdd_PowerUp(pPddObject->lpvOhcdMddObject);
    return;
}


extern void HcdPdd_PowerDown(DWORD hDeviceContext)
{
    SOhcdPdd * pPddObject = (SOhcdPdd *)hDeviceContext;

    HcdMdd_PowerDown(pPddObject->lpvOhcdMddObject);

    return;
}


extern BOOL HcdPdd_Deinit(DWORD hDeviceContext)
{
    SOhcdPdd * pPddObject = (SOhcdPdd *)hDeviceContext;

    if(pPddObject->lpvOhcdMddObject)
        HcdMdd_DestroyHcdObject(pPddObject->lpvOhcdMddObject);
    if(pPddObject->lpvMemoryObject)
        HcdMdd_DestroyMemoryObject(pPddObject->lpvMemoryObject);

    return TRUE;
}


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

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


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

    return TRUE;
}


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

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


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

    return (DWORD)-1;
}


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

    return (DWORD)-1;
}


extern BOOL HcdPdd_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;
}

/* @func  void | HcdPdd_InitiatePowerUp.
 *
 * @comm  This function will reinitialize the HCD after a Resume event. It should be called from the USB IST after a
 *        resume has been detected.
 * 
 */
void    HcdPdd_InitiatePowerUp(void)
{
}

⌨️ 快捷键说明

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