[PATCH 5/5] hidclass.sys: Create separate parent FDO and child PDO objects.

Zebediah Figura z.figura12 at gmail.com
Thu Apr 15 21:04:01 CDT 2021


Signed-off-by: Zebediah Figura <z.figura12 at gmail.com>
---
 dlls/hidclass.sys/device.c           | 237 +++++++------------
 dlls/hidclass.sys/hid.h              |  67 ++++--
 dlls/hidclass.sys/pnp.c              | 334 ++++++++++++++++++++-------
 dlls/ntoskrnl.exe/tests/driver_hid.c |   2 +-
 4 files changed, 376 insertions(+), 264 deletions(-)

diff --git a/dlls/hidclass.sys/device.c b/dlls/hidclass.sys/device.c
index 9c3fbdb66b0..ec34b6b8068 100644
--- a/dlls/hidclass.sys/device.c
+++ b/dlls/hidclass.sys/device.c
@@ -37,37 +37,6 @@
 WINE_DEFAULT_DEBUG_CHANNEL(hid);
 WINE_DECLARE_DEBUG_CHANNEL(hid_report);
 
-NTSTATUS HID_CreateDevice(DEVICE_OBJECT *native_device, HID_MINIDRIVER_REGISTRATION *driver, DEVICE_OBJECT **device)
-{
-    WCHAR dev_name[255];
-    UNICODE_STRING nameW;
-    NTSTATUS status;
-    BASE_DEVICE_EXTENSION *ext;
-
-    swprintf(dev_name, ARRAY_SIZE(dev_name), L"\\Device\\HID#%p&%p", driver->DriverObject, native_device);
-    RtlInitUnicodeString( &nameW, dev_name );
-
-    TRACE("Create base hid device %s\n", debugstr_w(dev_name));
-
-    status = IoCreateDevice(driver->DriverObject, driver->DeviceExtensionSize + sizeof(BASE_DEVICE_EXTENSION), &nameW, 0, 0, FALSE, device);
-    if (status)
-    {
-        FIXME( "failed to create device error %x\n", status );
-        return status;
-    }
-
-    ext = (*device)->DeviceExtension;
-
-    ext->deviceExtension.MiniDeviceExtension = ext + 1;
-    ext->deviceExtension.PhysicalDeviceObject = *device;
-    ext->deviceExtension.NextDeviceObject = native_device;
-    ext->link_name.Buffer = NULL;
-
-    IoAttachDeviceToDeviceStack(*device, native_device);
-
-    return STATUS_SUCCESS;
-}
-
 /* user32 reserves 1 & 2 for winemouse and winekeyboard,
  * keep this in sync with user_private.h */
 #define WINE_MOUSE_HANDLE 1
@@ -83,11 +52,13 @@ NTSTATUS HID_LinkDevice(DEVICE_OBJECT *device)
 {
     WCHAR device_instance_id[MAX_DEVICE_ID_LEN];
     SP_DEVINFO_DATA Data;
-    NTSTATUS status;
     HDEVINFO devinfo;
     BASE_DEVICE_EXTENSION *ext;
+    USAGE usage, page;
 
     ext = device->DeviceExtension;
+    page = ext->u.pdo.preparsed_data->caps.UsagePage;
+    usage = ext->u.pdo.preparsed_data->caps.Usage;
 
     lstrcpyW(device_instance_id, ext->device_id);
     lstrcatW(device_instance_id, L"\\");
@@ -115,26 +86,12 @@ NTSTATUS HID_LinkDevice(DEVICE_OBJECT *device)
     }
     SetupDiDestroyDeviceInfoList(devinfo);
 
-    status = IoRegisterDeviceInterface(device, &GUID_DEVINTERFACE_HID, NULL, &ext->link_name);
-    if (status != STATUS_SUCCESS)
-    {
-        FIXME( "failed to register device interface %x\n", status );
-        return status;
-    }
-
-    /* FIXME: This should probably be done in mouhid.sys. */
-    if (ext->preparseData->caps.UsagePage == HID_USAGE_PAGE_GENERIC
-            && ext->preparseData->caps.Usage == HID_USAGE_GENERIC_MOUSE)
-    {
-        if (!IoRegisterDeviceInterface(device, &GUID_DEVINTERFACE_MOUSE, NULL, &ext->mouse_link_name))
-            ext->is_mouse = TRUE;
-        ext->rawinput_handle = WINE_MOUSE_HANDLE;
-    }
-    else if (ext->preparseData->caps.UsagePage == HID_USAGE_PAGE_GENERIC
-            && ext->preparseData->caps.Usage == HID_USAGE_GENERIC_KEYBOARD)
-        ext->rawinput_handle = WINE_KEYBOARD_HANDLE;
+    if (page == HID_USAGE_PAGE_GENERIC && usage == HID_USAGE_GENERIC_MOUSE)
+        ext->u.pdo.rawinput_handle = WINE_MOUSE_HANDLE;
+    else if (page == HID_USAGE_PAGE_GENERIC && usage == HID_USAGE_GENERIC_KEYBOARD)
+        ext->u.pdo.rawinput_handle = WINE_KEYBOARD_HANDLE;
     else
-        ext->rawinput_handle = alloc_rawinput_handle();
+        ext->u.pdo.rawinput_handle = alloc_rawinput_handle();
 
     return STATUS_SUCCESS;
 
@@ -143,15 +100,15 @@ error:
     return STATUS_UNSUCCESSFUL;
 }
 
-static IRP *pop_irp_from_queue(BASE_DEVICE_EXTENSION *ext)
+IRP *pop_irp_from_queue(BASE_DEVICE_EXTENSION *ext)
 {
     LIST_ENTRY *entry;
     KIRQL old_irql;
     IRP *irp = NULL;
 
-    KeAcquireSpinLock(&ext->irp_queue_lock, &old_irql);
+    KeAcquireSpinLock(&ext->u.pdo.irp_queue_lock, &old_irql);
 
-    while (!irp && (entry = RemoveHeadList(&ext->irp_queue)) != &ext->irp_queue)
+    while (!irp && (entry = RemoveHeadList(&ext->u.pdo.irp_queue)) != &ext->u.pdo.irp_queue)
     {
         irp = CONTAINING_RECORD(entry, IRP, Tail.Overlay.s.ListEntry);
         if (!IoSetCancelRoutine(irp, NULL))
@@ -162,7 +119,7 @@ static IRP *pop_irp_from_queue(BASE_DEVICE_EXTENSION *ext)
         }
     }
 
-    KeReleaseSpinLock(&ext->irp_queue_lock, old_irql);
+    KeReleaseSpinLock(&ext->u.pdo.irp_queue_lock, old_irql);
     return irp;
 }
 
@@ -177,47 +134,17 @@ static void WINAPI read_cancel_routine(DEVICE_OBJECT *device, IRP *irp)
 
     IoReleaseCancelSpinLock(irp->CancelIrql);
 
-    KeAcquireSpinLock(&ext->irp_queue_lock, &old_irql);
+    KeAcquireSpinLock(&ext->u.pdo.irp_queue_lock, &old_irql);
 
     RemoveEntryList(&irp->Tail.Overlay.s.ListEntry);
 
-    KeReleaseSpinLock(&ext->irp_queue_lock, old_irql);
+    KeReleaseSpinLock(&ext->u.pdo.irp_queue_lock, old_irql);
 
     irp->IoStatus.u.Status = STATUS_CANCELLED;
     irp->IoStatus.Information = 0;
     IoCompleteRequest(irp, IO_NO_INCREMENT);
 }
 
