selftest.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  34. selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  35. for (i = 0; i < 8; i++) {
  36. bool passed = true;
  37. rc = of_parse_phandle_with_args(np, "phandle-list",
  38. "#phandle-cells", i, &args);
  39. /* Test the values from tests-phandle.dtsi */
  40. switch (i) {
  41. case 0:
  42. passed &= !rc;
  43. passed &= (args.args_count == 1);
  44. passed &= (args.args[0] == (i + 1));
  45. break;
  46. case 1:
  47. passed &= !rc;
  48. passed &= (args.args_count == 2);
  49. passed &= (args.args[0] == (i + 1));
  50. passed &= (args.args[1] == 0);
  51. break;
  52. case 2:
  53. passed &= (rc == -ENOENT);
  54. break;
  55. case 3:
  56. passed &= !rc;
  57. passed &= (args.args_count == 3);
  58. passed &= (args.args[0] == (i + 1));
  59. passed &= (args.args[1] == 4);
  60. passed &= (args.args[2] == 3);
  61. break;
  62. case 4:
  63. passed &= !rc;
  64. passed &= (args.args_count == 2);
  65. passed &= (args.args[0] == (i + 1));
  66. passed &= (args.args[1] == 100);
  67. break;
  68. case 5:
  69. passed &= !rc;
  70. passed &= (args.args_count == 0);
  71. break;
  72. case 6:
  73. passed &= !rc;
  74. passed &= (args.args_count == 1);
  75. passed &= (args.args[0] == (i + 1));
  76. break;
  77. case 7:
  78. passed &= (rc == -ENOENT);
  79. break;
  80. default:
  81. passed = false;
  82. }
  83. selftest(passed, "index %i - data error on node %s rc=%i\n",
  84. i, args.np->full_name, rc);
  85. }
  86. /* Check for missing list property */
  87. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  88. "#phandle-cells", 0, &args);
  89. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  90. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  91. "#phandle-cells");
  92. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  93. /* Check for missing cells property */
  94. rc = of_parse_phandle_with_args(np, "phandle-list",
  95. "#phandle-cells-missing", 0, &args);
  96. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  97. rc = of_count_phandle_with_args(np, "phandle-list",
  98. "#phandle-cells-missing");
  99. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  100. /* Check for bad phandle in list */
  101. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  102. "#phandle-cells", 0, &args);
  103. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  104. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  105. "#phandle-cells");
  106. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  107. /* Check for incorrectly formed argument list */
  108. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  109. "#phandle-cells", 1, &args);
  110. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  111. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  112. "#phandle-cells");
  113. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  114. }
  115. static void __init of_selftest_property_match_string(void)
  116. {
  117. struct device_node *np;
  118. int rc;
  119. pr_info("start\n");
  120. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  121. if (!np) {
  122. pr_err("No testcase data in device tree\n");
  123. return;
  124. }
  125. rc = of_property_match_string(np, "phandle-list-names", "first");
  126. selftest(rc == 0, "first expected:0 got:%i\n", rc);
  127. rc = of_property_match_string(np, "phandle-list-names", "second");
  128. selftest(rc == 1, "second expected:0 got:%i\n", rc);
  129. rc = of_property_match_string(np, "phandle-list-names", "third");
  130. selftest(rc == 2, "third expected:0 got:%i\n", rc);
  131. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  132. selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
  133. rc = of_property_match_string(np, "missing-property", "blah");
  134. selftest(rc == -EINVAL, "missing property; rc=%i", rc);
  135. rc = of_property_match_string(np, "empty-property", "blah");
  136. selftest(rc == -ENODATA, "empty property; rc=%i", rc);
  137. rc = of_property_match_string(np, "unterminated-string", "blah");
  138. selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
  139. }
  140. static int __init of_selftest(void)
  141. {
  142. struct device_node *np;
  143. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  144. if (!np) {
  145. pr_info("No testcase data in device tree; not running tests\n");
  146. return 0;
  147. }
  148. of_node_put(np);
  149. pr_info("start of selftest - you will see error messages\n");
  150. of_selftest_parse_phandle_with_args();
  151. of_selftest_property_match_string();
  152. pr_info("end of selftest - %s\n", selftest_passed ? "PASS" : "FAIL");
  153. return 0;
  154. }
  155. late_initcall(of_selftest);