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

📄 devload.c

📁 windows ce 3.00 嵌入式操作系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
// Return the HANDLE from RegisterDevice.
//
HANDLE
StartOneDriver(
    LPTSTR  RegPath,
    LPTSTR  PnpId,
    DWORD   LoadOrder,
    DWORD   ClientInfo,
    CARD_SOCKET_HANDLE hSock
    )
{
    BOOL bUseContext;
    BOOL bFoundIndex;
    HKEY ActiveKey;
    HKEY DevKey;
    DWORD Context;
    DWORD Disposition;
    DWORD Handle;
    DWORD Index;
    DWORD status;
    DWORD ValLen;
    DWORD ValType;
    LPTSTR str;
    TCHAR ActivePath[REG_PATH_LEN];
    TCHAR DevDll[DEVDLL_LEN];
    TCHAR DevName[DEVNAME_LEN];
    TCHAR DevNum[DEVNAME_LEN];
    TCHAR Prefix[DEVPREFIX_LEN];
    fsdev_t * lpdev;

    DEBUGMSG(ZONE_ACTIVE,
        (TEXT("DEVICE!StartOneDriver starting HLM\\%s.\r\n"), RegPath));

    //
    // Get the required (dll and prefix) and optional (index and context) values.
    //
    status = RegCreateKeyEx(
                HKEY_LOCAL_MACHINE,
                RegPath,
                0,
                NULL,
                REG_OPTION_NON_VOLATILE,
                0,
                NULL,
                &DevKey,     // HKEY result
                &Disposition);
    if (status) {
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegCreateKeyEx(%s) returned %d.\r\n"),
            RegPath, status));
        return NULL;
    }

    ValLen = sizeof(DevDll);
    status = RegQueryValueEx(
                DevKey,
                s_DllName_ValName,
                NULL,
                &ValType,
                (PUCHAR)DevDll,
                &ValLen);
    if (status != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegQueryValueEx(%s\\DllName) returned %d\r\n"),
            RegPath, status));
        RegCloseKey(DevKey);
        return NULL; // dll name is required
    }


    ValLen = sizeof(Prefix);
    status = RegQueryValueEx(
                DevKey,
                DEVLOAD_PREFIX_VALNAME,
                NULL,
                &ValType,
                (PUCHAR)Prefix,
                &ValLen);
    if (status != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegQueryValueEx(%s\\Prefix) returned %d\r\n"),
            RegPath, status));
        RegCloseKey(DevKey);
        return NULL; // Prefix is required
    }

    //
    // Read the optional index and context values
    //
    ValLen = sizeof(Index);
    status = RegQueryValueEx(
                DevKey,
                DEVLOAD_INDEX_VALNAME,
                NULL,
                &ValType,
                (PUCHAR)&Index,
                &ValLen);
    if (status != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegQueryValueEx(%s\\Index) returned %d\r\n"),
            RegPath, status));
        Index = (DWORD)-1;     // devload will find an index to use
    }

    bUseContext = TRUE;
    ValLen = sizeof(Context);
    status = RegQueryValueEx(
                DevKey,
                DEVLOAD_CONTEXT_VALNAME,
                NULL,
                &ValType,
                (PUCHAR)&Context,
                &ValLen);
    if (status != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegQueryValueEx(%s\\Context) returned %d\r\n"),
            RegPath, status));
        bUseContext = FALSE;    // context is pointer to active reg path string
    }

    //
    // Format the key's registry path (HLM\Drivers\Active\nnnn)
    //
    _tcscpy(ActivePath, s_ActiveKey);
    _tcscat(ActivePath, TEXT("\\"));
    wsprintf(DevNum, TEXT("%02d"), v_NextDeviceNum);
    _tcscat(ActivePath, DevNum);
    v_NextDeviceNum++;

    DEBUGMSG(ZONE_ACTIVE,
        (TEXT("DEVICE!StartOneDriver Adding HLM\\%s.\r\n"), ActivePath));

    //
    // Create a new key in the active list
    //
    status = RegCreateKeyEx(
                HKEY_LOCAL_MACHINE,
                ActivePath,
                0,
                NULL,
                REG_OPTION_NON_VOLATILE,
                0,
                NULL,
                &ActiveKey,     // HKEY result
                &Disposition);
    if (status) {
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegCreateKeyEx(%s) returned %d.\r\n"),
            ActivePath, status));
        RegCloseKey(DevKey);
        return NULL;
    }

    //
    // Default context is registry path
    //
    if (bUseContext == FALSE) {
        Context = (DWORD)ActivePath;
    }


    //
    // Install pnp id, socket and key name values in the device's active registry key.
    // (handle and device name are added after RegisterDevice())
    //

    //
    // Registry path of the device driver (from HLM\Drivers\BuiltIn or HLM\Drivers\PCMCIA)
    //
    if (RegPath != NULL) {    
        status = RegSetValueEx(
                    ActiveKey,
                    DEVLOAD_DEVKEY_VALNAME,
                    0,
                    DEVLOAD_DEVKEY_VALTYPE,
                    (PBYTE)RegPath,
                    sizeof(TCHAR)*(_tcslen(RegPath) + sizeof(TCHAR)));
        if (status) {
            DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
                (TEXT("DEVICE!StartOneDriver RegSetValueEx(%s) returned %d.\r\n"),
                DEVLOAD_DEVKEY_VALNAME, status));
        }
    }

    //
    // Plug and Play Id string
    //
    if (PnpId != NULL) {
        status = RegSetValueEx(
                    ActiveKey,
                    DEVLOAD_PNPID_VALNAME,
                    0,
                    DEVLOAD_PNPID_VALTYPE,
                    (PBYTE)PnpId,
                    sizeof(TCHAR)*(_tcslen(PnpId) + sizeof(TCHAR)));
        if (status) {
            DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
                (TEXT("DEVICE!StartOneDriver RegSetValueEx(%s) returned %d.\r\n"),
                DEVLOAD_PNPID_VALNAME, status));
        }
    }

    //
    // Socket and function number of this device
    //
    if (hSock.uSocket != 0xff) {
        status = RegSetValueEx(
                    ActiveKey,
                    DEVLOAD_SOCKET_VALNAME,
                    0,
                    DEVLOAD_SOCKET_VALTYPE,
                    (PBYTE)&hSock,
                    sizeof(hSock));
        if (status) {
            DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
                (TEXT("DEVICE!StartOneDriver RegSetValueEx(%s) returned %d.\r\n"),
                DEVLOAD_SOCKET_VALNAME, status));
        }
    }

    //
    // Add ClientInfo from ActivateDevice
    //
    status = RegSetValueEx(
                ActiveKey,
                DEVLOAD_CLIENTINFO_VALNAME,
                0,
                DEVLOAD_CLIENTINFO_VALTYPE,
                (PBYTE)&ClientInfo,
                sizeof(DWORD));
    if (status) {
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegSetValueEx(%s) returned %d.\r\n"),
            DEVLOAD_CLIENTINFO_VALNAME, status));
    }

    if (Index == -1) {
        //
        // Find the first available index for this device prefix.
        //
        bFoundIndex = FALSE;
        Index = 1;  // device index (run it through 1-9 and then 0)
        EnterCriticalSection(&g_devcs);
        while (!bFoundIndex) {
            bUseContext = FALSE; // reuse this flag for this loop.
            for (lpdev = (fsdev_t *)g_DevChain.Flink;
                lpdev != (fsdev_t *)&g_DevChain;
                lpdev = (fsdev_t *)lpdev->list.Flink) {
                if (!memcmp(Prefix, lpdev->type, sizeof(lpdev->type))) {
                    if (lpdev->index == Index) {
                        bUseContext = TRUE;
                        break;
                    }
                }
            }
            if (!bUseContext) {
                //
                // No other devices of this prefix are using this index.
                //
                bFoundIndex = TRUE;
                DEBUGMSG(ZONE_ACTIVE,
                    (TEXT("DEVICE:StartOneDriver using index %d for new %s device\r\n"),
                    Index, Prefix));
                break;
            }
            if (Index == 0) {   // There are no more indexes to try after 0
                break;
            }

            Index++;
            if (Index == 10) {
                Index = 0;       // Try 0 as the last index
            }
        }   // while (trying device indexes)
        LeaveCriticalSection(&g_devcs);
    } else {
        bFoundIndex = TRUE;
    }

    if (bFoundIndex) {
        //
        // Format device name from prefix and index and write it to registry
        //
        _tcscpy(DevName, Prefix);
        str = DevName + _tcslen(DevName);
        str[0] = (TCHAR)Index + (TCHAR)'0';
        str[1] = (TCHAR)':';
        str[2] = (TCHAR)0;
        status = RegSetValueEx(
                    ActiveKey,
                    DEVLOAD_DEVNAME_VALNAME,
                    0,
                    DEVLOAD_DEVNAME_VALTYPE,
                    (PBYTE)DevName,
                    sizeof(TCHAR)*(_tcslen(DevName) + sizeof(TCHAR)));
        if (status) {
            DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
                (TEXT("DEVICE!StartOneDriver RegSetValueEx(%s) returned %d.\r\n"),
                DEVLOAD_DEVNAME_VALNAME, status));
        }
    
