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

📄 pcibus.c

📁 WINCE5.0操作系统下的PCI总线驱动
💻 C
📖 第 1 页 / 共 5 页
字号:
                PCIInitDevice(&Info);

                // Request I/O and IRQ resources for device (phase 1 only)
                if (Phase1 && !PCIRequestResources(&Info)) {
                    RETAILMSG(1, (L"PCIbus!PCIEnum: WARNING: Resource request for device %d:%d:%d failed\r\n", bus, device, function));
                }
                
                // Search for match in registry and configure
                PCIReg(&Info);

                // Dump PCI configuration register to debug out (DEBUG only)
                DUMPPCICFG(&Info);

                if (function == 0 && !(pciConfig.HeaderType & PCI_MULTIFUNCTION)) {
                    // Not a multi-function device, continue on to next device
                    break;
                }
            }

            // Error on HalGetBusData, move on to next bus
            if (length == 0) {
                break;
            }
        }

        if (length == 0 && device == 0) {
            // Bus doesn't exist, exit
            break;
        }
    }

    return TRUE;
} 


//
// Find a match for a device in the registry.
//
static BOOL
PCIReg(
    PPCI_DEV_INFO pInfo
    )
{
    WCHAR SavePath[DEVKEY_LEN];

    DEBUGMSG(ZONE_FUNCTION, (L"PCIBUS!PCIReg+(%s, %d, %d, %d)\r\n", pInfo->RegPath, pInfo->Bus, pInfo->Device, pInfo->Function));

    wcsncpy(SavePath, pInfo->RegPath, DEVKEY_LEN - 1);
    SavePath[DEVKEY_LEN - 1] = L'\0';    // make sure longest string is terminated

    // Fail if key name too long
    if (wcslen(SavePath) + wcslen(L"\\") + wcslen(PCIBUS_INSTANCE_KEYNAME) >= DEVKEY_LEN) {
        wcscpy(pInfo->RegPath, SavePath);
        return FALSE;
    }
    
    // Check for exact match under Instance sub-key
    wcscat(pInfo->RegPath, L"\\");
    wcscat(pInfo->RegPath, PCIBUS_INSTANCE_KEYNAME);
    
    if (RegMatch(pInfo) == PCIBUS_MATCH_ALL) {
        // Get registry flags
        RegGetFlags(pInfo);
        
        // There is an exact instance match already, so don't do anything except re-request SYSINTR
        return RegSetConfig(pInfo);
    }

    // Fail if key name too long
    if (wcslen(SavePath) + wcslen(L"\\") + wcslen(PCIBUS_TEMPLATE_KEYNAME) >= DEVKEY_LEN) {
        wcscpy(pInfo->RegPath, SavePath);
        return FALSE;
    }
    
    // No instance exists, so check for potential match under Template key
    wcscpy(pInfo->RegPath, SavePath);
    wcscat(pInfo->RegPath, L"\\");
    wcscat(pInfo->RegPath, PCIBUS_TEMPLATE_KEYNAME);
    
    if (RegMatch(pInfo) == PCIBUS_MATCH_NONE) {
        // There is nothing that matches the device in the registry, return with error
        wcscpy(pInfo->RegPath, SavePath);
        return FALSE;
    }

    // Get registry flags
    RegGetFlags(pInfo);
    
    // A match in the registry exists, copy Template to Instance and generate device location info
    return RegCopy(pInfo);   
}
    

