unittest.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Self tests for device tree subsystem
  4. */
  5. #define pr_fmt(fmt) "### dt-test ### " fmt
  6. #include <linux/bootmem.h>
  7. #include <linux/clk.h>
  8. #include <linux/err.h>
  9. #include <linux/errno.h>
  10. #include <linux/hashtable.h>
  11. #include <linux/libfdt.h>
  12. #include <linux/of.h>
  13. #include <linux/of_fdt.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/list.h>
  17. #include <linux/mutex.h>
  18. #include <linux/slab.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c-mux.h>
  23. #include <linux/bitops.h>
  24. #include "of_private.h"
  25. static struct unittest_results {
  26. int passed;
  27. int failed;
  28. } unittest_results;
  29. #define unittest(result, fmt, ...) ({ \
  30. bool failed = !(result); \
  31. if (failed) { \
  32. unittest_results.failed++; \
  33. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  34. } else { \
  35. unittest_results.passed++; \
  36. pr_debug("pass %s():%i\n", __func__, __LINE__); \
  37. } \
  38. failed; \
  39. })
  40. static void __init of_unittest_find_node_by_name(void)
  41. {
  42. struct device_node *np;
  43. const char *options, *name;
  44. np = of_find_node_by_path("/testcase-data");
  45. name = kasprintf(GFP_KERNEL, "%pOF", np);
  46. unittest(np && !strcmp("/testcase-data", name),
  47. "find /testcase-data failed\n");
  48. of_node_put(np);
  49. kfree(name);
  50. /* Test if trailing '/' works */
  51. np = of_find_node_by_path("/testcase-data/");
  52. unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
  53. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  54. name = kasprintf(GFP_KERNEL, "%pOF", np);
  55. unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
  56. "find /testcase-data/phandle-tests/consumer-a failed\n");
  57. of_node_put(np);
  58. kfree(name);
  59. np = of_find_node_by_path("testcase-alias");
  60. name = kasprintf(GFP_KERNEL, "%pOF", np);
  61. unittest(np && !strcmp("/testcase-data", name),
  62. "find testcase-alias failed\n");
  63. of_node_put(np);
  64. kfree(name);
  65. /* Test if trailing '/' works on aliases */
  66. np = of_find_node_by_path("testcase-alias/");
  67. unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
  68. np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
  69. name = kasprintf(GFP_KERNEL, "%pOF", np);
  70. unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
  71. "find testcase-alias/phandle-tests/consumer-a failed\n");
  72. of_node_put(np);
  73. kfree(name);
  74. np = of_find_node_by_path("/testcase-data/missing-path");
  75. unittest(!np, "non-existent path returned node %pOF\n", np);
  76. of_node_put(np);
  77. np = of_find_node_by_path("missing-alias");
  78. unittest(!np, "non-existent alias returned node %pOF\n", np);
  79. of_node_put(np);
  80. np = of_find_node_by_path("testcase-alias/missing-path");
  81. unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
  82. of_node_put(np);
  83. np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
  84. unittest(np && !strcmp("testoption", options),
  85. "option path test failed\n");
  86. of_node_put(np);
  87. np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
  88. unittest(np && !strcmp("test/option", options),
  89. "option path test, subcase #1 failed\n");
  90. of_node_put(np);
  91. np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
  92. unittest(np && !strcmp("test/option", options),
  93. "option path test, subcase #2 failed\n");
  94. of_node_put(np);
  95. np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
  96. unittest(np, "NULL option path test failed\n");
  97. of_node_put(np);
  98. np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
  99. &options);
  100. unittest(np && !strcmp("testaliasoption", options),
  101. "option alias path test failed\n");
  102. of_node_put(np);
  103. np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
  104. &options);
  105. unittest(np && !strcmp("test/alias/option", options),
  106. "option alias path test, subcase #1 failed\n");
  107. of_node_put(np);
  108. np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
  109. unittest(np, "NULL option alias path test failed\n");
  110. of_node_put(np);
  111. options = "testoption";
  112. np = of_find_node_opts_by_path("testcase-alias", &options);
  113. unittest(np && !options, "option clearing test failed\n");
  114. of_node_put(np);
  115. options = "testoption";
  116. np = of_find_node_opts_by_path("/", &options);
  117. unittest(np && !options, "option clearing root node test failed\n");
  118. of_node_put(np);
  119. }
  120. static void __init of_unittest_dynamic(void)
  121. {
  122. struct device_node *np;
  123. struct property *prop;
  124. np = of_find_node_by_path("/testcase-data");
  125. if (!np) {
  126. pr_err("missing testcase data\n");
  127. return;
  128. }
  129. /* Array of 4 properties for the purpose of testing */
  130. prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
  131. if (!prop) {
  132. unittest(0, "kzalloc() failed\n");
  133. return;
  134. }
  135. /* Add a new property - should pass*/
  136. prop->name = "new-property";
  137. prop->value = "new-property-data";
  138. prop->length = strlen(prop->value) + 1;
  139. unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
  140. /* Try to add an existing property - should fail */
  141. prop++;
  142. prop->name = "new-property";
  143. prop->value = "new-property-data-should-fail";
  144. prop->length = strlen(prop->value) + 1;
  145. unittest(of_add_property(np, prop) != 0,
  146. "Adding an existing property should have failed\n");
  147. /* Try to modify an existing property - should pass */
  148. prop->value = "modify-property-data-should-pass";
  149. prop->length = strlen(prop->value) + 1;
  150. unittest(of_update_property(np, prop) == 0,
  151. "Updating an existing property should have passed\n");
  152. /* Try to modify non-existent property - should pass*/
  153. prop++;
  154. prop->name = "modify-property";
  155. prop->value = "modify-missing-property-data-should-pass";
  156. prop->length = strlen(prop->value) + 1;
  157. unittest(of_update_property(np, prop) == 0,
  158. "Updating a missing property should have passed\n");
  159. /* Remove property - should pass */
  160. unittest(of_remove_property(np, prop) == 0,
  161. "Removing a property should have passed\n");
  162. /* Adding very large property - should pass */
  163. prop++;
  164. prop->name = "large-property-PAGE_SIZEx8";
  165. prop->length = PAGE_SIZE * 8;
  166. prop->value = kzalloc(prop->length, GFP_KERNEL);
  167. unittest(prop->value != NULL, "Unable to allocate large buffer\n");
  168. if (prop->value)
  169. unittest(of_add_property(np, prop) == 0,
  170. "Adding a large property should have passed\n");
  171. }
  172. static int __init of_unittest_check_node_linkage(struct device_node *np)
  173. {
  174. struct device_node *child;
  175. int count = 0, rc;
  176. for_each_child_of_node(np, child) {
  177. if (child->parent != np) {
  178. pr_err("Child node %pOFn links to wrong parent %pOFn\n",
  179. child, np);
  180. rc = -EINVAL;
  181. goto put_child;
  182. }
  183. rc = of_unittest_check_node_linkage(child);
  184. if (rc < 0)
  185. goto put_child;
  186. count += rc;
  187. }
  188. return count + 1;
  189. put_child:
  190. of_node_put(child);
  191. return rc;
  192. }
  193. static void __init of_unittest_check_tree_linkage(void)
  194. {
  195. struct device_node *np;
  196. int allnode_count = 0, child_count;
  197. if (!of_root)
  198. return;
  199. for_each_of_allnodes(np)
  200. allnode_count++;
  201. child_count = of_unittest_check_node_linkage(of_root);
  202. unittest(child_count > 0, "Device node data structure is corrupted\n");
  203. unittest(child_count == allnode_count,
  204. "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
  205. allnode_count, child_count);
  206. pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
  207. }
  208. static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
  209. const char *expected)
  210. {
  211. unsigned char *buf;
  212. int buf_size;
  213. int size, i;
  214. buf_size = strlen(expected) + 10;
  215. buf = kmalloc(buf_size, GFP_KERNEL);
  216. if (!buf)
  217. return;
  218. /* Baseline; check conversion with a large size limit */
  219. memset(buf, 0xff, buf_size);
  220. size = snprintf(buf, buf_size - 2, fmt, np);
  221. /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
  222. unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
  223. "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
  224. fmt, expected, buf);
  225. /* Make sure length limits work */
  226. size++;
  227. for (i = 0; i < 2; i++, size--) {
  228. /* Clear the buffer, and make sure it works correctly still */
  229. memset(buf, 0xff, buf_size);
  230. snprintf(buf, size+1, fmt, np);
  231. unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
  232. "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
  233. size, fmt, expected, buf);
  234. }
  235. kfree(buf);
  236. }
  237. static void __init of_unittest_printf(void)
  238. {
  239. struct device_node *np;
  240. const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
  241. char phandle_str[16] = "";
  242. np = of_find_node_by_path(full_name);
  243. if (!np) {
  244. unittest(np, "testcase data missing\n");
  245. return;
  246. }
  247. num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
  248. of_unittest_printf_one(np, "%pOF", full_name);
  249. of_unittest_printf_one(np, "%pOFf", full_name);
  250. of_unittest_printf_one(np, "%pOFp", phandle_str);
  251. of_unittest_printf_one(np, "%pOFP", "dev@100");
  252. of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
  253. of_unittest_printf_one(np, "%10pOFP", " dev@100");
  254. of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
  255. of_unittest_printf_one(of_root, "%pOFP", "/");
  256. of_unittest_printf_one(np, "%pOFF", "----");
  257. of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
  258. of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
  259. of_unittest_printf_one(np, "%pOFc", "test-sub-device");
  260. of_unittest_printf_one(np, "%pOFC",
  261. "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
  262. }
  263. struct node_hash {
  264. struct hlist_node node;
  265. struct device_node *np;
  266. };
  267. static DEFINE_HASHTABLE(phandle_ht, 8);
  268. static void __init of_unittest_check_phandles(void)
  269. {
  270. struct device_node *np;
  271. struct node_hash *nh;
  272. struct hlist_node *tmp;
  273. int i, dup_count = 0, phandle_count = 0;
  274. for_each_of_allnodes(np) {
  275. if (!np->phandle)
  276. continue;
  277. hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
  278. if (nh->np->phandle == np->phandle) {
  279. pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
  280. np->phandle, nh->np, np);
  281. dup_count++;
  282. break;
  283. }
  284. }
  285. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  286. if (WARN_ON(!nh))
  287. return;
  288. nh->np = np;
  289. hash_add(phandle_ht, &nh->node, np->phandle);
  290. phandle_count++;
  291. }
  292. unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
  293. dup_count, phandle_count);
  294. /* Clean up */
  295. hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
  296. hash_del(&nh->node);
  297. kfree(nh);
  298. }
  299. }
  300. static void __init of_unittest_parse_phandle_with_args(void)
  301. {
  302. struct device_node *np;
  303. struct of_phandle_args args;
  304. int i, rc;
  305. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  306. if (!np) {
  307. pr_err("missing testcase data\n");
  308. return;
  309. }
  310. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  311. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  312. for (i = 0; i < 8; i++) {
  313. bool passed = true;
  314. rc = of_parse_phandle_with_args(np, "phandle-list",
  315. "#phandle-cells", i, &args);
  316. /* Test the values from tests-phandle.dtsi */
  317. switch (i) {
  318. case 0:
  319. passed &= !rc;
  320. passed &= (args.args_count == 1);
  321. passed &= (args.args[0] == (i + 1));
  322. break;
  323. case 1:
  324. passed &= !rc;
  325. passed &= (args.args_count == 2);
  326. passed &= (args.args[0] == (i + 1));
  327. passed &= (args.args[1] == 0);
  328. break;
  329. case 2:
  330. passed &= (rc == -ENOENT);
  331. break;
  332. case 3:
  333. passed &= !rc;
  334. passed &= (args.args_count == 3);
  335. passed &= (args.args[0] == (i + 1));
  336. passed &= (args.args[1] == 4);
  337. passed &= (args.args[2] == 3);
  338. break;
  339. case 4:
  340. passed &= !rc;
  341. passed &= (args.args_count == 2);
  342. passed &= (args.args[0] == (i + 1));
  343. passed &= (args.args[1] == 100);
  344. break;
  345. case 5:
  346. passed &= !rc;
  347. passed &= (args.args_count == 0);
  348. break;
  349. case 6:
  350. passed &= !rc;
  351. passed &= (args.args_count == 1);
  352. passed &= (args.args[0] == (i + 1));
  353. break;
  354. case 7:
  355. passed &= (rc == -ENOENT);
  356. break;
  357. default:
  358. passed = false;
  359. }
  360. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  361. i, args.np, rc);
  362. }
  363. /* Check for missing list property */
  364. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  365. "#phandle-cells", 0, &args);
  366. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  367. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  368. "#phandle-cells");
  369. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  370. /* Check for missing cells property */
  371. rc = of_parse_phandle_with_args(np, "phandle-list",
  372. "#phandle-cells-missing", 0, &args);
  373. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  374. rc = of_count_phandle_with_args(np, "phandle-list",
  375. "#phandle-cells-missing");
  376. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  377. /* Check for bad phandle in list */
  378. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  379. "#phandle-cells", 0, &args);
  380. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  381. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  382. "#phandle-cells");
  383. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  384. /* Check for incorrectly formed argument list */
  385. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  386. "#phandle-cells", 1, &args);
  387. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  388. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  389. "#phandle-cells");
  390. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  391. }
  392. static void __init of_unittest_parse_phandle_with_args_map(void)
  393. {
  394. struct device_node *np, *p0, *p1, *p2, *p3;
  395. struct of_phandle_args args;
  396. int i, rc;
  397. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
  398. if (!np) {
  399. pr_err("missing testcase data\n");
  400. return;
  401. }
  402. p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
  403. if (!p0) {
  404. pr_err("missing testcase data\n");
  405. return;
  406. }
  407. p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
  408. if (!p1) {
  409. pr_err("missing testcase data\n");
  410. return;
  411. }
  412. p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
  413. if (!p2) {
  414. pr_err("missing testcase data\n");
  415. return;
  416. }
  417. p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
  418. if (!p3) {
  419. pr_err("missing testcase data\n");
  420. return;
  421. }
  422. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  423. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  424. for (i = 0; i < 8; i++) {
  425. bool passed = true;
  426. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  427. "phandle", i, &args);
  428. /* Test the values from tests-phandle.dtsi */
  429. switch (i) {
  430. case 0:
  431. passed &= !rc;
  432. passed &= (args.np == p1);
  433. passed &= (args.args_count == 1);
  434. passed &= (args.args[0] == 1);
  435. break;
  436. case 1:
  437. passed &= !rc;
  438. passed &= (args.np == p3);
  439. passed &= (args.args_count == 3);
  440. passed &= (args.args[0] == 2);
  441. passed &= (args.args[1] == 5);
  442. passed &= (args.args[2] == 3);
  443. break;
  444. case 2:
  445. passed &= (rc == -ENOENT);
  446. break;
  447. case 3:
  448. passed &= !rc;
  449. passed &= (args.np == p0);
  450. passed &= (args.args_count == 0);
  451. break;
  452. case 4:
  453. passed &= !rc;
  454. passed &= (args.np == p1);
  455. passed &= (args.args_count == 1);
  456. passed &= (args.args[0] == 3);
  457. break;
  458. case 5:
  459. passed &= !rc;
  460. passed &= (args.np == p0);
  461. passed &= (args.args_count == 0);
  462. break;
  463. case 6:
  464. passed &= !rc;
  465. passed &= (args.np == p2);
  466. passed &= (args.args_count == 2);
  467. passed &= (args.args[0] == 15);
  468. passed &= (args.args[1] == 0x20);
  469. break;
  470. case 7:
  471. passed &= (rc == -ENOENT);
  472. break;
  473. default:
  474. passed = false;
  475. }
  476. unittest(passed, "index %i - data error on node %s rc=%i\n",
  477. i, args.np->full_name, rc);
  478. }
  479. /* Check for missing list property */
  480. rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
  481. "phandle", 0, &args);
  482. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  483. /* Check for missing cells,map,mask property */
  484. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  485. "phandle-missing", 0, &args);
  486. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  487. /* Check for bad phandle in list */
  488. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
  489. "phandle", 0, &args);
  490. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  491. /* Check for incorrectly formed argument list */
  492. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
  493. "phandle", 1, &args);
  494. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  495. }
  496. static void __init of_unittest_property_string(void)
  497. {
  498. const char *strings[4];
  499. struct device_node *np;
  500. int rc;
  501. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  502. if (!np) {
  503. pr_err("No testcase data in device tree\n");
  504. return;
  505. }
  506. rc = of_property_match_string(np, "phandle-list-names", "first");
  507. unittest(rc == 0, "first expected:0 got:%i\n", rc);
  508. rc = of_property_match_string(np, "phandle-list-names", "second");
  509. unittest(rc == 1, "second expected:1 got:%i\n", rc);
  510. rc = of_property_match_string(np, "phandle-list-names", "third");
  511. unittest(rc == 2, "third expected:2 got:%i\n", rc);
  512. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  513. unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  514. rc = of_property_match_string(np, "missing-property", "blah");
  515. unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  516. rc = of_property_match_string(np, "empty-property", "blah");
  517. unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  518. rc = of_property_match_string(np, "unterminated-string", "blah");
  519. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  520. /* of_property_count_strings() tests */
  521. rc = of_property_count_strings(np, "string-property");
  522. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  523. rc = of_property_count_strings(np, "phandle-list-names");
  524. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  525. rc = of_property_count_strings(np, "unterminated-string");
  526. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  527. rc = of_property_count_strings(np, "unterminated-string-list");
  528. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  529. /* of_property_read_string_index() tests */
  530. rc = of_property_read_string_index(np, "string-property", 0, strings);
  531. unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  532. strings[0] = NULL;
  533. rc = of_property_read_string_index(np, "string-property", 1, strings);
  534. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  535. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  536. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  537. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  538. unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  539. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  540. unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  541. strings[0] = NULL;
  542. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  543. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  544. strings[0] = NULL;
  545. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  546. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  547. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  548. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  549. strings[0] = NULL;
  550. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  551. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  552. strings[1] = NULL;
  553. /* of_property_read_string_array() tests */
  554. rc = of_property_read_string_array(np, "string-property", strings, 4);
  555. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  556. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  557. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  558. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  559. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  560. /* -- An incorrectly formed string should cause a failure */
  561. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  562. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  563. /* -- parsing the correctly formed strings should still work: */
  564. strings[2] = NULL;
  565. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  566. unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  567. strings[1] = NULL;
  568. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  569. unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  570. }
  571. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  572. (p1)->value && (p2)->value && \
  573. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  574. !strcmp((p1)->name, (p2)->name))
  575. static void __init of_unittest_property_copy(void)
  576. {
  577. #ifdef CONFIG_OF_DYNAMIC
  578. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  579. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  580. struct property *new;
  581. new = __of_prop_dup(&p1, GFP_KERNEL);
  582. unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  583. kfree(new->value);
  584. kfree(new->name);
  585. kfree(new);
  586. new = __of_prop_dup(&p2, GFP_KERNEL);
  587. unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  588. kfree(new->value);
  589. kfree(new->name);
  590. kfree(new);
  591. #endif
  592. }
  593. static void __init of_unittest_changeset(void)
  594. {
  595. #ifdef CONFIG_OF_DYNAMIC
  596. struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
  597. struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
  598. struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
  599. struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
  600. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  601. struct property *ppremove;
  602. struct device_node *n1, *n2, *n21, *nchangeset, *nremove, *parent, *np;
  603. struct of_changeset chgset;
  604. n1 = __of_node_dup(NULL, "n1");
  605. unittest(n1, "testcase setup failure\n");
  606. n2 = __of_node_dup(NULL, "n2");
  607. unittest(n2, "testcase setup failure\n");
  608. n21 = __of_node_dup(NULL, "n21");
  609. unittest(n21, "testcase setup failure %p\n", n21);
  610. nchangeset = of_find_node_by_path("/testcase-data/changeset");
  611. nremove = of_get_child_by_name(nchangeset, "node-remove");
  612. unittest(nremove, "testcase setup failure\n");
  613. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  614. unittest(ppadd, "testcase setup failure\n");
  615. ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
  616. unittest(ppname_n1, "testcase setup failure\n");
  617. ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
  618. unittest(ppname_n2, "testcase setup failure\n");
  619. ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
  620. unittest(ppname_n21, "testcase setup failure\n");
  621. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  622. unittest(ppupdate, "testcase setup failure\n");
  623. parent = nchangeset;
  624. n1->parent = parent;
  625. n2->parent = parent;
  626. n21->parent = n2;
  627. ppremove = of_find_property(parent, "prop-remove", NULL);
  628. unittest(ppremove, "failed to find removal prop");
  629. of_changeset_init(&chgset);
  630. unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  631. unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
  632. unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  633. unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
  634. unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  635. unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
  636. unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  637. unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
  638. unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  639. unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  640. unittest(!of_changeset_apply(&chgset), "apply failed\n");
  641. of_node_put(nchangeset);
  642. /* Make sure node names are constructed correctly */
  643. unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
  644. "'%pOF' not added\n", n21);
  645. of_node_put(np);
  646. unittest(!of_changeset_revert(&chgset), "revert failed\n");
  647. of_changeset_destroy(&chgset);
  648. #endif
  649. }
  650. static void __init of_unittest_parse_interrupts(void)
  651. {
  652. struct device_node *np;
  653. struct of_phandle_args args;
  654. int i, rc;
  655. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  656. return;
  657. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  658. if (!np) {
  659. pr_err("missing testcase data\n");
  660. return;
  661. }
  662. for (i = 0; i < 4; i++) {
  663. bool passed = true;
  664. args.args_count = 0;
  665. rc = of_irq_parse_one(np, i, &args);
  666. passed &= !rc;
  667. passed &= (args.args_count == 1);
  668. passed &= (args.args[0] == (i + 1));
  669. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  670. i, args.np, rc);
  671. }
  672. of_node_put(np);
  673. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  674. if (!np) {
  675. pr_err("missing testcase data\n");
  676. return;
  677. }
  678. for (i = 0; i < 4; i++) {
  679. bool passed = true;
  680. args.args_count = 0;
  681. rc = of_irq_parse_one(np, i, &args);
  682. /* Test the values from tests-phandle.dtsi */
  683. switch (i) {
  684. case 0:
  685. passed &= !rc;
  686. passed &= (args.args_count == 1);
  687. passed &= (args.args[0] == 9);
  688. break;
  689. case 1:
  690. passed &= !rc;
  691. passed &= (args.args_count == 3);
  692. passed &= (args.args[0] == 10);
  693. passed &= (args.args[1] == 11);
  694. passed &= (args.args[2] == 12);
  695. break;
  696. case 2:
  697. passed &= !rc;
  698. passed &= (args.args_count == 2);
  699. passed &= (args.args[0] == 13);
  700. passed &= (args.args[1] == 14);
  701. break;
  702. case 3:
  703. passed &= !rc;
  704. passed &= (args.args_count == 2);
  705. passed &= (args.args[0] == 15);
  706. passed &= (args.args[1] == 16);
  707. break;
  708. default:
  709. passed = false;
  710. }
  711. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  712. i, args.np, rc);
  713. }
  714. of_node_put(np);
  715. }
  716. static void __init of_unittest_parse_interrupts_extended(void)
  717. {
  718. struct device_node *np;
  719. struct of_phandle_args args;
  720. int i, rc;
  721. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  722. return;
  723. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  724. if (!np) {
  725. pr_err("missing testcase data\n");
  726. return;
  727. }
  728. for (i = 0; i < 7; i++) {
  729. bool passed = true;
  730. rc = of_irq_parse_one(np, i, &args);
  731. /* Test the values from tests-phandle.dtsi */
  732. switch (i) {
  733. case 0:
  734. passed &= !rc;
  735. passed &= (args.args_count == 1);
  736. passed &= (args.args[0] == 1);
  737. break;
  738. case 1:
  739. passed &= !rc;
  740. passed &= (args.args_count == 3);
  741. passed &= (args.args[0] == 2);
  742. passed &= (args.args[1] == 3);
  743. passed &= (args.args[2] == 4);
  744. break;
  745. case 2:
  746. passed &= !rc;
  747. passed &= (args.args_count == 2);
  748. passed &= (args.args[0] == 5);
  749. passed &= (args.args[1] == 6);
  750. break;
  751. case 3:
  752. passed &= !rc;
  753. passed &= (args.args_count == 1);
  754. passed &= (args.args[0] == 9);
  755. break;
  756. case 4:
  757. passed &= !rc;
  758. passed &= (args.args_count == 3);
  759. passed &= (args.args[0] == 10);
  760. passed &= (args.args[1] == 11);
  761. passed &= (args.args[2] == 12);
  762. break;
  763. case 5:
  764. passed &= !rc;
  765. passed &= (args.args_count == 2);
  766. passed &= (args.args[0] == 13);
  767. passed &= (args.args[1] == 14);
  768. break;
  769. case 6:
  770. passed &= !rc;
  771. passed &= (args.args_count == 1);
  772. passed &= (args.args[0] == 15);
  773. break;
  774. default:
  775. passed = false;
  776. }
  777. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  778. i, args.np, rc);
  779. }
  780. of_node_put(np);
  781. }
  782. static const struct of_device_id match_node_table[] = {
  783. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  784. { .data = "B", .type = "type1", }, /* followed by type alone */
  785. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  786. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  787. { .data = "Cc", .name = "name2", .type = "type2", },
  788. { .data = "E", .compatible = "compat3" },
  789. { .data = "G", .compatible = "compat2", },
  790. { .data = "H", .compatible = "compat2", .name = "name5", },
  791. { .data = "I", .compatible = "compat2", .type = "type1", },
  792. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  793. { .data = "K", .compatible = "compat2", .name = "name9", },
  794. {}
  795. };
  796. static struct {
  797. const char *path;
  798. const char *data;
  799. } match_node_tests[] = {
  800. { .path = "/testcase-data/match-node/name0", .data = "A", },
  801. { .path = "/testcase-data/match-node/name1", .data = "B", },
  802. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  803. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  804. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  805. { .path = "/testcase-data/match-node/name3", .data = "E", },
  806. { .path = "/testcase-data/match-node/name4", .data = "G", },
  807. { .path = "/testcase-data/match-node/name5", .data = "H", },
  808. { .path = "/testcase-data/match-node/name6", .data = "G", },
  809. { .path = "/testcase-data/match-node/name7", .data = "I", },
  810. { .path = "/testcase-data/match-node/name8", .data = "J", },
  811. { .path = "/testcase-data/match-node/name9", .data = "K", },
  812. };
  813. static void __init of_unittest_match_node(void)
  814. {
  815. struct device_node *np;
  816. const struct of_device_id *match;
  817. int i;
  818. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  819. np = of_find_node_by_path(match_node_tests[i].path);
  820. if (!np) {
  821. unittest(0, "missing testcase node %s\n",
  822. match_node_tests[i].path);
  823. continue;
  824. }
  825. match = of_match_node(match_node_table, np);
  826. if (!match) {
  827. unittest(0, "%s didn't match anything\n",
  828. match_node_tests[i].path);
  829. continue;
  830. }
  831. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  832. unittest(0, "%s got wrong match. expected %s, got %s\n",
  833. match_node_tests[i].path, match_node_tests[i].data,
  834. (const char *)match->data);
  835. continue;
  836. }
  837. unittest(1, "passed");
  838. }
  839. }
  840. static struct resource test_bus_res = {
  841. .start = 0xfffffff8,
  842. .end = 0xfffffff9,
  843. .flags = IORESOURCE_MEM,
  844. };
  845. static const struct platform_device_info test_bus_info = {
  846. .name = "unittest-bus",
  847. };
  848. static void __init of_unittest_platform_populate(void)
  849. {
  850. int irq, rc;
  851. struct device_node *np, *child, *grandchild;
  852. struct platform_device *pdev, *test_bus;
  853. const struct of_device_id match[] = {
  854. { .compatible = "test-device", },
  855. {}
  856. };
  857. np = of_find_node_by_path("/testcase-data");
  858. of_platform_default_populate(np, NULL, NULL);
  859. /* Test that a missing irq domain returns -EPROBE_DEFER */
  860. np = of_find_node_by_path("/testcase-data/testcase-device1");
  861. pdev = of_find_device_by_node(np);
  862. unittest(pdev, "device 1 creation failed\n");
  863. if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
  864. irq = platform_get_irq(pdev, 0);
  865. unittest(irq == -EPROBE_DEFER,
  866. "device deferred probe failed - %d\n", irq);
  867. /* Test that a parsing failure does not return -EPROBE_DEFER */
  868. np = of_find_node_by_path("/testcase-data/testcase-device2");
  869. pdev = of_find_device_by_node(np);
  870. unittest(pdev, "device 2 creation failed\n");
  871. irq = platform_get_irq(pdev, 0);
  872. unittest(irq < 0 && irq != -EPROBE_DEFER,
  873. "device parsing error failed - %d\n", irq);
  874. }
  875. np = of_find_node_by_path("/testcase-data/platform-tests");
  876. unittest(np, "No testcase data in device tree\n");
  877. if (!np)
  878. return;
  879. test_bus = platform_device_register_full(&test_bus_info);
  880. rc = PTR_ERR_OR_ZERO(test_bus);
  881. unittest(!rc, "testbus registration failed; rc=%i\n", rc);
  882. if (rc)
  883. return;
  884. test_bus->dev.of_node = np;
  885. /*
  886. * Add a dummy resource to the test bus node after it is
  887. * registered to catch problems with un-inserted resources. The
  888. * DT code doesn't insert the resources, and it has caused the
  889. * kernel to oops in the past. This makes sure the same bug
  890. * doesn't crop up again.
  891. */
  892. platform_device_add_resources(test_bus, &test_bus_res, 1);
  893. of_platform_populate(np, match, NULL, &test_bus->dev);
  894. for_each_child_of_node(np, child) {
  895. for_each_child_of_node(child, grandchild)
  896. unittest(of_find_device_by_node(grandchild),
  897. "Could not create device for node '%pOFn'\n",
  898. grandchild);
  899. }
  900. of_platform_depopulate(&test_bus->dev);
  901. for_each_child_of_node(np, child) {
  902. for_each_child_of_node(child, grandchild)
  903. unittest(!of_find_device_by_node(grandchild),
  904. "device didn't get destroyed '%pOFn'\n",
  905. grandchild);
  906. }
  907. platform_device_unregister(test_bus);
  908. of_node_put(np);
  909. }
  910. /**
  911. * update_node_properties - adds the properties
  912. * of np into dup node (present in live tree) and
  913. * updates parent of children of np to dup.
  914. *
  915. * @np: node already present in live tree
  916. * @dup: node present in live tree to be updated
  917. */
  918. static void update_node_properties(struct device_node *np,
  919. struct device_node *dup)
  920. {
  921. struct property *prop;
  922. struct device_node *child;
  923. for_each_property_of_node(np, prop)
  924. of_add_property(dup, prop);
  925. for_each_child_of_node(np, child)
  926. child->parent = dup;
  927. }
  928. /**
  929. * attach_node_and_children - attaches nodes
  930. * and its children to live tree
  931. *
  932. * @np: Node to attach to live tree
  933. */
  934. static int attach_node_and_children(struct device_node *np)
  935. {
  936. struct device_node *next, *dup, *child;
  937. unsigned long flags;
  938. const char *full_name;
  939. full_name = kasprintf(GFP_KERNEL, "%pOF", np);
  940. dup = of_find_node_by_path(full_name);
  941. kfree(full_name);
  942. if (dup) {
  943. update_node_properties(np, dup);
  944. return 0;
  945. }
  946. child = np->child;
  947. np->child = NULL;
  948. mutex_lock(&of_mutex);
  949. raw_spin_lock_irqsave(&devtree_lock, flags);
  950. np->sibling = np->parent->child;
  951. np->parent->child = np;
  952. of_node_clear_flag(np, OF_DETACHED);
  953. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  954. __of_attach_node_sysfs(np);
  955. mutex_unlock(&of_mutex);
  956. while (child) {
  957. next = child->sibling;
  958. attach_node_and_children(child);
  959. child = next;
  960. }
  961. return 0;
  962. }
  963. /**
  964. * unittest_data_add - Reads, copies data from
  965. * linked tree and attaches it to the live tree
  966. */
  967. static int __init unittest_data_add(void)
  968. {
  969. void *unittest_data;
  970. struct device_node *unittest_data_node, *np;
  971. /*
  972. * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
  973. * created by cmd_dt_S_dtb in scripts/Makefile.lib
  974. */
  975. extern uint8_t __dtb_testcases_begin[];
  976. extern uint8_t __dtb_testcases_end[];
  977. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  978. int rc;
  979. if (!size) {
  980. pr_warn("%s: No testcase data to attach; not running tests\n",
  981. __func__);
  982. return -ENODATA;
  983. }
  984. /* creating copy */
  985. unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  986. if (!unittest_data) {
  987. pr_warn("%s: Failed to allocate memory for unittest_data; "
  988. "not running tests\n", __func__);
  989. return -ENOMEM;
  990. }
  991. of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
  992. if (!unittest_data_node) {
  993. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  994. return -ENODATA;
  995. }
  996. /*
  997. * This lock normally encloses of_resolve_phandles()
  998. */
  999. of_overlay_mutex_lock();
  1000. rc = of_resolve_phandles(unittest_data_node);
  1001. if (rc) {
  1002. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  1003. of_overlay_mutex_unlock();
  1004. return -EINVAL;
  1005. }
  1006. if (!of_root) {
  1007. of_root = unittest_data_node;
  1008. for_each_of_allnodes(np)
  1009. __of_attach_node_sysfs(np);
  1010. of_aliases = of_find_node_by_path("/aliases");
  1011. of_chosen = of_find_node_by_path("/chosen");
  1012. of_overlay_mutex_unlock();
  1013. return 0;
  1014. }
  1015. /* attach the sub-tree to live tree */
  1016. np = unittest_data_node->child;
  1017. while (np) {
  1018. struct device_node *next = np->sibling;
  1019. np->parent = of_root;
  1020. attach_node_and_children(np);
  1021. np = next;
  1022. }
  1023. of_overlay_mutex_unlock();
  1024. return 0;
  1025. }
  1026. #ifdef CONFIG_OF_OVERLAY
  1027. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
  1028. static int unittest_probe(struct platform_device *pdev)
  1029. {
  1030. struct device *dev = &pdev->dev;
  1031. struct device_node *np = dev->of_node;
  1032. if (np == NULL) {
  1033. dev_err(dev, "No OF data for device\n");
  1034. return -EINVAL;
  1035. }
  1036. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1037. of_platform_populate(np, NULL, NULL, &pdev->dev);
  1038. return 0;
  1039. }
  1040. static int unittest_remove(struct platform_device *pdev)
  1041. {
  1042. struct device *dev = &pdev->dev;
  1043. struct device_node *np = dev->of_node;
  1044. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1045. return 0;
  1046. }
  1047. static const struct of_device_id unittest_match[] = {
  1048. { .compatible = "unittest", },
  1049. {},
  1050. };
  1051. static struct platform_driver unittest_driver = {
  1052. .probe = unittest_probe,
  1053. .remove = unittest_remove,
  1054. .driver = {
  1055. .name = "unittest",
  1056. .of_match_table = of_match_ptr(unittest_match),
  1057. },
  1058. };
  1059. /* get the platform device instantiated at the path */
  1060. static struct platform_device *of_path_to_platform_device(const char *path)
  1061. {
  1062. struct device_node *np;
  1063. struct platform_device *pdev;
  1064. np = of_find_node_by_path(path);
  1065. if (np == NULL)
  1066. return NULL;
  1067. pdev = of_find_device_by_node(np);
  1068. of_node_put(np);
  1069. return pdev;
  1070. }
  1071. /* find out if a platform device exists at that path */
  1072. static int of_path_platform_device_exists(const char *path)
  1073. {
  1074. struct platform_device *pdev;
  1075. pdev = of_path_to_platform_device(path);
  1076. platform_device_put(pdev);
  1077. return pdev != NULL;
  1078. }
  1079. #if IS_BUILTIN(CONFIG_I2C)
  1080. /* get the i2c client device instantiated at the path */
  1081. static struct i2c_client *of_path_to_i2c_client(const char *path)
  1082. {
  1083. struct device_node *np;
  1084. struct i2c_client *client;
  1085. np = of_find_node_by_path(path);
  1086. if (np == NULL)
  1087. return NULL;
  1088. client = of_find_i2c_device_by_node(np);
  1089. of_node_put(np);
  1090. return client;
  1091. }
  1092. /* find out if a i2c client device exists at that path */
  1093. static int of_path_i2c_client_exists(const char *path)
  1094. {
  1095. struct i2c_client *client;
  1096. client = of_path_to_i2c_client(path);
  1097. if (client)
  1098. put_device(&client->dev);
  1099. return client != NULL;
  1100. }
  1101. #else
  1102. static int of_path_i2c_client_exists(const char *path)
  1103. {
  1104. return 0;
  1105. }
  1106. #endif
  1107. enum overlay_type {
  1108. PDEV_OVERLAY,
  1109. I2C_OVERLAY
  1110. };
  1111. static int of_path_device_type_exists(const char *path,
  1112. enum overlay_type ovtype)
  1113. {
  1114. switch (ovtype) {
  1115. case PDEV_OVERLAY:
  1116. return of_path_platform_device_exists(path);
  1117. case I2C_OVERLAY:
  1118. return of_path_i2c_client_exists(path);
  1119. }
  1120. return 0;
  1121. }
  1122. static const char *unittest_path(int nr, enum overlay_type ovtype)
  1123. {
  1124. const char *base;
  1125. static char buf[256];
  1126. switch (ovtype) {
  1127. case PDEV_OVERLAY:
  1128. base = "/testcase-data/overlay-node/test-bus";
  1129. break;
  1130. case I2C_OVERLAY:
  1131. base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
  1132. break;
  1133. default:
  1134. buf[0] = '\0';
  1135. return buf;
  1136. }
  1137. snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
  1138. buf[sizeof(buf) - 1] = '\0';
  1139. return buf;
  1140. }
  1141. static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
  1142. {
  1143. const char *path;
  1144. path = unittest_path(unittest_nr, ovtype);
  1145. switch (ovtype) {
  1146. case PDEV_OVERLAY:
  1147. return of_path_platform_device_exists(path);
  1148. case I2C_OVERLAY:
  1149. return of_path_i2c_client_exists(path);
  1150. }
  1151. return 0;
  1152. }
  1153. static const char *overlay_name_from_nr(int nr)
  1154. {
  1155. static char buf[256];
  1156. snprintf(buf, sizeof(buf) - 1,
  1157. "overlay_%d", nr);
  1158. buf[sizeof(buf) - 1] = '\0';
  1159. return buf;
  1160. }
  1161. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  1162. /* it is guaranteed that overlay ids are assigned in sequence */
  1163. #define MAX_UNITTEST_OVERLAYS 256
  1164. static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
  1165. static int overlay_first_id = -1;
  1166. static void of_unittest_track_overlay(int id)
  1167. {
  1168. if (overlay_first_id < 0)
  1169. overlay_first_id = id;
  1170. id -= overlay_first_id;
  1171. /* we shouldn't need that many */
  1172. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  1173. overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
  1174. }
  1175. static void of_unittest_untrack_overlay(int id)
  1176. {
  1177. if (overlay_first_id < 0)
  1178. return;
  1179. id -= overlay_first_id;
  1180. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  1181. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1182. }
  1183. static void of_unittest_destroy_tracked_overlays(void)
  1184. {
  1185. int id, ret, defers, ovcs_id;
  1186. if (overlay_first_id < 0)
  1187. return;
  1188. /* try until no defers */
  1189. do {
  1190. defers = 0;
  1191. /* remove in reverse order */
  1192. for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
  1193. if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
  1194. continue;
  1195. ovcs_id = id + overlay_first_id;
  1196. ret = of_overlay_remove(&ovcs_id);
  1197. if (ret == -ENODEV) {
  1198. pr_warn("%s: no overlay to destroy for #%d\n",
  1199. __func__, id + overlay_first_id);
  1200. continue;
  1201. }
  1202. if (ret != 0) {
  1203. defers++;
  1204. pr_warn("%s: overlay destroy failed for #%d\n",
  1205. __func__, id + overlay_first_id);
  1206. continue;
  1207. }
  1208. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1209. }
  1210. } while (defers > 0);
  1211. }
  1212. static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
  1213. int *overlay_id)
  1214. {
  1215. const char *overlay_name;
  1216. overlay_name = overlay_name_from_nr(overlay_nr);
  1217. if (!overlay_data_apply(overlay_name, overlay_id)) {
  1218. unittest(0, "could not apply overlay \"%s\"\n",
  1219. overlay_name);
  1220. return -EFAULT;
  1221. }
  1222. of_unittest_track_overlay(*overlay_id);
  1223. return 0;
  1224. }
  1225. /* apply an overlay while checking before and after states */
  1226. static int __init of_unittest_apply_overlay_check(int overlay_nr,
  1227. int unittest_nr, int before, int after,
  1228. enum overlay_type ovtype)
  1229. {
  1230. int ret, ovcs_id;
  1231. /* unittest device must not be in before state */
  1232. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1233. unittest(0, "%s with device @\"%s\" %s\n",
  1234. overlay_name_from_nr(overlay_nr),
  1235. unittest_path(unittest_nr, ovtype),
  1236. !before ? "enabled" : "disabled");
  1237. return -EINVAL;
  1238. }
  1239. ovcs_id = 0;
  1240. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
  1241. if (ret != 0) {
  1242. /* of_unittest_apply_overlay already called unittest() */
  1243. return ret;
  1244. }
  1245. /* unittest device must be to set to after state */
  1246. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1247. unittest(0, "%s failed to create @\"%s\" %s\n",
  1248. overlay_name_from_nr(overlay_nr),
  1249. unittest_path(unittest_nr, ovtype),
  1250. !after ? "enabled" : "disabled");
  1251. return -EINVAL;
  1252. }
  1253. return 0;
  1254. }
  1255. /* apply an overlay and then revert it while checking before, after states */
  1256. static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
  1257. int unittest_nr, int before, int after,
  1258. enum overlay_type ovtype)
  1259. {
  1260. int ret, ovcs_id;
  1261. /* unittest device must be in before state */
  1262. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1263. unittest(0, "%s with device @\"%s\" %s\n",
  1264. overlay_name_from_nr(overlay_nr),
  1265. unittest_path(unittest_nr, ovtype),
  1266. !before ? "enabled" : "disabled");
  1267. return -EINVAL;
  1268. }
  1269. /* apply the overlay */
  1270. ovcs_id = 0;
  1271. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
  1272. if (ret != 0) {
  1273. /* of_unittest_apply_overlay already called unittest() */
  1274. return ret;
  1275. }
  1276. /* unittest device must be in after state */
  1277. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1278. unittest(0, "%s failed to create @\"%s\" %s\n",
  1279. overlay_name_from_nr(overlay_nr),
  1280. unittest_path(unittest_nr, ovtype),
  1281. !after ? "enabled" : "disabled");
  1282. return -EINVAL;
  1283. }
  1284. ret = of_overlay_remove(&ovcs_id);
  1285. if (ret != 0) {
  1286. unittest(0, "%s failed to be destroyed @\"%s\"\n",
  1287. overlay_name_from_nr(overlay_nr),
  1288. unittest_path(unittest_nr, ovtype));
  1289. return ret;
  1290. }
  1291. /* unittest device must be again in before state */
  1292. if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
  1293. unittest(0, "%s with device @\"%s\" %s\n",
  1294. overlay_name_from_nr(overlay_nr),
  1295. unittest_path(unittest_nr, ovtype),
  1296. !before ? "enabled" : "disabled");
  1297. return -EINVAL;
  1298. }
  1299. return 0;
  1300. }
  1301. /* test activation of device */
  1302. static void __init of_unittest_overlay_0(void)
  1303. {
  1304. /* device should enable */
  1305. if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY))
  1306. return;
  1307. unittest(1, "overlay test %d passed\n", 0);
  1308. }
  1309. /* test deactivation of device */
  1310. static void __init of_unittest_overlay_1(void)
  1311. {
  1312. /* device should disable */
  1313. if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY))
  1314. return;
  1315. unittest(1, "overlay test %d passed\n", 1);
  1316. }
  1317. /* test activation of device */
  1318. static void __init of_unittest_overlay_2(void)
  1319. {
  1320. /* device should enable */
  1321. if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY))
  1322. return;
  1323. unittest(1, "overlay test %d passed\n", 2);
  1324. }
  1325. /* test deactivation of device */
  1326. static void __init of_unittest_overlay_3(void)
  1327. {
  1328. /* device should disable */
  1329. if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY))
  1330. return;
  1331. unittest(1, "overlay test %d passed\n", 3);
  1332. }
  1333. /* test activation of a full device node */
  1334. static void __init of_unittest_overlay_4(void)
  1335. {
  1336. /* device should disable */
  1337. if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
  1338. return;
  1339. unittest(1, "overlay test %d passed\n", 4);
  1340. }
  1341. /* test overlay apply/revert sequence */
  1342. static void __init of_unittest_overlay_5(void)
  1343. {
  1344. /* device should disable */
  1345. if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY))
  1346. return;
  1347. unittest(1, "overlay test %d passed\n", 5);
  1348. }
  1349. /* test overlay application in sequence */
  1350. static void __init of_unittest_overlay_6(void)
  1351. {
  1352. int i, ov_id[2], ovcs_id;
  1353. int overlay_nr = 6, unittest_nr = 6;
  1354. int before = 0, after = 1;
  1355. const char *overlay_name;
  1356. /* unittest device must be in before state */
  1357. for (i = 0; i < 2; i++) {
  1358. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1359. != before) {
  1360. unittest(0, "%s with device @\"%s\" %s\n",
  1361. overlay_name_from_nr(overlay_nr + i),
  1362. unittest_path(unittest_nr + i,
  1363. PDEV_OVERLAY),
  1364. !before ? "enabled" : "disabled");
  1365. return;
  1366. }
  1367. }
  1368. /* apply the overlays */
  1369. for (i = 0; i < 2; i++) {
  1370. overlay_name = overlay_name_from_nr(overlay_nr + i);
  1371. if (!overlay_data_apply(overlay_name, &ovcs_id)) {
  1372. unittest(0, "could not apply overlay \"%s\"\n",
  1373. overlay_name);
  1374. return;
  1375. }
  1376. ov_id[i] = ovcs_id;
  1377. of_unittest_track_overlay(ov_id[i]);
  1378. }
  1379. for (i = 0; i < 2; i++) {
  1380. /* unittest device must be in after state */
  1381. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1382. != after) {
  1383. unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  1384. overlay_name_from_nr(overlay_nr + i),
  1385. unittest_path(unittest_nr + i,
  1386. PDEV_OVERLAY),
  1387. !after ? "enabled" : "disabled");
  1388. return;
  1389. }
  1390. }
  1391. for (i = 1; i >= 0; i--) {
  1392. ovcs_id = ov_id[i];
  1393. if (of_overlay_remove(&ovcs_id)) {
  1394. unittest(0, "%s failed destroy @\"%s\"\n",
  1395. overlay_name_from_nr(overlay_nr + i),
  1396. unittest_path(unittest_nr + i,
  1397. PDEV_OVERLAY));
  1398. return;
  1399. }
  1400. of_unittest_untrack_overlay(ov_id[i]);
  1401. }
  1402. for (i = 0; i < 2; i++) {
  1403. /* unittest device must be again in before state */
  1404. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1405. != before) {
  1406. unittest(0, "%s with device @\"%s\" %s\n",
  1407. overlay_name_from_nr(overlay_nr + i),
  1408. unittest_path(unittest_nr + i,
  1409. PDEV_OVERLAY),
  1410. !before ? "enabled" : "disabled");
  1411. return;
  1412. }
  1413. }
  1414. unittest(1, "overlay test %d passed\n", 6);
  1415. }
  1416. /* test overlay application in sequence */
  1417. static void __init of_unittest_overlay_8(void)
  1418. {
  1419. int i, ov_id[2], ovcs_id;
  1420. int overlay_nr = 8, unittest_nr = 8;
  1421. const char *overlay_name;
  1422. /* we don't care about device state in this test */
  1423. /* apply the overlays */
  1424. for (i = 0; i < 2; i++) {
  1425. overlay_name = overlay_name_from_nr(overlay_nr + i);
  1426. if (!overlay_data_apply(overlay_name, &ovcs_id)) {
  1427. unittest(0, "could not apply overlay \"%s\"\n",
  1428. overlay_name);
  1429. return;
  1430. }
  1431. ov_id[i] = ovcs_id;
  1432. of_unittest_track_overlay(ov_id[i]);
  1433. }
  1434. /* now try to remove first overlay (it should fail) */
  1435. ovcs_id = ov_id[0];
  1436. if (!of_overlay_remove(&ovcs_id)) {
  1437. unittest(0, "%s was destroyed @\"%s\"\n",
  1438. overlay_name_from_nr(overlay_nr + 0),
  1439. unittest_path(unittest_nr,
  1440. PDEV_OVERLAY));
  1441. return;
  1442. }
  1443. /* removing them in order should work */
  1444. for (i = 1; i >= 0; i--) {
  1445. ovcs_id = ov_id[i];
  1446. if (of_overlay_remove(&ovcs_id)) {
  1447. unittest(0, "%s not destroyed @\"%s\"\n",
  1448. overlay_name_from_nr(overlay_nr + i),
  1449. unittest_path(unittest_nr,
  1450. PDEV_OVERLAY));
  1451. return;
  1452. }
  1453. of_unittest_untrack_overlay(ov_id[i]);
  1454. }
  1455. unittest(1, "overlay test %d passed\n", 8);
  1456. }
  1457. /* test insertion of a bus with parent devices */
  1458. static void __init of_unittest_overlay_10(void)
  1459. {
  1460. int ret;
  1461. char *child_path;
  1462. /* device should disable */
  1463. ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
  1464. if (unittest(ret == 0,
  1465. "overlay test %d failed; overlay application\n", 10))
  1466. return;
  1467. child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
  1468. unittest_path(10, PDEV_OVERLAY));
  1469. if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
  1470. return;
  1471. ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
  1472. kfree(child_path);
  1473. unittest(ret, "overlay test %d failed; no child device\n", 10);
  1474. }
  1475. /* test insertion of a bus with parent devices (and revert) */
  1476. static void __init of_unittest_overlay_11(void)
  1477. {
  1478. int ret;
  1479. /* device should disable */
  1480. ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
  1481. PDEV_OVERLAY);
  1482. unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
  1483. }
  1484. #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
  1485. struct unittest_i2c_bus_data {
  1486. struct platform_device *pdev;
  1487. struct i2c_adapter adap;
  1488. };
  1489. static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
  1490. struct i2c_msg *msgs, int num)
  1491. {
  1492. struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
  1493. (void)std;
  1494. return num;
  1495. }
  1496. static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
  1497. {
  1498. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  1499. }
  1500. static const struct i2c_algorithm unittest_i2c_algo = {
  1501. .master_xfer = unittest_i2c_master_xfer,
  1502. .functionality = unittest_i2c_functionality,
  1503. };
  1504. static int unittest_i2c_bus_probe(struct platform_device *pdev)
  1505. {
  1506. struct device *dev = &pdev->dev;
  1507. struct device_node *np = dev->of_node;
  1508. struct unittest_i2c_bus_data *std;
  1509. struct i2c_adapter *adap;
  1510. int ret;
  1511. if (np == NULL) {
  1512. dev_err(dev, "No OF data for device\n");
  1513. return -EINVAL;
  1514. }
  1515. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1516. std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
  1517. if (!std) {
  1518. dev_err(dev, "Failed to allocate unittest i2c data\n");
  1519. return -ENOMEM;
  1520. }
  1521. /* link them together */
  1522. std->pdev = pdev;
  1523. platform_set_drvdata(pdev, std);
  1524. adap = &std->adap;
  1525. i2c_set_adapdata(adap, std);
  1526. adap->nr = -1;
  1527. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  1528. adap->class = I2C_CLASS_DEPRECATED;
  1529. adap->algo = &unittest_i2c_algo;
  1530. adap->dev.parent = dev;
  1531. adap->dev.of_node = dev->of_node;
  1532. adap->timeout = 5 * HZ;
  1533. adap->retries = 3;
  1534. ret = i2c_add_numbered_adapter(adap);
  1535. if (ret != 0) {
  1536. dev_err(dev, "Failed to add I2C adapter\n");
  1537. return ret;
  1538. }
  1539. return 0;
  1540. }
  1541. static int unittest_i2c_bus_remove(struct platform_device *pdev)
  1542. {
  1543. struct device *dev = &pdev->dev;
  1544. struct device_node *np = dev->of_node;
  1545. struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
  1546. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1547. i2c_del_adapter(&std->adap);
  1548. return 0;
  1549. }
  1550. static const struct of_device_id unittest_i2c_bus_match[] = {
  1551. { .compatible = "unittest-i2c-bus", },
  1552. {},
  1553. };
  1554. static struct platform_driver unittest_i2c_bus_driver = {
  1555. .probe = unittest_i2c_bus_probe,
  1556. .remove = unittest_i2c_bus_remove,
  1557. .driver = {
  1558. .name = "unittest-i2c-bus",
  1559. .of_match_table = of_match_ptr(unittest_i2c_bus_match),
  1560. },
  1561. };
  1562. static int unittest_i2c_dev_probe(struct i2c_client *client,
  1563. const struct i2c_device_id *id)
  1564. {
  1565. struct device *dev = &client->dev;
  1566. struct device_node *np = client->dev.of_node;
  1567. if (!np) {
  1568. dev_err(dev, "No OF node\n");
  1569. return -EINVAL;
  1570. }
  1571. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1572. return 0;
  1573. };
  1574. static int unittest_i2c_dev_remove(struct i2c_client *client)
  1575. {
  1576. struct device *dev = &client->dev;
  1577. struct device_node *np = client->dev.of_node;
  1578. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1579. return 0;
  1580. }
  1581. static const struct i2c_device_id unittest_i2c_dev_id[] = {
  1582. { .name = "unittest-i2c-dev" },
  1583. { }
  1584. };
  1585. static struct i2c_driver unittest_i2c_dev_driver = {
  1586. .driver = {
  1587. .name = "unittest-i2c-dev",
  1588. },
  1589. .probe = unittest_i2c_dev_probe,
  1590. .remove = unittest_i2c_dev_remove,
  1591. .id_table = unittest_i2c_dev_id,
  1592. };
  1593. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1594. static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
  1595. {
  1596. return 0;
  1597. }
  1598. static int unittest_i2c_mux_probe(struct i2c_client *client,
  1599. const struct i2c_device_id *id)
  1600. {
  1601. int i, nchans;
  1602. struct device *dev = &client->dev;
  1603. struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
  1604. struct device_node *np = client->dev.of_node, *child;
  1605. struct i2c_mux_core *muxc;
  1606. u32 reg, max_reg;
  1607. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1608. if (!np) {
  1609. dev_err(dev, "No OF node\n");
  1610. return -EINVAL;
  1611. }
  1612. max_reg = (u32)-1;
  1613. for_each_child_of_node(np, child) {
  1614. if (of_property_read_u32(child, "reg", &reg))
  1615. continue;
  1616. if (max_reg == (u32)-1 || reg > max_reg)
  1617. max_reg = reg;
  1618. }
  1619. nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
  1620. if (nchans == 0) {
  1621. dev_err(dev, "No channels\n");
  1622. return -EINVAL;
  1623. }
  1624. muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
  1625. unittest_i2c_mux_select_chan, NULL);
  1626. if (!muxc)
  1627. return -ENOMEM;
  1628. for (i = 0; i < nchans; i++) {
  1629. if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
  1630. dev_err(dev, "Failed to register mux #%d\n", i);
  1631. i2c_mux_del_adapters(muxc);
  1632. return -ENODEV;
  1633. }
  1634. }
  1635. i2c_set_clientdata(client, muxc);
  1636. return 0;
  1637. };
  1638. static int unittest_i2c_mux_remove(struct i2c_client *client)
  1639. {
  1640. struct device *dev = &client->dev;
  1641. struct device_node *np = client->dev.of_node;
  1642. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  1643. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1644. i2c_mux_del_adapters(muxc);
  1645. return 0;
  1646. }
  1647. static const struct i2c_device_id unittest_i2c_mux_id[] = {
  1648. { .name = "unittest-i2c-mux" },
  1649. { }
  1650. };
  1651. static struct i2c_driver unittest_i2c_mux_driver = {
  1652. .driver = {
  1653. .name = "unittest-i2c-mux",
  1654. },
  1655. .probe = unittest_i2c_mux_probe,
  1656. .remove = unittest_i2c_mux_remove,
  1657. .id_table = unittest_i2c_mux_id,
  1658. };
  1659. #endif
  1660. static int of_unittest_overlay_i2c_init(void)
  1661. {
  1662. int ret;
  1663. ret = i2c_add_driver(&unittest_i2c_dev_driver);
  1664. if (unittest(ret == 0,
  1665. "could not register unittest i2c device driver\n"))
  1666. return ret;
  1667. ret = platform_driver_register(&unittest_i2c_bus_driver);
  1668. if (unittest(ret == 0,
  1669. "could not register unittest i2c bus driver\n"))
  1670. return ret;
  1671. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1672. ret = i2c_add_driver(&unittest_i2c_mux_driver);
  1673. if (unittest(ret == 0,
  1674. "could not register unittest i2c mux driver\n"))
  1675. return ret;
  1676. #endif
  1677. return 0;
  1678. }
  1679. static void of_unittest_overlay_i2c_cleanup(void)
  1680. {
  1681. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1682. i2c_del_driver(&unittest_i2c_mux_driver);
  1683. #endif
  1684. platform_driver_unregister(&unittest_i2c_bus_driver);
  1685. i2c_del_driver(&unittest_i2c_dev_driver);
  1686. }
  1687. static void __init of_unittest_overlay_i2c_12(void)
  1688. {
  1689. /* device should enable */
  1690. if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY))
  1691. return;
  1692. unittest(1, "overlay test %d passed\n", 12);
  1693. }
  1694. /* test deactivation of device */
  1695. static void __init of_unittest_overlay_i2c_13(void)
  1696. {
  1697. /* device should disable */
  1698. if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY))
  1699. return;
  1700. unittest(1, "overlay test %d passed\n", 13);
  1701. }
  1702. /* just check for i2c mux existence */
  1703. static void of_unittest_overlay_i2c_14(void)
  1704. {
  1705. }
  1706. static void __init of_unittest_overlay_i2c_15(void)
  1707. {
  1708. /* device should enable */
  1709. if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY))
  1710. return;
  1711. unittest(1, "overlay test %d passed\n", 15);
  1712. }
  1713. #else
  1714. static inline void of_unittest_overlay_i2c_14(void) { }
  1715. static inline void of_unittest_overlay_i2c_15(void) { }
  1716. #endif
  1717. static void __init of_unittest_overlay(void)
  1718. {
  1719. struct device_node *bus_np = NULL;
  1720. if (platform_driver_register(&unittest_driver)) {
  1721. unittest(0, "could not register unittest driver\n");
  1722. goto out;
  1723. }
  1724. bus_np = of_find_node_by_path(bus_path);
  1725. if (bus_np == NULL) {
  1726. unittest(0, "could not find bus_path \"%s\"\n", bus_path);
  1727. goto out;
  1728. }
  1729. if (of_platform_default_populate(bus_np, NULL, NULL)) {
  1730. unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
  1731. goto out;
  1732. }
  1733. if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
  1734. unittest(0, "could not find unittest0 @ \"%s\"\n",
  1735. unittest_path(100, PDEV_OVERLAY));
  1736. goto out;
  1737. }
  1738. if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
  1739. unittest(0, "unittest1 @ \"%s\" should not exist\n",
  1740. unittest_path(101, PDEV_OVERLAY));
  1741. goto out;
  1742. }
  1743. unittest(1, "basic infrastructure of overlays passed");
  1744. /* tests in sequence */
  1745. of_unittest_overlay_0();
  1746. of_unittest_overlay_1();
  1747. of_unittest_overlay_2();
  1748. of_unittest_overlay_3();
  1749. of_unittest_overlay_4();
  1750. of_unittest_overlay_5();
  1751. of_unittest_overlay_6();
  1752. of_unittest_overlay_8();
  1753. of_unittest_overlay_10();
  1754. of_unittest_overlay_11();
  1755. #if IS_BUILTIN(CONFIG_I2C)
  1756. if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
  1757. goto out;
  1758. of_unittest_overlay_i2c_12();
  1759. of_unittest_overlay_i2c_13();
  1760. of_unittest_overlay_i2c_14();
  1761. of_unittest_overlay_i2c_15();
  1762. of_unittest_overlay_i2c_cleanup();
  1763. #endif
  1764. of_unittest_destroy_tracked_overlays();
  1765. out:
  1766. of_node_put(bus_np);
  1767. }
  1768. #else
  1769. static inline void __init of_unittest_overlay(void) { }
  1770. #endif
  1771. #ifdef CONFIG_OF_OVERLAY
  1772. /*
  1773. * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
  1774. * in scripts/Makefile.lib
  1775. */
  1776. #define OVERLAY_INFO_EXTERN(name) \
  1777. extern uint8_t __dtb_##name##_begin[]; \
  1778. extern uint8_t __dtb_##name##_end[]
  1779. #define OVERLAY_INFO(overlay_name, expected) \
  1780. { .dtb_begin = __dtb_##overlay_name##_begin, \
  1781. .dtb_end = __dtb_##overlay_name##_end, \
  1782. .expected_result = expected, \
  1783. .name = #overlay_name, \
  1784. }
  1785. struct overlay_info {
  1786. uint8_t *dtb_begin;
  1787. uint8_t *dtb_end;
  1788. int expected_result;
  1789. int overlay_id;
  1790. char *name;
  1791. };
  1792. OVERLAY_INFO_EXTERN(overlay_base);
  1793. OVERLAY_INFO_EXTERN(overlay);
  1794. OVERLAY_INFO_EXTERN(overlay_0);
  1795. OVERLAY_INFO_EXTERN(overlay_1);
  1796. OVERLAY_INFO_EXTERN(overlay_2);
  1797. OVERLAY_INFO_EXTERN(overlay_3);
  1798. OVERLAY_INFO_EXTERN(overlay_4);
  1799. OVERLAY_INFO_EXTERN(overlay_5);
  1800. OVERLAY_INFO_EXTERN(overlay_6);
  1801. OVERLAY_INFO_EXTERN(overlay_7);
  1802. OVERLAY_INFO_EXTERN(overlay_8);
  1803. OVERLAY_INFO_EXTERN(overlay_9);
  1804. OVERLAY_INFO_EXTERN(overlay_10);
  1805. OVERLAY_INFO_EXTERN(overlay_11);
  1806. OVERLAY_INFO_EXTERN(overlay_12);
  1807. OVERLAY_INFO_EXTERN(overlay_13);
  1808. OVERLAY_INFO_EXTERN(overlay_15);
  1809. OVERLAY_INFO_EXTERN(overlay_bad_phandle);
  1810. OVERLAY_INFO_EXTERN(overlay_bad_symbol);
  1811. /* order of entries is hard-coded into users of overlays[] */
  1812. static struct overlay_info overlays[] = {
  1813. OVERLAY_INFO(overlay_base, -9999),
  1814. OVERLAY_INFO(overlay, 0),
  1815. OVERLAY_INFO(overlay_0, 0),
  1816. OVERLAY_INFO(overlay_1, 0),
  1817. OVERLAY_INFO(overlay_2, 0),
  1818. OVERLAY_INFO(overlay_3, 0),
  1819. OVERLAY_INFO(overlay_4, 0),
  1820. OVERLAY_INFO(overlay_5, 0),
  1821. OVERLAY_INFO(overlay_6, 0),
  1822. OVERLAY_INFO(overlay_7, 0),
  1823. OVERLAY_INFO(overlay_8, 0),
  1824. OVERLAY_INFO(overlay_9, 0),
  1825. OVERLAY_INFO(overlay_10, 0),
  1826. OVERLAY_INFO(overlay_11, 0),
  1827. OVERLAY_INFO(overlay_12, 0),
  1828. OVERLAY_INFO(overlay_13, 0),
  1829. OVERLAY_INFO(overlay_15, 0),
  1830. OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
  1831. OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
  1832. {}
  1833. };
  1834. static struct device_node *overlay_base_root;
  1835. static void * __init dt_alloc_memory(u64 size, u64 align)
  1836. {
  1837. return memblock_virt_alloc(size, align);
  1838. }
  1839. /*
  1840. * Create base device tree for the overlay unittest.
  1841. *
  1842. * This is called from very early boot code.
  1843. *
  1844. * Do as much as possible the same way as done in __unflatten_device_tree
  1845. * and other early boot steps for the normal FDT so that the overlay base
  1846. * unflattened tree will have the same characteristics as the real tree
  1847. * (such as having memory allocated by the early allocator). The goal
  1848. * is to test "the real thing" as much as possible, and test "test setup
  1849. * code" as little as possible.
  1850. *
  1851. * Have to stop before resolving phandles, because that uses kmalloc.
  1852. */
  1853. void __init unittest_unflatten_overlay_base(void)
  1854. {
  1855. struct overlay_info *info;
  1856. u32 data_size;
  1857. void *new_fdt;
  1858. u32 size;
  1859. info = &overlays[0];
  1860. if (info->expected_result != -9999) {
  1861. pr_err("No dtb 'overlay_base' to attach\n");
  1862. return;
  1863. }
  1864. data_size = info->dtb_end - info->dtb_begin;
  1865. if (!data_size) {
  1866. pr_err("No dtb 'overlay_base' to attach\n");
  1867. return;
  1868. }
  1869. size = fdt_totalsize(info->dtb_begin);
  1870. if (size != data_size) {
  1871. pr_err("dtb 'overlay_base' header totalsize != actual size");
  1872. return;
  1873. }
  1874. new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
  1875. if (!new_fdt) {
  1876. pr_err("alloc for dtb 'overlay_base' failed");
  1877. return;
  1878. }
  1879. memcpy(new_fdt, info->dtb_begin, size);
  1880. __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
  1881. dt_alloc_memory, true);
  1882. }
  1883. /*
  1884. * The purpose of of_unittest_overlay_data_add is to add an
  1885. * overlay in the normal fashion. This is a test of the whole
  1886. * picture, instead of testing individual elements.
  1887. *
  1888. * A secondary purpose is to be able to verify that the contents of
  1889. * /proc/device-tree/ contains the updated structure and values from
  1890. * the overlay. That must be verified separately in user space.
  1891. *
  1892. * Return 0 on unexpected error.
  1893. */
  1894. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
  1895. {
  1896. struct overlay_info *info;
  1897. int found = 0;
  1898. int k;
  1899. int ret;
  1900. u32 size;
  1901. for (k = 0, info = overlays; info && info->name; info++, k++) {
  1902. if (!strcmp(overlay_name, info->name)) {
  1903. found = 1;
  1904. break;
  1905. }
  1906. }
  1907. if (!found) {
  1908. pr_err("no overlay data for %s\n", overlay_name);
  1909. return 0;
  1910. }
  1911. size = info->dtb_end - info->dtb_begin;
  1912. if (!size)
  1913. pr_err("no overlay data for %s\n", overlay_name);
  1914. ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
  1915. if (overlay_id)
  1916. *overlay_id = info->overlay_id;
  1917. if (ret < 0)
  1918. goto out;
  1919. pr_debug("%s applied\n", overlay_name);
  1920. out:
  1921. if (ret != info->expected_result)
  1922. pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
  1923. info->expected_result, ret, overlay_name);
  1924. return (ret == info->expected_result);
  1925. }
  1926. /*
  1927. * The purpose of of_unittest_overlay_high_level is to add an overlay
  1928. * in the normal fashion. This is a test of the whole picture,
  1929. * instead of individual elements.
  1930. *
  1931. * The first part of the function is _not_ normal overlay usage; it is
  1932. * finishing splicing the base overlay device tree into the live tree.
  1933. */
  1934. static __init void of_unittest_overlay_high_level(void)
  1935. {
  1936. struct device_node *last_sibling;
  1937. struct device_node *np;
  1938. struct device_node *of_symbols;
  1939. struct device_node *overlay_base_symbols;
  1940. struct device_node **pprev;
  1941. struct property *prop;
  1942. if (!overlay_base_root) {
  1943. unittest(0, "overlay_base_root not initialized\n");
  1944. return;
  1945. }
  1946. /*
  1947. * Could not fixup phandles in unittest_unflatten_overlay_base()
  1948. * because kmalloc() was not yet available.
  1949. */
  1950. of_overlay_mutex_lock();
  1951. of_resolve_phandles(overlay_base_root);
  1952. of_overlay_mutex_unlock();
  1953. /*
  1954. * do not allow overlay_base to duplicate any node already in
  1955. * tree, this greatly simplifies the code
  1956. */
  1957. /*
  1958. * remove overlay_base_root node "__local_fixups", after
  1959. * being used by of_resolve_phandles()
  1960. */
  1961. pprev = &overlay_base_root->child;
  1962. for (np = overlay_base_root->child; np; np = np->sibling) {
  1963. if (!of_node_cmp(np->name, "__local_fixups__")) {
  1964. *pprev = np->sibling;
  1965. break;
  1966. }
  1967. pprev = &np->sibling;
  1968. }
  1969. /* remove overlay_base_root node "__symbols__" if in live tree */
  1970. of_symbols = of_get_child_by_name(of_root, "__symbols__");
  1971. if (of_symbols) {
  1972. /* will have to graft properties from node into live tree */
  1973. pprev = &overlay_base_root->child;
  1974. for (np = overlay_base_root->child; np; np = np->sibling) {
  1975. if (!of_node_cmp(np->name, "__symbols__")) {
  1976. overlay_base_symbols = np;
  1977. *pprev = np->sibling;
  1978. break;
  1979. }
  1980. pprev = &np->sibling;
  1981. }
  1982. }
  1983. for (np = overlay_base_root->child; np; np = np->sibling) {
  1984. if (of_get_child_by_name(of_root, np->name)) {
  1985. unittest(0, "illegal node name in overlay_base %s",
  1986. np->name);
  1987. return;
  1988. }
  1989. }
  1990. /*
  1991. * overlay 'overlay_base' is not allowed to have root
  1992. * properties, so only need to splice nodes into main device tree.
  1993. *
  1994. * root node of *overlay_base_root will not be freed, it is lost
  1995. * memory.
  1996. */
  1997. for (np = overlay_base_root->child; np; np = np->sibling)
  1998. np->parent = of_root;
  1999. mutex_lock(&of_mutex);
  2000. for (last_sibling = np = of_root->child; np; np = np->sibling)
  2001. last_sibling = np;
  2002. if (last_sibling)
  2003. last_sibling->sibling = overlay_base_root->child;
  2004. else
  2005. of_root->child = overlay_base_root->child;
  2006. for_each_of_allnodes_from(overlay_base_root, np)
  2007. __of_attach_node_sysfs(np);
  2008. if (of_symbols) {
  2009. struct property *new_prop;
  2010. for_each_property_of_node(overlay_base_symbols, prop) {
  2011. new_prop = __of_prop_dup(prop, GFP_KERNEL);
  2012. if (!new_prop) {
  2013. unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
  2014. prop->name);
  2015. goto err_unlock;
  2016. }
  2017. if (__of_add_property(of_symbols, new_prop)) {
  2018. /* "name" auto-generated by unflatten */
  2019. if (!strcmp(new_prop->name, "name"))
  2020. continue;
  2021. unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
  2022. prop->name);
  2023. goto err_unlock;
  2024. }
  2025. if (__of_add_property_sysfs(of_symbols, new_prop)) {
  2026. unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
  2027. prop->name);
  2028. goto err_unlock;
  2029. }
  2030. }
  2031. }
  2032. mutex_unlock(&of_mutex);
  2033. /* now do the normal overlay usage test */
  2034. unittest(overlay_data_apply("overlay", NULL),
  2035. "Adding overlay 'overlay' failed\n");
  2036. unittest(overlay_data_apply("overlay_bad_phandle", NULL),
  2037. "Adding overlay 'overlay_bad_phandle' failed\n");
  2038. unittest(overlay_data_apply("overlay_bad_symbol", NULL),
  2039. "Adding overlay 'overlay_bad_symbol' failed\n");
  2040. return;
  2041. err_unlock:
  2042. mutex_unlock(&of_mutex);
  2043. }
  2044. #else
  2045. static inline __init void of_unittest_overlay_high_level(void) {}
  2046. #endif
  2047. static int __init of_unittest(void)
  2048. {
  2049. struct device_node *np;
  2050. int res;
  2051. /* adding data for unittest */
  2052. res = unittest_data_add();
  2053. if (res)
  2054. return res;
  2055. if (!of_aliases)
  2056. of_aliases = of_find_node_by_path("/aliases");
  2057. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  2058. if (!np) {
  2059. pr_info("No testcase data in device tree; not running tests\n");
  2060. return 0;
  2061. }
  2062. of_node_put(np);
  2063. pr_info("start of unittest - you will see error messages\n");
  2064. of_unittest_check_tree_linkage();
  2065. of_unittest_check_phandles();
  2066. of_unittest_find_node_by_name();
  2067. of_unittest_dynamic();
  2068. of_unittest_parse_phandle_with_args();
  2069. of_unittest_parse_phandle_with_args_map();
  2070. of_unittest_printf();
  2071. of_unittest_property_string();
  2072. of_unittest_property_copy();
  2073. of_unittest_changeset();
  2074. of_unittest_parse_interrupts();
  2075. of_unittest_parse_interrupts_extended();
  2076. of_unittest_match_node();
  2077. of_unittest_platform_populate();
  2078. of_unittest_overlay();
  2079. /* Double check linkage after removing testcase data */
  2080. of_unittest_check_tree_linkage();
  2081. of_unittest_overlay_high_level();
  2082. pr_info("end of unittest - %i passed, %i failed\n",
  2083. unittest_results.passed, unittest_results.failed);
  2084. return 0;
  2085. }
  2086. late_initcall(of_unittest);