[PATCH v2 07/17] platform/chrome: sysfs: Use cros_ec_cmd()

From: Prashant Malani
Date: Wed Feb 05 2020 - 14:09:39 EST


Replace cros_ec_cmd_xfer_status() calls to the new function
cros_ec_cmd() which is more readable and does the message setup code.

Signed-off-by: Prashant Malani <pmalani@xxxxxxxxxxxx>
---

Changes in v2:
- Updated to use new function name and parameter list.

drivers/platform/chrome/cros_ec_sysfs.c | 103 +++++++++---------------
1 file changed, 39 insertions(+), 64 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_sysfs.c b/drivers/platform/chrome/cros_ec_sysfs.c
index 07dac97ad57c67..107eb81dff1305 100644
--- a/drivers/platform/chrome/cros_ec_sysfs.c
+++ b/drivers/platform/chrome/cros_ec_sysfs.c
@@ -51,20 +51,13 @@ static ssize_t reboot_store(struct device *dev,
{"hibernate", EC_REBOOT_HIBERNATE, 0},
{"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
};
- struct cros_ec_command *msg;
- struct ec_params_reboot_ec *param;
+ struct ec_params_reboot_ec param = {0};
int got_cmd = 0, offset = 0;
int i;
int ret;
struct cros_ec_dev *ec = to_cros_ec_dev(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) {
/* Find word to start scanning */
while (buf[offset] && isspace(buf[offset]))
@@ -76,9 +69,9 @@ static ssize_t reboot_store(struct device *dev,
if (!strncasecmp(words[i].str, buf+offset,
strlen(words[i].str))) {
if (words[i].flags) {
- param->flags |= words[i].flags;
+ param.flags |= words[i].flags;
} else {
- param->cmd = words[i].cmd;
+ param.cmd = words[i].cmd;
got_cmd = 1;
}
break;
@@ -95,15 +88,11 @@ static ssize_t reboot_store(struct device *dev,
goto exit;
}

- msg->version = 0;
- msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset;
- msg->outsize = sizeof(*param);
- msg->insize = 0;
- ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
+ ret = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_REBOOT_EC + ec->cmd_offset,
+ &param, sizeof(param), NULL, 0, NULL);
if (ret < 0)
count = ret;
exit:
- kfree(msg);
return count;
}

@@ -115,25 +104,23 @@ static ssize_t version_show(struct device *dev,
struct ec_response_get_chip_info *r_chip;
struct ec_response_board_version *r_board;
struct cros_ec_command *msg;
+ void *ec_buf;
int ret;
int count = 0;
struct cros_ec_dev *ec = to_cros_ec_dev(dev);

- msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
- if (!msg)
+ ec_buf = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
+ if (!ec_buf)
return -ENOMEM;

/* Get versions. RW may change. */
- msg->version = 0;
- msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
- msg->insize = sizeof(*r_ver);
- msg->outsize = 0;
- ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
+ ret = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_GET_VERSION + ec->cmd_offset,
+ NULL, 0, ec_buf, sizeof(*r_ver), NULL);
if (ret < 0) {
count = ret;
goto exit;
}
- r_ver = (struct ec_response_get_version *)msg->data;
+ r_ver = (struct ec_response_get_version *)ec_buf;
/* 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_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
@@ -145,8 +132,10 @@ static ssize_t version_show(struct device *dev,
"Firmware copy: %s\n",
(r_ver->current_image < ARRAY_SIZE(image_names) ?
image_names[r_ver->current_image] : "?"));
+ memset(ec_buf, 0, sizeof(*msg) + EC_HOST_PARAM_SIZE);

/* Get build info. */
+ msg = (struct cros_ec_command *)ec_buf;
msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset;
msg->insize = EC_HOST_PARAM_SIZE;
ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
@@ -205,40 +194,28 @@ static ssize_t version_show(struct device *dev,
}

exit:
- kfree(msg);
+ kfree(ec_buf);
return count;
}

static ssize_t flashinfo_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
- struct ec_response_flash_info *resp;
- struct cros_ec_command *msg;
+ struct ec_response_flash_info resp = {0};
int ret;
struct cros_ec_dev *ec = to_cros_ec_dev(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. */
- msg->version = 0;
- msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset;
- msg->insize = sizeof(*resp);
- msg->outsize = 0;
- ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
+ ret = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_FLASH_INFO + ec->cmd_offset,
+ NULL, 0, &resp, sizeof(resp), NULL);
if (ret < 0)
goto exit;

- resp = (struct ec_response_flash_info *)msg->data;
-
ret = scnprintf(buf, PAGE_SIZE,
"FlashSize %d\nWriteSize %d\n"
"EraseSize %d\nProtectSize %d\n",
- resp->flash_size, resp->write_block_size,
- resp->erase_block_size, resp->protect_block_size);
+ resp.flash_size, resp.write_block_size,
+ resp.erase_block_size, resp.protect_block_size);
exit:
- kfree(msg);
return ret;
}

@@ -249,29 +226,27 @@ static ssize_t kb_wake_angle_show(struct device *dev,
struct cros_ec_dev *ec = to_cros_ec_dev(dev);
struct ec_response_motion_sense *resp;
struct ec_params_motion_sense *param;
- struct cros_ec_command *msg;
+ void *ec_buf;
int ret;

- msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
- if (!msg)
+ ec_buf = kmalloc(EC_HOST_PARAM_SIZE, GFP_KERNEL);
+ if (!ec_buf)
return -ENOMEM;

- param = (struct ec_params_motion_sense *)msg->data;
- msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
- msg->version = 2;
+ param = (struct ec_params_motion_sense *)ec_buf;
+ resp = (struct ec_response_motion_sense *)ec_buf;
param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
param->kb_wake_angle.data = EC_MOTION_SENSE_NO_VALUE;
- msg->outsize = sizeof(*param);
- msg->insize = sizeof(*resp);

- ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
+ ret = cros_ec_cmd(ec->ec_dev, 2,
+ EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset, param,
+ sizeof(*param), resp, sizeof(*resp), NULL);
if (ret < 0)
goto exit;

- resp = (struct ec_response_motion_sense *)msg->data;
ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->kb_wake_angle.ret);
exit:
- kfree(msg);
+ kfree(ec_buf);
return ret;
}

@@ -281,7 +256,8 @@ static ssize_t kb_wake_angle_store(struct device *dev,
{
struct cros_ec_dev *ec = to_cros_ec_dev(dev);
struct ec_params_motion_sense *param;
- struct cros_ec_command *msg;
+ struct ec_response_motion_sense *resp;
+ void *ec_buf;
u16 angle;
int ret;

@@ -289,20 +265,19 @@ static ssize_t kb_wake_angle_store(struct device *dev,
if (ret)
return ret;

- msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
- if (!msg)
+ ec_buf = kmalloc(EC_HOST_PARAM_SIZE, GFP_KERNEL);
+ if (!ec_buf)
return -ENOMEM;

- param = (struct ec_params_motion_sense *)msg->data;
- msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
- msg->version = 2;
+ param = (struct ec_params_motion_sense *)ec_buf;
+ resp = (struct ec_response_motion_sense *)ec_buf;
param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
param->kb_wake_angle.data = angle;
- msg->outsize = sizeof(*param);
- msg->insize = sizeof(struct ec_response_motion_sense);

- ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
- kfree(msg);
+ ret = cros_ec_cmd(ec->ec_dev, 2,
+ EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset, param,
+ sizeof(*param), resp, sizeof(*resp), NULL);
+ kfree(ec_buf);
if (ret < 0)
return ret;
return count;
--
2.25.0.341.g760bfbb309-goog