base.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
  11. *
  12. * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
  13. * Grant Likely.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/ctype.h>
  21. #include <linux/cpu.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_graph.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/slab.h>
  27. #include <linux/string.h>
  28. #include <linux/proc_fs.h>
  29. #include "of_private.h"
  30. LIST_HEAD(aliases_lookup);
  31. struct device_node *of_allnodes;
  32. EXPORT_SYMBOL(of_allnodes);
  33. struct device_node *of_chosen;
  34. struct device_node *of_aliases;
  35. static struct device_node *of_stdout;
  36. static struct kset *of_kset;
  37. /*
  38. * Used to protect the of_aliases; but also overloaded to hold off addition of
  39. * nodes to sysfs
  40. */
  41. DEFINE_MUTEX(of_aliases_mutex);
  42. /* use when traversing tree through the allnext, child, sibling,
  43. * or parent members of struct device_node.
  44. */
  45. DEFINE_RAW_SPINLOCK(devtree_lock);
  46. int of_n_addr_cells(struct device_node *np)
  47. {
  48. const __be32 *ip;
  49. do {
  50. if (np->parent)
  51. np = np->parent;
  52. ip = of_get_property(np, "#address-cells", NULL);
  53. if (ip)
  54. return be32_to_cpup(ip);
  55. } while (np->parent);
  56. /* No #address-cells property for the root node */
  57. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  58. }
  59. EXPORT_SYMBOL(of_n_addr_cells);
  60. int of_n_size_cells(struct device_node *np)
  61. {
  62. const __be32 *ip;
  63. do {
  64. if (np->parent)
  65. np = np->parent;
  66. ip = of_get_property(np, "#size-cells", NULL);
  67. if (ip)
  68. return be32_to_cpup(ip);
  69. } while (np->parent);
  70. /* No #size-cells property for the root node */
  71. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  72. }
  73. EXPORT_SYMBOL(of_n_size_cells);
  74. #ifdef CONFIG_NUMA
  75. int __weak of_node_to_nid(struct device_node *np)
  76. {
  77. return numa_node_id();
  78. }
  79. #endif
  80. #if defined(CONFIG_OF_DYNAMIC)
  81. /**
  82. * of_node_get - Increment refcount of a node
  83. * @node: Node to inc refcount, NULL is supported to
  84. * simplify writing of callers
  85. *
  86. * Returns node.
  87. */
  88. struct device_node *of_node_get(struct device_node *node)
  89. {
  90. if (node)
  91. kobject_get(&node->kobj);
  92. return node;
  93. }
  94. EXPORT_SYMBOL(of_node_get);
  95. static inline struct device_node *kobj_to_device_node(struct kobject *kobj)
  96. {
  97. return container_of(kobj, struct device_node, kobj);
  98. }
  99. /**
  100. * of_node_release - release a dynamically allocated node
  101. * @kref: kref element of the node to be released
  102. *
  103. * In of_node_put() this function is passed to kref_put()
  104. * as the destructor.
  105. */
  106. static void of_node_release(struct kobject *kobj)
  107. {
  108. struct device_node *node = kobj_to_device_node(kobj);
  109. struct property *prop = node->properties;
  110. /* We should never be releasing nodes that haven't been detached. */
  111. if (!of_node_check_flag(node, OF_DETACHED)) {
  112. pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
  113. dump_stack();
  114. return;
  115. }
  116. if (!of_node_check_flag(node, OF_DYNAMIC))
  117. return;
  118. while (prop) {
  119. struct property *next = prop->next;
  120. kfree(prop->name);
  121. kfree(prop->value);
  122. kfree(prop);
  123. prop = next;
  124. if (!prop) {
  125. prop = node->deadprops;
  126. node->deadprops = NULL;
  127. }
  128. }
  129. kfree(node->full_name);
  130. kfree(node->data);
  131. kfree(node);
  132. }
  133. /**
  134. * of_node_put - Decrement refcount of a node
  135. * @node: Node to dec refcount, NULL is supported to
  136. * simplify writing of callers
  137. *
  138. */
  139. void of_node_put(struct device_node *node)
  140. {
  141. if (node)
  142. kobject_put(&node->kobj);
  143. }
  144. EXPORT_SYMBOL(of_node_put);
  145. #else
  146. static void of_node_release(struct kobject *kobj)
  147. {
  148. /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
  149. }
  150. #endif /* CONFIG_OF_DYNAMIC */
  151. struct kobj_type of_node_ktype = {
  152. .release = of_node_release,
  153. };
  154. static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
  155. struct bin_attribute *bin_attr, char *buf,
  156. loff_t offset, size_t count)
  157. {
  158. struct property *pp = container_of(bin_attr, struct property, attr);
  159. return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
  160. }
  161. static const char *safe_name(struct kobject *kobj, const char *orig_name)
  162. {
  163. const char *name = orig_name;
  164. struct kernfs_node *kn;
  165. int i = 0;
  166. /* don't be a hero. After 16 tries give up */
  167. while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
  168. sysfs_put(kn);
  169. if (name != orig_name)
  170. kfree(name);
  171. name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
  172. }
  173. if (name != orig_name)
  174. pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
  175. kobject_name(kobj), name);
  176. return name;
  177. }
  178. static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
  179. {
  180. int rc;
  181. /* Important: Don't leak passwords */
  182. bool secure = strncmp(pp->name, "security-", 9) == 0;
  183. sysfs_bin_attr_init(&pp->attr);
  184. pp->attr.attr.name = safe_name(&np->kobj, pp->name);
  185. pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
  186. pp->attr.size = secure ? 0 : pp->length;
  187. pp->attr.read = of_node_property_read;
  188. rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
  189. WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
  190. return rc;
  191. }
  192. static int __of_node_add(struct device_node *np)
  193. {
  194. const char *name;
  195. struct property *pp;
  196. int rc;
  197. np->kobj.kset = of_kset;
  198. if (!np->parent) {
  199. /* Nodes without parents are new top level trees */
  200. rc = kobject_add(&np->kobj, NULL, "%s",
  201. safe_name(&of_kset->kobj, "base"));
  202. } else {
  203. name = safe_name(&np->parent->kobj, kbasename(np->full_name));
  204. if (!name || !name[0])
  205. return -EINVAL;
  206. rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
  207. }
  208. if (rc)
  209. return rc;
  210. for_each_property_of_node(np, pp)
  211. __of_add_property_sysfs(np, pp);
  212. return 0;
  213. }
  214. int of_node_add(struct device_node *np)
  215. {
  216. int rc = 0;
  217. BUG_ON(!of_node_is_initialized(np));
  218. /*
  219. * Grab the mutex here so that in a race condition between of_init() and
  220. * of_node_add(), node addition will still be consistent.
  221. */
  222. mutex_lock(&of_aliases_mutex);
  223. if (of_kset)
  224. rc = __of_node_add(np);
  225. else
  226. /* This scenario may be perfectly valid, but report it anyway */
  227. pr_info("of_node_add(%s) before of_init()\n", np->full_name);
  228. mutex_unlock(&of_aliases_mutex);
  229. return rc;
  230. }
  231. #if defined(CONFIG_OF_DYNAMIC)
  232. static void of_node_remove(struct device_node *np)
  233. {
  234. struct property *pp;
  235. BUG_ON(!of_node_is_initialized(np));
  236. /* only remove properties if on sysfs */
  237. if (of_node_is_attached(np)) {
  238. for_each_property_of_node(np, pp)
  239. sysfs_remove_bin_file(&np->kobj, &pp->attr);
  240. kobject_del(&np->kobj);
  241. }
  242. /* finally remove the kobj_init ref */
  243. of_node_put(np);
  244. }
  245. #endif
  246. static int __init of_init(void)
  247. {
  248. struct device_node *np;
  249. /* Create the kset, and register existing nodes */
  250. mutex_lock(&of_aliases_mutex);
  251. of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
  252. if (!of_kset) {
  253. mutex_unlock(&of_aliases_mutex);
  254. return -ENOMEM;
  255. }
  256. for_each_of_allnodes(np)
  257. __of_node_add(np);
  258. mutex_unlock(&of_aliases_mutex);
  259. /* Symlink in /proc as required by userspace ABI */
  260. if (of_allnodes)
  261. proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
  262. return 0;
  263. }
  264. core_initcall(of_init);
  265. static struct property *__of_find_property(const struct device_node *np,
  266. const char *name, int *lenp)
  267. {
  268. struct property *pp;
  269. if (!np)
  270. return NULL;
  271. for (pp = np->properties; pp; pp = pp->next) {
  272. if (of_prop_cmp(pp->name, name) == 0) {
  273. if (lenp)
  274. *lenp = pp->length;
  275. break;
  276. }
  277. }
  278. return pp;
  279. }
  280. struct property *of_find_property(const struct device_node *np,
  281. const char *name,
  282. int *lenp)
  283. {
  284. struct property *pp;
  285. unsigned long flags;
  286. raw_spin_lock_irqsave(&devtree_lock, flags);
  287. pp = __of_find_property(np, name, lenp);
  288. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  289. return pp;
  290. }
  291. EXPORT_SYMBOL(of_find_property);
  292. /**
  293. * of_find_all_nodes - Get next node in global list
  294. * @prev: Previous node or NULL to start iteration
  295. * of_node_put() will be called on it
  296. *
  297. * Returns a node pointer with refcount incremented, use
  298. * of_node_put() on it when done.
  299. */
  300. struct device_node *of_find_all_nodes(struct device_node *prev)
  301. {
  302. struct device_node *np;
  303. unsigned long flags;
  304. raw_spin_lock_irqsave(&devtree_lock, flags);
  305. np = prev ? prev->allnext : of_allnodes;
  306. for (; np != NULL; np = np->allnext)
  307. if (of_node_get(np))
  308. break;
  309. of_node_put(prev);
  310. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  311. return np;
  312. }
  313. EXPORT_SYMBOL(of_find_all_nodes);
  314. /*
  315. * Find a property with a given name for a given node
  316. * and return the value.
  317. */
  318. static const void *__of_get_property(const struct device_node *np,
  319. const char *name, int *lenp)
  320. {
  321. struct property *pp = __of_find_property(np, name, lenp);
  322. return pp ? pp->value : NULL;
  323. }
  324. /*
  325. * Find a property with a given name for a given node
  326. * and return the value.
  327. */
  328. const void *of_get_property(const struct device_node *np, const char *name,
  329. int *lenp)
  330. {
  331. struct property *pp = of_find_property(np, name, lenp);
  332. return pp ? pp->value : NULL;
  333. }
  334. EXPORT_SYMBOL(of_get_property);
  335. /*
  336. * arch_match_cpu_phys_id - Match the given logical CPU and physical id
  337. *
  338. * @cpu: logical cpu index of a core/thread
  339. * @phys_id: physical identifier of a core/thread
  340. *
  341. * CPU logical to physical index mapping is architecture specific.
  342. * However this __weak function provides a default match of physical
  343. * id to logical cpu index. phys_id provided here is usually values read
  344. * from the device tree which must match the hardware internal registers.
  345. *
  346. * Returns true if the physical identifier and the logical cpu index
  347. * correspond to the same core/thread, false otherwise.
  348. */
  349. bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
  350. {
  351. return (u32)phys_id == cpu;
  352. }
  353. /**
  354. * Checks if the given "prop_name" property holds the physical id of the
  355. * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
  356. * NULL, local thread number within the core is returned in it.
  357. */
  358. static bool __of_find_n_match_cpu_property(struct device_node *cpun,
  359. const char *prop_name, int cpu, unsigned int *thread)
  360. {
  361. const __be32 *cell;
  362. int ac, prop_len, tid;
  363. u64 hwid;
  364. ac = of_n_addr_cells(cpun);
  365. cell = of_get_property(cpun, prop_name, &prop_len);
  366. if (!cell || !ac)
  367. return false;
  368. prop_len /= sizeof(*cell) * ac;
  369. for (tid = 0; tid < prop_len; tid++) {
  370. hwid = of_read_number(cell, ac);
  371. if (arch_match_cpu_phys_id(cpu, hwid)) {
  372. if (thread)
  373. *thread = tid;
  374. return true;
  375. }
  376. cell += ac;
  377. }
  378. return false;
  379. }
  380. /*
  381. * arch_find_n_match_cpu_physical_id - See if the given device node is
  382. * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
  383. * else false. If 'thread' is non-NULL, the local thread number within the
  384. * core is returned in it.
  385. */
  386. bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  387. int cpu, unsigned int *thread)
  388. {
  389. /* Check for non-standard "ibm,ppc-interrupt-server#s" property
  390. * for thread ids on PowerPC. If it doesn't exist fallback to
  391. * standard "reg" property.
  392. */
  393. if (IS_ENABLED(CONFIG_PPC) &&
  394. __of_find_n_match_cpu_property(cpun,
  395. "ibm,ppc-interrupt-server#s",
  396. cpu, thread))
  397. return true;
  398. if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
  399. return true;
  400. return false;
  401. }
  402. /**
  403. * of_get_cpu_node - Get device node associated with the given logical CPU
  404. *
  405. * @cpu: CPU number(logical index) for which device node is required
  406. * @thread: if not NULL, local thread number within the physical core is
  407. * returned
  408. *
  409. * The main purpose of this function is to retrieve the device node for the
  410. * given logical CPU index. It should be used to initialize the of_node in
  411. * cpu device. Once of_node in cpu device is populated, all the further
  412. * references can use that instead.
  413. *
  414. * CPU logical to physical index mapping is architecture specific and is built
  415. * before booting secondary cores. This function uses arch_match_cpu_phys_id
  416. * which can be overridden by architecture specific implementation.
  417. *
  418. * Returns a node pointer for the logical cpu if found, else NULL.
  419. */
  420. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  421. {
  422. struct device_node *cpun;
  423. for_each_node_by_type(cpun, "cpu") {
  424. if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
  425. return cpun;
  426. }
  427. return NULL;
  428. }
  429. EXPORT_SYMBOL(of_get_cpu_node);
  430. /**
  431. * __of_device_is_compatible() - Check if the node matches given constraints
  432. * @device: pointer to node
  433. * @compat: required compatible string, NULL or "" for any match
  434. * @type: required device_type value, NULL or "" for any match
  435. * @name: required node name, NULL or "" for any match
  436. *
  437. * Checks if the given @compat, @type and @name strings match the
  438. * properties of the given @device. A constraints can be skipped by
  439. * passing NULL or an empty string as the constraint.
  440. *
  441. * Returns 0 for no match, and a positive integer on match. The return
  442. * value is a relative score with larger values indicating better
  443. * matches. The score is weighted for the most specific compatible value
  444. * to get the highest score. Matching type is next, followed by matching
  445. * name. Practically speaking, this results in the following priority
  446. * order for matches:
  447. *
  448. * 1. specific compatible && type && name
  449. * 2. specific compatible && type
  450. * 3. specific compatible && name
  451. * 4. specific compatible
  452. * 5. general compatible && type && name
  453. * 6. general compatible && type
  454. * 7. general compatible && name
  455. * 8. general compatible
  456. * 9. type && name
  457. * 10. type
  458. * 11. name
  459. */
  460. static int __of_device_is_compatible(const struct device_node *device,
  461. const char *compat, const char *type, const char *name)
  462. {
  463. struct property *prop;
  464. const char *cp;
  465. int index = 0, score = 0;
  466. /* Compatible match has highest priority */
  467. if (compat && compat[0]) {
  468. prop = __of_find_property(device, "compatible", NULL);
  469. for (cp = of_prop_next_string(prop, NULL); cp;
  470. cp = of_prop_next_string(prop, cp), index++) {
  471. if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
  472. score = INT_MAX/2 - (index << 2);
  473. break;
  474. }
  475. }
  476. if (!score)
  477. return 0;
  478. }
  479. /* Matching type is better than matching name */
  480. if (type && type[0]) {
  481. if (!device->type || of_node_cmp(type, device->type))
  482. return 0;
  483. score += 2;
  484. }
  485. /* Matching name is a bit better than not */
  486. if (name && name[0]) {
  487. if (!device->name || of_node_cmp(name, device->name))
  488. return 0;
  489. score++;
  490. }
  491. return score;
  492. }
  493. /** Checks if the given "compat" string matches one of the strings in
  494. * the device's "compatible" property
  495. */
  496. int of_device_is_compatible(const struct device_node *device,
  497. const char *compat)
  498. {
  499. unsigned long flags;
  500. int res;
  501. raw_spin_lock_irqsave(&devtree_lock, flags);
  502. res = __of_device_is_compatible(device, compat, NULL, NULL);
  503. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  504. return res;
  505. }
  506. EXPORT_SYMBOL(of_device_is_compatible);
  507. /**
  508. * of_machine_is_compatible - Test root of device tree for a given compatible value
  509. * @compat: compatible string to look for in root node's compatible property.
  510. *
  511. * Returns true if the root node has the given value in its
  512. * compatible property.
  513. */
  514. int of_machine_is_compatible(const char *compat)
  515. {
  516. struct device_node *root;
  517. int rc = 0;
  518. root = of_find_node_by_path("/");
  519. if (root) {
  520. rc = of_device_is_compatible(root, compat);
  521. of_node_put(root);
  522. }
  523. return rc;
  524. }
  525. EXPORT_SYMBOL(of_machine_is_compatible);
  526. /**
  527. * __of_device_is_available - check if a device is available for use
  528. *
  529. * @device: Node to check for availability, with locks already held
  530. *
  531. * Returns 1 if the status property is absent or set to "okay" or "ok",
  532. * 0 otherwise
  533. */
  534. static int __of_device_is_available(const struct device_node *device)
  535. {
  536. const char *status;
  537. int statlen;
  538. if (!device)
  539. return 0;
  540. status = __of_get_property(device, "status", &statlen);
  541. if (status == NULL)
  542. return 1;
  543. if (statlen > 0) {
  544. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  545. return 1;
  546. }
  547. return 0;
  548. }
  549. /**
  550. * of_device_is_available - check if a device is available for use
  551. *
  552. * @device: Node to check for availability
  553. *
  554. * Returns 1 if the status property is absent or set to "okay" or "ok",
  555. * 0 otherwise
  556. */
  557. int of_device_is_available(const struct device_node *device)
  558. {
  559. unsigned long flags;
  560. int res;
  561. raw_spin_lock_irqsave(&devtree_lock, flags);
  562. res = __of_device_is_available(device);
  563. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  564. return res;
  565. }
  566. EXPORT_SYMBOL(of_device_is_available);
  567. /**
  568. * of_get_parent - Get a node's parent if any
  569. * @node: Node to get parent
  570. *
  571. * Returns a node pointer with refcount incremented, use
  572. * of_node_put() on it when done.
  573. */
  574. struct device_node *of_get_parent(const struct device_node *node)
  575. {
  576. struct device_node *np;
  577. unsigned long flags;
  578. if (!node)
  579. return NULL;
  580. raw_spin_lock_irqsave(&devtree_lock, flags);
  581. np = of_node_get(node->parent);
  582. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  583. return np;
  584. }
  585. EXPORT_SYMBOL(of_get_parent);
  586. /**
  587. * of_get_next_parent - Iterate to a node's parent
  588. * @node: Node to get parent of
  589. *
  590. * This is like of_get_parent() except that it drops the
  591. * refcount on the passed node, making it suitable for iterating
  592. * through a node's parents.
  593. *
  594. * Returns a node pointer with refcount incremented, use
  595. * of_node_put() on it when done.
  596. */
  597. struct device_node *of_get_next_parent(struct device_node *node)
  598. {
  599. struct device_node *parent;
  600. unsigned long flags;
  601. if (!node)
  602. return NULL;
  603. raw_spin_lock_irqsave(&devtree_lock, flags);
  604. parent = of_node_get(node->parent);
  605. of_node_put(node);
  606. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  607. return parent;
  608. }
  609. EXPORT_SYMBOL(of_get_next_parent);
  610. static struct device_node *__of_get_next_child(const struct device_node *node,
  611. struct device_node *prev)
  612. {
  613. struct device_node *next;
  614. if (!node)
  615. return NULL;
  616. next = prev ? prev->sibling : node->child;
  617. for (; next; next = next->sibling)
  618. if (of_node_get(next))
  619. break;
  620. of_node_put(prev);
  621. return next;
  622. }
  623. #define __for_each_child_of_node(parent, child) \
  624. for (child = __of_get_next_child(parent, NULL); child != NULL; \
  625. child = __of_get_next_child(parent, child))
  626. /**
  627. * of_get_next_child - Iterate a node childs
  628. * @node: parent node
  629. * @prev: previous child of the parent node, or NULL to get first
  630. *
  631. * Returns a node pointer with refcount incremented, use
  632. * of_node_put() on it when done.
  633. */
  634. struct device_node *of_get_next_child(const struct device_node *node,
  635. struct device_node *prev)
  636. {
  637. struct device_node *next;
  638. unsigned long flags;
  639. raw_spin_lock_irqsave(&devtree_lock, flags);
  640. next = __of_get_next_child(node, prev);
  641. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  642. return next;
  643. }
  644. EXPORT_SYMBOL(of_get_next_child);
  645. /**
  646. * of_get_next_available_child - Find the next available child node
  647. * @node: parent node
  648. * @prev: previous child of the parent node, or NULL to get first
  649. *
  650. * This function is like of_get_next_child(), except that it
  651. * automatically skips any disabled nodes (i.e. status = "disabled").
  652. */
  653. struct device_node *of_get_next_available_child(const struct device_node *node,
  654. struct device_node *prev)
  655. {
  656. struct device_node *next;
  657. unsigned long flags;
  658. if (!node)
  659. return NULL;
  660. raw_spin_lock_irqsave(&devtree_lock, flags);
  661. next = prev ? prev->sibling : node->child;
  662. for (; next; next = next->sibling) {
  663. if (!__of_device_is_available(next))
  664. continue;
  665. if (of_node_get(next))
  666. break;
  667. }
  668. of_node_put(prev);
  669. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  670. return next;
  671. }
  672. EXPORT_SYMBOL(of_get_next_available_child);
  673. /**
  674. * of_get_child_by_name - Find the child node by name for a given parent
  675. * @node: parent node
  676. * @name: child name to look for.
  677. *
  678. * This function looks for child node for given matching name
  679. *
  680. * Returns a node pointer if found, with refcount incremented, use
  681. * of_node_put() on it when done.
  682. * Returns NULL if node is not found.
  683. */
  684. struct device_node *of_get_child_by_name(const struct device_node *node,
  685. const char *name)
  686. {
  687. struct device_node *child;
  688. for_each_child_of_node(node, child)
  689. if (child->name && (of_node_cmp(child->name, name) == 0))
  690. break;
  691. return child;
  692. }
  693. EXPORT_SYMBOL(of_get_child_by_name);
  694. static struct device_node *__of_find_node_by_path(struct device_node *parent,
  695. const char *path)
  696. {
  697. struct device_node *child;
  698. int len = strchrnul(path, '/') - path;
  699. if (!len)
  700. return NULL;
  701. __for_each_child_of_node(parent, child) {
  702. const char *name = strrchr(child->full_name, '/');
  703. if (WARN(!name, "malformed device_node %s\n", child->full_name))
  704. continue;
  705. name++;
  706. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  707. return child;
  708. }
  709. return NULL;
  710. }
  711. /**
  712. * of_find_node_by_path - Find a node matching a full OF path
  713. * @path: Either the full path to match, or if the path does not
  714. * start with '/', the name of a property of the /aliases
  715. * node (an alias). In the case of an alias, the node
  716. * matching the alias' value will be returned.
  717. *
  718. * Valid paths:
  719. * /foo/bar Full path
  720. * foo Valid alias
  721. * foo/bar Valid alias + relative path
  722. *
  723. * Returns a node pointer with refcount incremented, use
  724. * of_node_put() on it when done.
  725. */
  726. struct device_node *of_find_node_by_path(const char *path)
  727. {
  728. struct device_node *np = NULL;
  729. struct property *pp;
  730. unsigned long flags;
  731. if (strcmp(path, "/") == 0)
  732. return of_node_get(of_allnodes);
  733. /* The path could begin with an alias */
  734. if (*path != '/') {
  735. char *p = strchrnul(path, '/');
  736. int len = p - path;
  737. /* of_aliases must not be NULL */
  738. if (!of_aliases)
  739. return NULL;
  740. for_each_property_of_node(of_aliases, pp) {
  741. if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
  742. np = of_find_node_by_path(pp->value);
  743. break;
  744. }
  745. }
  746. if (!np)
  747. return NULL;
  748. path = p;
  749. }
  750. /* Step down the tree matching path components */
  751. raw_spin_lock_irqsave(&devtree_lock, flags);
  752. if (!np)
  753. np = of_node_get(of_allnodes);
  754. while (np && *path == '/') {
  755. path++; /* Increment past '/' delimiter */
  756. np = __of_find_node_by_path(np, path);
  757. path = strchrnul(path, '/');
  758. }
  759. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  760. return np;
  761. }
  762. EXPORT_SYMBOL(of_find_node_by_path);
  763. /**
  764. * of_find_node_by_name - Find a node by its "name" property
  765. * @from: The node to start searching from or NULL, the node
  766. * you pass will not be searched, only the next one
  767. * will; typically, you pass what the previous call
  768. * returned. of_node_put() will be called on it
  769. * @name: The name string to match against
  770. *
  771. * Returns a node pointer with refcount incremented, use
  772. * of_node_put() on it when done.
  773. */
  774. struct device_node *of_find_node_by_name(struct device_node *from,
  775. const char *name)
  776. {
  777. struct device_node *np;
  778. unsigned long flags;
  779. raw_spin_lock_irqsave(&devtree_lock, flags);
  780. np = from ? from->allnext : of_allnodes;
  781. for (; np; np = np->allnext)
  782. if (np->name && (of_node_cmp(np->name, name) == 0)
  783. && of_node_get(np))
  784. break;
  785. of_node_put(from);
  786. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  787. return np;
  788. }
  789. EXPORT_SYMBOL(of_find_node_by_name);
  790. /**
  791. * of_find_node_by_type - Find a node by its "device_type" property
  792. * @from: The node to start searching from, or NULL to start searching
  793. * the entire device tree. The node you pass will not be
  794. * searched, only the next one will; typically, you pass
  795. * what the previous call returned. of_node_put() will be
  796. * called on from for you.
  797. * @type: The type string to match against
  798. *
  799. * Returns a node pointer with refcount incremented, use
  800. * of_node_put() on it when done.
  801. */
  802. struct device_node *of_find_node_by_type(struct device_node *from,
  803. const char *type)
  804. {
  805. struct device_node *np;
  806. unsigned long flags;
  807. raw_spin_lock_irqsave(&devtree_lock, flags);
  808. np = from ? from->allnext : of_allnodes;
  809. for (; np; np = np->allnext)
  810. if (np->type && (of_node_cmp(np->type, type) == 0)
  811. && of_node_get(np))
  812. break;
  813. of_node_put(from);
  814. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  815. return np;
  816. }
  817. EXPORT_SYMBOL(of_find_node_by_type);
  818. /**
  819. * of_find_compatible_node - Find a node based on type and one of the
  820. * tokens in its "compatible" property
  821. * @from: The node to start searching from or NULL, the node
  822. * you pass will not be searched, only the next one
  823. * will; typically, you pass what the previous call
  824. * returned. of_node_put() will be called on it
  825. * @type: The type string to match "device_type" or NULL to ignore
  826. * @compatible: The string to match to one of the tokens in the device
  827. * "compatible" list.
  828. *
  829. * Returns a node pointer with refcount incremented, use
  830. * of_node_put() on it when done.
  831. */
  832. struct device_node *of_find_compatible_node(struct device_node *from,
  833. const char *type, const char *compatible)
  834. {
  835. struct device_node *np;
  836. unsigned long flags;
  837. raw_spin_lock_irqsave(&devtree_lock, flags);
  838. np = from ? from->allnext : of_allnodes;
  839. for (; np; np = np->allnext) {
  840. if (__of_device_is_compatible(np, compatible, type, NULL) &&
  841. of_node_get(np))
  842. break;
  843. }
  844. of_node_put(from);
  845. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  846. return np;
  847. }
  848. EXPORT_SYMBOL(of_find_compatible_node);
  849. /**
  850. * of_find_node_with_property - Find a node which has a property with
  851. * the given name.
  852. * @from: The node to start searching from or NULL, the node
  853. * you pass will not be searched, only the next one
  854. * will; typically, you pass what the previous call
  855. * returned. of_node_put() will be called on it
  856. * @prop_name: The name of the property to look for.
  857. *
  858. * Returns a node pointer with refcount incremented, use
  859. * of_node_put() on it when done.
  860. */
  861. struct device_node *of_find_node_with_property(struct device_node *from,
  862. const char *prop_name)
  863. {
  864. struct device_node *np;
  865. struct property *pp;
  866. unsigned long flags;
  867. raw_spin_lock_irqsave(&devtree_lock, flags);
  868. np = from ? from->allnext : of_allnodes;
  869. for (; np; np = np->allnext) {
  870. for (pp = np->properties; pp; pp = pp->next) {
  871. if (of_prop_cmp(pp->name, prop_name) == 0) {
  872. of_node_get(np);
  873. goto out;
  874. }
  875. }
  876. }
  877. out:
  878. of_node_put(from);
  879. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  880. return np;
  881. }
  882. EXPORT_SYMBOL(of_find_node_with_property);
  883. static
  884. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  885. const struct device_node *node)
  886. {
  887. const struct of_device_id *best_match = NULL;
  888. int score, best_score = 0;
  889. if (!matches)
  890. return NULL;
  891. for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
  892. score = __of_device_is_compatible(node, matches->compatible,
  893. matches->type, matches->name);
  894. if (score > best_score) {
  895. best_match = matches;
  896. best_score = score;
  897. }
  898. }
  899. return best_match;
  900. }
  901. /**
  902. * of_match_node - Tell if an device_node has a matching of_match structure
  903. * @matches: array of of device match structures to search in
  904. * @node: the of device structure to match against
  905. *
  906. * Low level utility function used by device matching.
  907. */
  908. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  909. const struct device_node *node)
  910. {
  911. const struct of_device_id *match;
  912. unsigned long flags;
  913. raw_spin_lock_irqsave(&devtree_lock, flags);
  914. match = __of_match_node(matches, node);
  915. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  916. return match;
  917. }
  918. EXPORT_SYMBOL(of_match_node);
  919. /**
  920. * of_find_matching_node_and_match - Find a node based on an of_device_id
  921. * match table.
  922. * @from: The node to start searching from or NULL, the node
  923. * you pass will not be searched, only the next one
  924. * will; typically, you pass what the previous call
  925. * returned. of_node_put() will be called on it
  926. * @matches: array of of device match structures to search in
  927. * @match Updated to point at the matches entry which matched
  928. *
  929. * Returns a node pointer with refcount incremented, use
  930. * of_node_put() on it when done.
  931. */
  932. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  933. const struct of_device_id *matches,
  934. const struct of_device_id **match)
  935. {
  936. struct device_node *np;
  937. const struct of_device_id *m;
  938. unsigned long flags;
  939. if (match)
  940. *match = NULL;
  941. raw_spin_lock_irqsave(&devtree_lock, flags);
  942. np = from ? from->allnext : of_allnodes;
  943. for (; np; np = np->allnext) {
  944. m = __of_match_node(matches, np);
  945. if (m && of_node_get(np)) {
  946. if (match)
  947. *match = m;
  948. break;
  949. }
  950. }
  951. of_node_put(from);
  952. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  953. return np;
  954. }
  955. EXPORT_SYMBOL(of_find_matching_node_and_match);
  956. /**
  957. * of_modalias_node - Lookup appropriate modalias for a device node
  958. * @node: pointer to a device tree node
  959. * @modalias: Pointer to buffer that modalias value will be copied into
  960. * @len: Length of modalias value
  961. *
  962. * Based on the value of the compatible property, this routine will attempt
  963. * to choose an appropriate modalias value for a particular device tree node.
  964. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  965. * from the first entry in the compatible list property.
  966. *
  967. * This routine returns 0 on success, <0 on failure.
  968. */
  969. int of_modalias_node(struct device_node *node, char *modalias, int len)
  970. {
  971. const char *compatible, *p;
  972. int cplen;
  973. compatible = of_get_property(node, "compatible", &cplen);
  974. if (!compatible || strlen(compatible) > cplen)
  975. return -ENODEV;
  976. p = strchr(compatible, ',');
  977. strlcpy(modalias, p ? p + 1 : compatible, len);
  978. return 0;
  979. }
  980. EXPORT_SYMBOL_GPL(of_modalias_node);
  981. /**
  982. * of_find_node_by_phandle - Find a node given a phandle
  983. * @handle: phandle of the node to find
  984. *
  985. * Returns a node pointer with refcount incremented, use
  986. * of_node_put() on it when done.
  987. */
  988. struct device_node *of_find_node_by_phandle(phandle handle)
  989. {
  990. struct device_node *np;
  991. unsigned long flags;
  992. raw_spin_lock_irqsave(&devtree_lock, flags);
  993. for (np = of_allnodes; np; np = np->allnext)
  994. if (np->phandle == handle)
  995. break;
  996. of_node_get(np);
  997. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  998. return np;
  999. }
  1000. EXPORT_SYMBOL(of_find_node_by_phandle);
  1001. /**
  1002. * of_property_count_elems_of_size - Count the number of elements in a property
  1003. *
  1004. * @np: device node from which the property value is to be read.
  1005. * @propname: name of the property to be searched.
  1006. * @elem_size: size of the individual element
  1007. *
  1008. * Search for a property in a device node and count the number of elements of
  1009. * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
  1010. * property does not exist or its length does not match a multiple of elem_size
  1011. * and -ENODATA if the property does not have a value.
  1012. */
  1013. int of_property_count_elems_of_size(const struct device_node *np,
  1014. const char *propname, int elem_size)
  1015. {
  1016. struct property *prop = of_find_property(np, propname, NULL);
  1017. if (!prop)
  1018. return -EINVAL;
  1019. if (!prop->value)
  1020. return -ENODATA;
  1021. if (prop->length % elem_size != 0) {
  1022. pr_err("size of %s in node %s is not a multiple of %d\n",
  1023. propname, np->full_name, elem_size);
  1024. return -EINVAL;
  1025. }
  1026. return prop->length / elem_size;
  1027. }
  1028. EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
  1029. /**
  1030. * of_find_property_value_of_size
  1031. *
  1032. * @np: device node from which the property value is to be read.
  1033. * @propname: name of the property to be searched.
  1034. * @len: requested length of property value
  1035. *
  1036. * Search for a property in a device node and valid the requested size.
  1037. * Returns the property value on success, -EINVAL if the property does not
  1038. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  1039. * property data isn't large enough.
  1040. *
  1041. */
  1042. static void *of_find_property_value_of_size(const struct device_node *np,
  1043. const char *propname, u32 len)
  1044. {
  1045. struct property *prop = of_find_property(np, propname, NULL);
  1046. if (!prop)
  1047. return ERR_PTR(-EINVAL);
  1048. if (!prop->value)
  1049. return ERR_PTR(-ENODATA);
  1050. if (len > prop->length)
  1051. return ERR_PTR(-EOVERFLOW);
  1052. return prop->value;
  1053. }
  1054. /**
  1055. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  1056. *
  1057. * @np: device node from which the property value is to be read.
  1058. * @propname: name of the property to be searched.
  1059. * @index: index of the u32 in the list of values
  1060. * @out_value: pointer to return value, modified only if no error.
  1061. *
  1062. * Search for a property in a device node and read nth 32-bit value from
  1063. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1064. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1065. * property data isn't large enough.
  1066. *
  1067. * The out_value is modified only if a valid u32 value can be decoded.
  1068. */
  1069. int of_property_read_u32_index(const struct device_node *np,
  1070. const char *propname,
  1071. u32 index, u32 *out_value)
  1072. {
  1073. const u32 *val = of_find_property_value_of_size(np, propname,
  1074. ((index + 1) * sizeof(*out_value)));
  1075. if (IS_ERR(val))
  1076. return PTR_ERR(val);
  1077. *out_value = be32_to_cpup(((__be32 *)val) + index);
  1078. return 0;
  1079. }
  1080. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  1081. /**
  1082. * of_property_read_u8_array - Find and read an array of u8 from a property.
  1083. *
  1084. * @np: device node from which the property value is to be read.
  1085. * @propname: name of the property to be searched.
  1086. * @out_values: pointer to return value, modified only if return value is 0.
  1087. * @sz: number of array elements to read
  1088. *
  1089. * Search for a property in a device node and read 8-bit value(s) from
  1090. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1091. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1092. * property data isn't large enough.
  1093. *
  1094. * dts entry of array should be like:
  1095. * property = /bits/ 8 <0x50 0x60 0x70>;
  1096. *
  1097. * The out_values is modified only if a valid u8 value can be decoded.
  1098. */
  1099. int of_property_read_u8_array(const struct device_node *np,
  1100. const char *propname, u8 *out_values, size_t sz)
  1101. {
  1102. const u8 *val = of_find_property_value_of_size(np, propname,
  1103. (sz * sizeof(*out_values)));
  1104. if (IS_ERR(val))
  1105. return PTR_ERR(val);
  1106. while (sz--)
  1107. *out_values++ = *val++;
  1108. return 0;
  1109. }
  1110. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  1111. /**
  1112. * of_property_read_u16_array - Find and read an array of u16 from a property.
  1113. *
  1114. * @np: device node from which the property value is to be read.
  1115. * @propname: name of the property to be searched.
  1116. * @out_values: pointer to return value, modified only if return value is 0.
  1117. * @sz: number of array elements to read
  1118. *
  1119. * Search for a property in a device node and read 16-bit value(s) from
  1120. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1121. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1122. * property data isn't large enough.
  1123. *
  1124. * dts entry of array should be like:
  1125. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  1126. *
  1127. * The out_values is modified only if a valid u16 value can be decoded.
  1128. */
  1129. int of_property_read_u16_array(const struct device_node *np,
  1130. const char *propname, u16 *out_values, size_t sz)
  1131. {
  1132. const __be16 *val = of_find_property_value_of_size(np, propname,
  1133. (sz * sizeof(*out_values)));
  1134. if (IS_ERR(val))
  1135. return PTR_ERR(val);
  1136. while (sz--)
  1137. *out_values++ = be16_to_cpup(val++);
  1138. return 0;
  1139. }
  1140. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  1141. /**
  1142. * of_property_read_u32_array - Find and read an array of 32 bit integers
  1143. * from a property.
  1144. *
  1145. * @np: device node from which the property value is to be read.
  1146. * @propname: name of the property to be searched.
  1147. * @out_values: pointer to return value, modified only if return value is 0.
  1148. * @sz: number of array elements to read
  1149. *
  1150. * Search for a property in a device node and read 32-bit value(s) from
  1151. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1152. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1153. * property data isn't large enough.
  1154. *
  1155. * The out_values is modified only if a valid u32 value can be decoded.
  1156. */
  1157. int of_property_read_u32_array(const struct device_node *np,
  1158. const char *propname, u32 *out_values,
  1159. size_t sz)
  1160. {
  1161. const __be32 *val = of_find_property_value_of_size(np, propname,
  1162. (sz * sizeof(*out_values)));
  1163. if (IS_ERR(val))
  1164. return PTR_ERR(val);
  1165. while (sz--)
  1166. *out_values++ = be32_to_cpup(val++);
  1167. return 0;
  1168. }
  1169. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  1170. /**
  1171. * of_property_read_u64 - Find and read a 64 bit integer from a property
  1172. * @np: device node from which the property value is to be read.
  1173. * @propname: name of the property to be searched.
  1174. * @out_value: pointer to return value, modified only if return value is 0.
  1175. *
  1176. * Search for a property in a device node and read a 64-bit value from
  1177. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1178. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1179. * property data isn't large enough.
  1180. *
  1181. * The out_value is modified only if a valid u64 value can be decoded.
  1182. */
  1183. int of_property_read_u64(const struct device_node *np, const char *propname,
  1184. u64 *out_value)
  1185. {
  1186. const __be32 *val = of_find_property_value_of_size(np, propname,
  1187. sizeof(*out_value));
  1188. if (IS_ERR(val))
  1189. return PTR_ERR(val);
  1190. *out_value = of_read_number(val, 2);
  1191. return 0;
  1192. }
  1193. EXPORT_SYMBOL_GPL(of_property_read_u64);
  1194. /**
  1195. * of_property_read_string - Find and read a string from a property
  1196. * @np: device node from which the property value is to be read.
  1197. * @propname: name of the property to be searched.
  1198. * @out_string: pointer to null terminated return string, modified only if
  1199. * return value is 0.
  1200. *
  1201. * Search for a property in a device tree node and retrieve a null
  1202. * terminated string value (pointer to data, not a copy). Returns 0 on
  1203. * success, -EINVAL if the property does not exist, -ENODATA if property
  1204. * does not have a value, and -EILSEQ if the string is not null-terminated
  1205. * within the length of the property data.
  1206. *
  1207. * The out_string pointer is modified only if a valid string can be decoded.
  1208. */
  1209. int of_property_read_string(struct device_node *np, const char *propname,
  1210. const char **out_string)
  1211. {
  1212. struct property *prop = of_find_property(np, propname, NULL);
  1213. if (!prop)
  1214. return -EINVAL;
  1215. if (!prop->value)
  1216. return -ENODATA;
  1217. if (strnlen(prop->value, prop->length) >= prop->length)
  1218. return -EILSEQ;
  1219. *out_string = prop->value;
  1220. return 0;
  1221. }
  1222. EXPORT_SYMBOL_GPL(of_property_read_string);
  1223. /**
  1224. * of_property_read_string_index - Find and read a string from a multiple
  1225. * strings property.
  1226. * @np: device node from which the property value is to be read.
  1227. * @propname: name of the property to be searched.
  1228. * @index: index of the string in the list of strings
  1229. * @out_string: pointer to null terminated return string, modified only if
  1230. * return value is 0.
  1231. *
  1232. * Search for a property in a device tree node and retrieve a null
  1233. * terminated string value (pointer to data, not a copy) in the list of strings
  1234. * contained in that property.
  1235. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  1236. * property does not have a value, and -EILSEQ if the string is not
  1237. * null-terminated within the length of the property data.
  1238. *
  1239. * The out_string pointer is modified only if a valid string can be decoded.
  1240. */
  1241. int of_property_read_string_index(struct device_node *np, const char *propname,
  1242. int index, const char **output)
  1243. {
  1244. struct property *prop = of_find_property(np, propname, NULL);
  1245. int i = 0;
  1246. size_t l = 0, total = 0;
  1247. const char *p;
  1248. if (!prop)
  1249. return -EINVAL;
  1250. if (!prop->value)
  1251. return -ENODATA;
  1252. if (strnlen(prop->value, prop->length) >= prop->length)
  1253. return -EILSEQ;
  1254. p = prop->value;
  1255. for (i = 0; total < prop->length; total += l, p += l) {
  1256. l = strlen(p) + 1;
  1257. if (i++ == index) {
  1258. *output = p;
  1259. return 0;
  1260. }
  1261. }
  1262. return -ENODATA;
  1263. }
  1264. EXPORT_SYMBOL_GPL(of_property_read_string_index);
  1265. /**
  1266. * of_property_match_string() - Find string in a list and return index
  1267. * @np: pointer to node containing string list property
  1268. * @propname: string list property name
  1269. * @string: pointer to string to search for in string list
  1270. *
  1271. * This function searches a string list property and returns the index
  1272. * of a specific string value.
  1273. */
  1274. int of_property_match_string(struct device_node *np, const char *propname,
  1275. const char *string)
  1276. {
  1277. struct property *prop = of_find_property(np, propname, NULL);
  1278. size_t l;
  1279. int i;
  1280. const char *p, *end;
  1281. if (!prop)
  1282. return -EINVAL;
  1283. if (!prop->value)
  1284. return -ENODATA;
  1285. p = prop->value;
  1286. end = p + prop->length;
  1287. for (i = 0; p < end; i++, p += l) {
  1288. l = strlen(p) + 1;
  1289. if (p + l > end)
  1290. return -EILSEQ;
  1291. pr_debug("comparing %s with %s\n", string, p);
  1292. if (strcmp(string, p) == 0)
  1293. return i; /* Found it; return index */
  1294. }
  1295. return -ENODATA;
  1296. }
  1297. EXPORT_SYMBOL_GPL(of_property_match_string);
  1298. /**
  1299. * of_property_count_strings - Find and return the number of strings from a
  1300. * multiple strings property.
  1301. * @np: device node from which the property value is to be read.
  1302. * @propname: name of the property to be searched.
  1303. *
  1304. * Search for a property in a device tree node and retrieve the number of null
  1305. * terminated string contain in it. Returns the number of strings on
  1306. * success, -EINVAL if the property does not exist, -ENODATA if property
  1307. * does not have a value, and -EILSEQ if the string is not null-terminated
  1308. * within the length of the property data.
  1309. */
  1310. int of_property_count_strings(struct device_node *np, const char *propname)
  1311. {
  1312. struct property *prop = of_find_property(np, propname, NULL);
  1313. int i = 0;
  1314. size_t l = 0, total = 0;
  1315. const char *p;
  1316. if (!prop)
  1317. return -EINVAL;
  1318. if (!prop->value)
  1319. return -ENODATA;
  1320. if (strnlen(prop->value, prop->length) >= prop->length)
  1321. return -EILSEQ;
  1322. p = prop->value;
  1323. for (i = 0; total < prop->length; total += l, p += l, i++)
  1324. l = strlen(p) + 1;
  1325. return i;
  1326. }
  1327. EXPORT_SYMBOL_GPL(of_property_count_strings);
  1328. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1329. {
  1330. int i;
  1331. printk("%s %s", msg, of_node_full_name(args->np));
  1332. for (i = 0; i < args->args_count; i++)
  1333. printk(i ? ",%08x" : ":%08x", args->args[i]);
  1334. printk("\n");
  1335. }
  1336. static int __of_parse_phandle_with_args(const struct device_node *np,
  1337. const char *list_name,
  1338. const char *cells_name,
  1339. int cell_count, int index,
  1340. struct of_phandle_args *out_args)
  1341. {
  1342. const __be32 *list, *list_end;
  1343. int rc = 0, size, cur_index = 0;
  1344. uint32_t count = 0;
  1345. struct device_node *node = NULL;
  1346. phandle phandle;
  1347. /* Retrieve the phandle list property */
  1348. list = of_get_property(np, list_name, &size);
  1349. if (!list)
  1350. return -ENOENT;
  1351. list_end = list + size / sizeof(*list);
  1352. /* Loop over the phandles until all the requested entry is found */
  1353. while (list < list_end) {
  1354. rc = -EINVAL;
  1355. count = 0;
  1356. /*
  1357. * If phandle is 0, then it is an empty entry with no
  1358. * arguments. Skip forward to the next entry.
  1359. */
  1360. phandle = be32_to_cpup(list++);
  1361. if (phandle) {
  1362. /*
  1363. * Find the provider node and parse the #*-cells
  1364. * property to determine the argument length.
  1365. *
  1366. * This is not needed if the cell count is hard-coded
  1367. * (i.e. cells_name not set, but cell_count is set),
  1368. * except when we're going to return the found node
  1369. * below.
  1370. */
  1371. if (cells_name || cur_index == index) {
  1372. node = of_find_node_by_phandle(phandle);
  1373. if (!node) {
  1374. pr_err("%s: could not find phandle\n",
  1375. np->full_name);
  1376. goto err;
  1377. }
  1378. }
  1379. if (cells_name) {
  1380. if (of_property_read_u32(node, cells_name,
  1381. &count)) {
  1382. pr_err("%s: could not get %s for %s\n",
  1383. np->full_name, cells_name,
  1384. node->full_name);
  1385. goto err;
  1386. }
  1387. } else {
  1388. count = cell_count;
  1389. }
  1390. /*
  1391. * Make sure that the arguments actually fit in the
  1392. * remaining property data length
  1393. */
  1394. if (list + count > list_end) {
  1395. pr_err("%s: arguments longer than property\n",
  1396. np->full_name);
  1397. goto err;
  1398. }
  1399. }
  1400. /*
  1401. * All of the error cases above bail out of the loop, so at
  1402. * this point, the parsing is successful. If the requested
  1403. * index matches, then fill the out_args structure and return,
  1404. * or return -ENOENT for an empty entry.
  1405. */
  1406. rc = -ENOENT;
  1407. if (cur_index == index) {
  1408. if (!phandle)
  1409. goto err;
  1410. if (out_args) {
  1411. int i;
  1412. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  1413. count = MAX_PHANDLE_ARGS;
  1414. out_args->np = node;
  1415. out_args->args_count = count;
  1416. for (i = 0; i < count; i++)
  1417. out_args->args[i] = be32_to_cpup(list++);
  1418. } else {
  1419. of_node_put(node);
  1420. }
  1421. /* Found it! return success */
  1422. return 0;
  1423. }
  1424. of_node_put(node);
  1425. node = NULL;
  1426. list += count;
  1427. cur_index++;
  1428. }
  1429. /*
  1430. * Unlock node before returning result; will be one of:
  1431. * -ENOENT : index is for empty phandle
  1432. * -EINVAL : parsing error on data
  1433. * [1..n] : Number of phandle (count mode; when index = -1)
  1434. */
  1435. rc = index < 0 ? cur_index : -ENOENT;
  1436. err:
  1437. if (node)
  1438. of_node_put(node);
  1439. return rc;
  1440. }
  1441. /**
  1442. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1443. * @np: Pointer to device node holding phandle property
  1444. * @phandle_name: Name of property holding a phandle value
  1445. * @index: For properties holding a table of phandles, this is the index into
  1446. * the table
  1447. *
  1448. * Returns the device_node pointer with refcount incremented. Use
  1449. * of_node_put() on it when done.
  1450. */
  1451. struct device_node *of_parse_phandle(const struct device_node *np,
  1452. const char *phandle_name, int index)
  1453. {
  1454. struct of_phandle_args args;
  1455. if (index < 0)
  1456. return NULL;
  1457. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1458. index, &args))
  1459. return NULL;
  1460. return args.np;
  1461. }
  1462. EXPORT_SYMBOL(of_parse_phandle);
  1463. /**
  1464. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1465. * @np: pointer to a device tree node containing a list
  1466. * @list_name: property name that contains a list
  1467. * @cells_name: property name that specifies phandles' arguments count
  1468. * @index: index of a phandle to parse out
  1469. * @out_args: optional pointer to output arguments structure (will be filled)
  1470. *
  1471. * This function is useful to parse lists of phandles and their arguments.
  1472. * Returns 0 on success and fills out_args, on error returns appropriate
  1473. * errno value.
  1474. *
  1475. * Caller is responsible to call of_node_put() on the returned out_args->node
  1476. * pointer.
  1477. *
  1478. * Example:
  1479. *
  1480. * phandle1: node1 {
  1481. * #list-cells = <2>;
  1482. * }
  1483. *
  1484. * phandle2: node2 {
  1485. * #list-cells = <1>;
  1486. * }
  1487. *
  1488. * node3 {
  1489. * list = <&phandle1 1 2 &phandle2 3>;
  1490. * }
  1491. *
  1492. * To get a device_node of the `node2' node you may call this:
  1493. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1494. */
  1495. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1496. const char *cells_name, int index,
  1497. struct of_phandle_args *out_args)
  1498. {
  1499. if (index < 0)
  1500. return -EINVAL;
  1501. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1502. index, out_args);
  1503. }
  1504. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1505. /**
  1506. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1507. * @np: pointer to a device tree node containing a list
  1508. * @list_name: property name that contains a list
  1509. * @cell_count: number of argument cells following the phandle
  1510. * @index: index of a phandle to parse out
  1511. * @out_args: optional pointer to output arguments structure (will be filled)
  1512. *
  1513. * This function is useful to parse lists of phandles and their arguments.
  1514. * Returns 0 on success and fills out_args, on error returns appropriate
  1515. * errno value.
  1516. *
  1517. * Caller is responsible to call of_node_put() on the returned out_args->node
  1518. * pointer.
  1519. *
  1520. * Example:
  1521. *
  1522. * phandle1: node1 {
  1523. * }
  1524. *
  1525. * phandle2: node2 {
  1526. * }
  1527. *
  1528. * node3 {
  1529. * list = <&phandle1 0 2 &phandle2 2 3>;
  1530. * }
  1531. *
  1532. * To get a device_node of the `node2' node you may call this:
  1533. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1534. */
  1535. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1536. const char *list_name, int cell_count,
  1537. int index, struct of_phandle_args *out_args)
  1538. {
  1539. if (index < 0)
  1540. return -EINVAL;
  1541. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1542. index, out_args);
  1543. }
  1544. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1545. /**
  1546. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1547. * @np: pointer to a device tree node containing a list
  1548. * @list_name: property name that contains a list
  1549. * @cells_name: property name that specifies phandles' arguments count
  1550. *
  1551. * Returns the number of phandle + argument tuples within a property. It
  1552. * is a typical pattern to encode a list of phandle and variable
  1553. * arguments into a single property. The number of arguments is encoded
  1554. * by a property in the phandle-target node. For example, a gpios
  1555. * property would contain a list of GPIO specifies consisting of a
  1556. * phandle and 1 or more arguments. The number of arguments are
  1557. * determined by the #gpio-cells property in the node pointed to by the
  1558. * phandle.
  1559. */
  1560. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1561. const char *cells_name)
  1562. {
  1563. return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
  1564. NULL);
  1565. }
  1566. EXPORT_SYMBOL(of_count_phandle_with_args);
  1567. #if defined(CONFIG_OF_DYNAMIC)
  1568. static int of_property_notify(int action, struct device_node *np,
  1569. struct property *prop)
  1570. {
  1571. struct of_prop_reconfig pr;
  1572. /* only call notifiers if the node is attached */
  1573. if (!of_node_is_attached(np))
  1574. return 0;
  1575. pr.dn = np;
  1576. pr.prop = prop;
  1577. return of_reconfig_notify(action, &pr);
  1578. }
  1579. #else
  1580. static int of_property_notify(int action, struct device_node *np,
  1581. struct property *prop)
  1582. {
  1583. return 0;
  1584. }
  1585. #endif
  1586. /**
  1587. * __of_add_property - Add a property to a node without lock operations
  1588. */
  1589. static int __of_add_property(struct device_node *np, struct property *prop)
  1590. {
  1591. struct property **next;
  1592. prop->next = NULL;
  1593. next = &np->properties;
  1594. while (*next) {
  1595. if (strcmp(prop->name, (*next)->name) == 0)
  1596. /* duplicate ! don't insert it */
  1597. return -EEXIST;
  1598. next = &(*next)->next;
  1599. }
  1600. *next = prop;
  1601. return 0;
  1602. }
  1603. /**
  1604. * of_add_property - Add a property to a node
  1605. */
  1606. int of_add_property(struct device_node *np, struct property *prop)
  1607. {
  1608. unsigned long flags;
  1609. int rc;
  1610. rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
  1611. if (rc)
  1612. return rc;
  1613. raw_spin_lock_irqsave(&devtree_lock, flags);
  1614. rc = __of_add_property(np, prop);
  1615. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1616. if (rc)
  1617. return rc;
  1618. if (of_node_is_attached(np))
  1619. __of_add_property_sysfs(np, prop);
  1620. return rc;
  1621. }
  1622. /**
  1623. * of_remove_property - Remove a property from a node.
  1624. *
  1625. * Note that we don't actually remove it, since we have given out
  1626. * who-knows-how-many pointers to the data using get-property.
  1627. * Instead we just move the property to the "dead properties"
  1628. * list, so it won't be found any more.
  1629. */
  1630. int of_remove_property(struct device_node *np, struct property *prop)
  1631. {
  1632. struct property **next;
  1633. unsigned long flags;
  1634. int found = 0;
  1635. int rc;
  1636. rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  1637. if (rc)
  1638. return rc;
  1639. raw_spin_lock_irqsave(&devtree_lock, flags);
  1640. next = &np->properties;
  1641. while (*next) {
  1642. if (*next == prop) {
  1643. /* found the node */
  1644. *next = prop->next;
  1645. prop->next = np->deadprops;
  1646. np->deadprops = prop;
  1647. found = 1;
  1648. break;
  1649. }
  1650. next = &(*next)->next;
  1651. }
  1652. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1653. if (!found)
  1654. return -ENODEV;
  1655. /* at early boot, bail hear and defer setup to of_init() */
  1656. if (!of_kset)
  1657. return 0;
  1658. sysfs_remove_bin_file(&np->kobj, &prop->attr);
  1659. return 0;
  1660. }
  1661. /*
  1662. * of_update_property - Update a property in a node, if the property does
  1663. * not exist, add it.
  1664. *
  1665. * Note that we don't actually remove it, since we have given out
  1666. * who-knows-how-many pointers to the data using get-property.
  1667. * Instead we just move the property to the "dead properties" list,
  1668. * and add the new property to the property list
  1669. */
  1670. int of_update_property(struct device_node *np, struct property *newprop)
  1671. {
  1672. struct property **next, *oldprop;
  1673. unsigned long flags;
  1674. int rc;
  1675. rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
  1676. if (rc)
  1677. return rc;
  1678. if (!newprop->name)
  1679. return -EINVAL;
  1680. raw_spin_lock_irqsave(&devtree_lock, flags);
  1681. next = &np->properties;
  1682. oldprop = __of_find_property(np, newprop->name, NULL);
  1683. if (!oldprop) {
  1684. /* add the new node */
  1685. rc = __of_add_property(np, newprop);
  1686. } else while (*next) {
  1687. /* replace the node */
  1688. if (*next == oldprop) {
  1689. newprop->next = oldprop->next;
  1690. *next = newprop;
  1691. oldprop->next = np->deadprops;
  1692. np->deadprops = oldprop;
  1693. break;
  1694. }
  1695. next = &(*next)->next;
  1696. }
  1697. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1698. if (rc)
  1699. return rc;
  1700. /* At early boot, bail out and defer setup to of_init() */
  1701. if (!of_kset)
  1702. return 0;
  1703. /* Update the sysfs attribute */
  1704. if (oldprop)
  1705. sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
  1706. __of_add_property_sysfs(np, newprop);
  1707. return 0;
  1708. }
  1709. #if defined(CONFIG_OF_DYNAMIC)
  1710. /*
  1711. * Support for dynamic device trees.
  1712. *
  1713. * On some platforms, the device tree can be manipulated at runtime.
  1714. * The routines in this section support adding, removing and changing
  1715. * device tree nodes.
  1716. */
  1717. static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
  1718. int of_reconfig_notifier_register(struct notifier_block *nb)
  1719. {
  1720. return blocking_notifier_chain_register(&of_reconfig_chain, nb);
  1721. }
  1722. EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
  1723. int of_reconfig_notifier_unregister(struct notifier_block *nb)
  1724. {
  1725. return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
  1726. }
  1727. EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
  1728. int of_reconfig_notify(unsigned long action, void *p)
  1729. {
  1730. int rc;
  1731. rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
  1732. return notifier_to_errno(rc);
  1733. }
  1734. /**
  1735. * of_attach_node - Plug a device node into the tree and global list.
  1736. */
  1737. int of_attach_node(struct device_node *np)
  1738. {
  1739. unsigned long flags;
  1740. int rc;
  1741. rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
  1742. if (rc)
  1743. return rc;
  1744. raw_spin_lock_irqsave(&devtree_lock, flags);
  1745. np->sibling = np->parent->child;
  1746. np->allnext = np->parent->allnext;
  1747. np->parent->allnext = np;
  1748. np->parent->child = np;
  1749. of_node_clear_flag(np, OF_DETACHED);
  1750. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1751. of_node_add(np);
  1752. return 0;
  1753. }
  1754. /**
  1755. * of_detach_node - "Unplug" a node from the device tree.
  1756. *
  1757. * The caller must hold a reference to the node. The memory associated with
  1758. * the node is not freed until its refcount goes to zero.
  1759. */
  1760. int of_detach_node(struct device_node *np)
  1761. {
  1762. struct device_node *parent;
  1763. unsigned long flags;
  1764. int rc = 0;
  1765. rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
  1766. if (rc)
  1767. return rc;
  1768. raw_spin_lock_irqsave(&devtree_lock, flags);
  1769. if (of_node_check_flag(np, OF_DETACHED)) {
  1770. /* someone already detached it */
  1771. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1772. return rc;
  1773. }
  1774. parent = np->parent;
  1775. if (!parent) {
  1776. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1777. return rc;
  1778. }
  1779. if (of_allnodes == np)
  1780. of_allnodes = np->allnext;
  1781. else {
  1782. struct device_node *prev;
  1783. for (prev = of_allnodes;
  1784. prev->allnext != np;
  1785. prev = prev->allnext)
  1786. ;
  1787. prev->allnext = np->allnext;
  1788. }
  1789. if (parent->child == np)
  1790. parent->child = np->sibling;
  1791. else {
  1792. struct device_node *prevsib;
  1793. for (prevsib = np->parent->child;
  1794. prevsib->sibling != np;
  1795. prevsib = prevsib->sibling)
  1796. ;
  1797. prevsib->sibling = np->sibling;
  1798. }
  1799. of_node_set_flag(np, OF_DETACHED);
  1800. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1801. of_node_remove(np);
  1802. return rc;
  1803. }
  1804. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1805. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1806. int id, const char *stem, int stem_len)
  1807. {
  1808. ap->np = np;
  1809. ap->id = id;
  1810. strncpy(ap->stem, stem, stem_len);
  1811. ap->stem[stem_len] = 0;
  1812. list_add_tail(&ap->link, &aliases_lookup);
  1813. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1814. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1815. }
  1816. /**
  1817. * of_alias_scan - Scan all properties of 'aliases' node
  1818. *
  1819. * The function scans all the properties of 'aliases' node and populate
  1820. * the the global lookup table with the properties. It returns the
  1821. * number of alias_prop found, or error code in error case.
  1822. *
  1823. * @dt_alloc: An allocator that provides a virtual address to memory
  1824. * for the resulting tree
  1825. */
  1826. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1827. {
  1828. struct property *pp;
  1829. of_chosen = of_find_node_by_path("/chosen");
  1830. if (of_chosen == NULL)
  1831. of_chosen = of_find_node_by_path("/chosen@0");
  1832. if (of_chosen) {
  1833. const char *name = of_get_property(of_chosen, "stdout-path", NULL);
  1834. if (!name)
  1835. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  1836. if (name)
  1837. of_stdout = of_find_node_by_path(name);
  1838. }
  1839. of_aliases = of_find_node_by_path("/aliases");
  1840. if (!of_aliases)
  1841. return;
  1842. for_each_property_of_node(of_aliases, pp) {
  1843. const char *start = pp->name;
  1844. const char *end = start + strlen(start);
  1845. struct device_node *np;
  1846. struct alias_prop *ap;
  1847. int id, len;
  1848. /* Skip those we do not want to proceed */
  1849. if (!strcmp(pp->name, "name") ||
  1850. !strcmp(pp->name, "phandle") ||
  1851. !strcmp(pp->name, "linux,phandle"))
  1852. continue;
  1853. np = of_find_node_by_path(pp->value);
  1854. if (!np)
  1855. continue;
  1856. /* walk the alias backwards to extract the id and work out
  1857. * the 'stem' string */
  1858. while (isdigit(*(end-1)) && end > start)
  1859. end--;
  1860. len = end - start;
  1861. if (kstrtoint(end, 10, &id) < 0)
  1862. continue;
  1863. /* Allocate an alias_prop with enough space for the stem */
  1864. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1865. if (!ap)
  1866. continue;
  1867. memset(ap, 0, sizeof(*ap) + len + 1);
  1868. ap->alias = start;
  1869. of_alias_add(ap, np, id, start, len);
  1870. }
  1871. }
  1872. /**
  1873. * of_alias_get_id - Get alias id for the given device_node
  1874. * @np: Pointer to the given device_node
  1875. * @stem: Alias stem of the given device_node
  1876. *
  1877. * The function travels the lookup table to get the alias id for the given
  1878. * device_node and alias stem. It returns the alias id if found.
  1879. */
  1880. int of_alias_get_id(struct device_node *np, const char *stem)
  1881. {
  1882. struct alias_prop *app;
  1883. int id = -ENODEV;
  1884. mutex_lock(&of_aliases_mutex);
  1885. list_for_each_entry(app, &aliases_lookup, link) {
  1886. if (strcmp(app->stem, stem) != 0)
  1887. continue;
  1888. if (np == app->np) {
  1889. id = app->id;
  1890. break;
  1891. }
  1892. }
  1893. mutex_unlock(&of_aliases_mutex);
  1894. return id;
  1895. }
  1896. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1897. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1898. u32 *pu)
  1899. {
  1900. const void *curv = cur;
  1901. if (!prop)
  1902. return NULL;
  1903. if (!cur) {
  1904. curv = prop->value;
  1905. goto out_val;
  1906. }
  1907. curv += sizeof(*cur);
  1908. if (curv >= prop->value + prop->length)
  1909. return NULL;
  1910. out_val:
  1911. *pu = be32_to_cpup(curv);
  1912. return curv;
  1913. }
  1914. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1915. const char *of_prop_next_string(struct property *prop, const char *cur)
  1916. {
  1917. const void *curv = cur;
  1918. if (!prop)
  1919. return NULL;
  1920. if (!cur)
  1921. return prop->value;
  1922. curv += strlen(cur) + 1;
  1923. if (curv >= prop->value + prop->length)
  1924. return NULL;
  1925. return curv;
  1926. }
  1927. EXPORT_SYMBOL_GPL(of_prop_next_string);
  1928. /**
  1929. * of_device_is_stdout_path - check if a device node matches the
  1930. * linux,stdout-path property
  1931. *
  1932. * Check if this device node matches the linux,stdout-path property
  1933. * in the chosen node. return true if yes, false otherwise.
  1934. */
  1935. int of_device_is_stdout_path(struct device_node *dn)
  1936. {
  1937. if (!of_stdout)
  1938. return false;
  1939. return of_stdout == dn;
  1940. }
  1941. EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
  1942. /**
  1943. * of_find_next_cache_node - Find a node's subsidiary cache
  1944. * @np: node of type "cpu" or "cache"
  1945. *
  1946. * Returns a node pointer with refcount incremented, use
  1947. * of_node_put() on it when done. Caller should hold a reference
  1948. * to np.
  1949. */
  1950. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1951. {
  1952. struct device_node *child;
  1953. const phandle *handle;
  1954. handle = of_get_property(np, "l2-cache", NULL);
  1955. if (!handle)
  1956. handle = of_get_property(np, "next-level-cache", NULL);
  1957. if (handle)
  1958. return of_find_node_by_phandle(be32_to_cpup(handle));
  1959. /* OF on pmac has nodes instead of properties named "l2-cache"
  1960. * beneath CPU nodes.
  1961. */
  1962. if (!strcmp(np->type, "cpu"))
  1963. for_each_child_of_node(np, child)
  1964. if (!strcmp(child->type, "cache"))
  1965. return child;
  1966. return NULL;
  1967. }
  1968. /**
  1969. * of_graph_parse_endpoint() - parse common endpoint node properties
  1970. * @node: pointer to endpoint device_node
  1971. * @endpoint: pointer to the OF endpoint data structure
  1972. *
  1973. * The caller should hold a reference to @node.
  1974. */
  1975. int of_graph_parse_endpoint(const struct device_node *node,
  1976. struct of_endpoint *endpoint)
  1977. {
  1978. struct device_node *port_node = of_get_parent(node);
  1979. WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
  1980. __func__, node->full_name);
  1981. memset(endpoint, 0, sizeof(*endpoint));
  1982. endpoint->local_node = node;
  1983. /*
  1984. * It doesn't matter whether the two calls below succeed.
  1985. * If they don't then the default value 0 is used.
  1986. */
  1987. of_property_read_u32(port_node, "reg", &endpoint->port);
  1988. of_property_read_u32(node, "reg", &endpoint->id);
  1989. of_node_put(port_node);
  1990. return 0;
  1991. }
  1992. EXPORT_SYMBOL(of_graph_parse_endpoint);
  1993. /**
  1994. * of_graph_get_next_endpoint() - get next endpoint node
  1995. * @parent: pointer to the parent device node
  1996. * @prev: previous endpoint node, or NULL to get first
  1997. *
  1998. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  1999. * of the passed @prev node is not decremented, the caller have to use
  2000. * of_node_put() on it when done.
  2001. */
  2002. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  2003. struct device_node *prev)
  2004. {
  2005. struct device_node *endpoint;
  2006. struct device_node *port;
  2007. if (!parent)
  2008. return NULL;
  2009. /*
  2010. * Start by locating the port node. If no previous endpoint is specified
  2011. * search for the first port node, otherwise get the previous endpoint
  2012. * parent port node.
  2013. */
  2014. if (!prev) {
  2015. struct device_node *node;
  2016. node = of_get_child_by_name(parent, "ports");
  2017. if (node)
  2018. parent = node;
  2019. port = of_get_child_by_name(parent, "port");
  2020. of_node_put(node);
  2021. if (!port) {
  2022. pr_err("%s(): no port node found in %s\n",
  2023. __func__, parent->full_name);
  2024. return NULL;
  2025. }
  2026. } else {
  2027. port = of_get_parent(prev);
  2028. if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
  2029. __func__, prev->full_name))
  2030. return NULL;
  2031. /*
  2032. * Avoid dropping prev node refcount to 0 when getting the next
  2033. * child below.
  2034. */
  2035. of_node_get(prev);
  2036. }
  2037. while (1) {
  2038. /*
  2039. * Now that we have a port node, get the next endpoint by
  2040. * getting the next child. If the previous endpoint is NULL this
  2041. * will return the first child.
  2042. */
  2043. endpoint = of_get_next_child(port, prev);
  2044. if (endpoint) {
  2045. of_node_put(port);
  2046. return endpoint;
  2047. }
  2048. /* No more endpoints under this port, try the next one. */
  2049. prev = NULL;
  2050. do {
  2051. port = of_get_next_child(parent, port);
  2052. if (!port)
  2053. return NULL;
  2054. } while (of_node_cmp(port->name, "port"));
  2055. }
  2056. }
  2057. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  2058. /**
  2059. * of_graph_get_remote_port_parent() - get remote port's parent node
  2060. * @node: pointer to a local endpoint device_node
  2061. *
  2062. * Return: Remote device node associated with remote endpoint node linked
  2063. * to @node. Use of_node_put() on it when done.
  2064. */
  2065. struct device_node *of_graph_get_remote_port_parent(
  2066. const struct device_node *node)
  2067. {
  2068. struct device_node *np;
  2069. unsigned int depth;
  2070. /* Get remote endpoint node. */
  2071. np = of_parse_phandle(node, "remote-endpoint", 0);
  2072. /* Walk 3 levels up only if there is 'ports' node. */
  2073. for (depth = 3; depth && np; depth--) {
  2074. np = of_get_next_parent(np);
  2075. if (depth == 2 && of_node_cmp(np->name, "ports"))
  2076. break;
  2077. }
  2078. return np;
  2079. }
  2080. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  2081. /**
  2082. * of_graph_get_remote_port() - get remote port node
  2083. * @node: pointer to a local endpoint device_node
  2084. *
  2085. * Return: Remote port node associated with remote endpoint node linked
  2086. * to @node. Use of_node_put() on it when done.
  2087. */
  2088. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  2089. {
  2090. struct device_node *np;
  2091. /* Get remote endpoint node. */
  2092. np = of_parse_phandle(node, "remote-endpoint", 0);
  2093. if (!np)
  2094. return NULL;
  2095. return of_get_next_parent(np);
  2096. }
  2097. EXPORT_SYMBOL(of_graph_get_remote_port);