//
// Function to clean stale registry entries
//
static BOOL
PCIRegClean(LPCWSTR EnumKey)
{
    HKEY Key;
    DWORD NumDevKeys;
    DWORD LongSubKey;
    DWORD status;
    DWORD ValLen;
    PWCHAR DelBuf;
    DWORD RegEnum;

    PCI_DEV_INFO Info;
    PCI_COMMON_CONFIG PciCfg;
    DWORD NumValues;

    PCI_SLOT_NUMBER SlotNumber;
    DWORD Length;

    DEBUGMSG(ZONE_FUNCTION, (L"PCIBUS!PCIRegClean(%s)\r\n", EnumKey));

    // Open key to be cleaned
    status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, EnumKey, 0, 0, &Key);
                
    if (status != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCIRegClean RegOpenKeyEx(%s) returned %d.\r\n", EnumKey, status));
        return FALSE;
    }

    // Find out how many sub-keys there are
    status = RegQueryInfoKey(Key, NULL, NULL, NULL, &NumDevKeys, &LongSubKey, NULL, NULL, NULL, NULL, NULL, NULL);                 
    if (status != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCIRegClean RegQueryInfoKey(Class) returned %d.\r\n", status));
        RegCloseKey(Key);
        return FALSE;
    }
    
    DEBUGMSG(ZONE_ENUM, (L"PCIBUS!PCIRegClean %d devices to check\r\n", NumDevKeys));

    // Allocate buffer for keys to delete
    LongSubKey++;
    if (!(DelBuf = (PWCHAR)LocalAlloc(LPTR, (NumDevKeys * LongSubKey) * sizeof(WCHAR)))) {
        DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCIRegClean LocalAlloc failed.\r\n"));
        RegCloseKey(Key);
        return FALSE;
    }
        
    //
    // Read the device list from the registry and check to make sure a device exists for each one.
    //
    for (RegEnum = 0; RegEnum < NumDevKeys; RegEnum++) {
        // Get sub-key according to RegEnum
        ValLen = LongSubKey;
        status = RegEnumKeyEx(Key, RegEnum, &DelBuf[RegEnum * LongSubKey], &ValLen, NULL, NULL, NULL, NULL);
        if (status != ERROR_SUCCESS) {
            DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCIRegClean RegEnumKeyEx(%d) returned %d\r\n", RegEnum, status));
            LocalFree(DelBuf);
            RegCloseKey(Key);
            return FALSE;
        }

        // Get information about device in registry
        wcscpy(Info.RegPath, EnumKey);
        wcscat(Info.RegPath, TEXT("\\"));
        wcscat(Info.RegPath, &DelBuf[RegEnum * LongSubKey]);
        
        NumValues = RegReadValues(Key, &DelBuf[RegEnum * LongSubKey], NULL, NULL,
                        PCIBUS_BUSNUMBER_VALNAME, PCIBUS_BUSNUMBER_VALTYPE, 0, &Info.Bus, sizeof(Info.Bus),
                        PCIBUS_DEVICENUMBER_VALNAME, PCIBUS_DEVICENUMBER_VALTYPE, 0, &Info.Device, sizeof(Info.Device),
                        PCIBUS_FUNCTIONNUMBER_VALNAME, PCIBUS_FUNCTIONNUMBER_VALTYPE, 0, &Info.Function, sizeof(Info.Function),
                        NULL);

        if (NumValues != 3) {
            // No device location, skip
            DelBuf[RegEnum * LongSubKey] = (WCHAR)'\0';
            continue;
        }

        DEBUGMSG(ZONE_ENUM, (L"PCIBUS!PCIRegClean: Examining '%s' (%d/%d/%d)\r\n", Info.RegPath, Info.Bus, Info.Device, Info.Function));
        
        // Read device information
        SlotNumber.u.bits.DeviceNumber = Info.Device;
        SlotNumber.u.bits.FunctionNumber = Info.Function;
        PciCfg.VendorID = 0xFFFF;
        Length = HalGetBusData(PCIConfiguration, Info.Bus, SlotNumber.u.AsULONG, &PciCfg, sizeof(PciCfg) - sizeof(PciCfg.DeviceSpecific));            
        if (Length == 0 || PciCfg.VendorID == 0xFFFF) {
            // Device does not exist, remove registry entry
            DEBUGMSG(ZONE_WARNING, (L"PCIBUS!PCIRegClean: No device exists (%d/%d/%d) for key '%s', removing.\r\n",
                Info.Bus, Info.Device, Info.Function, Info.RegPath));

            continue;
        }

        Info.Cfg = &PciCfg;

        // Match device with registry information
        if (RegMatchOne(&Info, Key, &DelBuf[RegEnum * LongSubKey]) != PCIBUS_MATCH_ALL) {
            // Not an exact match (i.e. another card is in slot), remove registry entry
            DEBUGMSG(ZONE_WARNING, (L"PCIBUS!PCIRegClean: No device match (%d/%d/%d) for key '%s', removing.\r\n",
                Info.Bus, Info.Device, Info.Function, Info.RegPath));

            continue;
        }

        // Device matches, go onto next one
        DelBuf[RegEnum * LongSubKey] = (WCHAR)'\0';
    }

    // Remove registry entries that didn't have matching device
    //
    // Note: this removes everything, including any instance information that was originally set in the
    // registry.  Unfortunately, even if we tried to keep that information around, if the device that
    // was removed was again inserted, it wouldn't be the same instance.  Effectively this instance is
    // lost.
    for (RegEnum = 0; RegEnum < NumDevKeys; RegEnum++) {
        if (DelBuf[RegEnum * LongSubKey] != (WCHAR)'\0')
            RegDeleteKey(Key, &DelBuf[RegEnum * LongSubKey]);
    }

    LocalFree(DelBuf);
    RegCloseKey(Key);
    return TRUE;
}   // Enum


