Skip to contentSkip to conversation

serdev: handle devices without an ACPI or device tree node

PATCH Superseded ChunAn Wu 3 participants 11 messages Oct 24

Subsystems (7)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

ChunAn Wu

author
#0 lore raw

v5 cover serdev: handle devices without an ACPI or device tree node

Driver-created serdev clients without an ACPI companion or device tree node currently have no modalias and cannot be matched to a driver:
their modalias sysfs read returns -ENODEV and their creators force a bind with device_driver_attach().

Backlight function on dell-uart-backlight is not affected: that class device is registered independently and gets its own add uevent. The user-visible breakage is the failing modalias read on an existing serdev child -- Checkbox dkms_info_attachment aborts with "OSError: [Errno 19] No such device".

Resolve the two long-standing serdev-core TODOs by letting the creator pass a modalias to serdev_device_add() and giving the serdev driver an id_table, modelled on the SPI bus. ACPI and OF match first and are unchanged. Convert dell-uart-backlight and the Lenovo Yoga Tab 2 Pro fastcharger to this path and drop their force-attach workarounds.

Changes in v5:
- Patch 1: restore is_serdev_device() guard at the top of serdev_device_match(). Dropping it was correct when match only called acpi/of helpers, but Patch 1 also adds serdev_match_id(sdrv->id_table, serdev->modalias), which dereferences serdev_device-only memory. Since serdev_controller shares serdev_bus_type, the driver core calls .match on controllers too; without the guard, serdev->modalias on a controller reads bytes from inside host/nr/serdev/ops, and strcmp() can walk past the end of the controller struct. Reported by sashiko-bot on 2/3 and 3/3.

Changes in v4:
- Patch 1: drop the dead is_serdev_device() guard from the match callback, and dereference serdev once at the top
- Patch 1: drop the "large enough" comment on SERDEV_NAME_SIZE
- Patch 1: in struct serdev_device_driver, place id_table right under driver, next to driver->of_match_table and driver->acpi_match_table
- Patch 1: merge serdev_device_set_modalias() into serdev_device_add() so the modalias is set at the only point where forgetting it would matter; internal firmware callers pass NULL
- All three patches: tightened commit messages and moved the v2/v3 changelog narrative out of Patch 1 into this cover letter

Changes in v3:
- Rename struct serdev_device::name to ::modalias; dev_name() is unchanged
- Model matching on the SPI bus: add struct serdev_device_id and id_table; drop driver->name matching
- Add serdev_device_set_modalias() helper and SERDEV_MODULE_PREFIX
- No MODULE_DEVICE_TABLE(serdev, ...) support: both in-tree firmware-less clients are created by the same module that provides their driver, so udev has nothing to autoload

Changes in v2:
- Instead of silently returning success with no modalias, use a creator-supplied name as a "serial:<name>" modalias fallback and as the matching key, so clients can bind through normal serdev matching

v4: https://lore.kernel.org/lkml/[email protected]/
v3: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

ChunAn Wu (3):
serdev: handle devices without an ACPI or device tree node platform/x86: dell-uart-backlight: Bind through normal serdev matching platform/x86: lenovo-yoga-tab2-pro-1380-fastcharger: Bind through
normal serdev matching

 MAINTAINERS                                   |  1 +
 .../platform/x86/dell/dell-uart-backlight.c   | 18 +++---
 .../lenovo/yoga-tab2-pro-1380-fastcharger.c   | 25 +++-----
 .../platform/x86/x86-android-tablets/core.c   |  2 +-
 drivers/power/sequencing/pwrseq-pcie-m2.c     |  2 +-
 drivers/tty/serdev/core.c                     | 62 ++++++++++++++++---
 include/linux/device-id/serdev.h              | 19 ++++++
 include/linux/serdev.h                        | 12 +++-
 8 files changed, 102 insertions(+), 39 deletions(-)
 create mode 100644 include/linux/device-id/serdev.h

base-commit: 9e947d96915af63e28896caae85a81b74855621b --
2.34.1

author
#1 lore raw

PATCH v5 1/3 serdev: handle devices without an ACPI or device tree node

Serdev clients instantiated by a driver have no ACPI companion and no device tree node. Their modalias read returns -ENODEV, their add uevent is suppressed, and they cannot be driver-matched, so creators force a bind with device_driver_attach().

Resolve both TODOs in the serdev core by modelling matching on the SPI
bus: let the creator pass a modalias to serdev_device_add() and give struct serdev_device_driver an id_table. ACPI and OF are still tried first; clients without their own modalias keep returning -ENODEV. The device name, derived from the controller, is unchanged.

Split struct serdev_device_id and its defines into a new include/linux/device-id/serdev.h, following the pattern already used by the SPI and I2C buses.

---
 MAINTAINERS                                   |  1 +
 .../platform/x86/x86-android-tablets/core.c   |  2 +-
 drivers/power/sequencing/pwrseq-pcie-m2.c     |  2 +-
 drivers/tty/serdev/core.c                     | 62 ++++++++++++++++---
 include/linux/device-id/serdev.h              | 19 ++++++
 include/linux/serdev.h                        | 12 +++-
 6 files changed, 86 insertions(+), 12 deletions(-)
 create mode 100644 include/linux/device-id/serdev.h
MAINTAINERS +1 -0
index 917f3f72e31b..06e67aa5d9b0 100644
@@ -24905,6 +24905,7 @@ L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/serial/serial.yaml
 F:	drivers/tty/serdev/
+F:	include/linux/device-id/serdev.h
 F:	include/linux/serdev.h
 F:	rust/helpers/serdev.c
 F:	rust/kernel/serdev.rs
