[PATCH 7/8] platform/chrome: cros_ec_chardev: Add event relayer

Tzung-Bi Shih
2026-04-27T13:47:56Z

Introduce an event relayer mechanism. Instead of each open file
registering directly with `ec_dev->event_notifier`, the platform device
registers a single relayer notifier. Individual files then register
with a local subscribers list in `chardev_pdata`.

This allows the driver to safely disconnect from the event chain
`ec_dev->event_notifier` during cros_ec_chardev_remove(), preventing
events from being delivered to open files after the device is removed,
while still allowing those files to be closed safely later.

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
This is an implementation of the idea from [1].

[1] https://lore.kernel.org/all/20251117153301.GD10864@nvidia.com/
---
drivers/platform/chrome/cros_ec_chardev.c | 32 ++++++++++++++++++++---
1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_chardev.c b/drivers/platform/chrome/cros_ec_chardev.c
index 352d61a2f3c6..7e046fc56998 100644
--- a/drivers/platform/chrome/cros_ec_chardev.c
+++ b/drivers/platform/chrome/cros_ec_chardev.c
@@ -40,6 +40,8 @@ struct chardev_pdata {
struct kref kref;
struct cros_ec_device *ec_dev;
u16 cmd_offset;
+ struct blocking_notifier_head subscribers;
+ struct notifier_block relay;
};

static void chardev_pdata_release(struct kref *kref)
@@ -49,6 +51,17 @@ static void chardev_pdata_release(struct kref *kref)
kfree(pdata);
}

+static int cros_ec_chardev_relay_event(struct notifier_block *nb,
+ unsigned long queued_during_suspend,
+ void *_notify)
+{
+ struct chardev_pdata *pdata = container_of(nb, typeof(*pdata), relay);
+
+ blocking_notifier_call_chain(&pdata->subscribers, queued_during_suspend,
+ _notify);
+ return NOTIFY_OK;
+}
+
struct chardev_priv {
struct notifier_block notifier;
wait_queue_head_t wait_event;
@@ -190,7 +203,7 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp)
nonseekable_open(inode, filp);

priv->notifier.notifier_call = cros_ec_chardev_mkbp_event;
- ret = blocking_notifier_chain_register(&pdata->ec_dev->event_notifier,
+ ret = blocking_notifier_chain_register(&pdata->subscribers,
&priv->notifier);
if (ret) {
dev_err(pdata->ec_dev->dev,
@@ -271,7 +284,7 @@ static int cros_ec_chardev_release(struct inode *inode, struct file *filp)
struct chardev_priv *priv = filp->private_data;
struct ec_event *event, *e;

- blocking_notifier_chain_unregister(&priv->pdata->ec_dev->event_notifier,
+ blocking_notifier_chain_unregister(&priv->pdata->subscribers,
&priv->notifier);
kref_put(&priv->pdata->kref, chardev_pdata_release);

@@ -403,6 +416,14 @@ static int cros_ec_chardev_probe(struct platform_device *pdev)
kref_init(&pdata->kref);
pdata->ec_dev = ec->ec_dev;
pdata->cmd_offset = ec->cmd_offset;
+ BLOCKING_INIT_NOTIFIER_HEAD(&pdata->subscribers);
+ pdata->relay.notifier_call = cros_ec_chardev_relay_event;
+ ret = blocking_notifier_chain_register(&pdata->ec_dev->event_notifier,
+ &pdata->relay);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to register event notifier\n");
+ goto err_put_pdata;
+ }

pdata->misc.minor = MISC_DYNAMIC_MINOR;
pdata->misc.fops = &chardev_fops;
@@ -412,10 +433,13 @@ static int cros_ec_chardev_probe(struct platform_device *pdev)
ret = misc_register(&pdata->misc);
if (ret) {
dev_err(&pdev->dev, "failed to register misc device\n");
- goto err_put_pdata;
+ goto err_unregister_notifier;
}

return 0;
+err_unregister_notifier:
+ blocking_notifier_chain_unregister(&pdata->ec_dev->event_notifier,
+ &pdata->relay);
err_put_pdata:
kref_put(&pdata->kref, chardev_pdata_release);
return ret;
@@ -425,6 +449,8 @@ static void cros_ec_chardev_remove(struct platform_device *pdev)
{
struct chardev_pdata *pdata = platform_get_drvdata(pdev);

+ blocking_notifier_chain_unregister(&pdata->ec_dev->event_notifier,
+ &pdata->relay);
misc_deregister(&pdata->misc);
kref_put(&pdata->kref, chardev_pdata_release);
}
--
2.51.0