Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Kernel Debugger Architecture Dependent Console I/O handler
0003  *
0004  * This file is subject to the terms and conditions of the GNU General Public
0005  * License.
0006  *
0007  * Copyright (c) 1999-2006 Silicon Graphics, Inc.  All Rights Reserved.
0008  * Copyright (c) 2009 Wind River Systems, Inc.  All Rights Reserved.
0009  */
0010 
0011 #include <linux/kdb.h>
0012 #include <linux/keyboard.h>
0013 #include <linux/ctype.h>
0014 #include <linux/io.h>
0015 
0016 /* Keyboard Controller Registers on normal PCs. */
0017 
0018 #define KBD_STATUS_REG      0x64    /* Status register (R) */
0019 #define KBD_DATA_REG        0x60    /* Keyboard data register (R/W) */
0020 
0021 /* Status Register Bits */
0022 
0023 #define KBD_STAT_OBF        0x01    /* Keyboard output buffer full */
0024 #define KBD_STAT_MOUSE_OBF  0x20    /* Mouse output buffer full */
0025 
0026 static int kbd_exists;
0027 static int kbd_last_ret;
0028 
0029 /*
0030  * Check if the keyboard controller has a keypress for us.
0031  * Some parts (Enter Release, LED change) are still blocking polled here,
0032  * but hopefully they are all short.
0033  */
0034 int kdb_get_kbd_char(void)
0035 {
0036     int scancode, scanstatus;
0037     static int shift_lock;  /* CAPS LOCK state (0-off, 1-on) */
0038     static int shift_key;   /* Shift next keypress */
0039     static int ctrl_key;
0040     u_short keychar;
0041 
0042     if (KDB_FLAG(NO_I8042) || KDB_FLAG(NO_VT_CONSOLE) ||
0043         (inb(KBD_STATUS_REG) == 0xff && inb(KBD_DATA_REG) == 0xff)) {
0044         kbd_exists = 0;
0045         return -1;
0046     }
0047     kbd_exists = 1;
0048 
0049     if ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
0050         return -1;
0051 
0052     /*
0053      * Fetch the scancode
0054      */
0055     scancode = inb(KBD_DATA_REG);
0056     scanstatus = inb(KBD_STATUS_REG);
0057 
0058     /*
0059      * Ignore mouse events.
0060      */
0061     if (scanstatus & KBD_STAT_MOUSE_OBF)
0062         return -1;
0063 
0064     /*
0065      * Ignore release, trigger on make
0066      * (except for shift keys, where we want to
0067      *  keep the shift state so long as the key is
0068      *  held down).
0069      */
0070 
0071     if (((scancode&0x7f) == 0x2a) || ((scancode&0x7f) == 0x36)) {
0072         /*
0073          * Next key may use shift table
0074          */
0075         if ((scancode & 0x80) == 0)
0076             shift_key = 1;
0077         else
0078             shift_key = 0;
0079         return -1;
0080     }
0081 
0082     if ((scancode&0x7f) == 0x1d) {
0083         /*
0084          * Left ctrl key
0085          */
0086         if ((scancode & 0x80) == 0)
0087             ctrl_key = 1;
0088         else
0089             ctrl_key = 0;
0090         return -1;
0091     }
0092 
0093     if ((scancode & 0x80) != 0) {
0094         if (scancode == 0x9c)
0095             kbd_last_ret = 0;
0096         return -1;
0097     }
0098 
0099     scancode &= 0x7f;
0100 
0101     /*
0102      * Translate scancode
0103      */
0104 
0105     if (scancode == 0x3a) {
0106         /*
0107          * Toggle caps lock
0108          */
0109         shift_lock ^= 1;
0110 
0111 #ifdef  KDB_BLINK_LED
0112         kdb_toggleled(0x4);
0113 #endif
0114         return -1;
0115     }
0116 
0117     if (scancode == 0x0e) {
0118         /*
0119          * Backspace
0120          */
0121         return 8;
0122     }
0123 
0124     /* Special Key */
0125     switch (scancode) {
0126     case 0xF: /* Tab */
0127         return 9;
0128     case 0x53: /* Del */
0129         return 4;
0130     case 0x47: /* Home */
0131         return 1;
0132     case 0x4F: /* End */
0133         return 5;
0134     case 0x4B: /* Left */
0135         return 2;
0136     case 0x48: /* Up */
0137         return 16;
0138     case 0x50: /* Down */
0139         return 14;
0140     case 0x4D: /* Right */
0141         return 6;
0142     }
0143 
0144     if (scancode == 0xe0)
0145         return -1;
0146 
0147     /*
0148      * For Japanese 86/106 keyboards
0149      *  See comment in drivers/char/pc_keyb.c.
0150      *  - Masahiro Adegawa
0151      */
0152     if (scancode == 0x73)
0153         scancode = 0x59;
0154     else if (scancode == 0x7d)
0155         scancode = 0x7c;
0156 
0157     if (!shift_lock && !shift_key && !ctrl_key) {
0158         keychar = plain_map[scancode];
0159     } else if ((shift_lock || shift_key) && key_maps[1]) {
0160         keychar = key_maps[1][scancode];
0161     } else if (ctrl_key && key_maps[4]) {
0162         keychar = key_maps[4][scancode];
0163     } else {
0164         keychar = 0x0020;
0165         kdb_printf("Unknown state/scancode (%d)\n", scancode);
0166     }
0167     keychar &= 0x0fff;
0168     if (keychar == '\t')
0169         keychar = ' ';
0170     switch (KTYP(keychar)) {
0171     case KT_LETTER:
0172     case KT_LATIN:
0173         if (isprint(keychar))
0174             break;      /* printable characters */
0175         fallthrough;
0176     case KT_SPEC:
0177         if (keychar == K_ENTER)
0178             break;
0179         fallthrough;
0180     default:
0181         return -1;  /* ignore unprintables */
0182     }
0183 
0184     if (scancode == 0x1c) {
0185         kbd_last_ret = 1;
0186         return 13;
0187     }
0188 
0189     return keychar & 0xff;
0190 }
0191 EXPORT_SYMBOL_GPL(kdb_get_kbd_char);
0192 
0193 /*
0194  * Best effort cleanup of ENTER break codes on leaving KDB. Called on
0195  * exiting KDB, when we know we processed an ENTER or KP ENTER scan
0196  * code.
0197  */
0198 void kdb_kbd_cleanup_state(void)
0199 {
0200     int scancode, scanstatus;
0201 
0202     /*
0203      * Nothing to clean up, since either
0204      * ENTER was never pressed, or has already
0205      * gotten cleaned up.
0206      */
0207     if (!kbd_last_ret)
0208         return;
0209 
0210     kbd_last_ret = 0;
0211     /*
0212      * Enter key. Need to absorb the break code here, lest it gets
0213      * leaked out if we exit KDB as the result of processing 'g'.
0214      *
0215      * This has several interesting implications:
0216      * + Need to handle KP ENTER, which has break code 0xe0 0x9c.
0217      * + Need to handle repeat ENTER and repeat KP ENTER. Repeats
0218      *   only get a break code at the end of the repeated
0219      *   sequence. This means we can't propagate the repeated key
0220      *   press, and must swallow it away.
0221      * + Need to handle possible PS/2 mouse input.
0222      * + Need to handle mashed keys.
0223      */
0224 
0225     while (1) {
0226         while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
0227             cpu_relax();
0228 
0229         /*
0230          * Fetch the scancode.
0231          */
0232         scancode = inb(KBD_DATA_REG);
0233         scanstatus = inb(KBD_STATUS_REG);
0234 
0235         /*
0236          * Skip mouse input.
0237          */
0238         if (scanstatus & KBD_STAT_MOUSE_OBF)
0239             continue;
0240 
0241         /*
0242          * If we see 0xe0, this is either a break code for KP
0243          * ENTER, or a repeat make for KP ENTER. Either way,
0244          * since the second byte is equivalent to an ENTER,
0245          * skip the 0xe0 and try again.
0246          *
0247          * If we see 0x1c, this must be a repeat ENTER or KP
0248          * ENTER (and we swallowed 0xe0 before). Try again.
0249          *
0250          * We can also see make and break codes for other keys
0251          * mashed before or after pressing ENTER. Thus, if we
0252          * see anything other than 0x9c, we have to try again.
0253          *
0254          * Note, if you held some key as ENTER was depressed,
0255          * that break code would get leaked out.
0256          */
0257         if (scancode != 0x9c)
0258             continue;
0259 
0260         return;
0261     }
0262 }