/*1* Kernel Debugger Architecture Dependent Console I/O handler2*3* This file is subject to the terms and conditions of the GNU General Public4* License.5*6* Copyright (c) 1999-2006 Silicon Graphics, Inc. All Rights Reserved.7* Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.8*/910#include <linux/kdb.h>11#include <linux/keyboard.h>12#include <linux/ctype.h>13#include <linux/io.h>1415#include "kdb_private.h"1617/* Keyboard Controller Registers on normal PCs. */1819#define KBD_STATUS_REG 0x64 /* Status register (R) */20#define KBD_DATA_REG 0x60 /* Keyboard data register (R/W) */2122/* Status Register Bits */2324#define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */25#define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */2627#define CTRL(c) ((c) - 64)2829static int kbd_exists;30static int kbd_last_ret;3132/*33* Check if the keyboard controller has a keypress for us.34* Some parts (Enter Release, LED change) are still blocking polled here,35* but hopefully they are all short.36*/37int kdb_get_kbd_char(void)38{39int scancode, scanstatus;40static int shift_lock; /* CAPS LOCK state (0-off, 1-on) */41static int shift_key; /* Shift next keypress */42static int ctrl_key;43u_short keychar;4445if (KDB_FLAG(NO_I8042) || KDB_FLAG(NO_VT_CONSOLE) ||46(inb(KBD_STATUS_REG) == 0xff && inb(KBD_DATA_REG) == 0xff)) {47kbd_exists = 0;48return -1;49}50kbd_exists = 1;5152if ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)53return -1;5455/*56* Fetch the scancode57*/58scancode = inb(KBD_DATA_REG);59scanstatus = inb(KBD_STATUS_REG);6061/*62* Ignore mouse events.63*/64if (scanstatus & KBD_STAT_MOUSE_OBF)65return -1;6667/*68* Ignore release, trigger on make69* (except for shift keys, where we want to70* keep the shift state so long as the key is71* held down).72*/7374if (((scancode&0x7f) == 0x2a) || ((scancode&0x7f) == 0x36)) {75/*76* Next key may use shift table77*/78if ((scancode & 0x80) == 0)79shift_key = 1;80else81shift_key = 0;82return -1;83}8485if ((scancode&0x7f) == 0x1d) {86/*87* Left ctrl key88*/89if ((scancode & 0x80) == 0)90ctrl_key = 1;91else92ctrl_key = 0;93return -1;94}9596if ((scancode & 0x80) != 0) {97if (scancode == 0x9c)98kbd_last_ret = 0;99return -1;100}101102scancode &= 0x7f;103104/*105* Translate scancode106*/107108if (scancode == 0x3a) {109/*110* Toggle caps lock111*/112shift_lock ^= 1;113114#ifdef KDB_BLINK_LED115kdb_toggleled(0x4);116#endif117return -1;118}119120if (scancode == 0x0e) {121/*122* Backspace123*/124return 8;125}126127/* Translate special keys to equivalent CTRL control characters */128switch (scancode) {129case 0xF: /* Tab */130return CTRL('I');131case 0x53: /* Del */132return CTRL('D');133case 0x47: /* Home */134return CTRL('A');135case 0x4F: /* End */136return CTRL('E');137case 0x4B: /* Left */138return CTRL('B');139case 0x48: /* Up */140return CTRL('P');141case 0x50: /* Down */142return CTRL('N');143case 0x4D: /* Right */144return CTRL('F');145}146147if (scancode == 0xe0)148return -1;149150/*151* For Japanese 86/106 keyboards152* See comment in drivers/char/pc_keyb.c.153* - Masahiro Adegawa154*/155if (scancode == 0x73)156scancode = 0x59;157else if (scancode == 0x7d)158scancode = 0x7c;159160if (!shift_lock && !shift_key && !ctrl_key) {161keychar = plain_map[scancode];162} else if ((shift_lock || shift_key) && key_maps[1]) {163keychar = key_maps[1][scancode];164} else if (ctrl_key && key_maps[4]) {165keychar = key_maps[4][scancode];166} else {167keychar = 0x0020;168kdb_printf("Unknown state/scancode (%d)\n", scancode);169}170keychar &= 0x0fff;171if (keychar == '\t')172keychar = ' ';173switch (KTYP(keychar)) {174case KT_LETTER:175case KT_LATIN:176switch (keychar) {177/* non-printable supported control characters */178case CTRL('A'): /* Home */179case CTRL('B'): /* Left */180case CTRL('D'): /* Del */181case CTRL('E'): /* End */182case CTRL('F'): /* Right */183case CTRL('I'): /* Tab */184case CTRL('N'): /* Down */185case CTRL('P'): /* Up */186return keychar;187}188189if (isprint(keychar))190break; /* printable characters */191fallthrough;192case KT_SPEC:193if (keychar == K_ENTER)194break;195fallthrough;196default:197return -1; /* ignore unprintables */198}199200if (scancode == 0x1c) {201kbd_last_ret = 1;202return 13;203}204205return keychar & 0xff;206}207EXPORT_SYMBOL_GPL(kdb_get_kbd_char);208209/*210* Best effort cleanup of ENTER break codes on leaving KDB. Called on211* exiting KDB, when we know we processed an ENTER or KP ENTER scan212* code.213*/214void kdb_kbd_cleanup_state(void)215{216int scancode, scanstatus;217218/*219* Nothing to clean up, since either220* ENTER was never pressed, or has already221* gotten cleaned up.222*/223if (!kbd_last_ret)224return;225226kbd_last_ret = 0;227/*228* Enter key. Need to absorb the break code here, lest it gets229* leaked out if we exit KDB as the result of processing 'g'.230*231* This has several interesting implications:232* + Need to handle KP ENTER, which has break code 0xe0 0x9c.233* + Need to handle repeat ENTER and repeat KP ENTER. Repeats234* only get a break code at the end of the repeated235* sequence. This means we can't propagate the repeated key236* press, and must swallow it away.237* + Need to handle possible PS/2 mouse input.238* + Need to handle mashed keys.239*/240241while (1) {242while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)243cpu_relax();244245/*246* Fetch the scancode.247*/248scancode = inb(KBD_DATA_REG);249scanstatus = inb(KBD_STATUS_REG);250251/*252* Skip mouse input.253*/254if (scanstatus & KBD_STAT_MOUSE_OBF)255continue;256257/*258* If we see 0xe0, this is either a break code for KP259* ENTER, or a repeat make for KP ENTER. Either way,260* since the second byte is equivalent to an ENTER,261* skip the 0xe0 and try again.262*263* If we see 0x1c, this must be a repeat ENTER or KP264* ENTER (and we swallowed 0xe0 before). Try again.265*266* We can also see make and break codes for other keys267* mashed before or after pressing ENTER. Thus, if we268* see anything other than 0x9c, we have to try again.269*270* Note, if you held some key as ENTER was depressed,271* that break code would get leaked out.272*/273if (scancode != 0x9c)274continue;275276return;277}278}279280281