diff options
author | Bill Richardson <wfrichar@chromium.org> | 2013-05-29 18:00:03 -0700 |
---|---|---|
committer | ChromeBot <chrome-bot@google.com> | 2013-06-05 14:12:23 -0700 |
commit | d0d1564434ae991ac99a6ce54f6f3360f47017a8 (patch) | |
tree | 68ce6707147fbf828fef543222fcde756da1acf2 /util/comm-dev.c | |
parent | 26475135b551a742f2d148d426c4aa18b338f5ca (diff) | |
download | chrome-ec-d0d1564434ae991ac99a6ce54f6f3360f47017a8.tar.gz |
ectool prefers /dev/cros_ec, then falls back to i2c, lpc
This is preparation for the common userspace EC interface. If/when that
appears, this will be ready.
BUG=chromium:239197
BRANCH=all
TEST=manual
Build, install, run it. Shouldn't be any change.
Change-Id: I9fa78515ec5443ba659f10a66bbaadcb7f4802b0
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/56131
Diffstat (limited to 'util/comm-dev.c')
-rw-r--r-- | util/comm-dev.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/util/comm-dev.c b/util/comm-dev.c new file mode 100644 index 0000000000..2802aeca24 --- /dev/null +++ b/util/comm-dev.c @@ -0,0 +1,82 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <errno.h> +#include <fcntl.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <sys/ioctl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include "cros_ec_dev.h" +#include "comm-host.h" +#include "ec_commands.h" + +static int fd = -1; + +static int ec_command_dev(int command, int version, + const void *outdata, int outsize, + void *indata, int insize) +{ + struct cros_ec_command s_cmd; + + s_cmd.command = command; + s_cmd.version = version; + s_cmd.result = 0xff; + s_cmd.outsize = outsize; + s_cmd.outdata = (uint8_t *)outdata; + s_cmd.insize = insize; + s_cmd.indata = indata; + + if (ioctl(fd, CROS_EC_DEV_IOCXCMD, &s_cmd)) + return -1; + + return s_cmd.insize; +} + +static int ec_readmem_dev(int offset, int bytes, void *dest) +{ + struct cros_ec_readmem s_mem; + + s_mem.offset = offset; + s_mem.bytes = bytes; + s_mem.buffer = dest; + + return ioctl(fd, CROS_EC_DEV_IOCRDMEM, &s_mem); +} + +int comm_init_dev(void) +{ + char version[80]; + int r; + char *s; + + fd = open("/dev/" CROS_EC_DEV_NAME, O_RDWR); + if (fd < 0) + return 1; + + r = read(fd, version, sizeof(version)-1); + if (r <= 0) { + close(fd); + return 2; + } + version[r] = '\0'; + s = strchr(version, '\n'); + if (s) + *s = '\0'; + if (strcmp(version, CROS_EC_DEV_VERSION)) { + close(fd); + return 3; + } + + ec_command = ec_command_dev; + if (ec_readmem_dev(EC_MEMMAP_ID, 2, version) == 2) + ec_readmem = ec_readmem_dev; + + return 0; +} |