History

driver core: add a faux bus for use when a simple device/bus is needed

Many drivers abuse the platform driver/bus system as it provides a simple way to create and bind a device to a driver-specific set of probe/release functions. Instead of doing that, and wasting all of the memory associated with a platform device, here is a "faux" bus that can be used instead.

Greg Kroah-Hartman committed 1 year ago

Commit 35fa2d81 parent a64dcfb6 files · +310 −1Browse files

Showing the changes to include/linux/device (1 of 6 files). Show all

Unified · Split

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