summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2023-05-09 13:48:33 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-05-11 02:05:49 +0000
commitd0d67299d61a2647ea76035d85ebf4eff65b4b8a (patch)
treea09fd14d33ad82babdd54fd8bdd86519eacd3065
parent3d6f339994bd5e9188ec22e073598f6aa811de6b (diff)
downloadchrome-ec-d0d67299d61a2647ea76035d85ebf4eff65b4b8a.tar.gz
zephyr: test: Test keyboard_8042.c console commands
Add tests for the `8042` set of console commands in `keyboard_8042.c`, covers about 100 new lines. BUG=b:281691908 TEST=./twister -s drivers/drivers.keyboard_8042 Change-Id: I1e28e0258305d0e1d17571346fcdca8f19102cb9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4520583 Commit-Queue: Peter Marheine <pmarheine@chromium.org> Tested-by: Tristan Honscheid <honscheid@google.com> Commit-Queue: Tristan Honscheid <honscheid@google.com> Auto-Submit: Tristan Honscheid <honscheid@google.com> Reviewed-by: Peter Marheine <pmarheine@chromium.org>
-rw-r--r--common/keyboard_8042.c31
-rw-r--r--include/keyboard_8042.h25
-rw-r--r--zephyr/test/drivers/CMakeLists.txt1
-rw-r--r--zephyr/test/drivers/Kconfig3
-rw-r--r--zephyr/test/drivers/keyboard_8042/CMakeLists.txt8
-rw-r--r--zephyr/test/drivers/keyboard_8042/src/keyboard_8042.c196
-rw-r--r--zephyr/test/drivers/testcase.yaml5
7 files changed, 267 insertions, 2 deletions
diff --git a/common/keyboard_8042.c b/common/keyboard_8042.c
index f3c454382b..3329ce38c0 100644
--- a/common/keyboard_8042.c
+++ b/common/keyboard_8042.c
@@ -430,7 +430,7 @@ static void set_typematic_delays(uint8_t data)
((typematic_value_from_host & 0x7) + 8) / 240;
}
-static void reset_rate_and_delay(void)
+test_export_static void reset_rate_and_delay(void)
{
set_typematic_delays(DEFAULT_TYPEMATIC_VALUE);
}
@@ -451,7 +451,7 @@ static void keyboard_wakeup(void)
host_set_single_event(EC_HOST_EVENT_KEY_PRESSED);
}
-static void set_typematic_key(const uint8_t *scan_code, int32_t len)
+test_export_static void set_typematic_key(const uint8_t *scan_code, int32_t len)
{
typematic_deadline.val = get_time().val + typematic_first_delay;
memcpy(typematic_scan_code, scan_code, len);
@@ -1390,3 +1390,30 @@ DECLARE_HOOK(HOOK_POWER_BUTTON_CHANGE, keyboard_power_button,
HOOK_PRIO_DEFAULT);
#endif /* CONFIG_POWER_BUTTON && !CONFIG_MKBP_INPUT_DEVICES */
+
+#ifdef TEST_BUILD
+void test_keyboard_8042_set_resend_command(const uint8_t *data, int length)
+{
+ length = MIN(length, sizeof(resend_command));
+
+ memcpy(resend_command, data, length);
+ resend_command_len = length;
+}
+
+void test_keyboard_8042_reset(void)
+{
+ /* Initialize controller ram */
+ memset(controller_ram, 0, sizeof(controller_ram));
+ controller_ram[0] = I8042_XLATE | I8042_AUX_DIS | I8042_KBD_DIS;
+
+ /* Typematic state reset */
+ reset_rate_and_delay();
+ clear_typematic_key();
+
+ /* Use default scancode set # 2 */
+ scancode_set = SCANCODE_SET_2;
+
+ /* Keyboard not enabled (matches I8042_KBD_DIS bit being set) */
+ keyboard_enabled = false;
+}
+#endif /* TEST_BUILD */
diff --git a/include/keyboard_8042.h b/include/keyboard_8042.h
index bd56106f1d..fbc5bcf073 100644
--- a/include/keyboard_8042.h
+++ b/include/keyboard_8042.h
@@ -59,4 +59,29 @@ void send_aux_data_to_host_interrupt(uint8_t data);
*/
void send_aux_data_to_device(uint8_t data);
+#ifdef TEST_BUILD
+
+/**
+ * @brief Expose function for testing to set typematic scan code
+ *
+ * @param scan_code Pointer to byte array with scan code
+ * @param len Length of scan code in number of bytes
+ */
+void set_typematic_key(const uint8_t *scan_code, int32_t len);
+
+/**
+ * @brief Force-set the resend command buffer for testing.
+ *
+ * @param data Pointer to command to copy from
+ * @param length Number of bytes to copy (capped at MAX_SCAN_CODE_LEN)
+ */
+__test_only void test_keyboard_8042_set_resend_command(const uint8_t *data,
+ int length);
+
+/**
+ * @brief Reset typematic, RAM, and scancode set for testing purposes.
+ */
+__test_only void test_keyboard_8042_reset(void);
+#endif /* TEST_BUILD */
+
#endif /* __CROS_EC_KEYBOARD_8042_H */
diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt
index b995d9ff95..f8c778bee2 100644
--- a/zephyr/test/drivers/CMakeLists.txt
+++ b/zephyr/test/drivers/CMakeLists.txt
@@ -31,6 +31,7 @@ add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_ISL923X isl923x)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_ISL9241 isl9241)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_I2C_CONTROLLER i2c_controller)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_IT8XXX2_HW_SHA256 it8xxx2_hw_sha256)
+add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_KEYBOARD_8042 keyboard_8042)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_KEYBOARD_SCAN keyboard_scan)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_LED_DRIVER led_driver)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_MKBP mkbp)
diff --git a/zephyr/test/drivers/Kconfig b/zephyr/test/drivers/Kconfig
index 0f846c003f..634eb00a71 100644
--- a/zephyr/test/drivers/Kconfig
+++ b/zephyr/test/drivers/Kconfig
@@ -83,6 +83,9 @@ config LINK_TEST_SUITE_IT8XXX2_HW_SHA256
Include the it8xxx2_hw_sha256 test suite
in the binary.
+config LINK_TEST_SUITE_KEYBOARD_8042
+ bool "Link and test the keyboard_8042 tests"
+
config LINK_TEST_SUITE_KEYBOARD_SCAN
bool "Link and test the keyboard_scan tests"
select PLATFORM_EC_MKBP_EVENT_WAKEUP_MASK
diff --git a/zephyr/test/drivers/keyboard_8042/CMakeLists.txt b/zephyr/test/drivers/keyboard_8042/CMakeLists.txt
new file mode 100644
index 0000000000..ed131ec38e
--- /dev/null
+++ b/zephyr/test/drivers/keyboard_8042/CMakeLists.txt
@@ -0,0 +1,8 @@
+# Copyright 2022 The ChromiumOS Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+target_sources(app PRIVATE
+ src/keyboard_8042.c
+)
+
diff --git a/zephyr/test/drivers/keyboard_8042/src/keyboard_8042.c b/zephyr/test/drivers/keyboard_8042/src/keyboard_8042.c
new file mode 100644
index 0000000000..82779bfdb2
--- /dev/null
+++ b/zephyr/test/drivers/keyboard_8042/src/keyboard_8042.c
@@ -0,0 +1,196 @@
+/* Copyright 2023 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "console.h"
+#include "keyboard_8042.h"
+#include "test/drivers/test_mocks.h"
+#include "test/drivers/test_state.h"
+
+#include <string.h>
+
+#include <zephyr/drivers/emul.h>
+#include <zephyr/drivers/gpio.h>
+#include <zephyr/drivers/gpio/gpio_emul.h>
+#include <zephyr/fff.h>
+#include <zephyr/shell/shell_dummy.h>
+#include <zephyr/ztest.h>
+
+ZTEST(keyboard_8042, test_console_cmd__typematic__status)
+{
+ const char *outbuffer;
+ size_t buffer_size;
+
+ /* Set a typematic scan code to verify */
+ const uint8_t scan_code[] = { 0x01, 0x02, 0x03 };
+
+ set_typematic_key(scan_code, ARRAY_SIZE(scan_code));
+
+ /* With no args, print current state */
+ shell_backend_dummy_clear_output(get_ec_shell());
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "8042 typematic"));
+ outbuffer =
+ shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
+
+ /* Check for some expected lines based off default typematic state */
+ zassert_true(buffer_size > 0);
+ zassert_ok(!strstr(outbuffer, "From host: 0x2b"));
+ zassert_ok(!strstr(outbuffer, "First delay: 500 ms"));
+ zassert_ok(!strstr(outbuffer, "Inter delay: 91 ms"));
+ zassert_ok(
+ !strstr(outbuffer, "Repeat scan code: {0x01, 0x02, 0x03, }"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__typematic__set_delays)
+{
+ const char *outbuffer;
+ size_t buffer_size;
+
+ /* Set first delay and inter delay */
+ shell_backend_dummy_clear_output(get_ec_shell());
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "8042 typematic 123 456"));
+ outbuffer =
+ shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
+
+ /* Check for some expected lines */
+ zassert_true(buffer_size > 0);
+ zassert_ok(!strstr(outbuffer, "First delay: 123 ms"));
+ zassert_ok(!strstr(outbuffer, "Inter delay: 456 ms"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__codeset__set_codeset1)
+{
+ const char *outbuffer;
+ size_t buffer_size;
+
+ /* Switch to codeset 1 and verify output */
+ shell_backend_dummy_clear_output(get_ec_shell());
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "8042 codeset 1"));
+ outbuffer =
+ shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
+
+ zassert_true(buffer_size > 0);
+ zassert_ok(!strstr(outbuffer, "Set: 1"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__codeset__set_invalid)
+{
+ zassert_equal(EC_ERROR_PARAM1,
+ shell_execute_cmd(get_ec_shell(), "8042 codeset 999"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__ram__writeread)
+{
+ const char *outbuffer;
+ size_t buffer_size;
+
+ /* Write a byte and verify the readback in console output */
+ shell_backend_dummy_clear_output(get_ec_shell());
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "8042 ctrlram 0x1f 0xaa"));
+ outbuffer =
+ shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
+
+ zassert_true(buffer_size > 0);
+ zassert_ok(!strstr(outbuffer, "31 = 0xaa"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__ram__invalid)
+{
+ /* Missing args */
+ zassert_equal(EC_ERROR_PARAM_COUNT,
+ shell_execute_cmd(get_ec_shell(), "8042 ctrlram"));
+
+ /* Address out of bounds */
+ zassert_equal(EC_ERROR_PARAM1,
+ shell_execute_cmd(get_ec_shell(), "8042 ctrlram 9999"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__enable__true)
+{
+ const char *outbuffer;
+ size_t buffer_size;
+
+ /* Enable the keyboard and verify in console output */
+ shell_backend_dummy_clear_output(get_ec_shell());
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "8042 kbd y"));
+ outbuffer =
+ shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
+
+ zassert_true(buffer_size > 0);
+ zassert_ok(!strstr(outbuffer, "Enabled: 1"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__enable__invalid)
+{
+ /* Non-bool arg */
+ zassert_equal(EC_ERROR_PARAM1,
+ shell_execute_cmd(get_ec_shell(), "8042 kbd abc"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__internal)
+{
+ const char *outbuffer;
+ size_t buffer_size;
+
+ uint8_t resend_command[] = { 7, 8, 9 };
+
+ test_keyboard_8042_set_resend_command(resend_command,
+ ARRAY_SIZE(resend_command));
+
+ /* Dump the internal state of the keyboard driver */
+ shell_backend_dummy_clear_output(get_ec_shell());
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "8042 internal"));
+ outbuffer =
+ shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
+
+ zassert_true(buffer_size > 0);
+ zassert_ok(!strstr(outbuffer, "keyboard_enabled=0"));
+ zassert_ok(!strstr(outbuffer, "i8042_keyboard_irq_enabled=0"));
+ zassert_ok(!strstr(outbuffer, "i8042_aux_irq_enabled=0"));
+ zassert_ok(!strstr(outbuffer, "keyboard_enabled=0"));
+ zassert_ok(!strstr(outbuffer, "keystroke_enabled=0"));
+ zassert_ok(!strstr(outbuffer, "aux_chan_enabled=0"));
+ zassert_ok(!strstr(outbuffer, "controller_ram_address=0x00"));
+ zassert_ok(!strstr(outbuffer, "resend_command[]={0x07, 0x08, 0x09, }"));
+ zassert_ok(!strstr(outbuffer, "A20_status=0"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__invalid)
+{
+ /* Non-existent subcommand */
+ zassert_equal(EC_ERROR_PARAM1,
+ shell_execute_cmd(get_ec_shell(), "8042 foobar"));
+}
+
+ZTEST(keyboard_8042, test_console_cmd__all)
+{
+ const char *outbuffer;
+ size_t buffer_size;
+
+ /* Run all the subcommands */
+ shell_backend_dummy_clear_output(get_ec_shell());
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "8042"));
+ outbuffer =
+ shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
+
+ /* Just look for the headers since we already tested the individual
+ * subcommands
+ */
+
+ zassert_true(buffer_size > 0);
+ zassert_ok(!strstr(outbuffer, "- Typematic:"));
+ zassert_ok(!strstr(outbuffer, "- Codeset:"));
+ zassert_ok(!strstr(outbuffer, "- Control RAM:"));
+ zassert_ok(!strstr(outbuffer, "- Keyboard:"));
+ zassert_ok(!strstr(outbuffer, "- Internal:"));
+}
+
+static void reset(void *fixture)
+{
+ ARG_UNUSED(fixture);
+
+ test_keyboard_8042_reset();
+}
+
+ZTEST_SUITE(keyboard_8042, drivers_predicate_post_main, NULL, reset, reset,
+ NULL);
diff --git a/zephyr/test/drivers/testcase.yaml b/zephyr/test/drivers/testcase.yaml
index 9e182db74c..7f47a93e70 100644
--- a/zephyr/test/drivers/testcase.yaml
+++ b/zephyr/test/drivers/testcase.yaml
@@ -236,6 +236,11 @@ tests:
extra_configs:
- CONFIG_LINK_TEST_SUITE_IT8XXX2_HW_SHA256=y
- CONFIG_PLATFORM_EC_SHA256_HW_ACCELERATE=y
+ drivers.keyboard_8042:
+ extra_configs:
+ - CONFIG_PLATFORM_EC_KEYBOARD_PROTOCOL_MKBP=n
+ - CONFIG_PLATFORM_EC_KEYBOARD_PROTOCOL_8042=y
+ - CONFIG_LINK_TEST_SUITE_KEYBOARD_8042=y
drivers.keyboard_scan:
extra_configs:
- CONFIG_LINK_TEST_SUITE_KEYBOARD_SCAN=y