Skip to content

Commit 56cb557

Browse files
author
Tzung-Bi Shih
committed
platform/chrome: cros_ec: Add a flag to track registration state
Introduce a `registered` flag to the `struct cros_ec_device` to allow callers to determine if the device has been fully registered and is ready for use. This is a preparatory step to prevent race conditions where other drivers might try to access the device before it is fully registered or after it has been unregistered. Link: https://lore.kernel.org/r/20250828083601.856083-5-tzungbi@kernel.org Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
1 parent 7a79b0b commit 56cb557

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

drivers/platform/chrome/cros_ec.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* battery charging and regulator control, firmware update.
1010
*/
1111

12+
#include <linux/cleanup.h>
1213
#include <linux/interrupt.h>
1314
#include <linux/module.h>
1415
#include <linux/of_platform.h>
@@ -316,6 +317,9 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
316317
goto exit;
317318
}
318319

320+
scoped_guard(mutex, &ec_dev->lock)
321+
ec_dev->registered = true;
322+
319323
dev_info(dev, "Chrome EC device registered\n");
320324

321325
/*
@@ -343,6 +347,9 @@ EXPORT_SYMBOL(cros_ec_register);
343347
*/
344348
void cros_ec_unregister(struct cros_ec_device *ec_dev)
345349
{
350+
scoped_guard(mutex, &ec_dev->lock)
351+
ec_dev->registered = false;
352+
346353
if (ec_dev->mkbp_event_supported)
347354
blocking_notifier_chain_unregister(&ec_dev->event_notifier,
348355
&ec_dev->notifier_ready);

drivers/platform/chrome/cros_ec_proto.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44
// Copyright (C) 2015 Google, Inc
55

6+
#include <linux/cleanup.h>
67
#include <linux/delay.h>
78
#include <linux/device.h>
89
#include <linux/limits.h>
@@ -1153,5 +1154,19 @@ int cros_ec_get_cmd_versions(struct cros_ec_device *ec_dev, u16 cmd)
11531154
}
11541155
EXPORT_SYMBOL_GPL(cros_ec_get_cmd_versions);
11551156

1157+
/**
1158+
* cros_ec_device_registered - Return if the ec_dev is registered.
1159+
*
1160+
* @ec_dev: EC device
1161+
*
1162+
* Return: true if registered. Otherwise, false.
1163+
*/
1164+
bool cros_ec_device_registered(struct cros_ec_device *ec_dev)
1165+
{
1166+
guard(mutex)(&ec_dev->lock);
1167+
return ec_dev->registered;
1168+
}
1169+
EXPORT_SYMBOL_GPL(cros_ec_device_registered);
1170+
11561171
MODULE_LICENSE("GPL");
11571172
MODULE_DESCRIPTION("ChromeOS EC communication protocol helpers");

include/linux/platform_data/cros_ec_proto.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ struct cros_ec_command {
128128
* @dout_size: Size of dout buffer to allocate (zero to use static dout).
129129
* @wake_enabled: True if this device can wake the system from sleep.
130130
* @suspended: True if this device had been suspended.
131+
* @registered: True if this device had been registered.
131132
* @cmd_xfer: Send command to EC and get response.
132133
* Returns the number of bytes received if the communication
133134
* succeeded, but that doesn't mean the EC was happy with the
@@ -186,6 +187,7 @@ struct cros_ec_device {
186187
int dout_size;
187188
bool wake_enabled;
188189
bool suspended;
190+
bool registered;
189191
int (*cmd_xfer)(struct cros_ec_device *ec,
190192
struct cros_ec_command *msg);
191193
int (*pkt_xfer)(struct cros_ec_device *ec,
@@ -278,6 +280,8 @@ int cros_ec_cmd_readmem(struct cros_ec_device *ec_dev, u8 offset, u8 size, void
278280

279281
int cros_ec_get_cmd_versions(struct cros_ec_device *ec_dev, u16 cmd);
280282

283+
bool cros_ec_device_registered(struct cros_ec_device *ec_dev);
284+
281285
/**
282286
* cros_ec_get_time_ns() - Return time in ns.
283287
*

0 commit comments

Comments
 (0)