2 * Kernel Debugger Architecture Dependent Console I/O handler
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (c) 1999-2006 Silicon Graphics, Inc. All Rights Reserved.
11 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/console.h>
15 #include <linux/ctype.h>
16 #include <linux/keyboard.h>
17 #include <linux/serial.h>
18 #include <linux/serial_reg.h>
20 #include <linux/kdb.h>
21 #include <linux/kdbprivate.h>
23 #if defined(CONFIG_SERIAL_8250_CONSOLE) || defined(CONFIG_SERIAL_SGI_L1_CONSOLE)
24 #define HAVE_KDBA_SERIAL_CONSOLE
27 /* from include/linux/pc_keyb.h on 2.4 */
28 #define KBD_STATUS_REG 0x64 /* Status register (R) */
29 #define KBD_DATA_REG 0x60 /* Keyboard data register (R/W) */
30 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
31 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
32 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
33 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
35 #ifdef CONFIG_VT_CONSOLE
36 #define KDB_BLINK_LED 1
43 /* support up to 8 USB keyboards (probably excessive, but...) */
44 #define KDB_USB_NUM_KEYBOARDS 8
45 struct kdb_usb_kbd_info kdb_usb_kbds[KDB_USB_NUM_KEYBOARDS];
46 EXPORT_SYMBOL(kdb_usb_kbds);
48 extern int kdb_no_usb;
50 static unsigned char kdb_usb_keycode[256] = {
51 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
52 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
53 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
54 27, 43, 84, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
55 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
56 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
57 72, 73, 82, 83, 86,127,116,117, 85, 89, 90, 91, 92, 93, 94, 95,
58 120,121,122,123,134,138,130,132,128,129,131,137,133,135,136,113,
59 115,114, 0, 0, 0,124, 0,181,182,183,184,185,186,187,188,189,
60 190,191,192,193,194,195,196,197,198, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
66 150,158,159,128,136,177,178,176,142,152,173,140
70 * kdb_usb_keyboard_attach()
71 * Attach a USB keyboard to kdb.
74 kdb_usb_keyboard_attach(struct urb *urb, unsigned char *buffer,
75 void *poll_func, void *compl_func,
76 kdb_hc_keyboard_attach_t kdb_hc_keyboard_attach,
77 kdb_hc_keyboard_detach_t kdb_hc_keyboard_detach,
78 unsigned int bufsize, struct urb *hid_urb)
87 * Search through the array of KDB USB keyboards (kdb_usb_kbds)
88 * looking for a free index. If found, assign the keyboard to
92 for (i = 0; i < KDB_USB_NUM_KEYBOARDS; i++) {
93 if (kdb_usb_kbds[i].urb) /* index is already assigned */
96 /* found a free array index */
97 kdb_usb_kbds[i].urb = urb;
98 kdb_usb_kbds[i].buffer = buffer;
99 kdb_usb_kbds[i].poll_func = poll_func;
101 kdb_usb_kbds[i].kdb_hc_urb_complete = compl_func;
102 kdb_usb_kbds[i].kdb_hc_keyboard_attach = kdb_hc_keyboard_attach;
103 kdb_usb_kbds[i].kdb_hc_keyboard_detach = kdb_hc_keyboard_detach;
105 /* USB Host Controller specific Keyboadr attach callback.
106 * Currently only UHCI has this callback.
108 if (kdb_usb_kbds[i].kdb_hc_keyboard_attach)
109 kdb_usb_kbds[i].kdb_hc_keyboard_attach(i, bufsize);
112 rc = 0; /* success */
119 EXPORT_SYMBOL_GPL (kdb_usb_keyboard_attach);
122 * kdb_usb_keyboard_detach()
123 * Detach a USB keyboard from kdb.
126 kdb_usb_keyboard_detach(struct urb *urb)
135 * Search through the array of KDB USB keyboards (kdb_usb_kbds)
136 * looking for the index with the matching URB. If found,
137 * clear the array index.
140 for (i = 0; i < KDB_USB_NUM_KEYBOARDS; i++) {
141 if (kdb_usb_kbds[i].urb != urb)
144 /* USB Host Controller specific Keyboard detach callback.
145 * Currently only UHCI has this callback.
147 if (kdb_usb_kbds[i].kdb_hc_keyboard_detach)
148 kdb_usb_kbds[i].kdb_hc_keyboard_detach(urb, i);
151 /* found it, clear the index */
152 kdb_usb_kbds[i].urb = NULL;
153 kdb_usb_kbds[i].buffer = NULL;
154 kdb_usb_kbds[i].poll_func = NULL;
155 kdb_usb_kbds[i].caps_lock = 0;
156 kdb_usb_kbds[i].hid_urb = NULL;
158 rc = 0; /* success */
165 EXPORT_SYMBOL_GPL (kdb_usb_keyboard_detach);
169 * This function drives the USB attached keyboards.
170 * Fetch the USB scancode and decode it.
177 unsigned char keycode, spec;
178 extern u_short plain_map[], shift_map[], ctrl_map[];
184 * Loop through all the USB keyboard(s) and return
185 * the first character obtained from them.
188 for (i = 0; i < KDB_USB_NUM_KEYBOARDS; i++) {
189 /* skip uninitialized keyboard array entries */
190 if (!kdb_usb_kbds[i].urb || !kdb_usb_kbds[i].buffer ||
191 !kdb_usb_kbds[i].poll_func)
195 ret = (*kdb_usb_kbds[i].poll_func)(kdb_usb_kbds[i].urb);
197 if (ret == -EBUSY && kdb_usb_kbds[i].poll_ret != -EBUSY)
198 kdb_printf("NOTICE: USB HC driver BUSY. "
199 "USB keyboard has been disabled.\n");
201 kdb_usb_kbds[i].poll_ret = ret;
203 if (ret < 0) /* error or no characters, try the next kbd */
206 spec = kdb_usb_kbds[i].buffer[0];
207 keycode = kdb_usb_kbds[i].buffer[2];
208 kdb_usb_kbds[i].buffer[0] = (char)0;
209 kdb_usb_kbds[i].buffer[2] = (char)0;
211 if(kdb_usb_kbds[i].buffer[3]) {
212 kdb_usb_kbds[i].buffer[3] = (char)0;
216 /* A normal key is pressed, decode it */
218 keycode = kdb_usb_keycode[keycode];
220 /* 2 Keys pressed at one time ? */
221 if (spec && keycode) {
225 case 0x20: /* Shift */
226 return shift_map[keycode];
228 case 0x10: /* Ctrl */
229 return ctrl_map[keycode];
234 } else if (keycode) { /* If only one key pressed */
237 case 0x1C: /* Enter */
240 case 0x3A: /* Capslock */
241 kdb_usb_kbds[i].caps_lock = !(kdb_usb_kbds[i].caps_lock);
243 case 0x0E: /* Backspace */
247 case 0x77: /* Pause */
250 if(!kdb_usb_kbds[i].caps_lock) {
251 return plain_map[keycode];
254 return shift_map[keycode];
260 /* no chars were returned from any of the USB keyboards */
264 #endif /* CONFIG_KDB_USB */
267 * This module contains code to read characters from the keyboard or a serial
270 * It is used by the kernel debugger, and is polled, not interrupt driven.
276 * send: Send a byte to the keyboard controller. Used primarily to
277 * alter LED settings.
281 kdb_kbdsend(unsigned char byte)
284 for (timeout = 200 * 1000; timeout && (inb(KBD_STATUS_REG) & KBD_STAT_IBF); timeout--);
285 outb(byte, KBD_DATA_REG);
287 for (timeout = 200 * 1000; timeout && (~inb(KBD_STATUS_REG) & KBD_STAT_OBF); timeout--);
293 kdb_toggleled(int led)
299 kdb_kbdsend(KBD_CMD_SET_LEDS);
300 kdb_kbdsend((unsigned char)leds);
302 #endif /* KDB_BLINK_LED */
304 #ifdef HAVE_KDBA_SERIAL_CONSOLE
306 struct kdb_serial kdb_serial;
307 enum kdba_serial_console kdba_serial_console;
308 static int get_serial_char(void);
310 /* There must be a serial_inp_xxx() and get_serial_char_xxx() for each type
311 * of console. See enum kdba_serial_console in include/asm-$(ARCH)/kdbprivate.h.
314 #ifdef CONFIG_SERIAL_8250_CONSOLE
317 serial_inp_standard(const struct kdb_serial *kdb_serial, int offset)
319 offset <<= kdb_serial->ioreg_shift;
321 switch (kdb_serial->io_type) {
323 return readb((void __iomem *)(kdb_serial->iobase + offset));
326 return inb(kdb_serial->iobase + offset);
331 /* Check if there is a byte ready at the serial port */
333 get_serial_char_standard(void)
336 static unsigned long fifon;
338 /* try to set the FIFO */
339 fifon = kdb_serial.iobase +
340 (UART_FCR << kdb_serial.ioreg_shift);
341 switch (kdb_serial.io_type) {
343 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
344 UART_FCR_CLEAR_XMIT), (void __iomem *)fifon);
347 outb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
348 UART_FCR_CLEAR_XMIT), fifon);
353 if (kdb_serial.iobase == 0)
356 if (serial_inp_standard(&kdb_serial, UART_LSR) & UART_LSR_DR) {
357 ch = serial_inp_standard(&kdb_serial, UART_RX);
365 #else /* !CONFIG_SERIAL_8250_CONSOLE */
367 #define get_serial_char_standard() -1
369 #endif /* CONFIG_SERIAL_8250_CONSOLE */
371 #ifdef CONFIG_SERIAL_SGI_L1_CONSOLE
373 extern u64 master_node_bedrock_address;
375 /* UART registers on the Bedrock start at 0x80 */
377 extern int l1_serial_in_polled(void);
378 extern int l1_control_in_polled(int);
380 /* Read a byte from the L1 port. kdb_serial is ignored */
382 serial_inp_sgi_l1(const struct kdb_serial *kdb_serial, int offset)
387 while ( counter-- ) {
388 value = l1_serial_in_polled();
389 /* Gobble up the 0's */
396 return l1_control_in_polled(offset);
400 /* Check if there is a byte ready at the L1 port. */
402 get_serial_char_sgi_l1(void)
407 if ((status = serial_inp_sgi_l1(&kdb_serial, UART_LSR)) & UART_LSR_DR) {
408 ch = serial_inp_sgi_l1(&kdb_serial, UART_RX | 0x80); /* bedrock offset */
416 #else /* !CONFIG_SERIAL_SGI_L1_CONSOLE */
418 #define get_serial_char_sgi_l1() -1
420 #endif /* CONFIG_SERIAL_SGI_L1_CONSOLE */
422 /* Select the serial console input at run time, to handle generic kernels */
425 get_serial_char(void)
427 switch (kdba_serial_console) {
430 case KDBA_SC_STANDARD:
431 return get_serial_char_standard();
433 return get_serial_char_sgi_l1();
435 /* gcc is not smart enough to realize that all paths return before here :( */
439 #endif /* HAVE_KDBA_SERIAL_CONSOLE */
441 #ifdef CONFIG_VT_CONSOLE
443 static int kbd_exists;
446 * Check if the keyboard controller has a keypress for us.
447 * Some parts (Enter Release, LED change) are still blocking polled here,
448 * but hopefully they are all short.
450 static int get_kbd_char(void)
452 int scancode, scanstatus;
453 static int shift_lock; /* CAPS LOCK state (0-off, 1-on) */
454 static int shift_key; /* Shift next keypress */
457 extern u_short plain_map[], shift_map[], ctrl_map[];
459 if (KDB_FLAG(NO_I8042) || KDB_FLAG(NO_VT_CONSOLE) ||
460 (inb(KBD_STATUS_REG) == 0xff && inb(KBD_DATA_REG) == 0xff)) {
466 if ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
472 scancode = inb(KBD_DATA_REG);
473 scanstatus = inb(KBD_STATUS_REG);
476 * Ignore mouse events.
478 if (scanstatus & KBD_STAT_MOUSE_OBF)
482 * Ignore release, trigger on make
483 * (except for shift keys, where we want to
484 * keep the shift state so long as the key is
488 if (((scancode&0x7f) == 0x2a) || ((scancode&0x7f) == 0x36)) {
490 * Next key may use shift table
492 if ((scancode & 0x80) == 0) {
500 if ((scancode&0x7f) == 0x1d) {
504 if ((scancode & 0x80) == 0) {
512 if ((scancode & 0x80) != 0)
521 if (scancode == 0x3a) {
533 if (scancode == 0x0e) {
546 case 0x47: /* Home */
550 case 0x4B: /* Left */
554 case 0x50: /* Down */
556 case 0x4D: /* Right */
560 if (scancode == 0xe0) {
565 * For Japanese 86/106 keyboards
566 * See comment in drivers/char/pc_keyb.c.
569 if (scancode == 0x73) {
571 } else if (scancode == 0x7d) {
575 if (!shift_lock && !shift_key && !ctrl_key) {
576 keychar = plain_map[scancode];
577 } else if (shift_lock || shift_key) {
578 keychar = shift_map[scancode];
579 } else if (ctrl_key) {
580 keychar = ctrl_map[scancode];
583 kdb_printf("Unknown state/scancode (%d)\n", scancode);
586 switch (KTYP(keychar)) {
589 if (isprint(keychar))
590 break; /* printable characters */
593 if (keychar == K_ENTER)
597 return(-1); /* ignore unprintables */
600 if ((scancode & 0x7f) == 0x1c) {
602 * enter key. All done. Absorb the release scancode.
604 while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
610 scancode = inb(KBD_DATA_REG);
611 scanstatus = inb(KBD_STATUS_REG);
613 while (scanstatus & KBD_STAT_MOUSE_OBF) {
614 scancode = inb(KBD_DATA_REG);
615 scanstatus = inb(KBD_STATUS_REG);
618 if (scancode != 0x9c) {
620 * Wasn't an enter-release, why not?
622 kdb_printf("kdb: expected enter got 0x%x status 0x%x\n",
623 scancode, scanstatus);
630 return keychar & 0xff;
632 #endif /* CONFIG_VT_CONSOLE */
636 /* Leave numlock alone, setting it messes up laptop keyboards with the keypad
637 * mapped over normal keys.
639 static int kdba_blink_mask = 0x1 | 0x4;
642 #define BOGOMIPS (local_cpu_data->loops_per_jiffy/(500000/HZ))
644 #define BOGOMIPS (loops_per_jiffy/(500000/HZ))
646 static int blink_led(void)
654 if (BOGOMIPS == 0) /* early kdb */
655 delay = 150000000/1000; /* arbitrary bogomips */
657 delay = 150000000/BOGOMIPS; /* Roughly 1 second when polling */
658 kdb_toggleled(kdba_blink_mask);
664 get_char_func poll_funcs[] = {
665 #if defined(CONFIG_VT_CONSOLE)
668 #ifdef HAVE_KDBA_SERIAL_CONSOLE
674 #ifdef CONFIG_KDB_USB
680 /* Dummy versions of kdba_local_arch_setup, kdba_local_arch_cleanup.
681 * FIXME: ia64 with legacy keyboard might need the same code as i386.
684 void kdba_local_arch_setup(void) {}
685 void kdba_local_arch_cleanup(void) {}