summaryrefslogtreecommitdiff
path: root/util/comm-i2c.c
diff options
context:
space:
mode:
authorBill Richardson <wfrichar@chromium.org>2013-05-29 18:00:03 -0700
committerChromeBot <chrome-bot@google.com>2013-06-05 14:12:23 -0700
commitd0d1564434ae991ac99a6ce54f6f3360f47017a8 (patch)
tree68ce6707147fbf828fef543222fcde756da1acf2 /util/comm-i2c.c
parent26475135b551a742f2d148d426c4aa18b338f5ca (diff)
downloadchrome-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-i2c.c')
-rw-r--r--util/comm-i2c.c151
1 files changed, 43 insertions, 108 deletions
diff --git a/util/comm-i2c.c b/util/comm-i2c.c
index d1471495c8..8f75320b48 100644
--- a/util/comm-i2c.c
+++ b/util/comm-i2c.c
@@ -40,44 +40,6 @@
static int i2c_fd = -1;
-int comm_init(void)
-{
- char *file_path;
- char buffer[64];
- FILE *f;
- int i;
-
- /* find the device number based on the adapter name */
- for (i = 0; i < I2C_MAX_ADAPTER; i++) {
- if (asprintf(&file_path, I2C_ADAPTER_NODE, i) < 0)
- return -1;
- f = fopen(file_path, "r");
- if (f) {
- if (fgets(buffer, sizeof(buffer), f) &&
- !strncmp(buffer, I2C_ADAPTER_NAME, 6)) {
- free(file_path);
- break;
- }
- fclose(f);
- }
- free(file_path);
- }
- if (i == I2C_MAX_ADAPTER) {
- fprintf(stderr, "Cannot find I2C adapter\n");
- return -1;
- }
-
- if (asprintf(&file_path, I2C_NODE, i) < 0)
- return -1;
- debug("using I2C adapter %s\n", file_path);
- i2c_fd = open(file_path, O_RDWR);
- if (i2c_fd < 0)
- fprintf(stderr, "Cannot open %s : %d\n", file_path, errno);
-
- free(file_path);
- return 0;
-}
-
/*
* Sends a command to the EC (protocol v2). Returns the command status code, or
@@ -86,8 +48,9 @@ int comm_init(void)
* Returns >= 0 for success, or negative if error.
*
*/
-int ec_command(int command, int version, const void *indata, int insize,
- void *outdata, int outsize)
+static int ec_command_i2c(int command, int version,
+ const void *outdata, int outsize,
+ void *indata, int insize)
{
struct i2c_rdwr_ioctl_data data;
int ret = -1;
@@ -127,7 +90,7 @@ int ec_command(int command, int version, const void *indata, int insize,
* allocate larger packet
* (version, command, size, ..., checksum)
*/
- req_len = insize + PROTO_V2_IN;
+ req_len = outsize + PROTO_V2_IN;
req_buf = calloc(1, req_len);
if (!req_buf)
goto done;
@@ -135,12 +98,12 @@ int ec_command(int command, int version, const void *indata, int insize,
i2c_msg[0].buf = (char *)req_buf;
req_buf[0] = version + EC_CMD_VERSION0;
req_buf[1] = command;
- req_buf[2] = insize;
+ req_buf[2] = outsize;
debug("i2c req %02x:", command);
sum = req_buf[0] + req_buf[1] + req_buf[2];
/* copy message payload and compute checksum */
- for (i = 0, c = indata; i < insize; i++, c++) {
+ for (i = 0, c = outdata; i < outsize; i++, c++) {
req_buf[i + 3] = *c;
sum += *c;
debug(" %02x", *c);
@@ -152,7 +115,7 @@ int ec_command(int command, int version, const void *indata, int insize,
* allocate larger packet
* (result, size, ..., checksum)
*/
- resp_len = outsize + PROTO_V2_OUT;
+ resp_len = insize + PROTO_V2_OUT;
resp_buf = calloc(1, resp_len);
if (!resp_buf)
goto done;
@@ -173,9 +136,9 @@ int ec_command(int command, int version, const void *indata, int insize,
/* TODO: handle EC_RES_IN_PROGRESS case. */
resp_len = resp_buf[1];
- if (resp_len > outsize) {
+ if (resp_len > insize) {
fprintf(stderr, "response size is too large %d > %d\n",
- resp_len, outsize);
+ resp_len, insize);
ret = -EC_RES_ERROR;
goto done;
}
@@ -185,11 +148,11 @@ int ec_command(int command, int version, const void *indata, int insize,
command, i2c_msg[1].buf[0]);
/* Translate ERROR to -ERROR */
ret = -ret;
- } else if (outsize) {
+ } else if (insize) {
debug("i2c resp :");
/* copy response packet payload and compute checksum */
sum = resp_buf[0] + resp_buf[1];
- for (i = 0, d = outdata; i < resp_len; i++, d++) {
+ for (i = 0, d = indata; i < resp_len; i++, d++) {
*d = resp_buf[i + 2];
sum += *d;
debug(" %02x", *d);
@@ -213,70 +176,42 @@ done:
return ret;
}
-uint8_t read_mapped_mem8(uint8_t offset)
+int comm_init_i2c(void)
{
- struct ec_params_read_memmap p;
- uint8_t val;
-
- p.offset = offset;
- p.size = sizeof(val);
-
- if (ec_command(EC_CMD_READ_MEMMAP, 0, &p, sizeof(p),
- &val, sizeof(val)) < 0)
- return 0xff;
-
- return val;
-}
-
-uint16_t read_mapped_mem16(uint8_t offset)
-{
- struct ec_params_read_memmap p;
- uint16_t val;
-
- p.offset = offset;
- p.size = sizeof(val);
-
- if (ec_command(EC_CMD_READ_MEMMAP, 0, &p, sizeof(p),
- &val, sizeof(val)) < 0)
- return 0xffff;
-
- return val;
-}
-
-uint32_t read_mapped_mem32(uint8_t offset)
-{
- struct ec_params_read_memmap p;
- uint32_t val;
-
- p.offset = offset;
- p.size = sizeof(val);
-
- if (ec_command(EC_CMD_READ_MEMMAP, 0, &p, sizeof(p),
- &val, sizeof(val)) < 0)
- return 0xffffffff;
-
- return val;
-}
-
-int read_mapped_string(uint8_t offset, char *buf)
-{
- struct ec_params_read_memmap p;
- int c;
-
- p.offset = offset;
- p.size = EC_MEMMAP_TEXT_MAX;
+ char *file_path;
+ char buffer[64];
+ FILE *f;
+ int i;
- if (ec_command(EC_CMD_READ_MEMMAP, 0, &p, sizeof(p),
- buf, EC_MEMMAP_TEXT_MAX) < 0) {
- *buf = 0;
+ /* find the device number based on the adapter name */
+ for (i = 0; i < I2C_MAX_ADAPTER; i++) {
+ if (asprintf(&file_path, I2C_ADAPTER_NODE, i) < 0)
+ return -1;
+ f = fopen(file_path, "r");
+ if (f) {
+ if (fgets(buffer, sizeof(buffer), f) &&
+ !strncmp(buffer, I2C_ADAPTER_NAME, 6)) {
+ free(file_path);
+ break;
+ }
+ fclose(f);
+ }
+ free(file_path);
+ }
+ if (i == I2C_MAX_ADAPTER) {
+ fprintf(stderr, "Cannot find I2C adapter\n");
return -1;
}
- for (c = 0; c < EC_MEMMAP_TEXT_MAX; c++) {
- if (buf[c] == 0)
- return c;
- }
+ if (asprintf(&file_path, I2C_NODE, i) < 0)
+ return -1;
+ debug("using I2C adapter %s\n", file_path);
+ i2c_fd = open(file_path, O_RDWR);
+ if (i2c_fd < 0)
+ fprintf(stderr, "Cannot open %s : %d\n", file_path, errno);
- buf[EC_MEMMAP_TEXT_MAX - 1] = 0;
- return EC_MEMMAP_TEXT_MAX - 1;
+ free(file_path);
+
+ ec_command = ec_command_i2c;
+ return 0;
}