summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util/comm-dev.c121
-rw-r--r--util/cros_ec_dev.h38
-rw-r--r--util/misc_util.h1
3 files changed, 156 insertions, 4 deletions
diff --git a/util/comm-dev.c b/util/comm-dev.c
index 6e3cd42c10..94a5cdde52 100644
--- a/util/comm-dev.c
+++ b/util/comm-dev.c
@@ -3,10 +3,12 @@
* found in the LICENSE file.
*/
+#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
@@ -16,6 +18,7 @@
#include "cros_ec_dev.h"
#include "comm-host.h"
#include "ec_commands.h"
+#include "misc_util.h"
static int fd = -1;
@@ -49,6 +52,7 @@ static const char *strresult(int i)
return meanings[i];
}
+/* Old ioctl format, used by Chrome OS 3.18 and older */
static int ec_command_dev(int command, int version,
const void *outdata, int outsize,
@@ -112,8 +116,112 @@ static int ec_readmem_dev(int offset, int bytes, void *dest)
dest, bytes);
}
+/* New ioctl format, used by Chrome OS 4.4 and later as well as upstream 4.0+ */
+
+static int ec_command_dev_v2(int command, int version,
+ const void *outdata, int outsize,
+ void *indata, int insize)
+{
+ struct cros_ec_command_v2 *s_cmd;
+ int r;
+
+ assert(outsize == 0 || outdata != NULL);
+ assert(insize == 0 || indata != NULL);
+
+ s_cmd = malloc(sizeof(struct cros_ec_command_v2) +
+ MAX(outsize, insize));
+ if (s_cmd == NULL)
+ return -EC_RES_ERROR;
+
+ s_cmd->command = command;
+ s_cmd->version = version;
+ s_cmd->result = 0xff;
+ s_cmd->outsize = outsize;
+ s_cmd->insize = insize;
+ memcpy(s_cmd->data, outdata, outsize);
+
+ r = ioctl(fd, CROS_EC_DEV_IOCXCMD_V2, s_cmd);
+ if (r < 0) {
+ fprintf(stderr, "ioctl %d, errno %d (%s), EC result %d (%s)\n",
+ r, errno, strerror(errno), s_cmd->result,
+ strresult(s_cmd->result));
+ if (errno == EAGAIN && s_cmd->result == EC_RES_IN_PROGRESS) {
+ s_cmd->command = EC_CMD_RESEND_RESPONSE;
+ r = ioctl(fd, CROS_EC_DEV_IOCXCMD_V2, &s_cmd);
+ fprintf(stderr,
+ "ioctl %d, errno %d (%s), EC result %d (%s)\n",
+ r, errno, strerror(errno), s_cmd->result,
+ strresult(s_cmd->result));
+ }
+ } else {
+ memcpy(indata, s_cmd->data, MIN(r, insize));
+ if (s_cmd->result != EC_RES_SUCCESS) {
+ fprintf(stderr, "EC result %d (%s)\n", s_cmd->result,
+ strresult(s_cmd->result));
+ r = -EECRESULT - s_cmd->result;
+ }
+ }
+ free(s_cmd);
+
+ return r;
+}
+
+static int ec_readmem_dev_v2(int offset, int bytes, void *dest)
+{
+ struct cros_ec_readmem_v2 s_mem;
+ struct ec_params_read_memmap r_mem;
+ int r;
+ static int fake_it;
+
+ if (!fake_it) {
+ s_mem.offset = offset;
+ s_mem.bytes = bytes;
+ r = ioctl(fd, CROS_EC_DEV_IOCRDMEM_V2, &s_mem);
+ if (r < 0 && errno == ENOTTY) {
+ fake_it = 1;
+ } else {
+ memcpy(dest, s_mem.buffer, bytes);
+ return r;
+ }
+ }
+
+ r_mem.offset = offset;
+ r_mem.size = bytes;
+ return ec_command_dev(EC_CMD_READ_MEMMAP, 0,
+ &r_mem, sizeof(r_mem),
+ dest, bytes);
+}
+
+/*
+ * Attempt to communicate with kernel using old ioctl format.
+ * If it returns ENOTTY, assume that this kernel uses the new format.
+ */
+static int ec_dev_is_v2(void)
+{
+ struct ec_params_hello h_req = {
+ .in_data = 0xa0b0c0d0
+ };
+ struct ec_response_hello h_resp;
+ struct cros_ec_command s_cmd = { };
+ int r;
+
+ s_cmd.command = EC_CMD_HELLO;
+ s_cmd.result = 0xff;
+ s_cmd.outsize = sizeof(h_req);
+ s_cmd.outdata = (uint8_t *)&h_req;
+ s_cmd.insize = sizeof(h_resp);
+ s_cmd.indata = (uint8_t *)&h_resp;
+
+ r = ioctl(fd, CROS_EC_DEV_IOCXCMD, &s_cmd);
+ if (r < 0 && errno == ENOTTY)
+ return 1;
+
+ return 0;
+}
+
int comm_init_dev(const char *device_name)
{
+ int (*ec_cmd_readmem)(int offset, int bytes, void *dest);
char version[80];
char device[80] = "/dev/";
int r;
@@ -138,10 +246,17 @@ int comm_init_dev(const char *device_name)
return 3;
}
- ec_command_proto = ec_command_dev;
- if (ec_readmem_dev(EC_MEMMAP_ID, 2, version) == 2 &&
+ if (ec_dev_is_v2()) {
+ ec_command_proto = ec_command_dev_v2;
+ ec_cmd_readmem = ec_readmem_dev_v2;
+ } else {
+ ec_command_proto = ec_command_dev;
+ ec_cmd_readmem = ec_readmem_dev;
+ }
+
+ if (ec_cmd_readmem(EC_MEMMAP_ID, 2, version) == 2 &&
version[0] == 'E' && version[1] == 'C')
- ec_readmem = ec_readmem_dev;
+ ec_readmem = ec_cmd_readmem;
/*
* Set temporary size, will be updated later.
diff --git a/util/cros_ec_dev.h b/util/cros_ec_dev.h
index 89a54b9e80..65c112c3b2 100644
--- a/util/cros_ec_dev.h
+++ b/util/cros_ec_dev.h
@@ -8,11 +8,11 @@
#include <linux/ioctl.h>
#include <linux/types.h>
+#include "include/ec_commands.h"
#define CROS_EC_DEV_NAME "cros_ec"
#define CROS_EC_DEV_VERSION "1.0.0"
-
/*
* @version: Command version number (often 0)
* @command: Command to send (EC_CMD_...)
@@ -50,4 +50,40 @@ struct cros_ec_readmem {
#define CROS_EC_DEV_IOCXCMD _IOWR(':', 0, struct cros_ec_command)
#define CROS_EC_DEV_IOCRDMEM _IOWR(':', 1, struct cros_ec_readmem)
+/*
+ * @version: Command version number (often 0)
+ * @command: Command to send (EC_CMD_...)
+ * @outsize: Outgoing length in bytes
+ * @insize: Max number of bytes to accept from EC
+ * @result: EC's response to the command (separate from communication failure)
+ * @data: Where to put the incoming data from EC and outgoing data to EC
+ */
+struct cros_ec_command_v2 {
+ uint32_t version;
+ uint32_t command;
+ uint32_t outsize;
+ uint32_t insize;
+ uint32_t result;
+ uint8_t data[0];
+};
+
+/*
+ * @offset: within EC_LPC_ADDR_MEMMAP region
+ * @bytes: number of bytes to read. zero means "read a string" (including '\0')
+ * (at most only EC_MEMMAP_SIZE bytes can be read)
+ * @buffer: where to store the result
+ * ioctl returns the number of bytes read, negative on error
+ */
+struct cros_ec_readmem_v2 {
+ uint32_t offset;
+ uint32_t bytes;
+ uint8_t buffer[EC_MEMMAP_SIZE];
+};
+
+#define CROS_EC_DEV_IOC_V2 0xEC
+#define CROS_EC_DEV_IOCXCMD_V2 _IOWR(CROS_EC_DEV_IOC_V2, 0, \
+ struct cros_ec_command_v2)
+#define CROS_EC_DEV_IOCRDMEM_V2 _IOWR(CROS_EC_DEV_IOC_V2, 1, \
+ struct cros_ec_readmem_v2)
+
#endif /* __UTIL_CROS_EC_DEV_H */
diff --git a/util/misc_util.h b/util/misc_util.h
index 401710f0e5..9fe86e63b2 100644
--- a/util/misc_util.h
+++ b/util/misc_util.h
@@ -8,6 +8,7 @@
/* Don't use a macro where an inline will do... */
static inline int MIN(int a, int b) { return a < b ? a : b; }
+static inline int MAX(int a, int b) { return a > b ? a : b; }
/**
* Write a buffer to the file.