USBCCGP 功能设备驱动FDO创建

回到入口函数DriverEntry,驱动扩展结构体的AddDevice函数指针被置为USBCCGP_AddDevice
这个函数指针是WDM驱动功能设备创建和扩展数据初始的回调函数。

usbccgp.c

NTSTATUS
NTAPI
DriverEntry(
    PDRIVER_OBJECT DriverObject,
    PUNICODE_STRING RegistryPath)
{

...
    DriverObject->DriverExtension->AddDevice = USBCCGP_AddDevice;
    ...

下面我们来分析这个函数的代码。这个函数位于usbccgp.c文件中,代码如下:
usbccgap.c

NTSTATUS
NTAPI
USBCCGP_AddDevice(
    PDRIVER_OBJECT DriverObject,
    PDEVICE_OBJECT PhysicalDeviceObject)
{
    NTSTATUS Status;
    PDEVICE_OBJECT DeviceObject;
    PFDO_DEVICE_EXTENSION FDODeviceExtension;

    /* Lets create the device */
    Status = IoCreateDevice(DriverObject,
                            sizeof(FDO_DEVICE_EXTENSION),
                            NULL,
                            FILE_DEVICE_USB,
                            FILE_AUTOGENERATED_DEVICE_NAME,
                            FALSE,
                            &DeviceObject);
    if (!NT_SUCCESS(Status))
    {
        /* Failed to create device */
        DPRINT1("USBCCGP_AddDevice failed to create device with %x\n", Status);
        return Status;
    }

    /* Get device extension */
    FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;

    /* Init device extension */
    RtlZeroMemory(FDODeviceExtension, sizeof(FDO_DEVICE_EXTENSION));
    FDODeviceExtension->Common.IsFDO = TRUE;
    FDODeviceExtension->DriverObject = DriverObject;
    FDODeviceExtension->PhysicalDeviceObject = PhysicalDeviceObject;
    InitializeListHead(&FDODeviceExtension->ResetPortListHead);
    InitializeListHead(&FDODeviceExtension->CyclePortListHead);
    KeInitializeSpinLock(&FDODeviceExtension->Lock);

    FDODeviceExtension->NextDeviceObject = IoAttachDeviceToDeviceStack(DeviceObject,
                                                                       PhysicalDeviceObject);
    if (!FDODeviceExtension->NextDeviceObject)
    {
        /* Failed to attach */
        DPRINT1("USBCCGP_AddDevice failed to attach device\n");
        IoDeleteDevice(DeviceObject);
        return STATUS_DEVICE_REMOVED;
    }

    /* Set device flags */
    DeviceObject->Flags |= DO_BUFFERED_IO | DO_POWER_PAGABLE;

    /* Device is initialized */
    DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;

    /* Device initialized */
    return Status;
}

可以看到,首先通过IoCreateDevice创建一个设备,设备类型为FILE_DEVICE_USB,在ReactOS中这个代表USB设备类型

设备类型其实不重要,只是设备的数据类型的标识。Windows下定义了多种设备类型,如下:

#define FILE_DEVICE_BEEP                0x00000001
#define FILE_DEVICE_CD_ROM              0x00000002
#define FILE_DEVICE_CD_ROM_FILE_SYSTEM  0x00000003
#define FILE_DEVICE_CONTROLLER          0x00000004
#define FILE_DEVICE_DATALINK            0x00000005
#define FILE_DEVICE_DFS                 0x00000006
#define FILE_DEVICE_DISK                0x00000007
#define FILE_DEVICE_DISK_FILE_SYSTEM    0x00000008
#define FILE_DEVICE_FILE_SYSTEM         0x00000009
#define FILE_DEVICE_INPORT_PORT         0x0000000a
#define FILE_DEVICE_KEYBOARD            0x0000000b
#define FILE_DEVICE_MAILSLOT            0x0000000c
#define FILE_DEVICE_MIDI_IN             0x0000000d
#define FILE_DEVICE_MIDI_OUT            0x0000000e
#define FILE_DEVICE_MOUSE               0x0000000f
#define FILE_DEVICE_MULTI_UNC_PROVIDER  0x00000010
#define FILE_DEVICE_NAMED_PIPE          0x00000011
#define FILE_DEVICE_NETWORK             0x00000012
#define FILE_DEVICE_NETWORK_BROWSER     0x00000013
#define FILE_DEVICE_NETWORK_FILE_SYSTEM 0x00000014
#define FILE_DEVICE_NULL                0x00000015
#define FILE_DEVICE_PARALLEL_PORT       0x00000016
#define FILE_DEVICE_PHYSICAL_NETCARD    0x00000017
#define FILE_DEVICE_PRINTER             0x00000018
#define FILE_DEVICE_SCANNER             0x00000019
#define FILE_DEVICE_SERIAL_MOUSE_PORT   0x0000001a
#define FILE_DEVICE_SERIAL_PORT         0x0000001b
#define FILE_DEVICE_SCREEN              0x0000001c
#define FILE_DEVICE_SOUND               0x0000001d
#define FILE_DEVICE_STREAMS             0x0000001e
#define FILE_DEVICE_TAPE                0x0000001f
#define FILE_DEVICE_TAPE_FILE_SYSTEM    0x00000020
#define FILE_DEVICE_TRANSPORT           0x00000021
#define FILE_DEVICE_UNKNOWN             0x00000022
#define FILE_DEVICE_VIDEO               0x00000023
#define FILE_DEVICE_VIRTUAL_DISK        0x00000024
#define FILE_DEVICE_WAVE_IN             0x00000025
#define FILE_DEVICE_WAVE_OUT            0x00000026
#define FILE_DEVICE_8042_PORT           0x00000027
#define FILE_DEVICE_NETWORK_REDIRECTOR  0x00000028
#define FILE_DEVICE_BATTERY             0x00000029
#define FILE_DEVICE_BUS_EXTENDER        0x0000002a
#define FILE_DEVICE_MODEM               0x0000002b
#define FILE_DEVICE_VDM                 0x0000002c
#define FILE_DEVICE_MASS_STORAGE        0x0000002d
#define FILE_DEVICE_SMB                 0x0000002e
#define FILE_DEVICE_KS                  0x0000002f
#define FILE_DEVICE_CHANGER             0x00000030
#define FILE_DEVICE_SMARTCARD           0x00000031
#define FILE_DEVICE_ACPI                0x00000032
#define FILE_DEVICE_DVD                 0x00000033
#define FILE_DEVICE_FULLSCREEN_VIDEO    0x00000034
#define FILE_DEVICE_DFS_FILE_SYSTEM     0x00000035
#define FILE_DEVICE_DFS_VOLUME          0x00000036
#define FILE_DEVICE_SERENUM             0x00000037
#define FILE_DEVICE_TERMSRV             0x00000038
#define FILE_DEVICE_KSEC                0x00000039
#define FILE_DEVICE_FIPS                0x0000003A
#define FILE_DEVICE_INFINIBAND          0x0000003B
#define FILE_DEVICE_VMBUS               0x0000003E
#define FILE_DEVICE_CRYPT_PROVIDER      0x0000003F
#define FILE_DEVICE_WPD                 0x00000040
#define FILE_DEVICE_BLUETOOTH           0x00000041
#define FILE_DEVICE_MT_COMPOSITE        0x00000042
#define FILE_DEVICE_MT_TRANSPORT        0x00000043
#define FILE_DEVICE_BIOMETRIC           0x00000044
#define FILE_DEVICE_PMI                 0x00000045
#define FILE_DEVICE_EHSTOR              0x00000046
#define FILE_DEVICE_DEVAPI              0x00000047
#define FILE_DEVICE_GPIO                0x00000048
#define FILE_DEVICE_USBEX               0x00000049
#define FILE_DEVICE_CONSOLE             0x00000050
#define FILE_DEVICE_NFP                 0x00000051
#define FILE_DEVICE_SYSENV              0x00000052
#define FILE_DEVICE_VIRTUAL_BLOCK       0x00000053
#define FILE_DEVICE_POINT_OF_SERVICE    0x00000054
#define FILE_DEVICE_STORAGE_REPLICATION 0x00000055
#define FILE_DEVICE_TRUST_ENV           0x00000056
#define FILE_DEVICE_UCM                 0x00000057
#define FILE_DEVICE_UCMTCPCI            0x00000058
#define FILE_DEVICE_PERSISTENT_MEMORY   0x00000059
#define FILE_DEVICE_NVDIMM              0x0000005a
#define FILE_DEVICE_HOLOGRAPHIC         0x0000005b
#define FILE_DEVICE_SDFXHCI             0x0000005c
#define FILE_DEVICE_UCMUCSI             0x0000005d

IoCreateDevice的第三个参数为FDO_DEVICE_EXTENSION的数据大小,这个大小就是设备的私有扩展数据空间大小,可以通过设备的DeviceExtension变量来引用。

FDO_DEVICE_EXTENSION这个数据结构保存了我们关于FDO需要的一些数据变量。

这里进行初始化,如识为FDO,记录总线驱动枚举的PhysicalDeviceObject,数据处理需要的链表结构体初始化,当然还有一个自旋锁。

调用IoAttachDeviceToDeviceStack获取顶层设备,记录在DODeviceExtension->NextDeviceObject.

当驱动无过滤驱动时,NextDeviceObject一般为PhysicalDeviceObject。

最后,标识数据的拷贝通过DO_BUFFERED_IO方式,即内存复制的方式。

内存复制的方式一般用于小数据的传递。

取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

Powered by bytekits.com,汇天下文字,成非凡梦想!!!