Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Advanced Micro Devices, Inc. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 30 | #include <libpayload.h> |
| 31 | |
Jordan Crouse | 63f181f | 2008-04-25 23:09:39 +0000 | [diff] [blame] | 32 | #define I8042_CMD_READ_MODE 0x20 |
| 33 | #define I8042_CMD_WRITE_MODE 0x60 |
| 34 | |
| 35 | #define I8042_MODE_XLATE 0x40 |
| 36 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 37 | unsigned char map[2][0x57] = { |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 38 | { |
| 39 | 0x00, 0x1B, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, |
| 40 | 0x37, 0x38, 0x39, 0x30, 0x2D, 0x3D, 0x08, 0x09, |
| 41 | 0x71, 0x77, 0x65, 0x72, 0x74, 0x79, 0x75, 0x69, |
| 42 | 0x6F, 0x70, 0x5B, 0x5D, 0x0A, 0x00, 0x61, 0x73, |
| 43 | 0x64, 0x66, 0x67, 0x68, 0x6A, 0x6B, 0x6C, 0x3B, |
| 44 | 0x27, 0x60, 0x00, 0x5C, 0x7A, 0x78, 0x63, 0x76, |
| 45 | 0x62, 0x6E, 0x6D, 0x2C, 0x2E, 0x2F, 0x00, 0x2A, |
| 46 | 0x00, 0x20, |
| 47 | }, |
| 48 | { |
| 49 | 0x00, 0x1B, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5E, |
| 50 | 0x26, 0x2A, 0x28, 0x29, 0x5F, 0x2B, 0x08, 0x00, |
| 51 | 0x51, 0x57, 0x45, 0x52, 0x54, 0x59, 0x55, 0x49, |
| 52 | 0x4F, 0x50, 0x7B, 0x7D, 0x0A, 0x00, 0x41, 0x53, |
| 53 | 0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4C, 0x3A, |
| 54 | 0x22, 0x7E, 0x00, 0x7C, 0x5A, 0x58, 0x43, 0x56, |
| 55 | 0x42, 0x4E, 0x4D, 0x3C, 0x3E, 0x3F, 0x00, 0x2A, |
| 56 | 0x00, 0x20, |
| 57 | } |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | #define MOD_SHIFT 1 |
| 61 | #define MOD_CTRL 2 |
| 62 | #define MOD_CAPSLOCK 4 |
| 63 | |
| 64 | int keyboard_havechar(void) |
| 65 | { |
| 66 | unsigned char c = inb(0x64); |
| 67 | return c & 1; |
| 68 | } |
| 69 | |
| 70 | unsigned char keyboard_get_scancode(void) |
| 71 | { |
| 72 | unsigned char ch = 0; |
| 73 | |
| 74 | if (keyboard_havechar()) |
| 75 | ch = inb(0x60); |
| 76 | |
| 77 | return ch; |
| 78 | } |
| 79 | |
| 80 | int keyboard_getchar(void) |
| 81 | { |
| 82 | static int modifier; |
| 83 | unsigned char ch; |
| 84 | int shift; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 85 | int ret = 0; |
| 86 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 87 | while (!keyboard_havechar()) ; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 88 | |
| 89 | ch = keyboard_get_scancode(); |
| 90 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 91 | switch (ch) { |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 92 | case 0x36: |
| 93 | case 0x2a: |
| 94 | modifier &= ~MOD_SHIFT; |
| 95 | break; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 96 | case 0x80 | 0x36: |
| 97 | case 0x80 | 0x2a: |
| 98 | modifier |= MOD_SHIFT; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 99 | case 0x1d: |
| 100 | modifier &= ~MOD_CTRL; |
| 101 | break; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 102 | case 0x80 | 0x1d: |
| 103 | modifier |= MOD_CTRL; |
| 104 | break; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 105 | case 0x3a: |
| 106 | if (modifier & MOD_CAPSLOCK) |
| 107 | modifier &= ~MOD_CAPSLOCK; |
| 108 | else |
| 109 | modifier |= MOD_CAPSLOCK; |
| 110 | break; |
| 111 | } |
| 112 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 113 | if (!(ch & 0x80) && ch < 0x57) { |
| 114 | shift = |
| 115 | (modifier & MOD_SHIFT) ^ (modifier & MOD_CAPSLOCK) ? 1 : 0; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 116 | ret = map[shift][ch]; |
| 117 | |
| 118 | if (modifier & MOD_CTRL) |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 119 | ret = (ret >= 0x3F && ret <= 0x5F) ? ret & 0x1f : 0; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | return ret; |
| 125 | } |
Jordan Crouse | 63f181f | 2008-04-25 23:09:39 +0000 | [diff] [blame] | 126 | |
| 127 | static int keyboard_wait_read(void) |
| 128 | { |
| 129 | int timeout = 10000; |
| 130 | |
| 131 | while(timeout-- && !(inb(0x64) & 0x01)) |
| 132 | udelay(50); |
| 133 | |
| 134 | return (timeout <= 0) ? -1 : 0; |
| 135 | } |
| 136 | |
| 137 | static int keyboard_wait_write(void) |
| 138 | { |
| 139 | int timeout = 10000; |
| 140 | |
| 141 | while(timeout-- && (inb(0x64) & 0x02)) |
| 142 | udelay(50); |
| 143 | |
| 144 | return (timeout <= 0) ? -1 : 0; |
| 145 | } |
| 146 | |
| 147 | static unsigned char keyboard_get_mode(void) |
| 148 | { |
| 149 | outb(I8042_CMD_READ_MODE, 0x64); |
| 150 | keyboard_wait_read(); |
| 151 | return inb(0x60); |
| 152 | } |
| 153 | |
| 154 | static void keyboard_set_mode(unsigned char mode) |
| 155 | { |
| 156 | outb(I8042_CMD_WRITE_MODE, 0x64); |
| 157 | keyboard_wait_write(); |
| 158 | outb(mode, 0x60); |
| 159 | } |
| 160 | |
| 161 | void keyboard_init(void) |
| 162 | { |
| 163 | u8 mode; |
| 164 | |
| 165 | /* Read the current mode */ |
| 166 | mode = keyboard_get_mode(); |
| 167 | |
| 168 | /* Turn on scancode translate mode so that we can |
| 169 | use the scancode set 1 tables */ |
| 170 | |
| 171 | mode |= I8042_MODE_XLATE; |
| 172 | |
| 173 | /* Write the new mode */ |
| 174 | keyboard_set_mode(mode); |
| 175 | } |
| 176 | |