platform.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test managed platform driver
  4. */
  5. #include <linux/completion.h>
  6. #include <linux/device/bus.h>
  7. #include <linux/device/driver.h>
  8. #include <linux/platform_device.h>
  9. #include <kunit/platform_device.h>
  10. #include <kunit/resource.h>
  11. struct kunit_platform_device_alloc_params {
  12. const char *name;
  13. int id;
  14. };
  15. static int kunit_platform_device_alloc_init(struct kunit_resource *res, void *context)
  16. {
  17. struct kunit_platform_device_alloc_params *params = context;
  18. struct platform_device *pdev;
  19. pdev = platform_device_alloc(params->name, params->id);
  20. if (!pdev)
  21. return -ENOMEM;
  22. res->data = pdev;
  23. return 0;
  24. }
  25. static void kunit_platform_device_alloc_exit(struct kunit_resource *res)
  26. {
  27. struct platform_device *pdev = res->data;
  28. platform_device_put(pdev);
  29. }
  30. /**
  31. * kunit_platform_device_alloc() - Allocate a KUnit test managed platform device
  32. * @test: test context
  33. * @name: device name of platform device to alloc
  34. * @id: identifier of platform device to alloc.
  35. *
  36. * Allocate a test managed platform device. The device is put when the test completes.
  37. *
  38. * Return: Allocated platform device on success, NULL on failure.
  39. */
  40. struct platform_device *
  41. kunit_platform_device_alloc(struct kunit *test, const char *name, int id)
  42. {
  43. struct kunit_platform_device_alloc_params params = {
  44. .name = name,
  45. .id = id,
  46. };
  47. return kunit_alloc_resource(test,
  48. kunit_platform_device_alloc_init,
  49. kunit_platform_device_alloc_exit,
  50. GFP_KERNEL, &params);
  51. }
  52. EXPORT_SYMBOL_GPL(kunit_platform_device_alloc);
  53. static void kunit_platform_device_add_exit(struct kunit_resource *res)
  54. {
  55. struct platform_device *pdev = res->data;
  56. platform_device_unregister(pdev);
  57. }
  58. static bool
  59. kunit_platform_device_alloc_match(struct kunit *test,
  60. struct kunit_resource *res, void *match_data)
  61. {
  62. struct platform_device *pdev = match_data;
  63. return res->data == pdev && res->free == kunit_platform_device_alloc_exit;
  64. }
  65. KUNIT_DEFINE_ACTION_WRAPPER(platform_device_unregister_wrapper,
  66. platform_device_unregister, struct platform_device *);
  67. /**
  68. * kunit_platform_device_add() - Register a KUnit test managed platform device
  69. * @test: test context
  70. * @pdev: platform device to add
  71. *
  72. * Register a test managed platform device. The device is unregistered when the
  73. * test completes.
  74. *
  75. * Return: 0 on success, negative errno on failure.
  76. */
  77. int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev)
  78. {
  79. struct kunit_resource *res;
  80. int ret;
  81. ret = platform_device_add(pdev);
  82. if (ret)
  83. return ret;
  84. res = kunit_find_resource(test, kunit_platform_device_alloc_match, pdev);
  85. if (res) {
  86. /*
  87. * Transfer the reference count of the platform device if it
  88. * was allocated with kunit_platform_device_alloc(). In this
  89. * case, calling platform_device_put() when the test exits from
  90. * kunit_platform_device_alloc_exit() would lead to reference
  91. * count underflow because platform_device_unregister_wrapper()
  92. * calls platform_device_unregister() which also calls
  93. * platform_device_put().
  94. *
  95. * Usually callers transfer the refcount initialized in
  96. * platform_device_alloc() to platform_device_add() by calling
  97. * platform_device_unregister() when platform_device_add()
  98. * succeeds or platform_device_put() when it fails. KUnit has to
  99. * keep this straight by redirecting the free routine for the
  100. * resource to the right function. Luckily this only has to
  101. * account for the success scenario.
  102. */
  103. res->free = kunit_platform_device_add_exit;
  104. kunit_put_resource(res);
  105. } else {
  106. ret = kunit_add_action_or_reset(test, platform_device_unregister_wrapper, pdev);
  107. if (ret)
  108. return ret;
  109. }
  110. return 0;
  111. }
  112. EXPORT_SYMBOL_GPL(kunit_platform_device_add);
  113. struct kunit_platform_device_probe_nb {
  114. struct completion *x;
  115. struct device *dev;
  116. struct notifier_block nb;
  117. };
  118. static int kunit_platform_device_probe_notify(struct notifier_block *nb,
  119. unsigned long event, void *data)
  120. {
  121. struct kunit_platform_device_probe_nb *knb;
  122. struct device *dev = data;
  123. knb = container_of(nb, struct kunit_platform_device_probe_nb, nb);
  124. if (event != BUS_NOTIFY_BOUND_DRIVER || knb->dev != dev)
  125. return NOTIFY_DONE;
  126. complete(knb->x);
  127. return NOTIFY_OK;
  128. }
  129. static void kunit_platform_device_probe_nb_remove(void *nb)
  130. {
  131. bus_unregister_notifier(&platform_bus_type, nb);
  132. }
  133. /**
  134. * kunit_platform_device_prepare_wait_for_probe() - Prepare a completion
  135. * variable to wait for a platform device to probe
  136. * @test: test context
  137. * @pdev: platform device to prepare to wait for probe of
  138. * @x: completion variable completed when @dev has probed
  139. *
  140. * Prepare a completion variable @x to wait for @pdev to probe. Waiting on the
  141. * completion forces a preemption, allowing the platform driver to probe.
  142. *
  143. * Example
  144. *
  145. * .. code-block:: c
  146. *
  147. * static int kunit_platform_driver_probe(struct platform_device *pdev)
  148. * {
  149. * return 0;
  150. * }
  151. *
  152. * static void kunit_platform_driver_test(struct kunit *test)
  153. * {
  154. * struct platform_device *pdev;
  155. * struct platform_driver *pdrv;
  156. * DECLARE_COMPLETION_ONSTACK(comp);
  157. *
  158. * pdev = kunit_platform_device_alloc(test, "kunit-platform", -1);
  159. * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  160. * KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev));
  161. *
  162. * pdrv = kunit_kzalloc(test, sizeof(*pdrv), GFP_KERNEL);
  163. * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdrv);
  164. *
  165. * pdrv->probe = kunit_platform_driver_probe;
  166. * pdrv->driver.name = "kunit-platform";
  167. * pdrv->driver.owner = THIS_MODULE;
  168. *
  169. * KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp));
  170. * KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, pdrv));
  171. *
  172. * KUNIT_EXPECT_NE(test, 0, wait_for_completion_timeout(&comp, 3 * HZ));
  173. * }
  174. *
  175. * Return: 0 on success, negative errno on failure.
  176. */
  177. int kunit_platform_device_prepare_wait_for_probe(struct kunit *test,
  178. struct platform_device *pdev,
  179. struct completion *x)
  180. {
  181. struct device *dev = &pdev->dev;
  182. struct kunit_platform_device_probe_nb *knb;
  183. bool bound;
  184. knb = kunit_kzalloc(test, sizeof(*knb), GFP_KERNEL);
  185. if (!knb)
  186. return -ENOMEM;
  187. knb->nb.notifier_call = kunit_platform_device_probe_notify;
  188. knb->dev = dev;
  189. knb->x = x;
  190. device_lock(dev);
  191. bound = device_is_bound(dev);
  192. if (bound) {
  193. device_unlock(dev);
  194. complete(x);
  195. kunit_kfree(test, knb);
  196. return 0;
  197. }
  198. bus_register_notifier(&platform_bus_type, &knb->nb);
  199. device_unlock(&pdev->dev);
  200. return kunit_add_action_or_reset(test, kunit_platform_device_probe_nb_remove, &knb->nb);
  201. }
  202. EXPORT_SYMBOL_GPL(kunit_platform_device_prepare_wait_for_probe);
  203. KUNIT_DEFINE_ACTION_WRAPPER(platform_driver_unregister_wrapper,
  204. platform_driver_unregister, struct platform_driver *);
  205. /**
  206. * kunit_platform_driver_register() - Register a KUnit test managed platform driver
  207. * @test: test context
  208. * @drv: platform driver to register
  209. *
  210. * Register a test managed platform driver. This allows callers to embed the
  211. * @drv in a container structure and use container_of() in the probe function
  212. * to pass information to KUnit tests.
  213. *
  214. * Example
  215. *
  216. * .. code-block:: c
  217. *
  218. * struct kunit_test_context {
  219. * struct platform_driver pdrv;
  220. * const char *data;
  221. * };
  222. *
  223. * static inline struct kunit_test_context *
  224. * to_test_context(struct platform_device *pdev)
  225. * {
  226. * return container_of(to_platform_driver(pdev->dev.driver),
  227. * struct kunit_test_context,
  228. * pdrv);
  229. * }
  230. *
  231. * static int kunit_platform_driver_probe(struct platform_device *pdev)
  232. * {
  233. * struct kunit_test_context *ctx;
  234. *
  235. * ctx = to_test_context(pdev);
  236. * ctx->data = "test data";
  237. *
  238. * return 0;
  239. * }
  240. *
  241. * static void kunit_platform_driver_test(struct kunit *test)
  242. * {
  243. * struct kunit_test_context *ctx;
  244. *
  245. * ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  246. * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
  247. *
  248. * ctx->pdrv.probe = kunit_platform_driver_probe;
  249. * ctx->pdrv.driver.name = "kunit-platform";
  250. * ctx->pdrv.driver.owner = THIS_MODULE;
  251. *
  252. * KUNIT_EXPECT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
  253. * <... wait for driver to probe ...>
  254. * KUNIT_EXPECT_STREQ(test, ctx->data, "test data");
  255. * }
  256. *
  257. * Return: 0 on success, negative errno on failure.
  258. */
  259. int kunit_platform_driver_register(struct kunit *test,
  260. struct platform_driver *drv)
  261. {
  262. int ret;
  263. ret = platform_driver_register(drv);
  264. if (ret)
  265. return ret;
  266. return kunit_add_action_or_reset(test, platform_driver_unregister_wrapper, drv);
  267. }
  268. EXPORT_SYMBOL_GPL(kunit_platform_driver_register);