[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250512012748.79749-4-damien.riegel@silabs.com>
Date: Sun, 11 May 2025 21:27:36 -0400
From: Damien Riégel <damien.riegel@...abs.com>
To: Andrew Lunn <andrew+netdev@...n.ch>,
"David S . Miller" <davem@...emloft.net>,
Eric Dumazet <edumazet@...gle.com>, Jakub Kicinski <kuba@...nel.org>,
Paolo Abeni <pabeni@...hat.com>, Rob Herring <robh@...nel.org>,
Krzysztof Kozlowski <krzk+dt@...nel.org>,
Conor Dooley <conor+dt@...nel.org>,
Silicon Labs Kernel Team <linux-devel@...abs.com>,
netdev@...r.kernel.org, devicetree@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [RFC net-next 03/15] net: cpc: introduce CPC driver and bus
Endpoints by itself are useless if there are no drivers to use them.
This commit adds the final bit of infrastructure for CPC module: a new
bus type and its associated driver.
As a very basic matching mechanism, the bus will match an endpoint with
its driver if driver's name (driver.name attribute) matches endpoint's
name.
Signed-off-by: Damien Riégel <damien.riegel@...abs.com>
---
drivers/net/cpc/cpc.h | 39 +++++++++++++++++++++++
drivers/net/cpc/endpoint.c | 1 +
drivers/net/cpc/main.c | 65 +++++++++++++++++++++++++++++++++++++-
3 files changed, 104 insertions(+), 1 deletion(-)
diff --git a/drivers/net/cpc/cpc.h b/drivers/net/cpc/cpc.h
index 529319f4339..cbd1b3d6a03 100644
--- a/drivers/net/cpc/cpc.h
+++ b/drivers/net/cpc/cpc.h
@@ -15,6 +15,8 @@ struct cpc_driver;
struct cpc_interface;
struct cpc_endpoint;
+extern const struct bus_type cpc_bus;
+
/**
* struct cpc_endpoint - Representation of CPC endpointl
* @dev: Driver model representation of the device.
@@ -98,4 +100,41 @@ static inline void cpc_endpoint_set_drvdata(struct cpc_endpoint *ep, void *data)
dev_set_drvdata(&ep->dev, data);
}
+/*---------------------------------------------------------------------------*/
+
+/**
+ * struct cpc_driver - CPC endpoint driver.
+ * @driver: Internal driver for the device driver model.
+ * @probe: Binds this driver to the endpoint.
+ * @remove: Unbinds this driver from the endpoint.
+ *
+ * This represents a device driver that uses an endpoint to communicate with a remote application at
+ * the other side of the CPC interface. The way to communicate with the remote is abstracted by the
+ * interface, and drivers don't have to care if other endpoints are present or not.
+ */
+struct cpc_driver {
+ struct device_driver driver;
+
+ int (*probe)(struct cpc_endpoint *ep);
+ void (*remove)(struct cpc_endpoint *ep);
+};
+
+int __cpc_driver_register(struct cpc_driver *cpc_drv, struct module *owner);
+void cpc_driver_unregister(struct cpc_driver *cpc_drv);
+
+/* Convenience macro with THIS_MODULE */
+#define cpc_driver_register(driver) \
+ __cpc_driver_register(driver, THIS_MODULE)
+
+/**
+ * cpc_driver_from_drv - Upcast from a device driver.
+ * @drv: Reference to a device driver.
+ *
+ * @return: Reference to the cpc driver.
+ */
+static inline struct cpc_driver *cpc_driver_from_drv(const struct device_driver *drv)
+{
+ return container_of(drv, struct cpc_driver, driver);
+}
+
#endif
diff --git a/drivers/net/cpc/endpoint.c b/drivers/net/cpc/endpoint.c
index 5aef8d7e43c..98e49614320 100644
--- a/drivers/net/cpc/endpoint.c
+++ b/drivers/net/cpc/endpoint.c
@@ -48,6 +48,7 @@ struct cpc_endpoint *cpc_endpoint_alloc(struct cpc_interface *intf, u8 id)
ep->id = id;
ep->dev.parent = &intf->dev;
+ ep->dev.bus = &cpc_bus;
ep->dev.release = cpc_ep_release;
device_initialize(&ep->dev);
diff --git a/drivers/net/cpc/main.c b/drivers/net/cpc/main.c
index ba9ab1ccf63..dcbe6dcb651 100644
--- a/drivers/net/cpc/main.c
+++ b/drivers/net/cpc/main.c
@@ -3,16 +3,79 @@
* Copyright (c) 2025, Silicon Laboratories, Inc.
*/
+#include <linux/device/driver.h>
#include <linux/module.h>
+#include "cpc.h"
+
+static int cpc_bus_match(struct device *dev, const struct device_driver *driver)
+{
+ struct cpc_driver *cpc_drv = cpc_driver_from_drv(driver);
+ struct cpc_endpoint *cpc_ep = cpc_endpoint_from_dev(dev);
+
+ return strcmp(cpc_drv->driver.name, cpc_ep->name) == 0;
+}
+
+static int cpc_bus_probe(struct device *dev)
+{
+ struct cpc_driver *cpc_drv = cpc_driver_from_drv(dev->driver);
+ struct cpc_endpoint *ep = cpc_endpoint_from_dev(dev);
+
+ return cpc_drv->probe(ep);
+}
+
+static void cpc_bus_remove(struct device *dev)
+{
+ struct cpc_driver *cpc_drv = cpc_driver_from_drv(dev->driver);
+ struct cpc_endpoint *ep = cpc_endpoint_from_dev(dev);
+
+ cpc_drv->remove(ep);
+}
+
+const struct bus_type cpc_bus = {
+ .name = KBUILD_MODNAME,
+ .match = cpc_bus_match,
+ .probe = cpc_bus_probe,
+ .remove = cpc_bus_remove,
+};
+
+/**
+ * __cpc_driver_register() - Register driver to the cpc bus.
+ * @cpc_drv: Reference to the cpc driver.
+ * @owner: Reference to this module's owner.
+ *
+ * @return: 0 on success, otherwise a negative error code.
+ */
+int __cpc_driver_register(struct cpc_driver *cpc_drv, struct module *owner)
+{
+ cpc_drv->driver.bus = &cpc_bus;
+ cpc_drv->driver.owner = owner;
+
+ return driver_register(&cpc_drv->driver);
+}
+
+/**
+ * cpc_driver_unregister() - Unregister driver from the cpc bus.
+ * @cpc_drv: Reference to the cpc driver.
+ */
+void cpc_driver_unregister(struct cpc_driver *cpc_drv)
+{
+ driver_unregister(&cpc_drv->driver);
+}
+
static int __init cpc_init(void)
{
- return 0;
+ int err;
+
+ err = bus_register(&cpc_bus);
+
+ return err;
}
module_init(cpc_init);
static void __exit cpc_exit(void)
{
+ bus_unregister(&cpc_bus);
}
module_exit(cpc_exit);
--
2.49.0
Powered by blists - more mailing lists