[PATCH 01/17] platform/chrome: Add EC command msg wrapper

From: Prashant Malani
Date: Thu Jan 30 2020 - 15:32:26 EST


Many callers of cros_ec_cmd_xfer_status() use a similar set up of
allocating and filling a message buffer and then copying any received
data to a target buffer.

Create a utility function cros_ec_send_cmd_msg() that performs this
setup so that callers can use this function instead. Subsequent patches
will convert callers of cros_ec_cmd_xfer_status() to the new function
instead.

Signed-off-by: Prashant Malani <pmalani@xxxxxxxxxxxx>
---
drivers/platform/chrome/cros_ec_proto.c | 57 +++++++++++++++++++++
include/linux/platform_data/cros_ec_proto.h | 5 ++
2 files changed, 62 insertions(+)

diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index da1b1c45043333..53f3bfac71d90e 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -5,6 +5,7 @@

#include <linux/delay.h>
#include <linux/device.h>
+#include <linux/mfd/cros_ec.h>
#include <linux/module.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
@@ -570,6 +571,62 @@ int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
}
EXPORT_SYMBOL(cros_ec_cmd_xfer_status);

+/**
+ * cros_ec_send_cmd_msg() - Utility function to send commands to ChromeOS EC.
+ * @ec: EC device struct.
+ * @version: Command version number (often 0).
+ * @command: Command ID including offset.
+ * @outdata: Data to be sent to the EC.
+ * @outsize: Size of the &outdata buffer.
+ * @indata: Data to be received from the EC.
+ * @insize: Size of the &indata buffer.
+ *
+ * This function is a wrapper around &cros_ec_cmd_xfer_status, and performs
+ * some of the common work involved with sending a command to the EC. This
+ * includes allocating and filling up a &struct cros_ec_command message buffer,
+ * and copying the received data to another buffer.
+ *
+ * Return: The number of bytes transferred on success or negative error code.
+ */
+int cros_ec_send_cmd_msg(struct cros_ec_device *ec, unsigned int version,
+ unsigned int command, void *outdata,
+ unsigned int outsize, void *indata,
+ unsigned int insize)
+{
+ struct cros_ec_command *msg;
+ int ret;
+
+ msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
+ if (!msg)
+ return -ENOMEM;
+
+ msg->version = version;
+ msg->command = command;
+ msg->outsize = outsize;
+ msg->insize = insize;
+
+ if (outdata && outsize > 0)
+ memcpy(msg->data, outdata, outsize);
+
+ ret = cros_ec_cmd_xfer(ec, msg);
+ if (ret < 0) {
+ dev_err(ec->dev, "Command xfer error (err:%d)\n", ret);
+ goto cleanup;
+ } else if (msg->result != EC_RES_SUCCESS) {
+ dev_dbg(ec->dev, "Command result (err: %d)\n", msg->result);
+ ret = -EPROTO;
+ goto cleanup;
+ }
+
+ if (insize)
+ memcpy(indata, msg->data, insize);
+
+cleanup:
+ kfree(msg);
+ return ret;
+}
+EXPORT_SYMBOL(cros_ec_send_cmd_msg);
+
static int get_next_event_xfer(struct cros_ec_device *ec_dev,
struct cros_ec_command *msg,
struct ec_response_get_next_event_v1 *event,
diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h
index 30098a5515231d..166ce26bdd79eb 100644
--- a/include/linux/platform_data/cros_ec_proto.h
+++ b/include/linux/platform_data/cros_ec_proto.h
@@ -201,6 +201,11 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
struct cros_ec_command *msg);

+int cros_ec_send_cmd_msg(struct cros_ec_device *ec_dev, unsigned int version,
+ unsigned int command, void *outdata,
+ unsigned int outsize, void *indata,
+ unsigned int insize);
+
int cros_ec_register(struct cros_ec_device *ec_dev);

int cros_ec_unregister(struct cros_ec_device *ec_dev);
--
2.25.0.341.g760bfbb309-goog