Skip to contentSkip to conversation

iio: common: cros_ec_sensors: Add protocol v3 support

PATCH Superseded Fabien Lahoudere 3 participants 5 messages Jun 28, 2019 – Jul 1, 2019

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover iio: common: cros_ec_sensors: Add protocol v3 support

This patch is part of a split of the following patch:
https://lkml.org/lkml/2019/6/18/268
To fix Enric comments from https://lkml.org/lkml/2019/6/25/949
I extract it from the other serie to speed up acceptance because other patches need it to be upstreamed.

Changes since v1:
- Drop second patch
- return ENODEV if version is 0

Fabien Lahoudere (1):
iio: common: cros_ec_sensors: determine protocol version

 .../cros_ec_sensors/cros_ec_sensors_core.c    | 40 ++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
Signature · 1 line
2.19.2
#1 lore raw

PATCH v2 1/1 iio: common: cros_ec_sensors: determine protocol version

This patch adds a function to determine which version of the protocol is used to communicate with EC.

---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 40 ++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +39 -1
index 130362ca421b..75d9b617f6c8 100644
@@ -25,6 +25,35 @@ static char *cros_ec_loc[] = {
 	[MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
+static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
+					     u16 cmd_offset, u16 cmd, u32 *mask)
+{
+	int ret;
+	struct {
+		struct cros_ec_command msg;
+		union {
+			struct ec_params_get_cmd_versions params;
+			struct ec_response_get_cmd_versions resp;
+		};
+	} __packed buf = {
+		.msg = {
+			.version = 0,
+			.command = EC_CMD_GET_CMD_VERSIONS + cmd_offset,
+			.insize = sizeof(struct ec_response_get_cmd_versions),
+			.outsize = sizeof(struct ec_params_get_cmd_versions)
+			},
+		.params = {.cmd = cmd}
+	};
+
+	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+	if (ret < 0)
+		return ret;
+
+	*mask = buf.resp.version_mask;
+
+	return 0;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
 			      struct iio_dev *indio_dev,
 			      bool physical_device)
@@ -33,6 +62,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 	struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
 	struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
 	struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
+	u32 ver_mask;
+	int ret;
 
 	platform_set_drvdata(pdev, indio_dev);
 
@@ -47,8 +78,15 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 
 	mutex_init(&state->cmd_lock);
 
+	ret = cros_ec_get_host_cmd_version_mask(state->ec,
+						ec->cmd_offset,
+						EC_CMD_MOTION_SENSE_CMD,
+						&ver_mask);
+	if (ret < 0 || ver_mask == 0)
+		return -ENODEV;
+
 	/* Set up the host command structure. */
-	state->msg->version = 2;
+	state->msg->version = fls(ver_mask) - 1;;
 	state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
 	state->msg->outsize = sizeof(struct ec_params_motion_sense);
 
Signature · 1 line
2.19.2
#2 lore raw

Re: [PATCH v2 1/1] iio: common: cros_ec_sensors: determine protocol version

Hi,

Thanks for the quick respin, two few comments below

Quoted 5 lines · Fabien Lahoudere

This patch adds a function to determine which version of the protocol is used to communicate with EC.

The order must be the opposite, first Nick and then you.

Quoted 45 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 40 ++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +29 -0
index 130362ca421b..75d9b617f6c8 100644
@@ -25,6 +25,35 @@ static char *cros_ec_loc[] = {
 	[MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
+static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
+					     u16 cmd_offset, u16 cmd, u32 *mask)
+{
+	int ret;
+	struct {
+		struct cros_ec_command msg;
+		union {
+			struct ec_params_get_cmd_versions params;
+			struct ec_response_get_cmd_versions resp;
+		};
+	} __packed buf = {
+		.msg = {
+			.version = 0,
+			.command = EC_CMD_GET_CMD_VERSIONS + cmd_offset,
+			.insize = sizeof(struct ec_response_get_cmd_versions),
+			.outsize = sizeof(struct ec_params_get_cmd_versions)
+			},
+		.params = {.cmd = cmd}
+	};
+
+	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+	if (ret < 0)
+		return ret;
+
+	*mask = buf.resp.version_mask;
+
+	return 0;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
 			      struct iio_dev *indio_dev,
 			      bool physical_device)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 65

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -0
@@ -33,6 +62,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 	struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
 	struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
 	struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
+	u32 ver_mask;

If you follow the error path in the cros_ec_get_host_cmd_version_mask there is a possible use of an uninitialized variable.

	u32 ver_mask = 0;

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 80

Quoted 8 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -0
+	int ret;
 
 	platform_set_drvdata(pdev, indio_dev);
 
@@ -47,8 +78,15 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 
 	mutex_init(&state->cmd_lock);
 

What about adding a comment to explain the ver_mask thing for the record?

/*
* If the EC is very old or misbehaving is it possible that the
* communication succeed and have the version mask set to an invalid
* value. So check that version mask is valid (!= 0), otherwise return
* an error.
*/

At least this is what I understood from the previous discussion.

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 -0
+	ret = cros_ec_get_host_cmd_version_mask(state->ec,
+						ec->cmd_offset,
+						EC_CMD_MOTION_SENSE_CMD,

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 92

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +5 -1
+						&ver_mask);
+	if (ret < 0 || ver_mask == 0)
+		return -ENODEV;
+
 	/* Set up the host command structure. */
-	state->msg->version = 2;
+	state->msg->version = fls(ver_mask) - 1;;
 	state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
 	state->msg->outsize = sizeof(struct ec_params_motion_sense);
 

Thanks,
~ Enric

#3 lore raw

Re: [PATCH v2 1/1] iio: common: cros_ec_sensors: determine protocol version

In reply to Fabien Lahoudere
Thank you for the patch! Perhaps something to improve:

Hi Fabien,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on iio/togreg]
[also build test WARNING on next-20190625]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Fabien-Lahoudere/iio-common-cros_ec_sensors-determine-protocol-version/20190701-223411
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg

If you fix the issue, kindly add following tag

coccinelle warnings: (new ones prefixed by >>)

>> drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c:89:41-42: Unneeded semicolon

Please review and possibly fold the followup patch.

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw

[PATCH] iio: common: cros_ec_sensors: fix semicolon.cocci warnings

In reply to Fabien Lahoudere
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c:89:41-42: Unneeded semicolon

From: kbuild test robot <[email protected]>

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c:89:41-42: Unneeded semicolon

Remove unneeded semicolon.

Generated by: scripts/coccinelle/misc/semicolon.cocci

Fixes a7862d65b738 ("iio: common: cros_ec_sensors: determine protocol version")
CC Fabien Lahoudere <[email protected]>

---

url:    https://github.com/0day-ci/linux/commits/Fabien-Lahoudere/iio-common-cros_ec_sensors-determine-protocol-version/20190701-223411
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
 cros_ec_sensors_core.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -1
@@ -86,7 +86,7 @@ int cros_ec_sensors_core_init(struct pla
 		return -ENODEV;
 
 	/* Set up the host command structure. */
-	state->msg->version = fls(ver_mask) - 1;;
+	state->msg->version = fls(ver_mask) - 1;
 	state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
 	state->msg->outsize = sizeof(struct ec_params_motion_sense);