0827a2f5b56e9e1a55dbd844eb4a38d2ce27eb7a
[reactos.git] / drivers / input / i8042prt / keyboard.c
1 /*
2 * PROJECT: ReactOS i8042 (ps/2 keyboard-mouse controller) driver
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/input/i8042prt/keyboard.c
5 * PURPOSE: Keyboard specific functions
6 * PROGRAMMERS: Copyright Victor Kirhenshtein (sauros@iname.com)
7 Copyright Jason Filby (jasonfilby@yahoo.com)
8 Copyright Martijn Vernooij (o112w8r02@sneakemail.com)
9 Copyright 2006-2007 Hervé Poussineau (hpoussin@reactos.org)
10 */
11
12 /* INCLUDES ****************************************************************/
13
14 #include "i8042prt.h"
15
16 #include <poclass.h>
17 #include <ndk/kdfuncs.h>
18
19 #include <debug.h>
20
21 /* GLOBALS *******************************************************************/
22
23 static IO_WORKITEM_ROUTINE i8042PowerWorkItem;
24 static KDEFERRED_ROUTINE i8042KbdDpcRoutine;
25
26 /* This structure starts with the same layout as KEYBOARD_INDICATOR_TRANSLATION */
27 typedef struct _LOCAL_KEYBOARD_INDICATOR_TRANSLATION {
28 USHORT NumberOfIndicatorKeys;
29 INDICATOR_LIST IndicatorList[3];
30 } LOCAL_KEYBOARD_INDICATOR_TRANSLATION, *PLOCAL_KEYBOARD_INDICATOR_TRANSLATION;
31
32 static LOCAL_KEYBOARD_INDICATOR_TRANSLATION IndicatorTranslation = { 3, {
33 {0x3A, KEYBOARD_CAPS_LOCK_ON},
34 {0x45, KEYBOARD_NUM_LOCK_ON},
35 {0x46, KEYBOARD_SCROLL_LOCK_ON}}};
36
37 /* FUNCTIONS *****************************************************************/
38
39 /*
40 * These functions are callbacks for filter driver custom interrupt
41 * service routines.
42 */
43 /*static VOID NTAPI
44 i8042KbdIsrWritePort(
45 IN PVOID Context,
46 IN UCHAR Value)
47 {
48 PI8042_KEYBOARD_EXTENSION DeviceExtension;
49
50 DeviceExtension = (PI8042_KEYBOARD_EXTENSION)Context;
51
52 if (DeviceExtension->KeyboardHook.IsrWritePort)
53 {
54 DeviceExtension->KeyboardHook.IsrWritePort(
55 DeviceExtension->KeyboardHook.CallContext,
56 Value);
57 }
58 else
59 i8042IsrWritePort(Context, Value, 0);
60 }*/
61
62 static VOID NTAPI
63 i8042KbdQueuePacket(
64 IN PVOID Context)
65 {
66 PI8042_KEYBOARD_EXTENSION DeviceExtension;
67
68 DeviceExtension = (PI8042_KEYBOARD_EXTENSION)Context;
69
70 DeviceExtension->KeyComplete = TRUE;
71 DeviceExtension->KeysInBuffer++;
72 if (DeviceExtension->KeysInBuffer > DeviceExtension->Common.PortDeviceExtension->Settings.KeyboardDataQueueSize)
73 {
74 WARN_(I8042PRT, "Keyboard buffer overflow\n");
75 DeviceExtension->KeysInBuffer--;
76 }
77
78 TRACE_(I8042PRT, "Irq completes key\n");
79 KeInsertQueueDpc(&DeviceExtension->DpcKeyboard, NULL, NULL);
80 }
81
82 /*
83 * These functions are callbacks for filter driver custom
84 * initialization routines.
85 */
86 NTSTATUS NTAPI
87 i8042SynchWritePortKbd(
88 IN PVOID Context,
89 IN UCHAR Value,
90 IN BOOLEAN WaitForAck)
91 {
92 return i8042SynchWritePort(
93 (PPORT_DEVICE_EXTENSION)Context,
94 0,
95 Value,
96 WaitForAck);
97 }
98
99 /*
100 * Process the keyboard internal device requests
101 */
102 VOID NTAPI
103 i8042KbdStartIo(
104 IN PDEVICE_OBJECT DeviceObject,
105 IN PIRP Irp)
106 {
107 PIO_STACK_LOCATION Stack;
108 PI8042_KEYBOARD_EXTENSION DeviceExtension;
109 PPORT_DEVICE_EXTENSION PortDeviceExtension;
110
111 Stack = IoGetCurrentIrpStackLocation(Irp);
112 DeviceExtension = (PI8042_KEYBOARD_EXTENSION)DeviceObject->DeviceExtension;
113 PortDeviceExtension = DeviceExtension->Common.PortDeviceExtension;
114
115 switch (Stack->Parameters.DeviceIoControl.IoControlCode)
116 {
117 case IOCTL_KEYBOARD_SET_INDICATORS:
118 {
119 TRACE_(I8042PRT, "IOCTL_KEYBOARD_SET_INDICATORS\n");
120 INFO_(I8042PRT, "Leds: {%s%s%s }\n",
121 DeviceExtension->KeyboardIndicators.LedFlags & KEYBOARD_CAPS_LOCK_ON ? " CAPSLOCK" : "",
122 DeviceExtension->KeyboardIndicators.LedFlags & KEYBOARD_NUM_LOCK_ON ? " NUMLOCK" : "",
123 DeviceExtension->KeyboardIndicators.LedFlags & KEYBOARD_SCROLL_LOCK_ON ? " SCROLLLOCK" : "");
124
125 PortDeviceExtension->PacketBuffer[0] = KBD_CMD_SET_LEDS;
126 PortDeviceExtension->PacketBuffer[1] = 0;
127 if (DeviceExtension->KeyboardIndicators.LedFlags & KEYBOARD_CAPS_LOCK_ON)
128 PortDeviceExtension->PacketBuffer[1] |= KBD_LED_CAPS;
129
130 if (DeviceExtension->KeyboardIndicators.LedFlags & KEYBOARD_NUM_LOCK_ON)
131 PortDeviceExtension->PacketBuffer[1] |= KBD_LED_NUM;
132
133 if (DeviceExtension->KeyboardIndicators.LedFlags & KEYBOARD_SCROLL_LOCK_ON)
134 PortDeviceExtension->PacketBuffer[1] |= KBD_LED_SCROLL;
135
136 i8042StartPacket(
137 PortDeviceExtension,
138 &DeviceExtension->Common,
139 PortDeviceExtension->PacketBuffer,
140 2,
141 Irp);
142 break;
143 }
144 default:
145 {
146 ERR_(I8042PRT, "Unknown ioctl code 0x%lx\n",
147 Stack->Parameters.DeviceIoControl.IoControlCode);
148 ASSERT(FALSE);
149 }
150 }
151 }
152
153 static VOID
154 i8042PacketDpc(
155 IN PPORT_DEVICE_EXTENSION DeviceExtension)
156 {
157 BOOLEAN FinishIrp = FALSE;
158 KIRQL Irql;
159 NTSTATUS Result = STATUS_INTERNAL_ERROR; /* Shouldn't happen */
160
161 /* If the interrupt happens before this is setup, the key
162 * was already in the buffer. Too bad! */
163 if (!DeviceExtension->HighestDIRQLInterrupt)
164 return;
165
166 Irql = KeAcquireInterruptSpinLock(DeviceExtension->HighestDIRQLInterrupt);
167
168 if (DeviceExtension->Packet.State == Idle
169 && DeviceExtension->PacketComplete)
170 {
171 FinishIrp = TRUE;
172 Result = DeviceExtension->PacketResult;
173 DeviceExtension->PacketComplete = FALSE;
174 }
175
176 KeReleaseInterruptSpinLock(DeviceExtension->HighestDIRQLInterrupt, Irql);
177
178 if (!FinishIrp)
179 return;
180
181 if (DeviceExtension->CurrentIrp)
182 {
183 DeviceExtension->CurrentIrp->IoStatus.Status = Result;
184 IoCompleteRequest(DeviceExtension->CurrentIrp, IO_NO_INCREMENT);
185 IoStartNextPacket(DeviceExtension->CurrentIrpDevice, FALSE);
186 DeviceExtension->CurrentIrp = NULL;
187 DeviceExtension->CurrentIrpDevice = NULL;
188 }
189 }
190
191 static VOID NTAPI
192 i8042PowerWorkItem(
193 IN PDEVICE_OBJECT DeviceObject,
194 IN PVOID Context)
195 {
196 PI8042_KEYBOARD_EXTENSION DeviceExtension;
197 PIRP WaitingIrp;
198 NTSTATUS Status;
199
200 UNREFERENCED_PARAMETER(DeviceObject);
201
202 __analysis_assume(Context != NULL);
203 DeviceExtension = Context;
204
205 /* See http://blogs.msdn.com/doronh/archive/2006/09/08/746961.aspx */
206
207 /* Register GUID_DEVICE_SYS_BUTTON interface and report capability */
208 if (DeviceExtension->NewCaps != DeviceExtension->ReportedCaps)
209 {
210 WaitingIrp = InterlockedExchangePointer((PVOID)&DeviceExtension->PowerIrp, NULL);
211 if (WaitingIrp)
212 {
213 /* Cancel the current power irp, as capability changed */
214 WaitingIrp->IoStatus.Status = STATUS_UNSUCCESSFUL;
215 WaitingIrp->IoStatus.Information = sizeof(ULONG);
216 IoCompleteRequest(WaitingIrp, IO_NO_INCREMENT);
217 }
218
219 if (DeviceExtension->PowerInterfaceName.MaximumLength == 0)
220 {
221 /* We have never registred this interface ; do it */
222 Status = IoRegisterDeviceInterface(
223 DeviceExtension->Common.Pdo,
224 &GUID_DEVICE_SYS_BUTTON,
225 NULL,
226 &DeviceExtension->PowerInterfaceName);
227 if (!NT_SUCCESS(Status))
228 {
229 /* We can't do more yet, ignore the keypress... */
230 WARN_(I8042PRT, "IoRegisterDeviceInterface(GUID_DEVICE_SYS_BUTTON) failed with status 0x%08lx\n",
231 Status);
232 DeviceExtension->PowerInterfaceName.MaximumLength = 0;
233 return;
234 }
235 }
236 else
237 {
238 /* Disable the interface. Once activated again, capabilities would be asked again */
239 Status = IoSetDeviceInterfaceState(
240 &DeviceExtension->PowerInterfaceName,
241 FALSE);
242 if (!NT_SUCCESS(Status))
243 {
244 /* Ignore the key press... */
245 WARN_(I8042PRT, "Disabling interface %wZ failed with status 0x%08lx\n",
246 &DeviceExtension->PowerInterfaceName, Status);
247 return;
248 }
249 }
250 /* Enable the interface. This leads to receving a IOCTL_GET_SYS_BUTTON_CAPS,
251 * so we can report new capability */
252 Status = IoSetDeviceInterfaceState(
253 &DeviceExtension->PowerInterfaceName,
254 TRUE);
255 if (!NT_SUCCESS(Status))
256 {
257 /* Ignore the key press... */
258 WARN_(I8042PRT, "Enabling interface %wZ failed with status 0x%08lx\n",
259 &DeviceExtension->PowerInterfaceName, Status);
260 return;
261 }
262 }
263
264 /* Directly complete the IOCTL_GET_SYS_BUTTON_EVENT Irp (if any) */
265 WaitingIrp = InterlockedExchangePointer((PVOID)&DeviceExtension->PowerIrp, NULL);
266 if (WaitingIrp)
267 {
268 PULONG pEvent = (PULONG)WaitingIrp->AssociatedIrp.SystemBuffer;
269
270 WaitingIrp->IoStatus.Status = STATUS_SUCCESS;
271 WaitingIrp->IoStatus.Information = sizeof(ULONG);
272 *pEvent = InterlockedExchange((PLONG)&DeviceExtension->LastPowerKey, 0);
273 IoCompleteRequest(WaitingIrp, IO_NO_INCREMENT);
274 }
275 }
276
277 /* Return TRUE if it was a power key */
278 static BOOLEAN
279 HandlePowerKeys(
280 IN PI8042_KEYBOARD_EXTENSION DeviceExtension)
281 {
282 PKEYBOARD_INPUT_DATA InputData;
283 ULONG KeyPress;
284
285 InputData = DeviceExtension->KeyboardBuffer + DeviceExtension->KeysInBuffer - 1;
286 if (!(InputData->Flags & KEY_E0))
287 return FALSE;
288
289 switch (InputData->MakeCode)
290 {
291 case KEYBOARD_POWER_CODE:
292 KeyPress = SYS_BUTTON_POWER;
293 break;
294 case KEYBOARD_SLEEP_CODE:
295 KeyPress = SYS_BUTTON_SLEEP;
296 break;
297 case KEYBOARD_WAKE_CODE:
298 KeyPress = SYS_BUTTON_WAKE;
299 break;
300 default:
301 return FALSE;
302 }
303
304 if (InputData->Flags & KEY_BREAK)
305 /* We already took care of the key press */
306 return TRUE;
307
308 /* Our work can only be done at passive level, so use a workitem */
309 DeviceExtension->NewCaps |= KeyPress;
310 InterlockedExchange((PLONG)&DeviceExtension->LastPowerKey, KeyPress);
311 IoQueueWorkItem(
312 DeviceExtension->PowerWorkItem,
313 &i8042PowerWorkItem,
314 DelayedWorkQueue,
315 DeviceExtension);
316 return TRUE;
317 }
318
319 static VOID NTAPI
320 i8042KbdDpcRoutine(
321 IN PKDPC Dpc,
322 IN PVOID DeferredContext,
323 IN PVOID SystemArgument1,
324 IN PVOID SystemArgument2)
325 {
326 PI8042_KEYBOARD_EXTENSION DeviceExtension;
327 PPORT_DEVICE_EXTENSION PortDeviceExtension;
328 ULONG KeysTransferred = 0;
329 ULONG KeysInBufferCopy;
330 KIRQL Irql;
331
332 UNREFERENCED_PARAMETER(Dpc);
333 UNREFERENCED_PARAMETER(SystemArgument1);
334 UNREFERENCED_PARAMETER(SystemArgument2);
335
336 __analysis_assume(DeferredContext != NULL);
337 DeviceExtension = DeferredContext;
338 PortDeviceExtension = DeviceExtension->Common.PortDeviceExtension;
339
340 if (HandlePowerKeys(DeviceExtension))
341 {
342 DeviceExtension->KeyComplete = FALSE;
343 return;
344 }
345
346 i8042PacketDpc(PortDeviceExtension);
347 if (!DeviceExtension->KeyComplete)
348 return;
349 /* We got the interrupt as it was being enabled, too bad */
350 if (!PortDeviceExtension->HighestDIRQLInterrupt)
351 return;
352
353 Irql = KeAcquireInterruptSpinLock(PortDeviceExtension->HighestDIRQLInterrupt);
354
355 DeviceExtension->KeyComplete = FALSE;
356 KeysInBufferCopy = DeviceExtension->KeysInBuffer;
357
358 KeReleaseInterruptSpinLock(PortDeviceExtension->HighestDIRQLInterrupt, Irql);
359
360 TRACE_(I8042PRT, "Send a key\n");
361
362 if (!DeviceExtension->KeyboardData.ClassService)
363 return;
364
365 INFO_(I8042PRT, "Sending %lu key(s)\n", KeysInBufferCopy);
366 (*(PSERVICE_CALLBACK_ROUTINE)DeviceExtension->KeyboardData.ClassService)(
367 DeviceExtension->KeyboardData.ClassDeviceObject,
368 DeviceExtension->KeyboardBuffer,
369 DeviceExtension->KeyboardBuffer + KeysInBufferCopy,
370 &KeysTransferred);
371
372 KeAcquireInterruptSpinLock(PortDeviceExtension->HighestDIRQLInterrupt);
373 DeviceExtension->KeysInBuffer -= KeysTransferred;
374 KeReleaseInterruptSpinLock(PortDeviceExtension->HighestDIRQLInterrupt, Irql);
375 }
376
377 /*
378 * Runs the keyboard IOCTL dispatch.
379 */
380 NTSTATUS NTAPI
381 i8042KbdDeviceControl(
382 IN PDEVICE_OBJECT DeviceObject,
383 IN PIRP Irp)
384 {
385 PIO_STACK_LOCATION Stack;
386 PI8042_KEYBOARD_EXTENSION DeviceExtension;
387 NTSTATUS Status;
388
389 Stack = IoGetCurrentIrpStackLocation(Irp);
390 Irp->IoStatus.Information = 0;
391 DeviceExtension = (PI8042_KEYBOARD_EXTENSION)DeviceObject->DeviceExtension;
392
393 switch (Stack->Parameters.DeviceIoControl.IoControlCode)
394 {
395 case IOCTL_GET_SYS_BUTTON_CAPS:
396 {
397 /* Part of GUID_DEVICE_SYS_BUTTON interface */
398 PULONG pCaps;
399 TRACE_(I8042PRT, "IOCTL_GET_SYS_BUTTON_CAPS\n");
400
401 if (Stack->Parameters.DeviceIoControl.OutputBufferLength != sizeof(ULONG))
402 Status = STATUS_INVALID_PARAMETER;
403 else
404 {
405 pCaps = (PULONG)Irp->AssociatedIrp.SystemBuffer;
406 *pCaps = DeviceExtension->NewCaps;
407 DeviceExtension->ReportedCaps = DeviceExtension->NewCaps;
408 Irp->IoStatus.Information = sizeof(ULONG);
409 Status = STATUS_SUCCESS;
410 }
411 break;
412 }
413 case IOCTL_GET_SYS_BUTTON_EVENT:
414 {
415 /* Part of GUID_DEVICE_SYS_BUTTON interface */
416 PIRP WaitingIrp;
417 TRACE_(I8042PRT, "IOCTL_GET_SYS_BUTTON_EVENT\n");
418
419 if (Stack->Parameters.DeviceIoControl.OutputBufferLength != sizeof(ULONG))
420 Status = STATUS_INVALID_PARAMETER;
421 else
422 {
423 WaitingIrp = InterlockedCompareExchangePointer(
424 (PVOID)&DeviceExtension->PowerIrp,
425 Irp,
426 NULL);
427 /* Check if an Irp is already pending */
428 if (WaitingIrp)
429 {
430 /* Unable to have a 2nd pending IRP for this IOCTL */
431 WARN_(I8042PRT, "Unable to pend a second IRP for IOCTL_GET_SYS_BUTTON_EVENT\n");
432 Status = STATUS_INVALID_PARAMETER;
433 Irp->IoStatus.Status = Status;
434 IoCompleteRequest(Irp, IO_NO_INCREMENT);
435 }
436 else
437 {
438 ULONG PowerKey;
439 PowerKey = InterlockedExchange((PLONG)&DeviceExtension->LastPowerKey, 0);
440 if (PowerKey != 0)
441 {
442 (VOID)InterlockedCompareExchangePointer((PVOID)&DeviceExtension->PowerIrp, NULL, Irp);
443 *(PULONG)Irp->AssociatedIrp.SystemBuffer = PowerKey;
444 Status = STATUS_SUCCESS;
445 Irp->IoStatus.Status = Status;
446 Irp->IoStatus.Information = sizeof(ULONG);
447 IoCompleteRequest(Irp, IO_NO_INCREMENT);
448 }
449 else
450 {
451 TRACE_(I8042PRT, "Pending IOCTL_GET_SYS_BUTTON_EVENT\n");
452 Status = STATUS_PENDING;
453 Irp->IoStatus.Status = Status;
454 IoMarkIrpPending(Irp);
455 }
456 }
457 return Status;
458 }
459 break;
460 }
461 default:
462 {
463 ERR_(I8042PRT, "IRP_MJ_DEVICE_CONTROL / unknown ioctl code 0x%lx\n",
464 Stack->Parameters.DeviceIoControl.IoControlCode);
465 ASSERT(FALSE);
466 return ForwardIrpAndForget(DeviceObject, Irp);
467 }
468 }
469
470 Irp->IoStatus.Status = Status;
471 if (Status == STATUS_PENDING)
472 IoMarkIrpPending(Irp);
473 else
474 IoCompleteRequest(Irp, IO_NO_INCREMENT);
475
476 return Status;
477 }
478
479 VOID
480 NTAPI
481 i8042InitializeKeyboardAttributes(
482 PI8042_KEYBOARD_EXTENSION DeviceExtension)
483 {
484 PPORT_DEVICE_EXTENSION PortDeviceExtension;
485 PI8042_SETTINGS Settings;
486 PKEYBOARD_ATTRIBUTES KeyboardAttributes;
487
488 PortDeviceExtension = DeviceExtension->Common.PortDeviceExtension;
489 Settings = &PortDeviceExtension->Settings;
490
491 KeyboardAttributes = &DeviceExtension->KeyboardAttributes;
492
493 KeyboardAttributes->KeyboardIdentifier.Type = (UCHAR)Settings->OverrideKeyboardType;
494 KeyboardAttributes->KeyboardIdentifier.Subtype = (UCHAR)Settings->OverrideKeyboardSubtype;
495 KeyboardAttributes->NumberOfFunctionKeys = 4;
496 KeyboardAttributes->NumberOfIndicators = 3;
497 KeyboardAttributes->NumberOfKeysTotal = 101;
498 KeyboardAttributes->InputDataQueueLength = Settings->KeyboardDataQueueSize;
499 KeyboardAttributes->KeyRepeatMinimum.UnitId = 0;
500 KeyboardAttributes->KeyRepeatMinimum.Rate = (USHORT)Settings->SampleRate;
501 KeyboardAttributes->KeyRepeatMinimum.Delay = 0;
502 KeyboardAttributes->KeyRepeatMinimum.UnitId = 0;
503 KeyboardAttributes->KeyRepeatMinimum.Rate = (USHORT)Settings->SampleRate;
504 KeyboardAttributes->KeyRepeatMinimum.Delay = 0;
505 }
506
507 /*
508 * Runs the keyboard IOCTL_INTERNAL dispatch.
509 */
510 NTSTATUS NTAPI
511 i8042KbdInternalDeviceControl(
512 IN PDEVICE_OBJECT DeviceObject,
513 IN PIRP Irp)
514 {
515 PIO_STACK_LOCATION Stack;
516 PI8042_KEYBOARD_EXTENSION DeviceExtension;
517 NTSTATUS Status;
518
519 Stack = IoGetCurrentIrpStackLocation(Irp);
520 Irp->IoStatus.Information = 0;
521 DeviceExtension = (PI8042_KEYBOARD_EXTENSION)DeviceObject->DeviceExtension;
522
523 switch (Stack->Parameters.DeviceIoControl.IoControlCode)
524 {
525 case IOCTL_INTERNAL_KEYBOARD_CONNECT:
526 {
527 SIZE_T Size;
528 PIO_WORKITEM WorkItem = NULL;
529 PI8042_HOOK_WORKITEM WorkItemData = NULL;
530
531 TRACE_(I8042PRT, "IRP_MJ_INTERNAL_DEVICE_CONTROL / IOCTL_INTERNAL_KEYBOARD_CONNECT\n");
532 if (Stack->Parameters.DeviceIoControl.InputBufferLength != sizeof(CONNECT_DATA))
533 {
534 Status = STATUS_INVALID_PARAMETER;
535 goto cleanup;
536 }
537
538 DeviceExtension->KeyboardData =
539 *((PCONNECT_DATA)Stack->Parameters.DeviceIoControl.Type3InputBuffer);
540
541 /* Send IOCTL_INTERNAL_I8042_HOOK_KEYBOARD to device stack */
542 WorkItem = IoAllocateWorkItem(DeviceObject);
543 if (!WorkItem)
544 {
545 WARN_(I8042PRT, "IoAllocateWorkItem() failed\n");
546 Status = STATUS_INSUFFICIENT_RESOURCES;
547 goto cleanup;
548 }
549 WorkItemData = ExAllocatePoolWithTag(
550 NonPagedPool,
551 sizeof(I8042_HOOK_WORKITEM),
552 I8042PRT_TAG);
553 if (!WorkItemData)
554 {
555 WARN_(I8042PRT, "ExAllocatePoolWithTag() failed\n");
556 Status = STATUS_NO_MEMORY;
557 goto cleanup;
558 }
559 WorkItemData->WorkItem = WorkItem;
560 WorkItemData->Irp = Irp;
561
562 /* Initialize extension */
563 DeviceExtension->Common.Type = Keyboard;
564 Size = DeviceExtension->Common.PortDeviceExtension->Settings.KeyboardDataQueueSize * sizeof(KEYBOARD_INPUT_DATA);
565 DeviceExtension->KeyboardBuffer = ExAllocatePoolWithTag(
566 NonPagedPool,
567 Size,
568 I8042PRT_TAG);
569 if (!DeviceExtension->KeyboardBuffer)
570 {
571 WARN_(I8042PRT, "ExAllocatePoolWithTag() failed\n");
572 Status = STATUS_NO_MEMORY;
573 goto cleanup;
574 }
575 RtlZeroMemory(DeviceExtension->KeyboardBuffer, Size);
576 KeInitializeDpc(
577 &DeviceExtension->DpcKeyboard,
578 i8042KbdDpcRoutine,
579 DeviceExtension);
580 DeviceExtension->PowerWorkItem = IoAllocateWorkItem(DeviceObject);
581 if (!DeviceExtension->PowerWorkItem)
582 {
583 WARN_(I8042PRT, "IoAllocateWorkItem() failed\n");
584 Status = STATUS_INSUFFICIENT_RESOURCES;
585 goto cleanup;
586 }
587 DeviceExtension->DebugWorkItem = IoAllocateWorkItem(DeviceObject);
588 if (!DeviceExtension->DebugWorkItem)
589 {
590 WARN_(I8042PRT, "IoAllocateWorkItem() failed\n");
591 Status = STATUS_INSUFFICIENT_RESOURCES;
592 goto cleanup;
593 }
594 DeviceExtension->Common.PortDeviceExtension->KeyboardExtension = DeviceExtension;
595 DeviceExtension->Common.PortDeviceExtension->Flags |= KEYBOARD_CONNECTED;
596
597 i8042InitializeKeyboardAttributes(DeviceExtension);
598
599 IoMarkIrpPending(Irp);
600 /* FIXME: DeviceExtension->KeyboardHook.IsrWritePort = ; */
601 DeviceExtension->KeyboardHook.QueueKeyboardPacket = i8042KbdQueuePacket;
602 DeviceExtension->KeyboardHook.CallContext = DeviceExtension;
603 IoQueueWorkItem(WorkItem,
604 i8042SendHookWorkItem,
605 DelayedWorkQueue,
606 WorkItemData);
607 Status = STATUS_PENDING;
608 break;
609
610 cleanup:
611 if (DeviceExtension->KeyboardBuffer)
612 ExFreePoolWithTag(DeviceExtension->KeyboardBuffer, I8042PRT_TAG);
613 if (DeviceExtension->PowerWorkItem)
614 IoFreeWorkItem(DeviceExtension->PowerWorkItem);
615 if (DeviceExtension->DebugWorkItem)
616 IoFreeWorkItem(DeviceExtension->DebugWorkItem);
617 if (WorkItem)
618 IoFreeWorkItem(WorkItem);
619 if (WorkItemData)
620 ExFreePoolWithTag(WorkItemData, I8042PRT_TAG);
621 break;
622 }
623 case IOCTL_INTERNAL_KEYBOARD_DISCONNECT:
624 {
625 TRACE_(I8042PRT, "IRP_MJ_INTERNAL_DEVICE_CONTROL / IOCTL_INTERNAL_KEYBOARD_DISCONNECT\n");
626 /* MSDN says that operation is to implemented.
627 * To implement it, we just have to do:
628 * DeviceExtension->KeyboardData.ClassService = NULL;
629 */
630 Status = STATUS_NOT_IMPLEMENTED;
631 break;
632 }
633 case IOCTL_INTERNAL_I8042_HOOK_KEYBOARD:
634 {
635 TRACE_(I8042PRT, "IRP_MJ_INTERNAL_DEVICE_CONTROL / IOCTL_INTERNAL_I8042_HOOK_KEYBOARD\n");
636 /* Nothing to do here */
637 Status = STATUS_SUCCESS;
638 break;
639 }
640 case IOCTL_KEYBOARD_QUERY_ATTRIBUTES:
641 {
642 PKEYBOARD_ATTRIBUTES KeyboardAttributes;
643
644 /* FIXME: KeyboardAttributes are not initialized anywhere */
645 TRACE_(I8042PRT, "IRP_MJ_INTERNAL_DEVICE_CONTROL / IOCTL_KEYBOARD_QUERY_ATTRIBUTES\n");
646 if (Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(KEYBOARD_ATTRIBUTES))
647 {
648 Status = STATUS_BUFFER_TOO_SMALL;
649 break;
650 }
651
652 KeyboardAttributes = Irp->AssociatedIrp.SystemBuffer;
653 *KeyboardAttributes = DeviceExtension->KeyboardAttributes;
654
655 Irp->IoStatus.Information = sizeof(KEYBOARD_ATTRIBUTES);
656 Status = STATUS_SUCCESS;
657 break;
658 }
659 case IOCTL_KEYBOARD_QUERY_TYPEMATIC:
660 {
661 DPRINT1("IOCTL_KEYBOARD_QUERY_TYPEMATIC not implemented\n");
662 Status = STATUS_NOT_IMPLEMENTED;
663 break;
664 }
665 case IOCTL_KEYBOARD_SET_TYPEMATIC:
666 {
667 DPRINT1("IOCTL_KEYBOARD_SET_TYPEMATIC not implemented\n");
668 Status = STATUS_NOT_IMPLEMENTED;
669 break;
670 }
671 case IOCTL_KEYBOARD_QUERY_INDICATOR_TRANSLATION:
672 {
673 TRACE_(I8042PRT, "IRP_MJ_INTERNAL_DEVICE_CONTROL / IOCTL_KEYBOARD_QUERY_INDICATOR_TRANSLATION\n");
674
675 /* We should check the UnitID, but it's kind of pointless as
676 * all keyboards are supposed to have the same one
677 */
678 if (Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(LOCAL_KEYBOARD_INDICATOR_TRANSLATION))
679 {
680 Status = STATUS_BUFFER_TOO_SMALL;
681 }
682 else
683 {
684 RtlCopyMemory(
685 Irp->AssociatedIrp.SystemBuffer,
686 &IndicatorTranslation,
687 sizeof(LOCAL_KEYBOARD_INDICATOR_TRANSLATION));
688 Irp->IoStatus.Information = sizeof(LOCAL_KEYBOARD_INDICATOR_TRANSLATION);
689 Status = STATUS_SUCCESS;
690 }
691 break;
692 }
693 case IOCTL_KEYBOARD_QUERY_INDICATORS:
694 {
695 TRACE_(I8042PRT, "IRP_MJ_INTERNAL_DEVICE_CONTROL / IOCTL_KEYBOARD_QUERY_INDICATORS\n");
696
697 if (Stack->Parameters.DeviceIoControl.InputBufferLength < sizeof(KEYBOARD_INDICATOR_PARAMETERS))
698 {
699 Status = STATUS_BUFFER_TOO_SMALL;
700 }
701 else
702 {
703 RtlCopyMemory(
704 Irp->AssociatedIrp.SystemBuffer,
705 &DeviceExtension->KeyboardIndicators,
706 sizeof(KEYBOARD_INDICATOR_PARAMETERS));
707 Irp->IoStatus.Information = sizeof(KEYBOARD_INDICATOR_PARAMETERS);
708 Status = STATUS_SUCCESS;
709 }
710 break;
711 }
712 case IOCTL_KEYBOARD_SET_INDICATORS:
713 {
714 TRACE_(I8042PRT, "IRP_MJ_INTERNAL_DEVICE_CONTROL / IOCTL_KEYBOARD_SET_INDICATORS\n");
715
716 if (Stack->Parameters.DeviceIoControl.InputBufferLength < sizeof(KEYBOARD_INDICATOR_PARAMETERS))
717 {
718 Status = STATUS_BUFFER_TOO_SMALL;
719 }
720 else
721 {
722 RtlCopyMemory(
723 &DeviceExtension->KeyboardIndicators,
724 Irp->AssociatedIrp.SystemBuffer,
725 sizeof(KEYBOARD_INDICATOR_PARAMETERS));
726 Status = STATUS_PENDING;
727 IoMarkIrpPending(Irp);
728 IoStartPacket(DeviceObject, Irp, NULL, NULL);
729 }
730 break;
731 }
732 default:
733 {
734 ERR_(I8042PRT, "IRP_MJ_INTERNAL_DEVICE_CONTROL / unknown ioctl code 0x%lx\n",
735 Stack->Parameters.DeviceIoControl.IoControlCode);
736 ASSERT(FALSE);
737 return ForwardIrpAndForget(DeviceObject, Irp);
738 }
739 }
740
741 Irp->IoStatus.Status = Status;
742 if (Status != STATUS_PENDING)
743 IoCompleteRequest(Irp, IO_NO_INCREMENT);
744 return Status;
745 }
746
747 /*
748 * Call the customization hook. The ToReturn parameter is about whether
749 * we should go on with the interrupt. The return value is what
750 * we should return (indicating to the system whether someone else
751 * should try to handle the interrupt)
752 */
753 static BOOLEAN
754 i8042KbdCallIsrHook(
755 IN PI8042_KEYBOARD_EXTENSION DeviceExtension,
756 IN UCHAR Status,
757 IN UCHAR Input,
758 OUT PBOOLEAN ToReturn)
759 {
760 BOOLEAN HookReturn, HookContinue;
761
762 HookContinue = FALSE;
763
764 if (DeviceExtension->KeyboardHook.IsrRoutine)
765 {
766 HookReturn = DeviceExtension->KeyboardHook.IsrRoutine(
767 DeviceExtension->KeyboardHook.Context,
768 DeviceExtension->KeyboardBuffer + DeviceExtension->KeysInBuffer,
769 &DeviceExtension->Common.PortDeviceExtension->Packet,
770 Status,
771 &Input,
772 &HookContinue,
773 &DeviceExtension->KeyboardScanState);
774
775 if (!HookContinue)
776 {
777 *ToReturn = HookReturn;
778 return TRUE;
779 }
780 }
781 return FALSE;
782 }
783
784 BOOLEAN NTAPI
785 i8042KbdInterruptService(
786 IN PKINTERRUPT Interrupt,
787 PVOID Context)
788 {
789 PI8042_KEYBOARD_EXTENSION DeviceExtension;
790 PPORT_DEVICE_EXTENSION PortDeviceExtension;
791 PKEYBOARD_INPUT_DATA InputData;
792 ULONG Counter;
793 UCHAR PortStatus = 0, Output = 0;
794 BOOLEAN ToReturn = FALSE;
795 NTSTATUS Status;
796
797 UNREFERENCED_PARAMETER(Interrupt);
798
799 __analysis_assume(Context != NULL);
800 DeviceExtension = Context;
801 PortDeviceExtension = DeviceExtension->Common.PortDeviceExtension;
802 InputData = DeviceExtension->KeyboardBuffer + DeviceExtension->KeysInBuffer;
803 Counter = PortDeviceExtension->Settings.PollStatusIterations;
804
805 while (Counter)
806 {
807 Status = i8042ReadStatus(PortDeviceExtension, &PortStatus);
808 if (!NT_SUCCESS(Status))
809 {
810 WARN_(I8042PRT, "i8042ReadStatus() failed with status 0x%08lx\n", Status);
811 return FALSE;
812 }
813 Status = i8042ReadKeyboardData(PortDeviceExtension, &Output);
814 if (NT_SUCCESS(Status))
815 break;
816 KeStallExecutionProcessor(1);
817 Counter--;
818 }
819 if (Counter == 0)
820 {
821 WARN_(I8042PRT, "Spurious i8042 keyboard interrupt\n");
822 return FALSE;
823 }
824
825 INFO_(I8042PRT, "Got: 0x%02x\n", Output);
826
827 if (PortDeviceExtension->Settings.CrashOnCtrlScroll)
828 {
829 /* Test for CTRL + SCROLL LOCK twice */
830 static const UCHAR ScanCodes[] = { 0x1d, 0x46, 0xc6, 0x46, 0 };
831
832 if (Output == ScanCodes[DeviceExtension->ComboPosition])
833 {
834 DeviceExtension->ComboPosition++;
835 if (ScanCodes[DeviceExtension->ComboPosition] == 0)
836 KeBugCheck(MANUALLY_INITIATED_CRASH);
837 }
838 else if (Output == 0xfa)
839 {
840 /* Ignore ACK */
841 }
842 else if (Output == ScanCodes[0])
843 DeviceExtension->ComboPosition = 1;
844 else
845 DeviceExtension->ComboPosition = 0;
846
847 /* Test for TAB + key combination */
848 if (InputData->MakeCode == 0x0F)
849 DeviceExtension->TabPressed = !(InputData->Flags & KEY_BREAK);
850 else if (DeviceExtension->TabPressed)
851 {
852 DeviceExtension->TabPressed = FALSE;
853
854 /* Check which action to do */
855 if (InputData->MakeCode == 0x25)
856 {
857 /* k - Breakpoint */
858 DbgBreakPoint();
859 }
860 else if (InputData->MakeCode == 0x30)
861 {
862 /* b - Bugcheck */
863 KeBugCheck(MANUALLY_INITIATED_CRASH);
864 }
865 else
866 {
867 /* Send request to the kernel debugger.
868 * Unknown requests will be ignored. */
869 KdSystemDebugControl(' soR',
870 (PVOID)(ULONG_PTR)InputData->MakeCode,
871 0,
872 NULL,
873 0,
874 NULL,
875 KernelMode);
876 }
877 }
878 }
879
880 if (i8042KbdCallIsrHook(DeviceExtension, PortStatus, Output, &ToReturn))
881 return ToReturn;
882
883 if (i8042PacketIsr(PortDeviceExtension, Output))
884 {
885 if (PortDeviceExtension->PacketComplete)
886 {
887 TRACE_(I8042PRT, "Packet complete\n");
888 KeInsertQueueDpc(&DeviceExtension->DpcKeyboard, NULL, NULL);
889 }
890 TRACE_(I8042PRT, "Irq eaten by packet\n");
891 return TRUE;
892 }
893
894 TRACE_(I8042PRT, "Irq is keyboard input\n");
895
896 if (DeviceExtension->KeyboardScanState == Normal)
897 {
898 switch (Output)
899 {
900 case 0xe0:
901 DeviceExtension->KeyboardScanState = GotE0;
902 return TRUE;
903 case 0xe1:
904 DeviceExtension->KeyboardScanState = GotE1;
905 return TRUE;
906 default:
907 break;
908 }
909 }
910
911 /* Update InputData */
912 InputData->Flags = 0;
913 switch (DeviceExtension->KeyboardScanState)
914 {
915 case GotE0:
916 InputData->Flags |= KEY_E0;
917 break;
918 case GotE1:
919 InputData->Flags |= KEY_E1;
920 break;
921 default:
922 break;
923 }
924 DeviceExtension->KeyboardScanState = Normal;
925 if (Output & 0x80)
926 InputData->Flags |= KEY_BREAK;
927 else
928 InputData->Flags |= KEY_MAKE;
929 InputData->MakeCode = Output & 0x7f;
930 InputData->Reserved = 0;
931
932 DeviceExtension->KeyboardHook.QueueKeyboardPacket(DeviceExtension->KeyboardHook.CallContext);
933
934 return TRUE;
935 }