Rajat Jain | a28e3fb | 2020-04-01 17:01:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 7 | #include <acpi/acpi.h> |
| 8 | #include <acpi/acpigen.h> |
| 9 | #include <acpi/acpigen_ps2_keybd.h> |
Rajat Jain | a28e3fb | 2020-04-01 17:01:13 -0700 | [diff] [blame] | 10 | #include <console/console.h> |
| 11 | #include <input-event-codes.h> |
| 12 | |
| 13 | #define KEYMAP(scancode, keycode) (((uint32_t)(scancode) << 16) | (keycode & 0xFFFF)) |
| 14 | #define SCANCODE(keymap) ((keymap >> 16) & 0xFFFF) |
| 15 | |
| 16 | /* Possible keymaps for function keys in the top row */ |
| 17 | static const uint32_t function_keymaps[] = { |
| 18 | KEYMAP(0x3b, KEY_F1), |
| 19 | KEYMAP(0x3c, KEY_F2), |
| 20 | KEYMAP(0x3d, KEY_F3), |
| 21 | KEYMAP(0x3e, KEY_F4), |
| 22 | KEYMAP(0x3f, KEY_F5), |
| 23 | KEYMAP(0x40, KEY_F6), |
| 24 | KEYMAP(0x41, KEY_F7), |
| 25 | KEYMAP(0x42, KEY_F8), |
| 26 | KEYMAP(0x43, KEY_F9), |
| 27 | KEYMAP(0x44, KEY_F10), |
| 28 | KEYMAP(0x57, KEY_F11), |
| 29 | KEYMAP(0x58, KEY_F12), |
| 30 | KEYMAP(0x59, KEY_F13), |
| 31 | KEYMAP(0x5a, KEY_F14), |
| 32 | KEYMAP(0x5b, KEY_F15), |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * Possible keymaps for action keys in the top row. This is a superset of |
| 37 | * possible keys. Individual keyboards will have a subset of these keys. |
| 38 | * The scancodes are true / condensed 1 byte scancodes from set-1 |
| 39 | */ |
| 40 | static const uint32_t action_keymaps[] = { |
| 41 | [PS2_KEY_BACK] = KEYMAP(0xea, KEY_BACK), /* e06a */ |
| 42 | [PS2_KEY_FORWARD] = KEYMAP(0xe9, KEY_FORWARD), /* e069 */ |
| 43 | [PS2_KEY_REFRESH] = KEYMAP(0xe7, KEY_REFRESH), /* e067 */ |
| 44 | [PS2_KEY_FULLSCREEN] = KEYMAP(0x91, KEY_FULL_SCREEN), /* e011 */ |
| 45 | [PS2_KEY_OVERVIEW] = KEYMAP(0x92, KEY_SCALE), /* e012 */ |
| 46 | [PS2_KEY_VOL_MUTE] = KEYMAP(0xa0, KEY_MUTE), /* e020 */ |
| 47 | [PS2_KEY_VOL_DOWN] = KEYMAP(0xae, KEY_VOLUMEDOWN), /* e02e */ |
| 48 | [PS2_KEY_VOL_UP] = KEYMAP(0xb0, KEY_VOLUMEUP), /* e030 */ |
| 49 | [PS2_KEY_PLAY_PAUSE] = KEYMAP(0x9a, KEY_PLAYPAUSE), /* e01a */ |
| 50 | [PS2_KEY_NEXT_TRACK] = KEYMAP(0x99, KEY_NEXTSONG), /* e019 */ |
| 51 | [PS2_KEY_PREV_TRACK] = KEYMAP(0x90, KEY_PREVIOUSSONG), /* e010 */ |
| 52 | [PS2_KEY_SNAPSHOT] = KEYMAP(0x93, KEY_SYSRQ), /* e013 */ |
| 53 | [PS2_KEY_BRIGHTNESS_DOWN] = KEYMAP(0x94, KEY_BRIGHTNESSDOWN),/* e014 */ |
| 54 | [PS2_KEY_BRIGHTNESS_UP] = KEYMAP(0x95, KEY_BRIGHTNESSUP), /* e015 */ |
| 55 | [PS2_KEY_KBD_BKLIGHT_DOWN] = KEYMAP(0x97, KEY_KBDILLUMDOWN), /* e017 */ |
| 56 | [PS2_KEY_KBD_BKLIGHT_UP] = KEYMAP(0x98, KEY_KBDILLUMUP), /* e018 */ |
| 57 | [PS2_KEY_PRIVACY_SCRN_TOGGLE] = KEYMAP(0x96, /* e016 */ |
| 58 | KEY_PRIVACY_SCREEN_TOGGLE), |
| 59 | }; |
| 60 | |
| 61 | /* Keymap for numeric keypad keys */ |
| 62 | static uint32_t numeric_keypad_keymaps[] = { |
| 63 | /* Row-0 */ |
| 64 | KEYMAP(0xc9, KEY_PAGEUP), |
| 65 | KEYMAP(0xd1, KEY_PAGEDOWN), |
| 66 | KEYMAP(0xc7, KEY_HOME), |
| 67 | KEYMAP(0xcf, KEY_END), |
| 68 | /* Row-1 */ |
| 69 | KEYMAP(0xd3, KEY_DELETE), |
| 70 | KEYMAP(0xb5, KEY_KPSLASH), |
| 71 | KEYMAP(0x37, KEY_KPASTERISK), |
| 72 | KEYMAP(0x4a, KEY_KPMINUS), |
| 73 | /* Row-2 */ |
| 74 | KEYMAP(0x47, KEY_KP7), |
| 75 | KEYMAP(0x48, KEY_KP8), |
| 76 | KEYMAP(0x49, KEY_KP9), |
| 77 | KEYMAP(0x4e, KEY_KPPLUS), |
| 78 | /* Row-3 */ |
| 79 | KEYMAP(0x4b, KEY_KP4), |
| 80 | KEYMAP(0x4c, KEY_KP5), |
| 81 | KEYMAP(0x4d, KEY_KP6), |
| 82 | /* Row-4 */ |
| 83 | KEYMAP(0x4f, KEY_KP1), |
| 84 | KEYMAP(0x50, KEY_KP2), |
| 85 | KEYMAP(0x51, KEY_KP3), |
| 86 | KEYMAP(0x9c, KEY_KPENTER), |
| 87 | /* Row-5 */ |
| 88 | KEYMAP(0x52, KEY_KP0), |
| 89 | KEYMAP(0x53, KEY_KPDOT), |
| 90 | }; |
| 91 | |
| 92 | /* |
| 93 | * Keymap for rest of non-top-row keys. This is a superset of all the possible |
| 94 | * keys that any chromeos keyboards can have. |
| 95 | */ |
| 96 | static uint32_t rest_of_keymaps[] = { |
| 97 | /* Row-0 */ |
| 98 | KEYMAP(0x01, KEY_ESC), |
| 99 | /* Row-1 */ |
| 100 | KEYMAP(0x29, KEY_GRAVE), |
| 101 | KEYMAP(0x02, KEY_1), |
| 102 | KEYMAP(0x03, KEY_2), |
| 103 | KEYMAP(0x04, KEY_3), |
| 104 | KEYMAP(0x05, KEY_4), |
| 105 | KEYMAP(0x06, KEY_5), |
| 106 | KEYMAP(0x07, KEY_6), |
| 107 | KEYMAP(0x08, KEY_7), |
| 108 | KEYMAP(0x09, KEY_8), |
| 109 | KEYMAP(0x0a, KEY_9), |
| 110 | KEYMAP(0x0b, KEY_0), |
| 111 | KEYMAP(0x0c, KEY_MINUS), |
| 112 | KEYMAP(0x0d, KEY_EQUAL), |
Rajat Jain | 93193a0 | 2020-04-20 17:56:00 -0700 | [diff] [blame] | 113 | KEYMAP(0x7d, KEY_YEN), /* JP keyboards only */ |
Rajat Jain | a28e3fb | 2020-04-01 17:01:13 -0700 | [diff] [blame] | 114 | KEYMAP(0x0e, KEY_BACKSPACE), |
| 115 | /* Row-2 */ |
| 116 | KEYMAP(0x0f, KEY_TAB), |
| 117 | KEYMAP(0x10, KEY_Q), |
| 118 | KEYMAP(0x11, KEY_W), |
| 119 | KEYMAP(0x12, KEY_E), |
| 120 | KEYMAP(0x13, KEY_R), |
| 121 | KEYMAP(0x14, KEY_T), |
| 122 | KEYMAP(0x15, KEY_Y), |
| 123 | KEYMAP(0x16, KEY_U), |
| 124 | KEYMAP(0x17, KEY_I), |
| 125 | KEYMAP(0x18, KEY_O), |
| 126 | KEYMAP(0x19, KEY_P), |
| 127 | KEYMAP(0x1a, KEY_LEFTBRACE), |
| 128 | KEYMAP(0x1b, KEY_RIGHTBRACE), |
| 129 | KEYMAP(0x2b, KEY_BACKSLASH), |
| 130 | /* Row-3 */ |
Rajat Jain | 93193a0 | 2020-04-20 17:56:00 -0700 | [diff] [blame] | 131 | KEYMAP(0xdb, KEY_LEFTMETA), /* Search Key */ |
Rajat Jain | a28e3fb | 2020-04-01 17:01:13 -0700 | [diff] [blame] | 132 | KEYMAP(0x1e, KEY_A), |
| 133 | KEYMAP(0x1f, KEY_S), |
| 134 | KEYMAP(0x20, KEY_D), |
| 135 | KEYMAP(0x21, KEY_F), |
| 136 | KEYMAP(0x22, KEY_G), |
| 137 | KEYMAP(0x23, KEY_H), |
| 138 | KEYMAP(0x24, KEY_J), |
| 139 | KEYMAP(0x25, KEY_K), |
| 140 | KEYMAP(0x26, KEY_L), |
| 141 | KEYMAP(0x27, KEY_SEMICOLON), |
| 142 | KEYMAP(0x28, KEY_APOSTROPHE), |
| 143 | KEYMAP(0x1c, KEY_ENTER), |
| 144 | /* Row-4 */ |
| 145 | KEYMAP(0x2a, KEY_LEFTSHIFT), |
Rajat Jain | 93193a0 | 2020-04-20 17:56:00 -0700 | [diff] [blame] | 146 | KEYMAP(0x56, KEY_102ND), /* UK keyboards only */ |
Rajat Jain | a28e3fb | 2020-04-01 17:01:13 -0700 | [diff] [blame] | 147 | KEYMAP(0x2c, KEY_Z), |
| 148 | KEYMAP(0x2d, KEY_X), |
| 149 | KEYMAP(0x2e, KEY_C), |
| 150 | KEYMAP(0x2f, KEY_V), |
| 151 | KEYMAP(0x30, KEY_B), |
| 152 | KEYMAP(0x31, KEY_N), |
| 153 | KEYMAP(0x32, KEY_M), |
| 154 | KEYMAP(0x33, KEY_COMMA), |
| 155 | KEYMAP(0x34, KEY_DOT), |
| 156 | KEYMAP(0x35, KEY_SLASH), |
Rajat Jain | 93193a0 | 2020-04-20 17:56:00 -0700 | [diff] [blame] | 157 | KEYMAP(0x73, KEY_RO), /* JP keyboards only */ |
Rajat Jain | a28e3fb | 2020-04-01 17:01:13 -0700 | [diff] [blame] | 158 | KEYMAP(0x36, KEY_RIGHTSHIFT), |
| 159 | /* Row-5 */ |
| 160 | KEYMAP(0x1d, KEY_LEFTCTRL), |
| 161 | KEYMAP(0x38, KEY_LEFTALT), |
Rajat Jain | 93193a0 | 2020-04-20 17:56:00 -0700 | [diff] [blame] | 162 | KEYMAP(0x7b, KEY_MUHENKAN), /* JP keyboards only */ |
Rajat Jain | a28e3fb | 2020-04-01 17:01:13 -0700 | [diff] [blame] | 163 | KEYMAP(0x39, KEY_SPACE), |
Rajat Jain | 93193a0 | 2020-04-20 17:56:00 -0700 | [diff] [blame] | 164 | KEYMAP(0x79, KEY_HENKAN), /* JP keyboards only */ |
Rajat Jain | a28e3fb | 2020-04-01 17:01:13 -0700 | [diff] [blame] | 165 | KEYMAP(0xb8, KEY_RIGHTALT), |
| 166 | KEYMAP(0x9d, KEY_RIGHTCTRL), |
| 167 | /* Arrow keys */ |
| 168 | KEYMAP(0xcb, KEY_LEFT), |
| 169 | KEYMAP(0xd0, KEY_DOWN), |
| 170 | KEYMAP(0xcd, KEY_RIGHT), |
| 171 | KEYMAP(0xc8, KEY_UP), |
| 172 | }; |
| 173 | |
| 174 | static void ssdt_generate_physmap(struct acpi_dp *dp, uint8_t num_top_row_keys, |
| 175 | enum ps2_action_key action_keys[]) |
| 176 | { |
| 177 | struct acpi_dp *dp_array; |
| 178 | enum ps2_action_key key; |
| 179 | uint32_t keymap, i; |
| 180 | |
| 181 | dp_array = acpi_dp_new_table("function-row-physmap"); |
| 182 | if (!dp_array) { |
| 183 | printk(BIOS_ERR, "PS2K: couldn't write function-row-physmap\n"); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | printk(BIOS_INFO, "PS2K: Physmap: ["); |
| 188 | for (i = 0; i < num_top_row_keys; i++) { |
| 189 | key = action_keys[i]; |
| 190 | if (key && key < ARRAY_SIZE(action_keymaps)) { |
| 191 | keymap = action_keymaps[key]; |
| 192 | } else { |
| 193 | keymap = 0; |
| 194 | printk(BIOS_ERR, |
| 195 | "PS2K: invalid top-action-key-%u: %u(skipped)\n", |
| 196 | i, key); |
| 197 | } |
| 198 | acpi_dp_add_integer(dp_array, NULL, SCANCODE(keymap)); |
| 199 | printk(BIOS_INFO, " %X", SCANCODE(keymap)); |
| 200 | } |
| 201 | |
| 202 | printk(BIOS_INFO, " ]\n"); |
| 203 | acpi_dp_add_array(dp, dp_array); |
| 204 | } |
| 205 | |
| 206 | static void ssdt_generate_keymap(struct acpi_dp *dp, uint8_t num_top_row_keys, |
| 207 | enum ps2_action_key action_keys[], |
| 208 | bool can_send_function_keys, |
| 209 | bool has_numeric_keypad, |
| 210 | bool has_scrnlock_key) |
| 211 | { |
| 212 | struct acpi_dp *dp_array; |
| 213 | enum ps2_action_key key; |
| 214 | uint32_t keymap; |
| 215 | unsigned int i, total = 0; |
| 216 | |
| 217 | dp_array = acpi_dp_new_table("linux,keymap"); |
| 218 | if (!dp_array) { |
| 219 | printk(BIOS_ERR, "PS2K: couldn't write linux,keymap\n"); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | /* Write out keymap for top row action keys */ |
| 224 | for (i = 0; i < num_top_row_keys; i++) { |
| 225 | key = action_keys[i]; |
| 226 | if (!key || key >= ARRAY_SIZE(action_keymaps)) { |
| 227 | printk(BIOS_ERR, |
| 228 | "PS2K: invalid top-action-key-%u: %u\n", i, key); |
| 229 | continue; |
| 230 | } |
| 231 | keymap = action_keymaps[key]; |
| 232 | acpi_dp_add_integer(dp_array, NULL, keymap); |
| 233 | total++; |
| 234 | } |
| 235 | |
| 236 | /* Write out keymap for function keys, if keyboard can send them */ |
| 237 | if (can_send_function_keys) { |
| 238 | for (i = 0; i < num_top_row_keys; i++) { |
| 239 | keymap = function_keymaps[i]; |
| 240 | acpi_dp_add_integer(dp_array, NULL, keymap); |
| 241 | } |
| 242 | |
| 243 | total += num_top_row_keys; |
| 244 | } |
| 245 | |
| 246 | /* Write out keymap for numeric keypad, if the keyboard has it */ |
| 247 | if (has_numeric_keypad) { |
| 248 | for (i = 0; i < ARRAY_SIZE(numeric_keypad_keymaps); i++) { |
| 249 | keymap = numeric_keypad_keymaps[i]; |
| 250 | acpi_dp_add_integer(dp_array, NULL, keymap); |
| 251 | } |
| 252 | |
| 253 | total += ARRAY_SIZE(numeric_keypad_keymaps); |
| 254 | } |
| 255 | |
| 256 | /* Provide keymap for screenlock only if it is present */ |
| 257 | if (has_scrnlock_key) { |
| 258 | acpi_dp_add_integer(dp_array, NULL, KEYMAP(0x5d, KEY_SLEEP)); |
| 259 | total++; |
| 260 | } |
| 261 | |
| 262 | /* Write out keymap for rest of keys */ |
| 263 | for (i = 0; i < ARRAY_SIZE(rest_of_keymaps); i++) { |
| 264 | keymap = rest_of_keymaps[i]; |
| 265 | acpi_dp_add_integer(dp_array, NULL, keymap); |
| 266 | } |
| 267 | |
| 268 | total += ARRAY_SIZE(rest_of_keymaps); |
| 269 | printk(BIOS_INFO, "PS2K: Passing %u keymaps to kernel\n", total); |
| 270 | |
| 271 | acpi_dp_add_array(dp, dp_array); |
| 272 | } |
| 273 | |
| 274 | void acpigen_ps2_keyboard_dsd(const char *scope, uint8_t num_top_row_keys, |
| 275 | enum ps2_action_key action_keys[], |
| 276 | bool can_send_function_keys, |
| 277 | bool has_numeric_keypad, |
| 278 | bool has_scrnlock_key) |
| 279 | { |
| 280 | struct acpi_dp *dsd; |
| 281 | |
| 282 | if (!scope || |
| 283 | num_top_row_keys < PS2_MIN_TOP_ROW_KEYS || |
| 284 | num_top_row_keys > PS2_MAX_TOP_ROW_KEYS) { |
| 285 | printk(BIOS_ERR, "PS2K: %s: invalid args\n", __func__); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | dsd = acpi_dp_new_table("_DSD"); |
| 290 | if (!dsd) { |
| 291 | printk(BIOS_ERR, "PS2K: couldn't write _DSD\n"); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | acpigen_write_scope(scope); |
| 296 | ssdt_generate_physmap(dsd, num_top_row_keys, action_keys); |
| 297 | ssdt_generate_keymap(dsd, num_top_row_keys, action_keys, |
| 298 | can_send_function_keys, has_numeric_keypad, |
| 299 | has_scrnlock_key); |
| 300 | acpi_dp_write(dsd); |
| 301 | acpigen_pop_len(); /* Scope */ |
| 302 | } |