base.c 63 KB

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