selftest.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Self tests for device tree subsystem
  3. */
  4. #define pr_fmt(fmt) "### dt-test ### " fmt
  5. #include <linux/clk.h>
  6. #include <linux/err.h>
  7. #include <linux/errno.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/list.h>
  11. #include <linux/mutex.h>
  12. #include <linux/slab.h>
  13. #include <linux/device.h>
  14. static bool selftest_passed = true;
  15. #define selftest(result, fmt, ...) { \
  16. if (!(result)) { \
  17. pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
  18. selftest_passed = false; \
  19. } else { \
  20. pr_info("pass %s:%i\n", __FILE__, __LINE__); \
  21. } \
  22. }
  23. static void __init of_selftest_parse_phandle_with_args(void)
  24. {
  25. struct device_node *np;
  26. struct of_phandle_args args;
  27. int i, rc;
  28. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  29. if (!np) {
  30. pr_err("missing testcase data\n");
  31. return;
  32. }
  33. for (i = 0; i < 8; i++) {
  34. bool passed = true;
  35. rc = of_parse_phandle_with_args(np, "phandle-list",
  36. "#phandle-cells", i, &args);
  37. /* Test the values from tests-phandle.dtsi */
  38. switch (i) {
  39. case 0:
  40. passed &= !rc;
  41. passed &= (args.args_count == 1);
  42. passed &= (args.args[0] == (i + 1));
  43. break;
  44. case 1:
  45. passed &= !rc;
  46. passed &= (args.args_count == 2);
  47. passed &= (args.args[0] == (i + 1));
  48. passed &= (args.args[1] == 0);
  49. break;
  50. case 2:
  51. passed &= (rc == -ENOENT);
  52. break;
  53. case 3:
  54. passed &= !rc;
  55. passed &= (args.args_count == 3);
  56. passed &= (args.args[0] == (i + 1));
  57. passed &= (args.args[1] == 4);
  58. passed &= (args.args[2] == 3);
  59. break;
  60. case 4:
  61. passed &= !rc;
  62. passed &= (args.args_count == 2);
  63. passed &= (args.args[0] == (i + 1));
  64. passed &= (args.args[1] == 100);
  65. break;
  66. case 5:
  67. passed &= !rc;
  68. passed &= (args.args_count == 0);
  69. break;
  70. case 6:
  71. passed &= !rc;
  72. passed &= (args.args_count == 1);
  73. passed &= (args.args[0] == (i + 1));
  74. break;
  75. case 7:
  76. passed &= (rc == -ENOENT);
  77. break;
  78. default:
  79. passed = false;
  80. }
  81. selftest(passed, "index %i - data error on node %s rc=%i\n",
  82. i, args.np->full_name, rc);
  83. }
  84. /* Check for missing list property */
  85. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  86. "#phandle-cells", 0, &args);
  87. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  88. /* Check for missing cells property */
  89. rc = of_parse_phandle_with_args(np, "phandle-list",
  90. "#phandle-cells-missing", 0, &args);
  91. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  92. /* Check for bad phandle in list */
  93. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  94. "#phandle-cells", 0, &args);
  95. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  96. /* Check for incorrectly formed argument list */
  97. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  98. "#phandle-cells", 1, &args);
  99. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  100. }
  101. static void __init of_selftest_property_match_string(void)
  102. {
  103. struct device_node *np;
  104. int rc;
  105. pr_info("start\n");
  106. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  107. if (!np) {
  108. pr_err("No testcase data in device tree\n");
  109. return;
  110. }
  111. rc = of_property_match_string(np, "phandle-list-names", "first");
  112. selftest(rc == 0, "first expected:0 got:%i\n", rc);
  113. rc = of_property_match_string(np, "phandle-list-names", "second");
  114. selftest(rc == 1, "second expected:0 got:%i\n", rc);
  115. rc = of_property_match_string(np, "phandle-list-names", "third");
  116. selftest(rc == 2, "third expected:0 got:%i\n", rc);
  117. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  118. selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
  119. rc = of_property_match_string(np, "missing-property", "blah");
  120. selftest(rc == -EINVAL, "missing property; rc=%i", rc);
  121. rc = of_property_match_string(np, "empty-property", "blah");
  122. selftest(rc == -ENODATA, "empty property; rc=%i", rc);
  123. rc = of_property_match_string(np, "unterminated-string", "blah");
  124. selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
  125. }
  126. static int __init of_selftest(void)
  127. {
  128. struct device_node *np;
  129. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  130. if (!np) {
  131. pr_info("No testcase data in device tree; not running tests\n");
  132. return 0;
  133. }
  134. of_node_put(np);
  135. pr_info("start of selftest - you will see error messages\n");
  136. of_selftest_parse_phandle_with_args();
  137. of_selftest_property_match_string();
  138. pr_info("end of selftest - %s\n", selftest_passed ? "PASS" : "FAIL");
  139. return 0;
  140. }
  141. late_initcall(of_selftest);