system.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:
system.c
Abstract:
Device dependant part of the USB Open Host Controller Driver (OHCD).
Notes:
--*/
#include <windows.h>
#include <nkintr.h>
#include <ceddk.h>
#include <ohcdddsi.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 OHCI_DRIVER_KEY TEXT("Drivers\\BuiltIn\\OHCI")
#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 _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 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,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;
}
#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("!OHCD: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("!OHCD: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("!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 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,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,SYSINTR_VALUE_NAME,0,REG_DWORD,(PUCHAR)&dwSysIntr,sizeof(DWORD));
if (dwRet != ERROR_SUCCESS) {
DEBUGMSG(ZONE_ERROR, (TEXT("!OHCD: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("!OHCD:SetRegistryConfig RegQueryValueEx(%s) failed %d\r\n"),
IOBASE_VALUE_NAME, dwRet));
goto SetRegistryConfig_exit;
}
fRet = TRUE;
SetRegistryConfig_exit:
RegCloseKey(hKey);
return fRet;
} // SetRegistryConfig
/* ConfigureOHCICard
*
* Configure OHCI 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
ConfigureOHCICard(
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
{
const USHORT cDefaultOhcdPortRange = 0x70;
const ULONG cHTBAMemorySpace = 0;
const ULONG cHTBAIOSpace = 1;
ULONG portRange = cDefaultOhcdPortRange;
ULONG inIoSpace = cHTBAMemorySpace;
ULONG portBase;
PHYSICAL_ADDRESS ioPhysicalBase;
PCI_SLOT_NUMBER slotNumber;
PCI_COMMON_CONFIG pciConfig;
int bus, device, function;
int length;
BOOL bFoundIt;
bFoundIt = FALSE;
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 == 0x10)) { // OHCI
RETAILMSG(1,(TEXT("OHCD: Found PCI USB OHCI controller, VendorId: %04X, DeviceId: %04X\r\n"),
pciConfig.VendorID, pciConfig.DeviceID));
bFoundIt = TRUE;
DEBUGMSG(ZONE_INIT,
(TEXT("OHCD: PCI config: Irq = 0x%X, Port Base = 0x%X\r\n"),
pciConfig.u.type0.InterruptLine, pciConfig.u.type0.BaseAddresses[0]));
#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[0] != (DWORD)*pioPortBase)) {
RETAILMSG(1,(TEXT("!OHCD: Configuration different from registry: IRQ %u, PortBase: 0x%X (reg: %u, 0x%X)\r\n"),
pciConfig.u.type0.InterruptLine,pciConfig.u.type0.BaseAddresses[0],
(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[0];
SetRegistryConfig(*pSysIntr, (DWORD)*pioPortBase);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -