// 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);157158ret = device_add(dev);159if (ret) {160pr_err("%s: device_add for faux device '%s' failed with %d\n",161__func__, name, ret);162put_device(dev);163return NULL;164}165166/*167* Verify that we did bind the driver to the device (i.e. probe worked),168* if not, let's fail the creation as trying to guess if probe was169* successful is almost impossible to determine by the caller.170*/171if (!dev->driver) {172dev_dbg(dev, "probe did not succeed, tearing down the device\n");173faux_device_destroy(faux_dev);174faux_dev = NULL;175}176177return faux_dev;178}179EXPORT_SYMBOL_GPL(faux_device_create_with_groups);180181/**182* faux_device_create - create and register with the driver core a faux device183* @name: The name of the device we are adding, must be unique for all184* faux devices.185* @parent: Pointer to a potential parent struct device. If set to186* NULL, the device will be created in the "root" of the faux187* device tree in sysfs.188* @faux_ops: struct faux_device_ops that the new device will call back189* into, can be NULL.190*191* Create a new faux device and register it in the driver core properly.192* If present, callbacks in @faux_ops will be called with the device that193* for the caller to do something with at the proper time given the194* device's lifecycle.195*196* Note, when this function is called, the functions specified in struct197* faux_ops can be called before the function returns, so be prepared for198* everything to be properly initialized before that point in time.199*200* Return:201* * NULL if an error happened with creating the device202* * pointer to a valid struct faux_device that is registered with sysfs203*/204struct faux_device *faux_device_create(const char *name,205struct device *parent,206const struct faux_device_ops *faux_ops)207{208return faux_device_create_with_groups(name, parent, faux_ops, NULL);209}210EXPORT_SYMBOL_GPL(faux_device_create);211212/**213* faux_device_destroy - destroy a faux device214* @faux_dev: faux device to destroy215*216* Unregisters and cleans up a device that was created with a call to217* faux_device_create()218*/219void faux_device_destroy(struct faux_device *faux_dev)220{221struct device *dev = &faux_dev->dev;222223if (!faux_dev)224return;225226device_del(dev);227228/* The final put_device() will clean up the memory we allocated for this device. */229put_device(dev);230}231EXPORT_SYMBOL_GPL(faux_device_destroy);232233int __init faux_bus_init(void)234{235int ret;236237ret = device_register(&faux_bus_root);238if (ret) {239put_device(&faux_bus_root);240return ret;241}242243ret = bus_register(&faux_bus_type);244if (ret)245goto error_bus;246247ret = driver_register(&faux_driver);248if (ret)249goto error_driver;250251return ret;252253error_driver:254bus_unregister(&faux_bus_type);255256error_bus:257device_unregister(&faux_bus_root);258return ret;259}260261262