drivers/platform/x86/x86-android-tablets/core.c +1 -1
index cfff7f5eac5d..853aa6ff5d50 100644
@@ -317,7 +317,7 @@ static __init int x86_instantiate_serdev(const struct x86_dev_info *dev_info, in
 	ACPI_COMPANION_SET(&serdev->dev, serdev_adev);
 	acpi_device_set_enumerated(serdev_adev);
 
-	ret = serdev_device_add(serdev);
+	ret = serdev_device_add(serdev, NULL);
 	if (ret) {
 		dev_err(&serdev->dev, "error %d adding serdev\n", ret);
 		serdev_device_put(serdev);
drivers/power/sequencing/pwrseq-pcie-m2.c +1 -1
index e42e85f47629..e0482f2ef39f 100644
@@ -309,7 +309,7 @@ static int pwrseq_pcie_m2_create_serdev_one(struct pwrseq_pcie_m2_ctx *ctx,
 	if (ret)
 		goto err_free_serdev;
 
-	ret = serdev_device_add(pci_dev->serdev);
+	ret = serdev_device_add(pci_dev->serdev, NULL);
 	if (ret) {
 		dev_err(dev, "Failed to add serdev for PCI device (%s): %d\n",
 			pci_name(pdev), ret);
drivers/tty/serdev/core.c +53 -9

123 lines not on this page · raw message

include/linux/device-id/serdev.h new file +19 -0
new file mode 100644
index 000000000000..96644953cece
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef LINUX_DEVICE_ID_SERDEV_H
+#define LINUX_DEVICE_ID_SERDEV_H
+
+#ifdef __KERNEL__
+typedef unsigned long kernel_ulong_t;
+#endif
+
+/* serdev */
+
+#define SERDEV_NAME_SIZE	40
+#define SERDEV_MODULE_PREFIX	"serial:"
+
+struct serdev_device_id {
+	char name[SERDEV_NAME_SIZE];
+	kernel_ulong_t driver_data;	/* Data private to the driver */
+};
+
+#endif /* ifndef LINUX_DEVICE_ID_SERDEV_H */
include/linux/serdev.h +11 -1
index b6c3d957ec15..96a441abc1ac 100644
@@ -7,7 +7,9 @@
 
 #include <linux/types.h>
 #include <linux/device.h>
+#include <linux/device-id/serdev.h>
 #include <linux/iopoll.h>
+#include <linux/string.h>
 #include <linux/uaccess.h>
 #include <linux/termios.h>
 #include <linux/delay.h>
@@ -35,6 +37,10 @@ struct serdev_device_ops {
  * struct serdev_device - Basic representation of an serdev device
  * @dev:	Driver model representation of the device.
  * @nr:		Device number on serdev bus.
+ * @modalias:	Name of the driver to use with this device, or the alias
+ *		for it. Empty for devices enumerated from firmware, which
+ *		are identified through their ACPI companion or their device
+ *		tree node instead. See serdev_device_add().
  * @ctrl:	serdev controller managing this device.
  * @ops:	Device operations.
  * @write_comp:	Completion used by serdev_device_write() internally
@@ -43,6 +49,7 @@ struct serdev_device_ops {
 struct serdev_device {
 	struct device dev;
 	int nr;
+	char modalias[SERDEV_NAME_SIZE];
 	struct serdev_controller *ctrl;
 	const struct serdev_device_ops *ops;
 	struct completion write_comp;
@@ -55,12 +62,15 @@ struct serdev_device {
  * struct serdev_device_driver - serdev slave device driver
  * @driver:	serdev device drivers should initialize name field of this
  *		structure.
+ * @id_table:	List of serdev devices supported by this driver, used to
+ *		match devices which are not enumerated from firmware.
  * @probe:	binds this driver to a serdev device.
  * @remove:	unbinds this driver from the serdev device.
  * @shutdown:	shut down this serdev device.
  */
 struct serdev_device_driver {
 	struct device_driver driver;
+	const struct serdev_device_id *id_table;
 	int	(*probe)(struct serdev_device *);
 	void	(*remove)(struct serdev_device *);
 	void	(*shutdown)(struct serdev_device *);
@@ -158,7 +168,7 @@ static inline void serdev_controller_put(struct serdev_controller *ctrl)
 }
 
 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
-int serdev_device_add(struct serdev_device *);
+int serdev_device_add(struct serdev_device *serdev, const char *modalias);
 void serdev_device_remove(struct serdev_device *);
 
 struct serdev_controller *serdev_controller_alloc(struct device *host,
Signature · 1 line
2.34.1
#2 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] serdev: unupdated serdev_device_add() callers break build in bisect - [Medium] serdev: missing…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] serdev: unupdated serdev_device_add() callers break build in bisect
- [Medium] serdev: missing modpost wiring for struct serdev_device_id
--

commit 5b1ae2343b2c9133fe8d3921a65c4b7fe749f748
Author: ChunAn Wu <[email protected]>

serdev: handle devices without an ACPI or device tree node

This commit adds modalias and id_table support to the serdev bus so that serdev clients without an ACPI companion or device tree node can emit modalias uevents and match against struct serdev_device_id tables. It also introduces include/linux/device-id/serdev.h and updates serdev_device_add() to accept a modalias string.

Quoted 4 lines
drivers/tty/serdev/core.c +0 -0
index 7500efcdfc218..e0517d5a48da2 100644

[ ... ]

Quoted 1 line
drivers/tty/serdev/core.c +0 -0
@@ -86,28 +101,57 @@ static const struct device_type serdev_ctrl_type = {

[ ... ]

Quoted 7 lines

/**
* serdev_device_add() - add a device previously constructed via serdev_device_alloc()
* @serdev: serdev_device to be added
+ * @modalias: Driver name or alias, used to match devices which have neither
+ * an ACPI companion nor a device tree node. NULL for devices
+ * enumerated from firmware.
*/

drivers/tty/serdev/core.c static const struct device_type serdev_ctrl_type = { · line 153

Quoted 9 lines
drivers/tty/serdev/core.c +3 -1
-int serdev_device_add(struct serdev_device *serdev)
+int serdev_device_add(struct serdev_device *serdev, const char *modalias)
 {
 	struct serdev_controller *ctrl = serdev->ctrl;
 	struct device *parent = serdev->dev.parent;
 	int err;
 
+	if (modalias)
+		strscpy(serdev->modalias, modalias);

[Severity: Medium]
Does changing the signature of serdev_device_add() here without updating all in-tree callers break compilation at this commit during a git bisect when CONFIG_DELL_UART_BACKLIGHT or CONFIG_YT2_1380 is enabled?

While this commit updates the callers in drivers/tty/serdev/core.c, drivers/platform/x86/x86-android-tablets/core.c, and drivers/power/sequencing/pwrseq-pcie-m2.c to pass NULL, two callers are left passing a single argument at this point in the series:

drivers/platform/x86/dell/dell-uart-backlight.c:
dell_uart_bl_pdev_probe()

	ret = serdev_device_add(serdev);

drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c:
yt2_1380_fc_pdev_probe()

	ret = serdev_device_add(serdev);

Although the subsequent commits in the series ("platform/x86:
dell-uart-backlight: Bind through normal serdev matching" and "platform/x86:
lenovo-yoga-tab2-pro-1380-fastcharger: Bind through normal serdev matching") update those two drivers to pass KBUILD_MODNAME, should both callers be updated to pass NULL in this commit so that every patch in the series builds cleanly?

[ ... ]

Quoted 15 lines
include/linux/device-id/serdev.h new file +9 -0
new file mode 100644
index 0000000000000..96644953ceced
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef LINUX_DEVICE_ID_SERDEV_H
+#define LINUX_DEVICE_ID_SERDEV_H
+
+#ifdef __KERNEL__
+typedef unsigned long kernel_ulong_t;
+#endif
+
+/* serdev */

include/linux/device-id/serdev.h line 19

Quoted 10 lines
include/linux/device-id/serdev.h new file +10 -0
+
+#define SERDEV_NAME_SIZE	40
+#define SERDEV_MODULE_PREFIX	"serial:"
+
+struct serdev_device_id {
+	char name[SERDEV_NAME_SIZE];
+	kernel_ulong_t driver_data;	/* Data private to the driver */
+};
+
+#endif /* ifndef LINUX_DEVICE_ID_SERDEV_H */

[Severity: Medium]
Should struct serdev_device_id also be wired into modpost so that MODULE_DEVICE_TABLE(serdev, ...) generates serial:* module aliases?

In drivers/tty/serdev/core.c, serdev_device_uevent() emits MODALIAS=serial:<modalias> using SERDEV_MODULE_PREFIX:

	return add_uevent_var(env, "MODALIAS=%s%s", SERDEV_MODULE_PREFIX,
			      serdev->modalias);

However, include/linux/mod_devicetable.h does not include device-id/serdev.h, scripts/mod/devicetable-offsets.c does not define DEVID(serdev_device_id) and DEVID_FIELD(serdev_device_id, name), and devtable[] in scripts/mod/file2alias.c has no "serdev" entry or handler. As a result, handle_moddevtable() silently ignores MODULE_DEVICE_TABLE(serdev, ...) declarations, preventing uevent-based module autoloading for modular serdev client drivers.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v5 1/3
#3 lore raw
In reply to ChunAn Wu
Quoted 226 lines · ChunAn Wu

Serdev clients instantiated by a driver have no ACPI companion and no device tree node. Their modalias read returns -ENODEV, their add uevent is suppressed, and they cannot be driver-matched, so creators force a bind with device_driver_attach().

Resolve both TODOs in the serdev core by modelling matching on the SPI
bus: let the creator pass a modalias to serdev_device_add() and give struct serdev_device_driver an id_table. ACPI and OF are still tried first; clients without their own modalias keep returning -ENODEV. The device name, derived from the controller, is unchanged.

Split struct serdev_device_id and its defines into a new include/linux/device-id/serdev.h, following the pattern already used by the SPI and I2C buses.

---
 MAINTAINERS                                   |  1 +
 .../platform/x86/x86-android-tablets/core.c   |  2 +-
 drivers/power/sequencing/pwrseq-pcie-m2.c     |  2 +-
 drivers/tty/serdev/core.c                     | 62 ++++++++++++++++---
 include/linux/device-id/serdev.h              | 19 ++++++
 include/linux/serdev.h                        | 12 +++-
 6 files changed, 86 insertions(+), 12 deletions(-)
 create mode 100644 include/linux/device-id/serdev.h
MAINTAINERS +1 -0
index 917f3f72e31b..06e67aa5d9b0 100644
@@ -24905,6 +24905,7 @@ L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/serial/serial.yaml
 F:	drivers/tty/serdev/
+F:	include/linux/device-id/serdev.h
 F:	include/linux/serdev.h
 F:	rust/helpers/serdev.c
 F:	rust/kernel/serdev.rs
drivers/platform/x86/x86-android-tablets/core.c +1 -1
index cfff7f5eac5d..853aa6ff5d50 100644
@@ -317,7 +317,7 @@ static __init int x86_instantiate_serdev(const struct x86_dev_info *dev_info, in
 	ACPI_COMPANION_SET(&serdev->dev, serdev_adev);
 	acpi_device_set_enumerated(serdev_adev);
 
-	ret = serdev_device_add(serdev);
+	ret = serdev_device_add(serdev, NULL);
 	if (ret) {
 		dev_err(&serdev->dev, "error %d adding serdev\n", ret);
 		serdev_device_put(serdev);
drivers/power/sequencing/pwrseq-pcie-m2.c +1 -1
index e42e85f47629..e0482f2ef39f 100644
@@ -309,7 +309,7 @@ static int pwrseq_pcie_m2_create_serdev_one(struct pwrseq_pcie_m2_ctx *ctx,
 	if (ret)
 		goto err_free_serdev;
 
-	ret = serdev_device_add(pci_dev->serdev);
+	ret = serdev_device_add(pci_dev->serdev, NULL);
 	if (ret) {
 		dev_err(dev, "Failed to add serdev for PCI device (%s): %d\n",
 			pci_name(pdev), ret);
drivers/tty/serdev/core.c +53 -9
index 7500efcdfc21..e0517d5a48da 100644
@@ -29,13 +29,21 @@ static DEFINE_IDA(ctrl_ida);
 static ssize_t modalias_show(struct device *dev,
 			     struct device_attribute *attr, char *buf)
 {
+	const struct serdev_device *serdev = to_serdev_device(dev);
 	int len;
 
 	len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
 	if (len != -ENODEV)
 		return len;
 
-	return of_device_modalias(dev, buf, PAGE_SIZE);
+	len = of_device_modalias(dev, buf, PAGE_SIZE);
+	if (len != -ENODEV)
+		return len;
+
+	if (!serdev->modalias[0])
+		return -ENODEV;
+
+	return sysfs_emit(buf, "%s%s\n", SERDEV_MODULE_PREFIX, serdev->modalias);
 }
 static DEVICE_ATTR_RO(modalias);
 
@@ -47,15 +55,22 @@ ATTRIBUTE_GROUPS(serdev_device);
 
 static int serdev_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
 {
+	const struct serdev_device *serdev = to_serdev_device(dev);
 	int rc;
 
-	/* TODO: platform modalias */
-
 	rc = acpi_device_uevent_modalias(dev, env);
 	if (rc != -ENODEV)
 		return rc;
 
-	return of_device_uevent_modalias(dev, env);
+	rc = of_device_uevent_modalias(dev, env);
+	if (rc != -ENODEV)
+		return rc;
+
+	if (!serdev->modalias[0])
+		return -ENODEV;
+
+	return add_uevent_var(env, "MODALIAS=%s%s", SERDEV_MODULE_PREFIX,
+			      serdev->modalias);
 }
 
 static void serdev_device_release(struct device *dev)
@@ -86,28 +101,57 @@ static const struct device_type serdev_ctrl_type = {
 	.release	= serdev_ctrl_release,
 };
 
+static const struct serdev_device_id *serdev_match_id(const struct serdev_device_id *id,
+						      const char *modalias)
+{
+	if (!id || !modalias[0])
+		return NULL;
+
+	while (id->name[0]) {
+		if (!strcmp(modalias, id->name))
+			return id;
+		id++;
+	}
+
+	return NULL;
+}
+
 static int serdev_device_match(struct device *dev, const struct device_driver *drv)
 {
+	const struct serdev_device *serdev;
+	const struct serdev_device_driver *sdrv;
+
 	if (!is_serdev_device(dev))
 		return 0;
 
-	/* TODO: platform matching */
+	serdev = to_serdev_device(dev);
+	sdrv = to_serdev_device_driver(drv);
+
 	if (acpi_driver_match_device(dev, drv))
 		return 1;
 
-	return of_driver_match_device(dev, drv);
+	if (of_driver_match_device(dev, drv))
+		return 1;
+
+	return !!serdev_match_id(sdrv->id_table, serdev->modalias);
 }
 
 /**
  * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
  * @serdev:	serdev_device to be added
+ * @modalias:	Driver name or alias, used to match devices which have neither
+ *		an ACPI companion nor a device tree node. NULL for devices
+ *		enumerated from firmware.
  */
-int serdev_device_add(struct serdev_device *serdev)
+int serdev_device_add(struct serdev_device *serdev, const char *modalias)
 {
 	struct serdev_controller *ctrl = serdev->ctrl;
 	struct device *parent = serdev->dev.parent;
 	int err;
 
+	if (modalias)
+		strscpy(serdev->modalias, modalias);
+
 	dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
 
 	/* Only a single slave device is currently supported. */
@@ -553,7 +597,7 @@ static int of_serdev_register_devices(struct serdev_controller *ctrl)
 
 		device_set_node(&serdev->dev, of_fwnode_handle(node));
 
-		err = serdev_device_add(serdev);
+		err = serdev_device_add(serdev, NULL);
 		if (err) {
 			dev_err(&serdev->dev,
 				"failure adding device. status %pe\n",
@@ -707,7 +751,7 @@ static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
 	ACPI_COMPANION_SET(&serdev->dev, adev);
 	acpi_device_set_enumerated(adev);
 
-	err = serdev_device_add(serdev);
+	err = serdev_device_add(serdev, NULL);
 	if (err) {
 		dev_err(&serdev->dev,
 			"failure adding ACPI serdev device. status %pe\n",
include/linux/device-id/serdev.h new file +19 -0
new file mode 100644
index 000000000000..96644953cece
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef LINUX_DEVICE_ID_SERDEV_H
+#define LINUX_DEVICE_ID_SERDEV_H
+
+#ifdef __KERNEL__
+typedef unsigned long kernel_ulong_t;
+#endif
+
+/* serdev */
+
+#define SERDEV_NAME_SIZE	40
+#define SERDEV_MODULE_PREFIX	"serial:"
+
+struct serdev_device_id {
+	char name[SERDEV_NAME_SIZE];
+	kernel_ulong_t driver_data;	/* Data private to the driver */
+};
+
+#endif /* ifndef LINUX_DEVICE_ID_SERDEV_H */
include/linux/serdev.h +0 -0
index b6c3d957ec15..96a441abc1ac 100644

include/linux/serdev.h line 12

Quoted 6 lines
include/linux/serdev.h +2 -0
@@ -7,7 +7,9 @@
 
 #include <linux/types.h>
 #include <linux/device.h>
+#include <linux/device-id/serdev.h>
 #include <linux/iopoll.h>
+#include <linux/string.h>

Unused include.

Quoted 39 lines
include/linux/serdev.h +8 -0
 #include <linux/uaccess.h>
 #include <linux/termios.h>
 #include <linux/delay.h>
@@ -35,6 +37,10 @@ struct serdev_device_ops {
  * struct serdev_device - Basic representation of an serdev device
  * @dev:	Driver model representation of the device.
  * @nr:		Device number on serdev bus.
+ * @modalias:	Name of the driver to use with this device, or the alias
+ *		for it. Empty for devices enumerated from firmware, which
+ *		are identified through their ACPI companion or their device
+ *		tree node instead. See serdev_device_add().
  * @ctrl:	serdev controller managing this device.
  * @ops:	Device operations.
  * @write_comp:	Completion used by serdev_device_write() internally
@@ -43,6 +49,7 @@ struct serdev_device_ops {
 struct serdev_device {
 	struct device dev;
 	int nr;
+	char modalias[SERDEV_NAME_SIZE];
 	struct serdev_controller *ctrl;
 	const struct serdev_device_ops *ops;
 	struct completion write_comp;
@@ -55,12 +62,15 @@ struct serdev_device {
  * struct serdev_device_driver - serdev slave device driver
  * @driver:	serdev device drivers should initialize name field of this
  *		structure.
+ * @id_table:	List of serdev devices supported by this driver, used to
+ *		match devices which are not enumerated from firmware.
  * @probe:	binds this driver to a serdev device.
  * @remove:	unbinds this driver from the serdev device.
  * @shutdown:	shut down this serdev device.
  */
 struct serdev_device_driver {
 	struct device_driver driver;
+	const struct serdev_device_id *id_table;
 	int	(*probe)(struct serdev_device *);
 	void	(*remove)(struct serdev_device *);
 	void	(*shutdown)(struct serdev_device *);

include/linux/serdev.h static inline void serdev_controller_put(struct serdev_controller *ctrl) · line 174

Quoted 8 lines
include/linux/serdev.h +1 -1
@@ -158,7 +168,7 @@ static inline void serdev_controller_put(struct serdev_controller *ctrl)
 }
 
 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
-int serdev_device_add(struct serdev_device *);
+int serdev_device_add(struct serdev_device *serdev, const char *modalias);
 void serdev_device_remove(struct serdev_device *);
 
 struct serdev_controller *serdev_controller_alloc(struct device *host,

You will need to address the sashiko report (both are true positives).

In the scripts/mod/file2alias.c file, you can copy the spi entry. scripts/mod/devicetable-offsets.{h,c,s} and include/linux/mod_devicetable.h are also relevant for this. (Didn't know myself those files existed, so thx Sashiko)

Also add serdev_device_id to scripts/coccinelle/misc/of_table.cocci .

With that fixed,

Thanks
- Markus Probst

author
#4 lore raw

Hi Markus
Working on it,
Thank you~

BR
An

Quoted 301 lines · Markus Probst
Quoted 226 lines · ChunAn Wu

Serdev clients instantiated by a driver have no ACPI companion and no device tree node. Their modalias read returns -ENODEV, their add uevent is suppressed, and they cannot be driver-matched, so creators force a bind with device_driver_attach().

Resolve both TODOs in the serdev core by modelling matching on the SPI
bus: let the creator pass a modalias to serdev_device_add() and give struct serdev_device_driver an id_table. ACPI and OF are still tried first; clients without their own modalias keep returning -ENODEV. The device name, derived from the controller, is unchanged.

Split struct serdev_device_id and its defines into a new include/linux/device-id/serdev.h, following the pattern already used by the SPI and I2C buses.

---
 MAINTAINERS                                   |  1 +
 .../platform/x86/x86-android-tablets/core.c   |  2 +-
 drivers/power/sequencing/pwrseq-pcie-m2.c     |  2 +-
 drivers/tty/serdev/core.c                     | 62 ++++++++++++++++---
 include/linux/device-id/serdev.h              | 19 ++++++
 include/linux/serdev.h                        | 12 +++-
 6 files changed, 86 insertions(+), 12 deletions(-)
 create mode 100644 include/linux/device-id/serdev.h
MAINTAINERS +1 -0
index 917f3f72e31b..06e67aa5d9b0 100644
@@ -24905,6 +24905,7 @@ L:    [email protected]
 S:   Maintained
 F:   Documentation/devicetree/bindings/serial/serial.yaml
 F:   drivers/tty/serdev/
+F:   include/linux/device-id/serdev.h
 F:   include/linux/serdev.h
 F:   rust/helpers/serdev.c
 F:   rust/kernel/serdev.rs
drivers/platform/x86/x86-android-tablets/core.c +1 -1
index cfff7f5eac5d..853aa6ff5d50 100644
@@ -317,7 +317,7 @@ static __init int x86_instantiate_serdev(const struct x86_dev_info *dev_info, in
      ACPI_COMPANION_SET(&serdev->dev, serdev_adev);
      acpi_device_set_enumerated(serdev_adev);
 
-     ret = serdev_device_add(serdev);
+     ret = serdev_device_add(serdev, NULL);
      if (ret) {
              dev_err(&serdev->dev, "error %d adding serdev\n", ret);
              serdev_device_put(serdev);
drivers/power/sequencing/pwrseq-pcie-m2.c +1 -1
index e42e85f47629..e0482f2ef39f 100644
@@ -309,7 +309,7 @@ static int pwrseq_pcie_m2_create_serdev_one(struct pwrseq_pcie_m2_ctx *ctx,
      if (ret)
              goto err_free_serdev;
 
-     ret = serdev_device_add(pci_dev->serdev);
+     ret = serdev_device_add(pci_dev->serdev, NULL);
      if (ret) {
              dev_err(dev, "Failed to add serdev for PCI device (%s): %d\n",
                      pci_name(pdev), ret);
drivers/tty/serdev/core.c +53 -9
index 7500efcdfc21..e0517d5a48da 100644
@@ -29,13 +29,21 @@ static DEFINE_IDA(ctrl_ida);
 static ssize_t modalias_show(struct device *dev,
                           struct device_attribute *attr, char *buf)
 {
+     const struct serdev_device *serdev = to_serdev_device(dev);
      int len;
 
      len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
      if (len != -ENODEV)
              return len;
 
-     return of_device_modalias(dev, buf, PAGE_SIZE);
+     len = of_device_modalias(dev, buf, PAGE_SIZE);
+     if (len != -ENODEV)
+             return len;
+
+     if (!serdev->modalias[0])
+             return -ENODEV;
+
+     return sysfs_emit(buf, "%s%s\n", SERDEV_MODULE_PREFIX, serdev->modalias);
 }
 static DEVICE_ATTR_RO(modalias);
 
@@ -47,15 +55,22 @@ ATTRIBUTE_GROUPS(serdev_device);
 
 static int serdev_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
 {
+     const struct serdev_device *serdev = to_serdev_device(dev);
      int rc;
 
-     /* TODO: platform modalias */
-
      rc = acpi_device_uevent_modalias(dev, env);
      if (rc != -ENODEV)
              return rc;
 
-     return of_device_uevent_modalias(dev, env);
+     rc = of_device_uevent_modalias(dev, env);
+     if (rc != -ENODEV)
+             return rc;
+
+     if (!serdev->modalias[0])
+             return -ENODEV;
+
+     return add_uevent_var(env, "MODALIAS=%s%s", SERDEV_MODULE_PREFIX,
+                           serdev->modalias);
 }
 
 static void serdev_device_release(struct device *dev)
@@ -86,28 +101,57 @@ static const struct device_type serdev_ctrl_type = {
      .release        = serdev_ctrl_release,
 };
 
+static const struct serdev_device_id *serdev_match_id(const struct serdev_device_id *id,
+                                                   const char *modalias)
+{
+     if (!id || !modalias[0])
+             return NULL;
+
+     while (id->name[0]) {
+             if (!strcmp(modalias, id->name))
+                     return id;
+             id++;
+     }
+
+     return NULL;
+}
+
 static int serdev_device_match(struct device *dev, const struct device_driver *drv)
 {
+     const struct serdev_device *serdev;
+     const struct serdev_device_driver *sdrv;
+
      if (!is_serdev_device(dev))
              return 0;
 
-     /* TODO: platform matching */
+     serdev = to_serdev_device(dev);
+     sdrv = to_serdev_device_driver(drv);
+
      if (acpi_driver_match_device(dev, drv))
              return 1;
 
-     return of_driver_match_device(dev, drv);
+     if (of_driver_match_device(dev, drv))
+             return 1;
+
+     return !!serdev_match_id(sdrv->id_table, serdev->modalias);
 }
 
 /**
  * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
  * @serdev:  serdev_device to be added
+ * @modalias:        Driver name or alias, used to match devices which have neither
+ *           an ACPI companion nor a device tree node. NULL for devices
+ *           enumerated from firmware.
  */
-int serdev_device_add(struct serdev_device *serdev)
+int serdev_device_add(struct serdev_device *serdev, const char *modalias)
 {
      struct serdev_controller *ctrl = serdev->ctrl;
      struct device *parent = serdev->dev.parent;
      int err;
 
+     if (modalias)
+             strscpy(serdev->modalias, modalias);
+
      dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
 
      /* Only a single slave device is currently supported. */
@@ -553,7 +597,7 @@ static int of_serdev_register_devices(struct serdev_controller *ctrl)
 
              device_set_node(&serdev->dev, of_fwnode_handle(node));
 
-             err = serdev_device_add(serdev);
+             err = serdev_device_add(serdev, NULL);
              if (err) {
                      dev_err(&serdev->dev,
                              "failure adding device. status %pe\n",
@@ -707,7 +751,7 @@ static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
      ACPI_COMPANION_SET(&serdev->dev, adev);
      acpi_device_set_enumerated(adev);
 
-     err = serdev_device_add(serdev);
+     err = serdev_device_add(serdev, NULL);
      if (err) {
              dev_err(&serdev->dev,
                      "failure adding ACPI serdev device. status %pe\n",
include/linux/device-id/serdev.h new file +19 -0
new file mode 100644
index 000000000000..96644953cece
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef LINUX_DEVICE_ID_SERDEV_H
+#define LINUX_DEVICE_ID_SERDEV_H
+
+#ifdef __KERNEL__
+typedef unsigned long kernel_ulong_t;
+#endif
+
+/* serdev */
+
+#define SERDEV_NAME_SIZE     40
+#define SERDEV_MODULE_PREFIX "serial:"
+
+struct serdev_device_id {
+     char name[SERDEV_NAME_SIZE];
+     kernel_ulong_t driver_data;     /* Data private to the driver */
+};
+
+#endif /* ifndef LINUX_DEVICE_ID_SERDEV_H */
include/linux/serdev.h +0 -0
index b6c3d957ec15..96a441abc1ac 100644

include/linux/serdev.h line 12

Quoted 6 lines
include/linux/serdev.h +2 -0
@@ -7,7 +7,9 @@
 
 #include <linux/types.h>
 #include <linux/device.h>
+#include <linux/device-id/serdev.h>
 #include <linux/iopoll.h>
+#include <linux/string.h>

Unused include.

Quoted 39 lines
include/linux/serdev.h +8 -0
 #include <linux/uaccess.h>
 #include <linux/termios.h>
 #include <linux/delay.h>
@@ -35,6 +37,10 @@ struct serdev_device_ops {
  * struct serdev_device - Basic representation of an serdev device
  * @dev:     Driver model representation of the device.
  * @nr:              Device number on serdev bus.
+ * @modalias:        Name of the driver to use with this device, or the alias
+ *           for it. Empty for devices enumerated from firmware, which
+ *           are identified through their ACPI companion or their device
+ *           tree node instead. See serdev_device_add().
  * @ctrl:    serdev controller managing this device.
  * @ops:     Device operations.
  * @write_comp:      Completion used by serdev_device_write() internally
@@ -43,6 +49,7 @@ struct serdev_device_ops {
 struct serdev_device {
      struct device dev;
      int nr;
+     char modalias[SERDEV_NAME_SIZE];
      struct serdev_controller *ctrl;
      const struct serdev_device_ops *ops;
      struct completion write_comp;
@@ -55,12 +62,15 @@ struct serdev_device {
  * struct serdev_device_driver - serdev slave device driver
  * @driver:  serdev device drivers should initialize name field of this
  *           structure.
+ * @id_table:        List of serdev devices supported by this driver, used to
+ *           match devices which are not enumerated from firmware.
  * @probe:   binds this driver to a serdev device.
  * @remove:  unbinds this driver from the serdev device.
  * @shutdown:        shut down this serdev device.
  */
 struct serdev_device_driver {
      struct device_driver driver;
+     const struct serdev_device_id *id_table;
      int     (*probe)(struct serdev_device *);
      void    (*remove)(struct serdev_device *);
      void    (*shutdown)(struct serdev_device *);

include/linux/serdev.h static inline void serdev_controller_put(struct serdev_controller *ctrl) · line 174

Quoted 8 lines
include/linux/serdev.h +1 -1
@@ -158,7 +168,7 @@ static inline void serdev_controller_put(struct serdev_controller *ctrl)
 }
 
 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
-int serdev_device_add(struct serdev_device *);
+int serdev_device_add(struct serdev_device *serdev, const char *modalias);
 void serdev_device_remove(struct serdev_device *);
 
 struct serdev_controller *serdev_controller_alloc(struct device *host,

You will need to address the sashiko report (both are true positives).

In the scripts/mod/file2alias.c file, you can copy the spi entry. scripts/mod/devicetable-offsets.{h,c,s} and include/linux/mod_devicetable.h are also relevant for this. (Didn't know myself those files existed, so thx Sashiko)

Also add serdev_device_id to scripts/coccinelle/misc/of_table.cocci .

With that fixed,

Thanks
- Markus Probst

author
#5 lore raw

PATCH v5 2/3 platform/x86: dell-uart-backlight: Bind through normal serdev matching

In reply to ChunAn Wu

The driver creates a serdev client without firmware metadata and force-attaches its driver to it.

Pass the modalias to serdev_device_add() and give the serdev driver a matching id_table, so that the serdev core binds the two through normal matching, then drop the manual attachment.

The client now also reports a "serial:dell_uart_backlight" modalias to userspace instead of failing with -ENODEV. A failing serdev probe no longer propagates out of the platform probe; as for any other bus, the client is simply left unbound.

---
 .../platform/x86/dell/dell-uart-backlight.c    | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/platform/x86/dell/dell-uart-backlight.c +7 -11
index f323a667dc2d..9b8ef91bb0a2 100644
@@ -325,11 +325,17 @@ static int dell_uart_bl_serdev_probe(struct serdev_device *serdev)
 	return PTR_ERR_OR_ZERO(dell_bl->bl);
 }
 
+static const struct serdev_device_id dell_uart_bl_serdev_id[] = {
+	{ KBUILD_MODNAME },
+	{ }
+};
+
 static struct serdev_device_driver dell_uart_bl_serdev_driver = {
 	.probe = dell_uart_bl_serdev_probe,
 	.driver = {
 		.name = KBUILD_MODNAME,
 	},
+	.id_table = dell_uart_bl_serdev_id,
 };
 
 static int dell_uart_bl_pdev_probe(struct platform_device *pdev)
@@ -354,7 +360,7 @@ static int dell_uart_bl_pdev_probe(struct platform_device *pdev)
 	if (!serdev)
 		return -ENOMEM;
 
-	ret = serdev_device_add(serdev);
+	ret = serdev_device_add(serdev, KBUILD_MODNAME);
 	if (ret) {
 		dev_err(&pdev->dev, "error %d adding serdev\n", ret);
 		serdev_device_put(serdev);
@@ -365,20 +371,10 @@ static int dell_uart_bl_pdev_probe(struct platform_device *pdev)
 	if (ret)
 		goto err_remove_serdev;
 
-	/*
-	 * serdev device <-> driver matching relies on OF or ACPI matches and
-	 * neither is available here, manually bind the driver.
-	 */
-	ret = device_driver_attach(&dell_uart_bl_serdev_driver.driver, &serdev->dev);
-	if (ret)
-		goto err_unregister_serdev_driver;
-
 	/* So that dell_uart_bl_pdev_remove() can remove the serdev */
 	platform_set_drvdata(pdev, serdev);
 	return 0;
 
-err_unregister_serdev_driver:
-	serdev_device_driver_unregister(&dell_uart_bl_serdev_driver);
 err_remove_serdev:
 	serdev_device_remove(serdev);
 	return ret;
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v5 2/3] platform/x86: dell-uart-backlight: Bind through normal serdev matching

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

The driver creates a serdev client without firmware metadata and force-attaches its driver to it.

Pass the modalias to serdev_device_add() and give the serdev driver a matching id_table, so that the serdev core binds the two through normal matching, then drop the manual attachment.

Quoted 6 lines

The client now also reports a "serial:dell_uart_backlight" modalias to userspace instead of failing with -ENODEV. A failing serdev probe no longer propagates out of the platform probe; as for any other bus, the client is simply left unbound.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Reviewed-by on v5 2/3
#7 lore raw

Re: [PATCH v5 2/3] platform/x86: dell-uart-backlight: Bind through normal serdev matching

In reply to ChunAn Wu
Quoted 59 lines · ChunAn Wu

The driver creates a serdev client without firmware metadata and force-attaches its driver to it.

Pass the modalias to serdev_device_add() and give the serdev driver a matching id_table, so that the serdev core binds the two through normal matching, then drop the manual attachment.

The client now also reports a "serial:dell_uart_backlight" modalias to userspace instead of failing with -ENODEV. A failing serdev probe no longer propagates out of the platform probe; as for any other bus, the client is simply left unbound.

---
 .../platform/x86/dell/dell-uart-backlight.c    | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/platform/x86/dell/dell-uart-backlight.c +7 -8
index f323a667dc2d..9b8ef91bb0a2 100644
@@ -325,11 +325,17 @@ static int dell_uart_bl_serdev_probe(struct serdev_device *serdev)
 	return PTR_ERR_OR_ZERO(dell_bl->bl);
 }
 
+static const struct serdev_device_id dell_uart_bl_serdev_id[] = {
+	{ KBUILD_MODNAME },
+	{ }
+};
+
 static struct serdev_device_driver dell_uart_bl_serdev_driver = {
 	.probe = dell_uart_bl_serdev_probe,
 	.driver = {
 		.name = KBUILD_MODNAME,
 	},
+	.id_table = dell_uart_bl_serdev_id,
 };
 
 static int dell_uart_bl_pdev_probe(struct platform_device *pdev)
@@ -354,7 +360,7 @@ static int dell_uart_bl_pdev_probe(struct platform_device *pdev)
 	if (!serdev)
 		return -ENOMEM;
 
-	ret = serdev_device_add(serdev);
+	ret = serdev_device_add(serdev, KBUILD_MODNAME);
 	if (ret) {
 		dev_err(&pdev->dev, "error %d adding serdev\n", ret);
 		serdev_device_put(serdev);
@@ -365,20 +371,10 @@ static int dell_uart_bl_pdev_probe(struct platform_device *pdev)
 	if (ret)
 		goto err_remove_serdev;
 
-	/*
-	 * serdev device <-> driver matching relies on OF or ACPI matches and
-	 * neither is available here, manually bind the driver.
-	 */
-	ret = device_driver_attach(&dell_uart_bl_serdev_driver.driver, &serdev->dev);
-	if (ret)
-		goto err_unregister_serdev_driver;

drivers/platform/x86/dell/dell-uart-backlight.c static int dell_uart_bl_pdev_probe(struct platform_device *pdev) · line 380

Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c +0 -3
-
 	/* So that dell_uart_bl_pdev_remove() can remove the serdev */
 	platform_set_drvdata(pdev, serdev);
 	return 0;
 
-err_unregister_serdev_driver:
-	serdev_device_driver_unregister(&dell_uart_bl_serdev_driver);
 err_remove_serdev:
 	serdev_device_remove(serdev);
 	return ret;

Thanks
- Markus Probst

author
#8 lore raw

PATCH v5 3/3 platform/x86: lenovo-yoga-tab2-pro-1380-fastcharger: Bind through normal serdev matching

In reply to ChunAn Wu

The software-node-backed serdev client cannot be matched through ACPI or device tree and is force-attached to its driver.

Pass the modalias to serdev_device_add() and give the serdev driver a matching id_table, so that the serdev core binds the two through normal matching. Deferred probing is then handled by the driver core, so the -EAGAIN translation is no longer needed.

A failing serdev probe no longer propagates out of the platform probe; as for any other bus, the client is simply left unbound.

---
 .../lenovo/yoga-tab2-pro-1380-fastcharger.c   | 25 +++++++------------
 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c +9 -16
index 8551ab4d2c7d..d0bd9d4e3137 100644
@@ -219,11 +219,17 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
 	return 0;
 }
 
+static const struct serdev_device_id yt2_1380_fc_serdev_id[] = {
+	{ KBUILD_MODNAME },
+	{ }
+};
+
 static struct serdev_device_driver yt2_1380_fc_serdev_driver = {
 	.probe = yt2_1380_fc_serdev_probe,
 	.driver = {
 		.name = KBUILD_MODNAME,
 	},
+	.id_table = yt2_1380_fc_serdev_id,
 };
 
 static const struct pinctrl_map yt2_1380_fc_pinctrl_map[] = {
@@ -260,25 +266,12 @@ static int yt2_1380_fc_pdev_probe(struct platform_device *pdev)
 	/* The fwnode is a managed node, so it will be auto-put on serdev_device_put() */
 	fwnode_handle_get(dev_fwnode(&serdev->dev));
 
-	ret = serdev_device_add(serdev);
+	ret = serdev_device_add(serdev, KBUILD_MODNAME);
 	if (ret) {
 		serdev_device_put(serdev);
 		return dev_err_probe(&pdev->dev, ret, "adding serdev\n");
 	}
 
-	/*
-	 * serdev device <-> driver matching relies on OF or ACPI matches and
-	 * neither is available here, manually bind the driver.
-	 */
-	ret = device_driver_attach(&yt2_1380_fc_serdev_driver.driver, &serdev->dev);
-	if (ret) {
-		/* device_driver_attach() maps EPROBE_DEFER to EAGAIN, map it back */
-		serdev_device_remove(serdev);
-		return dev_err_probe(&pdev->dev,
-				     (ret == -EAGAIN) ? -EPROBE_DEFER : ret,
-				     "attaching serdev driver\n");
-	}
-
 	/* So that yt2_1380_fc_pdev_remove() can remove the serdev */
 	platform_set_drvdata(pdev, serdev);
 	return 0;
@@ -305,8 +298,8 @@ static int __init yt2_1380_fc_module_init(void)
 	int ret;
 
 	/*
-	 * serdev driver MUST be registered first because pdev driver calls
-	 * device_driver_attach() on the serdev, serdev-driver pair.
+	 * serdev driver MUST be registered first so that the serdev device
+	 * created by the pdev probe binds to it via the id_table match.
 	 */
 	ret = serdev_device_driver_register(&yt2_1380_fc_serdev_driver);
 	if (ret)
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v5 3/3] platform/x86: lenovo-yoga-tab2-pro-1380-fastcharger: Bind through normal serdev matching

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

The software-node-backed serdev client cannot be matched through ACPI or device tree and is force-attached to its driver.

Pass the modalias to serdev_device_add() and give the serdev driver a matching id_table, so that the serdev core binds the two through normal matching. Deferred probing is then handled by the driver core, so the -EAGAIN translation is no longer needed.

Quoted 4 lines

A failing serdev probe no longer propagates out of the platform probe; as for any other bus, the client is simply left unbound.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Reviewed-by on v5 3/3
#10 lore raw

Re: [PATCH v5 3/3] platform/x86: lenovo-yoga-tab2-pro-1380-fastcharger: Bind through normal serdev matching

In reply to ChunAn Wu
Quoted 66 lines · ChunAn Wu

The software-node-backed serdev client cannot be matched through ACPI or device tree and is force-attached to its driver.

Pass the modalias to serdev_device_add() and give the serdev driver a matching id_table, so that the serdev core binds the two through normal matching. Deferred probing is then handled by the driver core, so the -EAGAIN translation is no longer needed.

A failing serdev probe no longer propagates out of the platform probe; as for any other bus, the client is simply left unbound.

---
 .../lenovo/yoga-tab2-pro-1380-fastcharger.c   | 25 +++++++------------
 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c +7 -14
index 8551ab4d2c7d..d0bd9d4e3137 100644
@@ -219,11 +219,17 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
 	return 0;
 }
 
+static const struct serdev_device_id yt2_1380_fc_serdev_id[] = {
+	{ KBUILD_MODNAME },
+	{ }
+};
+
 static struct serdev_device_driver yt2_1380_fc_serdev_driver = {
 	.probe = yt2_1380_fc_serdev_probe,
 	.driver = {
 		.name = KBUILD_MODNAME,
 	},
+	.id_table = yt2_1380_fc_serdev_id,
 };
 
 static const struct pinctrl_map yt2_1380_fc_pinctrl_map[] = {
@@ -260,25 +266,12 @@ static int yt2_1380_fc_pdev_probe(struct platform_device *pdev)
 	/* The fwnode is a managed node, so it will be auto-put on serdev_device_put() */
 	fwnode_handle_get(dev_fwnode(&serdev->dev));
 
-	ret = serdev_device_add(serdev);
+	ret = serdev_device_add(serdev, KBUILD_MODNAME);
 	if (ret) {
 		serdev_device_put(serdev);
 		return dev_err_probe(&pdev->dev, ret, "adding serdev\n");
 	}
 
-	/*
-	 * serdev device <-> driver matching relies on OF or ACPI matches and
-	 * neither is available here, manually bind the driver.
-	 */
-	ret = device_driver_attach(&yt2_1380_fc_serdev_driver.driver, &serdev->dev);
-	if (ret) {
-		/* device_driver_attach() maps EPROBE_DEFER to EAGAIN, map it back */
-		serdev_device_remove(serdev);
-		return dev_err_probe(&pdev->dev,
-				     (ret == -EAGAIN) ? -EPROBE_DEFER : ret,
-				     "attaching serdev driver\n");
-	}
-
 	/* So that yt2_1380_fc_pdev_remove() can remove the serdev */
 	platform_set_drvdata(pdev, serdev);
 	return 0;

drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c static int __init yt2_1380_fc_module_init(void) · line 303

Quoted 8 lines
drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c +2 -2
@@ -305,8 +298,8 @@ static int __init yt2_1380_fc_module_init(void)
 	int ret;
 
 	/*
-	 * serdev driver MUST be registered first because pdev driver calls
-	 * device_driver_attach() on the serdev, serdev-driver pair.
+	 * serdev driver MUST be registered first so that the serdev device
+	 * created by the pdev probe binds to it via the id_table match.
 	 */

NIT: I think the order doesn't matter anymore, since it will bind regardless whether driver or device is registered/added first. I think you can remove the comment.

Thanks
- Markus Probst

Quoted 2 lines
drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c +0 -0
 	ret = serdev_device_driver_register(&yt2_1380_fc_serdev_driver);
 	if (ret)