device.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit-managed device implementation
  4. *
  5. * Implementation of struct kunit_device helpers for fake devices whose
  6. * lifecycle is managed by KUnit.
  7. *
  8. * Copyright (C) 2023, Google LLC.
  9. * Author: David Gow <davidgow@google.com>
  10. */
  11. #include <linux/device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <kunit/test.h>
  14. #include <kunit/device.h>
  15. #include <kunit/resource.h>
  16. #include "device-impl.h"
  17. /* Wrappers for use with kunit_add_action() */
  18. KUNIT_DEFINE_ACTION_WRAPPER(device_unregister_wrapper, device_unregister, struct device *);
  19. KUNIT_DEFINE_ACTION_WRAPPER(driver_unregister_wrapper, driver_unregister, struct device_driver *);
  20. /* The root device for the KUnit bus, parent of all kunit_devices. */
  21. static struct device *kunit_bus_device;
  22. /* A device owned by a KUnit test. */
  23. struct kunit_device {
  24. struct device dev;
  25. /* The KUnit test which owns this device. */
  26. struct kunit *owner;
  27. /* If the driver is managed by KUnit and unique to this device. */
  28. const struct device_driver *driver;
  29. };
  30. #define to_kunit_device(d) container_of_const(d, struct kunit_device, dev)
  31. static const struct bus_type kunit_bus_type = {
  32. .name = "kunit",
  33. };
  34. /* Register the 'kunit_bus' used for fake devices. */
  35. int kunit_bus_init(void)
  36. {
  37. int error;
  38. kunit_bus_device = root_device_register("kunit");
  39. if (IS_ERR(kunit_bus_device))
  40. return PTR_ERR(kunit_bus_device);
  41. error = bus_register(&kunit_bus_type);
  42. if (error)
  43. root_device_unregister(kunit_bus_device);
  44. return error;
  45. }
  46. /* Unregister the 'kunit_bus' in case the KUnit module is unloaded. */
  47. void kunit_bus_shutdown(void)
  48. {
  49. /* Make sure the bus exists before we unregister it. */
  50. if (IS_ERR_OR_NULL(kunit_bus_device))
  51. return;
  52. bus_unregister(&kunit_bus_type);
  53. root_device_unregister(kunit_bus_device);
  54. kunit_bus_device = NULL;
  55. }
  56. /* Release a 'fake' KUnit device. */
  57. static void kunit_device_release(struct device *d)
  58. {
  59. kfree(to_kunit_device(d));
  60. }
  61. /*
  62. * Create and register a KUnit-managed struct device_driver on the kunit_bus.
  63. * Returns an error pointer on failure.
  64. */
  65. struct device_driver *kunit_driver_create(struct kunit *test, const char *name)
  66. {
  67. struct device_driver *driver;
  68. int err = -ENOMEM;
  69. driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
  70. if (!driver)
  71. return ERR_PTR(err);
  72. driver->name = kunit_kstrdup_const(test, name, GFP_KERNEL);
  73. driver->bus = &kunit_bus_type;
  74. driver->owner = THIS_MODULE;
  75. err = driver_register(driver);
  76. if (err) {
  77. kunit_kfree(test, driver);
  78. return ERR_PTR(err);
  79. }
  80. kunit_add_action(test, driver_unregister_wrapper, driver);
  81. return driver;
  82. }
  83. EXPORT_SYMBOL_GPL(kunit_driver_create);
  84. /* Helper which creates a kunit_device, attaches it to the kunit_bus*/
  85. static struct kunit_device *kunit_device_register_internal(struct kunit *test,
  86. const char *name)
  87. {
  88. struct kunit_device *kunit_dev;
  89. int err = -ENOMEM;
  90. kunit_dev = kzalloc_obj(*kunit_dev);
  91. if (!kunit_dev)
  92. return ERR_PTR(err);
  93. kunit_dev->owner = test;
  94. err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
  95. if (err) {
  96. kfree(kunit_dev);
  97. return ERR_PTR(err);
  98. }
  99. kunit_dev->dev.release = kunit_device_release;
  100. kunit_dev->dev.bus = &kunit_bus_type;
  101. kunit_dev->dev.parent = kunit_bus_device;
  102. err = device_register(&kunit_dev->dev);
  103. if (err) {
  104. put_device(&kunit_dev->dev);
  105. return ERR_PTR(err);
  106. }
  107. kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask;
  108. kunit_dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  109. kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
  110. return kunit_dev;
  111. }
  112. /*
  113. * Create and register a new KUnit-managed device, using the user-supplied device_driver.
  114. * On failure, returns an error pointer.
  115. */
  116. struct device *kunit_device_register_with_driver(struct kunit *test,
  117. const char *name,
  118. const struct device_driver *drv)
  119. {
  120. struct kunit_device *kunit_dev = kunit_device_register_internal(test, name);
  121. if (IS_ERR_OR_NULL(kunit_dev))
  122. return ERR_CAST(kunit_dev);
  123. return &kunit_dev->dev;
  124. }
  125. EXPORT_SYMBOL_GPL(kunit_device_register_with_driver);
  126. /*
  127. * Create and register a new KUnit-managed device, including a matching device_driver.
  128. * On failure, returns an error pointer.
  129. */
  130. struct device *kunit_device_register(struct kunit *test, const char *name)
  131. {
  132. struct device_driver *drv;
  133. struct kunit_device *dev;
  134. drv = kunit_driver_create(test, name);
  135. if (IS_ERR(drv))
  136. return ERR_CAST(drv);
  137. dev = kunit_device_register_internal(test, name);
  138. if (IS_ERR(dev)) {
  139. kunit_release_action(test, driver_unregister_wrapper, (void *)drv);
  140. return ERR_CAST(dev);
  141. }
  142. /* Request the driver be freed. */
  143. dev->driver = drv;
  144. return &dev->dev;
  145. }
  146. EXPORT_SYMBOL_GPL(kunit_device_register);
  147. /* Unregisters a KUnit-managed device early (including the driver, if automatically created). */
  148. void kunit_device_unregister(struct kunit *test, struct device *dev)
  149. {
  150. const struct device_driver *driver = to_kunit_device(dev)->driver;
  151. kunit_release_action(test, device_unregister_wrapper, dev);
  152. if (driver) {
  153. const char *driver_name = driver->name;
  154. kunit_release_action(test, driver_unregister_wrapper, (void *)driver);
  155. kunit_kfree_const(test, driver_name);
  156. }
  157. }
  158. EXPORT_SYMBOL_GPL(kunit_device_unregister);