Something went wrong. Try again.
Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202// SPDX-License-Identifier: GPL-2.0/* * KUnit-managed device implementation * * Implementation of struct kunit_device helpers for fake devices whose * lifecycle is managed by KUnit. * * Copyright (C) 2023, Google LLC. * Author: David Gow <davidgow@google.com> */
#include <linux/device.h>#include <linux/dma-mapping.h>
#include <kunit/test.h>#include <kunit/device.h>#include <kunit/resource.h>
#include "device-impl.h"
/* Wrappers for use with kunit_add_action() */KUNIT_DEFINE_ACTION_WRAPPER(device_unregister_wrapper, device_unregister, struct device *);KUNIT_DEFINE_ACTION_WRAPPER(driver_unregister_wrapper, driver_unregister, struct device_driver *);
/* The root device for the KUnit bus, parent of all kunit_devices. */static struct device *kunit_bus_device;
/* A device owned by a KUnit test. */struct kunit_device { struct device dev; /* The KUnit test which owns this device. */ struct kunit *owner; /* If the driver is managed by KUnit and unique to this device. */ const struct device_driver *driver;};
#define to_kunit_device(d) container_of_const(d, struct kunit_device, dev)
static const struct bus_type kunit_bus_type = { .name = "kunit",};
/* Register the 'kunit_bus' used for fake devices. */int kunit_bus_init(void){ int error;
kunit_bus_device = root_device_register("kunit"); if (IS_ERR(kunit_bus_device)) return PTR_ERR(kunit_bus_device);
error = bus_register(&kunit_bus_type); if (error) root_device_unregister(kunit_bus_device); return error;}
/* Unregister the 'kunit_bus' in case the KUnit module is unloaded. */void kunit_bus_shutdown(void){ /* Make sure the bus exists before we unregister it. */ if (IS_ERR_OR_NULL(kunit_bus_device)) return;
bus_unregister(&kunit_bus_type);
root_device_unregister(kunit_bus_device);
kunit_bus_device = NULL;}
/* Release a 'fake' KUnit device. */static void kunit_device_release(struct device *d){ kfree(to_kunit_device(d));}
/* * Create and register a KUnit-managed struct device_driver on the kunit_bus. * Returns an error pointer on failure. */struct device_driver *kunit_driver_create(struct kunit *test, const char *name){ struct device_driver *driver; int err = -ENOMEM;
driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
if (!driver) return ERR_PTR(err);
driver->name = kunit_kstrdup_const(test, name, GFP_KERNEL); driver->bus = &kunit_bus_type; driver->owner = THIS_MODULE;
err = driver_register(driver); if (err) { kunit_kfree(test, driver); return ERR_PTR(err); }
kunit_add_action(test, driver_unregister_wrapper, driver); return driver;}EXPORT_SYMBOL_GPL(kunit_driver_create);
/* Helper which creates a kunit_device, attaches it to the kunit_bus*/static struct kunit_device *kunit_device_register_internal(struct kunit *test, const char *name){ struct kunit_device *kunit_dev; int err = -ENOMEM;
kunit_dev = kzalloc_obj(*kunit_dev); if (!kunit_dev) return ERR_PTR(err);
kunit_dev->owner = test;
err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name); if (err) { kfree(kunit_dev); return ERR_PTR(err); }
kunit_dev->dev.release = kunit_device_release; kunit_dev->dev.bus = &kunit_bus_type; kunit_dev->dev.parent = kunit_bus_device;
err = device_register(&kunit_dev->dev); if (err) { put_device(&kunit_dev->dev); return ERR_PTR(err); }
kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask; kunit_dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
return kunit_dev;}
/* * Create and register a new KUnit-managed device, using the user-supplied device_driver. * On failure, returns an error pointer. */struct device *kunit_device_register_with_driver(struct kunit *test, const char *name, const struct device_driver *drv){ struct kunit_device *kunit_dev = kunit_device_register_internal(test, name);
if (IS_ERR_OR_NULL(kunit_dev)) return ERR_CAST(kunit_dev);
return &kunit_dev->dev;}EXPORT_SYMBOL_GPL(kunit_device_register_with_driver);
/* * Create and register a new KUnit-managed device, including a matching device_driver. * On failure, returns an error pointer. */struct device *kunit_device_register(struct kunit *test, const char *name){ struct device_driver *drv; struct kunit_device *dev;
drv = kunit_driver_create(test, name); if (IS_ERR(drv)) return ERR_CAST(drv);
dev = kunit_device_register_internal(test, name); if (IS_ERR(dev)) { kunit_release_action(test, driver_unregister_wrapper, (void *)drv); return ERR_CAST(dev); }
/* Request the driver be freed. */ dev->driver = drv;
return &dev->dev;}EXPORT_SYMBOL_GPL(kunit_device_register);
/* Unregisters a KUnit-managed device early (including the driver, if automatically created). */void kunit_device_unregister(struct kunit *test, struct device *dev){ const struct device_driver *driver = to_kunit_device(dev)->driver;
kunit_release_action(test, device_unregister_wrapper, dev); if (driver) { const char *driver_name = driver->name; kunit_release_action(test, driver_unregister_wrapper, (void *)driver); kunit_kfree_const(test, driver_name); }}EXPORT_SYMBOL_GPL(kunit_device_unregister);