// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (c) 2025 Greg Kroah-Hartman <[email protected]>3* Copyright (c) 2025 The Linux Foundation4*5* A "simple" faux bus that allows devices to be created and added6* automatically to it. This is to be used whenever you need to create a7* device that is not associated with any "real" system resources, and do8* not want to have to deal with a bus/driver binding logic. It is9* intended to be very simple, with only a create and a destroy function10* available.11*/12#include <linux/err.h>13#include <linux/init.h>14#include <linux/slab.h>15#include <linux/string.h>16#include <linux/container_of.h>17#include <linux/device/faux.h>18#include "base.h"1920/*21* Internal wrapper structure so we can hold a pointer to the22* faux_device_ops for this device.23*/24struct faux_object {25struct faux_device faux_dev;26const struct faux_device_ops *faux_ops;27const struct attribute_group **groups;28};29#define to_faux_object(dev) container_of_const(dev, struct faux_object, faux_dev.dev)3031static struct device faux_bus_root = {32.init_name = "faux",33};3435static int faux_match(struct device *dev, const struct device_driver *drv)36{37/* Match always succeeds, we only have one driver */38return 1;39}4041static int faux_probe(struct device *dev)42{43struct faux_object *faux_obj = to_faux_object(dev);44struct faux_device *faux_dev = &faux_obj->faux_dev;45const struct faux_device_ops *faux_ops = faux_obj->faux_ops;46int ret;4748if (faux_ops && faux_ops->probe) {49ret = faux_ops->probe(faux_dev);50if (ret)51return ret;52}5354/*55* Add groups after the probe succeeds to ensure resources are56* initialized correctly57*/58ret = device_add_groups(dev, faux_obj->groups);59if (ret && faux_ops && faux_ops->remove)60faux_ops->remove(faux_dev);6162return ret;63}6465static void faux_remove(struct device *dev)66{67struct faux_object *faux_obj = to_faux_object(dev);68struct faux_device *faux_dev = &faux_obj->faux_dev;69const struct faux_device_ops *faux_ops = faux_obj->faux_ops;7071device_remove_groups(dev, faux_obj->groups);7273if (faux_ops && faux_ops->remove)74faux_ops->remove(faux_dev);75}7677static const struct bus_type faux_bus_type = {78.name = "faux",79.match = faux_match,80.probe = faux_probe,81.remove = faux_remove,82};8384static struct device_driver faux_driver = {85.name = "faux_driver",86.bus = &faux_bus_type,87.probe_type = PROBE_FORCE_SYNCHRONOUS,88.suppress_bind_attrs = true,89};9091static void faux_device_release(struct device *dev)92{93struct faux_object *faux_obj = to_faux_object(dev);9495kfree(faux_obj);96}9798/**99* faux_device_create_with_groups - Create and register with the driver100* core a faux device and populate the device with an initial101* set of sysfs attributes.102* @name: The name of the device we are adding, must be unique for103* all faux devices.104* @parent: Pointer to a potential parent struct device. If set to105* NULL, the device will be created in the "root" of the faux106* device tree in sysfs.107* @faux_ops: struct faux_device_ops that the new device will call back108* into, can be NULL.109* @groups: The set of sysfs attributes that will be created for this110* device when it is registered with the driver core.111*112* Create a new faux device and register it in the driver core properly.113* If present, callbacks in @faux_ops will be called with the device that114* for the caller to do something with at the proper time given the115* device's lifecycle.116*117* Note, when this function is called, the functions specified in struct118* faux_ops can be called before the function returns, so be prepared for119* everything to be properly initialized before that point in time. If the120* probe callback (if one is present) does NOT succeed, the creation of the121* device will fail and NULL will be returned.122*123* Return:124* * NULL if an error happened with creating the device125* * pointer to a valid struct faux_device that is registered with sysfs126*/127struct faux_device *faux_device_create_with_groups(const char *name,128struct device *parent,129const struct faux_device_ops *faux_ops,130const struct attribute_group **groups)131{132struct faux_object *faux_obj;133struct faux_device *faux_dev;134struct device *dev;135int ret;136137faux_obj = kzalloc(sizeof(*faux_obj), GFP_KERNEL);138if (!faux_obj)139return NULL;140141/* Save off the callbacks and groups so we can use them in the future */142faux_obj->faux_ops = faux_ops;143faux_obj->groups = groups;144145/* Initialize the device portion and register it with the driver core */146faux_dev = &faux_obj->faux_dev;147dev = &faux_dev->dev;148149device_initialize(dev);150dev->release = faux_device_release;151if (parent)152dev->parent = parent;153else154dev->parent = &faux_bus_root;155dev->bus = &faux_bus_type;156dev_set_name(dev, "%s", name);157device_set_pm_not_required(dev);158159ret = device_add(dev);160if (ret) {161pr_err("%s: device_add for faux device '%s' failed with %d\n",162__func__, name, ret);163put_device(dev);164return NULL;165}166167/*168* Verify that we did bind the driver to the device (i.e. probe worked),169* if not, let's fail the creation as trying to guess if probe was170* successful is almost impossible to determine by the caller.171*/172if (!dev->driver) {173dev_dbg(dev, "probe did not succeed, tearing down the device\n");174faux_device_destroy(faux_dev);175faux_dev = NULL;176}177178return faux_dev;179}180EXPORT_SYMBOL_GPL(faux_device_create_with_groups);181182/**183* faux_device_create - create and register with the driver core a faux device184* @name: The name of the device we are adding, must be unique for all185* faux devices.186* @parent: Pointer to a potential parent struct device. If set to187* NULL, the device will be created in the "root" of the faux188* device tree in sysfs.189* @faux_ops: struct faux_device_ops that the new device will call back190* into, can be NULL.191*192* Create a new faux device and register it in the driver core properly.193* If present, callbacks in @faux_ops will be called with the device that194* for the caller to do something with at the proper time given the195* device's lifecycle.196*197* Note, when this function is called, the functions specified in struct198* faux_ops can be called before the function returns, so be prepared for199* everything to be properly initialized before that point in time.200*201* Return:202* * NULL if an error happened with creating the device203* * pointer to a valid struct faux_device that is registered with sysfs204*/205struct faux_device *faux_device_create(const char *name,206struct device *parent,207const struct faux_device_ops *faux_ops)208{209return faux_device_create_with_groups(name, parent, faux_ops, NULL);210}211EXPORT_SYMBOL_GPL(faux_device_create);212213/**214* faux_device_destroy - destroy a faux device215* @faux_dev: faux device to destroy216*217* Unregisters and cleans up a device that was created with a call to218* faux_device_create()219*/220void faux_device_destroy(struct faux_device *faux_dev)221{222struct device *dev = &faux_dev->dev;223224if (!faux_dev)225return;226227device_del(dev);228229/* The final put_device() will clean up the memory we allocated for this device. */230put_device(dev);231}232EXPORT_SYMBOL_GPL(faux_device_destroy);233234int __init faux_bus_init(void)235{236int ret;237238ret = device_register(&faux_bus_root);239if (ret) {240put_device(&faux_bus_root);241return ret;242}243244ret = bus_register(&faux_bus_type);245if (ret)246goto error_bus;247248ret = driver_register(&faux_driver);249if (ret)250goto error_driver;251252return ret;253254error_driver:255bus_unregister(&faux_bus_type);256257error_bus:258device_unregister(&faux_bus_root);259return ret;260}261262263