platform-device-test.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <kunit/platform_device.h>
  3. #include <kunit/resource.h>
  4. #include <linux/device.h>
  5. #include <linux/device/bus.h>
  6. #include <linux/of_platform.h>
  7. #include <linux/platform_device.h>
  8. #define DEVICE_NAME "test"
  9. struct test_priv {
  10. bool probe_done;
  11. bool release_done;
  12. wait_queue_head_t probe_wq;
  13. wait_queue_head_t release_wq;
  14. struct device *dev;
  15. };
  16. static int platform_device_devm_init(struct kunit *test)
  17. {
  18. struct test_priv *priv;
  19. priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
  20. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
  21. init_waitqueue_head(&priv->probe_wq);
  22. init_waitqueue_head(&priv->release_wq);
  23. test->priv = priv;
  24. return 0;
  25. }
  26. static void devm_device_action(void *ptr)
  27. {
  28. struct test_priv *priv = ptr;
  29. priv->release_done = true;
  30. wake_up_interruptible(&priv->release_wq);
  31. }
  32. static void devm_put_device_action(void *ptr)
  33. {
  34. struct test_priv *priv = ptr;
  35. put_device(priv->dev);
  36. priv->release_done = true;
  37. wake_up_interruptible(&priv->release_wq);
  38. }
  39. #define RELEASE_TIMEOUT_MS 100
  40. /*
  41. * Tests that a platform bus, non-probed device will run its
  42. * device-managed actions when unregistered.
  43. */
  44. static void platform_device_devm_register_unregister_test(struct kunit *test)
  45. {
  46. struct platform_device *pdev;
  47. struct test_priv *priv = test->priv;
  48. int ret;
  49. pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
  50. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  51. ret = platform_device_add(pdev);
  52. KUNIT_ASSERT_EQ(test, ret, 0);
  53. priv->dev = &pdev->dev;
  54. ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv);
  55. KUNIT_ASSERT_EQ(test, ret, 0);
  56. platform_device_unregister(pdev);
  57. ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
  58. msecs_to_jiffies(RELEASE_TIMEOUT_MS));
  59. KUNIT_EXPECT_GT(test, ret, 0);
  60. }
  61. /*
  62. * Tests that a platform bus, non-probed device will run its
  63. * device-managed actions when unregistered, even if someone still holds
  64. * a reference to it.
  65. */
  66. static void platform_device_devm_register_get_unregister_with_devm_test(struct kunit *test)
  67. {
  68. struct platform_device *pdev;
  69. struct test_priv *priv = test->priv;
  70. int ret;
  71. pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
  72. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  73. ret = platform_device_add(pdev);
  74. KUNIT_ASSERT_EQ(test, ret, 0);
  75. priv->dev = &pdev->dev;
  76. get_device(priv->dev);
  77. ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv);
  78. KUNIT_ASSERT_EQ(test, ret, 0);
  79. platform_device_unregister(pdev);
  80. ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
  81. msecs_to_jiffies(RELEASE_TIMEOUT_MS));
  82. KUNIT_EXPECT_GT(test, ret, 0);
  83. }
  84. static int fake_probe(struct platform_device *pdev)
  85. {
  86. struct test_priv *priv = platform_get_drvdata(pdev);
  87. priv->probe_done = true;
  88. wake_up_interruptible(&priv->probe_wq);
  89. return 0;
  90. }
  91. static struct platform_driver fake_driver = {
  92. .probe = fake_probe,
  93. .driver = {
  94. .name = DEVICE_NAME,
  95. },
  96. };
  97. /*
  98. * Tests that a platform bus, probed device will run its device-managed
  99. * actions when unregistered.
  100. */
  101. static void probed_platform_device_devm_register_unregister_test(struct kunit *test)
  102. {
  103. struct platform_device *pdev;
  104. struct test_priv *priv = test->priv;
  105. int ret;
  106. ret = platform_driver_register(&fake_driver);
  107. KUNIT_ASSERT_EQ(test, ret, 0);
  108. pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
  109. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  110. priv->dev = &pdev->dev;
  111. platform_set_drvdata(pdev, priv);
  112. ret = platform_device_add(pdev);
  113. KUNIT_ASSERT_EQ(test, ret, 0);
  114. ret = wait_event_interruptible_timeout(priv->probe_wq, priv->probe_done,
  115. msecs_to_jiffies(RELEASE_TIMEOUT_MS));
  116. KUNIT_ASSERT_GT(test, ret, 0);
  117. ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv);
  118. KUNIT_ASSERT_EQ(test, ret, 0);
  119. platform_device_unregister(pdev);
  120. ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
  121. msecs_to_jiffies(RELEASE_TIMEOUT_MS));
  122. KUNIT_EXPECT_GT(test, ret, 0);
  123. platform_driver_unregister(&fake_driver);
  124. }
  125. /*
  126. * Tests that a platform bus, probed device will run its device-managed
  127. * actions when unregistered, even if someone still holds a reference to
  128. * it.
  129. */
  130. static void probed_platform_device_devm_register_get_unregister_with_devm_test(struct kunit *test)
  131. {
  132. struct platform_device *pdev;
  133. struct test_priv *priv = test->priv;
  134. int ret;
  135. ret = platform_driver_register(&fake_driver);
  136. KUNIT_ASSERT_EQ(test, ret, 0);
  137. pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
  138. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  139. priv->dev = &pdev->dev;
  140. platform_set_drvdata(pdev, priv);
  141. ret = platform_device_add(pdev);
  142. KUNIT_ASSERT_EQ(test, ret, 0);
  143. ret = wait_event_interruptible_timeout(priv->probe_wq, priv->probe_done,
  144. msecs_to_jiffies(RELEASE_TIMEOUT_MS));
  145. KUNIT_ASSERT_GT(test, ret, 0);
  146. get_device(priv->dev);
  147. ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv);
  148. KUNIT_ASSERT_EQ(test, ret, 0);
  149. platform_device_unregister(pdev);
  150. ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
  151. msecs_to_jiffies(RELEASE_TIMEOUT_MS));
  152. KUNIT_EXPECT_GT(test, ret, 0);
  153. platform_driver_unregister(&fake_driver);
  154. }
  155. static struct kunit_case platform_device_devm_tests[] = {
  156. KUNIT_CASE(platform_device_devm_register_unregister_test),
  157. KUNIT_CASE(platform_device_devm_register_get_unregister_with_devm_test),
  158. KUNIT_CASE(probed_platform_device_devm_register_unregister_test),
  159. KUNIT_CASE(probed_platform_device_devm_register_get_unregister_with_devm_test),
  160. {}
  161. };
  162. static struct kunit_suite platform_device_devm_test_suite = {
  163. .name = "platform-device-devm",
  164. .init = platform_device_devm_init,
  165. .test_cases = platform_device_devm_tests,
  166. };
  167. static void platform_device_find_by_null_test(struct kunit *test)
  168. {
  169. struct platform_device *pdev;
  170. int ret;
  171. pdev = kunit_platform_device_alloc(test, DEVICE_NAME, PLATFORM_DEVID_NONE);
  172. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  173. ret = kunit_platform_device_add(test, pdev);
  174. KUNIT_ASSERT_EQ(test, ret, 0);
  175. KUNIT_EXPECT_PTR_EQ(test, of_find_device_by_node(NULL), NULL);
  176. KUNIT_EXPECT_PTR_EQ(test, bus_find_device_by_of_node(&platform_bus_type, NULL), NULL);
  177. KUNIT_EXPECT_PTR_EQ(test, bus_find_device_by_fwnode(&platform_bus_type, NULL), NULL);
  178. KUNIT_EXPECT_PTR_EQ(test, bus_find_device_by_acpi_dev(&platform_bus_type, NULL), NULL);
  179. KUNIT_EXPECT_FALSE(test, device_match_of_node(&pdev->dev, NULL));
  180. KUNIT_EXPECT_FALSE(test, device_match_fwnode(&pdev->dev, NULL));
  181. KUNIT_EXPECT_FALSE(test, device_match_acpi_dev(&pdev->dev, NULL));
  182. KUNIT_EXPECT_FALSE(test, device_match_acpi_handle(&pdev->dev, NULL));
  183. }
  184. static struct kunit_case platform_device_match_tests[] = {
  185. KUNIT_CASE(platform_device_find_by_null_test),
  186. {}
  187. };
  188. static struct kunit_suite platform_device_match_test_suite = {
  189. .name = "platform-device-match",
  190. .test_cases = platform_device_match_tests,
  191. };
  192. kunit_test_suites(
  193. &platform_device_devm_test_suite,
  194. &platform_device_match_test_suite,
  195. );
  196. MODULE_DESCRIPTION("Test module for platform devices");
  197. MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");
  198. MODULE_LICENSE("GPL");