test-driver.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * (C) Copyright 2012
  5. * Pavel Herrmann <morpheus.ibis@gmail.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <malloc.h>
  13. #include <dm/test.h>
  14. #include <test/ut.h>
  15. #include <asm/io.h>
  16. int dm_testdrv_op_count[DM_TEST_OP_COUNT];
  17. static struct unit_test_state *uts = &global_dm_test_state;
  18. static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
  19. {
  20. const struct dm_test_pdata *pdata = dev_get_platdata(dev);
  21. struct dm_test_priv *priv = dev_get_priv(dev);
  22. *pingret = pingval + pdata->ping_add;
  23. priv->ping_total += *pingret;
  24. return 0;
  25. }
  26. static const struct test_ops test_ops = {
  27. .ping = testdrv_ping,
  28. };
  29. static int test_bind(struct udevice *dev)
  30. {
  31. /* Private data should not be allocated */
  32. ut_assert(!dev_get_priv(dev));
  33. dm_testdrv_op_count[DM_TEST_OP_BIND]++;
  34. return 0;
  35. }
  36. static int test_probe(struct udevice *dev)
  37. {
  38. struct dm_test_priv *priv = dev_get_priv(dev);
  39. /* Private data should be allocated */
  40. ut_assert(priv);
  41. dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
  42. priv->ping_total += DM_TEST_START_TOTAL;
  43. return 0;
  44. }
  45. static int test_remove(struct udevice *dev)
  46. {
  47. /* Private data should still be allocated */
  48. ut_assert(dev_get_priv(dev));
  49. dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
  50. return 0;
  51. }
  52. static int test_unbind(struct udevice *dev)
  53. {
  54. /* Private data should not be allocated */
  55. ut_assert(!dev->priv);
  56. dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
  57. return 0;
  58. }
  59. U_BOOT_DRIVER(test_drv) = {
  60. .name = "test_drv",
  61. .id = UCLASS_TEST,
  62. .ops = &test_ops,
  63. .bind = test_bind,
  64. .probe = test_probe,
  65. .remove = test_remove,
  66. .unbind = test_unbind,
  67. .priv_auto_alloc_size = sizeof(struct dm_test_priv),
  68. };
  69. U_BOOT_DRIVER(test2_drv) = {
  70. .name = "test2_drv",
  71. .id = UCLASS_TEST,
  72. .ops = &test_ops,
  73. .bind = test_bind,
  74. .probe = test_probe,
  75. .remove = test_remove,
  76. .unbind = test_unbind,
  77. .priv_auto_alloc_size = sizeof(struct dm_test_priv),
  78. };
  79. static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
  80. {
  81. *pingret = pingval + 2;
  82. return 0;
  83. }
  84. static const struct test_ops test_manual_ops = {
  85. .ping = test_manual_drv_ping,
  86. };
  87. static int test_manual_bind(struct udevice *dev)
  88. {
  89. dm_testdrv_op_count[DM_TEST_OP_BIND]++;
  90. return 0;
  91. }
  92. static int test_manual_probe(struct udevice *dev)
  93. {
  94. struct dm_test_state *dms = uts->priv;
  95. dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
  96. if (!dms->force_fail_alloc)
  97. dev->priv = calloc(1, sizeof(struct dm_test_priv));
  98. if (!dev->priv)
  99. return -ENOMEM;
  100. return 0;
  101. }
  102. static int test_manual_remove(struct udevice *dev)
  103. {
  104. dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
  105. return 0;
  106. }
  107. static int test_manual_unbind(struct udevice *dev)
  108. {
  109. dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
  110. return 0;
  111. }
  112. U_BOOT_DRIVER(test_manual_drv) = {
  113. .name = "test_manual_drv",
  114. .id = UCLASS_TEST,
  115. .ops = &test_manual_ops,
  116. .bind = test_manual_bind,
  117. .probe = test_manual_probe,
  118. .remove = test_manual_remove,
  119. .unbind = test_manual_unbind,
  120. };
  121. U_BOOT_DRIVER(test_pre_reloc_drv) = {
  122. .name = "test_pre_reloc_drv",
  123. .id = UCLASS_TEST,
  124. .ops = &test_manual_ops,
  125. .bind = test_manual_bind,
  126. .probe = test_manual_probe,
  127. .remove = test_manual_remove,
  128. .unbind = test_manual_unbind,
  129. .flags = DM_FLAG_PRE_RELOC,
  130. };