summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHung-Te Lin <hungte@chromium.org>2018-08-08 11:43:01 +0800
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2018-08-09 02:15:24 +0000
commit370f6527e80c1ee44ac663bf87c6e39d03070b93 (patch)
tree7193d602729212d89e6eba1cc350aa19eb1d4e90
parentcbd826bd406036494ba0b02b9f5f031fd0ae0570 (diff)
downloadchrome-ec-370f6527e80c1ee44ac663bf87c6e39d03070b93.tar.gz
keyboard: Keep only scan code set 2 table and do translation for set 1.
Apply CL:1166742 to simplify (and speed up) scancode processing. BUG=None TEST=make BOARD=eve BRANCH=eve Change-Id: Idc4aa77978146fe971bfdf17406325ca4b98b953 Signed-off-by: Hung-Te Lin <hungte@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1166750
-rw-r--r--board/eve/board.c1
-rw-r--r--board/eve/keyboard_legacy.c61
-rw-r--r--common/keyboard_8042.c114
-rw-r--r--common/keyboard_8042_sharedlib.c102
-rw-r--r--include/keyboard_8042.h21
-rw-r--r--include/keyboard_8042_sharedlib.h9
6 files changed, 131 insertions, 177 deletions
diff --git a/board/eve/board.c b/board/eve/board.c
index 565a333833..3b69b274a0 100644
--- a/board/eve/board.c
+++ b/board/eve/board.c
@@ -508,7 +508,6 @@ static void board_init(void)
if (board_get_version() == BOARD_VERSION_EVT) {
/* Set F13 to new defined key on EVT */
CPRINTS("Overriding F13 scan code");
- scancode_set1[3][9] = 0xe058;
scancode_set2[3][9] = 0xe007;
}
#endif
diff --git a/board/eve/keyboard_legacy.c b/board/eve/keyboard_legacy.c
index bb6ee5ac30..3a5b5dbcfd 100644
--- a/board/eve/keyboard_legacy.c
+++ b/board/eve/keyboard_legacy.c
@@ -20,41 +20,40 @@
#define MK_DIM 0xf003
#define MK_BRIGHT 0xf004
+/* Scan codes in set2 */
+
/* Use SEARCH (before translate) as Fn key. */
-static const struct makecode_entry makecode_fn_key = {
- .set1 = 0xe05b, .set2 = 0xe01f };
+#define SCANCODE_FN 0xe01f
static const struct makecode_translate_entry legacy_mapping[] = {
- /* from[set1,set2], to[set1,set2] */
- { {0xe058, 0xe007}, {0xe05b, 0xe020} }, /* ASSIST => SEARCH(Win) */
- { {0x005d, 0x002f}, {0xe05d, 0xe02f} }, /* MENU => APP */
+ { 0xe007, 0xe020 }, /* ASSIST => SEARCH(Win) */
+ { 0x002f, 0xe02f }, /* MENU => APP */
};
/* Alternate mapping when Fn is pressed. */
static const struct makecode_translate_entry legacy_fn_mapping[] = {
- /* from[set1,set2], to[set1,set2] */
- {{0x003b, 0x0005}, {0xe06a, 0xe038}}, /* F1 => Browser Back */
- {{0x003c, 0x0006}, {0xe067, 0xe020}}, /* F2 => Browser Refresh */
- {{0x003d, 0x0004}, {0x0057, 0x0078}}, /* F3 => Full Screen */
- {{0x003e, 0x000c}, {0xe037, 0xe07c}}, /* F4 => Print Screen */
- {{0x003f, 0x0003}, {MK_DIM, MK_DIM}}, /* F5 => Dim Screen */
- {{0x0040, 0x000b}, {MK_BRIGHT, MK_BRIGHT}}, /* F6 => Brighten */
- {{0x0041, 0x0083}, {0xe022, 0xe034}}, /* F7 => Play/Pause */
- {{0x0042, 0x000a}, {0xe020, 0xe023}}, /* F8 => Mute */
- {{0x0043, 0x0001}, {0xe02e, 0xe021}}, /* F9 => Vol Down */
- {{0x0044, 0x0009}, {0xe030, 0xe032}}, /* F10 => Vol Up */
-
- {{0x0038, 0x0011}, {0x003a, 0x0058}}, /* LAlt => Caps Lock */
- {{0x0034, 0x0049}, {0xe052, 0xe070}}, /* Dot(.) => Insert */
- {{0x000e, 0x0066}, {0xe053, 0xe071}}, /* BackSpace => Delete */
-
- {{0x0019, 0x004d}, {MK_PAUSE, MK_PAUSE}}, /* P => Pause */
- {{0x0030, 0x0032}, {MK_CBREAK, MK_CBREAK}}, /* B => Ctrl-Break */
-
- {{0xe048, 0xe075}, {0xe049, 0xe07d}}, /* Up => Page Up */
- {{0xe050, 0xe072}, {0xe051, 0xe07a}}, /* Down => Page Down */
- {{0xe04b, 0xe06b}, {0xe047, 0xe06c}}, /* Left => Home */
- {{0xe04d, 0xe074}, {0xe04f, 0xe069}}, /* Right => End */
+ { 0x0005, 0xe038 }, /* F1 => Browser Back */
+ { 0x0006, 0xe020 }, /* F2 => Browser Refresh */
+ { 0x0004, 0x0078 }, /* F3 => Full Screen */
+ { 0x000c, 0xe07c }, /* F4 => Print Screen */
+ { 0x0003, MK_DIM }, /* F5 => Dim Screen */
+ { 0x000b, MK_BRIGHT }, /* F6 => Brighten Screen */
+ { 0x0083, 0xe034 }, /* F7 => Play/Pause */
+ { 0x000a, 0xe023 }, /* F8 => Mute */
+ { 0x0001, 0xe021 }, /* F9 => Vol Down */
+ { 0x0009, 0xe032 }, /* F10 => Vol Up */
+
+ { 0x0011, 0x0058 }, /* LAlt => Caps Lock */
+ { 0x0049, 0xe070 }, /* Dot(.) => Insert */
+ { 0x0066, 0xe071 }, /* BackSpace => Delete */
+
+ { 0x004d, MK_PAUSE }, /* P => Pause */
+ { 0x0032, MK_CBREAK }, /* B => Ctrl-Break */
+
+ { 0xe075, 0xe07d }, /* Up => Page Up */
+ { 0xe072, 0xe07a }, /* Down => Page Down */
+ { 0xe06b, 0xe06c }, /* Left => Home */
+ { 0xe074, 0xe069 }, /* Right => End */
};
static const char pause_key_scancode_set1[] = {
@@ -149,7 +148,7 @@ uint16_t keyboard_board_translate(uint16_t make_code, int8_t pressed,
return make_code;
/* Fn must be processed because Fn makecode conflicts with Win. */
- if (makecode_match(make_code, code_set, &makecode_fn_key)) {
+ if (make_code == SCANCODE_FN) {
fn_pressed = pressed;
/**
* TODO(hungte): If we press Fn, X, (triggers an Fn+X make) then
@@ -160,13 +159,13 @@ uint16_t keyboard_board_translate(uint16_t make_code, int8_t pressed,
}
make_code = makecode_translate(
- make_code, code_set, ARRAY_BEGIN(legacy_mapping),
+ make_code, ARRAY_BEGIN(legacy_mapping),
ARRAY_SIZE(legacy_mapping));
if (!fn_pressed)
return make_code;
make_code = makecode_translate(
- make_code, code_set, ARRAY_BEGIN(legacy_fn_mapping),
+ make_code, ARRAY_BEGIN(legacy_fn_mapping),
ARRAY_SIZE(legacy_fn_mapping));
switch (make_code) {
case MK_PAUSE:
diff --git a/common/keyboard_8042.c b/common/keyboard_8042.c
index 303515b6dd..4c3a5726d6 100644
--- a/common/keyboard_8042.c
+++ b/common/keyboard_8042.c
@@ -203,31 +203,13 @@ void keyboard_select_mapping(enum keyboard_mapping_type mapping)
}
#endif
-int makecode_match(uint16_t make_code, enum scancode_set_list code_set,
- const struct makecode_entry *entry)
-{
- /* Compare SET 2 (default) first. */
- if (code_set == SCANCODE_SET_2)
- return make_code == entry->set2;
- if (code_set == SCANCODE_SET_1)
- return make_code == entry->set1;
- return 0;
-}
-
-uint16_t makecode_translate(
- uint16_t make_code, enum scancode_set_list code_set,
- const struct makecode_translate_entry *entries, size_t count)
+uint16_t makecode_translate(uint16_t make_code,
+ const struct makecode_translate_entry *entries,
+ size_t count)
{
for (; count > 0; count--, entries++) {
- if (makecode_match(make_code, code_set, &entries->from)) {
- if (code_set == SCANCODE_SET_2)
- return entries->to.set2;
- if (code_set == SCANCODE_SET_1)
- return entries->to.set1;
-
- /* Should never reach here. */
- assert(!"Unexpected code set");
- }
+ if (make_code == entries->from)
+ return entries->to;
}
return make_code;
}
@@ -295,6 +277,11 @@ static enum scancode_set_list acting_code_set(enum scancode_set_list set)
return set;
}
+static int is_supported_code_set(enum scancode_set_list set)
+{
+ return (set == SCANCODE_SET_1 || set == SCANCODE_SET_2);
+}
+
/**
* Return the make or break code bytes for the active scancode set.
*
@@ -312,33 +299,22 @@ static void scancode_bytes(uint16_t make_code, int8_t pressed,
/* Output the make code (from table) */
if (make_code >= 0x0100) {
- *len += 2;
- scan_code[0] = make_code >> 8;
- scan_code[1] = make_code & 0xff;
- } else {
- *len += 1;
- scan_code[0] = make_code & 0xff;
+ scan_code[(*len)++] = make_code >> 8;
+ make_code &= 0xff;
}
switch (code_set) {
case SCANCODE_SET_1:
- /* OR 0x80 for the last byte. */
- if (!pressed) {
- ASSERT(*len >= 1);
- scan_code[*len - 1] |= 0x80;
- }
+ make_code = scancode_translate_set2_to_1(make_code);
+ scan_code[(*len)++] = pressed ? make_code : (make_code | 0x80);
break;
case SCANCODE_SET_2:
- /*
- * Insert the break byte, move back the last byte and insert a
- * 0xf0 byte before that.
- */
- if (!pressed) {
- ASSERT(*len >= 1);
- scan_code[*len] = scan_code[*len - 1];
- scan_code[*len - 1] = 0xf0;
- *len += 1;
+ if (pressed) {
+ scan_code[(*len)++] = make_code;
+ } else {
+ scan_code[(*len)++] = 0xf0;
+ scan_code[(*len)++] = make_code;
}
break;
default:
@@ -359,21 +335,12 @@ static enum ec_error_list matrix_callback(int8_t row, int8_t col,
if (row > KEYBOARD_ROWS || col > KEYBOARD_COLS)
return EC_ERROR_INVAL;
+ make_code = scancode_set2[row][col];
if (pressed)
- keyboard_special(scancode_set1[row][col]);
+ keyboard_special(make_code);
code_set = acting_code_set(code_set);
-
- switch (code_set) {
- case SCANCODE_SET_1:
- make_code = scancode_set1[row][col];
- break;
-
- case SCANCODE_SET_2:
- make_code = scancode_set2[row][col];
- break;
-
- default:
+ if (!is_supported_code_set(code_set)) {
CPRINTS("KB scancode set %d unsupported", code_set);
return EC_ERROR_UNIMPLEMENTED;
}
@@ -836,30 +803,30 @@ static void i8042_handle_from_host(void)
static void keyboard_special(uint16_t k)
{
static uint8_t s;
- static const uint16_t a[] = {0xe048, 0xe048, 0xe050, 0xe050, 0xe04b,
- 0xe04d, 0xe04b, 0xe04d, 0x0030, 0x001e};
+ static const uint16_t a[] = {0xe075, 0xe075, 0xe072, 0xe072, 0xe06b,
+ 0xe074, 0xe06b, 0xe074, 0x0032, 0x001c};
#ifdef HAS_TASK_LIGHTBAR
/* Lightbar demo mode: keyboard can fake the battery state */
switch (k) {
- case 0xe048: /* up */
+ case 0xe075: /* up */
demo_battery_level(1);
break;
- case 0xe050: /* down */
+ case 0xe072: /* down */
demo_battery_level(-1);
break;
- case 0xe04b: /* left */
+ case 0xe06b: /* left */
demo_is_charging(0);
break;
- case 0xe04d: /* right */
+ case 0xe074: /* right */
demo_is_charging(1);
break;
- case 0x0040: /* dim */
+ case 0x000b: /* dim */
demo_brightness(-1);
break;
- case 0x0041: /* bright */
+ case 0x0083: /* bright */
demo_brightness(1);
break;
- case 0x0014: /* T */
+ case 0x002c: /* T */
demo_tap();
break;
}
@@ -867,7 +834,7 @@ static void keyboard_special(uint16_t k)
if (k == a[s])
s++;
- else if (k != 0xe048)
+ else if (k != 0xe075)
s = 0;
else if (s != 2)
s = 1;
@@ -964,7 +931,6 @@ test_mockable void keyboard_update_button(enum keyboard_button_type button,
int is_pressed)
{
uint8_t scan_code[MAX_SCAN_CODE_LEN];
- uint16_t make_code;
uint32_t len;
struct button_8042_t button_8042;
enum scancode_set_list code_set;
@@ -1000,18 +966,12 @@ test_mockable void keyboard_update_button(enum keyboard_button_type button,
break;
}
- switch (code_set) {
- case SCANCODE_SET_1:
- make_code = button_8042.scancode_set1;
- break;
- case SCANCODE_SET_2:
- make_code = button_8042.scancode_set2;
- break;
- default:
- return; /* Other sets are not supported */
- }
+ if (!is_supported_code_set(code_set))
+ return;
- scancode_bytes(make_code, is_pressed, code_set, scan_code, &len);
+ button_8042 = buttons_8042[button];
+ scancode_bytes(button_8042.scancode, is_pressed, code_set, scan_code,
+ &len);
ASSERT(len > 0);
if (button_8042.repeat) {
diff --git a/common/keyboard_8042_sharedlib.c b/common/keyboard_8042_sharedlib.c
index 7aba63502d..0ea4dca792 100644
--- a/common/keyboard_8042_sharedlib.c
+++ b/common/keyboard_8042_sharedlib.c
@@ -12,39 +12,11 @@
#include "libsharedobjs.h"
#include "util.h"
-/* The standard Chrome OS keyboard matrix table. */
-#ifdef CONFIG_KEYBOARD_SCANCODE_MUTABLE
-uint16_t scancode_set1[KEYBOARD_ROWS][KEYBOARD_COLS] = {
-#else
-SHAREDLIB(const uint16_t scancode_set1[KEYBOARD_ROWS][KEYBOARD_COLS] = {
+/* The standard Chrome OS keyboard matrix table in scan code set 2. */
+#ifndef CONFIG_KEYBOARD_SCANCODE_MUTABLE
+SHAREDLIB(const
#endif
- {0x0000, 0xe05b, 0x003b, 0x0030, 0x0044, 0x0073, 0x0031, 0x0000, 0x000d,
- 0x0000, 0xe038, 0x0000, 0x0000},
- {0x0000, 0x0001, 0x003e, 0x0022, 0x0041, 0x0000, 0x0023, 0x0000, 0x0028,
- 0x0043, 0x0000, 0x000e, 0x0079},
- {0x001d, 0x000f, 0x003d, 0x0014, 0x0040, 0x001b, 0x0015, 0x0056, 0x001a,
- 0x0042, 0x007d, 0x0000, 0x0000},
- {0xe05b, 0x0029, 0x003c, 0x0006, 0x003f, 0x0000, 0x0007, 0x0000, 0x000c,
- 0x005d, 0x0000, 0x002b, 0x007b},
- {0xe01d, 0x001e, 0x0020, 0x0021, 0x001f, 0x0025, 0x0024, 0x0000, 0x0027,
- 0x0026, 0x002b, 0x001c, 0x0000},
- {0xe058, 0x002c, 0x002e, 0x002f, 0x002d, 0x0033, 0x0032, 0x002a, 0x0035,
- 0x0034, 0x0000, 0x0039, 0x0000},
- {0x0000, 0x0002, 0x0004, 0x0005, 0x0003, 0x0009, 0x0008, 0x0000, 0x000b,
- 0x000a, 0x0038, 0xe050, 0xe04d},
- {0x0000, 0x0010, 0x0012, 0x0013, 0x0011, 0x0017, 0x0016, 0x0036, 0x0019,
- 0x0018, 0x0000, 0xe048, 0xe04b},
-#ifdef CONFIG_KEYBOARD_SCANCODE_MUTABLE
-};
-#else
-});
-#endif
-
-#ifdef CONFIG_KEYBOARD_SCANCODE_MUTABLE
uint16_t scancode_set2[KEYBOARD_ROWS][KEYBOARD_COLS] = {
-#else
-SHAREDLIB(uint16_t const scancode_set2[KEYBOARD_ROWS][KEYBOARD_COLS] = {
-#endif
{0x0000, 0xe01f, 0x0005, 0x0032, 0x0009, 0x0051, 0x0031, 0x0000, 0x0055,
0x0000, 0xe011, 0x0000, 0x0000},
{0x0000, 0x0076, 0x000c, 0x0034, 0x0083, 0x0000, 0x0033, 0x0000, 0x0052,
@@ -61,27 +33,63 @@ SHAREDLIB(uint16_t const scancode_set2[KEYBOARD_ROWS][KEYBOARD_COLS] = {
0x0046, 0x0011, 0xe072, 0xe074},
{0x0000, 0x0015, 0x0024, 0x002d, 0x001d, 0x0043, 0x003c, 0x0059, 0x004d,
0x0044, 0x0000, 0xe075, 0xe06b},
-#ifdef CONFIG_KEYBOARD_SCANCODE_MUTABLE
-};
-#else
-});
+}
+#ifndef CONFIG_KEYBOARD_SCANCODE_MUTABLE
+)
#endif
+;
+
+/*
+ * The translation table from scan code set 2 to set 1.
+ * Ref: http://kbd-project.org/docs/scancodes/scancodes-10.html#ss10.3
+ * To reduce space, we only keep the translation for 0~127,
+ * so a real translation need to do 0x83=>0x41 explicitly (
+ * see scancode_translate_set2_to_1 below).
+ */
+SHAREDLIB(const uint8_t scancode_translate_table[128] = {
+ 0xff, 0x43, 0x41, 0x3f, 0x3d, 0x3b, 0x3c, 0x58,
+ 0x64, 0x44, 0x42, 0x40, 0x3e, 0x0f, 0x29, 0x59,
+ 0x65, 0x38, 0x2a, 0x70, 0x1d, 0x10, 0x02, 0x5a,
+ 0x66, 0x71, 0x2c, 0x1f, 0x1e, 0x11, 0x03, 0x5b,
+ 0x67, 0x2e, 0x2d, 0x20, 0x12, 0x05, 0x04, 0x5c,
+ 0x68, 0x39, 0x2f, 0x21, 0x14, 0x13, 0x06, 0x5d,
+ 0x69, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, 0x5e,
+ 0x6a, 0x72, 0x32, 0x24, 0x16, 0x08, 0x09, 0x5f,
+ 0x6b, 0x33, 0x25, 0x17, 0x18, 0x0b, 0x0a, 0x60,
+ 0x6c, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0c, 0x61,
+ 0x6d, 0x73, 0x28, 0x74, 0x1a, 0x0d, 0x62, 0x6e,
+ 0x3a, 0x36, 0x1c, 0x1b, 0x75, 0x2b, 0x63, 0x76,
+ 0x55, 0x56, 0x77, 0x78, 0x79, 0x7a, 0x0e, 0x7b,
+ 0x7c, 0x4f, 0x7d, 0x4b, 0x47, 0x7e, 0x7f, 0x6f,
+ 0x52, 0x53, 0x50, 0x4c, 0x4d, 0x48, 0x01, 0x45,
+ 0x57, 0x4e, 0x51, 0x4a, 0x37, 0x49, 0x46, 0x54,
+});
+
+uint8_t scancode_translate_set2_to_1(uint8_t code)
+{
+ if (code & 0x80) {
+ if (code == 0x83)
+ return 0x41;
+ return code;
+ }
+ return scancode_translate_table[code];
+}
/*
- * Button scancodes.
+ * Button scancodes in code set 2.
* Must be in the same order as defined in keyboard_button_type.
*/
SHAREDLIB(const struct button_8042_t buttons_8042[] = {
- {0xe05e, 0xe037, 0}, /* Power */
- {0xe02e, 0xe021, 1}, /* Volume Down */
- {0xe030, 0xe032, 1}, /* Volume Up */
- {0x0002, 0x0016, 1}, /* 1 */
- {0x0003, 0x001e, 1}, /* 2 */
- {0x0004, 0x0026, 1}, /* 3 */
- {0x0005, 0x0025, 1}, /* 4 */
- {0x0006, 0x002e, 1}, /* 5 */
- {0x0007, 0x0036, 1}, /* 6 */
- {0x0008, 0x003d, 1}, /* 7 */
- {0x0009, 0x003e, 1}, /* 8 */
+ {0xe037, 0}, /* Power */
+ {0xe021, 1}, /* Volume Down */
+ {0xe032, 1}, /* Volume Up */
+ {0x0016, 1}, /* 1 */
+ {0x001e, 1}, /* 2 */
+ {0x0026, 1}, /* 3 */
+ {0x0025, 1}, /* 4 */
+ {0x002e, 1}, /* 5 */
+ {0x0036, 1}, /* 6 */
+ {0x003d, 1}, /* 7 */
+ {0x003e, 1}, /* 8 */
});
BUILD_ASSERT(ARRAY_SIZE(buttons_8042) == KEYBOARD_BUTTON_COUNT);
diff --git a/include/keyboard_8042.h b/include/keyboard_8042.h
index e43ceb1164..4ae7c15a8e 100644
--- a/include/keyboard_8042.h
+++ b/include/keyboard_8042.h
@@ -52,33 +52,20 @@ enum scancode_set_list {
SCANCODE_MAX = SCANCODE_SET_3,
};
-struct makecode_entry {
- uint16_t set1, set2;
-};
-
struct makecode_translate_entry {
- struct makecode_entry from, to;
+ uint16_t from, to;
};
/**
- * Checks if a given make code matches given record.
- *
- * Returns 1 if matches.
- */
-int makecode_match(uint16_t make_code, enum scancode_set_list code_set,
- const struct makecode_entry *entry);
-
-/**
* Translate a make code to different value.
*
* @param make_code The value of make_code.
- * @param code_set The code set of make code.
* @param entries Pointer to array of struct makecode_translate_entry
* @param count Number of elements in entries
*/
-uint16_t makecode_translate(
- uint16_t make_code, enum scancode_set_list code_set,
- const struct makecode_translate_entry *entries, size_t count);
+uint16_t makecode_translate(uint16_t make_code,
+ const struct makecode_translate_entry *entries,
+ size_t count);
/**
* Returns a board-specific translated make code.
diff --git a/include/keyboard_8042_sharedlib.h b/include/keyboard_8042_sharedlib.h
index 5c9b559279..e475288ad5 100644
--- a/include/keyboard_8042_sharedlib.h
+++ b/include/keyboard_8042_sharedlib.h
@@ -13,20 +13,21 @@
#include "keyboard_protocol.h"
struct button_8042_t {
- uint16_t scancode_set1;
- uint16_t scancode_set2;
+ uint16_t scancode;
int repeat;
};
/* The standard Chrome OS keyboard matrix table. */
#ifdef CONFIG_KEYBOARD_SCANCODE_MUTABLE
-extern uint16_t scancode_set1[KEYBOARD_ROWS][KEYBOARD_COLS];
extern uint16_t scancode_set2[KEYBOARD_ROWS][KEYBOARD_COLS];
#else
-extern const uint16_t scancode_set1[KEYBOARD_ROWS][KEYBOARD_COLS];
extern const uint16_t scancode_set2[KEYBOARD_ROWS][KEYBOARD_COLS];
#endif
+/* Translation from scan code set 2 to set 1. */
+extern const uint8_t scancode_translate_table[];
+extern uint8_t scancode_translate_set2_to_1(uint8_t code);
+
/* Button scancodes (Power, Volume Down, Volume Up, etc.) */
extern const struct button_8042_t buttons_8042[KEYBOARD_BUTTON_COUNT];