[PATCH v2 1/8] driver core: add helper macro for module_faux_driver() boilerplate

Sudeep Holla posted 8 patches 9 months ago
[PATCH v2 1/8] driver core: add helper macro for module_faux_driver() boilerplate
Posted by Sudeep Holla 9 months ago
For simple modules that needs to create a faux device without any
additional setup code ends up being a block of duplicated boilerplate.

Add a new macro, module_faux_driver(), which help to replaces the
those duplicated boilerplate.

This macro use the same idea of module_platform_driver() but adds this
initial condition to avoid creation of faux device if not necessary.

Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
---
 include/linux/device/faux.h | 49 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/include/linux/device/faux.h b/include/linux/device/faux.h
index 9f43c0e46aa45bf492788adcdc081df5cc0c5fc0..4a54736d86595e46c98ac3ab9c45a7e5a344333e 100644
--- a/include/linux/device/faux.h
+++ b/include/linux/device/faux.h
@@ -15,6 +15,7 @@
 
 #include <linux/container_of.h>
 #include <linux/device.h>
+#include <linux/stringify.h>
 
 /**
  * struct faux_device - a "faux" device
@@ -66,4 +67,52 @@ static inline void faux_device_set_drvdata(struct faux_device *faux_dev, void *d
 	dev_set_drvdata(&faux_dev->dev, data);
 }
 
+#define FAUX_DEVICE(__faux_devname) \
+static struct faux_device *__faux_devname##_dev;
+
+#define FAUX_DEVICE_OPS(__faux_devname, __faux_probe, __faux_remove) \
+static const struct faux_device_ops __faux_devname##_ops = {	\
+	.probe = __faux_probe,					\
+	.remove = __faux_remove,				\
+};								\
+FAUX_DEVICE(__faux_devname)
+
+static inline int
+__faux_device_register(struct faux_device **faux_dev, const char *name,
+		       const struct faux_device_ops *faux_ops, bool condition)
+{
+	struct faux_device *fdev;
+
+	if (!condition)
+		return 0;
+
+	fdev = faux_device_create(name, NULL, faux_ops);
+	if (!fdev)
+		return -ENODEV;
+
+	*faux_dev = fdev;
+	return 0;
+}
+
+#define faux_device_register(faux_dev, faux_devname, init_condition) \
+	__faux_device_register(faux_dev, __stringify(faux_devname), \
+			       &faux_devname##_ops, init_condition)
+
+#define faux_device_unregister(faux_dev, ...) \
+		faux_device_destroy(*faux_dev)
+
+/* module_faux_driver() - Helper macro for faux drivers that don't do
+ * anything special in module init/exit.  This eliminates a lot of
+ * boilerplate.  Each module may only use this macro once, and
+ * calling it replaces module_init() and module_exit(). The module init
+ * creates a faux device if the init condition is met and module exit
+ * destroys the created device. FAUX_DEVICE_OPS must be used to declare
+ * faux device ops and the device pointer.
+ */
+#define module_faux_driver(__faux_devname, __faux_probe, __faux_remove, \
+			   __init_condition) \
+	FAUX_DEVICE_OPS(__faux_devname, __faux_probe, __faux_remove) \
+	module_driver(__faux_devname##_dev, faux_device_register, \
+		      faux_device_unregister, __faux_devname, __init_condition)
+
 #endif /* _FAUX_DEVICE_H_ */

-- 
2.34.1
Re: [PATCH v2 1/8] driver core: add helper macro for module_faux_driver() boilerplate
Posted by Greg Kroah-Hartman 8 months ago
On Tue, Mar 18, 2025 at 05:01:39PM +0000, Sudeep Holla wrote:
> For simple modules that needs to create a faux device without any
> additional setup code ends up being a block of duplicated boilerplate.
> 
> Add a new macro, module_faux_driver(), which help to replaces the
> those duplicated boilerplate.
> 
> This macro use the same idea of module_platform_driver() but adds this
> initial condition to avoid creation of faux device if not necessary.

What is this "condition" for?

Every time you put "true" or "false" in the function call, someone will
have to look it up to see what is going on, that's going to be a pain.

Making apis is hard, let's not making using them even harder.

thanks,

greg k-h
Re: [PATCH v2 1/8] driver core: add helper macro for module_faux_driver() boilerplate
Posted by Sudeep Holla 8 months ago
On Tue, Apr 15, 2025 at 02:21:10PM +0200, Greg Kroah-Hartman wrote:
> On Tue, Mar 18, 2025 at 05:01:39PM +0000, Sudeep Holla wrote:
> > For simple modules that needs to create a faux device without any
> > additional setup code ends up being a block of duplicated boilerplate.
> > 
> > Add a new macro, module_faux_driver(), which help to replaces the
> > those duplicated boilerplate.
> > 
> > This macro use the same idea of module_platform_driver() but adds this
> > initial condition to avoid creation of faux device if not necessary.
> 
> What is this "condition" for?
> 
> Every time you put "true" or "false" in the function call, someone will
> have to look it up to see what is going on, that's going to be a pain.
> 
> Making apis is hard, let's not making using them even harder.
> 

Agreed and also since the number of users the would use reduced due to
their autoload dependency, I dropped the idea of having the macro. All the
ones that can be moved to use faux_device have now moved(I mean queued)
without this macro as it was in v1 of the series.

Thanks for the time and review. Sorry I should have provided update on this
after I had to drop all the efi related patches for the above reason.

-- 
Regards,
Sudeep