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