Add GPIO get/set host command

These commands are used in factory test. If system is locked, GPIO
commands are disabled for security reason.

BUG=chrome-os-partner:11164
TEST= - 'ectool gpioget enable_backlight' gives 1.
      - 'ectool gpioset enable_backlight 0' turns off display.
      - Lock system. Check these commands return error.

Change-Id: I3ea41285075ebe963ba7d30e4ae183cef9b1c105
Reviewed-on: https://gerrit.chromium.org/gerrit/27019
Commit-Ready: Vic Yang <victoryang@chromium.org>
Reviewed-by: Vic Yang <victoryang@chromium.org>
Tested-by: Vic Yang <victoryang@chromium.org>
This commit is contained in:
Vic Yang
2012-07-11 10:10:47 +08:00
committed by Gerrit
parent dbefb29f02
commit 37f19ecc84
3 changed files with 144 additions and 1 deletions

View File

@@ -8,6 +8,8 @@
#include "board.h"
#include "console.h"
#include "gpio.h"
#include "host_command.h"
#include "system.h"
#include "util.h"
@@ -48,6 +50,8 @@ static int last_val_changed(int i, int v)
}
}
/*****************************************************************************/
/* Console commands */
static int command_gpio_get(int argc, char **argv)
{
@@ -118,3 +122,55 @@ DECLARE_CONSOLE_COMMAND(gpioset, command_gpio_set,
"name <0 | 1>",
"Set a GPIO",
NULL);
/*****************************************************************************/
/* Host commands */
static int gpio_command_get(struct host_cmd_handler_args *args)
{
struct ec_params_gpio_get *p =
(struct ec_params_gpio_get *)args->params;
struct ec_response_gpio_get *r =
(struct ec_response_gpio_get *)args->response;
int i;
if (system_is_locked())
return EC_RES_ACCESS_DENIED;
i = find_signal_by_name(p->name);
if (i == GPIO_COUNT)
return EC_RES_ERROR;
r->val = gpio_get_level(i);
args->response_size = sizeof(struct ec_response_gpio_get);
return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_GPIO_GET, gpio_command_get, EC_VER_MASK(0));
static int gpio_command_set(struct host_cmd_handler_args *args)
{
struct ec_params_gpio_set *p =
(struct ec_params_gpio_set *)args->params;
int i;
const struct gpio_info *g;
if (system_is_locked())
return EC_RES_ACCESS_DENIED;
i = find_signal_by_name(p->name);
if (i == GPIO_COUNT)
return EC_RES_ERROR;
g = gpio_list + i;
if (!g->mask)
return EC_RES_ERROR;
if (!(g->flags & GPIO_OUTPUT))
return EC_RES_ERROR;
if (gpio_set_level(i, p->val) != EC_SUCCESS)
return EC_RES_ERROR;
return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_GPIO_SET, gpio_command_set, EC_VER_MASK(0));

View File

@@ -781,7 +781,7 @@ struct ec_response_host_event_mask {
#define EC_CMD_HOST_EVENT_CLEAR_B 0x8f
/*****************************************************************************/
/* GPIO switch commands */
/* Switch commands */
/* Enable/disable LCD backlight */
#define EC_CMD_SWITCH_ENABLE_BKLIGHT 0x90
@@ -797,6 +797,27 @@ struct ec_params_switch_enable_wireless {
uint8_t enabled;
} __packed;
/*****************************************************************************/
/* GPIO commands. Only available on EC if write protect has been disabled. */
/* Set GPIO output value */
#define EC_CMD_GPIO_SET 0x92
struct ec_params_gpio_set {
char name[32];
uint8_t val;
} __packed;
/* Get GPIO value */
#define EC_CMD_GPIO_GET 0x93
struct ec_params_gpio_get {
char name[32];
} __packed;
struct ec_response_gpio_get {
uint8_t val;
} __packed;
/*****************************************************************************/
/* I2C commands. Only available when flash write protect is unlocked. */

View File

@@ -69,6 +69,10 @@ const char help_str[] =
" Reads from EC flash to a file\n"
" flashwrite <offset> <infile>\n"
" Writes to EC flash from a file\n"
" gpioget <GPIO name>\n"
" Get the value of GPIO signal\n"
" gpioset <GPIO name>\n"
" Set the value of GPIO signal\n"
" hello\n"
" Checks for basic communication with EC\n"
" kbpress\n"
@@ -1678,6 +1682,66 @@ int cmd_charge_force_idle(int argc, char *argv[])
}
int cmd_gpio_get(int argc, char *argv[])
{
struct ec_params_gpio_get p;
struct ec_response_gpio_get r;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <GPIO name>\n", argv[0]);
return -1;
}
if (strlen(argv[1]) + 1 > sizeof(p.name)) {
fprintf(stderr, "GPIO name too long.\n");
return -1;
}
strcpy(p.name, argv[1]);
rv = ec_command(EC_CMD_GPIO_GET, 0,
&p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
printf("GPIO %s = %d\n", p.name, r.val);
return 0;
}
int cmd_gpio_set(int argc, char *argv[])
{
struct ec_params_gpio_set p;
char *e;
int rv;
if (argc != 3) {
fprintf(stderr, "Usage: %s <GPIO name> <0 | 1>\n", argv[0]);
return -1;
}
if (strlen(argv[1]) + 1 > sizeof(p.name)) {
fprintf(stderr, "GPIO name too long.\n");
return -1;
}
strcpy(p.name, argv[1]);
p.val = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad value.\n");
return -1;
}
rv = ec_command(EC_CMD_GPIO_SET, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("GPIO %s set to %d\n", p.name, p.val);
return 0;
}
int cmd_battery(int argc, char *argv[])
{
char batt_text[EC_MEMMAP_TEXT_MAX];
@@ -1905,6 +1969,8 @@ const struct command commands[] = {
{"flashread", cmd_flash_read},
{"flashwrite", cmd_flash_write},
{"flashinfo", cmd_flash_info},
{"gpioget", cmd_gpio_get},
{"gpioset", cmd_gpio_set},
{"hello", cmd_hello},
{"kbpress", cmd_kbpress},
{"i2cread", cmd_i2c_read},