#ifdef TARGET_NT
        Handle = (DWORD)RegisterDevice(
                            Prefix,
                            Index,
                            DevDll,
                            Context);
#else
        Handle = (DWORD)RegisterDeviceEx(
                            Prefix,
                            Index,
                            DevDll,
                            Context,
                            LoadOrder
                            );
#endif // TARGET_NT
    } else {
        Handle = 0;
    }

    if (Handle == 0) {
        //
        // RegisterDevice failed
        //
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegisterDevice(%s, %d, %s, 0x%x) failed\r\n"),
            Prefix, Index, DevDll, Context));
        RegCloseKey(DevKey);
        RegCloseKey(ActiveKey);
        RegDeleteKey(HKEY_LOCAL_MACHINE, ActivePath);
        return NULL;
    }

    //
    // Add handle from RegisterDevice()
    //
    status = RegSetValueEx(
                ActiveKey,
                DEVLOAD_HANDLE_VALNAME,
                0,
                DEVLOAD_HANDLE_VALTYPE,
                (PBYTE)&Handle,
                sizeof(Handle));
    if (status) {
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegSetValueEx(%s) returned %d.\r\n"),
            DEVLOAD_HANDLE_VALNAME, status));
    }

    CallTapiDeviceChange(ActiveKey, DevName, FALSE); // bDelete == FALSE



    //
    // Determine whether this device wants a post init ioctl
    //
    ValLen = sizeof(Context);
    status = RegQueryValueEx(
                DevKey,
                DEVLOAD_INITCODE_VALNAME,
                NULL,
                &ValType,
                (PUCHAR)&Context,
                &ValLen);
    if (status != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_ACTIVE|ZONE_ERROR,
            (TEXT("DEVICE!StartOneDriver RegQueryValueEx(%s\\InitCode) returned %d\r\n"),
            RegPath, status));
    } else {
        //
        // Call the new device's IOCTL_DEVICE_INITIALIZED 
        //
        DevicePostInit(DevName, Context, Handle, DevKey);
    }

    RegCloseKey(DevKey);
    RegCloseKey(ActiveKey);

    //
    // Notify only for new PCMCIA devices
    //
#ifndef TARGET_NT
    if (hSock.uSocket != 0xff) {
        StartDeviceNotify(DevName, TRUE);
    }
#endif // TARGET_NT
    return (HANDLE)Handle;
}   // StartOneDriver


//
// Function to RegisterDevice a device driver and add it to the active device list
// in HLM\Drivers\Active and then signal the system that a new device is available.
//
// Return TRUE if the RegisterDevice call succeeded.
//
BOOL
StartDriver(
    LPTSTR  RegPath,
    LPTSTR  PnpId,
    DWORD   LoadOrder,
    CARD_SOCKET_HANDLE hSock

⌨️ 快捷键说明

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