2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Mouse class driver
4 * FILE: drivers/mouclass/mouclass.c
5 * PURPOSE: Mouse class driver
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
13 static DRIVER_UNLOAD DriverUnload
;
14 static DRIVER_DISPATCH ClassCreate
;
15 static DRIVER_DISPATCH ClassClose
;
16 static DRIVER_DISPATCH ClassCleanup
;
17 static DRIVER_DISPATCH ClassRead
;
18 static DRIVER_DISPATCH ClassDeviceControl
;
19 static DRIVER_DISPATCH IrpStub
;
20 static DRIVER_ADD_DEVICE ClassAddDevice
;
21 static DRIVER_STARTIO ClassStartIo
;
22 static DRIVER_CANCEL ClassCancelRoutine
;
25 IN PDEVICE_OBJECT DeviceObject
,
30 DriverUnload(IN PDRIVER_OBJECT DriverObject
)
32 // nothing to do here yet
37 IN PDEVICE_OBJECT DeviceObject
,
40 TRACE_(CLASS_NAME
, "IRP_MJ_CREATE\n");
42 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
43 return ForwardIrpAndForget(DeviceObject
, Irp
);
45 /* FIXME: open all associated Port devices */
46 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
47 Irp
->IoStatus
.Information
= 0;
48 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
49 return STATUS_SUCCESS
;
54 IN PDEVICE_OBJECT DeviceObject
,
57 TRACE_(CLASS_NAME
, "IRP_MJ_CLOSE\n");
59 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
60 return ForwardIrpAndForget(DeviceObject
, Irp
);
62 /* FIXME: close all associated Port devices */
63 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
64 Irp
->IoStatus
.Information
= 0;
65 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
66 return STATUS_SUCCESS
;
71 IN PDEVICE_OBJECT DeviceObject
,
74 TRACE_(CLASS_NAME
, "IRP_MJ_CLEANUP\n");
76 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
77 return ForwardIrpAndForget(DeviceObject
, Irp
);
79 /* FIXME: cleanup all associated Port devices */
80 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
81 Irp
->IoStatus
.Information
= 0;
82 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
83 return STATUS_SUCCESS
;
88 IN PDEVICE_OBJECT DeviceObject
,
91 PCLASS_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
95 TRACE_(CLASS_NAME
, "IRP_MJ_READ\n");
97 ASSERT(DeviceExtension
->Common
.IsClassDO
);
99 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
100 return ForwardIrpAndForget(DeviceObject
, Irp
);
102 if (IoGetCurrentIrpStackLocation(Irp
)->Parameters
.Read
.Length
< sizeof(MOUSE_INPUT_DATA
))
104 Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
;
105 Irp
->IoStatus
.Information
= 0;
106 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
108 return STATUS_BUFFER_TOO_SMALL
;
111 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &OldIrql
);
112 Status
= HandleReadIrp(DeviceObject
, Irp
, FALSE
);
113 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, OldIrql
);
117 static NTSTATUS NTAPI
119 IN PDEVICE_OBJECT DeviceObject
,
122 PCLASS_DEVICE_EXTENSION DeviceExtension
;
123 NTSTATUS Status
= STATUS_NOT_SUPPORTED
;
125 TRACE_(CLASS_NAME
, "IRP_MJ_DEVICE_CONTROL\n");
127 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
128 return ForwardIrpAndForget(DeviceObject
, Irp
);
130 DeviceExtension
= (PCLASS_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
132 switch (IoGetCurrentIrpStackLocation(Irp
)->Parameters
.DeviceIoControl
.IoControlCode
)
134 case IOCTL_MOUSE_QUERY_ATTRIBUTES
:
136 /* FIXME: We hope that all devices will return the same result.
137 * Ask only the first one */
138 PLIST_ENTRY Head
= &((PCLASS_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->ListHead
;
139 if (Head
->Flink
!= Head
)
141 /* We have at least one device */
142 PPORT_DEVICE_EXTENSION DevExt
= CONTAINING_RECORD(Head
->Flink
, PORT_DEVICE_EXTENSION
, ListEntry
);
143 IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
= IRP_MJ_INTERNAL_DEVICE_CONTROL
;
144 IoSkipCurrentIrpStackLocation(Irp
);
145 return IoCallDriver(DevExt
->DeviceObject
, Irp
);
150 WARN_(CLASS_NAME
, "IRP_MJ_DEVICE_CONTROL / unknown I/O control code 0x%lx\n",
151 IoGetCurrentIrpStackLocation(Irp
)->Parameters
.DeviceIoControl
.IoControlCode
);
156 Irp
->IoStatus
.Status
= Status
;
157 Irp
->IoStatus
.Information
= 0;
158 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
163 static NTSTATUS NTAPI
165 IN PDEVICE_OBJECT DeviceObject
,
168 NTSTATUS Status
= STATUS_NOT_SUPPORTED
;
170 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
172 /* Forward some IRPs to lower device */
173 switch (IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
)
176 case IRP_MJ_INTERNAL_DEVICE_CONTROL
:
177 return ForwardIrpAndForget(DeviceObject
, Irp
);
180 ERR_(CLASS_NAME
, "Port DO stub for major function 0x%lx\n",
181 IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
);
188 ERR_(CLASS_NAME
, "Class DO stub for major function 0x%lx\n",
189 IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
);
193 Irp
->IoStatus
.Status
= Status
;
194 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
200 IN PUNICODE_STRING RegistryPath
,
201 IN PCLASS_DRIVER_EXTENSION DriverExtension
)
203 UNICODE_STRING ParametersRegistryKey
;
204 RTL_QUERY_REGISTRY_TABLE Parameters
[4];
207 ULONG DefaultConnectMultiplePorts
= 1;
208 ULONG DefaultDataQueueSize
= 0x64;
209 PCWSTR DefaultDeviceBaseName
= L
"PointerClass";
211 ParametersRegistryKey
.Length
= 0;
212 ParametersRegistryKey
.MaximumLength
= RegistryPath
->Length
+ sizeof(L
"\\Parameters") + sizeof(UNICODE_NULL
);
213 ParametersRegistryKey
.Buffer
= ExAllocatePoolWithTag(PagedPool
, ParametersRegistryKey
.MaximumLength
, CLASS_TAG
);
214 if (!ParametersRegistryKey
.Buffer
)
216 WARN_(CLASS_NAME
, "ExAllocatePoolWithTag() failed\n");
217 return STATUS_NO_MEMORY
;
219 RtlCopyUnicodeString(&ParametersRegistryKey
, RegistryPath
);
220 RtlAppendUnicodeToString(&ParametersRegistryKey
, L
"\\Parameters");
221 ParametersRegistryKey
.Buffer
[ParametersRegistryKey
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
223 RtlZeroMemory(Parameters
, sizeof(Parameters
));
225 Parameters
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_REGISTRY_OPTIONAL
;
226 Parameters
[0].Name
= L
"ConnectMultiplePorts";
227 Parameters
[0].EntryContext
= &DriverExtension
->ConnectMultiplePorts
;
228 Parameters
[0].DefaultType
= REG_DWORD
;
229 Parameters
[0].DefaultData
= &DefaultConnectMultiplePorts
;
230 Parameters
[0].DefaultLength
= sizeof(ULONG
);
232 Parameters
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_REGISTRY_OPTIONAL
;
233 Parameters
[1].Name
= L
"MouseDataQueueSize";
234 Parameters
[1].EntryContext
= &DriverExtension
->DataQueueSize
;
235 Parameters
[1].DefaultType
= REG_DWORD
;
236 Parameters
[1].DefaultData
= &DefaultDataQueueSize
;
237 Parameters
[1].DefaultLength
= sizeof(ULONG
);
239 Parameters
[2].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_REGISTRY_OPTIONAL
;
240 Parameters
[2].Name
= L
"PointerDeviceBaseName";
241 Parameters
[2].EntryContext
= &DriverExtension
->DeviceBaseName
;
242 Parameters
[2].DefaultType
= REG_SZ
;
243 Parameters
[2].DefaultData
= (PVOID
)DefaultDeviceBaseName
;
244 Parameters
[2].DefaultLength
= 0;
246 Status
= RtlQueryRegistryValues(
247 RTL_REGISTRY_ABSOLUTE
,
248 ParametersRegistryKey
.Buffer
,
253 if (NT_SUCCESS(Status
))
256 if (DriverExtension
->ConnectMultiplePorts
!= 0
257 && DriverExtension
->ConnectMultiplePorts
!= 1)
259 DriverExtension
->ConnectMultiplePorts
= DefaultConnectMultiplePorts
;
261 if (DriverExtension
->DataQueueSize
== 0)
263 DriverExtension
->DataQueueSize
= DefaultDataQueueSize
;
266 else if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
268 /* Registry path doesn't exist. Set defaults */
269 DriverExtension
->ConnectMultiplePorts
= DefaultConnectMultiplePorts
;
270 DriverExtension
->DataQueueSize
= DefaultDataQueueSize
;
271 if (RtlCreateUnicodeString(&DriverExtension
->DeviceBaseName
, DefaultDeviceBaseName
))
272 Status
= STATUS_SUCCESS
;
274 Status
= STATUS_NO_MEMORY
;
277 ExFreePoolWithTag(ParametersRegistryKey
.Buffer
, CLASS_TAG
);
282 CreateClassDeviceObject(
283 IN PDRIVER_OBJECT DriverObject
,
284 OUT PDEVICE_OBJECT
*ClassDO OPTIONAL
)
286 PCLASS_DRIVER_EXTENSION DriverExtension
;
289 UNICODE_STRING DeviceNameU
;
290 PWSTR DeviceIdW
= NULL
; /* Pointer into DeviceNameU.Buffer */
292 PCLASS_DEVICE_EXTENSION DeviceExtension
;
295 TRACE_(CLASS_NAME
, "CreateClassDeviceObject(0x%p)\n", DriverObject
);
297 /* Create new device object */
298 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
299 DeviceNameU
.Length
= 0;
300 DeviceNameU
.MaximumLength
=
301 wcslen(L
"\\Device\\") * sizeof(WCHAR
) /* "\Device\" */
302 + DriverExtension
->DeviceBaseName
.Length
/* "PointerClass" */
303 + 4 * sizeof(WCHAR
) /* Id between 0 and 9999 */
304 + sizeof(UNICODE_NULL
); /* Final NULL char */
305 DeviceNameU
.Buffer
= ExAllocatePoolWithTag(PagedPool
, DeviceNameU
.MaximumLength
, CLASS_TAG
);
306 if (!DeviceNameU
.Buffer
)
308 WARN_(CLASS_NAME
, "ExAllocatePoolWithTag() failed\n");
309 return STATUS_NO_MEMORY
;
311 Status
= RtlAppendUnicodeToString(&DeviceNameU
, L
"\\Device\\");
312 if (!NT_SUCCESS(Status
))
314 WARN_(CLASS_NAME
, "RtlAppendUnicodeToString() failed with status 0x%08lx\n", Status
);
317 Status
= RtlAppendUnicodeStringToString(&DeviceNameU
, &DriverExtension
->DeviceBaseName
);
318 if (!NT_SUCCESS(Status
))
320 WARN_(CLASS_NAME
, "RtlAppendUnicodeStringToString() failed with status 0x%08lx\n", Status
);
323 PrefixLength
= DeviceNameU
.MaximumLength
- 4 * sizeof(WCHAR
) - sizeof(UNICODE_NULL
);
324 DeviceIdW
= &DeviceNameU
.Buffer
[PrefixLength
/ sizeof(WCHAR
)];
325 while (DeviceId
< 9999)
327 DeviceNameU
.Length
= (USHORT
)(PrefixLength
+ swprintf(DeviceIdW
, L
"%lu", DeviceId
) * sizeof(WCHAR
));
328 Status
= IoCreateDevice(
330 sizeof(CLASS_DEVICE_EXTENSION
),
333 FILE_DEVICE_SECURE_OPEN
,
336 if (NT_SUCCESS(Status
))
338 else if (Status
!= STATUS_OBJECT_NAME_COLLISION
)
340 WARN_(CLASS_NAME
, "IoCreateDevice() failed with status 0x%08lx\n", Status
);
345 WARN_(CLASS_NAME
, "Too many devices starting with '\\Device\\%wZ'\n", &DriverExtension
->DeviceBaseName
);
346 Status
= STATUS_TOO_MANY_NAMES
;
348 if (!NT_SUCCESS(Status
))
350 ExFreePoolWithTag(DeviceNameU
.Buffer
, CLASS_TAG
);
354 DeviceExtension
= (PCLASS_DEVICE_EXTENSION
)Fdo
->DeviceExtension
;
355 RtlZeroMemory(DeviceExtension
, sizeof(CLASS_DEVICE_EXTENSION
));
356 DeviceExtension
->Common
.IsClassDO
= TRUE
;
357 DeviceExtension
->DriverExtension
= DriverExtension
;
358 InitializeListHead(&DeviceExtension
->ListHead
);
359 KeInitializeSpinLock(&DeviceExtension
->ListSpinLock
);
360 KeInitializeSpinLock(&DeviceExtension
->SpinLock
);
361 DeviceExtension
->InputCount
= 0;
362 DeviceExtension
->PortData
= ExAllocatePoolWithTag(NonPagedPool
, DeviceExtension
->DriverExtension
->DataQueueSize
* sizeof(MOUSE_INPUT_DATA
), CLASS_TAG
);
363 if (!DeviceExtension
->PortData
)
365 ExFreePoolWithTag(DeviceNameU
.Buffer
, CLASS_TAG
);
366 return STATUS_NO_MEMORY
;
368 DeviceExtension
->DeviceName
= DeviceNameU
.Buffer
;
369 Fdo
->Flags
|= DO_POWER_PAGABLE
;
370 Fdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
372 /* Add entry entry to HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\[DeviceBaseName] */
373 RtlWriteRegistryValue(
374 RTL_REGISTRY_DEVICEMAP
,
375 DriverExtension
->DeviceBaseName
.Buffer
,
376 DeviceExtension
->DeviceName
,
378 DriverExtension
->RegistryPath
.Buffer
,
379 DriverExtension
->RegistryPath
.MaximumLength
);
384 return STATUS_SUCCESS
;
389 IN PDEVICE_OBJECT ClassDeviceObject
,
391 IN PMOUSE_INPUT_DATA DataStart
,
392 IN SIZE_T NumberOfEntries
)
394 NTSTATUS Status
= STATUS_SUCCESS
;
396 if (ClassDeviceObject
->Flags
& DO_BUFFERED_IO
)
399 Irp
->AssociatedIrp
.SystemBuffer
,
401 NumberOfEntries
* sizeof(MOUSE_INPUT_DATA
));
403 else if (ClassDeviceObject
->Flags
& DO_DIRECT_IO
)
405 PVOID DestAddress
= MmGetSystemAddressForMdlSafe(Irp
->MdlAddress
, NormalPagePriority
);
411 NumberOfEntries
* sizeof(MOUSE_INPUT_DATA
));
414 Status
= STATUS_UNSUCCESSFUL
;
423 NumberOfEntries
* sizeof(MOUSE_INPUT_DATA
));
427 Status
= _SEH_GetExceptionCode();
437 IN PDEVICE_OBJECT ClassDeviceObject
,
438 IN OUT PMOUSE_INPUT_DATA DataStart
,
439 IN PMOUSE_INPUT_DATA DataEnd
,
440 IN OUT PULONG ConsumedCount
)
442 PCLASS_DEVICE_EXTENSION ClassDeviceExtension
= ClassDeviceObject
->DeviceExtension
;
444 SIZE_T InputCount
= DataEnd
- DataStart
;
447 TRACE_(CLASS_NAME
, "ClassCallback()\n");
449 ASSERT(ClassDeviceExtension
->Common
.IsClassDO
);
451 KeAcquireSpinLock(&ClassDeviceExtension
->SpinLock
, &OldIrql
);
454 if (ClassDeviceExtension
->InputCount
+ InputCount
> ClassDeviceExtension
->DriverExtension
->DataQueueSize
)
457 * We're exceeding the buffer, and data will be thrown away...
458 * FIXME: What could we do, as we are at DISPATCH_LEVEL?
460 ReadSize
= ClassDeviceExtension
->DriverExtension
->DataQueueSize
- ClassDeviceExtension
->InputCount
;
463 ReadSize
= InputCount
;
466 * Move the input data from the port data queue to our class data
470 &ClassDeviceExtension
->PortData
[ClassDeviceExtension
->InputCount
],
472 sizeof(MOUSE_INPUT_DATA
) * ReadSize
);
474 /* Move the counter up */
475 ClassDeviceExtension
->InputCount
+= ReadSize
;
477 (*ConsumedCount
) += (ULONG
)ReadSize
;
479 /* Complete pending IRP (if any) */
480 if (ClassDeviceExtension
->PendingIrp
)
481 HandleReadIrp(ClassDeviceObject
, ClassDeviceExtension
->PendingIrp
, FALSE
);
483 KeReleaseSpinLock(&ClassDeviceExtension
->SpinLock
, OldIrql
);
485 TRACE_(CLASS_NAME
, "Leaving ClassCallback()\n");
489 /* Send IOCTL_INTERNAL_*_CONNECT to port */
492 IN PDEVICE_OBJECT PortDO
,
493 IN PDEVICE_OBJECT ClassDO
)
497 IO_STATUS_BLOCK IoStatus
;
498 CONNECT_DATA ConnectData
;
501 TRACE_(CLASS_NAME
, "Connecting PortDO %p to ClassDO %p\n", PortDO
, ClassDO
);
503 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
505 ConnectData
.ClassDeviceObject
= ClassDO
;
506 ConnectData
.ClassService
= ClassCallback
;
508 Irp
= IoBuildDeviceIoControlRequest(
509 IOCTL_INTERNAL_MOUSE_CONNECT
,
511 &ConnectData
, sizeof(CONNECT_DATA
),
513 TRUE
, &Event
, &IoStatus
);
515 return STATUS_INSUFFICIENT_RESOURCES
;
517 Status
= IoCallDriver(PortDO
, Irp
);
519 if (Status
== STATUS_PENDING
)
520 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
522 IoStatus
.Status
= Status
;
524 if (NT_SUCCESS(IoStatus
.Status
))
526 ObReferenceObject(PortDO
);
527 ExInterlockedInsertTailList(
528 &((PCLASS_DEVICE_EXTENSION
)ClassDO
->DeviceExtension
)->ListHead
,
529 &((PPORT_DEVICE_EXTENSION
)PortDO
->DeviceExtension
)->ListEntry
,
530 &((PCLASS_DEVICE_EXTENSION
)ClassDO
->DeviceExtension
)->ListSpinLock
);
531 if (ClassDO
->StackSize
<= PortDO
->StackSize
)
533 /* Increase the stack size, in case we have to
534 * forward some IRPs to the port device object
536 ClassDO
->StackSize
= PortDO
->StackSize
+ 1;
540 return IoStatus
.Status
;
543 /* Send IOCTL_INTERNAL_*_DISCONNECT to port + destroy the Port DO */
546 IN PDEVICE_OBJECT PortDO
)
548 PPORT_DEVICE_EXTENSION DeviceExtension
;
549 PCLASS_DEVICE_EXTENSION ClassDeviceExtension
;
550 PCLASS_DRIVER_EXTENSION DriverExtension
;
553 IO_STATUS_BLOCK IoStatus
;
557 TRACE_(CLASS_NAME
, "Destroying PortDO %p\n", PortDO
);
559 DeviceExtension
= (PPORT_DEVICE_EXTENSION
)PortDO
->DeviceExtension
;
560 ClassDeviceExtension
= DeviceExtension
->ClassDO
->DeviceExtension
;
561 DriverExtension
= IoGetDriverObjectExtension(PortDO
->DriverObject
, PortDO
->DriverObject
);
563 /* Send IOCTL_INTERNAL_*_DISCONNECT */
564 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
565 Irp
= IoBuildDeviceIoControlRequest(
566 IOCTL_INTERNAL_MOUSE_DISCONNECT
,
570 TRUE
, &Event
, &IoStatus
);
573 Status
= IoCallDriver(PortDO
, Irp
);
574 if (Status
== STATUS_PENDING
)
575 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
578 /* Remove from ClassDeviceExtension->ListHead list */
579 KeAcquireSpinLock(&ClassDeviceExtension
->ListSpinLock
, &OldIrql
);
580 RemoveHeadList(DeviceExtension
->ListEntry
.Blink
);
581 KeReleaseSpinLock(&ClassDeviceExtension
->ListSpinLock
, OldIrql
);
583 /* Remove entry from HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\[DeviceBaseName] */
584 RtlDeleteRegistryValue(
585 RTL_REGISTRY_DEVICEMAP
,
586 DriverExtension
->DeviceBaseName
.Buffer
,
587 ClassDeviceExtension
->DeviceName
);
589 if (DeviceExtension
->LowerDevice
)
590 IoDetachDevice(DeviceExtension
->LowerDevice
);
591 ObDereferenceObject(PortDO
);
593 if (!DriverExtension
->ConnectMultiplePorts
&& DeviceExtension
->ClassDO
)
595 ExFreePoolWithTag(ClassDeviceExtension
->PortData
, CLASS_TAG
);
596 ExFreePoolWithTag((PVOID
)ClassDeviceExtension
->DeviceName
, CLASS_TAG
);
597 IoDeleteDevice(DeviceExtension
->ClassDO
);
600 IoDeleteDevice(PortDO
);
603 static NTSTATUS NTAPI
605 IN PDRIVER_OBJECT DriverObject
,
606 IN PDEVICE_OBJECT Pdo
)
608 PCLASS_DRIVER_EXTENSION DriverExtension
;
609 PDEVICE_OBJECT Fdo
= NULL
;
610 PPORT_DEVICE_EXTENSION DeviceExtension
= NULL
;
613 TRACE_(CLASS_NAME
, "ClassAddDevice called. Pdo = 0x%p\n", Pdo
);
615 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
618 /* We may get a NULL Pdo at the first call as we're a legacy driver. Ignore it */
619 return STATUS_SUCCESS
;
621 /* Create new device object */
622 Status
= IoCreateDevice(
624 sizeof(PORT_DEVICE_EXTENSION
),
627 Pdo
->Characteristics
& FILE_DEVICE_SECURE_OPEN
? FILE_DEVICE_SECURE_OPEN
: 0,
630 if (!NT_SUCCESS(Status
))
632 WARN_(CLASS_NAME
, "IoCreateDevice() failed with status 0x%08lx\n", Status
);
635 IoSetStartIoAttributes(Fdo
, TRUE
, TRUE
);
637 DeviceExtension
= (PPORT_DEVICE_EXTENSION
)Fdo
->DeviceExtension
;
638 RtlZeroMemory(DeviceExtension
, sizeof(PORT_DEVICE_EXTENSION
));
639 DeviceExtension
->Common
.IsClassDO
= FALSE
;
640 DeviceExtension
->DeviceObject
= Fdo
;
641 DeviceExtension
->PnpState
= dsStopped
;
642 Status
= IoAttachDeviceToDeviceStackSafe(Fdo
, Pdo
, &DeviceExtension
->LowerDevice
);
643 if (!NT_SUCCESS(Status
))
645 WARN_(CLASS_NAME
, "IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status
);
648 if (DeviceExtension
->LowerDevice
->Flags
& DO_POWER_PAGABLE
)
649 Fdo
->Flags
|= DO_POWER_PAGABLE
;
650 if (DeviceExtension
->LowerDevice
->Flags
& DO_BUFFERED_IO
)
651 Fdo
->Flags
|= DO_BUFFERED_IO
;
652 if (DeviceExtension
->LowerDevice
->Flags
& DO_DIRECT_IO
)
653 Fdo
->Flags
|= DO_DIRECT_IO
;
655 if (DriverExtension
->ConnectMultiplePorts
)
656 DeviceExtension
->ClassDO
= DriverExtension
->MainClassDeviceObject
;
659 /* We need a new class device object for this Fdo */
660 Status
= CreateClassDeviceObject(
662 &DeviceExtension
->ClassDO
);
663 if (!NT_SUCCESS(Status
))
665 WARN_(CLASS_NAME
, "CreateClassDeviceObject() failed with status 0x%08lx\n", Status
);
669 Status
= ConnectPortDriver(Fdo
, DeviceExtension
->ClassDO
);
670 if (!NT_SUCCESS(Status
))
672 WARN_(CLASS_NAME
, "ConnectPortDriver() failed with status 0x%08lx\n", Status
);
675 Fdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
677 /* Register interface ; ignore the error (if any) as having
678 * a registred interface is not so important... */
679 Status
= IoRegisterDeviceInterface(
681 &GUID_DEVINTERFACE_MOUSE
,
683 &DeviceExtension
->InterfaceName
);
684 if (!NT_SUCCESS(Status
))
685 DeviceExtension
->InterfaceName
.Length
= 0;
687 return STATUS_SUCCESS
;
691 DestroyPortDriver(Fdo
);
697 IN PDEVICE_OBJECT DeviceObject
,
700 PCLASS_DEVICE_EXTENSION ClassDeviceExtension
= DeviceObject
->DeviceExtension
;
702 BOOLEAN wasQueued
= FALSE
;
704 TRACE_(CLASS_NAME
, "ClassCancelRoutine(DeviceObject %p, Irp %p)\n", DeviceObject
, Irp
);
706 ASSERT(ClassDeviceExtension
->Common
.IsClassDO
);
708 IoReleaseCancelSpinLock(Irp
->CancelIrql
);
710 KeAcquireSpinLock(&ClassDeviceExtension
->SpinLock
, &OldIrql
);
712 if (ClassDeviceExtension
->PendingIrp
== Irp
)
714 ClassDeviceExtension
->PendingIrp
= NULL
;
717 KeReleaseSpinLock(&ClassDeviceExtension
->SpinLock
, OldIrql
);
721 Irp
->IoStatus
.Status
= STATUS_CANCELLED
;
722 Irp
->IoStatus
.Information
= 0;
723 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
727 /* Hm, this shouldn't happen */
734 IN PDEVICE_OBJECT DeviceObject
,
738 PCLASS_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
741 TRACE_(CLASS_NAME
, "HandleReadIrp(DeviceObject %p, Irp %p)\n", DeviceObject
, Irp
);
743 ASSERT(DeviceExtension
->Common
.IsClassDO
);
745 if (DeviceExtension
->InputCount
> 0)
747 SIZE_T NumberOfEntries
;
749 NumberOfEntries
= MIN(
750 DeviceExtension
->InputCount
,
751 IoGetCurrentIrpStackLocation(Irp
)->Parameters
.Read
.Length
/ sizeof(MOUSE_INPUT_DATA
));
753 Status
= FillEntries(
756 DeviceExtension
->PortData
,
759 if (NT_SUCCESS(Status
))
761 if (DeviceExtension
->InputCount
> NumberOfEntries
)
764 &DeviceExtension
->PortData
[0],
765 &DeviceExtension
->PortData
[NumberOfEntries
],
766 (DeviceExtension
->InputCount
- NumberOfEntries
) * sizeof(MOUSE_INPUT_DATA
));
769 DeviceExtension
->InputCount
-= NumberOfEntries
;
771 Irp
->IoStatus
.Information
= NumberOfEntries
* sizeof(MOUSE_INPUT_DATA
);
774 /* Go to next packet and complete this request */
775 Irp
->IoStatus
.Status
= Status
;
777 (VOID
)IoSetCancelRoutine(Irp
, NULL
);
778 IoCompleteRequest(Irp
, IO_MOUSE_INCREMENT
);
779 DeviceExtension
->PendingIrp
= NULL
;
783 (VOID
)IoSetCancelRoutine(Irp
, ClassCancelRoutine
);
784 if (Irp
->Cancel
&& IoSetCancelRoutine(Irp
, NULL
))
786 DeviceExtension
->PendingIrp
= NULL
;
787 Status
= STATUS_CANCELLED
;
791 IoMarkIrpPending(Irp
);
792 DeviceExtension
->PendingIrp
= Irp
;
793 Status
= STATUS_PENDING
;
801 IN PDEVICE_OBJECT DeviceObject
,
804 PCLASS_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
807 TRACE_(CLASS_NAME
, "ClassStartIo(DeviceObject %p, Irp %p)\n", DeviceObject
, Irp
);
809 ASSERT(DeviceExtension
->Common
.IsClassDO
);
811 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &OldIrql
);
812 HandleReadIrp(DeviceObject
, Irp
, TRUE
);
813 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, OldIrql
);
817 SearchForLegacyDrivers(
818 IN PDRIVER_OBJECT DriverObject
,
819 IN PVOID Context
, /* PCLASS_DRIVER_EXTENSION */
822 UNICODE_STRING DeviceMapKeyU
= RTL_CONSTANT_STRING(L
"\\REGISTRY\\MACHINE\\HARDWARE\\DEVICEMAP");
823 PCLASS_DRIVER_EXTENSION DriverExtension
;
824 UNICODE_STRING PortBaseName
= {0, };
825 PKEY_VALUE_BASIC_INFORMATION KeyValueInformation
= NULL
;
826 OBJECT_ATTRIBUTES ObjectAttributes
;
827 HANDLE hDeviceMapKey
= (HANDLE
)-1;
828 HANDLE hPortKey
= (HANDLE
)-1;
830 ULONG Size
, ResultLength
;
833 TRACE_(CLASS_NAME
, "SearchForLegacyDrivers(%p %p %lu)\n",
834 DriverObject
, Context
, Count
);
838 DriverExtension
= (PCLASS_DRIVER_EXTENSION
)Context
;
840 /* Create port base name, by replacing Class by Port at the end of the class base name */
841 Status
= DuplicateUnicodeString(
842 RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
,
843 &DriverExtension
->DeviceBaseName
,
845 if (!NT_SUCCESS(Status
))
847 WARN_(CLASS_NAME
, "DuplicateUnicodeString() failed with status 0x%08lx\n", Status
);
850 PortBaseName
.Length
-= (sizeof(L
"Class") - sizeof(UNICODE_NULL
));
851 RtlAppendUnicodeToString(&PortBaseName
, L
"Port");
853 /* Allocate memory */
854 Size
= sizeof(KEY_VALUE_BASIC_INFORMATION
) + MAX_PATH
;
855 KeyValueInformation
= ExAllocatePoolWithTag(PagedPool
, Size
, CLASS_TAG
);
856 if (!KeyValueInformation
)
858 WARN_(CLASS_NAME
, "ExAllocatePoolWithTag() failed\n");
859 Status
= STATUS_NO_MEMORY
;
863 /* Open HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP */
864 InitializeObjectAttributes(&ObjectAttributes
, &DeviceMapKeyU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
865 Status
= ZwOpenKey(&hDeviceMapKey
, 0, &ObjectAttributes
);
866 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
868 INFO_(CLASS_NAME
, "HKLM\\HARDWARE\\DEVICEMAP is non-existent\n");
869 Status
= STATUS_SUCCESS
;
872 else if (!NT_SUCCESS(Status
))
874 WARN_(CLASS_NAME
, "ZwOpenKey() failed with status 0x%08lx\n", Status
);
879 InitializeObjectAttributes(&ObjectAttributes
, &PortBaseName
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hDeviceMapKey
, NULL
);
880 Status
= ZwOpenKey(&hPortKey
, KEY_QUERY_VALUE
, &ObjectAttributes
);
881 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
883 INFO_(CLASS_NAME
, "HKLM\\HARDWARE\\DEVICEMAP\\%wZ is non-existent\n", &PortBaseName
);
884 Status
= STATUS_SUCCESS
;
887 else if (!NT_SUCCESS(Status
))
889 WARN_(CLASS_NAME
, "ZwOpenKey() failed with status 0x%08lx\n", Status
);
893 /* Read each value name */
894 while (ZwEnumerateValueKey(hPortKey
, Index
++, KeyValueBasicInformation
, KeyValueInformation
, Size
, &ResultLength
) == STATUS_SUCCESS
)
896 UNICODE_STRING PortName
;
897 PDEVICE_OBJECT PortDeviceObject
= NULL
;
898 PFILE_OBJECT FileObject
= NULL
;
900 PortName
.Length
= PortName
.MaximumLength
= (USHORT
)KeyValueInformation
->NameLength
;
901 PortName
.Buffer
= KeyValueInformation
->Name
;
903 /* Open the device object pointer */
904 Status
= IoGetDeviceObjectPointer(&PortName
, FILE_READ_ATTRIBUTES
, &FileObject
, &PortDeviceObject
);
905 if (!NT_SUCCESS(Status
))
907 WARN_(CLASS_NAME
, "IoGetDeviceObjectPointer(%wZ) failed with status 0x%08lx\n", &PortName
, Status
);
910 INFO_(CLASS_NAME
, "Legacy driver found\n");
912 Status
= ClassAddDevice(DriverObject
, PortDeviceObject
);
913 if (!NT_SUCCESS(Status
))
915 /* FIXME: Log the error */
916 WARN_(CLASS_NAME
, "ClassAddDevice() failed with status 0x%08lx\n", Status
);
921 if (KeyValueInformation
!= NULL
)
922 ExFreePoolWithTag(KeyValueInformation
, CLASS_TAG
);
923 if (hDeviceMapKey
!= (HANDLE
)-1)
924 ZwClose(hDeviceMapKey
);
925 if (hPortKey
!= (HANDLE
)-1)
930 * Standard DriverEntry method.
934 IN PDRIVER_OBJECT DriverObject
,
935 IN PUNICODE_STRING RegistryPath
)
937 PCLASS_DRIVER_EXTENSION DriverExtension
;
941 Status
= IoAllocateDriverObjectExtension(
944 sizeof(CLASS_DRIVER_EXTENSION
),
945 (PVOID
*)&DriverExtension
);
946 if (!NT_SUCCESS(Status
))
948 WARN_(CLASS_NAME
, "IoAllocateDriverObjectExtension() failed with status 0x%08lx\n", Status
);
951 RtlZeroMemory(DriverExtension
, sizeof(CLASS_DRIVER_EXTENSION
));
953 Status
= DuplicateUnicodeString(
954 RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
,
956 &DriverExtension
->RegistryPath
);
957 if (!NT_SUCCESS(Status
))
959 WARN_(CLASS_NAME
, "DuplicateUnicodeString() failed with status 0x%08lx\n", Status
);
963 Status
= ReadRegistryEntries(RegistryPath
, DriverExtension
);
964 if (!NT_SUCCESS(Status
))
966 WARN_(CLASS_NAME
, "ReadRegistryEntries() failed with status 0x%08lx\n", Status
);
970 if (DriverExtension
->ConnectMultiplePorts
== 1)
972 Status
= CreateClassDeviceObject(
974 &DriverExtension
->MainClassDeviceObject
);
975 if (!NT_SUCCESS(Status
))
977 WARN_(CLASS_NAME
, "CreateClassDeviceObject() failed with status 0x%08lx\n", Status
);
982 DriverObject
->DriverExtension
->AddDevice
= ClassAddDevice
;
983 DriverObject
->DriverUnload
= DriverUnload
;
985 for (i
= 0; i
<= IRP_MJ_MAXIMUM_FUNCTION
; i
++)
986 DriverObject
->MajorFunction
[i
] = IrpStub
;
988 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = ClassCreate
;
989 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = ClassClose
;
990 DriverObject
->MajorFunction
[IRP_MJ_CLEANUP
] = ClassCleanup
;
991 DriverObject
->MajorFunction
[IRP_MJ_READ
] = ClassRead
;
992 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = ClassDeviceControl
;
993 DriverObject
->MajorFunction
[IRP_MJ_INTERNAL_DEVICE_CONTROL
] = ForwardIrpAndForget
;
994 DriverObject
->DriverStartIo
= ClassStartIo
;
996 /* We will detect the legacy devices later */
997 IoRegisterDriverReinitialization(
999 SearchForLegacyDrivers
,
1002 return STATUS_SUCCESS
;