Re: [PATCH v3 1/8] driver core: add a faux bus for use when a simple device/bus is needed

Zijun Hu
2025-02-07T02:55:14Z

On 2/7/2025 1:38 AM, Greg Kroah-Hartman wrote:
> +#include "base.h"
> +
> +#define MAX_FAUX_NAME_SIZE 256 /* Max size of a faux_device name */

Remove this macro?

> ++ */

<snip>

> +struct faux_device *faux_device_create_with_groups(const char *name,
> + const struct faux_device_ops *faux_ops,
> + const struct attribute_group **groups)
> +{
> + struct device *dev;
> + struct faux_object *faux_obj;
> + struct faux_device *faux_dev;
> + int name_size;

Remove @name_size?

> + int ret;
> +
> + name_size = strlen(name);
> + if (name_size > MAX_FAUX_NAME_SIZE)
> + return NULL;
> +

Remove above block related to @name_size

> + faux_obj = kzalloc(sizeof(*faux_obj) + name_size + 1, GFP_KERNEL);

faux_obj = kzalloc(sizeof(*faux_obj), GFP_KERNEL);

> + if (!faux_obj)
> + return NULL;
> +
> + /* Save off the callbacks so we can use them in the future */
> + faux_obj->faux_ops = faux_ops;
> +
> + /* Initialize the device portion and register it with the driver core */
> + faux_dev = &faux_obj->faux_dev;
> + dev = &faux_dev->dev;
> +
> + device_initialize(dev);
> + dev->release = faux_device_release;
> + dev->parent = &faux_bus_root;
> + dev->bus = &faux_bus_type;
> + dev->groups = groups;
> + dev_set_name(dev, "%s", name);
> +
> + ret = device_add(dev);
> + if (ret) {
> + pr_err("%s: device_add for faux device '%s' failed with %d\n",
> + __func__, name, ret);
> + put_device(dev);
> + return NULL;
> + }
> +
> + return faux_dev;
> +}
> +EXPORT_SYMBOL_GPL(faux_device_create_with_groups);

<snip>

> ++int __init faux_bus_init(void)
> +{
> + int ret;
> +
> + ret = device_register(&faux_bus_root);
> + if (ret) {
> + put_device(&faux_bus_root);
> + return ret;
> + }
> +
> + ret = bus_register(&faux_bus_type);
> + if (ret)
> + goto error_bus;
> +
> + ret = driver_register(&faux_driver);
> + if (ret)
> + goto error_driver;
> +
> + return ret;

return 0;

> +
> +error_driver:
> + bus_unregister(&faux_bus_type);
> +
> +error_bus:
> + device_unregister(&faux_bus_root);
> + return ret;
> +}

<snip>