unittest.c 62 KB

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