unittest.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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. rc = -EINVAL;
  172. goto put_child;
  173. }
  174. rc = of_unittest_check_node_linkage(child);
  175. if (rc < 0)
  176. goto put_child;
  177. count += rc;
  178. }
  179. return count + 1;
  180. put_child:
  181. of_node_put(child);
  182. return rc;
  183. }
  184. static void __init of_unittest_check_tree_linkage(void)
  185. {
  186. struct device_node *np;
  187. int allnode_count = 0, child_count;
  188. if (!of_root)
  189. return;
  190. for_each_of_allnodes(np)
  191. allnode_count++;
  192. child_count = of_unittest_check_node_linkage(of_root);
  193. unittest(child_count > 0, "Device node data structure is corrupted\n");
  194. unittest(child_count == allnode_count,
  195. "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
  196. allnode_count, child_count);
  197. pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
  198. }
  199. struct node_hash {
  200. struct hlist_node node;
  201. struct device_node *np;
  202. };
  203. static DEFINE_HASHTABLE(phandle_ht, 8);
  204. static void __init of_unittest_check_phandles(void)
  205. {
  206. struct device_node *np;
  207. struct node_hash *nh;
  208. struct hlist_node *tmp;
  209. int i, dup_count = 0, phandle_count = 0;
  210. for_each_of_allnodes(np) {
  211. if (!np->phandle)
  212. continue;
  213. hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
  214. if (nh->np->phandle == np->phandle) {
  215. pr_info("Duplicate phandle! %i used by %s and %s\n",
  216. np->phandle, nh->np->full_name, np->full_name);
  217. dup_count++;
  218. break;
  219. }
  220. }
  221. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  222. if (WARN_ON(!nh))
  223. return;
  224. nh->np = np;
  225. hash_add(phandle_ht, &nh->node, np->phandle);
  226. phandle_count++;
  227. }
  228. unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
  229. dup_count, phandle_count);
  230. /* Clean up */
  231. hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
  232. hash_del(&nh->node);
  233. kfree(nh);
  234. }
  235. }
  236. static void __init of_unittest_parse_phandle_with_args(void)
  237. {
  238. struct device_node *np;
  239. struct of_phandle_args args;
  240. int i, rc;
  241. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  242. if (!np) {
  243. pr_err("missing testcase data\n");
  244. return;
  245. }
  246. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  247. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  248. for (i = 0; i < 8; i++) {
  249. bool passed = true;
  250. rc = of_parse_phandle_with_args(np, "phandle-list",
  251. "#phandle-cells", i, &args);
  252. /* Test the values from tests-phandle.dtsi */
  253. switch (i) {
  254. case 0:
  255. passed &= !rc;
  256. passed &= (args.args_count == 1);
  257. passed &= (args.args[0] == (i + 1));
  258. break;
  259. case 1:
  260. passed &= !rc;
  261. passed &= (args.args_count == 2);
  262. passed &= (args.args[0] == (i + 1));
  263. passed &= (args.args[1] == 0);
  264. break;
  265. case 2:
  266. passed &= (rc == -ENOENT);
  267. break;
  268. case 3:
  269. passed &= !rc;
  270. passed &= (args.args_count == 3);
  271. passed &= (args.args[0] == (i + 1));
  272. passed &= (args.args[1] == 4);
  273. passed &= (args.args[2] == 3);
  274. break;
  275. case 4:
  276. passed &= !rc;
  277. passed &= (args.args_count == 2);
  278. passed &= (args.args[0] == (i + 1));
  279. passed &= (args.args[1] == 100);
  280. break;
  281. case 5:
  282. passed &= !rc;
  283. passed &= (args.args_count == 0);
  284. break;
  285. case 6:
  286. passed &= !rc;
  287. passed &= (args.args_count == 1);
  288. passed &= (args.args[0] == (i + 1));
  289. break;
  290. case 7:
  291. passed &= (rc == -ENOENT);
  292. break;
  293. default:
  294. passed = false;
  295. }
  296. unittest(passed, "index %i - data error on node %s rc=%i\n",
  297. i, args.np->full_name, rc);
  298. }
  299. /* Check for missing list property */
  300. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  301. "#phandle-cells", 0, &args);
  302. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  303. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  304. "#phandle-cells");
  305. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  306. /* Check for missing cells property */
  307. rc = of_parse_phandle_with_args(np, "phandle-list",
  308. "#phandle-cells-missing", 0, &args);
  309. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  310. rc = of_count_phandle_with_args(np, "phandle-list",
  311. "#phandle-cells-missing");
  312. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  313. /* Check for bad phandle in list */
  314. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  315. "#phandle-cells", 0, &args);
  316. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  317. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  318. "#phandle-cells");
  319. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  320. /* Check for incorrectly formed argument list */
  321. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  322. "#phandle-cells", 1, &args);
  323. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  324. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  325. "#phandle-cells");
  326. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  327. }
  328. static void __init of_unittest_property_string(void)
  329. {
  330. const char *strings[4];
  331. struct device_node *np;
  332. int rc;
  333. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  334. if (!np) {
  335. pr_err("No testcase data in device tree\n");
  336. return;
  337. }
  338. rc = of_property_match_string(np, "phandle-list-names", "first");
  339. unittest(rc == 0, "first expected:0 got:%i\n", rc);
  340. rc = of_property_match_string(np, "phandle-list-names", "second");
  341. unittest(rc == 1, "second expected:1 got:%i\n", rc);
  342. rc = of_property_match_string(np, "phandle-list-names", "third");
  343. unittest(rc == 2, "third expected:2 got:%i\n", rc);
  344. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  345. unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  346. rc = of_property_match_string(np, "missing-property", "blah");
  347. unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  348. rc = of_property_match_string(np, "empty-property", "blah");
  349. unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  350. rc = of_property_match_string(np, "unterminated-string", "blah");
  351. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  352. /* of_property_count_strings() tests */
  353. rc = of_property_count_strings(np, "string-property");
  354. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  355. rc = of_property_count_strings(np, "phandle-list-names");
  356. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  357. rc = of_property_count_strings(np, "unterminated-string");
  358. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  359. rc = of_property_count_strings(np, "unterminated-string-list");
  360. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  361. /* of_property_read_string_index() tests */
  362. rc = of_property_read_string_index(np, "string-property", 0, strings);
  363. unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  364. strings[0] = NULL;
  365. rc = of_property_read_string_index(np, "string-property", 1, strings);
  366. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  367. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  368. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  369. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  370. unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  371. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  372. unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  373. strings[0] = NULL;
  374. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  375. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  376. strings[0] = NULL;
  377. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  378. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  379. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  380. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  381. strings[0] = NULL;
  382. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  383. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  384. strings[1] = NULL;
  385. /* of_property_read_string_array() tests */
  386. rc = of_property_read_string_array(np, "string-property", strings, 4);
  387. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  388. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  389. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  390. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  391. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  392. /* -- An incorrectly formed string should cause a failure */
  393. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  394. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  395. /* -- parsing the correctly formed strings should still work: */
  396. strings[2] = NULL;
  397. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  398. unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  399. strings[1] = NULL;
  400. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  401. unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  402. }
  403. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  404. (p1)->value && (p2)->value && \
  405. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  406. !strcmp((p1)->name, (p2)->name))
  407. static void __init of_unittest_property_copy(void)
  408. {
  409. #ifdef CONFIG_OF_DYNAMIC
  410. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  411. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  412. struct property *new;
  413. new = __of_prop_dup(&p1, GFP_KERNEL);
  414. unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  415. kfree(new->value);
  416. kfree(new->name);
  417. kfree(new);
  418. new = __of_prop_dup(&p2, GFP_KERNEL);
  419. unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  420. kfree(new->value);
  421. kfree(new->name);
  422. kfree(new);
  423. #endif
  424. }
  425. static void __init of_unittest_changeset(void)
  426. {
  427. #ifdef CONFIG_OF_DYNAMIC
  428. struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
  429. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  430. struct property *ppremove;
  431. struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
  432. struct of_changeset chgset;
  433. n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
  434. unittest(n1, "testcase setup failure\n");
  435. n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
  436. unittest(n2, "testcase setup failure\n");
  437. n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
  438. unittest(n21, "testcase setup failure %p\n", n21);
  439. nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
  440. unittest(nremove, "testcase setup failure\n");
  441. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  442. unittest(ppadd, "testcase setup failure\n");
  443. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  444. unittest(ppupdate, "testcase setup failure\n");
  445. parent = nremove->parent;
  446. n1->parent = parent;
  447. n2->parent = parent;
  448. n21->parent = n2;
  449. n2->child = n21;
  450. ppremove = of_find_property(parent, "prop-remove", NULL);
  451. unittest(ppremove, "failed to find removal prop");
  452. of_changeset_init(&chgset);
  453. unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  454. unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  455. unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  456. unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  457. unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
  458. unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  459. unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  460. unittest(!of_changeset_apply(&chgset), "apply failed\n");
  461. /* Make sure node names are constructed correctly */
  462. unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
  463. "'%s' not added\n", n21->full_name);
  464. of_node_put(np);
  465. unittest(!of_changeset_revert(&chgset), "revert failed\n");
  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 struct resource test_bus_res = {
  656. .start = 0xfffffff8,
  657. .end = 0xfffffff9,
  658. .flags = IORESOURCE_MEM,
  659. };
  660. static const struct platform_device_info test_bus_info = {
  661. .name = "unittest-bus",
  662. };
  663. static void __init of_unittest_platform_populate(void)
  664. {
  665. int irq, rc;
  666. struct device_node *np, *child, *grandchild;
  667. struct platform_device *pdev, *test_bus;
  668. const struct of_device_id match[] = {
  669. { .compatible = "test-device", },
  670. {}
  671. };
  672. np = of_find_node_by_path("/testcase-data");
  673. of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
  674. /* Test that a missing irq domain returns -EPROBE_DEFER */
  675. np = of_find_node_by_path("/testcase-data/testcase-device1");
  676. pdev = of_find_device_by_node(np);
  677. unittest(pdev, "device 1 creation failed\n");
  678. irq = platform_get_irq(pdev, 0);
  679. unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
  680. /* Test that a parsing failure does not return -EPROBE_DEFER */
  681. np = of_find_node_by_path("/testcase-data/testcase-device2");
  682. pdev = of_find_device_by_node(np);
  683. unittest(pdev, "device 2 creation failed\n");
  684. irq = platform_get_irq(pdev, 0);
  685. unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
  686. np = of_find_node_by_path("/testcase-data/platform-tests");
  687. unittest(np, "No testcase data in device tree\n");
  688. if (!np)
  689. return;
  690. test_bus = platform_device_register_full(&test_bus_info);
  691. rc = PTR_ERR_OR_ZERO(test_bus);
  692. unittest(!rc, "testbus registration failed; rc=%i\n", rc);
  693. if (rc)
  694. return;
  695. test_bus->dev.of_node = np;
  696. /*
  697. * Add a dummy resource to the test bus node after it is
  698. * registered to catch problems with un-inserted resources. The
  699. * DT code doesn't insert the resources, and it has caused the
  700. * kernel to oops in the past. This makes sure the same bug
  701. * doesn't crop up again.
  702. */
  703. platform_device_add_resources(test_bus, &test_bus_res, 1);
  704. of_platform_populate(np, match, NULL, &test_bus->dev);
  705. for_each_child_of_node(np, child) {
  706. for_each_child_of_node(child, grandchild)
  707. unittest(of_find_device_by_node(grandchild),
  708. "Could not create device for node '%s'\n",
  709. grandchild->name);
  710. }
  711. of_platform_depopulate(&test_bus->dev);
  712. for_each_child_of_node(np, child) {
  713. for_each_child_of_node(child, grandchild)
  714. unittest(!of_find_device_by_node(grandchild),
  715. "device didn't get destroyed '%s'\n",
  716. grandchild->name);
  717. }
  718. platform_device_unregister(test_bus);
  719. of_node_put(np);
  720. }
  721. /**
  722. * update_node_properties - adds the properties
  723. * of np into dup node (present in live tree) and
  724. * updates parent of children of np to dup.
  725. *
  726. * @np: node already present in live tree
  727. * @dup: node present in live tree to be updated
  728. */
  729. static void update_node_properties(struct device_node *np,
  730. struct device_node *dup)
  731. {
  732. struct property *prop;
  733. struct device_node *child;
  734. for_each_property_of_node(np, prop)
  735. of_add_property(dup, prop);
  736. for_each_child_of_node(np, child)
  737. child->parent = dup;
  738. }
  739. /**
  740. * attach_node_and_children - attaches nodes
  741. * and its children to live tree
  742. *
  743. * @np: Node to attach to live tree
  744. */
  745. static int attach_node_and_children(struct device_node *np)
  746. {
  747. struct device_node *next, *dup, *child;
  748. unsigned long flags;
  749. dup = of_find_node_by_path(np->full_name);
  750. if (dup) {
  751. update_node_properties(np, dup);
  752. return 0;
  753. }
  754. child = np->child;
  755. np->child = NULL;
  756. mutex_lock(&of_mutex);
  757. raw_spin_lock_irqsave(&devtree_lock, flags);
  758. np->sibling = np->parent->child;
  759. np->parent->child = np;
  760. of_node_clear_flag(np, OF_DETACHED);
  761. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  762. __of_attach_node_sysfs(np);
  763. mutex_unlock(&of_mutex);
  764. while (child) {
  765. next = child->sibling;
  766. attach_node_and_children(child);
  767. child = next;
  768. }
  769. return 0;
  770. }
  771. /**
  772. * unittest_data_add - Reads, copies data from
  773. * linked tree and attaches it to the live tree
  774. */
  775. static int __init unittest_data_add(void)
  776. {
  777. void *unittest_data;
  778. struct device_node *unittest_data_node, *np;
  779. /*
  780. * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
  781. * created by cmd_dt_S_dtb in scripts/Makefile.lib
  782. */
  783. extern uint8_t __dtb_testcases_begin[];
  784. extern uint8_t __dtb_testcases_end[];
  785. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  786. int rc;
  787. if (!size) {
  788. pr_warn("%s: No testcase data to attach; not running tests\n",
  789. __func__);
  790. return -ENODATA;
  791. }
  792. /* creating copy */
  793. unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  794. if (!unittest_data) {
  795. pr_warn("%s: Failed to allocate memory for unittest_data; "
  796. "not running tests\n", __func__);
  797. return -ENOMEM;
  798. }
  799. of_fdt_unflatten_tree(unittest_data, &unittest_data_node);
  800. if (!unittest_data_node) {
  801. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  802. return -ENODATA;
  803. }
  804. of_node_set_flag(unittest_data_node, OF_DETACHED);
  805. rc = of_resolve_phandles(unittest_data_node);
  806. if (rc) {
  807. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  808. return -EINVAL;
  809. }
  810. if (!of_root) {
  811. of_root = unittest_data_node;
  812. for_each_of_allnodes(np)
  813. __of_attach_node_sysfs(np);
  814. of_aliases = of_find_node_by_path("/aliases");
  815. of_chosen = of_find_node_by_path("/chosen");
  816. return 0;
  817. }
  818. /* attach the sub-tree to live tree */
  819. np = unittest_data_node->child;
  820. while (np) {
  821. struct device_node *next = np->sibling;
  822. np->parent = of_root;
  823. attach_node_and_children(np);
  824. np = next;
  825. }
  826. return 0;
  827. }
  828. #ifdef CONFIG_OF_OVERLAY
  829. static int unittest_probe(struct platform_device *pdev)
  830. {
  831. struct device *dev = &pdev->dev;
  832. struct device_node *np = dev->of_node;
  833. if (np == NULL) {
  834. dev_err(dev, "No OF data for device\n");
  835. return -EINVAL;
  836. }
  837. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  838. of_platform_populate(np, NULL, NULL, &pdev->dev);
  839. return 0;
  840. }
  841. static int unittest_remove(struct platform_device *pdev)
  842. {
  843. struct device *dev = &pdev->dev;
  844. struct device_node *np = dev->of_node;
  845. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  846. return 0;
  847. }
  848. static const struct of_device_id unittest_match[] = {
  849. { .compatible = "unittest", },
  850. {},
  851. };
  852. static struct platform_driver unittest_driver = {
  853. .probe = unittest_probe,
  854. .remove = unittest_remove,
  855. .driver = {
  856. .name = "unittest",
  857. .of_match_table = of_match_ptr(unittest_match),
  858. },
  859. };
  860. /* get the platform device instantiated at the path */
  861. static struct platform_device *of_path_to_platform_device(const char *path)
  862. {
  863. struct device_node *np;
  864. struct platform_device *pdev;
  865. np = of_find_node_by_path(path);
  866. if (np == NULL)
  867. return NULL;
  868. pdev = of_find_device_by_node(np);
  869. of_node_put(np);
  870. return pdev;
  871. }
  872. /* find out if a platform device exists at that path */
  873. static int of_path_platform_device_exists(const char *path)
  874. {
  875. struct platform_device *pdev;
  876. pdev = of_path_to_platform_device(path);
  877. platform_device_put(pdev);
  878. return pdev != NULL;
  879. }
  880. #if IS_BUILTIN(CONFIG_I2C)
  881. /* get the i2c client device instantiated at the path */
  882. static struct i2c_client *of_path_to_i2c_client(const char *path)
  883. {
  884. struct device_node *np;
  885. struct i2c_client *client;
  886. np = of_find_node_by_path(path);
  887. if (np == NULL)
  888. return NULL;
  889. client = of_find_i2c_device_by_node(np);
  890. of_node_put(np);
  891. return client;
  892. }
  893. /* find out if a i2c client device exists at that path */
  894. static int of_path_i2c_client_exists(const char *path)
  895. {
  896. struct i2c_client *client;
  897. client = of_path_to_i2c_client(path);
  898. if (client)
  899. put_device(&client->dev);
  900. return client != NULL;
  901. }
  902. #else
  903. static int of_path_i2c_client_exists(const char *path)
  904. {
  905. return 0;
  906. }
  907. #endif
  908. enum overlay_type {
  909. PDEV_OVERLAY,
  910. I2C_OVERLAY
  911. };
  912. static int of_path_device_type_exists(const char *path,
  913. enum overlay_type ovtype)
  914. {
  915. switch (ovtype) {
  916. case PDEV_OVERLAY:
  917. return of_path_platform_device_exists(path);
  918. case I2C_OVERLAY:
  919. return of_path_i2c_client_exists(path);
  920. }
  921. return 0;
  922. }
  923. static const char *unittest_path(int nr, enum overlay_type ovtype)
  924. {
  925. const char *base;
  926. static char buf[256];
  927. switch (ovtype) {
  928. case PDEV_OVERLAY:
  929. base = "/testcase-data/overlay-node/test-bus";
  930. break;
  931. case I2C_OVERLAY:
  932. base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
  933. break;
  934. default:
  935. buf[0] = '\0';
  936. return buf;
  937. }
  938. snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
  939. buf[sizeof(buf) - 1] = '\0';
  940. return buf;
  941. }
  942. static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
  943. {
  944. const char *path;
  945. path = unittest_path(unittest_nr, ovtype);
  946. switch (ovtype) {
  947. case PDEV_OVERLAY:
  948. return of_path_platform_device_exists(path);
  949. case I2C_OVERLAY:
  950. return of_path_i2c_client_exists(path);
  951. }
  952. return 0;
  953. }
  954. static const char *overlay_path(int nr)
  955. {
  956. static char buf[256];
  957. snprintf(buf, sizeof(buf) - 1,
  958. "/testcase-data/overlay%d", nr);
  959. buf[sizeof(buf) - 1] = '\0';
  960. return buf;
  961. }
  962. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  963. /* it is guaranteed that overlay ids are assigned in sequence */
  964. #define MAX_UNITTEST_OVERLAYS 256
  965. static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
  966. static int overlay_first_id = -1;
  967. static void of_unittest_track_overlay(int id)
  968. {
  969. if (overlay_first_id < 0)
  970. overlay_first_id = id;
  971. id -= overlay_first_id;
  972. /* we shouldn't need that many */
  973. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  974. overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
  975. }
  976. static void of_unittest_untrack_overlay(int id)
  977. {
  978. if (overlay_first_id < 0)
  979. return;
  980. id -= overlay_first_id;
  981. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  982. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  983. }
  984. static void of_unittest_destroy_tracked_overlays(void)
  985. {
  986. int id, ret, defers;
  987. if (overlay_first_id < 0)
  988. return;
  989. /* try until no defers */
  990. do {
  991. defers = 0;
  992. /* remove in reverse order */
  993. for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
  994. if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
  995. continue;
  996. ret = of_overlay_destroy(id + overlay_first_id);
  997. if (ret == -ENODEV) {
  998. pr_warn("%s: no overlay to destroy for #%d\n",
  999. __func__, id + overlay_first_id);
  1000. continue;
  1001. }
  1002. if (ret != 0) {
  1003. defers++;
  1004. pr_warn("%s: overlay destroy failed for #%d\n",
  1005. __func__, id + overlay_first_id);
  1006. continue;
  1007. }
  1008. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1009. }
  1010. } while (defers > 0);
  1011. }
  1012. static int of_unittest_apply_overlay(int unittest_nr, int overlay_nr,
  1013. int *overlay_id)
  1014. {
  1015. struct device_node *np = NULL;
  1016. int ret, id = -1;
  1017. np = of_find_node_by_path(overlay_path(overlay_nr));
  1018. if (np == NULL) {
  1019. unittest(0, "could not find overlay node @\"%s\"\n",
  1020. overlay_path(overlay_nr));
  1021. ret = -EINVAL;
  1022. goto out;
  1023. }
  1024. ret = of_overlay_create(np);
  1025. if (ret < 0) {
  1026. unittest(0, "could not create overlay from \"%s\"\n",
  1027. overlay_path(overlay_nr));
  1028. goto out;
  1029. }
  1030. id = ret;
  1031. of_unittest_track_overlay(id);
  1032. ret = 0;
  1033. out:
  1034. of_node_put(np);
  1035. if (overlay_id)
  1036. *overlay_id = id;
  1037. return ret;
  1038. }
  1039. /* apply an overlay while checking before and after states */
  1040. static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr,
  1041. int before, int after, enum overlay_type ovtype)
  1042. {
  1043. int ret;
  1044. /* unittest device must not be in before state */
  1045. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1046. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1047. overlay_path(overlay_nr),
  1048. unittest_path(unittest_nr, ovtype),
  1049. !before ? "enabled" : "disabled");
  1050. return -EINVAL;
  1051. }
  1052. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, NULL);
  1053. if (ret != 0) {
  1054. /* of_unittest_apply_overlay already called unittest() */
  1055. return ret;
  1056. }
  1057. /* unittest device must be to set to after state */
  1058. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1059. unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
  1060. overlay_path(overlay_nr),
  1061. unittest_path(unittest_nr, ovtype),
  1062. !after ? "enabled" : "disabled");
  1063. return -EINVAL;
  1064. }
  1065. return 0;
  1066. }
  1067. /* apply an overlay and then revert it while checking before, after states */
  1068. static int of_unittest_apply_revert_overlay_check(int overlay_nr,
  1069. int unittest_nr, int before, int after,
  1070. enum overlay_type ovtype)
  1071. {
  1072. int ret, ov_id;
  1073. /* unittest device must be in before state */
  1074. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1075. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1076. overlay_path(overlay_nr),
  1077. unittest_path(unittest_nr, ovtype),
  1078. !before ? "enabled" : "disabled");
  1079. return -EINVAL;
  1080. }
  1081. /* apply the overlay */
  1082. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ov_id);
  1083. if (ret != 0) {
  1084. /* of_unittest_apply_overlay already called unittest() */
  1085. return ret;
  1086. }
  1087. /* unittest device must be in after state */
  1088. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1089. unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
  1090. overlay_path(overlay_nr),
  1091. unittest_path(unittest_nr, ovtype),
  1092. !after ? "enabled" : "disabled");
  1093. return -EINVAL;
  1094. }
  1095. ret = of_overlay_destroy(ov_id);
  1096. if (ret != 0) {
  1097. unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
  1098. overlay_path(overlay_nr),
  1099. unittest_path(unittest_nr, ovtype));
  1100. return ret;
  1101. }
  1102. /* unittest device must be again in before state */
  1103. if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
  1104. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1105. overlay_path(overlay_nr),
  1106. unittest_path(unittest_nr, ovtype),
  1107. !before ? "enabled" : "disabled");
  1108. return -EINVAL;
  1109. }
  1110. return 0;
  1111. }
  1112. /* test activation of device */
  1113. static void of_unittest_overlay_0(void)
  1114. {
  1115. int ret;
  1116. /* device should enable */
  1117. ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
  1118. if (ret != 0)
  1119. return;
  1120. unittest(1, "overlay test %d passed\n", 0);
  1121. }
  1122. /* test deactivation of device */
  1123. static void of_unittest_overlay_1(void)
  1124. {
  1125. int ret;
  1126. /* device should disable */
  1127. ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
  1128. if (ret != 0)
  1129. return;
  1130. unittest(1, "overlay test %d passed\n", 1);
  1131. }
  1132. /* test activation of device */
  1133. static void of_unittest_overlay_2(void)
  1134. {
  1135. int ret;
  1136. /* device should enable */
  1137. ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
  1138. if (ret != 0)
  1139. return;
  1140. unittest(1, "overlay test %d passed\n", 2);
  1141. }
  1142. /* test deactivation of device */
  1143. static void of_unittest_overlay_3(void)
  1144. {
  1145. int ret;
  1146. /* device should disable */
  1147. ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
  1148. if (ret != 0)
  1149. return;
  1150. unittest(1, "overlay test %d passed\n", 3);
  1151. }
  1152. /* test activation of a full device node */
  1153. static void of_unittest_overlay_4(void)
  1154. {
  1155. int ret;
  1156. /* device should disable */
  1157. ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
  1158. if (ret != 0)
  1159. return;
  1160. unittest(1, "overlay test %d passed\n", 4);
  1161. }
  1162. /* test overlay apply/revert sequence */
  1163. static void of_unittest_overlay_5(void)
  1164. {
  1165. int ret;
  1166. /* device should disable */
  1167. ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
  1168. if (ret != 0)
  1169. return;
  1170. unittest(1, "overlay test %d passed\n", 5);
  1171. }
  1172. /* test overlay application in sequence */
  1173. static void of_unittest_overlay_6(void)
  1174. {
  1175. struct device_node *np;
  1176. int ret, i, ov_id[2];
  1177. int overlay_nr = 6, unittest_nr = 6;
  1178. int before = 0, after = 1;
  1179. /* unittest device must be in before state */
  1180. for (i = 0; i < 2; i++) {
  1181. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1182. != before) {
  1183. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1184. overlay_path(overlay_nr + i),
  1185. unittest_path(unittest_nr + i,
  1186. PDEV_OVERLAY),
  1187. !before ? "enabled" : "disabled");
  1188. return;
  1189. }
  1190. }
  1191. /* apply the overlays */
  1192. for (i = 0; i < 2; i++) {
  1193. np = of_find_node_by_path(overlay_path(overlay_nr + i));
  1194. if (np == NULL) {
  1195. unittest(0, "could not find overlay node @\"%s\"\n",
  1196. overlay_path(overlay_nr + i));
  1197. return;
  1198. }
  1199. ret = of_overlay_create(np);
  1200. if (ret < 0) {
  1201. unittest(0, "could not create overlay from \"%s\"\n",
  1202. overlay_path(overlay_nr + i));
  1203. return;
  1204. }
  1205. ov_id[i] = ret;
  1206. of_unittest_track_overlay(ov_id[i]);
  1207. }
  1208. for (i = 0; i < 2; i++) {
  1209. /* unittest device must be in after state */
  1210. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1211. != after) {
  1212. unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  1213. overlay_path(overlay_nr + i),
  1214. unittest_path(unittest_nr + i,
  1215. PDEV_OVERLAY),
  1216. !after ? "enabled" : "disabled");
  1217. return;
  1218. }
  1219. }
  1220. for (i = 1; i >= 0; i--) {
  1221. ret = of_overlay_destroy(ov_id[i]);
  1222. if (ret != 0) {
  1223. unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
  1224. overlay_path(overlay_nr + i),
  1225. unittest_path(unittest_nr + i,
  1226. PDEV_OVERLAY));
  1227. return;
  1228. }
  1229. of_unittest_untrack_overlay(ov_id[i]);
  1230. }
  1231. for (i = 0; i < 2; i++) {
  1232. /* unittest device must be again in before state */
  1233. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1234. != before) {
  1235. unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1236. overlay_path(overlay_nr + i),
  1237. unittest_path(unittest_nr + i,
  1238. PDEV_OVERLAY),
  1239. !before ? "enabled" : "disabled");
  1240. return;
  1241. }
  1242. }
  1243. unittest(1, "overlay test %d passed\n", 6);
  1244. }
  1245. /* test overlay application in sequence */
  1246. static void of_unittest_overlay_8(void)
  1247. {
  1248. struct device_node *np;
  1249. int ret, i, ov_id[2];
  1250. int overlay_nr = 8, unittest_nr = 8;
  1251. /* we don't care about device state in this test */
  1252. /* apply the overlays */
  1253. for (i = 0; i < 2; i++) {
  1254. np = of_find_node_by_path(overlay_path(overlay_nr + i));
  1255. if (np == NULL) {
  1256. unittest(0, "could not find overlay node @\"%s\"\n",
  1257. overlay_path(overlay_nr + i));
  1258. return;
  1259. }
  1260. ret = of_overlay_create(np);
  1261. if (ret < 0) {
  1262. unittest(0, "could not create overlay from \"%s\"\n",
  1263. overlay_path(overlay_nr + i));
  1264. return;
  1265. }
  1266. ov_id[i] = ret;
  1267. of_unittest_track_overlay(ov_id[i]);
  1268. }
  1269. /* now try to remove first overlay (it should fail) */
  1270. ret = of_overlay_destroy(ov_id[0]);
  1271. if (ret == 0) {
  1272. unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
  1273. overlay_path(overlay_nr + 0),
  1274. unittest_path(unittest_nr,
  1275. PDEV_OVERLAY));
  1276. return;
  1277. }
  1278. /* removing them in order should work */
  1279. for (i = 1; i >= 0; i--) {
  1280. ret = of_overlay_destroy(ov_id[i]);
  1281. if (ret != 0) {
  1282. unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
  1283. overlay_path(overlay_nr + i),
  1284. unittest_path(unittest_nr,
  1285. PDEV_OVERLAY));
  1286. return;
  1287. }
  1288. of_unittest_untrack_overlay(ov_id[i]);
  1289. }
  1290. unittest(1, "overlay test %d passed\n", 8);
  1291. }
  1292. /* test insertion of a bus with parent devices */
  1293. static void of_unittest_overlay_10(void)
  1294. {
  1295. int ret;
  1296. char *child_path;
  1297. /* device should disable */
  1298. ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
  1299. if (unittest(ret == 0,
  1300. "overlay test %d failed; overlay application\n", 10))
  1301. return;
  1302. child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
  1303. unittest_path(10, PDEV_OVERLAY));
  1304. if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
  1305. return;
  1306. ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
  1307. kfree(child_path);
  1308. if (unittest(ret, "overlay test %d failed; no child device\n", 10))
  1309. return;
  1310. }
  1311. /* test insertion of a bus with parent devices (and revert) */
  1312. static void of_unittest_overlay_11(void)
  1313. {
  1314. int ret;
  1315. /* device should disable */
  1316. ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
  1317. PDEV_OVERLAY);
  1318. if (unittest(ret == 0,
  1319. "overlay test %d failed; overlay application\n", 11))
  1320. return;
  1321. }
  1322. #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
  1323. struct unittest_i2c_bus_data {
  1324. struct platform_device *pdev;
  1325. struct i2c_adapter adap;
  1326. };
  1327. static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
  1328. struct i2c_msg *msgs, int num)
  1329. {
  1330. struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
  1331. (void)std;
  1332. return num;
  1333. }
  1334. static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
  1335. {
  1336. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  1337. }
  1338. static const struct i2c_algorithm unittest_i2c_algo = {
  1339. .master_xfer = unittest_i2c_master_xfer,
  1340. .functionality = unittest_i2c_functionality,
  1341. };
  1342. static int unittest_i2c_bus_probe(struct platform_device *pdev)
  1343. {
  1344. struct device *dev = &pdev->dev;
  1345. struct device_node *np = dev->of_node;
  1346. struct unittest_i2c_bus_data *std;
  1347. struct i2c_adapter *adap;
  1348. int ret;
  1349. if (np == NULL) {
  1350. dev_err(dev, "No OF data for device\n");
  1351. return -EINVAL;
  1352. }
  1353. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1354. std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
  1355. if (!std) {
  1356. dev_err(dev, "Failed to allocate unittest i2c data\n");
  1357. return -ENOMEM;
  1358. }
  1359. /* link them together */
  1360. std->pdev = pdev;
  1361. platform_set_drvdata(pdev, std);
  1362. adap = &std->adap;
  1363. i2c_set_adapdata(adap, std);
  1364. adap->nr = -1;
  1365. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  1366. adap->class = I2C_CLASS_DEPRECATED;
  1367. adap->algo = &unittest_i2c_algo;
  1368. adap->dev.parent = dev;
  1369. adap->dev.of_node = dev->of_node;
  1370. adap->timeout = 5 * HZ;
  1371. adap->retries = 3;
  1372. ret = i2c_add_numbered_adapter(adap);
  1373. if (ret != 0) {
  1374. dev_err(dev, "Failed to add I2C adapter\n");
  1375. return ret;
  1376. }
  1377. return 0;
  1378. }
  1379. static int unittest_i2c_bus_remove(struct platform_device *pdev)
  1380. {
  1381. struct device *dev = &pdev->dev;
  1382. struct device_node *np = dev->of_node;
  1383. struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
  1384. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1385. i2c_del_adapter(&std->adap);
  1386. return 0;
  1387. }
  1388. static const struct of_device_id unittest_i2c_bus_match[] = {
  1389. { .compatible = "unittest-i2c-bus", },
  1390. {},
  1391. };
  1392. static struct platform_driver unittest_i2c_bus_driver = {
  1393. .probe = unittest_i2c_bus_probe,
  1394. .remove = unittest_i2c_bus_remove,
  1395. .driver = {
  1396. .name = "unittest-i2c-bus",
  1397. .of_match_table = of_match_ptr(unittest_i2c_bus_match),
  1398. },
  1399. };
  1400. static int unittest_i2c_dev_probe(struct i2c_client *client,
  1401. const struct i2c_device_id *id)
  1402. {
  1403. struct device *dev = &client->dev;
  1404. struct device_node *np = client->dev.of_node;
  1405. if (!np) {
  1406. dev_err(dev, "No OF node\n");
  1407. return -EINVAL;
  1408. }
  1409. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1410. return 0;
  1411. };
  1412. static int unittest_i2c_dev_remove(struct i2c_client *client)
  1413. {
  1414. struct device *dev = &client->dev;
  1415. struct device_node *np = client->dev.of_node;
  1416. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1417. return 0;
  1418. }
  1419. static const struct i2c_device_id unittest_i2c_dev_id[] = {
  1420. { .name = "unittest-i2c-dev" },
  1421. { }
  1422. };
  1423. static struct i2c_driver unittest_i2c_dev_driver = {
  1424. .driver = {
  1425. .name = "unittest-i2c-dev",
  1426. },
  1427. .probe = unittest_i2c_dev_probe,
  1428. .remove = unittest_i2c_dev_remove,
  1429. .id_table = unittest_i2c_dev_id,
  1430. };
  1431. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1432. struct unittest_i2c_mux_data {
  1433. int nchans;
  1434. struct i2c_adapter *adap[];
  1435. };
  1436. static int unittest_i2c_mux_select_chan(struct i2c_adapter *adap,
  1437. void *client, u32 chan)
  1438. {
  1439. return 0;
  1440. }
  1441. static int unittest_i2c_mux_probe(struct i2c_client *client,
  1442. const struct i2c_device_id *id)
  1443. {
  1444. int ret, i, nchans, size;
  1445. struct device *dev = &client->dev;
  1446. struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
  1447. struct device_node *np = client->dev.of_node, *child;
  1448. struct unittest_i2c_mux_data *stm;
  1449. u32 reg, max_reg;
  1450. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1451. if (!np) {
  1452. dev_err(dev, "No OF node\n");
  1453. return -EINVAL;
  1454. }
  1455. max_reg = (u32)-1;
  1456. for_each_child_of_node(np, child) {
  1457. ret = of_property_read_u32(child, "reg", &reg);
  1458. if (ret)
  1459. continue;
  1460. if (max_reg == (u32)-1 || reg > max_reg)
  1461. max_reg = reg;
  1462. }
  1463. nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
  1464. if (nchans == 0) {
  1465. dev_err(dev, "No channels\n");
  1466. return -EINVAL;
  1467. }
  1468. size = offsetof(struct unittest_i2c_mux_data, adap[nchans]);
  1469. stm = devm_kzalloc(dev, size, GFP_KERNEL);
  1470. if (!stm) {
  1471. dev_err(dev, "Out of memory\n");
  1472. return -ENOMEM;
  1473. }
  1474. stm->nchans = nchans;
  1475. for (i = 0; i < nchans; i++) {
  1476. stm->adap[i] = i2c_add_mux_adapter(adap, dev, client,
  1477. 0, i, 0, unittest_i2c_mux_select_chan, NULL);
  1478. if (!stm->adap[i]) {
  1479. dev_err(dev, "Failed to register mux #%d\n", i);
  1480. for (i--; i >= 0; i--)
  1481. i2c_del_mux_adapter(stm->adap[i]);
  1482. return -ENODEV;
  1483. }
  1484. }
  1485. i2c_set_clientdata(client, stm);
  1486. return 0;
  1487. };
  1488. static int unittest_i2c_mux_remove(struct i2c_client *client)
  1489. {
  1490. struct device *dev = &client->dev;
  1491. struct device_node *np = client->dev.of_node;
  1492. struct unittest_i2c_mux_data *stm = i2c_get_clientdata(client);
  1493. int i;
  1494. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  1495. for (i = stm->nchans - 1; i >= 0; i--)
  1496. i2c_del_mux_adapter(stm->adap[i]);
  1497. return 0;
  1498. }
  1499. static const struct i2c_device_id unittest_i2c_mux_id[] = {
  1500. { .name = "unittest-i2c-mux" },
  1501. { }
  1502. };
  1503. static struct i2c_driver unittest_i2c_mux_driver = {
  1504. .driver = {
  1505. .name = "unittest-i2c-mux",
  1506. },
  1507. .probe = unittest_i2c_mux_probe,
  1508. .remove = unittest_i2c_mux_remove,
  1509. .id_table = unittest_i2c_mux_id,
  1510. };
  1511. #endif
  1512. static int of_unittest_overlay_i2c_init(void)
  1513. {
  1514. int ret;
  1515. ret = i2c_add_driver(&unittest_i2c_dev_driver);
  1516. if (unittest(ret == 0,
  1517. "could not register unittest i2c device driver\n"))
  1518. return ret;
  1519. ret = platform_driver_register(&unittest_i2c_bus_driver);
  1520. if (unittest(ret == 0,
  1521. "could not register unittest i2c bus driver\n"))
  1522. return ret;
  1523. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1524. ret = i2c_add_driver(&unittest_i2c_mux_driver);
  1525. if (unittest(ret == 0,
  1526. "could not register unittest i2c mux driver\n"))
  1527. return ret;
  1528. #endif
  1529. return 0;
  1530. }
  1531. static void of_unittest_overlay_i2c_cleanup(void)
  1532. {
  1533. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1534. i2c_del_driver(&unittest_i2c_mux_driver);
  1535. #endif
  1536. platform_driver_unregister(&unittest_i2c_bus_driver);
  1537. i2c_del_driver(&unittest_i2c_dev_driver);
  1538. }
  1539. static void of_unittest_overlay_i2c_12(void)
  1540. {
  1541. int ret;
  1542. /* device should enable */
  1543. ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
  1544. if (ret != 0)
  1545. return;
  1546. unittest(1, "overlay test %d passed\n", 12);
  1547. }
  1548. /* test deactivation of device */
  1549. static void of_unittest_overlay_i2c_13(void)
  1550. {
  1551. int ret;
  1552. /* device should disable */
  1553. ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
  1554. if (ret != 0)
  1555. return;
  1556. unittest(1, "overlay test %d passed\n", 13);
  1557. }
  1558. /* just check for i2c mux existence */
  1559. static void of_unittest_overlay_i2c_14(void)
  1560. {
  1561. }
  1562. static void of_unittest_overlay_i2c_15(void)
  1563. {
  1564. int ret;
  1565. /* device should enable */
  1566. ret = of_unittest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY);
  1567. if (ret != 0)
  1568. return;
  1569. unittest(1, "overlay test %d passed\n", 15);
  1570. }
  1571. #else
  1572. static inline void of_unittest_overlay_i2c_14(void) { }
  1573. static inline void of_unittest_overlay_i2c_15(void) { }
  1574. #endif
  1575. static void __init of_unittest_overlay(void)
  1576. {
  1577. struct device_node *bus_np = NULL;
  1578. int ret;
  1579. ret = platform_driver_register(&unittest_driver);
  1580. if (ret != 0) {
  1581. unittest(0, "could not register unittest driver\n");
  1582. goto out;
  1583. }
  1584. bus_np = of_find_node_by_path(bus_path);
  1585. if (bus_np == NULL) {
  1586. unittest(0, "could not find bus_path \"%s\"\n", bus_path);
  1587. goto out;
  1588. }
  1589. ret = of_platform_populate(bus_np, of_default_bus_match_table,
  1590. NULL, NULL);
  1591. if (ret != 0) {
  1592. unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
  1593. goto out;
  1594. }
  1595. if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
  1596. unittest(0, "could not find unittest0 @ \"%s\"\n",
  1597. unittest_path(100, PDEV_OVERLAY));
  1598. goto out;
  1599. }
  1600. if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
  1601. unittest(0, "unittest1 @ \"%s\" should not exist\n",
  1602. unittest_path(101, PDEV_OVERLAY));
  1603. goto out;
  1604. }
  1605. unittest(1, "basic infrastructure of overlays passed");
  1606. /* tests in sequence */
  1607. of_unittest_overlay_0();
  1608. of_unittest_overlay_1();
  1609. of_unittest_overlay_2();
  1610. of_unittest_overlay_3();
  1611. of_unittest_overlay_4();
  1612. of_unittest_overlay_5();
  1613. of_unittest_overlay_6();
  1614. of_unittest_overlay_8();
  1615. of_unittest_overlay_10();
  1616. of_unittest_overlay_11();
  1617. #if IS_BUILTIN(CONFIG_I2C)
  1618. if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
  1619. goto out;
  1620. of_unittest_overlay_i2c_12();
  1621. of_unittest_overlay_i2c_13();
  1622. of_unittest_overlay_i2c_14();
  1623. of_unittest_overlay_i2c_15();
  1624. of_unittest_overlay_i2c_cleanup();
  1625. #endif
  1626. of_unittest_destroy_tracked_overlays();
  1627. out:
  1628. of_node_put(bus_np);
  1629. }
  1630. #else
  1631. static inline void __init of_unittest_overlay(void) { }
  1632. #endif
  1633. static int __init of_unittest(void)
  1634. {
  1635. struct device_node *np;
  1636. int res;
  1637. /* adding data for unittest */
  1638. res = unittest_data_add();
  1639. if (res)
  1640. return res;
  1641. if (!of_aliases)
  1642. of_aliases = of_find_node_by_path("/aliases");
  1643. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  1644. if (!np) {
  1645. pr_info("No testcase data in device tree; not running tests\n");
  1646. return 0;
  1647. }
  1648. of_node_put(np);
  1649. pr_info("start of unittest - you will see error messages\n");
  1650. of_unittest_check_tree_linkage();
  1651. of_unittest_check_phandles();
  1652. of_unittest_find_node_by_name();
  1653. of_unittest_dynamic();
  1654. of_unittest_parse_phandle_with_args();
  1655. of_unittest_property_string();
  1656. of_unittest_property_copy();
  1657. of_unittest_changeset();
  1658. of_unittest_parse_interrupts();
  1659. of_unittest_parse_interrupts_extended();
  1660. of_unittest_match_node();
  1661. of_unittest_platform_populate();
  1662. of_unittest_overlay();
  1663. /* Double check linkage after removing testcase data */
  1664. of_unittest_check_tree_linkage();
  1665. pr_info("end of unittest - %i passed, %i failed\n",
  1666. unittest_results.passed, unittest_results.failed);
  1667. return 0;
  1668. }
  1669. late_initcall(of_unittest);