From 00d63619edf7b6c1f62d7cfce7a2bd520635dbd3 Mon Sep 17 00:00:00 2001 From: Ting Shen Date: Thu, 20 May 2021 18:16:41 +0800 Subject: base_state: implement basestate host command This CL introduces an unified method to force base attach/detach, to deprecate the hard-coded gpio pin name table in hammerd/hammertests/common.py. Also modifies base_force_state to use the same parameter type as host command. BUG=b:188625010 TEST=manually, run `ectool basestate attach|detach|reset` on coachz BRANCH=trogdor,kukui Signed-off-by: Ting Shen Change-Id: I5235661727cbbd15015c49d588ec70605e4a33e8 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2910472 Reviewed-by: Wai-Hong Tam Tested-by: Ting Shen Commit-Queue: Ting Shen --- baseboard/kukui/base_detect_kukui.c | 12 +++++++----- board/cheza/base_detect.c | 6 +++--- board/coachz/base_detect.c | 6 +++--- board/homestar/base_detect.c | 6 +++--- board/nocturne/base_detect.c | 6 +++--- board/poppy/base_detect_lux.c | 6 +++--- board/poppy/base_detect_poppy.c | 6 +++--- common/base_state.c | 20 +++++++++++++++++--- include/base_state.h | 4 +++- include/ec_commands.h | 12 ++++++++++++ util/ectool.c | 32 ++++++++++++++++++++++++++++++++ 11 files changed, 89 insertions(+), 27 deletions(-) diff --git a/baseboard/kukui/base_detect_kukui.c b/baseboard/kukui/base_detect_kukui.c index ac7d9f7616..68542b4fb6 100644 --- a/baseboard/kukui/base_detect_kukui.c +++ b/baseboard/kukui/base_detect_kukui.c @@ -200,9 +200,9 @@ static void pogo_chipset_shutdown(void) } DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, pogo_chipset_shutdown, HOOK_PRIO_DEFAULT); -void base_force_state(int state) +void base_force_state(enum ec_set_base_state_cmd state) { - if (state != 1 && state != 0) { + if (state >= EC_SET_BASE_STATE_RESET) { CPRINTS("BD forced reset"); pogo_chipset_init(); return; @@ -210,9 +210,11 @@ void base_force_state(int state) gpio_disable_interrupt(GPIO_POGO_ADC_INT_L); pogo_type = (state == 1 ? DEVICE_TYPE_KEYBOARD : DEVICE_TYPE_DETACHED); - base_set_device_type(state == 1 ? DEVICE_TYPE_KEYBOARD : - DEVICE_TYPE_DETACHED); - CPRINTS("BD forced %sconnected", state == 1 ? "" : "dis"); + base_set_device_type(state == EC_SET_BASE_STATE_ATTACH + ? DEVICE_TYPE_KEYBOARD + : DEVICE_TYPE_DETACHED); + CPRINTS("BD forced %sconnected", state == EC_SET_BASE_STATE_ATTACH ? + "" : "dis"); } #ifdef VARIANT_KUKUI_POGO_DOCK diff --git a/board/cheza/base_detect.c b/board/cheza/base_detect.c index 3999ad022f..b47504144b 100644 --- a/board/cheza/base_detect.c +++ b/board/cheza/base_detect.c @@ -198,13 +198,13 @@ static void base_init(void) } DECLARE_HOOK(HOOK_INIT, base_init, HOOK_PRIO_DEFAULT+1); -void base_force_state(int state) +void base_force_state(enum ec_set_base_state_cmd state) { - if (state == 1) { + if (state == EC_SET_BASE_STATE_ATTACH) { gpio_disable_interrupt(GPIO_CC_LID_BASE_ADC); base_detect_change(BASE_CONNECTED); CPRINTS("BD forced connected"); - } else if (state == 0) { + } else if (state == EC_SET_BASE_STATE_DETACH) { gpio_disable_interrupt(GPIO_CC_LID_BASE_ADC); base_detect_change(BASE_DISCONNECTED); CPRINTS("BD forced disconnected"); diff --git a/board/coachz/base_detect.c b/board/coachz/base_detect.c index 505714a016..1a27538166 100644 --- a/board/coachz/base_detect.c +++ b/board/coachz/base_detect.c @@ -214,13 +214,13 @@ static void base_init(void) } DECLARE_HOOK(HOOK_INIT, base_init, HOOK_PRIO_DEFAULT+1); -void base_force_state(int state) +void base_force_state(enum ec_set_base_state_cmd state) { - if (state == 1) { + if (state == EC_SET_BASE_STATE_ATTACH) { gpio_disable_interrupt(GPIO_BASE_DET_L); base_detect_change(BASE_CONNECTED); CPRINTS("BD forced connected"); - } else if (state == 0) { + } else if (state == EC_SET_BASE_STATE_DETACH) { gpio_disable_interrupt(GPIO_BASE_DET_L); base_detect_change(BASE_DISCONNECTED); CPRINTS("BD forced disconnected"); diff --git a/board/homestar/base_detect.c b/board/homestar/base_detect.c index 59303fa1b2..53688777c1 100644 --- a/board/homestar/base_detect.c +++ b/board/homestar/base_detect.c @@ -213,13 +213,13 @@ static void base_init(void) } DECLARE_HOOK(HOOK_INIT, base_init, HOOK_PRIO_DEFAULT+1); -void base_force_state(int state) +void base_force_state(enum ec_set_base_state_cmd state) { - if (state == 1) { + if (state == EC_SET_BASE_STATE_ATTACH) { gpio_disable_interrupt(GPIO_BASE_DET_L); base_detect_change(BASE_CONNECTED); CPRINTS("BD forced connected"); - } else if (state == 0) { + } else if (state == EC_SET_BASE_STATE_DETACH) { gpio_disable_interrupt(GPIO_BASE_DET_L); base_detect_change(BASE_DISCONNECTED); CPRINTS("BD forced disconnected"); diff --git a/board/nocturne/base_detect.c b/board/nocturne/base_detect.c index 3735080c49..48c7b1f9dd 100644 --- a/board/nocturne/base_detect.c +++ b/board/nocturne/base_detect.c @@ -380,11 +380,11 @@ DECLARE_CONSOLE_COMMAND(basedebug, command_basedetectdebug, "[ena|dis]", "En/Disable base detection debug"); -void base_force_state(int state) +void base_force_state(enum ec_set_base_state_cmd state) { - if (state == 1) + if (state == EC_SET_BASE_STATE_ATTACH) forced_state = BASE_ATTACHED; - else if (state == 0) + else if (state == EC_SET_BASE_STATE_DETACH) forced_state = BASE_DETACHED; else forced_state = BASE_NO_FORCED_STATE; diff --git a/board/poppy/base_detect_lux.c b/board/poppy/base_detect_lux.c index 155243af0e..14a937a7fd 100644 --- a/board/poppy/base_detect_lux.c +++ b/board/poppy/base_detect_lux.c @@ -219,13 +219,13 @@ static void base_init(void) } DECLARE_HOOK(HOOK_INIT, base_init, HOOK_PRIO_DEFAULT+1); -void base_force_state(int state) +void base_force_state(enum ec_set_base_state_cmd state) { - if (state == 1) { + if (state == EC_SET_BASE_STATE_ATTACH) { gpio_disable_interrupt(GPIO_BASE_DET_A); base_detect_change(BASE_CONNECTED); CPRINTS("BD forced connected"); - } else if (state == 0) { + } else if (state == EC_SET_BASE_STATE_DETACH) { gpio_disable_interrupt(GPIO_BASE_DET_A); base_detect_change(BASE_DISCONNECTED); CPRINTS("BD forced disconnected"); diff --git a/board/poppy/base_detect_poppy.c b/board/poppy/base_detect_poppy.c index 358461896e..8a202a21d9 100644 --- a/board/poppy/base_detect_poppy.c +++ b/board/poppy/base_detect_poppy.c @@ -246,13 +246,13 @@ static void base_init(void) } DECLARE_HOOK(HOOK_INIT, base_init, HOOK_PRIO_DEFAULT+1); -void base_force_state(int state) +void base_force_state(enum ec_set_base_state_cmd state) { - if (state == 1) { + if (state == EC_SET_BASE_STATE_ATTACH) { gpio_disable_interrupt(GPIO_BASE_DET_A); base_detect_change(BASE_CONNECTED); CPRINTS("BD forced connected"); - } else if (state == 0) { + } else if (state == EC_SET_BASE_STATE_DETACH) { gpio_disable_interrupt(GPIO_BASE_DET_A); base_detect_change(BASE_DISCONNECTED); CPRINTS("BD forced disconnected"); diff --git a/common/base_state.c b/common/base_state.c index 053b6d4b23..43e201cab9 100644 --- a/common/base_state.c +++ b/common/base_state.c @@ -38,11 +38,11 @@ static int command_setbasestate(int argc, char **argv) if (argc != 2) return EC_ERROR_PARAM_COUNT; if (argv[1][0] == 'a') - base_force_state(1); + base_force_state(EC_SET_BASE_STATE_ATTACH); else if (argv[1][0] == 'd') - base_force_state(0); + base_force_state(EC_SET_BASE_STATE_DETACH); else if (argv[1][0] == 'r') - base_force_state(2); + base_force_state(EC_SET_BASE_STATE_RESET); else return EC_ERROR_PARAM1; @@ -52,3 +52,17 @@ static int command_setbasestate(int argc, char **argv) DECLARE_CONSOLE_COMMAND(basestate, command_setbasestate, "[attach | detach | reset]", "Manually force base state to attached, detached or reset."); + +static enum ec_status hostcmd_setbasestate(struct host_cmd_handler_args *args) +{ + const struct ec_params_set_base_state *params = args->params; + + if (params->cmd > EC_SET_BASE_STATE_RESET) + return EC_RES_INVALID_PARAM; + + base_force_state(params->cmd); + + return EC_SUCCESS; +} +DECLARE_HOST_COMMAND(EC_CMD_SET_BASE_STATE, hostcmd_setbasestate, + EC_VER_MASK(0)); diff --git a/include/base_state.h b/include/base_state.h index 6363ebdd7a..d8c72e5663 100644 --- a/include/base_state.h +++ b/include/base_state.h @@ -3,6 +3,8 @@ * found in the LICENSE file. */ +#include "ec_commands.h" + /** * Return 1 if base attached, 0 otherwise. */ @@ -19,4 +21,4 @@ void base_set_state(int state); * Force the current state of the base, with 0 meaning detached, * 1 meaning attached and 2 meaning reset to the original state. */ -void base_force_state(int state); +void base_force_state(enum ec_set_base_state_cmd state); diff --git a/include/ec_commands.h b/include/ec_commands.h index 90c3de8c42..0649a42797 100644 --- a/include/ec_commands.h +++ b/include/ec_commands.h @@ -6914,6 +6914,18 @@ struct ec_response_display_soc { } __ec_align2; +#define EC_CMD_SET_BASE_STATE 0x0138 + +struct ec_params_set_base_state { + uint8_t cmd; /* enum ec_set_base_state_cmd */ +} __ec_align1; + +enum ec_set_base_state_cmd { + EC_SET_BASE_STATE_DETACH = 0, + EC_SET_BASE_STATE_ATTACH, + EC_SET_BASE_STATE_RESET, +}; + /*****************************************************************************/ /* The command range 0x200-0x2FF is reserved for Rotor. */ diff --git a/util/ectool.c b/util/ectool.c index 25161265c3..65bb7c0a94 100644 --- a/util/ectool.c +++ b/util/ectool.c @@ -73,6 +73,8 @@ const char help_str[] = " Turn on automatic fan speed control.\n" " backlight \n" " Enable/disable LCD backlight\n" + " basestate [attach | detach | reset]\n" + " Manually force base state to attached, detached or reset.\n" " battery\n" " Prints battery info\n" " batterycutoff [at-shutdown]\n" @@ -7437,6 +7439,35 @@ int cmd_lcd_backlight(int argc, char *argv[]) return 0; } +static void cmd_basestate_help(void) +{ + fprintf(stderr, + "Usage: ectool basestate [attach | detach | reset]\n"); +} + +int cmd_basestate(int argc, char *argv[]) +{ + struct ec_params_set_base_state p; + + if (argc != 2) { + cmd_basestate_help(); + return -1; + } + + if (!strncmp(argv[1], "attach", 6)) { + p.cmd = EC_SET_BASE_STATE_ATTACH; + } else if (!strncmp(argv[1], "detach", 6)) { + p.cmd = EC_SET_BASE_STATE_DETACH; + } else if (!strncmp(argv[1], "reset", 5)) { + p.cmd = EC_SET_BASE_STATE_RESET; + } else { + cmd_basestate_help(); + return -1; + } + + return ec_command(EC_CMD_SET_BASE_STATE, 0, + &p, sizeof(p), NULL, 0); +} int cmd_ext_power_limit(int argc, char *argv[]) { @@ -10497,6 +10528,7 @@ const struct command commands[] = { {"apreset", cmd_apreset}, {"autofanctrl", cmd_thermal_auto_fan_ctrl}, {"backlight", cmd_lcd_backlight}, + {"basestate", cmd_basestate}, {"battery", cmd_battery}, {"batterycutoff", cmd_battery_cut_off}, {"batteryparam", cmd_battery_vendor_param}, -- cgit v1.2.1