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

📄 ohcd.c

📁 WinCE 3.0 BSP, 包含Inter SA1110, Intel_815E, Advantech_PCM9574 等
💻 C
字号:
/*

  Copyright(c) 1998,1999 SIC/Hitachi,Ltd.

	Module Name:

		ohcd.c

	Revision History:

		26th April 1999		Released
		14th June  1999		Put the sources in order

*/
#include <windows.h>
#include <nkintr.h>
//#include <oalintr.h>
#include <ceddk.h>
#include <ohcdddsi.h>
//#include <mobytel.h>
#include <cc.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")

// Amount of memory to use for HCD buffer

static const DWORD gcTotalAvailablePhysicalMemory = 0x1000; // 4K total Sram size for HD64465
static const DWORD gcHighPriorityPhysicalMemory = 0x0400; // 1K 

#define HD64465_USB_REGADDR		((PVOID)(HD64465_BASE + HD64465_USB_OFFSET)) // HCD  base register
#define HD64465_USB_REGSIZE		0x0060
#define HD64465_HCCA_PHYADDR	((PVOID)(HD64465_BASE + HD64465_EMBEDED_SDRAM_OFFSET)) // HCCA base register

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

#define UnusedParameter(x)  x = x

/* OhcdPdd_DllMain
 * 
 *  DLL Entry point.
 *
 * Return Value:
 */
extern BOOL OhcdPdd_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,OHCI_DRIVER_KEY,0,0,&hKey);
    if (dwRet != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR,(TEXT("!OHCD:GetRegistryConfig RegOpenKeyEx(%s) failed %d\r\n"),
                             OHCI_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("!OHCD: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("!OHCD: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("!OHCD: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,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;
    }

    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
{
    DWORD dwUseExistingSettings;
    DWORD dwIoPortBase = 0;
    DWORD dwSysIntr, dwIRQ;
    BOOL fResult = FALSE;
    LPVOID pobMem = NULL;
    LPVOID pobOhcd = NULL;
    LPVOID pvUsbRegister;
    LPVOID pvUsbHcca;

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

    pvUsbRegister = VirtualAlloc(0, HD64465_USB_REGSIZE, MEM_RESERVE, PAGE_NOACCESS);

	if ( pvUsbRegister == NULL ) 
	{
		ERRORMSG(1, (TEXT("InitializeOHCI: VirtualAlloc failed! (1)\r\n")));
	}
	if ( !VirtualCopy(pvUsbRegister, (LPVOID)(dwIoPortBase+HD64465_USB_OFFSET), HD64465_USB_REGSIZE, PAGE_READWRITE|PAGE_NOCACHE) ) 
	{
		ERRORMSG(1, (TEXT("InitializeOHCI: VirtualCopy failed! (1)\r\n")));
	}

    fResult=1;

    if(fResult)
    {

        //dwSysIntr=SYSINTR_USB;
		dwSysIntr=dwIRQ;

        // 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.

   	    pvUsbHcca = VirtualAlloc(0,gcTotalAvailablePhysicalMemory+0x10, MEM_RESERVE, PAGE_NOACCESS);
	    if ( pvUsbHcca == NULL ) 
	    {
		    ERRORMSG(1, (TEXT("InitializeOHCI: VirtualAlloc failed! (2)\r\n")));
	    }
	    if ( !VirtualCopy(pvUsbHcca, (LPVOID)(dwIoPortBase+HD64465_EMBEDED_SDRAM_OFFSET),gcTotalAvailablePhysicalMemory+0x10 , PAGE_READWRITE|PAGE_NOCACHE) ) 
	    {
		    ERRORMSG(1, (TEXT("InitializeOHCI: VirtualCopy failed! (1)\r\n")));
	    }
	
        pobMem = OhcdMdd_CreateMemoryObject(gcTotalAvailablePhysicalMemory, 
                                            gcHighPriorityPhysicalMemory, pvUsbHcca,(LPVOID)(dwIoPortBase+HD64465_EMBEDED_SDRAM_OFFSET)); 

        if(pobMem)
        {
            pobOhcd = OhcdMdd_CreateOhcdObject(pPddObject, pobMem,
                    szDriverRegKey, pvUsbRegister, dwSysIntr);

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

        if(!fResult)
        {
            if(pobOhcd)
                OhcdMdd_DestroyOhcdObject(pobOhcd);
            if(pobMem)
                OhcdMdd_DestroyMemoryObject(pobMem);

            pobOhcd = NULL;
            pobMem = NULL;
        }
    }

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

    return fResult;
}

/* OhcdPdd_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 
OhcdPdd_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 OhcdPdd_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 OhcdPdd_PowerUp(DWORD hDeviceContext)
{
    SOhcdPdd * pPddObject = (SOhcdPdd *)hDeviceContext;
    OhcdMdd_PowerUp(pPddObject->lpvOhcdMddObject);

    return;
}


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

    OhcdMdd_PowerDown(pPddObject->lpvOhcdMddObject);

    return;
}


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

    if(pPddObject->lpvOhcdMddObject)
        OhcdMdd_DestroyOhcdObject(pPddObject->lpvOhcdMddObject);
    if(pPddObject->lpvMemoryObject)
        OhcdMdd_DestroyMemoryObject(pPddObject->lpvMemoryObject);

    return TRUE;
}


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

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


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

    return TRUE;
}


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

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


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

    return (DWORD)-1;
}


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

    return (DWORD)-1;
}


extern BOOL OhcdPdd_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 + -