Skip to contentSkip to conversation

mfd: cros_ec: non-wake host events support

PATCH Superseded Thierry Escande 2 participants 6 messages Jan 25, 2017 – Feb 13, 2017

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover mfd: cros_ec: non-wake host events support

Hi,

This patchset adds support for non-wake host events. These are events sent by the EC that should not wakeup the device.

This patchset also adds 2 fixes in cros_ec regarding a possible deadlock and an error code returned when it should not.

Gwendal Grignou (1):
cros_ec: Fix deadlock when EC is not responsive at probe

Shawn Nematbakhsh (1):
cros_ec: Don't signal wake event for non-wake host events

Thierry Escande (1):
cros_ec: Don't return error when checking command version

 drivers/mfd/cros_ec.c                   | 14 +++++-
 drivers/platform/chrome/cros_ec_proto.c | 80 ++++++++++++++++++++++++++++++++-
 include/linux/mfd/cros_ec.h             | 12 +++++
 3 files changed, 103 insertions(+), 3 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/3 cros_ec: Don't return error when checking command version

With this patch, cros_ec_query_all() does not return an error if it fails to check for MKBP events support. Instead, the EC device structure indicates that it does not support MKBP events (mkbp_event_supported field) and cros_ec_query_all() returns 0.

---
 drivers/platform/chrome/cros_ec_proto.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/platform/chrome/cros_ec_proto.c +2 -0
index 04053fe..403649a 100644
@@ -371,6 +371,8 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
 	else
 		ec_dev->mkbp_event_supported = 1;
 
+	ret = 0;
+
 exit:
 	kfree(proto_msg);
 	return ret;
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/3 cros_ec: Fix deadlock when EC is not responsive at probe

In reply to Thierry Escande

From: Gwendal Grignou <[email protected]>

When the EC is not responsive at probe, we try to get basic information (protocol to use) later on through cros_xfer_cmd() call. This patch makes sure there is no deadlock when re-probing the EC by replacing call to cros_xfer_cmd() with send_command() in the function cros_ec_get_host_command_version_mask(). Also, this patch adds the function header indicating it must be called protected.

---
 drivers/platform/chrome/cros_ec_proto.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/platform/chrome/cros_ec_proto.c +17 -1
index 403649a..256249b 100644
@@ -235,6 +235,22 @@ static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
 	return ret;
 }
 
