diff options
Diffstat (limited to 'zephyr/test/drivers/keyboard_scan')
7 files changed, 1028 insertions, 0 deletions
diff --git a/zephyr/test/drivers/keyboard_scan/CMakeLists.txt b/zephyr/test/drivers/keyboard_scan/CMakeLists.txt new file mode 100644 index 0000000000..07040187f2 --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/CMakeLists.txt @@ -0,0 +1,15 @@ +# 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_backlight.c + src/keyboard_scan.c + src/keyboard_test_utils.c + src/mkbp_event.c + src/mkbp_info.c +) + +target_include_directories(app PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/include +) diff --git a/zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h b/zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h new file mode 100644 index 0000000000..0117fea09c --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h @@ -0,0 +1,19 @@ +/* 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. + */ + +/** + * @brief Press or release a key through the keyboard emulator + * + * @param row Key row + * @param col Key column + * @param pressed 1 if pressed, 0 otherwise + * @return int 0 if successful + */ +int emulate_keystate(int row, int col, int pressed); + +/** + * @brief Clears any pressed keys in the keyboard emulator + */ +void clear_emulated_keys(void); diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c new file mode 100644 index 0000000000..149f25dfdd --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c @@ -0,0 +1,134 @@ +/* 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. + */ + +#include <stdint.h> +#include <string.h> +#include <zephyr/ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest_assert.h> + +#include "console.h" +#include "host_command.h" +#include "keyboard_backlight.h" +#include "test/drivers/test_state.h" + +/** + * @brief Send host command to set the backlight percentage + * + * @param percent Backlight intensity, from 0 to 100 (inclusive). + * @return uint16_t Host command return code + */ +static uint16_t set_backlight_percent_helper(uint8_t percent) +{ + struct ec_params_pwm_set_keyboard_backlight params = { + .percent = percent + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT, 0, params); + + return host_command_process(&args); +} + +ZTEST(keyboard_backlight, host_command_set_backlight__normal) +{ + /* Set the backlight intensity level to this and verify */ + uint8_t expected_percentage = 50; + + zassert_ok(set_backlight_percent_helper(expected_percentage), NULL); + zassert_equal(expected_percentage, kblight_get(), NULL); +} + +ZTEST(keyboard_backlight, host_command_set_backlight__out_of_range) +{ + /* Too high */ + uint8_t expected_percentage = 101; + + zassert_equal(EC_RES_ERROR, + set_backlight_percent_helper(expected_percentage), NULL); +} + +ZTEST(keyboard_backlight, host_command_get_backlight__normal) +{ + /* Set this backlight intensity and verify via host command */ + uint8_t expected_percentage = 50; + int ret; + + zassume_ok(set_backlight_percent_helper(expected_percentage), NULL); + + /* Brief delay to allow a deferred function to enable the backlight */ + k_sleep(K_MSEC(50)); + + struct ec_response_pwm_get_keyboard_backlight response; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT, 0, response); + + ret = host_command_process(&args); + zassert_ok(ret, "Host command failed: %d", ret); + zassert_equal(expected_percentage, response.percent, NULL); + zassert_equal(1, response.enabled, "Got 0x%02x", response.enabled); +} + +ZTEST(keyboard_backlight, console_command__noargs) +{ + /* Command should print current status. Set backlight on and to 70% */ + + const char *outbuffer; + size_t buffer_size; + + zassume_ok(set_backlight_percent_helper(70), NULL); + k_sleep(K_MSEC(50)); + + /* With no args, print current state */ + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight"), NULL); + outbuffer = + shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + + zassert_ok(!strstr(outbuffer, "Keyboard backlight: 70% enabled: 1"), + "Actual string: `%s`", outbuffer); +} + +ZTEST(keyboard_backlight, console_command__set_on) +{ + /* Command should enable backlight to given intensity */ + + zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight 65"), NULL); + zassert_equal(65, kblight_get(), NULL); + zassert_equal(1, kblight_get_current_enable(), NULL); +} + +ZTEST(keyboard_backlight, console_command__set_off) +{ + zassume_ok(set_backlight_percent_helper(40), NULL); + k_sleep(K_MSEC(50)); + + /* Turn back off */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight 0"), NULL); + zassert_equal(0, kblight_get(), NULL); + zassert_equal(0, kblight_get_current_enable(), NULL); +} + +ZTEST(keyboard_backlight, console_command__bad_params) +{ + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "kblight NaN"), NULL); + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "kblight -1"), NULL); + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "kblight 101"), NULL); +} + +static void reset(void *data) +{ + ARG_UNUSED(data); + + /* Reset the backlight to off and 0% brightness */ + kblight_set(0); + kblight_enable(0); +} + +ZTEST_SUITE(keyboard_backlight, drivers_predicate_post_main, NULL, reset, reset, + NULL); diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c new file mode 100644 index 0000000000..c7955ec655 --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c @@ -0,0 +1,420 @@ +/* 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. + */ +#include <string.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/fff.h> +#include <emul/emul_kb_raw.h> + +#include "console.h" +#include "host_command.h" +#include "keyboard_scan.h" +#include "keyboard_test_utils.h" +#include "test/drivers/test_mocks.h" +#include "test/drivers/test_state.h" + +ZTEST(keyboard_scan, test_boot_key) +{ + const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw)); + const int kb_cols = DT_PROP(DT_NODELABEL(cros_kb_raw), cols); + + emul_kb_raw_reset(dev); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_NONE, NULL); + + /* Case 1: refresh + esc -> BOOT_KEY_ESC */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_ESC, KEYBOARD_COL_ESC, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_ESC, NULL); + + /* + * Case 1.5: + * GSC may hold ksi2 when power button is pressed, simulate this + * behavior and verify boot key detection again. + */ + zassert_true(IS_ENABLED(CONFIG_KEYBOARD_PWRBTN_ASSERTS_KSI2), NULL); + for (int i = 0; i < kb_cols; i++) { + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, i, true), + NULL); + } + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_ESC, NULL); + + /* Case 2: esc only -> BOOT_KEY_NONE */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_ESC, KEYBOARD_COL_ESC, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_NONE, NULL); + + /* Case 3: refresh + arrow down -> BOOT_KEY_DOWN_ARROW */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_DOWN, KEYBOARD_COL_DOWN, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_DOWN_ARROW, NULL); + + /* Case 4: refresh + L shift -> BOOT_KEY_LEFT_SHIFT */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_LEFT_SHIFT, + KEYBOARD_COL_LEFT_SHIFT, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_LEFT_SHIFT, NULL); + + /* Case 5: refresh + esc + other random key -> BOOT_KEY_NONE */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_ESC, KEYBOARD_COL_ESC, true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_KEY_0, KEYBOARD_COL_KEY_0, + true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_NONE, NULL); + + /* Case 6: BOOT_KEY_NONE after late sysjump */ + system_jumped_late_fake.return_val = 1; + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_LEFT_SHIFT, + KEYBOARD_COL_LEFT_SHIFT, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_NONE, NULL); +} + +ZTEST(keyboard_scan, test_press_enter) +{ + zassert_ok(emulate_keystate(4, 11, true), NULL); + k_sleep(K_MSEC(100)); + /* TODO(jbettis): Check espi_emul to verify the AP was notified. */ + zassert_ok(emulate_keystate(4, 11, false), NULL); + k_sleep(K_MSEC(100)); +} + +ZTEST(keyboard_scan, console_command_ksstate__noargs) +{ + const char *outbuffer; + size_t buffer_size; + + /* With no args, print current state */ + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate"), NULL); + outbuffer = + shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + + /* Check for some expected lines */ + zassert_true(buffer_size > 0, NULL); + zassert_ok(!strstr(outbuffer, "Keyboard scan disable mask: 0x00000000"), + "Output was: `%s`", outbuffer); + zassert_ok(!strstr(outbuffer, "Keyboard scan state printing off"), + "Output was: `%s`", outbuffer); + + /* Ensure we are still scanning */ + zassert_true(keyboard_scan_is_enabled(), NULL); +} + +ZTEST(keyboard_scan, console_command_ksstate__force) +{ + /* This command forces the keyboard to start scanning (if not already) + * and enable state change printing. To test: turn scanning off, run + * command, and verify we are scanning and printing state + */ + + keyboard_scan_enable(false, -1); + zassume_false(keyboard_scan_is_enabled(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate force"), NULL); + + zassert_true(keyboard_scan_is_enabled(), NULL); + zassert_true(keyboard_scan_get_print_state_changes(), NULL); +} + +ZTEST(keyboard_scan, console_command_ksstate__on_off) +{ + /* This command turns state change printing on/off */ + + zassume_false(keyboard_scan_get_print_state_changes(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate on"), NULL); + zassert_true(keyboard_scan_get_print_state_changes(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate off"), NULL); + zassert_false(keyboard_scan_get_print_state_changes(), NULL); +} + +ZTEST(keyboard_scan, console_command_ksstate__invalid) +{ + /* Pass a string that cannot be parsed as a bool */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "ksstate xyz"), NULL); +} + +ZTEST(keyboard_scan, console_command_kbpress__noargs) +{ + const char *outbuffer; + size_t buffer_size; + + /* With no args, print list of simulated keys */ + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress"), NULL); + outbuffer = + shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + + /* Check for an expected line */ + zassert_true(buffer_size > 0, NULL); + zassert_ok(!strstr(outbuffer, "Simulated keys:"), "Output was: `%s`", + outbuffer); +} + +ZTEST(keyboard_scan, console_command_kbpress__invalid) +{ + /* Row or column number out of range, or wrong type */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "kbpress -1 0"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "kbpress foo 0"), NULL); + zassert_ok(!shell_execute_cmd( + get_ec_shell(), + "kbpress " STRINGIFY(KEYBOARD_COLS_MAX) " 0"), + NULL); + + zassert_ok(!shell_execute_cmd(get_ec_shell(), "kbpress 0 -1"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "kbpress 0 foo"), NULL); + zassert_ok( + !shell_execute_cmd(get_ec_shell(), + "kbpress 0 " STRINGIFY(KEYBOARD_COLS_MAX)), + NULL); +} + +/* Mock the key_state_changed callback that the key scan task invokes whenever + * a key event occurs. This will capture a history of key presses. + */ +FAKE_VOID_FUNC(key_state_changed, int, int, uint8_t); + +ZTEST(keyboard_scan, console_command_kbpress__press_and_release) +{ + /* Pres and release a key */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 1 2"), NULL); + + /* Hold a key down */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 3 4 1"), NULL); + + /* Release the key */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 3 4 0"), NULL); + + /* Pause a bit to allow the key scan task to process. */ + k_sleep(K_MSEC(200)); + + /* Expect four key events */ + zassert_equal(4, key_state_changed_fake.call_count, NULL); + + /* Press col=1,row=2 (state==1) */ + zassert_equal(1, key_state_changed_fake.arg1_history[0], NULL); + zassert_equal(2, key_state_changed_fake.arg0_history[0], NULL); + zassert_true(key_state_changed_fake.arg2_history[0], NULL); + + /* Release col=1,row=2 (state==0) */ + zassert_equal(1, key_state_changed_fake.arg1_history[1], NULL); + zassert_equal(2, key_state_changed_fake.arg0_history[1], NULL); + zassert_false(key_state_changed_fake.arg2_history[1], NULL); + + /* Press col=3,row=4 (state==1) */ + zassert_equal(3, key_state_changed_fake.arg1_history[2], NULL); + zassert_equal(4, key_state_changed_fake.arg0_history[2], NULL); + zassert_true(key_state_changed_fake.arg2_history[2], NULL); + + /* Release col=3,row=4 (state==0) */ + zassert_equal(3, key_state_changed_fake.arg1_history[3], NULL); + zassert_equal(4, key_state_changed_fake.arg0_history[3], NULL); + zassert_false(key_state_changed_fake.arg2_history[3], NULL); +} + +ZTEST(keyboard_scan, host_command_simulate_key__locked) +{ + uint16_t ret; + + zassume_true(system_is_locked(), "Expecting locked system."); + + struct ec_response_keyboard_factory_test response; + struct ec_params_mkbp_simulate_key params; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MKBP_SIMULATE_KEY, 0, response, params); + + ret = host_command_process(&args); + zassert_equal(EC_RES_ACCESS_DENIED, ret, "Command returned %u", ret); +} + +ZTEST(keyboard_scan, host_command_simulate_key__bad_params) +{ + uint16_t ret; + + system_is_locked_fake.return_val = 0; + zassume_false(system_is_locked(), "Expecting unlocked system."); + + struct ec_response_keyboard_factory_test response; + struct ec_params_mkbp_simulate_key params = { + .col = KEYBOARD_COLS_MAX, + .row = KEYBOARD_ROWS, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MKBP_SIMULATE_KEY, 0, response, params); + + ret = host_command_process(&args); + zassert_equal(EC_RES_INVALID_PARAM, ret, "Command returned %u", ret); +} + +/** + * @brief Helper function that sends a host command to press or release the + * specified key. + * + * @param col Key column + * @param row Key row + * @param pressed 1=press, 0=release + * @return uint16_t Host command return code. + */ +static uint16_t send_keypress_host_command(uint8_t col, uint8_t row, + uint8_t pressed) +{ + struct ec_params_mkbp_simulate_key params = { + .col = col, + .row = row, + .pressed = pressed, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_MKBP_SIMULATE_KEY, 0, params); + + return host_command_process(&args); +} + +ZTEST(keyboard_scan, host_command_simulate__key_press) +{ + uint16_t ret; + + system_is_locked_fake.return_val = 0; + zassume_false(system_is_locked(), "Expecting unlocked system."); + + ret = send_keypress_host_command(1, 2, 1); + zassert_equal(EC_RES_SUCCESS, ret, "Command returned %u", ret); + + /* Release the key */ + ret = send_keypress_host_command(1, 2, 0); + zassert_equal(EC_RES_SUCCESS, ret, "Command returned %u", ret); + + /* Verify key events happened */ + + zassert_equal(2, key_state_changed_fake.call_count, NULL); + + /* Press col=1,row=2 (state==1) */ + zassert_equal(1, key_state_changed_fake.arg1_history[0], NULL); + zassert_equal(2, key_state_changed_fake.arg0_history[0], NULL); + zassert_true(key_state_changed_fake.arg2_history[0], NULL); + + /* Release col=1,row=2 (state==0) */ + zassert_equal(1, key_state_changed_fake.arg1_history[1], NULL); + zassert_equal(2, key_state_changed_fake.arg0_history[1], NULL); + zassert_false(key_state_changed_fake.arg2_history[1], NULL); +} + +FAKE_VOID_FUNC(system_enter_hibernate, uint32_t, uint32_t); +FAKE_VOID_FUNC(chipset_reset, int); + +ZTEST(keyboard_scan, special_key_combos) +{ + system_is_locked_fake.return_val = 0; + zassume_false(system_is_locked(), "Expecting unlocked system."); + + /* Set the volume up key coordinates to something arbitrary */ + int vol_up_col = 1; + int vol_up_row = 2; + + set_vol_up_key(vol_up_row, vol_up_col); + + /* Vol up and the alt keys must be in different columns */ + zassume_false(vol_up_col == KEYBOARD_COL_LEFT_ALT, NULL); + + /* Hold down volume up, left alt (either alt key works), and R */ + zassert_ok(send_keypress_host_command(vol_up_col, vol_up_row, 1), NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_LEFT_ALT, + KEYBOARD_ROW_LEFT_ALT, 1), + NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_R, + KEYBOARD_ROW_KEY_R, 1), + NULL); + + k_sleep(K_MSEC(100)); + + /* Release R and the press H */ + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_R, + KEYBOARD_ROW_KEY_R, 0), + NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_H, + KEYBOARD_ROW_KEY_H, 1), + NULL); + + k_sleep(K_MSEC(100)); + + /* Release all */ + zassert_ok(send_keypress_host_command(vol_up_col, vol_up_row, 0), NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_LEFT_ALT, + KEYBOARD_ROW_LEFT_ALT, 0), + NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_H, + KEYBOARD_ROW_KEY_H, 0), + NULL); + + /* Check that a reboot was requested (VOLUP + ALT + R) */ + zassert_equal(1, chipset_reset_fake.call_count, + "Did not try to reboot"); + zassert_equal(CHIPSET_RESET_KB_WARM_REBOOT, + chipset_reset_fake.arg0_history[0], NULL); + + /* Check that we called system_enter_hibernate (VOLUP + ALT + H) */ + zassert_equal(1, system_enter_hibernate_fake.call_count, + "Did not enter hibernate"); +} + +static void reset_keyboard(void *data) +{ + ARG_UNUSED(data); + + /* Enable scanning and clear all reason bits (reason bits explain why + * scanning was disabled -- see `enum kb_scan_disable_masks`) + */ + keyboard_scan_enable(true, -1); + + /* Turn off key state change printing */ + keyboard_scan_set_print_state_changes(0); + + /* Reset KB emulator */ + clear_emulated_keys(); + + /* Reset all mocks. */ + RESET_FAKE(key_state_changed); + RESET_FAKE(system_is_locked); + RESET_FAKE(system_enter_hibernate); + RESET_FAKE(chipset_reset); + + /* Be locked by default */ + system_is_locked_fake.return_val = 1; +} + +ZTEST_SUITE(keyboard_scan, drivers_predicate_post_main, NULL, reset_keyboard, + reset_keyboard, NULL); diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c new file mode 100644 index 0000000000..7b49bd1df4 --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c @@ -0,0 +1,19 @@ +/* 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. + */ + +#include <emul/emul_kb_raw.h> +#include <zephyr/drivers/emul.h> + +const static struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw)); + +int emulate_keystate(int row, int col, int pressed) +{ + return emul_kb_raw_set_kbstate(dev, row, col, pressed); +} + +void clear_emulated_keys(void) +{ + emul_kb_raw_reset(dev); +} diff --git a/zephyr/test/drivers/keyboard_scan/src/mkbp_event.c b/zephyr/test/drivers/keyboard_scan/src/mkbp_event.c new file mode 100644 index 0000000000..dee2ba2d1b --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/mkbp_event.c @@ -0,0 +1,189 @@ +/* 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. + */ +#include <zephyr/ztest.h> +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/fff.h> +#include <emul/emul_kb_raw.h> + +#include "console.h" +#include "host_command.h" +#include "mkbp_event.h" +#include "mkbp_fifo.h" +#include "test/drivers/test_mocks.h" +#include "test/drivers/test_state.h" + +/** + * @brief FFF fake that will be registered as a callback to monitor the EC->AP + * interrupt pin. Implements `gpio_callback_handler_t`. + */ +FAKE_VOID_FUNC(interrupt_gpio_monitor, const struct device *, + struct gpio_callback *, gpio_port_pins_t); + +/** + * @brief Fixture to hold state while the suite is running. + */ +struct event_fixture { + /** Configuration for the interrupt pin change callback */ + struct gpio_callback callback_config; +}; + +static struct event_fixture fixture; + +ZTEST(mkbp_event, host_command_get_events__empty) +{ + /* Issue a host command to get the next event (from any source) */ + uint16_t ret; + struct ec_response_get_next_event response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_NEXT_EVENT, 0, response); + + ret = host_command_process(&args); + zassert_equal(EC_RES_UNAVAILABLE, ret, + "Expected EC_RES_UNAVAILABLE but got %d", ret); +} + +ZTEST(mkbp_event, host_command_get_events__get_event) +{ + /* Dispatch a fake keyboard event and ensure it gets returned by the + * host command. + */ + int ret; + + struct ec_response_get_next_event expected_event = { + .event_type = EC_MKBP_EVENT_KEY_MATRIX, + .data.key_matrix = { + /* Arbitrary key matrix data (uint8_t[13]) */ + 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, + 0xc, 0xd + }, + }; + + /* Add the above event to the MKBP keyboard FIFO and raise the event */ + + ret = mkbp_fifo_add(expected_event.event_type, + (const uint8_t *)&expected_event.data.key_matrix); + activate_mkbp_with_events(BIT(expected_event.event_type)); + + zassert_equal(EC_SUCCESS, ret, "Got %d when adding to FIFO", ret); + + /* Retrieve this event via host command */ + + struct ec_response_get_next_event response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_NEXT_EVENT, 0, response); + + ret = host_command_process(&args); + zassert_equal(EC_RES_SUCCESS, ret, "Expected EC_RES_SUCCESS but got %d", + ret); + + /* Compare event data in response */ + zassert_equal(expected_event.event_type, response.event_type, + "Got event type 0x%02x", response.event_type); + zassert_mem_equal(&expected_event.data.key_matrix, + &response.data.key_matrix, + sizeof(expected_event.data.key_matrix), + "Event data payload does not match."); + + /* Check for two pin change events (initial assertion when the event + * was sent, and a de-assertion once we retrieved it through the host + * command) + */ + + zassert_equal(2, interrupt_gpio_monitor_fake.call_count, + "Only %d pin events", + interrupt_gpio_monitor_fake.call_count); +} + +ZTEST(mkbp_event, no_ap_response) +{ + /* Cause an event but do not send any host commands. This should cause + * the EC to send the interrupt to the AP 3 times before giving up. + * Use the GPIO emulator to monitor for interrupts. + */ + + int ret; + + struct ec_response_get_next_event expected_event = { + .event_type = EC_MKBP_EVENT_KEY_MATRIX, + }; + + ret = mkbp_fifo_add(expected_event.event_type, + (uint8_t *)&expected_event.data.key_matrix); + activate_mkbp_with_events(BIT(expected_event.event_type)); + zassert_equal(EC_SUCCESS, ret, "Got %d when adding to FIFO", ret); + + /* EC will attempt to signal the interrupt 3 times. Each attempt lasts + * 1 second, so sleep for 5 and then count the number of times the + * interrupt pin was asserted. (It does not get de-asserted) + */ + + k_sleep(K_SECONDS(5)); + + zassert_equal(3, interrupt_gpio_monitor_fake.call_count, + "Interrupt pin asserted only %d times.", + interrupt_gpio_monitor_fake.call_count); +} + +/* Set up a mock for mkbp_send_event(). This function is called by the MKBP + * event sources to signal that a new event is available for servicing. Since we + * are unit testing just event handling code, we do not want the various event + * source tasks to raise unexpected events during testing and throw us off. + * This mock will essentially cause mkbp_send_event() to become a no-op and + * block the reset of the EC code from raising events and interfering. The test + * code will bypass this by calling mkbp_event.c's internal + * `activate_mkbp_with_events()` directly. + */ +FAKE_VALUE_FUNC(int, mkbp_send_event, uint8_t); + +static void *setup(void) +{ + /* Add a callback to the EC->AP interrupt pin so we can log interrupt + * attempts with an FFF fake. + */ + + const struct gpio_dt_spec *interrupt_pin = + GPIO_DT_FROM_NODELABEL(gpio_ap_ec_int_l); + + fixture.callback_config = (struct gpio_callback){ + .pin_mask = BIT(interrupt_pin->pin), + .handler = interrupt_gpio_monitor, + }; + + zassume_ok(gpio_add_callback(interrupt_pin->port, + &fixture.callback_config), + "Could not configure GPIO callback."); + + return &fixture; +} + +static void teardown(void *data) +{ + /* Remove the GPIO callback on the interrupt pin */ + + struct event_fixture *f = (struct event_fixture *)data; + const struct gpio_dt_spec *interrupt_pin = + GPIO_DT_FROM_NODELABEL(gpio_ap_ec_int_l); + + gpio_remove_callback(interrupt_pin->port, &f->callback_config); +} + +static void reset_events(void *data) +{ + /* Clear any keyboard scan events (type EC_MKBP_EVENT_KEY_MATRIX) */ + mkbp_clear_fifo(); + + /* Clear pending events */ + mkbp_event_clear_all(); + + /* Mock reset */ + RESET_FAKE(interrupt_gpio_monitor); + RESET_FAKE(mkbp_send_event); + mkbp_send_event_fake.return_val = 1; +} + +ZTEST_SUITE(mkbp_event, drivers_predicate_post_main, setup, reset_events, + reset_events, teardown); diff --git a/zephyr/test/drivers/keyboard_scan/src/mkbp_info.c b/zephyr/test/drivers/keyboard_scan/src/mkbp_info.c new file mode 100644 index 0000000000..b0d64eb1da --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/mkbp_info.c @@ -0,0 +1,232 @@ +/* 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. + */ +#include <zephyr/ztest.h> +#include <zephyr/fff.h> +#include <emul/emul_kb_raw.h> + +#include "console.h" +#include "host_command.h" +#include "keyboard_scan.h" +#include "keyboard_test_utils.h" +#include "mkbp_info.h" +#include "mkbp_input_devices.h" +#include "test/drivers/test_state.h" + +ZTEST(mkbp_info, host_command_mkbp_info__keyboard_info) +{ + /* Get the number of keyboard rows and columns */ + + int ret; + struct ec_response_mkbp_info response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_KBD, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(KEYBOARD_ROWS, response.rows, NULL); + zassert_equal(KEYBOARD_COLS_MAX, response.cols, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__supported_buttons) +{ + /* Get the set of supported buttons */ + + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_SUPPORTED, + .event_type = EC_MKBP_EVENT_BUTTON, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(get_supported_buttons(), response.buttons, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__supported_switches) +{ + /* Get the set of supported switches */ + + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_SUPPORTED, + .event_type = EC_MKBP_EVENT_SWITCH, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(get_supported_switches(), response.switches, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__supported_invalid) +{ + /* Request support info on a non-existent type of input device. */ + + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_SUPPORTED, + .event_type = EC_MKBP_EVENT_COUNT, /* Unsupported */ + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_RES_INVALID_PARAM, ret, + "Host command didn't fail properly: %d", ret); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_keyboard_matrix) +{ + /* Hold down a key so we can validate the returned keyboard matrix state + */ + const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw)); + + emul_kb_raw_set_kbstate(dev, KEYBOARD_ROW_KEY_R, KEYBOARD_COL_KEY_R, 1); + keyboard_scan_init(); + + k_sleep(K_MSEC(100)); + + /* Get the current keyboard matrix state */ + + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_KEY_MATRIX, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + + zassert_true(response.key_matrix[KEYBOARD_COL_KEY_R] & + KEYBOARD_MASK_KEY_R, + "Expected key is not pressed"); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_host_events) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_HOST_EVENT, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal((uint32_t)host_get_events(), response.host_event, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_host_events64) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_HOST_EVENT64, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(host_get_events(), response.host_event64, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_buttons) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_BUTTON, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(mkbp_get_button_state(), response.buttons, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_switches) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_SWITCH, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(mkbp_get_switch_state(), response.switches, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_invalid) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_COUNT, /* Unsupported */ + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_RES_INVALID_PARAM, ret, "Host command failed: %d", + ret); +} + +ZTEST(mkbp_info, host_command_mkbp_info__invalid) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = -1, /* Unsupported */ + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_RES_ERROR, ret, "Host command failed: %d", ret); +} + +static void reset(void *data) +{ + ARG_UNUSED(data); + + /* Release any pressed keys in the emulator */ + clear_emulated_keys(); +} + +ZTEST_SUITE(mkbp_info, drivers_predicate_post_main, NULL, reset, reset, NULL); |