//
// Device/registry matching routine.
//
static DWORD
RegMatchOne(
    PPCI_DEV_INFO pInfo,
    HKEY Key,
    LPCWSTR DevName
    )
{
    HKEY DevKey;
    DWORD status;
    DWORD ValType;
    DWORD ValLen;
    DWORD Match = 0;
    DWORD BestMatch = 0;
    WCHAR RegPath[DEVKEY_LEN];

    DWORD ClassVal;
    DWORD SubClassVal;
    DWORD Val32;

    DWORD ProgIFs[PCI_MAX_REG_LIST], ProgIFNum;
    DWORD VendorIDs[PCI_MAX_REG_LIST], VendorIDNum;
    DWORD DeviceIDs[PCI_MAX_REG_LIST], DeviceIDNum;
    DWORD SubVendorIDs[PCI_MAX_REG_LIST], SubVendorIDNum;
    DWORD SubSystemIDs[PCI_MAX_REG_LIST], SubSystemIDNum;
    DWORD RevisionIDs[PCI_MAX_REG_LIST], RevisionIDNum;

    DWORD ListNum[4], MaxListNum;
    DWORD i;
    

    DEBUGMSG(ZONE_FUNCTION, (L"PCIBUS!RegMatchOne+(%s, %d, %d, %d)\r\n", pInfo->RegPath, pInfo->Bus, pInfo->Device, pInfo->Function));

        // Open sub-key
        status = RegOpenKeyEx(Key, DevName, 0, 0, &DevKey);
        
        if (status != ERROR_SUCCESS) {
            DEBUGMSG(ZONE_ENUM|ZONE_ERROR, (TEXT("PCIBUS!RegMatchOne RegOpenKeyEx(%s) returned %d\r\n"), DevName, status));
            return 0;
        }

        DEBUGMSG(ZONE_ENUM, (TEXT("PCIBUS!RegMatchOne Checking key %s\\%s.\r\n"), pInfo->RegPath, DevName));
                
        //
        // Examine Class value, check against device's base class
        //
        ValLen = sizeof(ClassVal);
        status = RegQueryValueEx(DevKey, PCIBUS_CLASS_VALNAME, NULL, &ValType, (PUCHAR)&ClassVal, &ValLen);
                    
        if (status != ERROR_SUCCESS) {
            // No Class value found
            goto RegMatchOne_Exit;
        }

        if ((UCHAR)ClassVal != pInfo->Cfg->BaseClass) {
            // Class doesn't match
            Match = 0;
            goto RegMatchOne_Exit;
        } else {
            DEBUGMSG(ZONE_ENUM, (TEXT("PCIBUS!RegMatchOne Match for Class (0x%X) found.\r\n"), ClassVal));
                
            Match++;
        }

        // There is at least a partial match, construct path to device key
        wcscpy(RegPath, pInfo->RegPath);
        wcscat(RegPath, TEXT("\\"));
        wcscat(RegPath, DevName);

        //
        // Examine SubClass value, check against device's sub-class
        //
        ValLen = sizeof(SubClassVal);
        status = RegQueryValueEx(DevKey, PCIBUS_SUBCLASS_VALNAME, NULL, &ValType, (PUCHAR)&SubClassVal, &ValLen);
                    
        if (status != ERROR_SUCCESS) {
            // No SubClass value found
            goto RegMatchOne_Exit;
        }

        if ((UCHAR)SubClassVal != pInfo->Cfg->SubClass) {
            // SubClass doesn't match
            Match = 0;
            goto RegMatchOne_Exit;
        } else {
            DEBUGMSG(ZONE_ENUM, (TEXT("PCIBUS!RegMatchOne Match for SubClass (0x%X) found.\r\n"), SubClassVal));
                
            Match++;
        }

        //
        // Get ProgIF value or list and check against device's programing interface
        //
        ProgIFNum = PCI_MAX_REG_LIST;
        if (!RegGetList(DevKey, PCIBUS_PROGIF_VALNAME, &ProgIFNum, ProgIFs) && (ProgIFNum == PCI_MAX_REG_LIST)) {
            DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list.  Ignoring the additional items.\r\n",
                RegPath, PCIBUS_PROGIF_VALNAME, PCI_MAX_REG_LIST));
        }

        if (!ProgIFNum) {
            // No ProgIF value found
            goto RegMatchOne_Exit;
        }
        
        for (i = 0; i < ProgIFNum; i++) {
            if ((UCHAR)ProgIFs[i] == pInfo->Cfg->ProgIf) break;
        }

        if (i == ProgIFNum) {
            // No match found
            Match = 0;
            goto RegMatchOne_Exit;
        } else {
            DEBUGMSG(ZONE_ENUM, (TEXT("PCIBUS!RegMatchOne Match for ProgIF (0x%X) found.\r\n"), ProgIFs[i]));
            
            Match++;
        }

        //
        // Get Vendor ID, Device ID lists and look for match
        //
        VendorIDNum = PCI_MAX_REG_LIST;
        if (!RegGetList(DevKey, PCIBUS_VENDORID_VALNAME, &VendorIDNum, VendorIDs) && (VendorIDNum == PCI_MAX_REG_LIST)) {
            DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list.  Ignoring the additional items.\r\n",
                RegPath, PCIBUS_VENDORID_VALNAME, PCI_MAX_REG_LIST));
        }

        if (!VendorIDNum) {
            // No VendorID value found
            goto RegMatchOne_Exit;
        }

        DeviceIDNum = PCI_MAX_REG_LIST;
        if (!RegGetList(DevKey, PCIBUS_DEVICEID_VALNAME, &DeviceIDNum, DeviceIDs) && (DeviceIDNum == PCI_MAX_REG_LIST)) {
            DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list.  Ignoring the additional items.\r\n",
                RegPath, PCIBUS_DEVICEID_VALNAME, PCI_MAX_REG_LIST));
        }

        if (!DeviceIDNum) {
            // No DeviceID value found
            goto RegMatchOne_Exit;
        }

        if ((pInfo->Cfg->HeaderType & ~PCI_MULTIFUNCTION) == PCI_DEVICE_TYPE) {
            // Get Subsystem Vendor ID and Subsystem ID lists only if h/w is a device
            SubVendorIDNum = PCI_MAX_REG_LIST;
            if (!RegGetList(DevKey, PCIBUS_SUBVENDORID_VALNAME, &SubVendorIDNum, SubVendorIDs) && (VendorIDNum == PCI_MAX_REG_LIST)) {
                DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list.  Ignoring the additional items.\r\n",
                    RegPath, PCIBUS_SUBVENDORID_VALNAME, PCI_MAX_REG_LIST));
            }

            SubSystemIDNum = PCI_MAX_REG_LIST;
            if (!RegGetList(DevKey, PCIBUS_SUBSYSTEMID_VALNAME, &SubSystemIDNum, SubSystemIDs) && (SubSystemIDNum == PCI_MAX_REG_LIST)) {
                DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list.  Ignoring the additional items.\r\n",
                    RegPath, PCIBUS_SUBSYSTEMID_VALNAME, PCI_MAX_REG_LIST));
            }

            if (!SubVendorIDNum && SubSystemIDNum) {
                // If the Subsystem ID exists, but not Subsytem Vendor ID, then ignore SubSystem ID
                DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: If registry value '%s\\%s' exists, then '%s\\%s' must exist.  Ignoring.\r\n",
                    RegPath, PCIBUS_SUBSYSTEMID_VALNAME, RegPath, PCIBUS_SUBVENDORID_VALNAME));

                SubSystemIDNum = 0;
            }
        } else {
            SubVendorIDNum = 0;
            SubSystemIDNum = 0;
        }
        
        // Find smallest list length
        ListNum[0] = VendorIDNum;
        ListNum[1] = DeviceIDNum;
        ListNum[2] = SubVendorIDNum ? SubVendorIDNum : PCI_MAX_REG_LIST;
        ListNum[3] = SubSystemIDNum ? SubSystemIDNum : PCI_MAX_REG_LIST;
        MaxListNum = MinVal(ListNum, 4);

⌨️ 快捷键说明

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