faux.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2025 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  4. * Copyright (c) 2025 The Linux Foundation
  5. *
  6. * A "simple" faux bus that allows devices to be created and added
  7. * automatically to it. This is to be used whenever you need to create a
  8. * device that is not associated with any "real" system resources, and do
  9. * not want to have to deal with a bus/driver binding logic. It is
  10. * intended to be very simple, with only a create and a destroy function
  11. * available.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/container_of.h>
  18. #include <linux/device/faux.h>
  19. #include "base.h"
  20. /*
  21. * Internal wrapper structure so we can hold a pointer to the
  22. * faux_device_ops for this device.
  23. */
  24. struct faux_object {
  25. struct faux_device faux_dev;
  26. const struct faux_device_ops *faux_ops;
  27. const struct attribute_group **groups;
  28. };
  29. #define to_faux_object(dev) container_of_const(dev, struct faux_object, faux_dev.dev)
  30. static struct device *faux_bus_root;
  31. static int faux_match(struct device *dev, const struct device_driver *drv)
  32. {
  33. /* Match always succeeds, we only have one driver */
  34. return 1;
  35. }
  36. static int faux_probe(struct device *dev)
  37. {
  38. struct faux_object *faux_obj = to_faux_object(dev);
  39. struct faux_device *faux_dev = &faux_obj->faux_dev;
  40. const struct faux_device_ops *faux_ops = faux_obj->faux_ops;
  41. int ret;
  42. if (faux_ops && faux_ops->probe) {
  43. ret = faux_ops->probe(faux_dev);
  44. if (ret)
  45. return ret;
  46. }
  47. /*
  48. * Add groups after the probe succeeds to ensure resources are
  49. * initialized correctly
  50. */
  51. ret = device_add_groups(dev, faux_obj->groups);
  52. if (ret && faux_ops && faux_ops->remove)
  53. faux_ops->remove(faux_dev);
  54. return ret;
  55. }
  56. static void faux_remove(struct device *dev)
  57. {
  58. struct faux_object *faux_obj = to_faux_object(dev);
  59. struct faux_device *faux_dev = &faux_obj->faux_dev;
  60. const struct faux_device_ops *faux_ops = faux_obj->faux_ops;
  61. device_remove_groups(dev, faux_obj->groups);
  62. if (faux_ops && faux_ops->remove)
  63. faux_ops->remove(faux_dev);
  64. }
  65. static const struct bus_type faux_bus_type = {
  66. .name = "faux",
  67. .match = faux_match,
  68. .probe = faux_probe,
  69. .remove = faux_remove,
  70. };
  71. static struct device_driver faux_driver = {
  72. .name = "faux_driver",
  73. .bus = &faux_bus_type,
  74. .probe_type = PROBE_FORCE_SYNCHRONOUS,
  75. .suppress_bind_attrs = true,
  76. };
  77. static void faux_device_release(struct device *dev)
  78. {
  79. struct faux_object *faux_obj = to_faux_object(dev);
  80. kfree(faux_obj);
  81. }
  82. /**
  83. * faux_device_create_with_groups - Create and register with the driver
  84. * core a faux device and populate the device with an initial
  85. * set of sysfs attributes.
  86. * @name: The name of the device we are adding, must be unique for
  87. * all faux devices.
  88. * @parent: Pointer to a potential parent struct device. If set to
  89. * NULL, the device will be created in the "root" of the faux
  90. * device tree in sysfs.
  91. * @faux_ops: struct faux_device_ops that the new device will call back
  92. * into, can be NULL.
  93. * @groups: The set of sysfs attributes that will be created for this
  94. * device when it is registered with the driver core.
  95. *
  96. * Create a new faux device and register it in the driver core properly.
  97. * If present, callbacks in @faux_ops will be called with the device that
  98. * for the caller to do something with at the proper time given the
  99. * device's lifecycle.
  100. *
  101. * Note, when this function is called, the functions specified in struct
  102. * faux_ops can be called before the function returns, so be prepared for
  103. * everything to be properly initialized before that point in time. If the
  104. * probe callback (if one is present) does NOT succeed, the creation of the
  105. * device will fail and NULL will be returned.
  106. *
  107. * Return:
  108. * * NULL if an error happened with creating the device
  109. * * pointer to a valid struct faux_device that is registered with sysfs
  110. */
  111. struct faux_device *faux_device_create_with_groups(const char *name,
  112. struct device *parent,
  113. const struct faux_device_ops *faux_ops,
  114. const struct attribute_group **groups)
  115. {
  116. struct faux_object *faux_obj;
  117. struct faux_device *faux_dev;
  118. struct device *dev;
  119. int ret;
  120. faux_obj = kzalloc_obj(*faux_obj);
  121. if (!faux_obj)
  122. return NULL;
  123. /* Save off the callbacks and groups so we can use them in the future */
  124. faux_obj->faux_ops = faux_ops;
  125. faux_obj->groups = groups;
  126. /* Initialize the device portion and register it with the driver core */
  127. faux_dev = &faux_obj->faux_dev;
  128. dev = &faux_dev->dev;
  129. device_initialize(dev);
  130. dev->release = faux_device_release;
  131. if (parent)
  132. dev->parent = parent;
  133. else
  134. dev->parent = faux_bus_root;
  135. dev->bus = &faux_bus_type;
  136. dev_set_name(dev, "%s", name);
  137. device_set_pm_not_required(dev);
  138. ret = device_add(dev);
  139. if (ret) {
  140. pr_err("%s: device_add for faux device '%s' failed with %d\n",
  141. __func__, name, ret);
  142. put_device(dev);
  143. return NULL;
  144. }
  145. /*
  146. * Verify that we did bind the driver to the device (i.e. probe worked),
  147. * if not, let's fail the creation as trying to guess if probe was
  148. * successful is almost impossible to determine by the caller.
  149. */
  150. if (!dev->driver) {
  151. dev_dbg(dev, "probe did not succeed, tearing down the device\n");
  152. faux_device_destroy(faux_dev);
  153. faux_dev = NULL;
  154. }
  155. return faux_dev;
  156. }
  157. EXPORT_SYMBOL_GPL(faux_device_create_with_groups);
  158. /**
  159. * faux_device_create - create and register with the driver core a faux device
  160. * @name: The name of the device we are adding, must be unique for all
  161. * faux devices.
  162. * @parent: Pointer to a potential parent struct device. If set to
  163. * NULL, the device will be created in the "root" of the faux
  164. * device tree in sysfs.
  165. * @faux_ops: struct faux_device_ops that the new device will call back
  166. * into, can be NULL.
  167. *
  168. * Create a new faux device and register it in the driver core properly.
  169. * If present, callbacks in @faux_ops will be called with the device that
  170. * for the caller to do something with at the proper time given the
  171. * device's lifecycle.
  172. *
  173. * Note, when this function is called, the functions specified in struct
  174. * faux_ops can be called before the function returns, so be prepared for
  175. * everything to be properly initialized before that point in time.
  176. *
  177. * Return:
  178. * * NULL if an error happened with creating the device
  179. * * pointer to a valid struct faux_device that is registered with sysfs
  180. */
  181. struct faux_device *faux_device_create(const char *name,
  182. struct device *parent,
  183. const struct faux_device_ops *faux_ops)
  184. {
  185. return faux_device_create_with_groups(name, parent, faux_ops, NULL);
  186. }
  187. EXPORT_SYMBOL_GPL(faux_device_create);
  188. /**
  189. * faux_device_destroy - destroy a faux device
  190. * @faux_dev: faux device to destroy
  191. *
  192. * Unregisters and cleans up a device that was created with a call to
  193. * faux_device_create()
  194. */
  195. void faux_device_destroy(struct faux_device *faux_dev)
  196. {
  197. struct device *dev = &faux_dev->dev;
  198. if (!faux_dev)
  199. return;
  200. device_del(dev);
  201. /* The final put_device() will clean up the memory we allocated for this device. */
  202. put_device(dev);
  203. }
  204. EXPORT_SYMBOL_GPL(faux_device_destroy);
  205. int __init faux_bus_init(void)
  206. {
  207. int ret;
  208. faux_bus_root = kzalloc_obj(*faux_bus_root);
  209. if (!faux_bus_root)
  210. return -ENOMEM;
  211. dev_set_name(faux_bus_root, "faux");
  212. ret = device_register(faux_bus_root);
  213. if (ret) {
  214. put_device(faux_bus_root);
  215. return ret;
  216. }
  217. ret = bus_register(&faux_bus_type);
  218. if (ret)
  219. goto error_bus;
  220. ret = driver_register(&faux_driver);
  221. if (ret)
  222. goto error_driver;
  223. return ret;
  224. error_driver:
  225. bus_unregister(&faux_bus_type);
  226. error_bus:
  227. device_unregister(faux_bus_root);
  228. return ret;
  229. }