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)
14 #include <pseh/pseh2.h>
17 static DRIVER_UNLOAD DriverUnload
;
18 static DRIVER_DISPATCH ClassCreate
;
19 static DRIVER_DISPATCH ClassClose
;
20 static DRIVER_DISPATCH ClassCleanup
;
21 static DRIVER_DISPATCH ClassRead
;
22 static DRIVER_DISPATCH ClassDeviceControl
;
23 static DRIVER_DISPATCH IrpStub
;
24 static DRIVER_ADD_DEVICE ClassAddDevice
;
25 static DRIVER_STARTIO ClassStartIo
;
26 static DRIVER_CANCEL ClassCancelRoutine
;
29 IN PDEVICE_OBJECT DeviceObject
,
34 DriverUnload(IN PDRIVER_OBJECT DriverObject
)
36 // nothing to do here yet
41 IN PDEVICE_OBJECT DeviceObject
,
44 TRACE_(CLASS_NAME
, "IRP_MJ_CREATE\n");
46 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
47 return ForwardIrpAndForget(DeviceObject
, Irp
);
49 /* FIXME: open all associated Port devices */
50 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
51 Irp
->IoStatus
.Information
= 0;
52 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
53 return STATUS_SUCCESS
;
58 IN PDEVICE_OBJECT DeviceObject
,
61 TRACE_(CLASS_NAME
, "IRP_MJ_CLOSE\n");
63 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
64 return ForwardIrpAndForget(DeviceObject
, Irp
);
66 /* FIXME: close all associated Port devices */
67 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
68 Irp
->IoStatus
.Information
= 0;
69 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
70 return STATUS_SUCCESS
;
75 IN PDEVICE_OBJECT DeviceObject
,
78 TRACE_(CLASS_NAME
, "IRP_MJ_CLEANUP\n");
80 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
81 return ForwardIrpAndForget(DeviceObject
, Irp
);
83 /* FIXME: cleanup all associated Port devices */
84 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
85 Irp
->IoStatus
.Information
= 0;
86 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
87 return STATUS_SUCCESS
;
92 IN PDEVICE_OBJECT DeviceObject
,
95 PCLASS_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
99 TRACE_(CLASS_NAME
, "IRP_MJ_READ\n");
101 ASSERT(DeviceExtension
->Common
.IsClassDO
);
103 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
104 return ForwardIrpAndForget(DeviceObject
, Irp
);
106 if (IoGetCurrentIrpStackLocation(Irp
)->Parameters
.Read
.Length
< sizeof(MOUSE_INPUT_DATA
))
108 Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
;
109 Irp
->IoStatus
.Information
= 0;
110 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
112 return STATUS_BUFFER_TOO_SMALL
;
115 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &OldIrql
);
116 Status
= HandleReadIrp(DeviceObject
, Irp
, FALSE
);
117 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, OldIrql
);
121 static NTSTATUS NTAPI
123 IN PDEVICE_OBJECT DeviceObject
,
126 //PCLASS_DEVICE_EXTENSION DeviceExtension;
127 NTSTATUS Status
= STATUS_NOT_SUPPORTED
;
129 TRACE_(CLASS_NAME
, "IRP_MJ_DEVICE_CONTROL\n");
131 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
132 return ForwardIrpAndForget(DeviceObject
, Irp
);
134 //DeviceExtension = (PCLASS_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
136 switch (IoGetCurrentIrpStackLocation(Irp
)->Parameters
.DeviceIoControl
.IoControlCode
)
138 case IOCTL_MOUSE_QUERY_ATTRIBUTES
:
140 /* FIXME: We hope that all devices will return the same result.
141 * Ask only the first one */
142 PLIST_ENTRY Head
= &((PCLASS_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->ListHead
;
143 if (Head
->Flink
!= Head
)
145 /* We have at least one device */
146 PPORT_DEVICE_EXTENSION DevExt
= CONTAINING_RECORD(Head
->Flink
, PORT_DEVICE_EXTENSION
, ListEntry
);
147 IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
= IRP_MJ_INTERNAL_DEVICE_CONTROL
;
148 IoSkipCurrentIrpStackLocation(Irp
);
149 return IoCallDriver(DevExt
->DeviceObject
, Irp
);
154 WARN_(CLASS_NAME
, "IRP_MJ_DEVICE_CONTROL / unknown I/O control code 0x%lx\n",
155 IoGetCurrentIrpStackLocation(Irp
)->Parameters
.DeviceIoControl
.IoControlCode
);
160 Irp
->IoStatus
.Status
= Status
;
161 Irp
->IoStatus
.Information
= 0;
162 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
167 static NTSTATUS NTAPI
169 IN PDEVICE_OBJECT DeviceObject
,
172 NTSTATUS Status
= STATUS_NOT_SUPPORTED
;
174 if (!((PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->IsClassDO
)
176 /* Forward some IRPs to lower device */
177 switch (IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
)
180 case IRP_MJ_INTERNAL_DEVICE_CONTROL
:
181 return ForwardIrpAndForget(DeviceObject
, Irp
);
184 ERR_(CLASS_NAME
, "Port DO stub for major function 0x%lx\n",
185 IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
);
192 ERR_(CLASS_NAME
, "Class DO stub for major function 0x%lx\n",
193 IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
);
197 Irp
->IoStatus
.Status
= Status
;
198 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
204 IN PUNICODE_STRING RegistryPath
,
205 IN PCLASS_DRIVER_EXTENSION DriverExtension
)
207 UNICODE_STRING ParametersRegistryKey
;
208 RTL_QUERY_REGISTRY_TABLE Parameters
[4];
211 ULONG DefaultConnectMultiplePorts
= 1;
212 ULONG DefaultDataQueueSize
= 0x64;
213 PCWSTR DefaultDeviceBaseName
= L
"PointerClass";
215 ParametersRegistryKey
.Length
= 0;
216 ParametersRegistryKey
.MaximumLength
= RegistryPath
->Length
+ sizeof(L
"\\Parameters") + sizeof(UNICODE_NULL
);
217 ParametersRegistryKey
.Buffer
= ExAllocatePoolWithTag(PagedPool
, ParametersRegistryKey
.MaximumLength
, CLASS_TAG
);
218 if (!ParametersRegistryKey
.Buffer
)
220 WARN_(CLASS_NAME
, "ExAllocatePoolWithTag() failed\n");
221 return STATUS_NO_MEMORY
;
223 RtlCopyUnicodeString(&ParametersRegistryKey
, RegistryPath
);
224 RtlAppendUnicodeToString(&ParametersRegistryKey
, L
"\\Parameters");
225 ParametersRegistryKey
.Buffer
[ParametersRegistryKey
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
227 RtlZeroMemory(Parameters
, sizeof(Parameters
));
229 Parameters
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_REGISTRY_OPTIONAL
;
230 Parameters
[0].Name
= L
"ConnectMultiplePorts";
231 Parameters
[0].EntryContext
= &DriverExtension
->ConnectMultiplePorts
;
232 Parameters
[0].DefaultType
= REG_DWORD
;
233 Parameters
[0].DefaultData
= &DefaultConnectMultiplePorts
;
234 Parameters
[0].DefaultLength
= sizeof(ULONG
);
236 Parameters
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_REGISTRY_OPTIONAL
;
237 Parameters
[1].Name
= L
"MouseDataQueueSize";
238 Parameters
[1].EntryContext
= &DriverExtension
->DataQueueSize
;
239 Parameters
[1].DefaultType
= REG_DWORD
;
240 Parameters
[1].DefaultData
= &DefaultDataQueueSize
;
241 Parameters
[1].DefaultLength
= sizeof(ULONG
);
243 Parameters
[2].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_REGISTRY_OPTIONAL
;
244 Parameters
[2].Name
= L
"PointerDeviceBaseName";
245 Parameters
[2].EntryContext
= &DriverExtension
->DeviceBaseName
;
246 Parameters
[2].DefaultType
= REG_SZ
;
247 Parameters
[2].DefaultData
= (PVOID
)DefaultDeviceBaseName
;
248 Parameters
[2].DefaultLength
= 0;
250 Status
= RtlQueryRegistryValues(
251 RTL_REGISTRY_ABSOLUTE
,
252 ParametersRegistryKey
.Buffer
,
257 if (NT_SUCCESS(Status
))
260 if (DriverExtension
->ConnectMultiplePorts
!= 0
261 && DriverExtension
->ConnectMultiplePorts
!= 1)
263 DriverExtension
->ConnectMultiplePorts
= DefaultConnectMultiplePorts
;
265 if (DriverExtension
->DataQueueSize
== 0)
267 DriverExtension
->DataQueueSize
= DefaultDataQueueSize
;
270 else if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
272 /* Registry path doesn't exist. Set defaults */
273 DriverExtension
->ConnectMultiplePorts
= DefaultConnectMultiplePorts
;
274 DriverExtension
->DataQueueSize
= DefaultDataQueueSize
;
275 if (RtlCreateUnicodeString(&DriverExtension
->DeviceBaseName
, DefaultDeviceBaseName
))
276 Status
= STATUS_SUCCESS
;
278 Status
= STATUS_NO_MEMORY
;
281 ExFreePoolWithTag(ParametersRegistryKey
.Buffer
, CLASS_TAG
);
286 CreateClassDeviceObject(
287 IN PDRIVER_OBJECT DriverObject
,
288 OUT PDEVICE_OBJECT
*ClassDO OPTIONAL
)
290 PCLASS_DRIVER_EXTENSION DriverExtension
;
293 UNICODE_STRING DeviceNameU
;
294 PWSTR DeviceIdW
= NULL
; /* Pointer into DeviceNameU.Buffer */
296 PCLASS_DEVICE_EXTENSION DeviceExtension
;
299 TRACE_(CLASS_NAME
, "CreateClassDeviceObject(0x%p)\n", DriverObject
);
301 /* Create new device object */
302 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
303 DeviceNameU
.Length
= 0;
304 DeviceNameU
.MaximumLength
=
305 wcslen(L
"\\Device\\") * sizeof(WCHAR
) /* "\Device\" */
306 + DriverExtension
->DeviceBaseName
.Length
/* "PointerClass" */
307 + 4 * sizeof(WCHAR
) /* Id between 0 and 9999 */
308 + sizeof(UNICODE_NULL
); /* Final NULL char */
309 DeviceNameU
.Buffer
= ExAllocatePoolWithTag(PagedPool
, DeviceNameU
.MaximumLength
, CLASS_TAG
);
310 if (!DeviceNameU
.Buffer
)
312 WARN_(CLASS_NAME
, "ExAllocatePoolWithTag() failed\n");
313 return STATUS_NO_MEMORY
;
315 Status
= RtlAppendUnicodeToString(&DeviceNameU
, L
"\\Device\\");
316 if (!NT_SUCCESS(Status
))
318 WARN_(CLASS_NAME
, "RtlAppendUnicodeToString() failed with status 0x%08lx\n", Status
);
321 Status
= RtlAppendUnicodeStringToString(&DeviceNameU
, &DriverExtension
->DeviceBaseName
);
322 if (!NT_SUCCESS(Status
))
324 WARN_(CLASS_NAME
, "RtlAppendUnicodeStringToString() failed with status 0x%08lx\n", Status
);
327 PrefixLength
= DeviceNameU
.MaximumLength
- 4 * sizeof(WCHAR
) - sizeof(UNICODE_NULL
);
328 DeviceIdW
= &DeviceNameU
.Buffer
[PrefixLength
/ sizeof(WCHAR
)];
329 while (DeviceId
< 9999)
331 DeviceNameU
.Length
= (USHORT
)(PrefixLength
+ swprintf(DeviceIdW
, L
"%lu", DeviceId
) * sizeof(WCHAR
));
332 Status
= IoCreateDevice(
334 sizeof(CLASS_DEVICE_EXTENSION
),
337 FILE_DEVICE_SECURE_OPEN
,
340 if (NT_SUCCESS(Status
))
342 else if (Status
!= STATUS_OBJECT_NAME_COLLISION
)
344 WARN_(CLASS_NAME
, "IoCreateDevice() failed with status 0x%08lx\n", Status
);
349 WARN_(CLASS_NAME
, "Too many devices starting with '\\Device\\%wZ'\n", &DriverExtension
->DeviceBaseName
);
350 Status
= STATUS_TOO_MANY_NAMES
;
352 if (!NT_SUCCESS(Status
))
354 ExFreePoolWithTag(DeviceNameU
.Buffer
, CLASS_TAG
);
358 DeviceExtension
= (PCLASS_DEVICE_EXTENSION
)Fdo
->DeviceExtension
;
359 RtlZeroMemory(DeviceExtension
, sizeof(CLASS_DEVICE_EXTENSION
));
360 DeviceExtension
->Common
.IsClassDO
= TRUE
;
361 DeviceExtension
->DriverExtension
= DriverExtension
;
362 InitializeListHead(&DeviceExtension
->ListHead
);
363 KeInitializeSpinLock(&DeviceExtension
->ListSpinLock
);
364 KeInitializeSpinLock(&DeviceExtension
->SpinLock
);
365 DeviceExtension
->InputCount
= 0;
366 DeviceExtension
->PortData
= ExAllocatePoolWithTag(NonPagedPool
, DeviceExtension
->DriverExtension
->DataQueueSize
* sizeof(MOUSE_INPUT_DATA
), CLASS_TAG
);
367 if (!DeviceExtension
->PortData
)
369 ExFreePoolWithTag(DeviceNameU
.Buffer
, CLASS_TAG
);
370 return STATUS_NO_MEMORY
;
372 DeviceExtension
->DeviceName
= DeviceNameU
.Buffer
;
373 Fdo
->Flags
|= DO_POWER_PAGABLE
;
374 Fdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
375 Fdo
->Flags
|= DO_BUFFERED_IO
;
377 /* Add entry entry to HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\[DeviceBaseName] */
378 RtlWriteRegistryValue(
379 RTL_REGISTRY_DEVICEMAP
,
380 DriverExtension
->DeviceBaseName
.Buffer
,
381 DeviceExtension
->DeviceName
,
383 DriverExtension
->RegistryPath
.Buffer
,
384 DriverExtension
->RegistryPath
.MaximumLength
);
389 return STATUS_SUCCESS
;
394 IN PDEVICE_OBJECT ClassDeviceObject
,
396 IN PMOUSE_INPUT_DATA DataStart
,
397 IN SIZE_T NumberOfEntries
)
399 NTSTATUS Status
= STATUS_SUCCESS
;
401 if (ClassDeviceObject
->Flags
& DO_BUFFERED_IO
)
404 Irp
->AssociatedIrp
.SystemBuffer
,
406 NumberOfEntries
* sizeof(MOUSE_INPUT_DATA
));
408 else if (ClassDeviceObject
->Flags
& DO_DIRECT_IO
)
410 PVOID DestAddress
= MmGetSystemAddressForMdlSafe(Irp
->MdlAddress
, NormalPagePriority
);
416 NumberOfEntries
* sizeof(MOUSE_INPUT_DATA
));
419 Status
= STATUS_UNSUCCESSFUL
;
428 NumberOfEntries
* sizeof(MOUSE_INPUT_DATA
));
430 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
432 Status
= _SEH2_GetExceptionCode();
442 IN PDEVICE_OBJECT ClassDeviceObject
,
443 IN OUT PMOUSE_INPUT_DATA DataStart
,
444 IN PMOUSE_INPUT_DATA DataEnd
,
445 IN OUT PULONG ConsumedCount
)
447 PCLASS_DEVICE_EXTENSION ClassDeviceExtension
= ClassDeviceObject
->DeviceExtension
;
449 SIZE_T InputCount
= DataEnd
- DataStart
;
452 TRACE_(CLASS_NAME
, "ClassCallback()\n");
454 ASSERT(ClassDeviceExtension
->Common
.IsClassDO
);
456 KeAcquireSpinLock(&ClassDeviceExtension
->SpinLock
, &OldIrql
);
459 if (ClassDeviceExtension
->InputCount
+ InputCount
> ClassDeviceExtension
->DriverExtension
->DataQueueSize
)
462 * We're exceeding the buffer, and data will be thrown away...
463 * FIXME: What could we do, as we are at DISPATCH_LEVEL?
465 ReadSize
= ClassDeviceExtension
->DriverExtension
->DataQueueSize
- ClassDeviceExtension
->InputCount
;
468 ReadSize
= InputCount
;
471 * Move the input data from the port data queue to our class data
475 &ClassDeviceExtension
->PortData
[ClassDeviceExtension
->InputCount
],
477 sizeof(MOUSE_INPUT_DATA
) * ReadSize
);
479 /* Move the counter up */
480 ClassDeviceExtension
->InputCount
+= ReadSize
;
482 (*ConsumedCount
) += (ULONG
)ReadSize
;
484 /* Complete pending IRP (if any) */
485 if (ClassDeviceExtension
->PendingIrp
)
486 HandleReadIrp(ClassDeviceObject
, ClassDeviceExtension
->PendingIrp
, FALSE
);
488 KeReleaseSpinLock(&ClassDeviceExtension
->SpinLock
, OldIrql
);
490 TRACE_(CLASS_NAME
, "Leaving ClassCallback()\n");
494 /* Send IOCTL_INTERNAL_*_CONNECT to port */
497 IN PDEVICE_OBJECT PortDO
,
498 IN PDEVICE_OBJECT ClassDO
)
502 IO_STATUS_BLOCK IoStatus
;
503 CONNECT_DATA ConnectData
;
506 TRACE_(CLASS_NAME
, "Connecting PortDO %p to ClassDO %p\n", PortDO
, ClassDO
);
508 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
510 ConnectData
.ClassDeviceObject
= ClassDO
;
511 ConnectData
.ClassService
= ClassCallback
;
513 Irp
= IoBuildDeviceIoControlRequest(
514 IOCTL_INTERNAL_MOUSE_CONNECT
,
516 &ConnectData
, sizeof(CONNECT_DATA
),
518 TRUE
, &Event
, &IoStatus
);
520 return STATUS_INSUFFICIENT_RESOURCES
;
522 Status
= IoCallDriver(PortDO
, Irp
);
524 if (Status
== STATUS_PENDING
)
525 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
527 IoStatus
.Status
= Status
;
529 if (NT_SUCCESS(IoStatus
.Status
))
531 ObReferenceObject(PortDO
);
532 ExInterlockedInsertTailList(
533 &((PCLASS_DEVICE_EXTENSION
)ClassDO
->DeviceExtension
)->ListHead
,
534 &((PPORT_DEVICE_EXTENSION
)PortDO
->DeviceExtension
)->ListEntry
,
535 &((PCLASS_DEVICE_EXTENSION
)ClassDO
->DeviceExtension
)->ListSpinLock
);
536 if (ClassDO
->StackSize
<= PortDO
->StackSize
)
538 /* Increase the stack size, in case we have to
539 * forward some IRPs to the port device object
541 ClassDO
->StackSize
= PortDO
->StackSize
+ 1;
545 return IoStatus
.Status
;
548 /* Send IOCTL_INTERNAL_*_DISCONNECT to port + destroy the Port DO */
551 IN PDEVICE_OBJECT PortDO
)
553 PPORT_DEVICE_EXTENSION DeviceExtension
;
554 PCLASS_DEVICE_EXTENSION ClassDeviceExtension
;
555 PCLASS_DRIVER_EXTENSION DriverExtension
;
558 IO_STATUS_BLOCK IoStatus
;
562 TRACE_(CLASS_NAME
, "Destroying PortDO %p\n", PortDO
);
564 DeviceExtension
= (PPORT_DEVICE_EXTENSION
)PortDO
->DeviceExtension
;
565 ClassDeviceExtension
= DeviceExtension
->ClassDO
->DeviceExtension
;
566 DriverExtension
= IoGetDriverObjectExtension(PortDO
->DriverObject
, PortDO
->DriverObject
);
568 /* Send IOCTL_INTERNAL_*_DISCONNECT */
569 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
570 Irp
= IoBuildDeviceIoControlRequest(
571 IOCTL_INTERNAL_MOUSE_DISCONNECT
,
575 TRUE
, &Event
, &IoStatus
);
578 Status
= IoCallDriver(PortDO
, Irp
);
579 if (Status
== STATUS_PENDING
)
580 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
583 /* Remove from ClassDeviceExtension->ListHead list */
584 KeAcquireSpinLock(&ClassDeviceExtension
->ListSpinLock
, &OldIrql
);
585 RemoveEntryList(&DeviceExtension
->ListEntry
);
586 KeReleaseSpinLock(&ClassDeviceExtension
->ListSpinLock
, OldIrql
);
588 /* Remove entry from HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\[DeviceBaseName] */
589 RtlDeleteRegistryValue(
590 RTL_REGISTRY_DEVICEMAP
,
591 DriverExtension
->DeviceBaseName
.Buffer
,
592 ClassDeviceExtension
->DeviceName
);
594 if (DeviceExtension
->LowerDevice
)
595 IoDetachDevice(DeviceExtension
->LowerDevice
);
596 ObDereferenceObject(PortDO
);
598 if (!DriverExtension
->ConnectMultiplePorts
&& DeviceExtension
->ClassDO
)
600 ExFreePoolWithTag(ClassDeviceExtension
->PortData
, CLASS_TAG
);
601 ExFreePoolWithTag((PVOID
)ClassDeviceExtension
->DeviceName
, CLASS_TAG
);
602 IoDeleteDevice(DeviceExtension
->ClassDO
);
605 IoDeleteDevice(PortDO
);
608 static NTSTATUS NTAPI
610 IN PDRIVER_OBJECT DriverObject
,
611 IN PDEVICE_OBJECT Pdo
)
613 PCLASS_DRIVER_EXTENSION DriverExtension
;
614 PDEVICE_OBJECT Fdo
= NULL
;
615 PPORT_DEVICE_EXTENSION DeviceExtension
= NULL
;
618 TRACE_(CLASS_NAME
, "ClassAddDevice called. Pdo = 0x%p\n", Pdo
);
620 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
623 /* We may get a NULL Pdo at the first call as we're a legacy driver. Ignore it */
624 return STATUS_SUCCESS
;
626 /* Create new device object */
627 Status
= IoCreateDevice(
629 sizeof(PORT_DEVICE_EXTENSION
),
632 Pdo
->Characteristics
& FILE_DEVICE_SECURE_OPEN
? FILE_DEVICE_SECURE_OPEN
: 0,
635 if (!NT_SUCCESS(Status
))
637 WARN_(CLASS_NAME
, "IoCreateDevice() failed with status 0x%08lx\n", Status
);
640 IoSetStartIoAttributes(Fdo
, TRUE
, TRUE
);
642 DeviceExtension
= (PPORT_DEVICE_EXTENSION
)Fdo
->DeviceExtension
;
643 RtlZeroMemory(DeviceExtension
, sizeof(PORT_DEVICE_EXTENSION
));
644 DeviceExtension
->Common
.IsClassDO
= FALSE
;
645 DeviceExtension
->DeviceObject
= Fdo
;
646 DeviceExtension
->PnpState
= dsStopped
;
647 Status
= IoAttachDeviceToDeviceStackSafe(Fdo
, Pdo
, &DeviceExtension
->LowerDevice
);
648 if (!NT_SUCCESS(Status
))
650 WARN_(CLASS_NAME
, "IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status
);
653 if (DeviceExtension
->LowerDevice
->Flags
& DO_POWER_PAGABLE
)
654 Fdo
->Flags
|= DO_POWER_PAGABLE
;
655 if (DeviceExtension
->LowerDevice
->Flags
& DO_BUFFERED_IO
)
656 Fdo
->Flags
|= DO_BUFFERED_IO
;
657 if (DeviceExtension
->LowerDevice
->Flags
& DO_DIRECT_IO
)
658 Fdo
->Flags
|= DO_DIRECT_IO
;
660 if (DriverExtension
->ConnectMultiplePorts
)
661 DeviceExtension
->ClassDO
= DriverExtension
->MainClassDeviceObject
;
664 /* We need a new class device object for this Fdo */
665 Status
= CreateClassDeviceObject(
667 &DeviceExtension
->ClassDO
);
668 if (!NT_SUCCESS(Status
))
670 WARN_(CLASS_NAME
, "CreateClassDeviceObject() failed with status 0x%08lx\n", Status
);
674 Status
= ConnectPortDriver(Fdo
, DeviceExtension
->ClassDO
);
675 if (!NT_SUCCESS(Status
))
677 WARN_(CLASS_NAME
, "ConnectPortDriver() failed with status 0x%08lx\n", Status
);
680 Fdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
682 /* Register interface ; ignore the error (if any) as having
683 * a registred interface is not so important... */
684 Status
= IoRegisterDeviceInterface(
686 &GUID_DEVINTERFACE_MOUSE
,
688 &DeviceExtension
->InterfaceName
);
689 if (!NT_SUCCESS(Status
))
690 DeviceExtension
->InterfaceName
.Length
= 0;
692 return STATUS_SUCCESS
;
696 DestroyPortDriver(Fdo
);
702 IN PDEVICE_OBJECT DeviceObject
,
705 PCLASS_DEVICE_EXTENSION ClassDeviceExtension
= DeviceObject
->DeviceExtension
;
707 BOOLEAN wasQueued
= FALSE
;
709 TRACE_(CLASS_NAME
, "ClassCancelRoutine(DeviceObject %p, Irp %p)\n", DeviceObject
, Irp
);
711 ASSERT(ClassDeviceExtension
->Common
.IsClassDO
);
713 IoReleaseCancelSpinLock(Irp
->CancelIrql
);
715 KeAcquireSpinLock(&ClassDeviceExtension
->SpinLock
, &OldIrql
);
717 if (ClassDeviceExtension
->PendingIrp
== Irp
)
719 ClassDeviceExtension
->PendingIrp
= NULL
;
722 KeReleaseSpinLock(&ClassDeviceExtension
->SpinLock
, OldIrql
);
726 Irp
->IoStatus
.Status
= STATUS_CANCELLED
;
727 Irp
->IoStatus
.Information
= 0;
728 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
732 DPRINT1("Cancelled IRP is not pending. Race condition?\n");
738 IN PDEVICE_OBJECT DeviceObject
,
742 PCLASS_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
746 TRACE_(CLASS_NAME
, "HandleReadIrp(DeviceObject %p, Irp %p)\n", DeviceObject
, Irp
);
748 ASSERT(DeviceExtension
->Common
.IsClassDO
);
750 if (DeviceExtension
->InputCount
> 0)
752 SIZE_T NumberOfEntries
;
754 NumberOfEntries
= MIN(
755 DeviceExtension
->InputCount
,
756 IoGetCurrentIrpStackLocation(Irp
)->Parameters
.Read
.Length
/ sizeof(MOUSE_INPUT_DATA
));
758 Status
= FillEntries(
761 DeviceExtension
->PortData
,
764 if (NT_SUCCESS(Status
))
766 if (DeviceExtension
->InputCount
> NumberOfEntries
)
769 &DeviceExtension
->PortData
[0],
770 &DeviceExtension
->PortData
[NumberOfEntries
],
771 (DeviceExtension
->InputCount
- NumberOfEntries
) * sizeof(MOUSE_INPUT_DATA
));
774 DeviceExtension
->InputCount
-= NumberOfEntries
;
776 Irp
->IoStatus
.Information
= NumberOfEntries
* sizeof(MOUSE_INPUT_DATA
);
779 /* Go to next packet and complete this request */
780 Irp
->IoStatus
.Status
= Status
;
782 (VOID
)IoSetCancelRoutine(Irp
, NULL
);
783 IoCompleteRequest(Irp
, IO_MOUSE_INCREMENT
);
784 DeviceExtension
->PendingIrp
= NULL
;
788 IoAcquireCancelSpinLock(&OldIrql
);
791 DeviceExtension
->PendingIrp
= NULL
;
792 Status
= STATUS_CANCELLED
;
796 IoMarkIrpPending(Irp
);
797 DeviceExtension
->PendingIrp
= Irp
;
798 (VOID
)IoSetCancelRoutine(Irp
, ClassCancelRoutine
);
799 Status
= STATUS_PENDING
;
801 IoReleaseCancelSpinLock(OldIrql
);
806 static NTSTATUS NTAPI
808 IN PDEVICE_OBJECT DeviceObject
,
811 PPORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
812 PIO_STACK_LOCATION IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
813 OBJECT_ATTRIBUTES ObjectAttributes
;
814 IO_STATUS_BLOCK Iosb
;
817 switch (IrpSp
->MinorFunction
)
819 case IRP_MN_START_DEVICE
:
820 Status
= ForwardIrpAndWait(DeviceObject
, Irp
);
821 if (NT_SUCCESS(Status
))
823 InitializeObjectAttributes(&ObjectAttributes
,
824 &DeviceExtension
->InterfaceName
,
825 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
829 Status
= ZwOpenFile(&DeviceExtension
->FileHandle
,
835 if (!NT_SUCCESS(Status
))
836 DeviceExtension
->FileHandle
= NULL
;
839 DeviceExtension
->FileHandle
= NULL
;
840 Irp
->IoStatus
.Status
= Status
;
841 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
844 case IRP_MN_STOP_DEVICE
:
845 if (DeviceExtension
->FileHandle
)
847 ZwClose(DeviceExtension
->FileHandle
);
848 DeviceExtension
->FileHandle
= NULL
;
850 Status
= STATUS_SUCCESS
;
853 case IRP_MN_REMOVE_DEVICE
:
854 if (DeviceExtension
->FileHandle
)
856 ZwClose(DeviceExtension
->FileHandle
);
857 DeviceExtension
->FileHandle
= NULL
;
859 IoSkipCurrentIrpStackLocation(Irp
);
860 Status
= IoCallDriver(DeviceExtension
->LowerDevice
, Irp
);
861 DestroyPortDriver(DeviceObject
);
865 Status
= Irp
->IoStatus
.Status
;
869 Irp
->IoStatus
.Status
= Status
;
870 if (NT_SUCCESS(Status
) || Status
== STATUS_NOT_SUPPORTED
)
872 IoSkipCurrentIrpStackLocation(Irp
);
873 return IoCallDriver(DeviceExtension
->LowerDevice
, Irp
);
877 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
884 IN PDEVICE_OBJECT DeviceObject
,
887 PCLASS_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
890 TRACE_(CLASS_NAME
, "ClassStartIo(DeviceObject %p, Irp %p)\n", DeviceObject
, Irp
);
892 ASSERT(DeviceExtension
->Common
.IsClassDO
);
894 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &OldIrql
);
895 HandleReadIrp(DeviceObject
, Irp
, TRUE
);
896 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, OldIrql
);
900 SearchForLegacyDrivers(
901 IN PDRIVER_OBJECT DriverObject
,
902 IN PVOID Context
, /* PCLASS_DRIVER_EXTENSION */
905 UNICODE_STRING DeviceMapKeyU
= RTL_CONSTANT_STRING(L
"\\REGISTRY\\MACHINE\\HARDWARE\\DEVICEMAP");
906 PCLASS_DRIVER_EXTENSION DriverExtension
;
907 UNICODE_STRING PortBaseName
= { 0, 0, NULL
};
908 PKEY_VALUE_BASIC_INFORMATION KeyValueInformation
= NULL
;
909 OBJECT_ATTRIBUTES ObjectAttributes
;
910 HANDLE hDeviceMapKey
= (HANDLE
)-1;
911 HANDLE hPortKey
= (HANDLE
)-1;
913 ULONG Size
, ResultLength
;
916 TRACE_(CLASS_NAME
, "SearchForLegacyDrivers(%p %p %lu)\n",
917 DriverObject
, Context
, Count
);
921 DriverExtension
= (PCLASS_DRIVER_EXTENSION
)Context
;
923 /* Create port base name, by replacing Class by Port at the end of the class base name */
924 Status
= DuplicateUnicodeString(
925 RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
,
926 &DriverExtension
->DeviceBaseName
,
928 if (!NT_SUCCESS(Status
))
930 WARN_(CLASS_NAME
, "DuplicateUnicodeString() failed with status 0x%08lx\n", Status
);
933 PortBaseName
.Length
-= (sizeof(L
"Class") - sizeof(UNICODE_NULL
));
934 RtlAppendUnicodeToString(&PortBaseName
, L
"Port");
936 /* Allocate memory */
937 Size
= sizeof(KEY_VALUE_BASIC_INFORMATION
) + MAX_PATH
;
938 KeyValueInformation
= ExAllocatePoolWithTag(PagedPool
, Size
, CLASS_TAG
);
939 if (!KeyValueInformation
)
941 WARN_(CLASS_NAME
, "ExAllocatePoolWithTag() failed\n");
942 Status
= STATUS_NO_MEMORY
;
946 /* Open HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP */
947 InitializeObjectAttributes(&ObjectAttributes
, &DeviceMapKeyU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
948 Status
= ZwOpenKey(&hDeviceMapKey
, 0, &ObjectAttributes
);
949 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
951 INFO_(CLASS_NAME
, "HKLM\\HARDWARE\\DEVICEMAP is non-existent\n");
952 Status
= STATUS_SUCCESS
;
955 else if (!NT_SUCCESS(Status
))
957 WARN_(CLASS_NAME
, "ZwOpenKey() failed with status 0x%08lx\n", Status
);
962 InitializeObjectAttributes(&ObjectAttributes
, &PortBaseName
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hDeviceMapKey
, NULL
);
963 Status
= ZwOpenKey(&hPortKey
, KEY_QUERY_VALUE
, &ObjectAttributes
);
964 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
966 INFO_(CLASS_NAME
, "HKLM\\HARDWARE\\DEVICEMAP\\%wZ is non-existent\n", &PortBaseName
);
967 Status
= STATUS_SUCCESS
;
970 else if (!NT_SUCCESS(Status
))
972 WARN_(CLASS_NAME
, "ZwOpenKey() failed with status 0x%08lx\n", Status
);
976 /* Read each value name */
977 while (ZwEnumerateValueKey(hPortKey
, Index
++, KeyValueBasicInformation
, KeyValueInformation
, Size
, &ResultLength
) == STATUS_SUCCESS
)
979 UNICODE_STRING PortName
;
980 PDEVICE_OBJECT PortDeviceObject
= NULL
;
981 PFILE_OBJECT FileObject
= NULL
;
983 PortName
.Length
= PortName
.MaximumLength
= (USHORT
)KeyValueInformation
->NameLength
;
984 PortName
.Buffer
= KeyValueInformation
->Name
;
986 /* Open the device object pointer */
987 Status
= IoGetDeviceObjectPointer(&PortName
, FILE_READ_ATTRIBUTES
, &FileObject
, &PortDeviceObject
);
988 if (!NT_SUCCESS(Status
))
990 WARN_(CLASS_NAME
, "IoGetDeviceObjectPointer(%wZ) failed with status 0x%08lx\n", &PortName
, Status
);
993 INFO_(CLASS_NAME
, "Legacy driver found\n");
995 Status
= ClassAddDevice(DriverObject
, PortDeviceObject
);
996 if (!NT_SUCCESS(Status
))
998 /* FIXME: Log the error */
999 WARN_(CLASS_NAME
, "ClassAddDevice() failed with status 0x%08lx\n", Status
);
1002 ObDereferenceObject(FileObject
);
1006 if (KeyValueInformation
!= NULL
)
1007 ExFreePoolWithTag(KeyValueInformation
, CLASS_TAG
);
1008 if (hDeviceMapKey
!= (HANDLE
)-1)
1009 ZwClose(hDeviceMapKey
);
1010 if (hPortKey
!= (HANDLE
)-1)
1015 * Standard DriverEntry method.
1019 IN PDRIVER_OBJECT DriverObject
,
1020 IN PUNICODE_STRING RegistryPath
)
1022 PCLASS_DRIVER_EXTENSION DriverExtension
;
1026 Status
= IoAllocateDriverObjectExtension(
1029 sizeof(CLASS_DRIVER_EXTENSION
),
1030 (PVOID
*)&DriverExtension
);
1031 if (!NT_SUCCESS(Status
))
1033 WARN_(CLASS_NAME
, "IoAllocateDriverObjectExtension() failed with status 0x%08lx\n", Status
);
1036 RtlZeroMemory(DriverExtension
, sizeof(CLASS_DRIVER_EXTENSION
));
1038 Status
= DuplicateUnicodeString(
1039 RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
,
1041 &DriverExtension
->RegistryPath
);
1042 if (!NT_SUCCESS(Status
))
1044 WARN_(CLASS_NAME
, "DuplicateUnicodeString() failed with status 0x%08lx\n", Status
);
1048 Status
= ReadRegistryEntries(RegistryPath
, DriverExtension
);
1049 if (!NT_SUCCESS(Status
))
1051 WARN_(CLASS_NAME
, "ReadRegistryEntries() failed with status 0x%08lx\n", Status
);
1055 if (DriverExtension
->ConnectMultiplePorts
== 1)
1057 Status
= CreateClassDeviceObject(
1059 &DriverExtension
->MainClassDeviceObject
);
1060 if (!NT_SUCCESS(Status
))
1062 WARN_(CLASS_NAME
, "CreateClassDeviceObject() failed with status 0x%08lx\n", Status
);
1067 DriverObject
->DriverExtension
->AddDevice
= ClassAddDevice
;
1068 DriverObject
->DriverUnload
= DriverUnload
;
1070 for (i
= 0; i
<= IRP_MJ_MAXIMUM_FUNCTION
; i
++)
1071 DriverObject
->MajorFunction
[i
] = IrpStub
;
1073 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = ClassCreate
;
1074 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = ClassClose
;
1075 DriverObject
->MajorFunction
[IRP_MJ_CLEANUP
] = ClassCleanup
;
1076 DriverObject
->MajorFunction
[IRP_MJ_READ
] = ClassRead
;
1077 DriverObject
->MajorFunction
[IRP_MJ_PNP
] = ClassPnp
;
1078 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = ClassDeviceControl
;
1079 DriverObject
->MajorFunction
[IRP_MJ_INTERNAL_DEVICE_CONTROL
] = ForwardIrpAndForget
;
1080 DriverObject
->DriverStartIo
= ClassStartIo
;
1082 /* We will detect the legacy devices later */
1083 IoRegisterDriverReinitialization(
1085 SearchForLegacyDrivers
,
1088 return STATUS_SUCCESS
;