5211e83972fa2a88a8d5fc916a8833567bb62bcf
[reactos.git] / reactos / drivers / input / kbdclass / kbdclass.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Keyboard class driver
4 * FILE: drivers/kbdclass/kbdclass.c
5 * PURPOSE: Keyboard class driver
6 *
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
8 */
9
10 #define NDEBUG
11 #include <debug.h>
12
13 #define INITGUID
14 #include "kbdclass.h"
15
16 static NTSTATUS
17 SearchForLegacyDrivers(
18 IN PDRIVER_OBJECT DriverObject,
19 IN PCLASS_DRIVER_EXTENSION DriverExtension);
20
21 static VOID NTAPI
22 DriverUnload(IN PDRIVER_OBJECT DriverObject)
23 {
24 // nothing to do here yet
25 }
26
27 static NTSTATUS NTAPI
28 ClassCreate(
29 IN PDEVICE_OBJECT DeviceObject,
30 IN PIRP Irp)
31 {
32 DPRINT("IRP_MJ_CREATE\n");
33
34 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
35 return ForwardIrpAndForget(DeviceObject, Irp);
36
37 /* FIXME: open all associated Port devices */
38 return STATUS_SUCCESS;
39 }
40
41 static NTSTATUS NTAPI
42 ClassClose(
43 IN PDEVICE_OBJECT DeviceObject,
44 IN PIRP Irp)
45 {
46 DPRINT("IRP_MJ_CLOSE\n");
47
48 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
49 return ForwardIrpAndForget(DeviceObject, Irp);
50
51 /* FIXME: close all associated Port devices */
52 return STATUS_SUCCESS;
53 }
54
55 static NTSTATUS NTAPI
56 ClassCleanup(
57 IN PDEVICE_OBJECT DeviceObject,
58 IN PIRP Irp)
59 {
60 DPRINT("IRP_MJ_CLEANUP\n");
61
62 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
63 return ForwardIrpAndForget(DeviceObject, Irp);
64
65 /* FIXME: cleanup all associated Port devices */
66 return STATUS_SUCCESS;
67 }
68
69 static NTSTATUS NTAPI
70 ClassRead(
71 IN PDEVICE_OBJECT DeviceObject,
72 IN PIRP Irp)
73 {
74 DPRINT("IRP_MJ_READ\n");
75
76 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
77 return ForwardIrpAndForget(DeviceObject, Irp);
78
79 if (IoGetCurrentIrpStackLocation(Irp)->Parameters.Read.Length < sizeof(KEYBOARD_INPUT_DATA))
80 {
81 Irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
82 Irp->IoStatus.Information = 0;
83 IoCompleteRequest(Irp, IO_NO_INCREMENT);
84
85 return STATUS_BUFFER_TOO_SMALL;
86 }
87
88 IoMarkIrpPending(Irp);
89 IoStartPacket(DeviceObject, Irp, NULL, NULL);
90 return STATUS_PENDING;
91 }
92
93 static NTSTATUS NTAPI
94 ClassDeviceControl(
95 IN PDEVICE_OBJECT DeviceObject,
96 IN PIRP Irp)
97 {
98 PCLASS_DEVICE_EXTENSION DeviceExtension;
99 NTSTATUS Status;
100
101 DPRINT("IRP_MJ_DEVICE_CONTROL\n");
102
103 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
104 return ForwardIrpAndForget(DeviceObject, Irp);
105
106 DeviceExtension = (PCLASS_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
107
108 switch (IoGetCurrentIrpStackLocation(Irp)->Parameters.DeviceIoControl.IoControlCode)
109 {
110 case IOCTL_KEYBOARD_QUERY_ATTRIBUTES:
111 case IOCTL_KEYBOARD_QUERY_INDICATOR_TRANSLATION:
112 case IOCTL_KEYBOARD_QUERY_INDICATORS:
113 case IOCTL_KEYBOARD_QUERY_TYPEMATIC:
114 case IOCTL_KEYBOARD_SET_INDICATORS:
115 case IOCTL_KEYBOARD_SET_TYPEMATIC: /* not in MSDN, would seem logical */
116 /* FIXME: send it to all associated Port devices */
117 Status = STATUS_NOT_SUPPORTED;
118 break;
119 default:
120 DPRINT1("IRP_MJ_DEVICE_CONTROL / unknown I/O control code 0x%lx\n",
121 IoGetCurrentIrpStackLocation(Irp)->Parameters.DeviceIoControl.IoControlCode);
122 Status = STATUS_NOT_SUPPORTED;
123 }
124
125 Irp->IoStatus.Status = Status;
126 Irp->IoStatus.Information = 0;
127 IoCompleteRequest(Irp, IO_NO_INCREMENT);
128
129 return Status;
130 }
131
132 static NTSTATUS NTAPI
133 IrpStub(
134 IN PDEVICE_OBJECT DeviceObject,
135 IN PIRP Irp)
136 {
137 NTSTATUS Status = STATUS_NOT_SUPPORTED;
138
139 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
140 {
141 /* Forward some IRPs to lower device */
142 switch (IoGetCurrentIrpStackLocation(Irp)->MajorFunction)
143 {
144 case IRP_MJ_INTERNAL_DEVICE_CONTROL:
145 return ForwardIrpAndForget(DeviceObject, Irp);
146 default:
147 {
148 DPRINT1("Port DO stub for major function 0x%lx\n",
149 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
150 ASSERT(FALSE);
151 Status = Irp->IoStatus.Status;
152 }
153 }
154 }
155 else
156 {
157 DPRINT1("Class DO stub for major function 0x%lx\n",
158 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
159 ASSERT(FALSE);
160 Status = Irp->IoStatus.Status;
161 }
162
163 Irp->IoStatus.Status = Status;
164 IoCompleteRequest(Irp, IO_NO_INCREMENT);
165 return Status;
166 }
167
168 static NTSTATUS
169 ReadRegistryEntries(
170 IN PUNICODE_STRING RegistryPath,
171 IN PCLASS_DRIVER_EXTENSION DriverExtension)
172 {
173 UNICODE_STRING ParametersRegistryKey;
174 RTL_QUERY_REGISTRY_TABLE Parameters[4];
175 NTSTATUS Status;
176
177 ULONG DefaultConnectMultiplePorts = 0;
178 ULONG DefaultDataQueueSize = 0x64;
179 UNICODE_STRING DefaultDeviceBaseName = RTL_CONSTANT_STRING(L"KeyboardClass");
180
181 ParametersRegistryKey.Length = 0;
182 ParametersRegistryKey.MaximumLength = RegistryPath->Length + sizeof(L"\\Parameters") + sizeof(UNICODE_NULL);
183 ParametersRegistryKey.Buffer = ExAllocatePool(PagedPool, ParametersRegistryKey.MaximumLength);
184 if (!ParametersRegistryKey.Buffer)
185 {
186 DPRINT("ExAllocatePool() failed\n");
187 return STATUS_INSUFFICIENT_RESOURCES;
188 }
189 RtlCopyUnicodeString(&ParametersRegistryKey, RegistryPath);
190 RtlAppendUnicodeToString(&ParametersRegistryKey, L"\\Parameters");
191 ParametersRegistryKey.Buffer[ParametersRegistryKey.Length / sizeof(WCHAR)] = UNICODE_NULL;
192
193 RtlZeroMemory(Parameters, sizeof(Parameters));
194
195 Parameters[0].Flags = RTL_QUERY_REGISTRY_DIRECT | RTL_REGISTRY_OPTIONAL;
196 Parameters[0].Name = L"ConnectMultiplePorts";
197 Parameters[0].EntryContext = &DriverExtension->ConnectMultiplePorts;
198 Parameters[0].DefaultType = REG_DWORD;
199 Parameters[0].DefaultData = &DefaultConnectMultiplePorts;
200 Parameters[0].DefaultLength = sizeof(ULONG);
201
202 Parameters[1].Flags = RTL_QUERY_REGISTRY_DIRECT | RTL_REGISTRY_OPTIONAL;
203 Parameters[1].Name = L"KeyboardDataQueueSize";
204 Parameters[1].EntryContext = &DriverExtension->DataQueueSize;
205 Parameters[1].DefaultType = REG_DWORD;
206 Parameters[1].DefaultData = &DefaultDataQueueSize;
207 Parameters[1].DefaultLength = sizeof(ULONG);
208
209 Parameters[2].Flags = RTL_QUERY_REGISTRY_DIRECT | RTL_REGISTRY_OPTIONAL;
210 Parameters[2].Name = L"KeyboardDeviceBaseName";
211 Parameters[2].EntryContext = &DriverExtension->DeviceBaseName;
212 Parameters[2].DefaultType = REG_SZ;
213 Parameters[2].DefaultData = &DefaultDeviceBaseName;
214 Parameters[2].DefaultLength = 0;
215
216 Status = RtlQueryRegistryValues(
217 RTL_REGISTRY_ABSOLUTE,
218 ParametersRegistryKey.Buffer,
219 Parameters,
220 NULL,
221 NULL);
222
223 if (NT_SUCCESS(Status))
224 {
225 /* Check values */
226 if (DriverExtension->ConnectMultiplePorts != 0
227 && DriverExtension->ConnectMultiplePorts != 1)
228 {
229 DriverExtension->ConnectMultiplePorts = DefaultConnectMultiplePorts;
230 }
231 if (DriverExtension->DataQueueSize == 0)
232 {
233 DriverExtension->DataQueueSize = DefaultDataQueueSize;
234 }
235 }
236 else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
237 {
238 /* Registry path doesn't exist. Set defaults */
239 DriverExtension->ConnectMultiplePorts = DefaultConnectMultiplePorts;
240 DriverExtension->DataQueueSize = DefaultDataQueueSize;
241 Status = RtlDuplicateUnicodeString(
242 RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
243 &DefaultDeviceBaseName,
244 &DriverExtension->DeviceBaseName);
245 }
246
247 return Status;
248 }
249
250 static NTSTATUS
251 CreateClassDeviceObject(
252 IN PDRIVER_OBJECT DriverObject,
253 OUT PDEVICE_OBJECT *ClassDO OPTIONAL)
254 {
255 PCLASS_DRIVER_EXTENSION DriverExtension;
256 UNICODE_STRING SymbolicLinkName = RTL_CONSTANT_STRING(L"\\??\\Keyboard");
257 ULONG DeviceId = 0;
258 ULONG PrefixLength;
259 UNICODE_STRING DeviceNameU;
260 PWSTR DeviceIdW = NULL; /* Pointer into DeviceNameU.Buffer */
261 PDEVICE_OBJECT Fdo;
262 PCLASS_DEVICE_EXTENSION DeviceExtension;
263 NTSTATUS Status;
264
265 DPRINT("CreateClassDeviceObject(0x%p)\n", DriverObject);
266
267 /* Create new device object */
268 DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject);
269 DeviceNameU.Length = 0;
270 DeviceNameU.MaximumLength =
271 wcslen(L"\\Device\\") * sizeof(WCHAR) /* "\Device\" */
272 + DriverExtension->DeviceBaseName.Length /* "KeyboardClass" */
273 + 4 * sizeof(WCHAR) /* Id between 0 and 9999 */
274 + sizeof(UNICODE_NULL); /* Final NULL char */
275 DeviceNameU.Buffer = ExAllocatePool(PagedPool, DeviceNameU.MaximumLength);
276 if (!DeviceNameU.Buffer)
277 {
278 DPRINT("ExAllocatePool() failed\n");
279 return STATUS_INSUFFICIENT_RESOURCES;
280 }
281 Status = RtlAppendUnicodeToString(&DeviceNameU, L"\\Device\\");
282 if (!NT_SUCCESS(Status))
283 {
284 DPRINT("RtlAppendUnicodeToString() failed with status 0x%08lx\n", Status);
285 goto cleanup;
286 }
287 Status = RtlAppendUnicodeStringToString(&DeviceNameU, &DriverExtension->DeviceBaseName);
288 if (!NT_SUCCESS(Status))
289 {
290 DPRINT("RtlAppendUnicodeStringToString() failed with status 0x%08lx\n", Status);
291 goto cleanup;
292 }
293 PrefixLength = DeviceNameU.MaximumLength - 4 * sizeof(WCHAR) - sizeof(UNICODE_NULL);
294 DeviceIdW = &DeviceNameU.Buffer[PrefixLength / sizeof(WCHAR)];
295 while (DeviceId < 9999)
296 {
297 DeviceNameU.Length = PrefixLength + swprintf(DeviceIdW, L"%lu", DeviceId) * sizeof(WCHAR);
298 Status = IoCreateDevice(
299 DriverObject,
300 sizeof(CLASS_DEVICE_EXTENSION),
301 &DeviceNameU,
302 FILE_DEVICE_KEYBOARD,
303 FILE_DEVICE_SECURE_OPEN,
304 TRUE,
305 &Fdo);
306 if (NT_SUCCESS(Status))
307 goto cleanup;
308 else if (Status != STATUS_OBJECT_NAME_COLLISION)
309 {
310 DPRINT("IoCreateDevice() failed with status 0x%08lx\n", Status);
311 goto cleanup;
312 }
313 DeviceId++;
314 }
315 DPRINT("Too much devices starting with '\\Device\\%wZ'\n", &DriverExtension->DeviceBaseName);
316 Status = STATUS_UNSUCCESSFUL;
317 cleanup:
318 if (!NT_SUCCESS(Status))
319 {
320 ExFreePool(DeviceNameU.Buffer);
321 return Status;
322 }
323
324 DeviceExtension = (PCLASS_DEVICE_EXTENSION)Fdo->DeviceExtension;
325 RtlZeroMemory(DeviceExtension, sizeof(CLASS_DEVICE_EXTENSION));
326 DeviceExtension->Common.IsClassDO = TRUE;
327 DeviceExtension->DriverExtension = DriverExtension;
328 KeInitializeSpinLock(&(DeviceExtension->SpinLock));
329 DeviceExtension->ReadIsPending = FALSE;
330 DeviceExtension->InputCount = 0;
331 DeviceExtension->PortData = ExAllocatePool(NonPagedPool, DeviceExtension->DriverExtension->DataQueueSize * sizeof(KEYBOARD_INPUT_DATA));
332 Fdo->Flags |= DO_POWER_PAGABLE | DO_BUFFERED_IO;
333 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
334
335 /* Add entry entry to HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\[DeviceBaseName] */
336 RtlWriteRegistryValue(
337 RTL_REGISTRY_DEVICEMAP,
338 DriverExtension->DeviceBaseName.Buffer,
339 DeviceNameU.Buffer,
340 REG_SZ,
341 DriverExtension->RegistryPath.Buffer,
342 DriverExtension->RegistryPath.MaximumLength);
343
344 /* HACK: 1st stage setup needs a keyboard to open it in user-mode
345 * Create a link to user space... */
346 IoCreateSymbolicLink(&SymbolicLinkName, &DeviceNameU);
347
348 ExFreePool(DeviceNameU.Buffer);
349
350 if (ClassDO)
351 *ClassDO = Fdo;
352
353 return STATUS_SUCCESS;
354 }
355
356 static BOOLEAN
357 ClassCallback(
358 IN PDEVICE_OBJECT ClassDeviceObject,
359 IN OUT PKEYBOARD_INPUT_DATA DataStart,
360 IN PKEYBOARD_INPUT_DATA DataEnd,
361 IN OUT PULONG ConsumedCount)
362 {
363 PCLASS_DEVICE_EXTENSION ClassDeviceExtension = ClassDeviceObject->DeviceExtension;
364 PIRP Irp = NULL;
365 KIRQL OldIrql;
366 PIO_STACK_LOCATION Stack;
367 ULONG InputCount = DataEnd - DataStart;
368 ULONG ReadSize;
369
370 ASSERT(ClassDeviceExtension->Common.IsClassDO);
371
372 DPRINT("ClassCallback()\n");
373 /* A filter driver might have consumed all the data already; I'm
374 * not sure if they are supposed to move the packets when they
375 * consume them though.
376 */
377 if (ClassDeviceExtension->ReadIsPending == TRUE && InputCount)
378 {
379 Irp = ClassDeviceObject->CurrentIrp;
380 ClassDeviceObject->CurrentIrp = NULL;
381 Stack = IoGetCurrentIrpStackLocation(Irp);
382
383 /* A read request is waiting for input, so go straight to it */
384 /* FIXME: use SEH */
385 RtlCopyMemory(
386 Irp->MdlAddress ? MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority) : Irp->AssociatedIrp.SystemBuffer,
387 DataStart,
388 sizeof(KEYBOARD_INPUT_DATA));
389
390 /* Go to next packet and complete this request with STATUS_SUCCESS */
391 Irp->IoStatus.Status = STATUS_SUCCESS;
392 Irp->IoStatus.Information = sizeof(KEYBOARD_INPUT_DATA);
393 Stack->Parameters.Read.Length = sizeof(KEYBOARD_INPUT_DATA);
394
395 ClassDeviceExtension->ReadIsPending = FALSE;
396
397 /* Skip the packet we just sent away */
398 DataStart++;
399 (*ConsumedCount)++;
400 InputCount--;
401 }
402
403 /* If we have data from the port driver and a higher service to send the data to */
404 if (InputCount != 0)
405 {
406 KeAcquireSpinLock(&ClassDeviceExtension->SpinLock, &OldIrql);
407
408 if (ClassDeviceExtension->InputCount + InputCount > ClassDeviceExtension->DriverExtension->DataQueueSize)
409 ReadSize = ClassDeviceExtension->DriverExtension->DataQueueSize - ClassDeviceExtension->InputCount;
410 else
411 ReadSize = InputCount;
412
413 /*
414 * FIXME: If we exceed the buffer, data gets thrown away.. better
415 * solution?
416 */
417
418 /*
419 * Move the input data from the port data queue to our class data
420 * queue.
421 */
422 RtlMoveMemory(
423 ClassDeviceExtension->PortData,
424 (PCHAR)DataStart,
425 sizeof(KEYBOARD_INPUT_DATA) * ReadSize);
426
427 /* Move the pointer and counter up */
428 ClassDeviceExtension->PortData += ReadSize;
429 ClassDeviceExtension->InputCount += ReadSize;
430
431 KeReleaseSpinLock(&ClassDeviceExtension->SpinLock, OldIrql);
432 (*ConsumedCount) += ReadSize;
433 }
434 else
435 {
436 DPRINT("ClassCallBack() entered, InputCount = %lu - DOING NOTHING\n", InputCount);
437 }
438
439 if (Irp != NULL)
440 {
441 IoStartNextPacket(ClassDeviceObject, FALSE);
442 IoCompleteRequest(Irp, IO_KEYBOARD_INCREMENT);
443 }
444
445 DPRINT("Leaving ClassCallback()\n");
446 return TRUE;
447 }
448
449 /* Send IOCTL_INTERNAL_*_CONNECT to port */
450 static NTSTATUS
451 ConnectPortDriver(
452 IN PDEVICE_OBJECT PortDO,
453 IN PDEVICE_OBJECT ClassDO)
454 {
455 KEVENT Event;
456 PIRP Irp;
457 IO_STATUS_BLOCK IoStatus;
458 CONNECT_DATA ConnectData;
459 NTSTATUS Status;
460
461 KeInitializeEvent(&Event, NotificationEvent, FALSE);
462
463 ConnectData.ClassDeviceObject = ClassDO;
464 ConnectData.ClassService = ClassCallback;
465
466 Irp = IoBuildDeviceIoControlRequest(IOCTL_INTERNAL_KEYBOARD_CONNECT,
467 PortDO,
468 &ConnectData, sizeof(CONNECT_DATA),
469 NULL, 0,
470 TRUE, &Event, &IoStatus);
471
472 Status = IoCallDriver(PortDO, Irp);
473
474 if (Status == STATUS_PENDING)
475 KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
476 else
477 IoStatus.Status = Status;
478
479 if (NT_SUCCESS(IoStatus.Status))
480 ObReferenceObject(PortDO);
481
482 return IoStatus.Status;
483 }
484
485 static NTSTATUS NTAPI
486 ClassAddDevice(
487 IN PDRIVER_OBJECT DriverObject,
488 IN PDEVICE_OBJECT Pdo)
489 {
490 PCLASS_DRIVER_EXTENSION DriverExtension;
491 PDEVICE_OBJECT Fdo;
492 PPORT_DEVICE_EXTENSION DeviceExtension;
493 NTSTATUS Status;
494
495 DPRINT("ClassAddDevice called. Pdo = 0x%p\n", Pdo);
496
497 DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject);
498
499 if (Pdo == NULL)
500 /* We're getting a NULL Pdo at the first call as we're a legacy driver.
501 * Use it to search for legacy port drivers. */
502 return SearchForLegacyDrivers(DriverObject, DriverExtension);
503
504 /* Create new device object */
505 Status = IoCreateDevice(
506 DriverObject,
507 sizeof(PORT_DEVICE_EXTENSION),
508 NULL,
509 Pdo->DeviceType,
510 FILE_DEVICE_SECURE_OPEN,
511 TRUE,
512 &Fdo);
513 if (!NT_SUCCESS(Status))
514 {
515 DPRINT("IoCreateDevice() failed with status 0x%08lx\n", Status);
516 return Status;
517 }
518
519 DeviceExtension = (PPORT_DEVICE_EXTENSION)Fdo->DeviceExtension;
520 RtlZeroMemory(DeviceExtension, sizeof(CLASS_DEVICE_EXTENSION));
521 DeviceExtension->Common.IsClassDO = FALSE;
522 DeviceExtension->PnpState = dsStopped;
523 Fdo->Flags |= DO_POWER_PAGABLE;
524 Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice);
525 if (!NT_SUCCESS(Status))
526 {
527 DPRINT("IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status);
528 IoDeleteDevice(Fdo);
529 return Status;
530 }
531 Fdo->Flags |= DO_BUFFERED_IO;
532
533 if (DriverExtension->ConnectMultiplePorts)
534 Status = ConnectPortDriver(Fdo, DriverExtension->MainClassDeviceObject);
535 else
536 Status = ConnectPortDriver(Fdo, Fdo);
537 if (!NT_SUCCESS(Status))
538 {
539 DPRINT("ConnectPortDriver() failed with status 0x%08lx\n", Status);
540 IoDetachDevice(DeviceExtension->LowerDevice);
541 /* FIXME: why can't I cleanup without error? */
542 //IoDeleteDevice(Fdo);
543 return Status;
544 }
545 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
546
547 /* Register interface */
548 Status = IoRegisterDeviceInterface(
549 Pdo,
550 &GUID_DEVINTERFACE_KEYBOARD,
551 NULL,
552 &DeviceExtension->InterfaceName);
553 if (!NT_SUCCESS(Status))
554 {
555 DPRINT("IoRegisterDeviceInterface() failed with status 0x%08lx\n", Status);
556 return Status;
557 }
558
559 return STATUS_SUCCESS;
560 }
561
562 static VOID NTAPI
563 ClassStartIo(
564 IN PDEVICE_OBJECT DeviceObject,
565 IN PIRP Irp)
566 {
567 PCLASS_DEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
568 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
569
570 ASSERT(DeviceExtension->Common.IsClassDO);
571
572 if (DeviceExtension->InputCount > 0)
573 {
574 KIRQL oldIrql;
575
576 KeAcquireSpinLock(&DeviceExtension->SpinLock, &oldIrql);
577
578 /* FIXME: use SEH */
579 RtlCopyMemory(
580 Irp->MdlAddress ? MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority) : Irp->UserBuffer,
581 DeviceExtension->PortData - DeviceExtension->InputCount,
582 sizeof(KEYBOARD_INPUT_DATA));
583
584 if (DeviceExtension->InputCount > 1)
585 {
586 RtlMoveMemory(
587 DeviceExtension->PortData - DeviceExtension->InputCount,
588 DeviceExtension->PortData - DeviceExtension->InputCount + 1,
589 (DeviceExtension->InputCount - 1) * sizeof(KEYBOARD_INPUT_DATA));
590 }
591 DeviceExtension->PortData--;
592 DeviceExtension->InputCount--;
593 DeviceExtension->ReadIsPending = FALSE;
594
595 /* Go to next packet and complete this request with STATUS_SUCCESS */
596 Irp->IoStatus.Status = STATUS_SUCCESS;
597 Irp->IoStatus.Information = sizeof(KEYBOARD_INPUT_DATA);
598 Stack->Parameters.Read.Length = sizeof(KEYBOARD_INPUT_DATA);
599 IoCompleteRequest(Irp, IO_KEYBOARD_INCREMENT);
600
601 IoStartNextPacket(DeviceObject, FALSE);
602 KeReleaseSpinLock(&DeviceExtension->SpinLock, oldIrql);
603 }
604 else
605 {
606 DeviceExtension->ReadIsPending = TRUE;
607 }
608 }
609
610 static NTSTATUS
611 SearchForLegacyDrivers(
612 IN PDRIVER_OBJECT DriverObject,
613 IN PCLASS_DRIVER_EXTENSION DriverExtension)
614 {
615 UNICODE_STRING DeviceMapKeyU = RTL_CONSTANT_STRING(L"\\REGISTRY\\MACHINE\\HARDWARE\\DEVICEMAP");
616 UNICODE_STRING PortBaseName = {0, };
617 PKEY_VALUE_BASIC_INFORMATION KeyValueInformation = NULL;
618 OBJECT_ATTRIBUTES ObjectAttributes;
619 HANDLE hDeviceMapKey = (HANDLE)-1;
620 HANDLE hPortKey = (HANDLE)-1;
621 ULONG Index = 0;
622 ULONG Size, ResultLength;
623 NTSTATUS Status;
624
625 /* Create port base name, by replacing Class by Port at the end of the class base name */
626 Status = RtlDuplicateUnicodeString(
627 RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
628 &DriverExtension->DeviceBaseName,
629 &PortBaseName);
630 if (!NT_SUCCESS(Status))
631 {
632 DPRINT("RtlDuplicateUnicodeString() failed with status 0x%08lx\n", Status);
633 goto cleanup;
634 }
635 PortBaseName.Length -= (sizeof(L"Class") - sizeof(UNICODE_NULL));
636 RtlAppendUnicodeToString(&PortBaseName, L"Port");
637
638 /* Allocate memory */
639 Size = sizeof(KEY_VALUE_BASIC_INFORMATION) + MAX_PATH;
640 KeyValueInformation = ExAllocatePool(PagedPool, Size);
641 if (!KeyValueInformation)
642 {
643 DPRINT("ExAllocatePool() failed\n");
644 Status = STATUS_INSUFFICIENT_RESOURCES;
645 goto cleanup;
646 }
647
648 /* Open HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP */
649 InitializeObjectAttributes(&ObjectAttributes, &DeviceMapKeyU, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
650 Status = ZwOpenKey(&hDeviceMapKey, 0, &ObjectAttributes);
651 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
652 {
653 DPRINT("HKLM\\HARDWARE\\DEVICEMAP is non-existent\n");
654 Status = STATUS_SUCCESS;
655 goto cleanup;
656 }
657 else if (!NT_SUCCESS(Status))
658 {
659 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
660 goto cleanup;
661 }
662
663 /* Open sub key */
664 InitializeObjectAttributes(&ObjectAttributes, &PortBaseName, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, hDeviceMapKey, NULL);
665 Status = ZwOpenKey(&hPortKey, KEY_QUERY_VALUE, &ObjectAttributes);
666 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
667 {
668 DPRINT("HKLM\\HARDWARE\\DEVICEMAP\\%wZ is non-existent\n", &PortBaseName);
669 Status = STATUS_SUCCESS;
670 goto cleanup;
671 }
672 else if (!NT_SUCCESS(Status))
673 {
674 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
675 goto cleanup;
676 }
677
678 /* Read each value name */
679 while (ZwEnumerateValueKey(hPortKey, Index++, KeyValueBasicInformation, KeyValueInformation, Size, &ResultLength) == STATUS_SUCCESS)
680 {
681 UNICODE_STRING PortName;
682 PDEVICE_OBJECT PortDeviceObject = NULL;
683 PFILE_OBJECT FileObject = NULL;
684
685 PortName.Length = PortName.MaximumLength = KeyValueInformation->NameLength;
686 PortName.Buffer = KeyValueInformation->Name;
687
688 /* Open the device object pointer */
689 Status = IoGetDeviceObjectPointer(&PortName, FILE_READ_ATTRIBUTES, &FileObject, &PortDeviceObject);
690 if (!NT_SUCCESS(Status))
691 {
692 DPRINT("IoGetDeviceObjectPointer(%wZ) failed with status 0x%08lx\n", Status);
693 }
694
695 /* Connect the port device object */
696 if (DriverExtension->ConnectMultiplePorts)
697 {
698 Status = ConnectPortDriver(PortDeviceObject, DriverExtension->MainClassDeviceObject);
699 if (!NT_SUCCESS(Status))
700 {
701 /* FIXME: Log the error */
702 DPRINT("ConnectPortDriver() failed with status 0x%08lx\n", Status);
703 }
704 }
705 else
706 {
707 PDEVICE_OBJECT ClassDO;
708 Status = CreateClassDeviceObject(DriverObject, &ClassDO);
709 if (!NT_SUCCESS(Status))
710 {
711 /* FIXME: Log the error */
712 DPRINT("CreatePointerClassDeviceObject() failed with status 0x%08lx\n", Status);
713 continue;
714 }
715 Status = ConnectPortDriver(PortDeviceObject, ClassDO);
716 if (!NT_SUCCESS(Status))
717 {
718 /* FIXME: Log the error */
719 DPRINT("ConnectPortDriver() failed with status 0x%08lx\n", Status);
720 IoDeleteDevice(ClassDO);
721 }
722 }
723 }
724 if (Status == STATUS_NO_MORE_ENTRIES)
725 Status = STATUS_SUCCESS;
726
727 cleanup:
728 if (KeyValueInformation != NULL)
729 ExFreePool(KeyValueInformation);
730 if (hDeviceMapKey != (HANDLE)-1)
731 ZwClose(hDeviceMapKey);
732 if (hPortKey != (HANDLE)-1)
733 ZwClose(hPortKey);
734 return Status;
735 }
736
737 /*
738 * Standard DriverEntry method.
739 */
740 NTSTATUS NTAPI
741 DriverEntry(
742 IN PDRIVER_OBJECT DriverObject,
743 IN PUNICODE_STRING RegistryPath)
744 {
745 PCLASS_DRIVER_EXTENSION DriverExtension;
746 ULONG i;
747 NTSTATUS Status;
748
749 Status = IoAllocateDriverObjectExtension(
750 DriverObject,
751 DriverObject,
752 sizeof(CLASS_DRIVER_EXTENSION),
753 (PVOID*)&DriverExtension);
754 if (!NT_SUCCESS(Status))
755 {
756 DPRINT("IoAllocateDriverObjectExtension() failed with status 0x%08lx\n", Status);
757 return Status;
758 }
759 RtlZeroMemory(DriverExtension, sizeof(CLASS_DRIVER_EXTENSION));
760
761 Status = RtlDuplicateUnicodeString(
762 RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
763 RegistryPath,
764 &DriverExtension->RegistryPath);
765 if (!NT_SUCCESS(Status))
766 {
767 DPRINT("RtlDuplicateUnicodeString() failed with status 0x%08lx\n", Status);
768 return Status;
769 }
770
771 Status = ReadRegistryEntries(RegistryPath, DriverExtension);
772 if (!NT_SUCCESS(Status))
773 {
774 DPRINT("ReadRegistryEntries() failed with status 0x%08lx\n", Status);
775 return Status;
776 }
777
778 if (DriverExtension->ConnectMultiplePorts == 1)
779 {
780 Status = CreateClassDeviceObject(
781 DriverObject,
782 &DriverExtension->MainClassDeviceObject);
783 if (!NT_SUCCESS(Status))
784 {
785 DPRINT("CreateClassDeviceObject() failed with status 0x%08lx\n", Status);
786 return Status;
787 }
788 }
789
790 DriverObject->DriverExtension->AddDevice = ClassAddDevice;
791 DriverObject->DriverUnload = DriverUnload;
792
793 for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
794 DriverObject->MajorFunction[i] = IrpStub;
795
796 DriverObject->MajorFunction[IRP_MJ_CREATE] = ClassCreate;
797 DriverObject->MajorFunction[IRP_MJ_CLOSE] = ClassClose;
798 DriverObject->MajorFunction[IRP_MJ_CLEANUP] = ClassCleanup;
799 DriverObject->MajorFunction[IRP_MJ_READ] = ClassRead;
800 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = ClassDeviceControl;
801 DriverObject->DriverStartIo = ClassStartIo;
802
803 return STATUS_SUCCESS;
804 }