2011-09-15 182 views
0

我刚刚写了一个KMDF USB驱动程序。现在我想连接几台(最多四台)设备到PC。我从哪说起呢?我注意到,当我将第二个设备连接到PC时,它使用与第一个连接设备相同的驱动程序实例。 EvtDeviceAdd(...)运行一次,每个设备,因为我没有几个设备事情变得怪异任何处理......现在我EvtDeviceAdd看起来是这样的:多设备驱动程序? (KMDF/WDF)

NTSTATUS EvtDeviceAdd(IN WDFDRIVER Driver, IN PWDFDEVICE_INIT DeviceInit) { 
    WDF_PNPPOWER_EVENT_CALLBACKS  pnpPowerCallbacks; 
    WDF_OBJECT_ATTRIBUTES    attributes; 
    NTSTATUS       status; 
    WDFDEVICE       device; 
    WDF_DEVICE_PNP_CAPABILITIES   pnpCaps; 
    WDF_IO_QUEUE_CONFIG     ioQueueConfig; 
    PDEVICE_CONTEXT      pDevContext; 
    WDFQUEUE       queue; 
    PWSTR        driverRegistryPath; 

    UNREFERENCED_PARAMETER(Driver); 
    PAGED_CODE(); 

    DbgPrint("New device was added\n"); 

    WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks); 
    pnpPowerCallbacks.EvtDevicePrepareHardware = EvtDevicePrepareHardware; 
    WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks); 

    WdfDeviceInitSetIoType(DeviceInit, WdfDeviceIoBuffered); 

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, DEVICE_CONTEXT); 

    status = WdfDeviceCreate(&DeviceInit, &attributes, &device); 
    if (!NT_SUCCESS(status)) { 
     DbgPrint("WdfDeviceCreate failed with Status code %!STATUS!\n", status); 
     return status; 
    } 

    pDevContext = GetDeviceContext(device); 

    WDF_DEVICE_PNP_CAPABILITIES_INIT(&pnpCaps); 
    pnpCaps.SurpriseRemovalOK = WdfTrue; 

    WdfDeviceSetPnpCapabilities(device, &pnpCaps); 

    WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&ioQueueConfig, WdfIoQueueDispatchParallel); 

    ioQueueConfig.EvtIoRead = EvtIoRead; 
    ioQueueConfig.EvtIoWrite = EvtIoWrite; 
    ioQueueConfig.EvtIoDeviceControl = EvtIoDeviceControl; 
    ioQueueConfig.PowerManaged = WdfTrue; 

    status = WdfIoQueueCreate(device, &ioQueueConfig, WDF_NO_OBJECT_ATTRIBUTES, &queue); 
    if (!NT_SUCCESS(status)) { 
     DbgPrint("WdfIoQueueCreate failed %!STATUS!\n", status); 
     return status; 
    } 
    pDevContext->DeviceIOControlQueue = queue; 

    status = WdfDeviceCreateDeviceInterface(device, (LPGUID) &GUID_DEVINTERFACE_MYDEVICE, NULL); 

    if (!NT_SUCCESS(status)) { 
     DbgPrint("WdfDeviceCreateDeviceInterface failed %!STATUS!\n", status); 
     return status; 
    } 
} 

从哪里开始?有什么好的例子吗?

回答

0

所有连接的设备在内存中只有一个驱动程序实例(它是一个单例)。操作系统对驱动程序的调用伴随着相关的设备环境,从这一点来说,设备不应该干扰其他操作。如果使用非常量全局/静态变量,则问题开始。由于内核空间是共享的,因此这些变量实际上将从所有连接的设备共享和访问。由于这个原因,全局/静态数据不应该是特定于设备的,因为它是共享资源,所以应该保护它们。 WDK中有一些示例演示了多设备驱动程序。