unittest.c 52 KB

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