From fd830d0ff53aa70488a69e4eaf3fb0de9205c39f Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Fri, 4 Dec 2020 09:46:22 +0100 Subject: [PATCH] driver core: aux test code try to test out the aux bus code --- drivers/base/Kconfig | 2 drivers/base/Makefile | 1 drivers/base/aux_test.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/base/auxiliary.c | 1 4 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 drivers/base/aux_test.c --- a/drivers/base/Kconfig +++ b/drivers/base/Kconfig @@ -2,7 +2,7 @@ menu "Generic Driver Options" config AUXILIARY_BUS - bool + tristate "aux bus" config UEVENT_HELPER bool "Support for uevent helper" --- a/drivers/base/Makefile +++ b/drivers/base/Makefile @@ -8,6 +8,7 @@ obj-y := component.o core.o bus.o dd.o topology.o container.o property.o cacheinfo.o \ swnode.o obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o +obj-$(CONFIG_AUXILIARY_BUS) += aux_test.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/ obj-$(CONFIG_ISA_BUS_API) += isa.o --- /dev/null +++ b/drivers/base/aux_test.c @@ -0,0 +1,140 @@ +// SPDX-License-Identifier: GPL-2.0-only + + +#include +#include +#include +#include +#include +#include + + +struct aux_test_device { + struct auxiliary_device auxdev; + int foo; + void *data; +}; + +#define aux_dev_to_test_device(auxdev) \ + container_of(auxdev, struct aux_test_device, auxdev) + +static void aux_test_dev_release(struct device *dev) +{ + struct auxiliary_device *auxdev = to_auxiliary_dev(dev); + struct aux_test_device *test_dev = aux_dev_to_test_device(auxdev); + + kfree(test_dev); +} + +static struct aux_test_device *test_device_alloc(struct device *parent, + const char *name, u32 id) +{ + struct aux_test_device *test_dev; + struct auxiliary_device *auxdev; + int retval; + + test_dev = kzalloc(sizeof(*test_dev), GFP_KERNEL); + if (!test_dev) + return NULL; + + auxdev= &test_dev->auxdev; + auxdev->name = name; + auxdev->dev.parent = parent; + auxdev->dev.release = aux_test_dev_release; + auxdev->id = id; + + retval = auxiliary_device_init(auxdev); + if (retval) { + dev_err(parent, "aux device failed to init\n"); + kfree(test_dev); + return NULL; + } + + return test_dev; +} + +static struct aux_test_device *test_device_create(struct device *parent, + const char *name, u32 id) +{ + struct aux_test_device *test_dev; + int retval; + + test_dev = test_device_alloc(parent, name, id); + if (!test_dev) { + dev_err(parent, "aux device %s failed to be created\n", name); + return NULL; + } + + retval = auxiliary_device_add(&test_dev->auxdev); + if (retval) { + dev_err(parent, "aux device %s failed to be added, error %d\n", + name, retval); + auxiliary_device_uninit(&test_dev->auxdev); + return NULL; + } + + return test_dev; +} + +static void test_dev_del(struct aux_test_device *test_dev) +{ + if (!test_dev) + return; + + auxiliary_device_delete(&test_dev->auxdev); + auxiliary_device_uninit(&test_dev->auxdev); +} + + +static struct aux_test_device *tdev1, *tdev2, *tdev3; + +/* Make a random device to be the "parent" of our tests */ +static struct platform_device *root_device; + +static void root_device_release(struct device *dev) +{ + struct platform_device *pdev = to_platform_device(dev); + kfree(pdev); +} + +static int __init aux_test_init(void) +{ + int retval; + + root_device = kzalloc(sizeof(*root_device), GFP_KERNEL); + if (!root_device) + return -ENOMEM; + + root_device->name = "aux_test_root"; + root_device->dev.release = root_device_release; + + retval = platform_device_register(root_device); + if (retval) { + kfree(root_device); + return retval; + } + + /* Allocate 3 test devices as a child of this parent */ + tdev1 = test_device_create(&root_device->dev, "test_dev_1", 21); + tdev2 = test_device_create(&root_device->dev, "test_dev_2", 32); + tdev3 = test_device_create(&root_device->dev, "test_dev_3", 43); + + return 0; +} + +static void __exit aux_test_exit(void) +{ + test_dev_del(tdev1); + test_dev_del(tdev2); + test_dev_del(tdev3); + platform_device_unregister(root_device); + +} + + + +module_init(aux_test_init); +module_exit(aux_test_exit); + + +MODULE_LICENSE("GPL v2"); --- a/drivers/base/auxiliary.c +++ b/drivers/base/auxiliary.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include