unittest.c 52 KB

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