+/*
+ * cros_ec_get_host_command_version_mask
+ *
+ * Get the version mask of a given command.
+ *
+ * @ec_dev: EC device to call
+ * @msg: message structure to use
+ * @cmd: command to get the version of.
+ * @mask: result when function returns 0.
+ *
+ * @return 0 on success, error code otherwise
+ *
+ * LOCKING:
+ * the caller has ec_dev->lock mutex or the caller knows there is
+ * no other command in progress.
+ */
 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
 	u16 cmd, u32 *mask)
 {
@@ -256,7 +272,7 @@ static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
 	pver = (struct ec_params_get_cmd_versions *)msg->data;
 	pver->cmd = cmd;
 
-	ret = cros_ec_cmd_xfer(ec_dev, msg);
+	ret = send_command(ec_dev, msg);
 	if (ret > 0) {
 		rver = (struct ec_response_get_cmd_versions *)msg->data;
 		*mask = rver->version_mask;
Signature · 1 line
2.7.4
#3 lore raw

PATCH 3/3 cros_ec: Don't signal wake event for non-wake host events

In reply to Thierry Escande

From: Shawn Nematbakhsh <[email protected]>

The subset of wake-enabled host events is defined by the EC, but the EC may still send non-wake host events if we're in the process of suspending. Get the mask of wake-enabled host events from the EC and filter out non-wake events to prevent spurious aborted suspend attempts.

---
 drivers/mfd/cros_ec.c                   | 14 ++++++--
 drivers/platform/chrome/cros_ec_proto.c | 60 +++++++++++++++++++++++++++++++++
 include/linux/mfd/cros_ec.h             | 12 +++++++
 3 files changed, 84 insertions(+), 2 deletions(-)
drivers/mfd/cros_ec.c +12 -2
index abd8342..510dfbb 100644
@@ -53,12 +53,22 @@ static const struct mfd_cell ec_pd_cell = {
 static irqreturn_t ec_irq_thread(int irq, void *data)
 {
 	struct cros_ec_device *ec_dev = data;
+	u8 wake_event = 1;
+	u32 host_event;
 	int ret;
 
-	if (device_may_wakeup(ec_dev->dev))
+	ret = cros_ec_get_next_event(ec_dev);
+
+	if (ret > 0 && ec_dev->mkbp_event_supported) {
+		/* Don't signal wake event for non-wake host events */
+		host_event = cros_ec_get_host_event(ec_dev);
+		if (host_event && !(host_event & ec_dev->host_event_wake_mask))
+			wake_event = 0;
+	}
+
+	if (wake_event && device_may_wakeup(ec_dev->dev))
 		pm_wakeup_event(ec_dev->dev, 0);
 
-	ret = cros_ec_get_next_event(ec_dev);
 	if (ret > 0)
 		blocking_notifier_call_chain(&ec_dev->event_notifier,
 					     0, ec_dev);
drivers/platform/chrome/cros_ec_proto.c +60 -0

75 lines not on this page · raw message

include/linux/mfd/cros_ec.h +12 -0
index f62043a..8f37c4e 100644
@@ -146,6 +146,7 @@ struct cros_ec_device {
 
 	struct ec_response_get_next_event event_data;
 	int event_size;
+	u32 host_event_wake_mask;
 };
 
 /**
@@ -297,6 +298,17 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev);
  */
 int cros_ec_get_next_event(struct cros_ec_device *ec_dev);
 
+/**
+ * cros_ec_get_host_event - Return a mask of event set by the EC.
+ *
+ * Once cros_ec_get_next_event() has been called, if the event source is
+ * a host event, this function returns the precise event that triggered
+ * the interrupt.
+ *
+ * This function is a helper to know which events are raised.
+ */
+u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
+
 /* sysfs stuff */
 extern struct attribute_group cros_ec_attr_group;
 extern struct attribute_group cros_ec_lightbar_attr_group;
Signature · 1 line
2.7.4
#4 lore raw

Re: [PATCH 3/3] cros_ec: Don't signal wake event for non-wake host events

Quoted 21 lines · Thierry Escande

From: Shawn Nematbakhsh <[email protected]>

The subset of wake-enabled host events is defined by the EC, but the EC may still send non-wake host events if we're in the process of suspending. Get the mask of wake-enabled host events from the EC and filter out non-wake events to prevent spurious aborted suspend attempts.

---
 drivers/mfd/cros_ec.c                   | 14 ++++++--
 drivers/platform/chrome/cros_ec_proto.c | 60 +++++++++++++++++++++++++++++++++
 include/linux/mfd/cros_ec.h             | 12 +++++++
 3 files changed, 84 insertions(+), 2 deletions(-)
drivers/mfd/cros_ec.c +0 -0
index abd8342..510dfbb 100644

drivers/mfd/cros_ec.c static const struct mfd_cell ec_pd_cell = { · line 56

Quoted 4 lines
drivers/mfd/cros_ec.c +1 -0
@@ -53,12 +53,22 @@ static const struct mfd_cell ec_pd_cell = {
 static irqreturn_t ec_irq_thread(int irq, void *data)
 {
 	struct cros_ec_device *ec_dev = data;
+	u8 wake_event = 1;

Bool, true?

drivers/mfd/cros_ec.c static const struct mfd_cell ec_pd_cell = { · line 60

Quoted 5 lines
drivers/mfd/cros_ec.c +2 -1
+	u32 host_event;
 	int ret;
 
-	if (device_may_wakeup(ec_dev->dev))
+	ret = cros_ec_get_next_event(ec_dev);

What does (ret == 0) mean?

... and is is possible for (ret < 0)?

drivers/mfd/cros_ec.c static const struct mfd_cell ec_pd_cell = { · line 62

Quoted 1 line
drivers/mfd/cros_ec.c +1 -0
+	if (ret > 0 && ec_dev->mkbp_event_supported) {

cros_ec_get_host_event() checks for (ec_dev->mkbp_event_supported) anyway, so you can drop it here no?

drivers/mfd/cros_ec.c static const struct mfd_cell ec_pd_cell = { · line 66

Quoted 4 lines
drivers/mfd/cros_ec.c +4 -0
+		/* Don't signal wake event for non-wake host events */
+		host_event = cros_ec_get_host_event(ec_dev);
+		if (host_event && !(host_event & ec_dev->host_event_wake_mask))
+			wake_event = 0;

false

Quoted 121 lines
drivers/mfd/cros_ec.c +3 -1
+	}
+
+	if (wake_event && device_may_wakeup(ec_dev->dev))
 		pm_wakeup_event(ec_dev->dev, 0);
 
-	ret = cros_ec_get_next_event(ec_dev);
 	if (ret > 0)
 		blocking_notifier_call_chain(&ec_dev->event_notifier,
 					     0, ec_dev);
drivers/platform/chrome/cros_ec_proto.c +60 -0
index 256249b..a216a32 100644
@@ -150,6 +150,40 @@ int cros_ec_check_result(struct cros_ec_device *ec_dev,
 }
 EXPORT_SYMBOL(cros_ec_check_result);
 
+/*
+ * cros_ec_get_host_event_wake_mask
+ *
+ * Get the mask of host events that cause wake from suspend.
+ *
+ * @ec_dev: EC device to call
+ * @msg: message structure to use
+ * @mask: result when function returns >=0.
+ *
+ * LOCKING:
+ * the caller has ec_dev->lock mutex, or the caller knows there is
+ * no other command in progress.
+ */
+static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
+					    struct cros_ec_command *msg,
+					    uint32_t *mask)
+{
+	struct ec_response_host_event_mask *r;
+	int ret;
+
+	msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
+	msg->version = 0;
+	msg->outsize = 0;
+	msg->insize = sizeof(*r);
+
+	ret = send_command(ec_dev, msg);
+	if (ret > 0) {
+		r = (struct ec_response_host_event_mask *)msg->data;
+		*mask = r->mask;
+	}
+
+	return ret;
+}
+
 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
 					    int devidx,
 					    struct cros_ec_command *msg)
@@ -387,6 +421,15 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
 	else
 		ec_dev->mkbp_event_supported = 1;
 
+	/*
+	 * Get host event wake mask, assume all events are wake events
+	 * if unavailable.
+	 */
+	ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
+					       &ec_dev->host_event_wake_mask);
+	if (ret < 0)
+		ec_dev->host_event_wake_mask = U32_MAX;
+
 	ret = 0;
 
 exit:
@@ -507,3 +550,20 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev)
 		return get_keyboard_state_event(ec_dev);
 }
 EXPORT_SYMBOL(cros_ec_get_next_event);
+
+u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
+{
+	if (WARN_ON(!ec_dev->mkbp_event_supported))
+		return 0;
+
+	if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
+		return 0;
+
+	if (ec_dev->event_size != sizeof(u32)) {
+		dev_warn(ec_dev->dev, "Invalid host event size\n");
+		return 0;
+	}
+
+	return get_unaligned_le32(&ec_dev->event_data.data.host_event);
+}
+EXPORT_SYMBOL(cros_ec_get_host_event);
include/linux/mfd/cros_ec.h +12 -0
index f62043a..8f37c4e 100644
@@ -146,6 +146,7 @@ struct cros_ec_device {
 
 	struct ec_response_get_next_event event_data;
 	int event_size;
+	u32 host_event_wake_mask;
 };
 
 /**
@@ -297,6 +298,17 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev);
  */
 int cros_ec_get_next_event(struct cros_ec_device *ec_dev);
 
+/**
+ * cros_ec_get_host_event - Return a mask of event set by the EC.
+ *
+ * Once cros_ec_get_next_event() has been called, if the event source is
+ * a host event, this function returns the precise event that triggered
+ * the interrupt.
+ *
+ * This function is a helper to know which events are raised.
+ */
+u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
+
 /* sysfs stuff */
 extern struct attribute_group cros_ec_attr_group;
 extern struct attribute_group cros_ec_lightbar_attr_group;
Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#5 lore raw

Re: [PATCH 3/3] cros_ec: Don't signal wake event for non-wake host events

Hi Lee,

Quoted 18 lines · Lee Jones
Quoted 16 lines · Thierry Escande

From: Shawn Nematbakhsh <[email protected]>

The subset of wake-enabled host events is defined by the EC, but the EC may still send non-wake host events if we're in the process of suspending. Get the mask of wake-enabled host events from the EC and filter out non-wake events to prevent spurious aborted suspend attempts.

---
 drivers/mfd/cros_ec.c                   | 14 ++++++--
 drivers/platform/chrome/cros_ec_proto.c | 60 +++++++++++++++++++++++++++++++++
 include/linux/mfd/cros_ec.h             | 12 +++++++
 3 files changed, 84 insertions(+), 2 deletions(-)
Quoted 11 lines

drivers/mfd/cros_ec.c static const struct mfd_cell ec_pd_cell = { · line 56

Quoted 9 lines
drivers/mfd/cros_ec.c +1 -0
index abd8342..510dfbb 100644
@@ -53,12 +53,22 @@ static const struct mfd_cell ec_pd_cell = {
 static irqreturn_t ec_irq_thread(int irq, void *data)
 {
 	struct cros_ec_device *ec_dev = data;
+	u8 wake_event = 1;

Bool, true?

Ok.

Quoted 8 lines

drivers/mfd/cros_ec.c static const struct mfd_cell ec_pd_cell = { · line 60

Quoted 6 lines
drivers/mfd/cros_ec.c +2 -1
+	u32 host_event;
 	int ret;
 
-	if (device_may_wakeup(ec_dev->dev))
+	ret = cros_ec_get_next_event(ec_dev);

What does (ret == 0) mean?

The get_next_event() API called from cros_ec_get_next_event() only sets event_data in the ec_dev structure if the command has actually been sent. So (ret == 0) is not treated as an error and is silently ignored. In this case, the interrupt is treated as a wake event.

Quoted 7 lines

... and is is possible for (ret < 0)?

drivers/mfd/cros_ec.c static const struct mfd_cell ec_pd_cell = { · line 62

Quoted 1 line
drivers/mfd/cros_ec.c +1 -0
+	if (ret > 0 && ec_dev->mkbp_event_supported) {

cros_ec_get_host_event() checks for (ec_dev->mkbp_event_supported) anyway, so you can drop it here no?

Sure but we only need to call cros_ec_get_host_event() if mkbp_event_supported is true. Anyway, I'll move the host_event check with cros_ec_get_host_event() into cros_ec_get_next_event() to avoid this double check.

Quoted 7 lines

drivers/mfd/cros_ec.c static const struct mfd_cell ec_pd_cell = { · line 66

Quoted 5 lines
drivers/mfd/cros_ec.c +4 -0
+		/* Don't signal wake event for non-wake host events */
+		host_event = cros_ec_get_host_event(ec_dev);
+		if (host_event && !(host_event & ec_dev->host_event_wake_mask))
+			wake_event = 0;

false

Will do.

Thanks,
Thierry

Quoted 123 lines
Quoted 122 lines
drivers/mfd/cros_ec.c +3 -1
+	}
+
+	if (wake_event && device_may_wakeup(ec_dev->dev))
 		pm_wakeup_event(ec_dev->dev, 0);
 
-	ret = cros_ec_get_next_event(ec_dev);
 	if (ret > 0)
 		blocking_notifier_call_chain(&ec_dev->event_notifier,
 					     0, ec_dev);
drivers/platform/chrome/cros_ec_proto.c +60 -0
index 256249b..a216a32 100644
@@ -150,6 +150,40 @@ int cros_ec_check_result(struct cros_ec_device *ec_dev,
 }
 EXPORT_SYMBOL(cros_ec_check_result);
 
+/*
+ * cros_ec_get_host_event_wake_mask
+ *
+ * Get the mask of host events that cause wake from suspend.
+ *
+ * @ec_dev: EC device to call
+ * @msg: message structure to use
+ * @mask: result when function returns >=0.
+ *
+ * LOCKING:
+ * the caller has ec_dev->lock mutex, or the caller knows there is
+ * no other command in progress.
+ */
+static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
+					    struct cros_ec_command *msg,
+					    uint32_t *mask)
+{
+	struct ec_response_host_event_mask *r;
+	int ret;
+
+	msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
+	msg->version = 0;
+	msg->outsize = 0;
+	msg->insize = sizeof(*r);
+
+	ret = send_command(ec_dev, msg);
+	if (ret > 0) {
+		r = (struct ec_response_host_event_mask *)msg->data;
+		*mask = r->mask;
+	}
+
+	return ret;
+}
+
 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
 					    int devidx,
 					    struct cros_ec_command *msg)
@@ -387,6 +421,15 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
 	else
 		ec_dev->mkbp_event_supported = 1;
 
+	/*
+	 * Get host event wake mask, assume all events are wake events
+	 * if unavailable.
+	 */
+	ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
+					       &ec_dev->host_event_wake_mask);
+	if (ret < 0)
+		ec_dev->host_event_wake_mask = U32_MAX;
+
 	ret = 0;
 
 exit:
@@ -507,3 +550,20 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev)
 		return get_keyboard_state_event(ec_dev);
 }
 EXPORT_SYMBOL(cros_ec_get_next_event);
+
+u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
+{
+	if (WARN_ON(!ec_dev->mkbp_event_supported))
+		return 0;
+
+	if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
+		return 0;
+
+	if (ec_dev->event_size != sizeof(u32)) {
+		dev_warn(ec_dev->dev, "Invalid host event size\n");
+		return 0;
+	}
+
+	return get_unaligned_le32(&ec_dev->event_data.data.host_event);
+}
+EXPORT_SYMBOL(cros_ec_get_host_event);
include/linux/mfd/cros_ec.h +12 -0
index f62043a..8f37c4e 100644
@@ -146,6 +146,7 @@ struct cros_ec_device {
 
 	struct ec_response_get_next_event event_data;
 	int event_size;
+	u32 host_event_wake_mask;
 };
 
 /**
@@ -297,6 +298,17 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev);
  */
 int cros_ec_get_next_event(struct cros_ec_device *ec_dev);
 
+/**
+ * cros_ec_get_host_event - Return a mask of event set by the EC.
+ *
+ * Once cros_ec_get_next_event() has been called, if the event source is
+ * a host event, this function returns the precise event that triggered
+ * the interrupt.
+ *
+ * This function is a helper to know which events are raised.
+ */
+u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
+
 /* sysfs stuff */
 extern struct attribute_group cros_ec_attr_group;
 extern struct attribute_group cros_ec_lightbar_attr_group;