unittest.c 52 KB

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