Commit a8411784 authored by Javier Martinez Canillas's avatar Javier Martinez Canillas Committed by Lee Jones

mfd: cros_ec: Use a zero-length array for command data

Commit 1b84f2a4 ("mfd: cros_ec: Use fixed size arrays to transfer
data with the EC") modified the struct cros_ec_command fields to not
use pointers for the input and output buffers and use fixed length
arrays instead.

This change was made because the cros_ec ioctl API uses that struct
cros_ec_command to allow user-space to send commands to the EC and
to get data from the EC. So using pointers made the API not 64-bit
safe. Unfortunately this approach was not flexible enough for all
the use-cases since there may be a need to send larger commands
on newer versions of the EC command protocol.

So to avoid to choose a constant length that it may be too big for
most commands and thus wasting memory and CPU cycles on copy from
and to user-space or having a size that is too small for some big
commands, use a zero-length array that is both 64-bit safe and
flexible. The same buffer is used for both output and input data
so the maximum of these values should be used to allocate it.
Suggested-by: default avatarGwendal Grignou <gwendal@chromium.org>
Signed-off-by: default avatarJavier Martinez Canillas <javier.martinez@collabora.co.uk>
Tested-by: default avatarHeiko Stuebner <heiko@sntech.de>
Acked-by: default avatarLee Jones <lee.jones@linaro.org>
Acked-by: default avatarOlof Johansson <olof@lixom.net>
Signed-off-by: default avatarLee Jones <lee.jones@linaro.org>
parent bb03ffb9
...@@ -182,8 +182,9 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[], ...@@ -182,8 +182,9 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
const u16 bus_num = bus->remote_bus; const u16 bus_num = bus->remote_bus;
int request_len; int request_len;
int response_len; int response_len;
int alloc_size;
int result; int result;
struct cros_ec_command msg = { }; struct cros_ec_command *msg;
request_len = ec_i2c_count_message(i2c_msgs, num); request_len = ec_i2c_count_message(i2c_msgs, num);
if (request_len < 0) { if (request_len < 0) {
...@@ -198,25 +199,39 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[], ...@@ -198,25 +199,39 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
return response_len; return response_len;
} }
result = ec_i2c_construct_message(msg.outdata, i2c_msgs, num, bus_num); alloc_size = max(request_len, response_len);
if (result) msg = kmalloc(sizeof(*msg) + alloc_size, GFP_KERNEL);
return result; if (!msg)
return -ENOMEM;
msg.version = 0; result = ec_i2c_construct_message(msg->data, i2c_msgs, num, bus_num);
msg.command = EC_CMD_I2C_PASSTHRU; if (result) {
msg.outsize = request_len; dev_err(dev, "Error constructing EC i2c message %d\n", result);
msg.insize = response_len; goto exit;
}
result = cros_ec_cmd_xfer(bus->ec, &msg); msg->version = 0;
if (result < 0) msg->command = EC_CMD_I2C_PASSTHRU;
return result; msg->outsize = request_len;
msg->insize = response_len;
result = ec_i2c_parse_response(msg.indata, i2c_msgs, &num); result = cros_ec_cmd_xfer(bus->ec, msg);
if (result < 0) if (result < 0) {
return result; dev_err(dev, "Error transferring EC i2c message %d\n", result);
goto exit;
}
result = ec_i2c_parse_response(msg->data, i2c_msgs, &num);
if (result < 0) {
dev_err(dev, "Error parsing EC i2c message %d\n", result);
goto exit;
}
/* Indicate success by saying how many messages were sent */ /* Indicate success by saying how many messages were sent */
return num; result = num;
exit:
kfree(msg);
return result;
} }
static u32 ec_i2c_functionality(struct i2c_adapter *adap) static u32 ec_i2c_functionality(struct i2c_adapter *adap)
......
...@@ -148,19 +148,28 @@ static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev, ...@@ -148,19 +148,28 @@ static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state) static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
{ {
int ret; int ret = 0;
struct cros_ec_command msg = { struct cros_ec_command *msg;
.command = EC_CMD_MKBP_STATE,
.insize = ckdev->cols,
};
ret = cros_ec_cmd_xfer(ckdev->ec, &msg); msg = kmalloc(sizeof(*msg) + ckdev->cols, GFP_KERNEL);
if (ret < 0) if (!msg)
return ret; return -ENOMEM;
memcpy(kb_state, msg.indata, ckdev->cols); msg->version = 0;
msg->command = EC_CMD_MKBP_STATE;
msg->insize = ckdev->cols;
msg->outsize = 0;
return 0; ret = cros_ec_cmd_xfer(ckdev->ec, msg);
if (ret < 0) {
dev_err(ckdev->dev, "Error transferring EC message %d\n", ret);
goto exit;
}
memcpy(kb_state, msg->data, ckdev->cols);
exit:
kfree(msg);
return ret;
} }
static irqreturn_t cros_ec_keyb_irq(int irq, void *data) static irqreturn_t cros_ec_keyb_irq(int irq, void *data)
......
...@@ -41,7 +41,7 @@ int cros_ec_prepare_tx(struct cros_ec_device *ec_dev, ...@@ -41,7 +41,7 @@ int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
out[2] = msg->outsize; out[2] = msg->outsize;
csum = out[0] + out[1] + out[2]; csum = out[0] + out[1] + out[2];
for (i = 0; i < msg->outsize; i++) for (i = 0; i < msg->outsize; i++)
csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->outdata[i]; csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = (uint8_t)(csum & 0xff); out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = (uint8_t)(csum & 0xff);
return EC_MSG_TX_PROTO_BYTES + msg->outsize; return EC_MSG_TX_PROTO_BYTES + msg->outsize;
...@@ -75,11 +75,20 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, ...@@ -75,11 +75,20 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
ret = ec_dev->cmd_xfer(ec_dev, msg); ret = ec_dev->cmd_xfer(ec_dev, msg);
if (msg->result == EC_RES_IN_PROGRESS) { if (msg->result == EC_RES_IN_PROGRESS) {
int i; int i;
struct cros_ec_command status_msg = { }; struct cros_ec_command *status_msg;
struct ec_response_get_comms_status *status; struct ec_response_get_comms_status *status;
status_msg.command = EC_CMD_GET_COMMS_STATUS; status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
status_msg.insize = sizeof(*status); GFP_KERNEL);
if (!status_msg) {
ret = -ENOMEM;
goto exit;
}
status_msg->version = 0;
status_msg->command = EC_CMD_GET_COMMS_STATUS;
status_msg->insize = sizeof(*status);
status_msg->outsize = 0;
/* /*
* Query the EC's status until it's no longer busy or * Query the EC's status until it's no longer busy or
...@@ -88,20 +97,23 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, ...@@ -88,20 +97,23 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
for (i = 0; i < EC_COMMAND_RETRIES; i++) { for (i = 0; i < EC_COMMAND_RETRIES; i++) {
usleep_range(10000, 11000); usleep_range(10000, 11000);
ret = ec_dev->cmd_xfer(ec_dev, &status_msg); ret = ec_dev->cmd_xfer(ec_dev, status_msg);
if (ret < 0) if (ret < 0)
break; break;
msg->result = status_msg.result; msg->result = status_msg->result;
if (status_msg.result != EC_RES_SUCCESS) if (status_msg->result != EC_RES_SUCCESS)
break; break;
status = (struct ec_response_get_comms_status *) status = (struct ec_response_get_comms_status *)
status_msg.indata; status_msg->data;
if (!(status->flags & EC_COMMS_STATUS_PROCESSING)) if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
break; break;
} }
kfree(status_msg);
} }
exit:
mutex_unlock(&ec_dev->lock); mutex_unlock(&ec_dev->lock);
return ret; return ret;
......
...@@ -76,7 +76,7 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev, ...@@ -76,7 +76,7 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
/* copy message payload and compute checksum */ /* copy message payload and compute checksum */
sum = out_buf[0] + out_buf[1] + out_buf[2]; sum = out_buf[0] + out_buf[1] + out_buf[2];
for (i = 0; i < msg->outsize; i++) { for (i = 0; i < msg->outsize; i++) {
out_buf[3 + i] = msg->outdata[i]; out_buf[3 + i] = msg->data[i];
sum += out_buf[3 + i]; sum += out_buf[3 + i];
} }
out_buf[3 + msg->outsize] = sum; out_buf[3 + msg->outsize] = sum;
...@@ -109,7 +109,7 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev, ...@@ -109,7 +109,7 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
/* copy response packet payload and compute checksum */ /* copy response packet payload and compute checksum */
sum = in_buf[0] + in_buf[1]; sum = in_buf[0] + in_buf[1];
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
msg->indata[i] = in_buf[2 + i]; msg->data[i] = in_buf[2 + i];
sum += in_buf[2 + i]; sum += in_buf[2 + i];
} }
dev_dbg(ec_dev->dev, "packet: %*ph, sum = %02x\n", dev_dbg(ec_dev->dev, "packet: %*ph, sum = %02x\n",
......
...@@ -299,7 +299,7 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, ...@@ -299,7 +299,7 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
sum += ptr[i + 2]; sum += ptr[i + 2];
if (ec_msg->insize) if (ec_msg->insize)
ec_msg->indata[i] = ptr[i + 2]; ec_msg->data[i] = ptr[i + 2];
} }
sum &= 0xff; sum &= 0xff;
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include "cros_ec_dev.h" #include "cros_ec_dev.h"
...@@ -36,28 +37,31 @@ static int ec_get_version(struct cros_ec_device *ec, char *str, int maxlen) ...@@ -36,28 +37,31 @@ static int ec_get_version(struct cros_ec_device *ec, char *str, int maxlen)
static const char * const current_image_name[] = { static const char * const current_image_name[] = {
"unknown", "read-only", "read-write", "invalid", "unknown", "read-only", "read-write", "invalid",
}; };
struct cros_ec_command msg = { struct cros_ec_command *msg;
.version = 0,
.command = EC_CMD_GET_VERSION,
.outdata = { 0 },
.outsize = 0,
.indata = { 0 },
.insize = sizeof(*resp),
};
int ret; int ret;
ret = cros_ec_cmd_xfer(ec, &msg); msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
if (!msg)
return -ENOMEM;
msg->version = 0;
msg->command = EC_CMD_GET_VERSION;
msg->insize = sizeof(*resp);
msg->outsize = 0;
ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) if (ret < 0)
return ret; goto exit;
if (msg.result != EC_RES_SUCCESS) { if (msg->result != EC_RES_SUCCESS) {
snprintf(str, maxlen, snprintf(str, maxlen,
"%s\nUnknown EC version: EC returned %d\n", "%s\nUnknown EC version: EC returned %d\n",
CROS_EC_DEV_VERSION, msg.result); CROS_EC_DEV_VERSION, msg->result);
return 0; ret = -EINVAL;
goto exit;
} }
resp = (struct ec_response_get_version *)msg.indata; resp = (struct ec_response_get_version *)msg->data;
if (resp->current_image >= ARRAY_SIZE(current_image_name)) if (resp->current_image >= ARRAY_SIZE(current_image_name))
resp->current_image = 3; /* invalid */ resp->current_image = 3; /* invalid */
...@@ -65,7 +69,10 @@ static int ec_get_version(struct cros_ec_device *ec, char *str, int maxlen) ...@@ -65,7 +69,10 @@ static int ec_get_version(struct cros_ec_device *ec, char *str, int maxlen)
resp->version_string_ro, resp->version_string_rw, resp->version_string_ro, resp->version_string_rw,
current_image_name[resp->current_image]); current_image_name[resp->current_image]);
return 0; ret = 0;
exit:
kfree(msg);
return ret;
} }
/* Device file ops */ /* Device file ops */
...@@ -110,20 +117,32 @@ static ssize_t ec_device_read(struct file *filp, char __user *buffer, ...@@ -110,20 +117,32 @@ static ssize_t ec_device_read(struct file *filp, char __user *buffer,
static long ec_device_ioctl_xcmd(struct cros_ec_device *ec, void __user *arg) static long ec_device_ioctl_xcmd(struct cros_ec_device *ec, void __user *arg)
{ {
long ret; long ret;
struct cros_ec_command s_cmd = { }; struct cros_ec_command u_cmd;
struct cros_ec_command *s_cmd;
if (copy_from_user(&s_cmd, arg, sizeof(s_cmd))) if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
return -EFAULT; return -EFAULT;
ret = cros_ec_cmd_xfer(ec, &s_cmd); s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
GFP_KERNEL);
if (!s_cmd)
return -ENOMEM;
if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
ret = -EFAULT;
goto exit;
}
ret = cros_ec_cmd_xfer(ec, s_cmd);
/* Only copy data to userland if data was received. */ /* Only copy data to userland if data was received. */
if (ret < 0) if (ret < 0)
return ret; goto exit;
if (copy_to_user(arg, &s_cmd, sizeof(s_cmd))) if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + u_cmd.insize))
return -EFAULT; ret = -EFAULT;
exit:
return 0; kfree(s_cmd);
return ret;
} }
static long ec_device_ioctl_readmem(struct cros_ec_device *ec, void __user *arg) static long ec_device_ioctl_readmem(struct cros_ec_device *ec, void __user *arg)
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/slab.h>
#include "cros_ec_dev.h" #include "cros_ec_dev.h"
...@@ -91,54 +92,79 @@ static int lb_throttle(void) ...@@ -91,54 +92,79 @@ static int lb_throttle(void)
return ret; return ret;
} }
#define INIT_MSG(P, R) { \ static struct cros_ec_command *alloc_lightbar_cmd_msg(void)
.command = EC_CMD_LIGHTBAR_CMD, \ {
.outsize = sizeof(*P), \ struct cros_ec_command *msg;
.insize = sizeof(*R), \ int len;
}
len = max(sizeof(struct ec_params_lightbar),
sizeof(struct ec_response_lightbar));
msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
if (!msg)
return NULL;
msg->version = 0;
msg->command = EC_CMD_LIGHTBAR_CMD;
msg->outsize = sizeof(struct ec_params_lightbar);
msg->insize = sizeof(struct ec_response_lightbar);
return msg;
}
static int get_lightbar_version(struct cros_ec_device *ec, static int get_lightbar_version(struct cros_ec_device *ec,
uint32_t *ver_ptr, uint32_t *flg_ptr) uint32_t *ver_ptr, uint32_t *flg_ptr)
{ {
struct ec_params_lightbar *param; struct ec_params_lightbar *param;
struct ec_response_lightbar *resp; struct ec_response_lightbar *resp;
struct cros_ec_command msg = INIT_MSG(param, resp); struct cros_ec_command *msg;
int ret; int ret;
param = (struct ec_params_lightbar *)msg.outdata; msg = alloc_lightbar_cmd_msg();
param->cmd = LIGHTBAR_CMD_VERSION; if (!msg)
ret = cros_ec_cmd_xfer(ec, &msg);
if (ret < 0)
return 0; return 0;
switch (msg.result) { param = (struct ec_params_lightbar *)msg->data;
param->cmd = LIGHTBAR_CMD_VERSION;
ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) {
ret = 0;
goto exit;
}
switch (msg->result) {
case EC_RES_INVALID_PARAM: case EC_RES_INVALID_PARAM:
/* Pixel had no version command. */ /* Pixel had no version command. */
if (ver_ptr) if (ver_ptr)
*ver_ptr = 0; *ver_ptr = 0;
if (flg_ptr) if (flg_ptr)
*flg_ptr = 0; *flg_ptr = 0;
return 1; ret = 1;
goto exit;
case EC_RES_SUCCESS: case EC_RES_SUCCESS:
resp = (struct ec_response_lightbar *)msg.indata; resp = (struct ec_response_lightbar *)msg->data;
/* Future devices w/lightbars should implement this command */ /* Future devices w/lightbars should implement this command */
if (ver_ptr) if (ver_ptr)
*ver_ptr = resp->version.num; *ver_ptr = resp->version.num;
if (flg_ptr) if (flg_ptr)
*flg_ptr = resp->version.flags; *flg_ptr = resp->version.flags;
return 1; ret = 1;
goto exit;
} }
/* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */ /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
return 0; ret = 0;
exit:
kfree(msg);
return ret;
} }
static ssize_t version_show(struct device *dev, static ssize_t version_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
uint32_t version, flags; uint32_t version = 0, flags = 0;
struct cros_ec_device *ec = dev_get_drvdata(dev); struct cros_ec_device *ec = dev_get_drvdata(dev);
int ret; int ret;
...@@ -158,8 +184,7 @@ static ssize_t brightness_store(struct device *dev, ...@@ -158,8 +184,7 @@ static ssize_t brightness_store(struct device *dev,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct ec_params_lightbar *param; struct ec_params_lightbar *param;
struct ec_response_lightbar *resp; struct cros_ec_command *msg;
struct cros_ec_command msg = INIT_MSG(param, resp);
int ret; int ret;
unsigned int val; unsigned int val;
struct cros_ec_device *ec = dev_get_drvdata(dev); struct cros_ec_device *ec = dev_get_drvdata(dev);
...@@ -167,21 +192,30 @@ static ssize_t brightness_store(struct device *dev, ...@@ -167,21 +192,30 @@ static ssize_t brightness_store(struct device *dev,
if (kstrtouint(buf, 0, &val)) if (kstrtouint(buf, 0, &val))
return -EINVAL; return -EINVAL;
param = (struct ec_params_lightbar *)msg.outdata; msg = alloc_lightbar_cmd_msg();
if (!msg)
return -ENOMEM;
param = (struct ec_params_lightbar *)msg->data;
param->cmd = LIGHTBAR_CMD_BRIGHTNESS; param->cmd = LIGHTBAR_CMD_BRIGHTNESS;
param->brightness.num = val; param->brightness.num = val;
ret = lb_throttle(); ret = lb_throttle();
if (ret) if (ret)
return ret; goto exit;
ret = cros_ec_cmd_xfer(ec, &msg); ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) if (ret < 0)
return ret; goto exit;
if (msg.result != EC_RES_SUCCESS) if (msg->result != EC_RES_SUCCESS) {
return -EINVAL; ret = -EINVAL;
goto exit;
}
return count; ret = count;
exit:
kfree(msg);
return ret;
} }
...@@ -196,12 +230,15 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr, ...@@ -196,12 +230,15 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct ec_params_lightbar *param; struct ec_params_lightbar *param;
struct ec_response_lightbar *resp; struct cros_ec_command *msg;
struct cros_ec_command msg = INIT_MSG(param, resp);
struct cros_ec_device *ec = dev_get_drvdata(dev); struct cros_ec_device *ec = dev_get_drvdata(dev);
unsigned int val[4]; unsigned int val[4];
int ret, i = 0, j = 0, ok = 0; int ret, i = 0, j = 0, ok = 0;
msg = alloc_lightbar_cmd_msg();
if (!msg)
return -ENOMEM;
do { do {
/* Skip any whitespace */ /* Skip any whitespace */
while (*buf && isspace(*buf)) while (*buf && isspace(*buf))
...@@ -215,7 +252,7 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr, ...@@ -215,7 +252,7 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
return -EINVAL; return -EINVAL;
if (i == 4) { if (i == 4) {
param = (struct ec_params_lightbar *)msg.outdata; param = (struct ec_params_lightbar *)msg->data;
param->cmd = LIGHTBAR_CMD_RGB; param->cmd = LIGHTBAR_CMD_RGB;
param->rgb.led = val[0]; param->rgb.led = val[0];
param->rgb.red = val[1]; param->rgb.red = val[1];
...@@ -231,12 +268,14 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr, ...@@ -231,12 +268,14 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
return ret; return ret;
} }
ret = cros_ec_cmd_xfer(ec, &msg); ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) if (ret < 0)
return ret; goto exit;
if (msg.result != EC_RES_SUCCESS) if (msg->result != EC_RES_SUCCESS) {
return -EINVAL; ret = -EINVAL;
goto exit;
}
i = 0; i = 0;
ok = 1; ok = 1;
...@@ -248,6 +287,8 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr, ...@@ -248,6 +287,8 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
} while (*buf); } while (*buf);
exit:
kfree(msg);
return (ok && i == 0) ? count : -EINVAL; return (ok && i == 0) ? count : -EINVAL;
} }
...@@ -261,42 +302,55 @@ static ssize_t sequence_show(struct device *dev, ...@@ -261,42 +302,55 @@ static ssize_t sequence_show(struct device *dev,
{ {
struct ec_params_lightbar *param; struct ec_params_lightbar *param;
struct ec_response_lightbar *resp; struct ec_response_lightbar *resp;
struct cros_ec_command msg = INIT_MSG(param, resp); struct cros_ec_command *msg;
int ret; int ret;
struct cros_ec_device *ec = dev_get_drvdata(dev); struct cros_ec_device *ec = dev_get_drvdata(dev);
param = (struct ec_params_lightbar *)msg.outdata; msg = alloc_lightbar_cmd_msg();
if (!msg)
return -ENOMEM;
param = (struct ec_params_lightbar *)msg->data;
param->cmd = LIGHTBAR_CMD_GET_SEQ; param->cmd = LIGHTBAR_CMD_GET_SEQ;
ret = lb_throttle(); ret = lb_throttle();
if (ret) if (ret)
return ret; goto exit;
ret = cros_ec_cmd_xfer(ec, &msg); ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) if (ret < 0)
return ret; goto exit;
if (msg.result != EC_RES_SUCCESS) if (msg->result != EC_RES_SUCCESS) {
return scnprintf(buf, PAGE_SIZE, ret = scnprintf(buf, PAGE_SIZE,
"ERROR: EC returned %d\n", msg.result); "ERROR: EC returned %d\n", msg->result);
goto exit;
}
resp = (struct ec_response_lightbar *)msg.indata; resp = (struct ec_response_lightbar *)msg->data;
if (resp->get_seq.num >= ARRAY_SIZE(seqname)) if (resp->get_seq.num >= ARRAY_SIZE(seqname))
return scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num); ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
else else
return scnprintf(buf, PAGE_SIZE, "%s\n", ret = scnprintf(buf, PAGE_SIZE, "%s\n",
seqname[resp->get_seq.num]); seqname[resp->get_seq.num]);
exit:
kfree(msg);
return ret;
} }
static ssize_t sequence_store(struct device *dev, struct device_attribute *attr, static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct ec_params_lightbar *param; struct ec_params_lightbar *param;
struct ec_response_lightbar *resp; struct cros_ec_command *msg;
struct cros_ec_command msg = INIT_MSG(param, resp);
unsigned int num; unsigned int num;
int ret, len; int ret, len;
struct cros_ec_device *ec = dev_get_drvdata(dev); struct cros_ec_device *ec = dev_get_drvdata(dev);
msg = alloc_lightbar_cmd_msg();
if (!msg)
return -ENOMEM;
for (len = 0; len < count; len++) for (len = 0; len < count; len++)
if (!isalnum(buf[len])) if (!isalnum(buf[len]))
break; break;
...@@ -311,18 +365,18 @@ static ssize_t sequence_store(struct device *dev, struct device_attribute *attr, ...@@ -311,18 +365,18 @@ static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
return ret; return ret;
} }
param = (struct ec_params_lightbar *)msg.outdata; param = (struct ec_params_lightbar *)msg->data;
param->cmd = LIGHTBAR_CMD_SEQ; param->cmd = LIGHTBAR_CMD_SEQ;
param->seq.num = num; param->seq.num = num;
ret = lb_throttle(); ret = lb_throttle();
if (ret) if (ret)
return ret; return ret;
ret = cros_ec_cmd_xfer(ec, &msg); ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) if (ret < 0)
return ret; return ret;
if (msg.result != EC_RES_SUCCESS) if (msg->result != EC_RES_SUCCESS)
return -EINVAL; return -EINVAL;
return count; return count;
......
...@@ -73,8 +73,8 @@ static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec, ...@@ -73,8 +73,8 @@ static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
/* Copy data and update checksum */ /* Copy data and update checksum */
for (i = 0; i < msg->outsize; i++) { for (i = 0; i < msg->outsize; i++) {
outb(msg->outdata[i], EC_LPC_ADDR_HOST_PARAM + i); outb(msg->data[i], EC_LPC_ADDR_HOST_PARAM + i);
csum += msg->outdata[i]; csum += msg->data[i];
} }
/* Finalize checksum and write args */ /* Finalize checksum and write args */
...@@ -129,8 +129,8 @@ static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec, ...@@ -129,8 +129,8 @@ static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
/* Read response and update checksum */ /* Read response and update checksum */
for (i = 0; i < args.data_size; i++) { for (i = 0; i < args.data_size; i++) {
msg->indata[i] = inb(EC_LPC_ADDR_HOST_PARAM + i); msg->data[i] = inb(EC_LPC_ADDR_HOST_PARAM + i);
csum += msg->indata[i]; csum += msg->data[i];
} }
/* Verify checksum */ /* Verify checksum */
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/printk.h> #include <linux/printk.h>
#include <linux/slab.h>
#include <linux/stat.h> #include <linux/stat.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
...@@ -66,14 +67,19 @@ static ssize_t store_ec_reboot(struct device *dev, ...@@ -66,14 +67,19 @@ static ssize_t store_ec_reboot(struct device *dev,
{"hibernate", EC_REBOOT_HIBERNATE, 0}, {"hibernate", EC_REBOOT_HIBERNATE, 0},
{"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN}, {"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
}; };
struct cros_ec_command msg = { 0 }; struct cros_ec_command *msg;
struct ec_params_reboot_ec *param = struct ec_params_reboot_ec *param;
(struct ec_params_reboot_ec *)msg.outdata;
int got_cmd = 0, offset = 0; int got_cmd = 0, offset = 0;
int i; int i;
int ret; int ret;
struct cros_ec_device *ec = dev_get_drvdata(dev); struct cros_ec_device *ec = dev_get_drvdata(dev);
msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
if (!msg)
return -ENOMEM;
param = (struct ec_params_reboot_ec *)msg->data;
param->flags = 0; param->flags = 0;
while (1) { while (1) {
/* Find word to start scanning */ /* Find word to start scanning */
...@@ -100,19 +106,26 @@ static ssize_t store_ec_reboot(struct device *dev, ...@@ -100,19 +106,26 @@ static ssize_t store_ec_reboot(struct device *dev,
offset++; offset++;
} }
if (!got_cmd) if (!got_cmd) {
return -EINVAL; count = -EINVAL;
goto exit;
msg.command = EC_CMD_REBOOT_EC;
msg.outsize = sizeof(param);
ret = cros_ec_cmd_xfer(ec, &msg);
if (ret < 0)
return ret;
if (msg.result != EC_RES_SUCCESS) {
dev_dbg(ec->dev, "EC result %d\n", msg.result);
return -EINVAL;
} }
msg->version = 0;
msg->command = EC_CMD_REBOOT_EC;
msg->outsize = sizeof(*param);
msg->insize = 0;
ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) {
count = ret;
goto exit;
}
if (msg->result != EC_RES_SUCCESS) {
dev_dbg(ec->dev, "EC result %d\n", msg->result);
count = -EINVAL;
}
exit:
kfree(msg);
return count; return count;
} }
...@@ -123,22 +136,32 @@ static ssize_t show_ec_version(struct device *dev, ...@@ -123,22 +136,32 @@ static ssize_t show_ec_version(struct device *dev,
struct ec_response_get_version *r_ver; struct ec_response_get_version *r_ver;
struct ec_response_get_chip_info *r_chip; struct ec_response_get_chip_info *r_chip;
struct ec_response_board_version *r_board; struct ec_response_board_version *r_board;
struct cros_ec_command msg = { 0 }; struct cros_ec_command *msg;
int ret; int ret;
int count = 0; int count = 0;
struct cros_ec_device *ec = dev_get_drvdata(dev); struct cros_ec_device *ec = dev_get_drvdata(dev);
msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
if (!msg)
return -ENOMEM;
/* Get versions. RW may change. */ /* Get versions. RW may change. */
msg.command = EC_CMD_GET_VERSION; msg->version = 0;
msg.insize = sizeof(*r_ver); msg->command = EC_CMD_GET_VERSION;
ret = cros_ec_cmd_xfer(ec, &msg); msg->insize = sizeof(*r_ver);
if (ret < 0) msg->outsize = 0;
return ret; ret = cros_ec_cmd_xfer(ec, msg);
if (msg.result != EC_RES_SUCCESS) if (ret < 0) {
return scnprintf(buf, PAGE_SIZE, count = ret;
"ERROR: EC returned %d\n", msg.result); goto exit;
}
if (msg->result != EC_RES_SUCCESS) {
count = scnprintf(buf, PAGE_SIZE,
"ERROR: EC returned %d\n", msg->result);
goto exit;
}
r_ver = (struct ec_response_get_version *)msg.indata; r_ver = (struct ec_response_get_version *)msg->data;
/* Strings should be null-terminated, but let's be sure. */ /* Strings should be null-terminated, but let's be sure. */
r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0'; r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0'; r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
...@@ -152,33 +175,33 @@ static ssize_t show_ec_version(struct device *dev, ...@@ -152,33 +175,33 @@ static ssize_t show_ec_version(struct device *dev,
image_names[r_ver->current_image] : "?")); image_names[r_ver->current_image] : "?"));
/* Get build info. */ /* Get build info. */
msg.command = EC_CMD_GET_BUILD_INFO; msg->command = EC_CMD_GET_BUILD_INFO;
msg.insize = sizeof(msg.indata); msg->insize = EC_HOST_PARAM_SIZE;
ret = cros_ec_cmd_xfer(ec, &msg); ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) if (ret < 0)
count += scnprintf(buf + count, PAGE_SIZE - count, count += scnprintf(buf + count, PAGE_SIZE - count,
"Build info: XFER ERROR %d\n", ret); "Build info: XFER ERROR %d\n", ret);
else if (msg.result != EC_RES_SUCCESS) else if (msg->result != EC_RES_SUCCESS)
count += scnprintf(buf + count, PAGE_SIZE - count, count += scnprintf(buf + count, PAGE_SIZE - count,
"Build info: EC error %d\n", msg.result); "Build info: EC error %d\n", msg->result);
else { else {
msg.indata[sizeof(msg.indata) - 1] = '\0'; msg->data[sizeof(msg->data) - 1] = '\0';
count += scnprintf(buf + count, PAGE_SIZE - count, count += scnprintf(buf + count, PAGE_SIZE - count,
"Build info: %s\n", msg.indata); "Build info: %s\n", msg->data);
} }
/* Get chip info. */ /* Get chip info. */
msg.command = EC_CMD_GET_CHIP_INFO; msg->command = EC_CMD_GET_CHIP_INFO;
msg.insize = sizeof(*r_chip); msg->insize = sizeof(*r_chip);
ret = cros_ec_cmd_xfer(ec, &msg); ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) if (ret < 0)
count += scnprintf(buf + count, PAGE_SIZE - count, count += scnprintf(buf + count, PAGE_SIZE - count,
"Chip info: XFER ERROR %d\n", ret); "Chip info: XFER ERROR %d\n", ret);
else if (msg.result != EC_RES_SUCCESS) else if (msg->result != EC_RES_SUCCESS)
count += scnprintf(buf + count, PAGE_SIZE - count, count += scnprintf(buf + count, PAGE_SIZE - count,
"Chip info: EC error %d\n", msg.result); "Chip info: EC error %d\n", msg->result);
else { else {
r_chip = (struct ec_response_get_chip_info *)msg.indata; r_chip = (struct ec_response_get_chip_info *)msg->data;
r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0'; r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
r_chip->name[sizeof(r_chip->name) - 1] = '\0'; r_chip->name[sizeof(r_chip->name) - 1] = '\0';
...@@ -192,23 +215,25 @@ static ssize_t show_ec_version(struct device *dev, ...@@ -192,23 +215,25 @@ static ssize_t show_ec_version(struct device *dev,
} }
/* Get board version */ /* Get board version */
msg.command = EC_CMD_GET_BOARD_VERSION; msg->command = EC_CMD_GET_BOARD_VERSION;
msg.insize = sizeof(*r_board); msg->insize = sizeof(*r_board);
ret = cros_ec_cmd_xfer(ec, &msg); ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) if (ret < 0)
count += scnprintf(buf + count, PAGE_SIZE - count, count += scnprintf(buf + count, PAGE_SIZE - count,
"Board version: XFER ERROR %d\n", ret); "Board version: XFER ERROR %d\n", ret);
else if (msg.result != EC_RES_SUCCESS) else if (msg->result != EC_RES_SUCCESS)
count += scnprintf(buf + count, PAGE_SIZE - count, count += scnprintf(buf + count, PAGE_SIZE - count,
"Board version: EC error %d\n", msg.result); "Board version: EC error %d\n", msg->result);
else { else {
r_board = (struct ec_response_board_version *)msg.indata; r_board = (struct ec_response_board_version *)msg->data;
count += scnprintf(buf + count, PAGE_SIZE - count, count += scnprintf(buf + count, PAGE_SIZE - count,
"Board version: %d\n", "Board version: %d\n",
r_board->board_version); r_board->board_version);
} }
exit:
kfree(msg);
return count; return count;
} }
...@@ -216,27 +241,38 @@ static ssize_t show_ec_flashinfo(struct device *dev, ...@@ -216,27 +241,38 @@ static ssize_t show_ec_flashinfo(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct ec_response_flash_info *resp; struct ec_response_flash_info *resp;
struct cros_ec_command msg = { 0 }; struct cros_ec_command *msg;
int ret; int ret;
struct cros_ec_device *ec = dev_get_drvdata(dev); struct cros_ec_device *ec = dev_get_drvdata(dev);
msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
if (!msg)
return -ENOMEM;
/* The flash info shouldn't ever change, but ask each time anyway. */ /* The flash info shouldn't ever change, but ask each time anyway. */
msg.command = EC_CMD_FLASH_INFO; msg->version = 0;
msg.insize = sizeof(*resp); msg->command = EC_CMD_FLASH_INFO;
ret = cros_ec_cmd_xfer(ec, &msg); msg->insize = sizeof(*resp);
msg->outsize = 0;
ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0) if (ret < 0)
return ret; goto exit;
if (msg.result != EC_RES_SUCCESS) if (msg->result != EC_RES_SUCCESS) {
return scnprintf(buf, PAGE_SIZE, ret = scnprintf(buf, PAGE_SIZE,
"ERROR: EC returned %d\n", msg.result); "ERROR: EC returned %d\n", msg->result);
goto exit;
resp = (struct ec_response_flash_info *)msg.indata; }
return scnprintf(buf, PAGE_SIZE, resp = (struct ec_response_flash_info *)msg->data;
"FlashSize %d\nWriteSize %d\n"
"EraseSize %d\nProtectSize %d\n", ret = scnprintf(buf, PAGE_SIZE,
resp->flash_size, resp->write_block_size, "FlashSize %d\nWriteSize %d\n"
resp->erase_block_size, resp->protect_block_size); "EraseSize %d\nProtectSize %d\n",
resp->flash_size, resp->write_block_size,
resp->erase_block_size, resp->protect_block_size);
exit:
kfree(msg);
return ret;
} }
/* Module initialization */ /* Module initialization */
......
...@@ -42,8 +42,7 @@ enum { ...@@ -42,8 +42,7 @@ enum {
* @outsize: Outgoing length in bytes * @outsize: Outgoing length in bytes
* @insize: Max number of bytes to accept from EC * @insize: Max number of bytes to accept from EC
* @result: EC's response to the command (separate from communication failure) * @result: EC's response to the command (separate from communication failure)
* @outdata: Outgoing data to EC * @data: Where to put the incoming data from EC and outgoing data to EC
* @indata: Where to put the incoming data from EC
*/ */
struct cros_ec_command { struct cros_ec_command {
uint32_t version; uint32_t version;
...@@ -51,8 +50,7 @@ struct cros_ec_command { ...@@ -51,8 +50,7 @@ struct cros_ec_command {
uint32_t outsize; uint32_t outsize;
uint32_t insize; uint32_t insize;
uint32_t result; uint32_t result;
uint8_t outdata[EC_PROTO2_MAX_PARAM_SIZE]; uint8_t data[0];
uint8_t indata[EC_PROTO2_MAX_PARAM_SIZE];
}; };
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment