Little KDB update ;-) If you have any problems and/or questions let me know. I hope...
[reactos.git] / reactos / ntoskrnl / dbg / kdb_keyboard.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/dbg/kdb_keyboard.c
6 * PURPOSE: Keyboard driver
7 *
8 * PROGRAMMERS: Victor Kirhenshtein (sauros@iname.com)
9 * Jason Filby (jasonfilby@yahoo.com)
10 */
11
12 /* INCLUDES ****************************************************************/
13
14 #include <ntoskrnl.h>
15 #define NDEBUG
16 #include <debug.h>
17
18 #if 1
19
20 #define KBD_STATUS_REG 0x64
21 #define KBD_CNTL_REG 0x64
22 #define KBD_DATA_REG 0x60
23
24 #define KBD_DISABLE_MOUSE 0xA7
25 #define KBD_ENABLE_MOUSE 0xA8
26
27 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
28
29 #define kbd_write_command(cmd) WRITE_PORT_UCHAR((PUCHAR)KBD_STATUS_REG,cmd)
30 #define kbd_read_input() READ_PORT_UCHAR((PUCHAR)KBD_DATA_REG)
31 #define kbd_read_status() READ_PORT_UCHAR((PUCHAR)KBD_STATUS_REG)
32
33 static unsigned char keyb_layout[2][128] =
34 {
35 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
36 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
37 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
38 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
39 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
40 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
41 "\r\000/" /* 0x60 - 0x6f */
42 ,
43 "\000\033!@#$%^&*()_+\177\t" /* 0x00 - 0x0f */
44 "QWERTYUIOP{}\r\000AS" /* 0x10 - 0x1f */
45 "DFGHJKL:\"`\000\\ZXCV" /* 0x20 - 0x2f */
46 "BNM<>?\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
47 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
48 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
49 "\r\000/" /* 0x60 - 0x6f */
50 };
51
52 typedef BYTE byte_t;
53
54 VOID KbdEnableMouse()
55 {
56 kbd_write_command(KBD_ENABLE_MOUSE);
57 }
58
59 VOID KbdDisableMouse()
60 {
61 kbd_write_command(KBD_DISABLE_MOUSE);
62 }
63
64 CHAR
65 KdbpTryGetCharKeyboard(PULONG ScanCode)
66 {
67 static byte_t last_key = 0;
68 static byte_t shift = 0;
69 char c;
70 while(1) {
71 unsigned char status = kbd_read_status();
72 while (status & KBD_STAT_OBF) {
73 byte_t scancode;
74 scancode = kbd_read_input();
75 /* check for SHIFT-keys */
76 if (((scancode & 0x7F) == 42) || ((scancode & 0x7F) == 54))
77 {
78 shift = !(scancode & 0x80);
79 continue;
80 }
81 /* ignore all other RELEASED-codes */
82 if (scancode & 0x80)
83 last_key = 0;
84 else if (last_key != scancode)
85 {
86 //printf("kbd: %d, %d, %c\n", scancode, last_key, keyb_layout[shift][scancode]);
87 last_key = scancode;
88 c = keyb_layout[shift][scancode];
89 *ScanCode = scancode;
90 if (c > 0) return c;
91 }
92 }
93 }
94 }
95
96 #endif
97
98 #if 0
99
100 /* GLOBALS *******************************************************************/
101
102 /*
103 * Keyboard I/O ports.
104 */
105 #define K_RDWR 0x60 /* keyboard data & cmds (read/write) */
106 #define K_STATUS 0x64 /* keybd status (read-only) */
107 #define K_CMD 0x64 /* keybd ctlr command (write-only) */
108
109 /*
110 * Bit definitions for K_STATUS port.
111 */
112 #define K_OBUF_FUL 0x01 /* output (from keybd) buffer full */
113 #define K_IBUF_FUL 0x02 /* input (to keybd) buffer full */
114 #define K_SYSFLAG 0x04 /* "System Flag" */
115 #define K_CMD_DATA 0x08 /* 1 = input buf has cmd, 0 = data */
116 #define K_KBD_INHIBIT 0x10 /* 0 if keyboard inhibited */
117 #define K_AUX_OBUF_FUL 0x20 /* 1 = obuf holds aux device data */
118 #define K_TIMEOUT 0x40 /* timout error flag */
119 #define K_PARITY_ERROR 0x80 /* parity error flag */
120
121 /*
122 * Keyboard controller commands (sent to K_CMD port).
123 */
124 #define KC_CMD_READ 0x20 /* read controller command byte */
125 #define KC_CMD_WRITE 0x60 /* write controller command byte */
126 #define KC_CMD_DIS_AUX 0xa7 /* disable auxiliary device */
127 #define KC_CMD_ENB_AUX 0xa8 /* enable auxiliary device */
128 #define KC_CMD_TEST_AUX 0xa9 /* test auxiliary device interface */
129 #define KC_CMD_SELFTEST 0xaa /* keyboard controller self-test */
130 #define KC_CMD_TEST 0xab /* test keyboard interface */
131 #define KC_CMD_DUMP 0xac /* diagnostic dump */
132 #define KC_CMD_DISABLE 0xad /* disable keyboard */
133 #define KC_CMD_ENABLE 0xae /* enable keyboard */
134 #define KC_CMD_RDKBD 0xc4 /* read keyboard ID */
135 #define KC_CMD_WIN 0xd0 /* read output port */
136 #define KC_CMD_WOUT 0xd1 /* write output port */
137 #define KC_CMD_ECHO 0xee /* used for diagnostic testing */
138 #define KC_CMD_PULSE 0xff /* pulse bits 3-0 based on low nybble */
139
140 /*
141 * Keyboard commands (send to K_RDWR).
142 */
143 #define K_CMD_LEDS 0xed /* set status LEDs (caps lock, etc.) */
144 #define K_CMD_TYPEMATIC 0xf3 /* set key repeat and delay */
145
146 /*
147 * Bit definitions for controller command byte (sent following
148 * KC_CMD_WRITE command).
149 *
150 * Bits 0x02 and 0x80 unused, always set to 0.
151 */
152 #define K_CB_ENBLIRQ 0x01 /* enable data-ready intrpt */
153 #define K_CB_SETSYSF 0x04 /* Set System Flag */
154 #define K_CB_INHBOVR 0x08 /* Inhibit Override */
155 #define K_CB_DISBLE 0x10 /* disable keyboard */
156 #define K_CB_IGNPARITY 0x20 /* ignore parity from keyboard */
157 #define K_CB_SCAN 0x40 /* standard scan conversion */
158
159 /*
160 * Bit definitions for "Indicator Status Byte" (sent after a
161 * K_CMD_LEDS command). If the bit is on, the LED is on. Undefined
162 * bit positions must be 0.
163 */
164 #define K_LED_SCRLLK 0x1 /* scroll lock */
165 #define K_LED_NUMLK 0x2 /* num lock */
166 #define K_LED_CAPSLK 0x4 /* caps lock */
167
168 /*
169 * Bit definitions for "Miscellaneous port B" (K_PORTB).
170 */
171 /* read/write */
172 #define K_ENABLETMR2 0x01 /* enable output from timer 2 */
173 #define K_SPKRDATA 0x02 /* direct input to speaker */
174 #define K_ENABLEPRTB 0x04 /* "enable" port B */
175 #define K_EIOPRTB 0x08 /* enable NMI on parity error */
176 /* read-only */
177 #define K_REFRESHB 0x10 /* refresh flag from INLTCONT PAL */
178 #define K_OUT2B 0x20 /* timer 2 output */
179 #define K_ICKB 0x40 /* I/O channel check (parity error) */
180
181 /*
182 * Bit definitions for the keyboard controller's output port.
183 */
184 #define KO_SYSRESET 0x01 /* processor reset */
185 #define KO_GATE20 0x02 /* A20 address line enable */
186 #define KO_AUX_DATA_OUT 0x04 /* output data to auxiliary device */
187 #define KO_AUX_CLOCK 0x08 /* auxiliary device clock */
188 #define KO_OBUF_FUL 0x10 /* keyboard output buffer full */
189 #define KO_AUX_OBUF_FUL 0x20 /* aux device output buffer full */
190 #define KO_CLOCK 0x40 /* keyboard clock */
191 #define KO_DATA_OUT 0x80 /* output data to keyboard */
192
193 /*
194 * Keyboard return codes.
195 */
196 #define K_RET_RESET_DONE 0xaa /* BAT complete */
197 #define K_RET_ECHO 0xee /* echo after echo command */
198 #define K_RET_ACK 0xfa /* ack */
199 #define K_RET_RESET_FAIL 0xfc /* BAT error */
200 #define K_RET_RESEND 0xfe /* resend request */
201
202 #define SHIFT -1
203 #define CTRL -2
204 #define META -3
205
206 static char keymap[128][2] = {
207 {0}, /* 0 */
208 {27, 27}, /* 1 - ESC */
209 {'1', '!'}, /* 2 */
210 {'2', '@'},
211 {'3', '#'},
212 {'4', '$'},
213 {'5', '%'},
214 {'6', '^'},
215 {'7', '&'},
216 {'8', '*'},
217 {'9', '('},
218 {'0', ')'},
219 {'-', '_'},
220 {'=', '+'},
221 {8, 8}, /* 14 - Backspace */
222 {'\t', '\t'}, /* 15 */
223 {'q', 'Q'},
224 {'w', 'W'},
225 {'e', 'E'},
226 {'r', 'R'},
227 {'t', 'T'},
228 {'y', 'Y'},
229 {'u', 'U'},
230 {'i', 'I'},
231 {'o', 'O'},
232 {'p', 'P'},
233 {'[', '{'},
234 {']', '}'}, /* 27 */
235 {'\r', '\r'}, /* 28 - Enter */
236 {CTRL, CTRL}, /* 29 - Ctrl */
237 {'a', 'A'}, /* 30 */
238 {'s', 'S'},
239 {'d', 'D'},
240 {'f', 'F'},
241 {'g', 'G'},
242 {'h', 'H'},
243 {'j', 'J'},
244 {'k', 'K'},
245 {'l', 'L'},
246 {';', ':'},
247 {'\'', '"'}, /* 40 */
248 {'`', '~'}, /* 41 */
249 {SHIFT, SHIFT}, /* 42 - Left Shift */
250 {'\\', '|'}, /* 43 */
251 {'z', 'Z'}, /* 44 */
252 {'x', 'X'},
253 {'c', 'C'},
254 {'v', 'V'},
255 {'b', 'B'},
256 {'n', 'N'},
257 {'m', 'M'},
258 {',', '<'},
259 {'.', '>'},
260 {'/', '?'}, /* 53 */
261 {SHIFT, SHIFT}, /* 54 - Right Shift */
262 {0, 0}, /* 55 - Print Screen */
263 {META, META}, /* 56 - Alt */
264 {' ', ' '}, /* 57 - Space bar */
265 {0, 0}, /* 58 - Caps Lock */
266 {0, 0}, /* 59 - F1 */
267 {0, 0}, /* 60 - F2 */
268 {0, 0}, /* 61 - F3 */
269 {0, 0}, /* 62 - F4 */
270 {0, 0}, /* 63 - F5 */
271 {0, 0}, /* 64 - F6 */
272 {0, 0}, /* 65 - F7 */
273 {0, 0}, /* 66 - F8 */
274 {0, 0}, /* 67 - F9 */
275 {0, 0}, /* 68 - F10 */
276 {0, 0}, /* 69 - Num Lock */
277 {0, 0}, /* 70 - Scroll Lock */
278 {'7', '7'}, /* 71 - Numeric keypad 7 */
279 {'8', '8'}, /* 72 - Numeric keypad 8 */
280 {'9', '9'}, /* 73 - Numeric keypad 9 */
281 {'-', '-'}, /* 74 - Numeric keypad '-' */
282 {'4', '4'}, /* 75 - Numeric keypad 4 */
283 {'5', '5'}, /* 76 - Numeric keypad 5 */
284 {'6', '6'}, /* 77 - Numeric keypad 6 */
285 {'+', '+'}, /* 78 - Numeric keypad '+' */
286 {'1', '1'}, /* 79 - Numeric keypad 1 */
287 {'2', '2'}, /* 80 - Numeric keypad 2 */
288 {'3', '3'}, /* 81 - Numeric keypad 3 */
289 {'0', '0'}, /* 82 - Numeric keypad 0 */
290 {'.', '.'}, /* 83 - Numeric keypad '.' */
291 };
292
293 /* FUNCTIONS *****************************************************************/
294
295 /*
296 * Quick poll for a pending input character.
297 * Returns a character if available, -1 otherwise. This routine can return
298 * false negatives in the following cases:
299 *
300 * - a valid character is in transit from the keyboard when called
301 * - a key release is received (from a previous key press)
302 * - a SHIFT key press is received (shift state is recorded however)
303 * - a key press for a multi-character sequence is received
304 *
305 * Yes, this is horrible.
306 */
307 ULONG
308 KdbpTryGetCharKeyboard(VOID)
309 {
310 static unsigned shift_state, ctrl_state, meta_state;
311 unsigned scan_code, ch;
312
313 /* See if a scan code is ready, returning if none. */
314 if ((READ_PORT_UCHAR((PUCHAR)K_STATUS) & K_OBUF_FUL) == 0) {
315 return -1;
316 }
317 scan_code = READ_PORT_UCHAR((PUCHAR)K_RDWR);
318
319 /* Handle key releases - only release of SHIFT is important. */
320 if (scan_code & 0x80) {
321 scan_code &= 0x7f;
322 if (keymap[scan_code][0] == SHIFT)
323 shift_state = 0;
324 else if (keymap[scan_code][0] == CTRL)
325 ctrl_state = 0;
326 else if (keymap[scan_code][0] == META)
327 meta_state = 0;
328 ch = -1;
329 } else {
330 /* Translate the character through the keymap. */
331 ch = keymap[scan_code][shift_state] | meta_state;
332 if (ch == SHIFT) {
333 shift_state = 1;
334 ch = -1;
335 } else if (ch == CTRL) {
336 ctrl_state = 1;
337 ch = -1;
338 } else if (ch == META) {
339 meta_state = 0200;
340 ch = -1;
341 } else if (ch == 0)
342 ch = -1;
343 else if (ctrl_state)
344 ch = (keymap[scan_code][1] - '@') | meta_state;
345 }
346
347 return ch;
348 }
349
350 #endif