[PATCH v3 17/31] Input: serio - use the compatibility driver attachment API
From: Leon Romanovsky
Date: Wed Nov 18 2026 - 14:35:34 EST
From: Leon Romanovsky <leonro@xxxxxxxxxx>
Manual serio binding holds serio_mutex, but generic sysfs probing does
not take that mutex. The two paths can race while updating driver state
or cleaning up a failed probe.
Use device_driver_attach_unrestricted() after the port ID check to
centralize binding and cleanup. Acquire the device lock with
device_trylock() to exclude generic probing, returning -EBUSY instead of
waiting while holding serio_mutex. Map legacy -1 connect failures to
-ENODEV in the probe callback so they are not reported as -EPERM, while
preserving errors returned by the attachment helper.
Signed-off-by: Leon Romanovsky <leonro@xxxxxxxxxx>
---
drivers/input/serio/serio.c | 29 ++++++++++-------------------
1 file changed, 10 insertions(+), 19 deletions(-)
diff --git a/drivers/input/serio/serio.c b/drivers/input/serio/serio.c
index 54dd26249b027..f239217d7de45 100644
--- a/drivers/input/serio/serio.c
+++ b/drivers/input/serio/serio.c
@@ -83,26 +83,15 @@ static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
{
int error;
- if (serio_match_port(drv->id_table, serio)) {
+ if (!serio_match_port(drv->id_table, serio))
+ return 0;
- serio->dev.driver = &drv->driver;
- if (serio_connect_driver(serio, drv)) {
- serio->dev.driver = NULL;
- return -ENODEV;
- }
+ if (!device_trylock(&serio->dev))
+ return -EBUSY;
- error = device_bind_driver(&serio->dev);
- if (error) {
- dev_warn(&serio->dev,
- "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
- serio->phys, serio->name,
- drv->description, error);
- serio_disconnect_driver(serio);
- serio->dev.driver = NULL;
- return error;
- }
- }
- return 0;
+ error = device_driver_attach_unrestricted(&drv->driver, &serio->dev);
+ device_unlock(&serio->dev);
+ return error;
}
static void serio_find_driver(struct serio *serio)
@@ -744,8 +733,10 @@ static int serio_driver_probe(struct device *dev)
{
struct serio *serio = to_serio_port(dev);
struct serio_driver *drv = to_serio_driver(dev->driver);
+ int error;
- return serio_connect_driver(serio, drv);
+ error = serio_connect_driver(serio, drv);
+ return error == -1 ? -ENODEV : error;
}
static void serio_driver_remove(struct device *dev)
--
2.56.1