selftest.c 4.4 KB

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