1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2023 Linaro Ltd.
13 /* Some sane limit "just in case" */
14 #define MAX_BTN_CMDS 32
23 * Button commands are set via environment variables, e.g.:
24 * button_cmd_N_name=Volume Up
25 * button_cmd_N=fastboot usb 0
27 * This function will retrieve the command for the given button N
28 * and populate the cmd struct with the command string and pressed
29 * state of the button.
31 * Returns 1 if a command was found, 0 otherwise.
33 static int get_button_cmd(int n, struct button_cmd *cmd)
36 struct udevice *btn = NULL;
39 snprintf(buf, sizeof(buf), "button_cmd_%d_name", n);
40 cmd->btn_name = env_get(buf);
44 button_get_by_label(cmd->btn_name, &btn);
46 log_err("No button labelled '%s'\n", cmd->btn_name);
50 cmd->pressed = button_get_state(btn) == BUTTON_ON;
51 /* If the button isn't pressed then cmd->cmd will be unused so don't waste
57 snprintf(buf, sizeof(buf), "button_cmd_%d", n);
58 cmd_str = env_get(buf);
60 log_err("No command set for button '%s'\n", cmd->btn_name);
69 void process_button_cmds(void)
71 struct button_cmd cmd = {0};
74 while (get_button_cmd(i++, &cmd) && i < MAX_BTN_CMDS) {
78 log_info("BTN '%s'> %s\n", cmd.btn_name, cmd.cmd);
79 run_command(cmd.cmd, CMD_FLAG_ENV);
80 /* Don't run commands for multiple buttons */