-void HID_DeleteDevice(DEVICE_OBJECT *device)
-{
-    BASE_DEVICE_EXTENSION *ext;
-    IRP *irp;
-
-    ext = device->DeviceExtension;
-
-    if (ext->thread)
-    {
-        SetEvent(ext->halt_event);
-        WaitForSingleObject(ext->thread, INFINITE);
-    }
-    CloseHandle(ext->halt_event);
-
-    HeapFree(GetProcessHeap(), 0, ext->preparseData);
-    if (ext->ring_buffer)
-        RingBuffer_Destroy(ext->ring_buffer);
-
-    while((irp = pop_irp_from_queue(ext)))
-    {
-        irp->IoStatus.u.Status = STATUS_DEVICE_REMOVED;
-        IoCompleteRequest(irp, IO_NO_INCREMENT);
-    }
-
-    RtlFreeUnicodeString(&ext->link_name);
-
-    IoDetachDevice(ext->deviceExtension.NextDeviceObject);
-    IoDeleteDevice(device);
-}
-
 static NTSTATUS copy_packet_into_buffer(HID_XFER_PACKET *packet, BYTE* buffer, ULONG buffer_length, ULONG *out_length)
 {
     BOOL zero_id = (packet->reportId == 0);
@@ -248,7 +175,7 @@ static void HID_Device_processQueue(DEVICE_OBJECT *device)
 {
     IRP *irp;
     BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
-    UINT buffer_size = RingBuffer_GetBufferSize(ext->ring_buffer);
+    UINT buffer_size = RingBuffer_GetBufferSize(ext->u.pdo.ring_buffer);
     HID_XFER_PACKET *packet;
 
     packet = HeapAlloc(GetProcessHeap(), 0, buffer_size);
@@ -258,7 +185,7 @@ static void HID_Device_processQueue(DEVICE_OBJECT *device)
         int ptr;
         ptr = PtrToUlong( irp->Tail.Overlay.OriginalFileObject->FsContext );
 
-        RingBuffer_Read(ext->ring_buffer, ptr, packet, &buffer_size);
+        RingBuffer_Read(ext->u.pdo.ring_buffer, ptr, packet, &buffer_size);
         if (buffer_size)
         {
             NTSTATUS rc;
@@ -290,11 +217,12 @@ static DWORD CALLBACK hid_device_thread(void *args)
     DWORD rc;
 
     BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
+    USHORT report_size = ext->u.pdo.preparsed_data->caps.InputReportByteLength;
 
-    packet = HeapAlloc(GetProcessHeap(), 0, sizeof(*packet) + ext->preparseData->caps.InputReportByteLength);
+    packet = HeapAlloc(GetProcessHeap(), 0, sizeof(*packet) + report_size);
     packet->reportBuffer = (BYTE *)packet + sizeof(*packet);
 
-    if (ext->information.Polled)
+    if (ext->u.pdo.information.Polled)
     {
         while(1)
         {
@@ -302,22 +230,23 @@ static DWORD CALLBACK hid_device_thread(void *args)
 
             KeInitializeEvent(&event, NotificationEvent, FALSE);
 
-            packet->reportBufferLen = ext->preparseData->caps.InputReportByteLength;
+            packet->reportBufferLen = report_size;
             packet->reportId = 0;
 
-            irp = IoBuildDeviceIoControlRequest(IOCTL_HID_GET_INPUT_REPORT, device,
+            irp = IoBuildDeviceIoControlRequest(IOCTL_HID_GET_INPUT_REPORT, ext->u.pdo.parent_fdo,
                     NULL, 0, packet, sizeof(*packet), TRUE, &event, &irp_status);
 
-            if (IoCallDriver(device, irp) == STATUS_PENDING)
+            if (IoCallDriver(ext->u.pdo.parent_fdo, irp) == STATUS_PENDING)
                 KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
 
             if (irp_status.u.Status == STATUS_SUCCESS)
             {
-                RingBuffer_Write(ext->ring_buffer, packet);
+                RingBuffer_Write(ext->u.pdo.ring_buffer, packet);
                 HID_Device_processQueue(device);
             }
 
-            rc = WaitForSingleObject(ext->halt_event, ext->poll_interval ? ext->poll_interval : DEFAULT_POLL_INTERVAL);
+            rc = WaitForSingleObject(ext->u.pdo.halt_event,
+                    ext->u.pdo.poll_interval ? ext->u.pdo.poll_interval : DEFAULT_POLL_INTERVAL);
 
             if (rc == WAIT_OBJECT_0)
                 break;
@@ -335,24 +264,24 @@ static DWORD CALLBACK hid_device_thread(void *args)
 
             KeInitializeEvent(&event, NotificationEvent, FALSE);
 
-            irp = IoBuildDeviceIoControlRequest(IOCTL_HID_READ_REPORT, device, NULL, 0, packet->reportBuffer,
-                    ext->preparseData->caps.InputReportByteLength, TRUE, &event, &irp_status);
+            irp = IoBuildDeviceIoControlRequest(IOCTL_HID_READ_REPORT, ext->u.pdo.parent_fdo,
+                    NULL, 0, packet->reportBuffer, report_size, TRUE, &event, &irp_status);
 
-            if (IoCallDriver(device, irp) == STATUS_PENDING)
+            if (IoCallDriver(ext->u.pdo.parent_fdo, irp) == STATUS_PENDING)
                 KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
 
-            rc = WaitForSingleObject(ext->halt_event, 0);
+            rc = WaitForSingleObject(ext->u.pdo.halt_event, 0);
             if (rc == WAIT_OBJECT_0)
                 exit_now = TRUE;
 
             if (!exit_now && irp_status.u.Status == STATUS_SUCCESS)
             {
                 packet->reportBufferLen = irp_status.Information;
-                if (ext->preparseData->reports[0].reportID)
+                if (ext->u.pdo.preparsed_data->reports[0].reportID)
                     packet->reportId = packet->reportBuffer[0];
                 else
                     packet->reportId = 0;
-                RingBuffer_Write(ext->ring_buffer, packet);
+                RingBuffer_Write(ext->u.pdo.ring_buffer, packet);
                 HID_Device_processQueue(device);
             }
 
@@ -368,11 +297,11 @@ static DWORD CALLBACK hid_device_thread(void *args)
 void HID_StartDeviceThread(DEVICE_OBJECT *device)
 {
     BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
-    ext->halt_event = CreateEventA(NULL, TRUE, FALSE, NULL);
-    ext->thread = CreateThread(NULL, 0, hid_device_thread, device, 0, NULL);
+    ext->u.pdo.halt_event = CreateEventA(NULL, TRUE, FALSE, NULL);
+    ext->u.pdo.thread = CreateThread(NULL, 0, hid_device_thread, device, 0, NULL);
 }
 
-static NTSTATUS handle_IOCTL_HID_GET_COLLECTION_INFORMATION(IRP *irp, BASE_DEVICE_EXTENSION *base)
+static NTSTATUS handle_IOCTL_HID_GET_COLLECTION_INFORMATION(IRP *irp, BASE_DEVICE_EXTENSION *ext)
 {
     IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
     if (irpsp->Parameters.DeviceIoControl.OutputBufferLength <  sizeof(HID_COLLECTION_INFORMATION))
@@ -382,32 +311,33 @@ static NTSTATUS handle_IOCTL_HID_GET_COLLECTION_INFORMATION(IRP *irp, BASE_DEVIC
     }
     else
     {
-        memcpy(irp->AssociatedIrp.SystemBuffer, &base->information, sizeof(HID_COLLECTION_INFORMATION));
+        memcpy(irp->AssociatedIrp.SystemBuffer, &ext->u.pdo.information, sizeof(HID_COLLECTION_INFORMATION));
         irp->IoStatus.Information = sizeof(HID_COLLECTION_INFORMATION);
         irp->IoStatus.u.Status = STATUS_SUCCESS;
     }
     return STATUS_SUCCESS;
 }
 
-static NTSTATUS handle_IOCTL_HID_GET_COLLECTION_DESCRIPTOR(IRP *irp, BASE_DEVICE_EXTENSION *base)
+static NTSTATUS handle_IOCTL_HID_GET_COLLECTION_DESCRIPTOR(IRP *irp, BASE_DEVICE_EXTENSION *ext)
 {
     IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
+    const WINE_HIDP_PREPARSED_DATA *data = ext->u.pdo.preparsed_data;
 
-    if (irpsp->Parameters.DeviceIoControl.OutputBufferLength <  base->preparseData->dwSize)
+    if (irpsp->Parameters.DeviceIoControl.OutputBufferLength < data->dwSize)
     {
         irp->IoStatus.u.Status = STATUS_INVALID_BUFFER_SIZE;
         irp->IoStatus.Information = 0;
     }
     else
     {
-        memcpy(irp->UserBuffer, base->preparseData, base->preparseData->dwSize);
-        irp->IoStatus.Information = base->preparseData->dwSize;
+        memcpy(irp->UserBuffer, data, data->dwSize);
+        irp->IoStatus.Information = data->dwSize;
         irp->IoStatus.u.Status = STATUS_SUCCESS;
     }
     return STATUS_SUCCESS;
 }
 
-static NTSTATUS handle_minidriver_string(DEVICE_OBJECT *device, IRP *irp, SHORT index)
+static NTSTATUS handle_minidriver_string(BASE_DEVICE_EXTENSION *ext, IRP *irp, SHORT index)
 {
     IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
     WCHAR buffer[127];
@@ -415,7 +345,8 @@ static NTSTATUS handle_minidriver_string(DEVICE_OBJECT *device, IRP *irp, SHORT
     ULONG InputBuffer;
 
     InputBuffer = MAKELONG(index, 0);
-    status = call_minidriver(IOCTL_HID_GET_STRING, device, ULongToPtr(InputBuffer), sizeof(InputBuffer), buffer, sizeof(buffer));
+    status = call_minidriver(IOCTL_HID_GET_STRING, ext->u.pdo.parent_fdo,
+            ULongToPtr(InputBuffer), sizeof(InputBuffer), buffer, sizeof(buffer));
 
     if (status == STATUS_SUCCESS)
     {
@@ -430,7 +361,7 @@ static NTSTATUS handle_minidriver_string(DEVICE_OBJECT *device, IRP *irp, SHORT
     return STATUS_SUCCESS;
 }
 
-static NTSTATUS HID_get_feature(DEVICE_OBJECT *device, IRP *irp)
+static NTSTATUS HID_get_feature(BASE_DEVICE_EXTENSION *ext, IRP *irp)
 {
     IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
     HID_XFER_PACKET *packet;
@@ -441,7 +372,7 @@ static NTSTATUS HID_get_feature(DEVICE_OBJECT *device, IRP *irp)
     irp->IoStatus.Information = 0;
 
     out_buffer = MmGetSystemAddressForMdlSafe(irp->MdlAddress, NormalPagePriority);
-    TRACE_(hid_report)("Device %p Buffer length %i Buffer %p\n", device, irpsp->Parameters.DeviceIoControl.OutputBufferLength, out_buffer);
+    TRACE_(hid_report)("Device %p Buffer length %i Buffer %p\n", ext, irpsp->Parameters.DeviceIoControl.OutputBufferLength, out_buffer);
 
     len = sizeof(*packet) + irpsp->Parameters.DeviceIoControl.OutputBufferLength;
     packet = HeapAlloc(GetProcessHeap(), 0, len);
@@ -451,7 +382,7 @@ static NTSTATUS HID_get_feature(DEVICE_OBJECT *device, IRP *irp)
 
     TRACE_(hid_report)("(id %i, len %i buffer %p)\n", packet->reportId, packet->reportBufferLen, packet->reportBuffer);
 
-    rc = call_minidriver(IOCTL_HID_GET_FEATURE, device, NULL, 0, packet, sizeof(*packet));
+    rc = call_minidriver(IOCTL_HID_GET_FEATURE, ext->u.pdo.parent_fdo, NULL, 0, packet, sizeof(*packet));
 
     irp->IoStatus.u.Status = rc;
     if (irp->IoStatus.u.Status == STATUS_SUCCESS)
@@ -473,6 +404,7 @@ static NTSTATUS HID_set_to_device(DEVICE_OBJECT *device, IRP *irp)
 {
     IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation(irp);
     BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
+    const WINE_HIDP_PREPARSED_DATA *data = ext->u.pdo.preparsed_data;
     HID_XFER_PACKET packet;
     ULONG max_len;
     NTSTATUS rc;
@@ -484,18 +416,19 @@ static NTSTATUS HID_set_to_device(DEVICE_OBJECT *device, IRP *irp)
         packet.reportBuffer = &((BYTE*)irp->AssociatedIrp.SystemBuffer)[1];
         packet.reportBufferLen = irpsp->Parameters.DeviceIoControl.InputBufferLength - 1;
         if (irpsp->Parameters.DeviceIoControl.IoControlCode == IOCTL_HID_SET_FEATURE)
-            max_len = ext->preparseData->caps.FeatureReportByteLength;
+            max_len = data->caps.FeatureReportByteLength;
         else
-            max_len = ext->preparseData->caps.OutputReportByteLength;
+            max_len = data->caps.OutputReportByteLength;
     }
     else
     {
         packet.reportBuffer = irp->AssociatedIrp.SystemBuffer;
         packet.reportBufferLen = irpsp->Parameters.DeviceIoControl.InputBufferLength;
         if (irpsp->Parameters.DeviceIoControl.IoControlCode == IOCTL_HID_SET_FEATURE)
-            max_len = (ext->preparseData->reports[ext->preparseData->reportIdx[HidP_Feature][packet.reportId]].bitSize + 7) / 8;
+            max_len = data->reports[data->reportIdx[HidP_Feature][packet.reportId]].bitSize;
         else
-            max_len = (ext->preparseData->reports[ext->preparseData->reportIdx[HidP_Output][packet.reportId]].bitSize + 7) / 8;
+            max_len = data->reports[data->reportIdx[HidP_Output][packet.reportId]].bitSize;
+        max_len = (max_len + 7) / 8;
     }
     if (packet.reportBufferLen > max_len)
         packet.reportBufferLen = max_len;
@@ -503,7 +436,7 @@ static NTSTATUS HID_set_to_device(DEVICE_OBJECT *device, IRP *irp)
     TRACE_(hid_report)("(id %i, len %i buffer %p)\n", packet.reportId, packet.reportBufferLen, packet.reportBuffer);
 
     rc = call_minidriver(irpsp->Parameters.DeviceIoControl.IoControlCode,
-        device, NULL, 0, &packet, sizeof(packet));
+            ext->u.pdo.parent_fdo, NULL, 0, &packet, sizeof(packet));
 
     irp->IoStatus.u.Status = rc;
     if (irp->IoStatus.u.Status == STATUS_SUCCESS)
@@ -516,11 +449,11 @@ static NTSTATUS HID_set_to_device(DEVICE_OBJECT *device, IRP *irp)
     return rc;
 }
 
-NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp)
+NTSTATUS WINAPI pdo_ioctl(DEVICE_OBJECT *device, IRP *irp)
 {
     NTSTATUS rc = STATUS_SUCCESS;
     IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
-    BASE_DEVICE_EXTENSION *extension = device->DeviceExtension;
+    BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
 
     irp->IoStatus.Information = 0;
 
@@ -536,7 +469,7 @@ NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp)
                 irp->IoStatus.Information = 0;
                 break;
             }
-            *((ULONG*)irp->AssociatedIrp.SystemBuffer) = extension->poll_interval;
+            *(ULONG *)irp->AssociatedIrp.SystemBuffer = ext->u.pdo.poll_interval;
             irp->IoStatus.Information = sizeof(ULONG);
             irp->IoStatus.u.Status = STATUS_SUCCESS;
             break;
@@ -552,7 +485,7 @@ NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp)
             poll_interval = *(ULONG *)irp->AssociatedIrp.SystemBuffer;
             if (poll_interval <= MAX_POLL_INTERVAL_MSEC)
             {
-                extension->poll_interval = poll_interval;
+                ext->u.pdo.poll_interval = poll_interval;
                 irp->IoStatus.u.Status = STATUS_SUCCESS;
             }
             else
@@ -561,27 +494,27 @@ NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp)
         }
         case IOCTL_HID_GET_PRODUCT_STRING:
         {
-            rc = handle_minidriver_string(device, irp, HID_STRING_ID_IPRODUCT);
+            rc = handle_minidriver_string(ext, irp, HID_STRING_ID_IPRODUCT);
             break;
         }
         case IOCTL_HID_GET_SERIALNUMBER_STRING:
         {
-            rc = handle_minidriver_string(device, irp, HID_STRING_ID_ISERIALNUMBER);
+            rc = handle_minidriver_string(ext, irp, HID_STRING_ID_ISERIALNUMBER);
             break;
         }
         case IOCTL_HID_GET_MANUFACTURER_STRING:
         {
-            rc = handle_minidriver_string(device, irp, HID_STRING_ID_IMANUFACTURER);
+            rc = handle_minidriver_string(ext, irp, HID_STRING_ID_IMANUFACTURER);
             break;
         }
         case IOCTL_HID_GET_COLLECTION_INFORMATION:
         {
-            rc = handle_IOCTL_HID_GET_COLLECTION_INFORMATION(irp, extension);
+            rc = handle_IOCTL_HID_GET_COLLECTION_INFORMATION(irp, ext);
             break;
         }
         case IOCTL_HID_GET_COLLECTION_DESCRIPTOR:
         {
-            rc = handle_IOCTL_HID_GET_COLLECTION_DESCRIPTOR(irp, extension);
+            rc = handle_IOCTL_HID_GET_COLLECTION_DESCRIPTOR(irp, ext);
             break;
         }
         case IOCTL_HID_GET_INPUT_REPORT:
@@ -593,14 +526,14 @@ NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp)
 
             packet = HeapAlloc(GetProcessHeap(), 0, packet_size);
 
-            if (extension->preparseData->reports[0].reportID)
+            if (ext->u.pdo.preparsed_data->reports[0].reportID)
                 packet->reportId = buffer[0];
             else
                 packet->reportId = 0;
             packet->reportBuffer = (BYTE *)packet + sizeof(*packet);
             packet->reportBufferLen = irpsp->Parameters.DeviceIoControl.OutputBufferLength - 1;
 
-            rc = call_minidriver(IOCTL_HID_GET_INPUT_REPORT, device, NULL, 0, packet, sizeof(*packet));
+            rc = call_minidriver(IOCTL_HID_GET_INPUT_REPORT, ext->u.pdo.parent_fdo, NULL, 0, packet, sizeof(*packet));
             if (rc == STATUS_SUCCESS)
             {
                 rc = copy_packet_into_buffer(packet, buffer, irpsp->Parameters.DeviceIoControl.OutputBufferLength, &out_length);
@@ -622,7 +555,7 @@ NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp)
             }
             else
             {
-                rc = RingBuffer_SetSize(extension->ring_buffer, *(ULONG*)irp->AssociatedIrp.SystemBuffer);
+                rc = RingBuffer_SetSize(ext->u.pdo.ring_buffer, *(ULONG *)irp->AssociatedIrp.SystemBuffer);
                 irp->IoStatus.u.Status = rc;
             }
             break;
@@ -635,13 +568,13 @@ NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp)
             }
             else
             {
-                *(ULONG*)irp->AssociatedIrp.SystemBuffer = RingBuffer_GetSize(extension->ring_buffer);
+                *(ULONG *)irp->AssociatedIrp.SystemBuffer = RingBuffer_GetSize(ext->u.pdo.ring_buffer);
                 rc = irp->IoStatus.u.Status = STATUS_SUCCESS;
             }
             break;
         }
         case IOCTL_HID_GET_FEATURE:
