unittest.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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/hashtable.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_fdt.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/list.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/i2c.h>
  21. #include <linux/i2c-mux.h>
  22. #include <linux/bitops.h>
  23. #include "of_private.h"
  24. static struct unittest_results {
  25. int passed;
  26. int failed;
  27. } unittest_results;
  28. #define unittest(result, fmt, ...) ({ \
  29. bool failed = !(result); \
  30. if (failed) { \
  31. unittest_results.failed++; \
  32. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  33. } else { \
  34. unittest_results.passed++; \
  35. pr_debug("pass %s():%i\n", __func__, __LINE__); \
  36. } \
  37. failed; \
  38. })
  39. static void __init of_unittest_find_node_by_name(void)
  40. {
  41. struct device_node *np;
  42. const char *options;
  43. np = of_find_node_by_path("/testcase-data");
  44. unittest(np && !strcmp("/testcase-data", np->full_name),
  45. "find /testcase-data failed\n");
  46. of_node_put(np);
  47. /* Test if trailing '/' works */
  48. np = of_find_node_by_path("/testcase-data/");
  49. unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
  50. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  51. unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
  52. "find /testcase-data/phandle-tests/consumer-a failed\n");
  53. of_node_put(np);
  54. np = of_find_node_by_path("testcase-alias");
  55. unittest(np && !strcmp("/testcase-data", np->full_name),
  56. "find testcase-alias failed\n");
  57. of_node_put(np);
  58. /* Test if trailing '/' works on aliases */
  59. np = of_find_node_by_path("testcase-alias/");
  60. unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
  61. np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
  62. unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
  63. "find testcase-alias/phandle-tests/consumer-a failed\n");
  64. of_node_put(np);
  65. np = of_find_node_by_path("/testcase-data/missing-path");
  66. unittest(!np, "non-existent path returned node %s\n", np->full_name);
  67. of_node_put(np);
  68. np = of_find_node_by_path("missing-alias");
  69. unittest(!np, "non-existent alias returned node %s\n", np->full_name);
  70. of_node_put(np);
  71. np = of_find_node_by_path("testcase-alias/missing-path");
  72. unittest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
  73. of_node_put(np);
  74. np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
  75. unittest(np && !strcmp("testoption", options),
  76. "option path test failed\n");
  77. of_node_put(np);
  78. np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
  79. unittest(np && !strcmp("test/option", options),
  80. "option path test, subcase #1 failed\n");
  81. of_node_put(np);
  82. np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
  83. unittest(np && !strcmp("test/option", options),
  84. "option path test, subcase #2 failed\n");
  85. of_node_put(np);
  86. np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
  87. unittest(np, "NULL option path test failed\n");
  88. of_node_put(np);
  89. np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
  90. &options);
  91. unittest(np && !strcmp("testaliasoption", options),
  92. "option alias path test failed\n");
  93. of_node_put(np);
  94. np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
  95. &options);
  96. unittest(np && !strcmp("test/alias/option", options),
  97. "option alias path test, subcase #1 failed\n");
  98. of_node_put(np);
  99. np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
  100. unittest(np, "NULL option alias path test failed\n");
  101. of_node_put(np);
  102. options = "testoption";
  103. np = of_find_node_opts_by_path("testcase-alias", &options);
  104. unittest(np && !options, "option clearing test failed\n");
  105. of_node_put(np);
  106. options = "testoption";
  107. np = of_find_node_opts_by_path("/", &options);
  108. unittest(np && !options, "option clearing root node test failed\n");
  109. of_node_put(np);
  110. }
  111. static void __init of_unittest_dynamic(void)
  112. {
  113. struct device_node *np;
  114. struct property *prop;
  115. np = of_find_node_by_path("/testcase-data");
  116. if (!np) {
  117. pr_err("missing testcase data\n");
  118. return;
  119. }
  120. /* Array of 4 properties for the purpose of testing */
  121. prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
  122. if (!prop) {
  123. unittest(0, "kzalloc() failed\n");
  124. return;
  125. }
  126. /* Add a new property - should pass*/
  127. prop->name = "new-property";
  128. prop->value = "new-property-data";
  129. prop->length = strlen(prop->value);
  130. unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
  131. /* Try to add an existing property - should fail */
  132. prop++;
  133. prop->name = "new-property";
  134. prop->value = "new-property-data-should-fail";
  135. prop->length = strlen(prop->value);
  136. unittest(of_add_property(np, prop) != 0,
  137. "Adding an existing property should have failed\n");
  138. /* Try to modify an existing property - should pass */
  139. prop->value = "modify-property-data-should-pass";
  140. prop->length = strlen(prop->value);
  141. unittest(of_update_property(np, prop) == 0,
  142. "Updating an existing property should have passed\n");
  143. /* Try to modify non-existent property - should pass*/
  144. prop++;
  145. prop->name = "modify-property";
  146. prop->value = "modify-missing-property-data-should-pass";
  147. prop->length = strlen(prop->value);
  148. unittest(of_update_property(np, prop) == 0,
  149. "Updating a missing property should have passed\n");
  150. /* Remove property - should pass */
  151. unittest(of_remove_property(np, prop) == 0,
  152. "Removing a property should have passed\n");
  153. /* Adding very large property - should pass */
  154. prop++;
  155. prop->name = "large-property-PAGE_SIZEx8";
  156. prop->length = PAGE_SIZE * 8;
  157. prop->value = kzalloc(prop->length, GFP_KERNEL);
  158. unittest(prop->value != NULL, "Unable to allocate large buffer\n");
  159. if (prop->value)
  160. unittest(of_add_property(np, prop) == 0,
  161. "Adding a large property should have passed\n");
  162. }
  163. static int __init of_unittest_check_node_linkage(struct device_node *np)
  164. {
  165. struct device_node *child;
  166. int count = 0, rc;
  167. for_each_child_of_node(np, child) {
  168. if (child->parent != np) {
  169. pr_err("Child node %s links to wrong parent %s\n",
  170. child->name, np->name);
  171. return -EINVAL;
  172. }
  173. rc = of_unittest_check_node_linkage(child);
  174. if (rc < 0)
  175. return rc;
  176. count += rc;
  177. }
  178. return count + 1;
  179. }
  180. static void __init of_unittest_check_tree_linkage(void)
  181. {
  182. struct device_node *np;
  183. int allnode_count = 0, child_count;
  184. if (!of_root)
  185. return;
  186. for_each_of_allnodes(np)
  187. allnode_count++;
  188. child_count = of_unittest_check_node_linkage(of_root);
  189. unittest(child_count > 0, "Device node data structure is corrupted\n");
  190. unittest(child_count == allnode_count,
  191. "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
  192. allnode_count, child_count);
  193. pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
  194. }
  195. struct node_hash {
  196. struct hlist_node node;
  197. struct device_node *np;
  198. };
  199. static DEFINE_HASHTABLE(phandle_ht, 8);
  200. static void __init of_unittest_check_phandles(void)
  201. {
  202. struct device_node *np;
  203. struct node_hash *nh;
  204. struct hlist_node *tmp;
  205. int i, dup_count = 0, phandle_count = 0;
  206. for_each_of_allnodes(np) {
  207. if (!np->phandle)
  208. continue;
  209. hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
  210. if (nh->np->phandle == np->phandle) {
  211. pr_info("Duplicate phandle! %i used by %s and %s\n",
  212. np->phandle, nh->np->full_name, np->full_name);
  213. dup_count++;
  214. break;
  215. }
  216. }
  217. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  218. if (WARN_ON(!nh))
  219. return;
  220. nh->np = np;
  221. hash_add(phandle_ht, &nh->node, np->phandle);
  222. phandle_count++;
  223. }
  224. unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
  225. dup_count, phandle_count);
  226. /* Clean up */
  227. hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
  228. hash_del(&nh->node);
  229. kfree(nh);
  230. }
  231. }
  232. static void __init of_unittest_parse_phandle_with_args(void)
  233. {
  234. struct device_node *np;
  235. struct of_phandle_args args;
  236. int i, rc;
  237. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  238. if (!np) {
  239. pr_err("missing testcase data\n");
  240. return;
  241. }
  242. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  243. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  244. for (i = 0; i < 8; i++) {
  245. bool passed = true;
  246. rc = of_parse_phandle_with_args(np, "phandle-list",
  247. "#phandle-cells", i, &args);
  248. /* Test the values from tests-phandle.dtsi */
  249. switch (i) {
  250. case 0:
  251. passed &= !rc;
  252. passed &= (args.args_count == 1);
  253. passed &= (args.args[0] == (i + 1));
  254. break;
  255. case 1:
  256. passed &= !rc;
  257. passed &= (args.args_count == 2);
  258. passed &= (args.args[0] == (i + 1));
  259. passed &= (args.args[1] == 0);
  260. break;
  261. case 2:
  262. passed &= (rc == -ENOENT);
  263. break;
  264. case 3:
  265. passed &= !rc;
  266. passed &= (args.args_count == 3);
  267. passed &= (args.args[0] == (i + 1));
  268. passed &= (args.args[1] == 4);
  269. passed &= (args.args[2] == 3);
  270. break;
  271. case 4:
  272. passed &= !rc;
  273. passed &= (args.args_count == 2);
  274. passed &= (args.args[0] == (i + 1));
  275. passed &= (args.args[1] == 100);
  276. break;
  277. case 5:
  278. passed &= !rc;
  279. passed &= (args.args_count == 0);
  280. break;
  281. case 6:
  282. passed &= !rc;
  283. passed &= (args.args_count == 1);
  284. passed &= (args.args[0] == (i + 1));
  285. break;
  286. case 7:
  287. passed &= (rc == -ENOENT);
  288. break;
  289. default:
  290. passed = false;
  291. }
  292. unittest(passed, "index %i - data error on node %s rc=%i\n",
  293. i, args.np->full_name, rc);
  294. }
  295. /* Check for missing list property */
  296. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  297. "#phandle-cells", 0, &args);
  298. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  299. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  300. "#phandle-cells");
  301. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  302. /* Check for missing cells property */
  303. rc = of_parse_phandle_with_args(np, "phandle-list",
  304. "#phandle-cells-missing", 0, &args);
  305. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  306. rc = of_count_phandle_with_args(np, "phandle-list",
  307. "#phandle-cells-missing");
  308. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  309. /* Check for bad phandle in list */
  310. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  311. "#phandle-cells", 0, &args);
  312. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  313. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  314. "#phandle-cells");
  315. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  316. /* Check for incorrectly formed argument list */
  317. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  318. "#phandle-cells", 1, &args);
  319. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  320. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  321. "#phandle-cells");
  322. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  323. }
  324. static void __init of_unittest_property_string(void)
  325. {
  326. const char *strings[4];
  327. struct device_node *np;
  328. int rc;
  329. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  330. if (!np) {
  331. pr_err("No testcase data in device tree\n");
  332. return;
  333. }
  334. rc = of_property_match_string(np, "phandle-list-names", "first");
  335. unittest(rc == 0, "first expected:0 got:%i\n", rc);
  336. rc = of_property_match_string(np, "phandle-list-names", "second");
  337. unittest(rc == 1, "second expected:1 got:%i\n", rc);
  338. rc = of_property_match_string(np, "phandle-list-names", "third");
  339. unittest(rc == 2, "third expected:2 got:%i\n", rc);
  340. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  341. unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  342. rc = of_property_match_string(np, "missing-property", "blah");
  343. unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  344. rc = of_property_match_string(np, "empty-property", "blah");
  345. unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  346. rc = of_property_match_string(np, "unterminated-string", "blah");
  347. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  348. /* of_property_count_strings() tests */
  349. rc = of_property_count_strings(np, "string-property");
  350. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  351. rc = of_property_count_strings(np, "phandle-list-names");
  352. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  353. rc = of_property_count_strings(np, "unterminated-string");
  354. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  355. rc = of_property_count_strings(np, "unterminated-string-list");
  356. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  357. /* of_property_read_string_index() tests */
  358. rc = of_property_read_string_index(np, "string-property", 0, strings);
  359. unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  360. strings[0] = NULL;
  361. rc = of_property_read_string_index(np, "string-property", 1, strings);
  362. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  363. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  364. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  365. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  366. unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  367. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  368. unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  369. strings[0] = NULL;
  370. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  371. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  372. strings[0] = NULL;
  373. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  374. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  375. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  376. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  377. strings[0] = NULL;
  378. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  379. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  380. strings[1] = NULL;
  381. /* of_property_read_string_array() tests */
  382. rc = of_property_read_string_array(np, "string-property", strings, 4);
  383. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  384. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  385. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  386. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  387. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  388. /* -- An incorrectly formed string should cause a failure */
  389. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  390. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  391. /* -- parsing the correctly formed strings should still work: */
  392. strings[2] = NULL;
  393. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  394. unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  395. strings[1] = NULL;
  396. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  397. unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  398. }
  399. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  400. (p1)->value && (p2)->value && \
  401. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  402. !strcmp((p1)->name, (p2)->name))
  403. static void __init of_unittest_property_copy(void)
  404. {
  405. #ifdef CONFIG_OF_DYNAMIC
  406. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  407. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  408. struct property *new;
  409. new = __of_prop_dup(&p1, GFP_KERNEL);
  410. unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  411. kfree(new->value);
  412. kfree(new->name);
  413. kfree(new);
  414. new = __of_prop_dup(&p2, GFP_KERNEL);
  415. unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  416. kfree(new->value);
  417. kfree(new->name);
  418. kfree(new);
  419. #endif
  420. }
  421. static void __init of_unittest_changeset(void)
  422. {
  423. #ifdef CONFIG_OF_DYNAMIC
  424. struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
  425. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  426. struct property *ppremove;
  427. struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
  428. struct of_changeset chgset;
  429. n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
  430. unittest(n1, "testcase setup failure\n");
  431. n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
  432. unittest(n2, "testcase setup failure\n");
  433. n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
  434. unittest(n21, "testcase setup failure %p\n", n21);
  435. nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
  436. unittest(nremove, "testcase setup failure\n");
  437. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  438. unittest(ppadd, "testcase setup failure\n");
  439. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  440. unittest(ppupdate, "testcase setup failure\n");
  441. parent = nremove->parent;
  442. n1->parent = parent;
  443. n2->parent = parent;
  444. n21->parent = n2;
  445. n2->child = n21;
  446. ppremove = of_find_property(parent, "prop-remove", NULL);
  447. unittest(ppremove, "failed to find removal prop");
  448. of_changeset_init(&chgset);
  449. unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  450. unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  451. unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  452. unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  453. unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
  454. unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  455. unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  456. mutex_lock(&of_mutex);
  457. unittest(!of_changeset_apply(&chgset), "apply failed\n");
  458. mutex_unlock(&of_mutex);
  459. /* Make sure node names are constructed correctly */
  460. unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
  461. "'%s' not added\n", n21->full_name);
  462. of_node_put(np);
  463. mutex_lock(&of_mutex);
  464. unittest(!of_changeset_revert(&chgset), "revert failed\n");
  465. mutex_unlock(&of_mutex);
  466. of_changeset_destroy(&chgset);
  467. #endif
  468. }
  469. static void __init of_unittest_parse_interrupts(void)
  470. {
  471. struct device_node *np;
  472. struct of_phandle_args args;
  473. int i, rc;
  474. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  475. if (!np) {
  476. pr_err("missing testcase data\n");
  477. return;
  478. }
  479. for (i = 0; i < 4; i++) {
  480. bool passed = true;
  481. args.args_count = 0;
  482. rc = of_irq_parse_one(np, i, &args);
  483. passed &= !rc;
  484. passed &= (args.args_count == 1);
  485. passed &= (args.args[0] == (i + 1));
  486. unittest(passed, "index %i - data error on node %s rc=%i\n",
  487. i, args.np->full_name, rc);
  488. }
  489. of_node_put(np);
  490. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  491. if (!np) {
  492. pr_err("missing testcase data\n");
  493. return;
  494. }
  495. for (i = 0; i < 4; i++) {
  496. bool passed = true;
  497. args.args_count = 0;
  498. rc = of_irq_parse_one(np, i, &args);
  499. /* Test the values from tests-phandle.dtsi */
  500. switch (i) {
  501. case 0:
  502. passed &= !rc;
  503. passed &= (args.args_count == 1);
  504. passed &= (args.args[0] == 9);
  505. break;
  506. case 1:
  507. passed &= !rc;
  508. passed &= (args.args_count == 3);
  509. passed &= (args.args[0] == 10);
  510. passed &= (args.args[1] == 11);
  511. passed &= (args.args[2] == 12);
  512. break;
  513. case 2:
  514. passed &= !rc;
  515. passed &= (args.args_count == 2);
  516. passed &= (args.args[0] == 13);
  517. passed &= (args.args[1] == 14);
  518. break;
  519. case 3:
  520. passed &= !rc;
  521. passed &= (args.args_count == 2);
  522. passed &= (args.args[0] == 15);
  523. passed &= (args.args[1] == 16);
  524. break;
  525. default:
  526. passed = false;
  527. }
  528. unittest(passed, "index %i - data error on node %s rc=%i\n",
  529. i, args.np->full_name, rc);
  530. }
  531. of_node_put(np);
  532. }
  533. static void __init of_unittest_parse_interrupts_extended(void)
  534. {
  535. struct device_node *np;
  536. struct of_phandle_args args;
  537. int i, rc;
  538. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  539. if (!np) {
  540. pr_err("missing testcase data\n");
  541. return;
  542. }
  543. for (i = 0; i < 7; i++) {
  544. bool passed = true;
  545. rc = of_irq_parse_one(np, i, &args);
  546. /* Test the values from tests-phandle.dtsi */
  547. switch (i) {
  548. case 0:
  549. passed &= !rc;
  550. passed &= (args.args_count == 1);
  551. passed &= (args.args[0] == 1);
  552. break;
  553. case 1:
  554. passed &= !rc;
  555. passed &= (args.args_count == 3);
  556. passed &= (args.args[0] == 2);
  557. passed &= (args.args[1] == 3);
  558. passed &= (args.args[2] == 4);
  559. break;
  560. case 2:
  561. passed &= !rc;
  562. passed &= (args.args_count == 2);
  563. passed &= (args.args[0] == 5);
  564. passed &= (args.args[1] == 6);
  565. break;
  566. case 3:
  567. passed &= !rc;
  568. passed &= (args.args_count == 1);
  569. passed &= (args.args[0] == 9);
  570. break;
  571. case 4:
  572. passed &= !rc;
  573. passed &= (args.args_count == 3);
  574. passed &= (args.args[0] == 10);
  575. passed &= (args.args[1] == 11);
  576. passed &= (args.args[2] == 12);
  577. break;
  578. case 5:
  579. passed &= !rc;
  580. passed &= (args.args_count == 2);
  581. passed &= (args.args[0] == 13);
  582. passed &= (args.args[1] == 14);
  583. break;
  584. case 6:
  585. passed &= !rc;
  586. passed &= (args.args_count == 1);
  587. passed &= (args.args[0] == 15);
  588. break;
  589. default:
  590. passed = false;
  591. }
  592. unittest(passed, "index %i - data error on node %s rc=%i\n",
  593. i, args.np->full_name, rc);
  594. }
  595. of_node_put(np);
  596. }
  597. static const struct of_device_id match_node_table[] = {
  598. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  599. { .data = "B", .type = "type1", }, /* followed by type alone */
  600. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  601. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  602. { .data = "Cc", .name = "name2", .type = "type2", },
  603. { .data = "E", .compatible = "compat3" },
  604. { .data = "G", .compatible = "compat2", },
  605. { .data = "H", .compatible = "compat2", .name = "name5", },
  606. { .data = "I", .compatible = "compat2", .type = "type1", },
  607. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  608. { .data = "K", .compatible = "compat2", .name = "name9", },
  609. {}
  610. };
  611. static struct {
  612. const char *path;
  613. const char *data;
  614. } match_node_tests[] = {
  615. { .path = "/testcase-data/match-node/name0", .data = "A", },
  616. { .path = "/testcase-data/match-node/name1", .data = "B", },
  617. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  618. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  619. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  620. { .path = "/testcase-data/match-node/name3", .data = "E", },
  621. { .path = "/testcase-data/match-node/name4", .data = "G", },
  622. { .path = "/testcase-data/match-node/name5", .data = "H", },
  623. { .path = "/testcase-data/match-node/name6", .data = "G", },
  624. { .path = "/testcase-data/match-node/name7", .data = "I", },
  625. { .path = "/testcase-data/match-node/name8", .data = "J", },
  626. { .path = "/testcase-data/match-node/name9", .data = "K", },
  627. };
  628. static void __init of_unittest_match_node(void)
  629. {
  630. struct device_node *np;
  631. const struct of_device_id *match;
  632. int i;
  633. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  634. np = of_find_node_by_path(match_node_tests[i].path);
  635. if (!np) {
  636. unittest(0, "missing testcase node %s\n",
  637. match_node_tests[i].path);
  638. continue;
  639. }
  640. match = of_match_node(match_node_table, np);
  641. if (!match) {
  642. unittest(0, "%s didn't match anything\n",
  643. match_node_tests[i].path);
  644. continue;
  645. }
  646. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  647. unittest(0, "%s got wrong match. expected %s, got %s\n",
  648. match_node_tests[i].path, match_node_tests[i].data,
  649. (const char *)match->data);
  650. continue;
  651. }
  652. unittest(1, "passed");
  653. }
  654. }
  655. static const struct platform_device_info test_bus_info = {
  656. .name = "unittest-bus",
  657. };
  658. static void __init of_unittest_platform_populate(void)
  659. {
  660. int irq, rc;
  661. struct device_node *np, *child, *grandchild;
  662. struct platform_device *pdev, *test_bus;
  663. const struct of_device_id match[] = {
  664. { .compatible = "test-device", },
  665. {}
  666. };
  667. np = of_find_node_by_path("/testcase-data");
  668. of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
  669. /* Test that a missing irq domain returns -EPROBE_DEFER */
  670. np = of_find_node_by_path("/testcase-data/testcase-device1");
  671. pdev = of_find_device_by_node(np);
  672. unittest(pdev, "device 1 creation failed\n");
  673. irq = platform_get_irq(pdev, 0);
  674. unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
  675. /* Test that a parsing failure does not return -EPROBE_DEFER */
  676. np = of_find_node_by_path("/testcase-data/testcase-device2");
  677. pdev = of_find_device_by_node(np);
  678. unittest(pdev, "device 2 creation failed\n");
  679. irq = platform_get_irq(pdev, 0);
  680. unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
  681. np = of_find_node_by_path("/testcase-data/platform-tests");
  682. unittest(np, "No testcase data in device tree\n");
  683. if (!np)
  684. return;
  685. test_bus = platform_device_register_full(&test_bus_info);
  686. rc = PTR_ERR_OR_ZERO(test_bus);
  687. unittest(!rc, "testbus registration failed; rc=%i\n", rc);
  688. if (rc)
  689. return;
  690. test_bus->dev.of_node = np;
  691. of_platform_populate(np, match, NULL, &test_bus->dev);
  692. for_each_child_of_node(np, child) {
  693. for_each_child_of_node(child, grandchild)
  694. unittest(of_find_device_by_node(grandchild),
  695. "Could not create device for node '%s'\n",
  696. grandchild->name);
  697. }
  698. of_platform_depopulate(&test_bus->dev);
  699. for_each_child_of_node(np, child) {
  700. for_each_child_of_node(child, grandchild)
  701. unittest(!of_find_device_by_node(grandchild),
  702. "device didn't get destroyed '%s'\n",
  703. grandchild->name);
  704. }
  705. platform_device_unregister(test_bus);
  706. of_node_put(np);
  707. }
  708. /**
  709. * update_node_properties - adds the properties
  710. * of np into dup node (present in live tree) and
  711. * updates parent of children of np to dup.
  712. *
  713. * @np: node already present in live tree
  714. * @dup: node present in live tree to be updated
  715. */
  716. static void update_node_properties(struct device_node *np,
  717. struct device_node *dup)
  718. {
  719. struct property *prop;
  720. struct device_node *child;
  721. for_each_property_of_node(np, prop)
  722. of_add_property(dup, prop);
  723. for_each_child_of_node(np, child)
  724. child->parent = dup;
  725. }
  726. /**
  727. * attach_node_and_children - attaches nodes
  728. * and its children to live tree
  729. *
  730. * @np: Node to attach to live tree
  731. */
  732. static int attach_node_and_children(struct device_node *np)
  733. {
  734. struct device_node *next, *dup, *child;
  735. unsigned long flags;
  736. dup = of_find_node_by_path(np->full_name);
  737. if (dup) {
  738. update_node_properties(np, dup);
  739. return 0;
  740. }
  741. child = np->child;
  742. np->child = NULL;
  743. mutex_lock(&of_mutex);
  744. raw_spin_lock_irqsave(&devtree_lock, flags);
  745. np->sibling = np->parent->child;
  746. np->parent->child = np;
  747. of_node_clear_flag(np, OF_DETACHED);
  748. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  749. __of_attach_node_sysfs(np);
  750. mutex_unlock(&of_mutex);
  751. while (child) {
  752. next = child->sibling;
  753. attach_node_and_children(child);
  754. child = next;
  755. }
  756. return 0;
  757. }
  758. /**
  759. * unittest_data_add - Reads, copies data from
  760. * linked tree and attaches it to the live tree
  761. */
  762. static int __init unittest_data_add(void)
  763. {
  764. void *unittest_data;
  765. struct device_node *unittest_data_node, *np;
  766. /*
  767. * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
  768. * created by cmd_dt_S_dtb in scripts/Makefile.lib
  769. */
  770. extern uint8_t __dtb_testcases_begin[];
  771. extern uint8_t __dtb_testcases_end[];
  772. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  773. int rc;
  774. if (!size) {
  775. pr_warn("%s: No testcase data to attach; not running tests\n",
  776. __func__);
  777. return -ENODATA;
  778. }
  779. /* creating copy */
  780. unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  781. if (!unittest_data) {
  782. pr_warn("%s: Failed to allocate memory for unittest_data; "
  783. "not running tests\n", __func__);
  784. return -ENOMEM;
  785. }
  786. of_fdt_unflatten_tree(unittest_data, &unittest_data_node);
  787. if (!unittest_data_node) {
  788. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  789. return -ENODATA;
  790. }
  791. of_node_set_flag(unittest_data_node, OF_DETACHED);
  792. rc = of_resolve_phandles(unittest_data_node);
  793. if (rc) {
  794. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  795. return -EINVAL;
  796. }
  797. if (!of_root) {
  798. of_root = unittest_data_node;
  799. for_each_of_allnodes(np)
  800. __of_attach_node_sysfs(np);
  801. of_aliases = of_find_node_by_path("/aliases");
  802. of_chosen = of_find_node_by_path("/chosen");
  803. return 0;
  804. }
  805. /* attach the sub-tree to live tree */
  806. np = unittest_data_node->child;
  807. while (np) {
  808. struct device_node *next = np->sibling;
  809. np->parent = of_root;
  810. attach_node_and_children(np);
  811. np = next;
  812. }
  813. return 0;
  814. }
  815. #ifdef CONFIG_OF_OVERLAY
  816. static int unittest_probe(struct platform_device *pdev)
  817. {
  818. struct device *dev = &pdev->dev;
  819. struct device_node *np = dev->of_node;
  820. if (np == NULL) {
  821. dev_err(dev, "No OF data for device\n");
  822. return -EINVAL;
  823. }
  824. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  825. of_platform_populate(np, NULL, NULL, &pdev->dev);
  826. return 0;
  827. }
  828. static int unittest_remove(struct platform_device *pdev)
  829. {
  830. struct device *dev = &pdev->dev;
  831. struct device_node *np = dev->of_node;
  832. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  833. return 0;
  834. }
  835. static const struct of_device_id unittest_match[] = {
  836. { .compatible = "unittest", },
  837. {},
  838. };
  839. static struct platform_driver unittest_driver = {
  840. .probe = unittest_probe,
  841. .remove = unittest_remove,
  842. .driver = {
  843. .name = "unittest",
  844. .owner = THIS_MODULE,
  845. .of_match_table = of_match_ptr(unittest_match),
  846. },
  847. };
  848. /* get the platform device instantiated at the path */
  849. static struct platform_device *of_path_to_platform_device(const char *path)
  850. {
  851. struct device_node *np;
  852. struct platform_device *pdev;
  853. np = of_find_node_by_path(path);
  854. if (np == NULL)
  855. return NULL;
  856. pdev = of_find_device_by_node(np);
  857. of_node_put(np);
  858. return pdev;
  859. }
  860. /* find out if a platform device exists at that path */
  861. static int of_path_platform_device_exists(const char *path)
  862. {
  863. struct platform_device *pdev;
  864. pdev = of_path_to_platform_device(path);
  865. platform_device_put(pdev);
  866. return pdev != NULL;
  867. }
  868. #if IS_BUILTIN(CONFIG_I2C)
  869. /* get the i2c client device instantiated at the path */
  870. static struct i2c_client *of_path_to_i2c_client(const char *path)
  871. {
  872. struct device_node *np;
  873. struct i2c_client *client;
  874. np = of_find_node_by_path(path);
  875. if (np == NULL)
  876. return NULL;
  877. client = of_find_i2c_device_by_node(np);
  878. of_node_put(np);
  879. return client;
  880. }
  881. /* find out if a i2c client device exists at that path */
  882. static int of_path_i2c_client_exists(const char *path)
  883. {
  884. struct i2c_client *client;
  885. client = of_path_to_i2c_client(path);
  886. if (client)
  887. put_device(&client->dev);
  888. return client != NULL;
  889. }
  890. #else
  891. static int of_path_i2c_client_exists(const char *path)
  892. {
  893. return 0;
  894. }
  895. #endif
  896. enum overlay_type {
  897. PDEV_OVERLAY,
  898. I2C_OVERLAY
  899. };
  900. static int of_path_device_type_exists(const char *path,
  901. enum overlay_type ovtype)
  902. {
  903. switch (ovtype) {
  904. case PDEV_OVERLAY:
  905. return of_path_platform_device_exists(path);
  906. case I2C_OVERLAY:
  907. return of_path_i2c_client_exists(path);
  908. }
  909. return 0;
  910. }
  911. static const char *unittest_path(int nr, enum overlay_type ovtype)
  912. {
  913. const char *base;
  914. static char buf[256];
  915. switch (ovtype) {
  916. case PDEV_OVERLAY:
  917. base = "/testcase-data/overlay-node/test-bus";
  918. break;
  919. case I2C_OVERLAY:
  920. base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
  921. break;
  922. default:
  923. buf[0] = '\0';
  924. return buf;
  925. }
  926. snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
  927. buf[sizeof(buf) - 1] = '\0';
  928. return buf;
  929. }
  930. static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
  931. {
  932. const char *path;
  933. path = unittest_path(unittest_nr, ovtype);
  934. switch (ovtype) {
  935. case PDEV_OVERLAY:
  936. return of_path_platform_device_exists(path);
  937. case I2C_OVERLAY:
  938. return of_path_i2c_client_exists(path);
  939. }
  940. return 0;
  941. }
  942. static const char *overlay_path(int nr)
  943. {
  944. static char buf[256];
  945. snprintf(buf, sizeof(buf) - 1,
  946. "/testcase-data/overlay%d", nr);
  947. buf[sizeof(buf) - 1] = '\0';
  948. return buf;
  949. }
  950. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  951. /* it is guaranteed that overlay ids are assigned in sequence */
  952. #define MAX_UNITTEST_OVERLAYS 256
  953. static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
  954. static int overlay_first_id = -1;
  955. static void of_unittest_track_overlay(int id)
  956. {
  957. if (overlay_first_id < 0)
  958. overlay_first_id = id;
  959. id -= overlay_first_id;
  960. /* we shouldn't need that many */
  961. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  962. overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
  963. }
  964. static void of_unittest_untrack_overlay(int id)
  965. {
  966. if (overlay_first_id < 0)
  967. return;
  968. id -= overlay_first_id;
  969. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  970. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  971. }
  972. static void of_unittest_destroy_tracked_overlays(void)
  973. {
  974. int id, ret, defers;
  975. if (overlay_first_id < 0)
  976. return;
  977. /* try until no defers */
  978. do {
  979. defers = 0;
  980. /* remove in reverse order */
  981. for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
  982. if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
  983. continue;
  984. ret = of_overlay_destroy(id + overlay_first_id);
  985. if (ret != 0) {
  986. defers++;
  987. pr_warn("%s: overlay destroy failed for #%d\n",
  988. __func__, id + overlay_first_id);
  989. continue;
  990. }
  991. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  992. }
  993. } while (defers > 0);
  994. }
  995. static int of_unittest_apply_overlay(int unittest_nr, int overlay_nr,
  996. int *overlay_id)
  997. {
  998. struct device_node *np = NULL;
  999. int ret, id = -1;
  1000. np = of_find_node_by_path(overlay_path(overlay_nr));
  1001. if (np == NULL) {
  1002. unittest(0, "could not find overlay node @\"%s\"\n",
  1003. overlay_path(overlay_nr));
  1004. ret = -EINVAL;
  1005. goto out;
  1006. }
  1007. ret = of_overlay_create(np);
  1008. if (ret < 0) {
  1009. unittest(0, "could not create overlay from \"%s\"\n",
  1010. overlay_path(overlay_nr));
  1011. goto out;
  1012. }
  1013. id = ret;
  1014. of_unittest_track_overlay(id);
  1015. ret = 0;
  1016. out:
  1017. of_node_put(np);
  1018. if (overlay_id)
  1019. *overlay_id = id;
  1020. return ret;
  1021. }
  1022. /* apply an overlay while checking before and after states */
  1023. static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr,
  1024. int before, int after, enum overlay_type ovtype)
  1025. {
  1026. int ret;
  1027. /* unittest device must not be in before state */
  1028. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1029. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1030. overlay_path(overlay_nr),
  1031. unittest_path(unittest_nr, ovtype),
  1032. !before ? "enabled" : "disabled");
  1033. return -EINVAL;
  1034. }
  1035. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, NULL);
  1036. if (ret != 0) {
  1037. /* of_unittest_apply_overlay already called unittest() */
  1038. return ret;
  1039. }
  1040. /* unittest device must be to set to after state */
  1041. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1042. unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
  1043. overlay_path(overlay_nr),
  1044. unittest_path(unittest_nr, ovtype),
  1045. !after ? "enabled" : "disabled");
  1046. return -EINVAL;
  1047. }
  1048. return 0;
  1049. }
  1050. /* apply an overlay and then revert it while checking before, after states */
  1051. static int of_unittest_apply_revert_overlay_check(int overlay_nr,
  1052. int unittest_nr, int before, int after,
  1053. enum overlay_type ovtype)
  1054. {
  1055. int ret, ov_id;
  1056. /* unittest device must be in before state */
  1057. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1058. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1059. overlay_path(overlay_nr),
  1060. unittest_path(unittest_nr, ovtype),
  1061. !before ? "enabled" : "disabled");
  1062. return -EINVAL;
  1063. }
  1064. /* apply the overlay */
  1065. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ov_id);
  1066. if (ret != 0) {
  1067. /* of_unittest_apply_overlay already called unittest() */
  1068. return ret;
  1069. }
  1070. /* unittest device must be in after state */
  1071. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1072. unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
  1073. overlay_path(overlay_nr),
  1074. unittest_path(unittest_nr, ovtype),
  1075. !after ? "enabled" : "disabled");
  1076. return -EINVAL;
  1077. }
  1078. ret = of_overlay_destroy(ov_id);
  1079. if (ret != 0) {
  1080. unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
  1081. overlay_path(overlay_nr),
  1082. unittest_path(unittest_nr, ovtype));
  1083. return ret;
  1084. }
  1085. /* unittest device must be again in before state */
  1086. if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
  1087. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1088. overlay_path(overlay_nr),
  1089. unittest_path(unittest_nr, ovtype),
  1090. !before ? "enabled" : "disabled");
  1091. return -EINVAL;
  1092. }
  1093. return 0;
  1094. }
  1095. /* test activation of device */
  1096. static void of_unittest_overlay_0(void)
  1097. {
  1098. int ret;
  1099. /* device should enable */
  1100. ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
  1101. if (ret != 0)
  1102. return;
  1103. unittest(1, "overlay test %d passed\n", 0);
  1104. }
  1105. /* test deactivation of device */
  1106. static void of_unittest_overlay_1(void)
  1107. {
  1108. int ret;
  1109. /* device should disable */
  1110. ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
  1111. if (ret != 0)
  1112. return;
  1113. unittest(1, "overlay test %d passed\n", 1);
  1114. }
  1115. /* test activation of device */
  1116. static void of_unittest_overlay_2(void)
  1117. {
  1118. int ret;
  1119. /* device should enable */
  1120. ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
  1121. if (ret != 0)
  1122. return;
  1123. unittest(1, "overlay test %d passed\n", 2);
  1124. }
  1125. /* test deactivation of device */
  1126. static void of_unittest_overlay_3(void)
  1127. {
  1128. int ret;
  1129. /* device should disable */
  1130. ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
  1131. if (ret != 0)
  1132. return;
  1133. unittest(1, "overlay test %d passed\n", 3);
  1134. }
  1135. /* test activation of a full device node */
  1136. static void of_unittest_overlay_4(void)
  1137. {
  1138. int ret;
  1139. /* device should disable */
  1140. ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
  1141. if (ret != 0)
  1142. return;
  1143. unittest(1, "overlay test %d passed\n", 4);
  1144. }
  1145. /* test overlay apply/revert sequence */
  1146. static void of_unittest_overlay_5(void)
  1147. {
  1148. int ret;
  1149. /* device should disable */
  1150. ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
  1151. if (ret != 0)
  1152. return;
  1153. unittest(1, "overlay test %d passed\n", 5);
  1154. }
  1155. /* test overlay application in sequence */
  1156. static void of_unittest_overlay_6(void)
  1157. {
  1158. struct device_node *np;
  1159. int ret, i, ov_id[2];
  1160. int overlay_nr = 6, unittest_nr = 6;
  1161. int before = 0, after = 1;
  1162. /* unittest device must be in before state */
  1163. for (i = 0; i < 2; i++) {
  1164. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1165. != before) {
  1166. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1167. overlay_path(overlay_nr + i),
  1168. unittest_path(unittest_nr + i,
  1169. PDEV_OVERLAY),
  1170. !before ? "enabled" : "disabled");
  1171. return;
  1172. }
  1173. }
  1174. /* apply the overlays */
  1175. for (i = 0; i < 2; i++) {
  1176. np = of_find_node_by_path(overlay_path(overlay_nr + i));
  1177. if (np == NULL) {
  1178. unittest(0, "could not find overlay node @\"%s\"\n",
  1179. overlay_path(overlay_nr + i));
  1180. return;
  1181. }
  1182. ret = of_overlay_create(np);
  1183. if (ret < 0) {
  1184. unittest(0, "could not create overlay from \"%s\"\n",
  1185. overlay_path(overlay_nr + i));
  1186. return;
  1187. }
  1188. ov_id[i] = ret;
  1189. of_unittest_track_overlay(ov_id[i]);
  1190. }
  1191. for (i = 0; i < 2; i++) {
  1192. /* unittest device must be in after state */
  1193. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1194. != after) {
  1195. unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  1196. overlay_path(overlay_nr + i),
  1197. unittest_path(unittest_nr + i,
  1198. PDEV_OVERLAY),
  1199. !after ? "enabled" : "disabled");
  1200. return;
  1201. }
  1202. }
  1203. for (i = 1; i >= 0; i--) {
  1204. ret = of_overlay_destroy(ov_id[i]);
  1205. if (ret != 0) {
  1206. unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
  1207. overlay_path(overlay_nr + i),
  1208. unittest_path(unittest_nr + i,
  1209. PDEV_OVERLAY));
  1210. return;
  1211. }
  1212. of_unittest_untrack_overlay(ov_id[i]);
  1213. }
  1214. for (i = 0; i < 2; i++) {
  1215. /* unittest device must be again in before state */
  1216. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1217. != before) {
  1218. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1219. overlay_path(overlay_nr + i),
  1220. unittest_path(unittest_nr + i,
  1221. PDEV_OVERLAY),
  1222. !before ? "enabled" : "disabled");
  1223. return;
  1224. }
  1225. }
  1226. unittest(1, "overlay test %d passed\n", 6);
  1227. }
  1228. /* test overlay application in sequence */
  1229. static void of_unittest_overlay_8(void)
  1230. {
  1231. struct device_node *np;
  1232. int ret, i, ov_id[2];
  1233. int overlay_nr = 8, unittest_nr = 8;
  1234. /* we don't care about device state in this test */
  1235. /* apply the overlays */
  1236. for (i = 0; i < 2; i++) {
  1237. np = of_find_node_by_path(overlay_path(overlay_nr + i));
  1238. if (np == NULL) {
  1239. unittest(0, "could not find overlay node @\"%s\"\n",
  1240. overlay_path(overlay_nr + i));
  1241. return;
  1242. }
  1243. ret = of_overlay_create(np);
  1244. if (ret < 0) {
  1245. unittest(0, "could not create overlay from \"%s\"\n",
  1246. overlay_path(overlay_nr + i));
  1247. return;
  1248. }
  1249. ov_id[i] = ret;
  1250. of_unittest_track_overlay(ov_id[i]);
  1251. }
  1252. /* now try to remove first overlay (it should fail) */
  1253. ret = of_overlay_destroy(ov_id[0]);
  1254. if (ret == 0) {
  1255. unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
  1256. overlay_path(overlay_nr + 0),
  1257. unittest_path(unittest_nr,
  1258. PDEV_OVERLAY));
  1259. return;
  1260. }
  1261. /* removing them in order should work */
  1262. for (i = 1; i >= 0; i--) {
  1263. ret = of_overlay_destroy(ov_id[i]);
  1264. if (ret != 0) {
  1265. unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
  1266. overlay_path(overlay_nr + i),
  1267. unittest_path(unittest_nr,
  1268. PDEV_OVERLAY));
  1269. return;
  1270. }
  1271. of_unittest_untrack_overlay(ov_id[i]);
  1272. }
  1273. unittest(1, "overlay test %d passed\n", 8);
  1274. }
  1275. /* test insertion of a bus with parent devices */
  1276. static void of_unittest_overlay_10(void)
  1277. {
  1278. int ret;
  1279. char *child_path;
  1280. /* device should disable */
  1281. ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
  1282. if (unittest(ret == 0,
  1283. "overlay test %d failed; overlay application\n", 10))
  1284. return;
  1285. child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
  1286. unittest_path(10, PDEV_OVERLAY));
  1287. if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
  1288. return;
  1289. ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
  1290. kfree(child_path);
  1291. if (unittest(ret, "overlay test %d failed; no child device\n", 10))
  1292. return;
  1293. }
  1294. /* test insertion of a bus with parent devices (and revert) */
  1295. static void of_unittest_overlay_11(void)
  1296. {
  1297. int ret;
  1298. /* device should disable */
  1299. ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
  1300. PDEV_OVERLAY);
  1301. if (unittest(ret == 0,
  1302. "overlay test %d failed; overlay application\n", 11))
  1303. return;
  1304. }
  1305. #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
  1306. struct unittest_i2c_bus_data {
  1307. struct platform_device *pdev;
  1308. struct i2c_adapter adap;
  1309. };
  1310. static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
  1311. struct i2c_msg *msgs, int num)
  1312. {
  1313. struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
  1314. (void)std;
  1315. return num;
  1316. }
  1317. static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
  1318. {
  1319. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  1320. }
  1321. static const struct i2c_algorithm unittest_i2c_algo = {
  1322. .master_xfer = unittest_i2c_master_xfer,
  1323. .functionality = unittest_i2c_functionality,
  1324. };
  1325. static int unittest_i2c_bus_probe(struct platform_device *pdev)
  1326. {
  1327. struct device *dev = &pdev->dev;
  1328. struct device_node *np = dev->of_node;
  1329. struct unittest_i2c_bus_data *std;
  1330. struct i2c_adapter *adap;
  1331. int ret;
  1332. if (np == NULL) {
  1333. dev_err(dev, "No OF data for device\n");
  1334. return -EINVAL;
  1335. }
  1336. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1337. std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
  1338. if (!std) {
  1339. dev_err(dev, "Failed to allocate unittest i2c data\n");
  1340. return -ENOMEM;
  1341. }
  1342. /* link them together */
  1343. std->pdev = pdev;
  1344. platform_set_drvdata(pdev, std);
  1345. adap = &std->adap;
  1346. i2c_set_adapdata(adap, std);
  1347. adap->nr = -1;
  1348. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  1349. adap->class = I2C_CLASS_DEPRECATED;
  1350. adap->algo = &unittest_i2c_algo;
  1351. adap->dev.parent = dev;
  1352. adap->dev.of_node = dev->of_node;
  1353. adap->timeout = 5 * HZ;
  1354. adap->retries = 3;
  1355. ret = i2c_add_numbered_adapter(adap);
  1356. if (ret != 0) {
  1357. dev_err(dev, "Failed to add I2C adapter\n");
  1358. return ret;
  1359. }
  1360. return 0;
  1361. }
  1362. static int unittest_i2c_bus_remove(struct platform_device *pdev)
  1363. {
  1364. struct device *dev = &pdev->dev;
  1365. struct device_node *np = dev->of_node;
  1366. struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
  1367. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1368. i2c_del_adapter(&std->adap);
  1369. return 0;
  1370. }
  1371. static const struct of_device_id unittest_i2c_bus_match[] = {
  1372. { .compatible = "unittest-i2c-bus", },
  1373. {},
  1374. };
  1375. static struct platform_driver unittest_i2c_bus_driver = {
  1376. .probe = unittest_i2c_bus_probe,
  1377. .remove = unittest_i2c_bus_remove,
  1378. .driver = {
  1379. .name = "unittest-i2c-bus",
  1380. .of_match_table = of_match_ptr(unittest_i2c_bus_match),
  1381. },
  1382. };
  1383. static int unittest_i2c_dev_probe(struct i2c_client *client,
  1384. const struct i2c_device_id *id)
  1385. {
  1386. struct device *dev = &client->dev;
  1387. struct device_node *np = client->dev.of_node;
  1388. if (!np) {
  1389. dev_err(dev, "No OF node\n");
  1390. return -EINVAL;
  1391. }
  1392. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1393. return 0;
  1394. };
  1395. static int unittest_i2c_dev_remove(struct i2c_client *client)
  1396. {
  1397. struct device *dev = &client->dev;
  1398. struct device_node *np = client->dev.of_node;
  1399. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1400. return 0;
  1401. }
  1402. static const struct i2c_device_id unittest_i2c_dev_id[] = {
  1403. { .name = "unittest-i2c-dev" },
  1404. { }
  1405. };
  1406. static struct i2c_driver unittest_i2c_dev_driver = {
  1407. .driver = {
  1408. .name = "unittest-i2c-dev",
  1409. .owner = THIS_MODULE,
  1410. },
  1411. .probe = unittest_i2c_dev_probe,
  1412. .remove = unittest_i2c_dev_remove,
  1413. .id_table = unittest_i2c_dev_id,
  1414. };
  1415. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1416. struct unittest_i2c_mux_data {
  1417. int nchans;
  1418. struct i2c_adapter *adap[];
  1419. };
  1420. static int unittest_i2c_mux_select_chan(struct i2c_adapter *adap,
  1421. void *client, u32 chan)
  1422. {
  1423. return 0;
  1424. }
  1425. static int unittest_i2c_mux_probe(struct i2c_client *client,
  1426. const struct i2c_device_id *id)
  1427. {
  1428. int ret, i, nchans, size;
  1429. struct device *dev = &client->dev;
  1430. struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
  1431. struct device_node *np = client->dev.of_node, *child;
  1432. struct unittest_i2c_mux_data *stm;
  1433. u32 reg, max_reg;
  1434. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1435. if (!np) {
  1436. dev_err(dev, "No OF node\n");
  1437. return -EINVAL;
  1438. }
  1439. max_reg = (u32)-1;
  1440. for_each_child_of_node(np, child) {
  1441. ret = of_property_read_u32(child, "reg", &reg);
  1442. if (ret)
  1443. continue;
  1444. if (max_reg == (u32)-1 || reg > max_reg)
  1445. max_reg = reg;
  1446. }
  1447. nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
  1448. if (nchans == 0) {
  1449. dev_err(dev, "No channels\n");
  1450. return -EINVAL;
  1451. }
  1452. size = offsetof(struct unittest_i2c_mux_data, adap[nchans]);
  1453. stm = devm_kzalloc(dev, size, GFP_KERNEL);
  1454. if (!stm) {
  1455. dev_err(dev, "Out of memory\n");
  1456. return -ENOMEM;
  1457. }
  1458. stm->nchans = nchans;
  1459. for (i = 0; i < nchans; i++) {
  1460. stm->adap[i] = i2c_add_mux_adapter(adap, dev, client,
  1461. 0, i, 0, unittest_i2c_mux_select_chan, NULL);
  1462. if (!stm->adap[i]) {
  1463. dev_err(dev, "Failed to register mux #%d\n", i);
  1464. for (i--; i >= 0; i--)
  1465. i2c_del_mux_adapter(stm->adap[i]);
  1466. return -ENODEV;
  1467. }
  1468. }
  1469. i2c_set_clientdata(client, stm);
  1470. return 0;
  1471. };
  1472. static int unittest_i2c_mux_remove(struct i2c_client *client)
  1473. {
  1474. struct device *dev = &client->dev;
  1475. struct device_node *np = client->dev.of_node;
  1476. struct unittest_i2c_mux_data *stm = i2c_get_clientdata(client);
  1477. int i;
  1478. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1479. for (i = stm->nchans - 1; i >= 0; i--)
  1480. i2c_del_mux_adapter(stm->adap[i]);
  1481. return 0;
  1482. }
  1483. static const struct i2c_device_id unittest_i2c_mux_id[] = {
  1484. { .name = "unittest-i2c-mux" },
  1485. { }
  1486. };
  1487. static struct i2c_driver unittest_i2c_mux_driver = {
  1488. .driver = {
  1489. .name = "unittest-i2c-mux",
  1490. .owner = THIS_MODULE,
  1491. },
  1492. .probe = unittest_i2c_mux_probe,
  1493. .remove = unittest_i2c_mux_remove,
  1494. .id_table = unittest_i2c_mux_id,
  1495. };
  1496. #endif
  1497. static int of_unittest_overlay_i2c_init(void)
  1498. {
  1499. int ret;
  1500. ret = i2c_add_driver(&unittest_i2c_dev_driver);
  1501. if (unittest(ret == 0,
  1502. "could not register unittest i2c device driver\n"))
  1503. return ret;
  1504. ret = platform_driver_register(&unittest_i2c_bus_driver);
  1505. if (unittest(ret == 0,
  1506. "could not register unittest i2c bus driver\n"))
  1507. return ret;
  1508. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1509. ret = i2c_add_driver(&unittest_i2c_mux_driver);
  1510. if (unittest(ret == 0,
  1511. "could not register unittest i2c mux driver\n"))
  1512. return ret;
  1513. #endif
  1514. return 0;
  1515. }
  1516. static void of_unittest_overlay_i2c_cleanup(void)
  1517. {
  1518. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1519. i2c_del_driver(&unittest_i2c_mux_driver);
  1520. #endif
  1521. platform_driver_unregister(&unittest_i2c_bus_driver);
  1522. i2c_del_driver(&unittest_i2c_dev_driver);
  1523. }
  1524. static void of_unittest_overlay_i2c_12(void)
  1525. {
  1526. int ret;
  1527. /* device should enable */
  1528. ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
  1529. if (ret != 0)
  1530. return;
  1531. unittest(1, "overlay test %d passed\n", 12);
  1532. }
  1533. /* test deactivation of device */
  1534. static void of_unittest_overlay_i2c_13(void)
  1535. {
  1536. int ret;
  1537. /* device should disable */
  1538. ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
  1539. if (ret != 0)
  1540. return;
  1541. unittest(1, "overlay test %d passed\n", 13);
  1542. }
  1543. /* just check for i2c mux existence */
  1544. static void of_unittest_overlay_i2c_14(void)
  1545. {
  1546. }
  1547. static void of_unittest_overlay_i2c_15(void)
  1548. {
  1549. int ret;
  1550. /* device should enable */
  1551. ret = of_unittest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY);
  1552. if (ret != 0)
  1553. return;
  1554. unittest(1, "overlay test %d passed\n", 15);
  1555. }
  1556. #else
  1557. static inline void of_unittest_overlay_i2c_14(void) { }
  1558. static inline void of_unittest_overlay_i2c_15(void) { }
  1559. #endif
  1560. static void __init of_unittest_overlay(void)
  1561. {
  1562. struct device_node *bus_np = NULL;
  1563. int ret;
  1564. ret = platform_driver_register(&unittest_driver);
  1565. if (ret != 0) {
  1566. unittest(0, "could not register unittest driver\n");
  1567. goto out;
  1568. }
  1569. bus_np = of_find_node_by_path(bus_path);
  1570. if (bus_np == NULL) {
  1571. unittest(0, "could not find bus_path \"%s\"\n", bus_path);
  1572. goto out;
  1573. }
  1574. ret = of_platform_populate(bus_np, of_default_bus_match_table,
  1575. NULL, NULL);
  1576. if (ret != 0) {
  1577. unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
  1578. goto out;
  1579. }
  1580. if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
  1581. unittest(0, "could not find unittest0 @ \"%s\"\n",
  1582. unittest_path(100, PDEV_OVERLAY));
  1583. goto out;
  1584. }
  1585. if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
  1586. unittest(0, "unittest1 @ \"%s\" should not exist\n",
  1587. unittest_path(101, PDEV_OVERLAY));
  1588. goto out;
  1589. }
  1590. unittest(1, "basic infrastructure of overlays passed");
  1591. /* tests in sequence */
  1592. of_unittest_overlay_0();
  1593. of_unittest_overlay_1();
  1594. of_unittest_overlay_2();
  1595. of_unittest_overlay_3();
  1596. of_unittest_overlay_4();
  1597. of_unittest_overlay_5();
  1598. of_unittest_overlay_6();
  1599. of_unittest_overlay_8();
  1600. of_unittest_overlay_10();
  1601. of_unittest_overlay_11();
  1602. #if IS_BUILTIN(CONFIG_I2C)
  1603. if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
  1604. goto out;
  1605. of_unittest_overlay_i2c_12();
  1606. of_unittest_overlay_i2c_13();
  1607. of_unittest_overlay_i2c_14();
  1608. of_unittest_overlay_i2c_15();
  1609. of_unittest_overlay_i2c_cleanup();
  1610. #endif
  1611. of_unittest_destroy_tracked_overlays();
  1612. out:
  1613. of_node_put(bus_np);
  1614. }
  1615. #else
  1616. static inline void __init of_unittest_overlay(void) { }
  1617. #endif
  1618. static int __init of_unittest(void)
  1619. {
  1620. struct device_node *np;
  1621. int res;
  1622. /* adding data for unittest */
  1623. res = unittest_data_add();
  1624. if (res)
  1625. return res;
  1626. if (!of_aliases)
  1627. of_aliases = of_find_node_by_path("/aliases");
  1628. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  1629. if (!np) {
  1630. pr_info("No testcase data in device tree; not running tests\n");
  1631. return 0;
  1632. }
  1633. of_node_put(np);
  1634. pr_info("start of unittest - you will see error messages\n");
  1635. of_unittest_check_tree_linkage();
  1636. of_unittest_check_phandles();
  1637. of_unittest_find_node_by_name();
  1638. of_unittest_dynamic();
  1639. of_unittest_parse_phandle_with_args();
  1640. of_unittest_property_string();
  1641. of_unittest_property_copy();
  1642. of_unittest_changeset();
  1643. of_unittest_parse_interrupts();
  1644. of_unittest_parse_interrupts_extended();
  1645. of_unittest_match_node();
  1646. of_unittest_platform_populate();
  1647. of_unittest_overlay();
  1648. /* Double check linkage after removing testcase data */
  1649. of_unittest_check_tree_linkage();
  1650. pr_info("end of unittest - %i passed, %i failed\n",
  1651. unittest_results.passed, unittest_results.failed);
  1652. return 0;
  1653. }
  1654. late_initcall(of_unittest);