Skip to content

Commit 7a79b0b

Browse files
author
Tzung-Bi Shih
committed
platform/chrome: cros_ec: Separate initialization from cros_ec_register()
Move the initialization of the `struct cros_ec_device` from cros_ec_register() into cros_ec_device_alloc(). This decouples device initialization from registration. By doing so, the per-device lock is now available immediately after allocation, allowing it to be used safely even before the device is fully registered. Link: https://lore.kernel.org/r/20250828083601.856083-4-tzungbi@kernel.org Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
1 parent e19ceeb commit 7a79b0b

1 file changed

Lines changed: 30 additions & 27 deletions

File tree

drivers/platform/chrome/cros_ec.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ static struct cros_ec_platform pd_p = {
3030
.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
3131
};
3232

33+
static void cros_ec_device_free(void *data)
34+
{
35+
struct cros_ec_device *ec_dev = data;
36+
37+
mutex_destroy(&ec_dev->lock);
38+
lockdep_unregister_key(&ec_dev->lockdep_key);
39+
}
40+
3341
struct cros_ec_device *cros_ec_device_alloc(struct device *dev)
3442
{
3543
struct cros_ec_device *ec_dev;
@@ -45,7 +53,28 @@ struct cros_ec_device *cros_ec_device_alloc(struct device *dev)
4553
sizeof(struct ec_params_rwsig_action) +
4654
EC_MAX_REQUEST_OVERHEAD;
4755

56+
ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
57+
if (!ec_dev->din)
58+
return NULL;
59+
60+
ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
61+
if (!ec_dev->dout)
62+
return NULL;
63+
4864
ec_dev->dev = dev;
65+
ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
66+
ec_dev->max_request = sizeof(struct ec_params_rwsig_action);
67+
ec_dev->suspend_timeout_ms = EC_HOST_SLEEP_TIMEOUT_DEFAULT;
68+
69+
BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
70+
BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->panic_notifier);
71+
72+
lockdep_register_key(&ec_dev->lockdep_key);
73+
mutex_init(&ec_dev->lock);
74+
lockdep_set_class(&ec_dev->lock, &ec_dev->lockdep_key);
75+
76+
if (devm_add_action_or_reset(dev, cros_ec_device_free, ec_dev))
77+
return NULL;
4978

5079
return ec_dev;
5180
}
@@ -200,29 +229,7 @@ static int cros_ec_ready_event(struct notifier_block *nb,
200229
int cros_ec_register(struct cros_ec_device *ec_dev)
201230
{
202231
struct device *dev = ec_dev->dev;
203-
int err = 0;
204-
205-
BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
206-
BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->panic_notifier);
207-
208-
ec_dev->max_request = sizeof(struct ec_params_hello);
209-
ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
210-
ec_dev->max_passthru = 0;
211-
ec_dev->ec = NULL;
212-
ec_dev->pd = NULL;
213-
ec_dev->suspend_timeout_ms = EC_HOST_SLEEP_TIMEOUT_DEFAULT;
214-
215-
ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
216-
if (!ec_dev->din)
217-
return -ENOMEM;
218-
219-
ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
220-
if (!ec_dev->dout)
221-
return -ENOMEM;
222-
223-
lockdep_register_key(&ec_dev->lockdep_key);
224-
mutex_init(&ec_dev->lock);
225-
lockdep_set_class(&ec_dev->lock, &ec_dev->lockdep_key);
232+
int err;
226233

227234
/* Send RWSIG continue to jump to RW for devices using RWSIG. */
228235
err = cros_ec_rwsig_continue(ec_dev);
@@ -322,8 +329,6 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
322329
exit:
323330
platform_device_unregister(ec_dev->ec);
324331
platform_device_unregister(ec_dev->pd);
325-
mutex_destroy(&ec_dev->lock);
326-
lockdep_unregister_key(&ec_dev->lockdep_key);
327332
return err;
328333
}
329334
EXPORT_SYMBOL(cros_ec_register);
@@ -343,8 +348,6 @@ void cros_ec_unregister(struct cros_ec_device *ec_dev)
343348
&ec_dev->notifier_ready);
344349
platform_device_unregister(ec_dev->pd);
345350
platform_device_unregister(ec_dev->ec);
346-
mutex_destroy(&ec_dev->lock);
347-
lockdep_unregister_key(&ec_dev->lockdep_key);
348351
}
349352
EXPORT_SYMBOL(cros_ec_unregister);
350353

0 commit comments

Comments
 (0)