device.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * KUnit basic device implementation
  4. *
  5. * Helpers for creating and managing fake devices for KUnit tests.
  6. *
  7. * Copyright (C) 2023, Google LLC.
  8. * Author: David Gow <davidgow@google.com>
  9. */
  10. #ifndef _KUNIT_DEVICE_H
  11. #define _KUNIT_DEVICE_H
  12. #if IS_ENABLED(CONFIG_KUNIT)
  13. #include <kunit/test.h>
  14. struct device;
  15. struct device_driver;
  16. /**
  17. * kunit_driver_create() - Create a struct device_driver attached to the kunit_bus
  18. * @test: The test context object.
  19. * @name: The name to give the created driver.
  20. *
  21. * Creates a struct device_driver attached to the kunit_bus, with the name @name.
  22. * This driver will automatically be cleaned up on test exit.
  23. *
  24. * Return: a stub struct device_driver, managed by KUnit, with the name @name.
  25. */
  26. struct device_driver *kunit_driver_create(struct kunit *test, const char *name);
  27. /**
  28. * kunit_device_register() - Create a struct device for use in KUnit tests
  29. * @test: The test context object.
  30. * @name: The name to give the created device.
  31. *
  32. * Creates a struct kunit_device (which is a struct device) with the given name,
  33. * and a corresponding driver. The device and driver will be cleaned up on test
  34. * exit, or when kunit_device_unregister is called. See also
  35. * kunit_device_register_with_driver, if you wish to provide your own
  36. * struct device_driver.
  37. *
  38. * Return: a pointer to a struct device which will be cleaned up when the test
  39. * exits, or an error pointer if the device could not be allocated or registered.
  40. */
  41. struct device *kunit_device_register(struct kunit *test, const char *name);
  42. /**
  43. * kunit_device_register_with_driver() - Create a struct device for use in KUnit tests
  44. * @test: The test context object.
  45. * @name: The name to give the created device.
  46. * @drv: The struct device_driver to associate with the device.
  47. *
  48. * Creates a struct kunit_device (which is a struct device) with the given
  49. * name, and driver. The device will be cleaned up on test exit, or when
  50. * kunit_device_unregister is called. See also kunit_device_register, if you
  51. * wish KUnit to create and manage a driver for you.
  52. *
  53. * Return: a pointer to a struct device which will be cleaned up when the test
  54. * exits, or an error pointer if the device could not be allocated or registered.
  55. */
  56. struct device *kunit_device_register_with_driver(struct kunit *test,
  57. const char *name,
  58. const struct device_driver *drv);
  59. /**
  60. * kunit_device_unregister() - Unregister a KUnit-managed device
  61. * @test: The test context object which created the device
  62. * @dev: The device.
  63. *
  64. * Unregisters and destroys a struct device which was created with
  65. * kunit_device_register or kunit_device_register_with_driver. If KUnit created
  66. * a driver, cleans it up as well.
  67. */
  68. void kunit_device_unregister(struct kunit *test, struct device *dev);
  69. #endif
  70. #endif