3 Copyright (c) 1998-2001 Klaus P. Gerlicher
11 hooking of kernel internal keyboard interrupt handler
20 Reactos Port: Eugene Ingerman
25 15-Nov-2000: general cleanup of source files
26 12/1/2001 reactos port
30 This file may be distributed under the terms of the GNU Public License.
34 ////////////////////////////////////////////////////
40 //#include <asm/system.h>
42 #include <ddk/ntddkbd.h>
43 #include <ddk/ntdd8042.h>
45 ////////////////////////////////////////////////////
49 static PUCHAR pPatchAddress;
50 static ULONG ulOldOffset = 0;
51 static ULONG ulKeyPatchFlags;
53 void (*old_handle_scancode)(UCHAR,int);
55 UCHAR ucBreakKey = 'd'; // key that will break into debugger in combination with CTRL
57 ////////////////////////////////////////////////////
61 //***********************************************************************************
62 // PiceKbdIsr - keyboard isr hook routine.
63 // IsrContext - context that we passed to keyboard driver in internal iocontrol
64 // pCurrentInput, pCurrentOutput - not implemented yet
65 // StatusByte - keyboard status register
66 // pByte - pointer to the byte read from keyboard data port. can be changed.
67 // pContinueProcessing - should keyboard driver continue processing this byte.
68 //***********************************************************************************
71 PKEYBOARD_INPUT_DATA pCurrentInput,
72 POUTPUT_PACKET pCurrentOutput,
75 PBOOLEAN pContinueProcessing,
76 PKEYBOARD_SCAN_STATE pScanState
79 static BOOLEAN bControl = FALSE;
80 BOOLEAN bForward=TRUE; // should we let keyboard driver process this keystroke
81 BOOLEAN isDown=!(*pByte & 0x80);
82 UCHAR ucKey = *pByte & 0x7f;
86 // BUG?? should protect with spinlock since bControl is static.
87 DPRINT((0,"PiceKbdIsr(pByte: %x, val: %x,%u)\n",pByte,*pByte,isDown));
88 DPRINT((0,"PiceKbdIsr(1): bControl = %u bForward = %u bEnterNow = %u\n",bControl,bForward,bEnterNow));
92 DPRINT((0,"bControl: %x, ucKey: %x, breakkey: %x\n", bControl, ucKey, AsciiToScan(ucBreakKey)));
98 else if(bControl==TRUE && ucKey==AsciiToScan(ucBreakKey)) // CTRL-D
100 // fake a CTRL-D release call
103 // simulate an initial break
107 pushl $returnpoint\n\t \
108 pushl $" STR(REASON_CTRLF) "\n\t \
109 jmp NewInt31Handler\n\t \
111 *pByte = 0x1d | 0x80 | 0x7f;
114 else if((ucKey == 66|| ucKey == 68) && bStepping)
127 else if((ucKey == 66|| ucKey == 68) && bStepping)
132 *pContinueProcessing = bForward;
133 DPRINT((5,"*pContinueProcessing: %d\n", *pContinueProcessing));
138 //***********************************************************************************
139 // PiceSendIoctl - send internal_io_control to the driver
140 // Target - Device Object that receives control request
142 // InputBuffer - Type3Buffer will be pointing here
143 // InputBufferLength - length of inputbuffer
144 //***********************************************************************************
145 NTSTATUS PiceSendIoctl(PDEVICE_OBJECT Target, ULONG Ioctl,
146 PVOID InputBuffer, ULONG InputBufferLength)
149 NTSTATUS status = STATUS_SUCCESS;
150 IO_STATUS_BLOCK iosb;
153 KeInitializeEvent(&event,
158 if (NULL == (irp = IoBuildDeviceIoControlRequest(Ioctl,
167 DPRINT((0,"PiceSendIoctl: STATUS_INSUFFICIENT_RESOURCES\n"));
168 return STATUS_INSUFFICIENT_RESOURCES;
171 status = IoCallDriver(Target, irp);
173 if (STATUS_PENDING == status) {
175 status = KeWaitForSingleObject(&event,
181 ASSERT(STATUS_SUCCESS == status);
182 status = iosb.Status;
184 DPRINT((0,"PiceSendIoctl: status: %d\n",NT_SUCCESS(status)));
188 //**************************************************
189 // PatchKeyboardDriver - set keyboard driver hook.
190 // We use interface supported by standard keyboard drivers.
191 //**************************************************
192 BOOLEAN PatchKeyboardDriver(void)
194 PINTERNAL_I8042_HOOK_KEYBOARD phkData;
195 //When we have i8042 driver this should be changed!!!!!!!
196 UNICODE_STRING DevName = UNICODE_STRING_INITIALIZER(L"\\Device\\Keyboard");
197 PDEVICE_OBJECT kbdDevice = NULL;
198 PFILE_OBJECT FO = NULL;
203 //Get pointer to keyboard device
204 if( !NT_SUCCESS( status = IoGetDeviceObjectPointer( &DevName, FILE_READ_ACCESS, &FO, &kbdDevice ) ) )
206 DPRINT((0,"PatchKeyboardDriver: IoGetDeviceObjectPointer status: %x\n", status));
209 phkData = ExAllocatePool( PagedPool, sizeof( INTERNAL_I8042_HOOK_KEYBOARD ) );
210 RtlZeroMemory( phkData, sizeof( INTERNAL_I8042_HOOK_KEYBOARD ) );
212 phkData->IsrRoutine = (PI8042_KEYBOARD_ISR) PiceKbdIsr;
213 phkData->Context = (PVOID) NULL; //DeviceObject;
215 //call keyboard device internal io control to hook keyboard input stream
216 status = PiceSendIoctl( kbdDevice, IOCTL_INTERNAL_I8042_HOOK_KEYBOARD,
217 phkData, sizeof( INTERNAL_I8042_HOOK_KEYBOARD ) );
218 DPRINT((0,"PatchKeyboardDriver: PiceSendIoctl status: %x\n", status));
221 ObDereferenceObject(FO);
226 return NT_SUCCESS(status);
229 void RestoreKeyboardDriver(void)
232 DbgPrint("RestoreKeyboardDriver: Not Implemented yet!!!\n");