-            rc = HID_get_feature(device, irp);
+            rc = HID_get_feature(ext, irp);
             break;
         case IOCTL_HID_SET_FEATURE:
         case IOCTL_HID_SET_OUTPUT_REPORT:
@@ -664,11 +597,11 @@ NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp)
     return rc;
 }
 
-NTSTATUS WINAPI HID_Device_read(DEVICE_OBJECT *device, IRP *irp)
+NTSTATUS WINAPI pdo_read(DEVICE_OBJECT *device, IRP *irp)
 {
     HID_XFER_PACKET *packet;
     BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
-    UINT buffer_size = RingBuffer_GetBufferSize(ext->ring_buffer);
+    UINT buffer_size = RingBuffer_GetBufferSize(ext->u.pdo.ring_buffer);
     NTSTATUS rc = STATUS_SUCCESS;
     IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation(irp);
     int ptr = -1;
@@ -677,7 +610,7 @@ NTSTATUS WINAPI HID_Device_read(DEVICE_OBJECT *device, IRP *irp)
     ptr = PtrToUlong( irp->Tail.Overlay.OriginalFileObject->FsContext );
 
     irp->IoStatus.Information = 0;
-    RingBuffer_ReadNew(ext->ring_buffer, ptr, packet, &buffer_size);
+    RingBuffer_ReadNew(ext->u.pdo.ring_buffer, ptr, packet, &buffer_size);
 
     if (buffer_size)
     {
@@ -694,27 +627,26 @@ NTSTATUS WINAPI HID_Device_read(DEVICE_OBJECT *device, IRP *irp)
     }
     else
     {
-        BASE_DEVICE_EXTENSION *extension = device->DeviceExtension;
-        if (extension->poll_interval)
+        if (ext->u.pdo.poll_interval)
         {
             KIRQL old_irql;
             TRACE_(hid_report)("Queue irp\n");
 
-            KeAcquireSpinLock(&ext->irp_queue_lock, &old_irql);
+            KeAcquireSpinLock(&ext->u.pdo.irp_queue_lock, &old_irql);
 
             IoSetCancelRoutine(irp, read_cancel_routine);
             if (irp->Cancel && !IoSetCancelRoutine(irp, NULL))
             {
                 /* IRP was canceled before we set cancel routine */
                 InitializeListHead(&irp->Tail.Overlay.s.ListEntry);
-                KeReleaseSpinLock(&ext->irp_queue_lock, old_irql);
+                KeReleaseSpinLock(&ext->u.pdo.irp_queue_lock, old_irql);
                 return STATUS_CANCELLED;
             }
 
-            InsertTailList(&ext->irp_queue, &irp->Tail.Overlay.s.ListEntry);
+            InsertTailList(&ext->u.pdo.irp_queue, &irp->Tail.Overlay.s.ListEntry);
             IoMarkIrpPending(irp);
 
-            KeReleaseSpinLock(&ext->irp_queue_lock, old_irql);
+            KeReleaseSpinLock(&ext->u.pdo.irp_queue_lock, old_irql);
             rc = STATUS_PENDING;
         }
         else
@@ -724,7 +656,7 @@ NTSTATUS WINAPI HID_Device_read(DEVICE_OBJECT *device, IRP *irp)
             packet.reportId = ((BYTE*)irp->AssociatedIrp.SystemBuffer)[0];
             packet.reportBuffer = &((BYTE*)irp->AssociatedIrp.SystemBuffer)[1];
             packet.reportBufferLen = irpsp->Parameters.Read.Length - 1;
-            rc = call_minidriver(IOCTL_HID_GET_INPUT_REPORT, device, NULL, 0, &packet, sizeof(packet));
+            rc = call_minidriver(IOCTL_HID_GET_INPUT_REPORT, ext->u.pdo.parent_fdo, NULL, 0, &packet, sizeof(packet));
 
             if (rc == STATUS_SUCCESS)
             {
@@ -740,10 +672,11 @@ NTSTATUS WINAPI HID_Device_read(DEVICE_OBJECT *device, IRP *irp)
     return rc;
 }
 
-NTSTATUS WINAPI HID_Device_write(DEVICE_OBJECT *device, IRP *irp)
+NTSTATUS WINAPI pdo_write(DEVICE_OBJECT *device, IRP *irp)
 {
     IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
     BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
+    const WINE_HIDP_PREPARSED_DATA *data = ext->u.pdo.preparsed_data;
     HID_XFER_PACKET packet;
     ULONG max_len;
     NTSTATUS rc;
@@ -756,20 +689,20 @@ NTSTATUS WINAPI HID_Device_write(DEVICE_OBJECT *device, IRP *irp)
     {
         packet.reportBuffer = &((BYTE*)irp->AssociatedIrp.SystemBuffer)[1];
         packet.reportBufferLen = irpsp->Parameters.Write.Length - 1;
-        max_len = ext->preparseData->caps.OutputReportByteLength;
+        max_len = data->caps.OutputReportByteLength;
     }
     else
     {
         packet.reportBuffer = irp->AssociatedIrp.SystemBuffer;
         packet.reportBufferLen = irpsp->Parameters.Write.Length;
-        max_len = (ext->preparseData->reports[ext->preparseData->reportIdx[HidP_Output][packet.reportId]].bitSize + 7) / 8;
+        max_len = (data->reports[data->reportIdx[HidP_Output][packet.reportId]].bitSize + 7) / 8;
     }
     if (packet.reportBufferLen > max_len)
         packet.reportBufferLen = max_len;
 
     TRACE_(hid_report)("(id %i, len %i buffer %p)\n", packet.reportId, packet.reportBufferLen, packet.reportBuffer);
 
-    rc = call_minidriver(IOCTL_HID_WRITE_REPORT, device, NULL, 0, &packet, sizeof(packet));
+    rc = call_minidriver(IOCTL_HID_WRITE_REPORT, ext->u.pdo.parent_fdo, NULL, 0, &packet, sizeof(packet));
 
     irp->IoStatus.u.Status = rc;
     if (irp->IoStatus.u.Status == STATUS_SUCCESS)
@@ -783,23 +716,23 @@ NTSTATUS WINAPI HID_Device_write(DEVICE_OBJECT *device, IRP *irp)
     return rc;
 }
 
-NTSTATUS WINAPI HID_Device_create(DEVICE_OBJECT *device, IRP *irp)
+NTSTATUS WINAPI pdo_create(DEVICE_OBJECT *device, IRP *irp)
 {
     BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
 
     TRACE("Open handle on device %p\n", device);
-    irp->Tail.Overlay.OriginalFileObject->FsContext = UlongToPtr(RingBuffer_AddPointer(ext->ring_buffer));
+    irp->Tail.Overlay.OriginalFileObject->FsContext = UlongToPtr(RingBuffer_AddPointer(ext->u.pdo.ring_buffer));
     irp->IoStatus.u.Status = STATUS_SUCCESS;
     IoCompleteRequest( irp, IO_NO_INCREMENT );
     return STATUS_SUCCESS;
 }
 
-NTSTATUS WINAPI HID_Device_close(DEVICE_OBJECT *device, IRP *irp)
+NTSTATUS WINAPI pdo_close(DEVICE_OBJECT *device, IRP *irp)
 {
     BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
     int ptr = PtrToUlong(irp->Tail.Overlay.OriginalFileObject->FsContext);
     TRACE("Close handle on device %p\n", device);
-    RingBuffer_RemovePointer(ext->ring_buffer, ptr);
+    RingBuffer_RemovePointer(ext->u.pdo.ring_buffer, ptr);
     irp->IoStatus.u.Status = STATUS_SUCCESS;
     IoCompleteRequest( irp, IO_NO_INCREMENT );
     return STATUS_SUCCESS;
diff --git a/dlls/hidclass.sys/hid.h b/dlls/hidclass.sys/hid.h
index ccbcd8ffb0b..2a5636121be 100644
--- a/dlls/hidclass.sys/hid.h
+++ b/dlls/hidclass.sys/hid.h
@@ -37,28 +37,47 @@
 /* Ring buffer functions */
 struct ReportRingBuffer;
 
-typedef struct _BASE_DEVICE_EXTENSION {
-    HID_DEVICE_EXTENSION deviceExtension;
+typedef struct _BASE_DEVICE_EXTENSION
+{
+    union
+    {
+        struct
+        {
+            /* this must be the first member */
+            HID_DEVICE_EXTENSION hid_ext;
 
-    HID_COLLECTION_INFORMATION information;
-    WINE_HIDP_PREPARSED_DATA *preparseData;
+            DEVICE_OBJECT *child_pdo;
+        } fdo;
 
-    ULONG poll_interval;
-    UNICODE_STRING link_name;
+        struct
+        {
+            DEVICE_OBJECT *parent_fdo;
+
+            HID_COLLECTION_INFORMATION information;
+            WINE_HIDP_PREPARSED_DATA *preparsed_data;
+
+            ULONG poll_interval;
+            struct ReportRingBuffer *ring_buffer;
+            HANDLE halt_event;
+            HANDLE thread;
+            UINT32 rawinput_handle;
+
+            UNICODE_STRING link_name;
+
+            KSPIN_LOCK irp_queue_lock;
+            LIST_ENTRY irp_queue;
+
+            BOOL is_mouse;
+            UNICODE_STRING mouse_link_name;
+        } pdo;
+    } u;
+
+    /* These are unique to the parent FDO, but stored in the children as well
+     * for convenience. */
     WCHAR device_id[MAX_DEVICE_ID_LEN];
     WCHAR instance_id[MAX_DEVICE_ID_LEN];
-    struct ReportRingBuffer *ring_buffer;
-    HANDLE halt_event;
-    HANDLE thread;
-    UINT32 rawinput_handle;
 
-    KSPIN_LOCK irp_queue_lock;
-    LIST_ENTRY irp_queue;
-
-    BOOL is_mouse;
-    UNICODE_STRING mouse_link_name;
-
-    /* Minidriver Specific stuff will end up here */
+    BOOL is_fdo;
 } BASE_DEVICE_EXTENSION;
 
 void RingBuffer_Write(struct ReportRingBuffer *buffer, void *data) DECLSPEC_HIDDEN;
@@ -87,16 +106,16 @@ typedef struct _minidriver
 NTSTATUS call_minidriver(ULONG code, DEVICE_OBJECT *device, void *in_buff, ULONG in_size, void *out_buff, ULONG out_size) DECLSPEC_HIDDEN;
 
 /* Internal device functions */
-NTSTATUS HID_CreateDevice(DEVICE_OBJECT *native_device, HID_MINIDRIVER_REGISTRATION *driver, DEVICE_OBJECT **device) DECLSPEC_HIDDEN;
 NTSTATUS HID_LinkDevice(DEVICE_OBJECT *device) DECLSPEC_HIDDEN;
-void HID_DeleteDevice(DEVICE_OBJECT *device) DECLSPEC_HIDDEN;
 void HID_StartDeviceThread(DEVICE_OBJECT *device) DECLSPEC_HIDDEN;
 
-NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
-NTSTATUS WINAPI HID_Device_read(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
-NTSTATUS WINAPI HID_Device_write(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
-NTSTATUS WINAPI HID_Device_create(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
-NTSTATUS WINAPI HID_Device_close(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
+IRP *pop_irp_from_queue(BASE_DEVICE_EXTENSION *ext) DECLSPEC_HIDDEN;
+
+NTSTATUS WINAPI pdo_ioctl(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
+NTSTATUS WINAPI pdo_read(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
+NTSTATUS WINAPI pdo_write(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
+NTSTATUS WINAPI pdo_create(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
+NTSTATUS WINAPI pdo_close(DEVICE_OBJECT *device, IRP *irp) DECLSPEC_HIDDEN;
 
 /* Parsing HID Report Descriptors into preparsed data */
 WINE_HIDP_PREPARSED_DATA* ParseDescriptor(BYTE *descriptor, unsigned int length) DECLSPEC_HIDDEN;
diff --git a/dlls/hidclass.sys/pnp.c b/dlls/hidclass.sys/pnp.c
index bb2035cac21..88918a614f0 100644
--- a/dlls/hidclass.sys/pnp.c
+++ b/dlls/hidclass.sys/pnp.c
@@ -22,6 +22,7 @@
 #include <unistd.h>
 #include <stdarg.h>
 #include "hid.h"
+#include "ntddmou.h"
 #include "ddk/hidtypes.h"
 #include "ddk/wdm.h"
 #include "regstr.h"
@@ -30,6 +31,23 @@
 
 WINE_DEFAULT_DEBUG_CHANNEL(hid);
 
+#if defined(__i386__) && !defined(_WIN32)
+
+extern void * WINAPI wrap_fastcall_func1( void *func, const void *a );
+__ASM_STDCALL_FUNC( wrap_fastcall_func1, 8,
+                   "popl %ecx\n\t"
+                   "popl %eax\n\t"
+                   "xchgl (%esp),%ecx\n\t"
+                   "jmp *%eax" );
+
+#define call_fastcall_func1(func,a) wrap_fastcall_func1(func,a)
+
+#else
+
+#define call_fastcall_func1(func,a) func(a)
+
+#endif
+
 static struct list minidriver_list = LIST_INIT(minidriver_list);
 
 static minidriver *find_minidriver(DRIVER_OBJECT *driver)
@@ -82,74 +100,92 @@ static NTSTATUS get_device_id(DEVICE_OBJECT *device, BUS_QUERY_ID_TYPE type, WCH
     return status;
 }
 
-static NTSTATUS WINAPI driver_add_device(DRIVER_OBJECT *driver, DEVICE_OBJECT *PDO)
+static NTSTATUS WINAPI driver_add_device(DRIVER_OBJECT *driver, DEVICE_OBJECT *bus_pdo)
 {
-    WCHAR device_id[MAX_DEVICE_ID_LEN], instance_id[MAX_DEVICE_ID_LEN];
-    DEVICE_OBJECT *device = NULL;
+    WCHAR device_id[MAX_DEVICE_ID_LEN], instance_id[MAX_DEVICE_ID_LEN], pdo_name[255];
+    BASE_DEVICE_EXTENSION *ext, *pdo_ext;
+    HID_DEVICE_ATTRIBUTES attr = {0};
+    DEVICE_OBJECT *fdo, *child_pdo;
+    UNICODE_STRING string;
     NTSTATUS status;
     minidriver *minidriver;
-    HID_DEVICE_ATTRIBUTES attr = {0};
-    BASE_DEVICE_EXTENSION *ext = NULL;
     HID_DESCRIPTOR descriptor;
     BYTE *reportDescriptor;
     INT i;
 
-    if ((status = get_device_id(PDO, BusQueryDeviceID, device_id)))
+    if ((status = get_device_id(bus_pdo, BusQueryDeviceID, device_id)))
     {
         ERR("Failed to get PDO device id, status %#x.\n", status);
         return status;
     }
 
-    if ((status = get_device_id(PDO, BusQueryInstanceID, instance_id)))
+    if ((status = get_device_id(bus_pdo, BusQueryInstanceID, instance_id)))
     {
         ERR("Failed to get PDO instance id, status %#x.\n", status);
         return status;
     }
 
-    TRACE("Adding device to PDO %p, id %s\\%s.\n", PDO, debugstr_w(device_id), debugstr_w(instance_id));
+    TRACE("Adding device to PDO %p, id %s\\%s.\n", bus_pdo, debugstr_w(device_id), debugstr_w(instance_id));
     minidriver = find_minidriver(driver);
 
-    status = HID_CreateDevice(PDO, &minidriver->minidriver, &device);
-    if (status != STATUS_SUCCESS)
+    if ((status = IoCreateDevice(driver, sizeof(*ext) + minidriver->minidriver.DeviceExtensionSize,
+            NULL, FILE_DEVICE_BUS_EXTENDER, 0, FALSE, &fdo)))
     {
-        ERR("Failed to create HID object (%x)\n",status);
+        ERR("Failed to create bus FDO, status %#x.\n", status);
         return status;
     }
+    ext = fdo->DeviceExtension;
+    ext->is_fdo = TRUE;
+    ext->u.fdo.hid_ext.MiniDeviceExtension = ext + 1;
+    ext->u.fdo.hid_ext.PhysicalDeviceObject = fdo;
+    ext->u.fdo.hid_ext.NextDeviceObject = bus_pdo;
+    swprintf(ext->device_id, ARRAY_SIZE(ext->device_id), L"HID\\%s", wcsrchr(device_id, '\\') + 1);
+    wcscpy(ext->instance_id, instance_id);
 
-    ext = device->DeviceExtension;
-    InitializeListHead(&ext->irp_queue);
-    KeInitializeSpinLock(&ext->irp_queue_lock);
-
-    TRACE("Created device %p\n",device);
-    status = minidriver->AddDevice(minidriver->minidriver.DriverObject, device);
+    status = minidriver->AddDevice(minidriver->minidriver.DriverObject, fdo);
     if (status != STATUS_SUCCESS)
     {
         ERR("Minidriver AddDevice failed (%x)\n",status);
-        HID_DeleteDevice(device);
+        IoDeleteDevice(fdo);
         return status;
     }
 
-    status = call_minidriver(IOCTL_HID_GET_DEVICE_ATTRIBUTES, device,
-        NULL, 0, &attr, sizeof(attr));
-
+    status = call_minidriver(IOCTL_HID_GET_DEVICE_ATTRIBUTES, fdo, NULL, 0, &attr, sizeof(attr));
     if (status != STATUS_SUCCESS)
     {
         ERR("Minidriver failed to get Attributes(%x)\n",status);
-        HID_DeleteDevice(device);
+        IoDeleteDevice(fdo);
         return status;
     }
 
-    ext->information.VendorID = attr.VendorID;
-    ext->information.ProductID = attr.ProductID;
-    ext->information.VersionNumber = attr.VersionNumber;
-    ext->information.Polled = minidriver->minidriver.DevicesArePolled;
+    swprintf(pdo_name, ARRAY_SIZE(pdo_name), L"\\Device\\HID#%p&%p", driver, bus_pdo);
+    RtlInitUnicodeString(&string, pdo_name);
+    if ((status = IoCreateDevice(driver, sizeof(*ext), &string, 0, 0, FALSE, &child_pdo)))
+    {
+        ERR("Failed to create child PDO, status %#x.\n", status);
+        IoDeleteDevice(fdo);
+        return status;
+    }
+    ext->u.fdo.child_pdo = child_pdo;
 
-    status = call_minidriver(IOCTL_HID_GET_DEVICE_DESCRIPTOR, device, NULL, 0,
-        &descriptor, sizeof(descriptor));
+    pdo_ext = child_pdo->DeviceExtension;
+    pdo_ext->u.pdo.parent_fdo = fdo;
+    InitializeListHead(&pdo_ext->u.pdo.irp_queue);
+    KeInitializeSpinLock(&pdo_ext->u.pdo.irp_queue_lock);
+    wcscpy(pdo_ext->device_id, ext->device_id);
+    wcscpy(pdo_ext->instance_id, ext->instance_id);
+
+    pdo_ext->u.pdo.information.VendorID = attr.VendorID;
+    pdo_ext->u.pdo.information.ProductID = attr.ProductID;
+    pdo_ext->u.pdo.information.VersionNumber = attr.VersionNumber;
+    pdo_ext->u.pdo.information.Polled = minidriver->minidriver.DevicesArePolled;
+
+    status = call_minidriver(IOCTL_HID_GET_DEVICE_DESCRIPTOR, fdo, NULL, 0, &descriptor, sizeof(descriptor));
     if (status != STATUS_SUCCESS)
     {
         ERR("Cannot get Device Descriptor(%x)\n",status);
-        HID_DeleteDevice(device);
+        IoDeleteDevice(child_pdo);
+        IoDeleteDevice(fdo);
         return status;
     }
     for (i = 0; i < descriptor.bNumDescriptors; i++)
@@ -159,84 +195,114 @@ static NTSTATUS WINAPI driver_add_device(DRIVER_OBJECT *driver, DEVICE_OBJECT *P
     if (i >= descriptor.bNumDescriptors)
     {
         ERR("No Report Descriptor found in reply\n");
-        HID_DeleteDevice(device);
+        IoDeleteDevice(child_pdo);
+        IoDeleteDevice(fdo);
         return status;
     }
 
     reportDescriptor = HeapAlloc(GetProcessHeap(), 0, descriptor.DescriptorList[i].wReportLength);
-    status = call_minidriver(IOCTL_HID_GET_REPORT_DESCRIPTOR, device, NULL, 0,
+    status = call_minidriver(IOCTL_HID_GET_REPORT_DESCRIPTOR, fdo, NULL, 0,
         reportDescriptor, descriptor.DescriptorList[i].wReportLength);
     if (status != STATUS_SUCCESS)
     {
         ERR("Cannot get Report Descriptor(%x)\n",status);
-        HID_DeleteDevice(device);
         HeapFree(GetProcessHeap(), 0, reportDescriptor);
+        IoDeleteDevice(child_pdo);
+        IoDeleteDevice(fdo);
         return status;
     }
 
-    ext->preparseData = ParseDescriptor(reportDescriptor, descriptor.DescriptorList[i].wReportLength);
-
+    pdo_ext->u.pdo.preparsed_data = ParseDescriptor(reportDescriptor, descriptor.DescriptorList[i].wReportLength);
     HeapFree(GetProcessHeap(), 0, reportDescriptor);
-    if (!ext->preparseData)
+    if (!pdo_ext->u.pdo.preparsed_data)
     {
         ERR("Cannot parse Report Descriptor\n");
-        HID_DeleteDevice(device);
+        IoDeleteDevice(child_pdo);
+        IoDeleteDevice(fdo);
         return STATUS_NOT_SUPPORTED;
     }
 
-    ext->information.DescriptorSize = ext->preparseData->dwSize;
+    pdo_ext->u.pdo.information.DescriptorSize = pdo_ext->u.pdo.preparsed_data->dwSize;
 
-    lstrcpyW(ext->instance_id, instance_id);
+    IoAttachDeviceToDeviceStack(fdo, bus_pdo);
 
-    lstrcpyW(ext->device_id, L"HID");
-    lstrcatW(ext->device_id, L"\\");
-    lstrcatW(ext->device_id, wcschr(device_id, '\\') + 1);
+    IoInvalidateDeviceRelations(bus_pdo, BusRelations);
 
-    HID_LinkDevice(device);
+    HID_LinkDevice(child_pdo);
 
-    ext->poll_interval = DEFAULT_POLL_INTERVAL;
+    pdo_ext->u.pdo.poll_interval = DEFAULT_POLL_INTERVAL;
 
-    ext->ring_buffer = RingBuffer_Create(sizeof(HID_XFER_PACKET) + ext->preparseData->caps.InputReportByteLength);
+    pdo_ext->u.pdo.ring_buffer = RingBuffer_Create(
+            sizeof(HID_XFER_PACKET) + pdo_ext->u.pdo.preparsed_data->caps.InputReportByteLength);
 
-    HID_StartDeviceThread(device);
+    HID_StartDeviceThread(child_pdo);
 
+    fdo->Flags &= ~DO_DEVICE_INITIALIZING;
     return STATUS_SUCCESS;
 }
 
-static NTSTATUS remove_device(minidriver *minidriver, DEVICE_OBJECT *device, IRP *irp)
+static NTSTATUS fdo_pnp(DEVICE_OBJECT *device, IRP *irp)
 {
+    minidriver *minidriver = find_minidriver(device->DriverObject);
+    IO_STACK_LOCATION *stack = IoGetCurrentIrpStackLocation(irp);
     BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
-    NTSTATUS rc = STATUS_NOT_SUPPORTED;
 
-    rc = IoSetDeviceInterfaceState(&ext->link_name, FALSE);
-    if (rc)
+    TRACE("irp %p, minor function %#x.\n", irp, stack->MinorFunction);
+
+    switch (stack->MinorFunction)
     {
-        FIXME("failed to disable interface %x\n", rc);
-        return rc;
+        case IRP_MN_QUERY_DEVICE_RELATIONS:
+        {
+            DEVICE_RELATIONS *devices;
+
+            if (stack->Parameters.QueryDeviceRelations.Type != BusRelations)
+                return minidriver->PNPDispatch(device, irp);
+
+            if (!(devices = ExAllocatePool(PagedPool, offsetof(DEVICE_RELATIONS, Objects[1]))))
+            {
+                irp->IoStatus.u.Status = STATUS_NO_MEMORY;
+                IoCompleteRequest(irp, IO_NO_INCREMENT);
+                return STATUS_NO_MEMORY;
+            }
+
+            devices->Objects[0] = ext->u.fdo.child_pdo;
+            call_fastcall_func1(ObfReferenceObject, ext->u.fdo.child_pdo);
+            devices->Count = 1;
+
+            irp->IoStatus.Information = (ULONG_PTR)devices;
+            irp->IoStatus.u.Status = STATUS_SUCCESS;
+            IoSkipCurrentIrpStackLocation(irp);
+            return IoCallDriver(ext->u.fdo.hid_ext.NextDeviceObject, irp);
+        }
+
+        case IRP_MN_REMOVE_DEVICE:
+        {
+            NTSTATUS ret;
+
+            ret = minidriver->PNPDispatch(device, irp);
+
+            IoDetachDevice(ext->u.fdo.hid_ext.NextDeviceObject);
+            IoDeleteDevice(device);
+            return ret;
+        }
+
+        default:
+            return minidriver->PNPDispatch(device, irp);
     }
-
-    if (ext->is_mouse)
-        IoSetDeviceInterfaceState(&ext->mouse_link_name, FALSE);
-
-    if (irp)
-        rc = minidriver->PNPDispatch(device, irp);
-    HID_DeleteDevice(device);
-    return rc;
 }
 
-static NTSTATUS WINAPI driver_pnp(DEVICE_OBJECT *device, IRP *irp)
+static NTSTATUS pdo_pnp(DEVICE_OBJECT *device, IRP *irp)
 {
-    NTSTATUS rc = STATUS_NOT_SUPPORTED;
     IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation(irp);
-    minidriver *minidriver = find_minidriver(device->DriverObject);
+    BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
+    NTSTATUS status = irp->IoStatus.u.Status;
 
-    TRACE("%p, %p\n", device, irp);
+    TRACE("irp %p, minor function %#x.\n", irp, irpsp->MinorFunction);
 
     switch(irpsp->MinorFunction)
     {
         case IRP_MN_QUERY_ID:
         {
-            BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
             WCHAR *id = ExAllocatePool(PagedPool, sizeof(WCHAR) * REGSTR_VAL_MAX_HCID_LEN);
             TRACE("IRP_MN_QUERY_ID[%i]\n", irpsp->Parameters.QueryId.IdType);
             switch (irpsp->Parameters.QueryId.IdType)
@@ -261,18 +327,18 @@ static NTSTATUS WINAPI driver_pnp(DEVICE_OBJECT *device, IRP *irp)
                     ptr += lstrlenW(L"HID") + 1;
                     *ptr = 0;
                     irp->IoStatus.Information = (ULONG_PTR)id;
-                    rc = STATUS_SUCCESS;
+                    status = STATUS_SUCCESS;
                     break;
                 }
                 case BusQueryDeviceID:
                     lstrcpyW(id, ext->device_id);
                     irp->IoStatus.Information = (ULONG_PTR)id;
-                    rc = STATUS_SUCCESS;
+                    status = STATUS_SUCCESS;
                     break;
                 case BusQueryInstanceID:
                     lstrcpyW(id, ext->instance_id);
                     irp->IoStatus.Information = (ULONG_PTR)id;
-                    rc = STATUS_SUCCESS;
+                    status = STATUS_SUCCESS;
                     break;
 
                 case BusQueryContainerID:
@@ -283,31 +349,125 @@ static NTSTATUS WINAPI driver_pnp(DEVICE_OBJECT *device, IRP *irp)
             }
             break;
         }
-        case IRP_MN_START_DEVICE:
+
+        case IRP_MN_QUERY_CAPABILITIES:
         {
-            BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
+            DEVICE_CAPABILITIES *caps = irpsp->Parameters.DeviceCapabilities.Capabilities;
 
-            rc = minidriver->PNPDispatch(device, irp);
-
-            IoSetDeviceInterfaceState(&ext->link_name, TRUE);
-            if (ext->is_mouse)
-                IoSetDeviceInterfaceState(&ext->mouse_link_name, TRUE);
-            return rc;
+            caps->RawDeviceOK = 1;
+            status = STATUS_SUCCESS;
+            break;
         }
+
+        case IRP_MN_START_DEVICE:
+            if ((status = IoRegisterDeviceInterface(device, &GUID_DEVINTERFACE_HID, NULL, &ext->u.pdo.link_name)))
+            {
+                ERR("Failed to register interface, status %#x.\n", status);
+                break;
+            }
+
+            /* FIXME: This should probably be done in mouhid.sys. */
+            if (ext->u.pdo.preparsed_data->caps.UsagePage == HID_USAGE_PAGE_GENERIC
+                    && ext->u.pdo.preparsed_data->caps.Usage == HID_USAGE_GENERIC_MOUSE)
+            {
+                if (!IoRegisterDeviceInterface(device, &GUID_DEVINTERFACE_MOUSE, NULL, &ext->u.pdo.mouse_link_name))
+                    ext->u.pdo.is_mouse = TRUE;
+            }
+
+            IoSetDeviceInterfaceState(&ext->u.pdo.link_name, TRUE);
+            if (ext->u.pdo.is_mouse)
+                IoSetDeviceInterfaceState(&ext->u.pdo.mouse_link_name, TRUE);
+            status = STATUS_SUCCESS;
+            break;
+
         case IRP_MN_REMOVE_DEVICE:
         {
-            return remove_device(minidriver, device, irp);
+            IRP *queued_irp;
+
+            IoSetDeviceInterfaceState(&ext->u.pdo.link_name, FALSE);
+            if (ext->u.pdo.is_mouse)
+                IoSetDeviceInterfaceState(&ext->u.pdo.mouse_link_name, FALSE);
+
+            if (ext->u.pdo.thread)
+            {
+                SetEvent(ext->u.pdo.halt_event);
+                WaitForSingleObject(ext->u.pdo.thread, INFINITE);
+            }
+            CloseHandle(ext->u.pdo.halt_event);
+
+            HeapFree(GetProcessHeap(), 0, ext->u.pdo.preparsed_data);
+            if (ext->u.pdo.ring_buffer)
+                RingBuffer_Destroy(ext->u.pdo.ring_buffer);
+
+            while ((queued_irp = pop_irp_from_queue(ext)))
+            {
+                queued_irp->IoStatus.u.Status = STATUS_DEVICE_REMOVED;
+                IoCompleteRequest(queued_irp, IO_NO_INCREMENT);
+            }
+
+            RtlFreeUnicodeString(&ext->u.pdo.link_name);
+
+            irp->IoStatus.u.Status = STATUS_SUCCESS;
+            IoCompleteRequest(irp, IO_NO_INCREMENT);
+            IoDeleteDevice(device);
+            return STATUS_SUCCESS;
         }
+
+        case IRP_MN_SURPRISE_REMOVAL:
+            status = STATUS_SUCCESS;
+            break;
+
         default:
-        {
-            /* Forward IRP to the minidriver */
-            return minidriver->PNPDispatch(device, irp);
-        }
+            FIXME("Unhandled minor function %#x.\n", irpsp->MinorFunction);
     }
 
-    irp->IoStatus.u.Status = rc;
+    irp->IoStatus.u.Status = status;
     IoCompleteRequest( irp, IO_NO_INCREMENT );
-    return rc;
+    return status;
+}
+
+static NTSTATUS WINAPI driver_pnp(DEVICE_OBJECT *device, IRP *irp)
+{
+    BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
+
+    if (ext->is_fdo)
+        return fdo_pnp(device, irp);
+    else
+        return pdo_pnp(device, irp);
+}
+
+static NTSTATUS WINAPI driver_create(DEVICE_OBJECT *device, IRP *irp)
+{
+    BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
+
+    if (ext->is_fdo)
+    {
+        irp->IoStatus.u.Status = STATUS_UNSUCCESSFUL;
+        IoCompleteRequest(irp, IO_NO_INCREMENT);
+        return STATUS_UNSUCCESSFUL;
+    }
+
+    return pdo_create(device, irp);
+}
+
+static NTSTATUS WINAPI driver_close(DEVICE_OBJECT *device, IRP *irp)
+{
+    return pdo_close(device, irp);
+}
+
+static NTSTATUS WINAPI driver_ioctl(DEVICE_OBJECT *device, IRP *irp)
+{
+    return pdo_ioctl(device, irp);
+}
+
+static NTSTATUS WINAPI driver_read(DEVICE_OBJECT *device, IRP *irp)
+{
+    return pdo_read(device, irp);
+}
+
+static NTSTATUS WINAPI driver_write(DEVICE_OBJECT *device, IRP *irp)
+{
+    return pdo_write(device, irp);
 }
 
 static void WINAPI driver_unload(DRIVER_OBJECT *driver)
@@ -335,11 +495,11 @@ NTSTATUS WINAPI HidRegisterMinidriver(HID_MINIDRIVER_REGISTRATION *registration)
     driver->DriverUnload = registration->DriverObject->DriverUnload;
     registration->DriverObject->DriverUnload = driver_unload;
 
-    registration->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = HID_Device_ioctl;
-    registration->DriverObject->MajorFunction[IRP_MJ_READ] = HID_Device_read;
-    registration->DriverObject->MajorFunction[IRP_MJ_WRITE] = HID_Device_write;
-    registration->DriverObject->MajorFunction[IRP_MJ_CREATE] = HID_Device_create;
-    registration->DriverObject->MajorFunction[IRP_MJ_CLOSE] = HID_Device_close;
+    registration->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = driver_ioctl;
+    registration->DriverObject->MajorFunction[IRP_MJ_READ] = driver_read;
+    registration->DriverObject->MajorFunction[IRP_MJ_WRITE] = driver_write;
+    registration->DriverObject->MajorFunction[IRP_MJ_CREATE] = driver_create;
+    registration->DriverObject->MajorFunction[IRP_MJ_CLOSE] = driver_close;
 
     driver->PNPDispatch = registration->DriverObject->MajorFunction[IRP_MJ_PNP];
     registration->DriverObject->MajorFunction[IRP_MJ_PNP] = driver_pnp;
diff --git a/dlls/ntoskrnl.exe/tests/driver_hid.c b/dlls/ntoskrnl.exe/tests/driver_hid.c
index 184a96321fc..7ddf49f4dcc 100644
--- a/dlls/ntoskrnl.exe/tests/driver_hid.c
+++ b/dlls/ntoskrnl.exe/tests/driver_hid.c
@@ -217,7 +217,7 @@ static NTSTATUS WINAPI driver_add_device(DRIVER_OBJECT *driver, DEVICE_OBJECT *f
     ok(!!ext->PhysicalDeviceObject, "expected non-NULL pdo\n");
     todo_wine ok(ext->NextDeviceObject == ext->PhysicalDeviceObject, "got pdo %p, next %p\n",
             ext->PhysicalDeviceObject, ext->NextDeviceObject);
-    ok(ext->NextDeviceObject->AttachedDevice == fdo, "wrong attached device\n");
+    todo_wine ok(ext->NextDeviceObject->AttachedDevice == fdo, "wrong attached device\n");
 
     ret = IoRegisterDeviceInterface(ext->PhysicalDeviceObject, &control_class, NULL, &control_symlink);
     ok(!ret, "got %#x\n", ret);
-- 
2.30.2




More information about the wine-devel mailing list