diff mbox series

[v3,1/3] platform/chrome: cros_ec_proto: Introduce cros_ec_cmd_readmem()

Message ID 20240527-cros_ec-hwmon-v3-1-e5cd5ab5ba37@weissschuh.net (mailing list archive)
State Superseded
Headers show
Series ChromeOS Embedded controller hwmon driver | expand

Commit Message

Thomas Weißschuh May 27, 2024, 8:58 p.m. UTC
To read from the EC memory different mechanism are possible.
ECs connected via LPC expose their memory via a ->cmd_readmem operation.
Other protocols require the usage of EC_CMD_READ_MEMMAP, which on the
other hand is not implemented by LPC ECs.

Provide a helper that automatically selects the correct mechanism.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/platform/chrome/cros_ec_proto.c     | 27 +++++++++++++++++++++++++++
 include/linux/platform_data/cros_ec_proto.h |  2 ++
 2 files changed, 29 insertions(+)

Comments

Tzung-Bi Shih May 28, 2024, 6:39 a.m. UTC | #1
On Mon, May 27, 2024 at 10:58:31PM +0200, Thomas Weißschuh wrote:
> +/**
> + * cros_ec_cmd_readmem - Read from EC memory.
> + *
> + * @ec_dev: EC device
> + * @offset: Is within EC_LPC_ADDR_MEMMAP region.
> + * @size: Number of bytes to read. zero means "read a string" (including
> + *        the trailing '\0').

The behavior is cros_ec_lpc_readmem() only but not for cros_ec_cmd().
Thomas Weißschuh May 28, 2024, 7:15 a.m. UTC | #2
On 2024-05-28 06:39:03+0000, Tzung-Bi Shih wrote:
> On Mon, May 27, 2024 at 10:58:31PM +0200, Thomas Weißschuh wrote:
> > +/**
> > + * cros_ec_cmd_readmem - Read from EC memory.
> > + *
> > + * @ec_dev: EC device
> > + * @offset: Is within EC_LPC_ADDR_MEMMAP region.
> > + * @size: Number of bytes to read. zero means "read a string" (including
> > + *        the trailing '\0').
> 
> The behavior is cros_ec_lpc_readmem() only but not for cros_ec_cmd().

Indeed.
I thought I checked for this specifically, but got it wrong.

I'll drop the docs and add a

	if (!size)
		return -EINVAL;

to make sure nobody starts relying on that behaviour when writing a
driver against an LPC EC.
diff mbox series

Patch

diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index 945b1b15a04c..4b48fa1fe3e0 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -1035,3 +1035,30 @@  int cros_ec_cmd(struct cros_ec_device *ec_dev,
 	return ret;
 }
 EXPORT_SYMBOL_GPL(cros_ec_cmd);
+
+/**
+ * cros_ec_cmd_readmem - Read from EC memory.
+ *
+ * @ec_dev: EC device
+ * @offset: Is within EC_LPC_ADDR_MEMMAP region.
+ * @size: Number of bytes to read. zero means "read a string" (including
+ *        the trailing '\0').
+ *        Caller must ensure that the buffer is large enough for the
+ *        result when reading a string.
+ * @dest: EC command output data
+ *
+ * Return: >= 0 on success, negative error number on failure.
+ */
+int cros_ec_cmd_readmem(struct cros_ec_device *ec_dev, u8 offset, u8 size, void *dest)
+{
+	struct ec_params_read_memmap params;
+
+	if (ec_dev->cmd_readmem)
+		return ec_dev->cmd_readmem(ec_dev, offset, size, dest);
+
+	params.offset = offset;
+	params.size = size;
+	return cros_ec_cmd(ec_dev, 0, EC_CMD_READ_MEMMAP,
+			   &params, sizeof(params), dest, size);
+}
+EXPORT_SYMBOL_GPL(cros_ec_cmd_readmem);
diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h
index 8865e350c12a..1ddc52603f9a 100644
--- a/include/linux/platform_data/cros_ec_proto.h
+++ b/include/linux/platform_data/cros_ec_proto.h
@@ -261,6 +261,8 @@  int cros_ec_get_sensor_count(struct cros_ec_dev *ec);
 int cros_ec_cmd(struct cros_ec_device *ec_dev, unsigned int version, int command, const void *outdata,
 		    size_t outsize, void *indata, size_t insize);
 
+int cros_ec_cmd_readmem(struct cros_ec_device *ec_dev, u8 offset, u8 size, void *dest);
+
 /**
  * cros_ec_get_time_ns() - Return time in ns.
  *