xref: /linux-6.15/include/linux/device/faux.h (revision 35fa2d88)
1*35fa2d88SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0-only */
2*35fa2d88SGreg Kroah-Hartman /*
3*35fa2d88SGreg Kroah-Hartman  * Copyright (c) 2025 Greg Kroah-Hartman <[email protected]>
4*35fa2d88SGreg Kroah-Hartman  * Copyright (c) 2025 The Linux Foundation
5*35fa2d88SGreg Kroah-Hartman  *
6*35fa2d88SGreg Kroah-Hartman  * A "simple" faux bus that allows devices to be created and added
7*35fa2d88SGreg Kroah-Hartman  * automatically to it.  This is to be used whenever you need to create a
8*35fa2d88SGreg Kroah-Hartman  * device that is not associated with any "real" system resources, and do
9*35fa2d88SGreg Kroah-Hartman  * not want to have to deal with a bus/driver binding logic.  It is
10*35fa2d88SGreg Kroah-Hartman  * intended to be very simple, with only a create and a destroy function
11*35fa2d88SGreg Kroah-Hartman  * available.
12*35fa2d88SGreg Kroah-Hartman  */
13*35fa2d88SGreg Kroah-Hartman #ifndef _FAUX_DEVICE_H_
14*35fa2d88SGreg Kroah-Hartman #define _FAUX_DEVICE_H_
15*35fa2d88SGreg Kroah-Hartman 
16*35fa2d88SGreg Kroah-Hartman #include <linux/container_of.h>
17*35fa2d88SGreg Kroah-Hartman #include <linux/device.h>
18*35fa2d88SGreg Kroah-Hartman 
19*35fa2d88SGreg Kroah-Hartman /**
20*35fa2d88SGreg Kroah-Hartman  * struct faux_device - a "faux" device
21*35fa2d88SGreg Kroah-Hartman  * @dev:	internal struct device of the object
22*35fa2d88SGreg Kroah-Hartman  *
23*35fa2d88SGreg Kroah-Hartman  * A simple faux device that can be created/destroyed.  To be used when a
24*35fa2d88SGreg Kroah-Hartman  * driver only needs to have a device to "hang" something off.  This can be
25*35fa2d88SGreg Kroah-Hartman  * used for downloading firmware or other basic tasks.  Use this instead of
26*35fa2d88SGreg Kroah-Hartman  * a struct platform_device if the device has no resources assigned to
27*35fa2d88SGreg Kroah-Hartman  * it at all.
28*35fa2d88SGreg Kroah-Hartman  */
29*35fa2d88SGreg Kroah-Hartman struct faux_device {
30*35fa2d88SGreg Kroah-Hartman 	struct device dev;
31*35fa2d88SGreg Kroah-Hartman };
32*35fa2d88SGreg Kroah-Hartman #define to_faux_device(x) container_of_const((x), struct faux_device, dev)
33*35fa2d88SGreg Kroah-Hartman 
34*35fa2d88SGreg Kroah-Hartman /**
35*35fa2d88SGreg Kroah-Hartman  * struct faux_device_ops - a set of callbacks for a struct faux_device
36*35fa2d88SGreg Kroah-Hartman  * @probe:	called when a faux device is probed by the driver core
37*35fa2d88SGreg Kroah-Hartman  *		before the device is fully bound to the internal faux bus
38*35fa2d88SGreg Kroah-Hartman  *		code.  If probe succeeds, return 0, otherwise return a
39*35fa2d88SGreg Kroah-Hartman  *		negative error number to stop the probe sequence from
40*35fa2d88SGreg Kroah-Hartman  *		succeeding.
41*35fa2d88SGreg Kroah-Hartman  * @remove:	called when a faux device is removed from the system
42*35fa2d88SGreg Kroah-Hartman  *
43*35fa2d88SGreg Kroah-Hartman  * Both @probe and @remove are optional, if not needed, set to NULL.
44*35fa2d88SGreg Kroah-Hartman  */
45*35fa2d88SGreg Kroah-Hartman struct faux_device_ops {
46*35fa2d88SGreg Kroah-Hartman 	int (*probe)(struct faux_device *faux_dev);
47*35fa2d88SGreg Kroah-Hartman 	void (*remove)(struct faux_device *faux_dev);
48*35fa2d88SGreg Kroah-Hartman };
49*35fa2d88SGreg Kroah-Hartman 
50*35fa2d88SGreg Kroah-Hartman struct faux_device *faux_device_create(const char *name,
51*35fa2d88SGreg Kroah-Hartman 				       struct device *parent,
52*35fa2d88SGreg Kroah-Hartman 				       const struct faux_device_ops *faux_ops);
53*35fa2d88SGreg Kroah-Hartman struct faux_device *faux_device_create_with_groups(const char *name,
54*35fa2d88SGreg Kroah-Hartman 						   struct device *parent,
55*35fa2d88SGreg Kroah-Hartman 						   const struct faux_device_ops *faux_ops,
56*35fa2d88SGreg Kroah-Hartman 						   const struct attribute_group **groups);
57*35fa2d88SGreg Kroah-Hartman void faux_device_destroy(struct faux_device *faux_dev);
58*35fa2d88SGreg Kroah-Hartman 
faux_device_get_drvdata(const struct faux_device * faux_dev)59*35fa2d88SGreg Kroah-Hartman static inline void *faux_device_get_drvdata(const struct faux_device *faux_dev)
60*35fa2d88SGreg Kroah-Hartman {
61*35fa2d88SGreg Kroah-Hartman 	return dev_get_drvdata(&faux_dev->dev);
62*35fa2d88SGreg Kroah-Hartman }
63*35fa2d88SGreg Kroah-Hartman 
faux_device_set_drvdata(struct faux_device * faux_dev,void * data)64*35fa2d88SGreg Kroah-Hartman static inline void faux_device_set_drvdata(struct faux_device *faux_dev, void *data)
65*35fa2d88SGreg Kroah-Hartman {
66*35fa2d88SGreg Kroah-Hartman 	dev_set_drvdata(&faux_dev->dev, data);
67*35fa2d88SGreg Kroah-Hartman }
68*35fa2d88SGreg Kroah-Hartman 
69*35fa2d88SGreg Kroah-Hartman #endif /* _FAUX_DEVICE_H_ */
70