base.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  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. /**
  610. * of_get_next_child - Iterate a node childs
  611. * @node: parent node
  612. * @prev: previous child of the parent node, or NULL to get first
  613. *
  614. * Returns a node pointer with refcount incremented, use
  615. * of_node_put() on it when done.
  616. */
  617. struct device_node *of_get_next_child(const struct device_node *node,
  618. struct device_node *prev)
  619. {
  620. struct device_node *next;
  621. unsigned long flags;
  622. raw_spin_lock_irqsave(&devtree_lock, flags);
  623. next = prev ? prev->sibling : node->child;
  624. for (; next; next = next->sibling)
  625. if (of_node_get(next))
  626. break;
  627. of_node_put(prev);
  628. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  629. return next;
  630. }
  631. EXPORT_SYMBOL(of_get_next_child);
  632. /**
  633. * of_get_next_available_child - Find the next available child node
  634. * @node: parent node
  635. * @prev: previous child of the parent node, or NULL to get first
  636. *
  637. * This function is like of_get_next_child(), except that it
  638. * automatically skips any disabled nodes (i.e. status = "disabled").
  639. */
  640. struct device_node *of_get_next_available_child(const struct device_node *node,
  641. struct device_node *prev)
  642. {
  643. struct device_node *next;
  644. unsigned long flags;
  645. raw_spin_lock_irqsave(&devtree_lock, flags);
  646. next = prev ? prev->sibling : node->child;
  647. for (; next; next = next->sibling) {
  648. if (!__of_device_is_available(next))
  649. continue;
  650. if (of_node_get(next))
  651. break;
  652. }
  653. of_node_put(prev);
  654. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  655. return next;
  656. }
  657. EXPORT_SYMBOL(of_get_next_available_child);
  658. /**
  659. * of_get_child_by_name - Find the child node by name for a given parent
  660. * @node: parent node
  661. * @name: child name to look for.
  662. *
  663. * This function looks for child node for given matching name
  664. *
  665. * Returns a node pointer if found, with refcount incremented, use
  666. * of_node_put() on it when done.
  667. * Returns NULL if node is not found.
  668. */
  669. struct device_node *of_get_child_by_name(const struct device_node *node,
  670. const char *name)
  671. {
  672. struct device_node *child;
  673. for_each_child_of_node(node, child)
  674. if (child->name && (of_node_cmp(child->name, name) == 0))
  675. break;
  676. return child;
  677. }
  678. EXPORT_SYMBOL(of_get_child_by_name);
  679. /**
  680. * of_find_node_by_path - Find a node matching a full OF path
  681. * @path: The full path to match
  682. *
  683. * Returns a node pointer with refcount incremented, use
  684. * of_node_put() on it when done.
  685. */
  686. struct device_node *of_find_node_by_path(const char *path)
  687. {
  688. struct device_node *np = of_allnodes;
  689. unsigned long flags;
  690. raw_spin_lock_irqsave(&devtree_lock, flags);
  691. for (; np; np = np->allnext) {
  692. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  693. && of_node_get(np))
  694. break;
  695. }
  696. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  697. return np;
  698. }
  699. EXPORT_SYMBOL(of_find_node_by_path);
  700. /**
  701. * of_find_node_by_name - Find a node by its "name" property
  702. * @from: The node to start searching from or NULL, the node
  703. * you pass will not be searched, only the next one
  704. * will; typically, you pass what the previous call
  705. * returned. of_node_put() will be called on it
  706. * @name: The name string to match against
  707. *
  708. * Returns a node pointer with refcount incremented, use
  709. * of_node_put() on it when done.
  710. */
  711. struct device_node *of_find_node_by_name(struct device_node *from,
  712. const char *name)
  713. {
  714. struct device_node *np;
  715. unsigned long flags;
  716. raw_spin_lock_irqsave(&devtree_lock, flags);
  717. np = from ? from->allnext : of_allnodes;
  718. for (; np; np = np->allnext)
  719. if (np->name && (of_node_cmp(np->name, name) == 0)
  720. && of_node_get(np))
  721. break;
  722. of_node_put(from);
  723. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  724. return np;
  725. }
  726. EXPORT_SYMBOL(of_find_node_by_name);
  727. /**
  728. * of_find_node_by_type - Find a node by its "device_type" property
  729. * @from: The node to start searching from, or NULL to start searching
  730. * the entire device tree. The node you pass will not be
  731. * searched, only the next one will; typically, you pass
  732. * what the previous call returned. of_node_put() will be
  733. * called on from for you.
  734. * @type: The type string to match against
  735. *
  736. * Returns a node pointer with refcount incremented, use
  737. * of_node_put() on it when done.
  738. */
  739. struct device_node *of_find_node_by_type(struct device_node *from,
  740. const char *type)
  741. {
  742. struct device_node *np;
  743. unsigned long flags;
  744. raw_spin_lock_irqsave(&devtree_lock, flags);
  745. np = from ? from->allnext : of_allnodes;
  746. for (; np; np = np->allnext)
  747. if (np->type && (of_node_cmp(np->type, type) == 0)
  748. && of_node_get(np))
  749. break;
  750. of_node_put(from);
  751. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  752. return np;
  753. }
  754. EXPORT_SYMBOL(of_find_node_by_type);
  755. /**
  756. * of_find_compatible_node - Find a node based on type and one of the
  757. * tokens in its "compatible" property
  758. * @from: The node to start searching from or NULL, the node
  759. * you pass will not be searched, only the next one
  760. * will; typically, you pass what the previous call
  761. * returned. of_node_put() will be called on it
  762. * @type: The type string to match "device_type" or NULL to ignore
  763. * @compatible: The string to match to one of the tokens in the device
  764. * "compatible" list.
  765. *
  766. * Returns a node pointer with refcount incremented, use
  767. * of_node_put() on it when done.
  768. */
  769. struct device_node *of_find_compatible_node(struct device_node *from,
  770. const char *type, const char *compatible)
  771. {
  772. struct device_node *np;
  773. unsigned long flags;
  774. raw_spin_lock_irqsave(&devtree_lock, flags);
  775. np = from ? from->allnext : of_allnodes;
  776. for (; np; np = np->allnext) {
  777. if (__of_device_is_compatible(np, compatible, type, NULL) &&
  778. of_node_get(np))
  779. break;
  780. }
  781. of_node_put(from);
  782. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  783. return np;
  784. }
  785. EXPORT_SYMBOL(of_find_compatible_node);
  786. /**
  787. * of_find_node_with_property - Find a node which has a property with
  788. * the given name.
  789. * @from: The node to start searching from or NULL, the node
  790. * you pass will not be searched, only the next one
  791. * will; typically, you pass what the previous call
  792. * returned. of_node_put() will be called on it
  793. * @prop_name: The name of the property to look for.
  794. *
  795. * Returns a node pointer with refcount incremented, use
  796. * of_node_put() on it when done.
  797. */
  798. struct device_node *of_find_node_with_property(struct device_node *from,
  799. const char *prop_name)
  800. {
  801. struct device_node *np;
  802. struct property *pp;
  803. unsigned long flags;
  804. raw_spin_lock_irqsave(&devtree_lock, flags);
  805. np = from ? from->allnext : of_allnodes;
  806. for (; np; np = np->allnext) {
  807. for (pp = np->properties; pp; pp = pp->next) {
  808. if (of_prop_cmp(pp->name, prop_name) == 0) {
  809. of_node_get(np);
  810. goto out;
  811. }
  812. }
  813. }
  814. out:
  815. of_node_put(from);
  816. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  817. return np;
  818. }
  819. EXPORT_SYMBOL(of_find_node_with_property);
  820. static
  821. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  822. const struct device_node *node)
  823. {
  824. const struct of_device_id *best_match = NULL;
  825. int score, best_score = 0;
  826. if (!matches)
  827. return NULL;
  828. for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
  829. score = __of_device_is_compatible(node, matches->compatible,
  830. matches->type, matches->name);
  831. if (score > best_score) {
  832. best_match = matches;
  833. best_score = score;
  834. }
  835. }
  836. return best_match;
  837. }
  838. /**
  839. * of_match_node - Tell if an device_node has a matching of_match structure
  840. * @matches: array of of device match structures to search in
  841. * @node: the of device structure to match against
  842. *
  843. * Low level utility function used by device matching.
  844. */
  845. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  846. const struct device_node *node)
  847. {
  848. const struct of_device_id *match;
  849. unsigned long flags;
  850. raw_spin_lock_irqsave(&devtree_lock, flags);
  851. match = __of_match_node(matches, node);
  852. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  853. return match;
  854. }
  855. EXPORT_SYMBOL(of_match_node);
  856. /**
  857. * of_find_matching_node_and_match - Find a node based on an of_device_id
  858. * match table.
  859. * @from: The node to start searching from or NULL, the node
  860. * you pass will not be searched, only the next one
  861. * will; typically, you pass what the previous call
  862. * returned. of_node_put() will be called on it
  863. * @matches: array of of device match structures to search in
  864. * @match Updated to point at the matches entry which matched
  865. *
  866. * Returns a node pointer with refcount incremented, use
  867. * of_node_put() on it when done.
  868. */
  869. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  870. const struct of_device_id *matches,
  871. const struct of_device_id **match)
  872. {
  873. struct device_node *np;
  874. const struct of_device_id *m;
  875. unsigned long flags;
  876. if (match)
  877. *match = NULL;
  878. raw_spin_lock_irqsave(&devtree_lock, flags);
  879. np = from ? from->allnext : of_allnodes;
  880. for (; np; np = np->allnext) {
  881. m = __of_match_node(matches, np);
  882. if (m && of_node_get(np)) {
  883. if (match)
  884. *match = m;
  885. break;
  886. }
  887. }
  888. of_node_put(from);
  889. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  890. return np;
  891. }
  892. EXPORT_SYMBOL(of_find_matching_node_and_match);
  893. /**
  894. * of_modalias_node - Lookup appropriate modalias for a device node
  895. * @node: pointer to a device tree node
  896. * @modalias: Pointer to buffer that modalias value will be copied into
  897. * @len: Length of modalias value
  898. *
  899. * Based on the value of the compatible property, this routine will attempt
  900. * to choose an appropriate modalias value for a particular device tree node.
  901. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  902. * from the first entry in the compatible list property.
  903. *
  904. * This routine returns 0 on success, <0 on failure.
  905. */
  906. int of_modalias_node(struct device_node *node, char *modalias, int len)
  907. {
  908. const char *compatible, *p;
  909. int cplen;
  910. compatible = of_get_property(node, "compatible", &cplen);
  911. if (!compatible || strlen(compatible) > cplen)
  912. return -ENODEV;
  913. p = strchr(compatible, ',');
  914. strlcpy(modalias, p ? p + 1 : compatible, len);
  915. return 0;
  916. }
  917. EXPORT_SYMBOL_GPL(of_modalias_node);
  918. /**
  919. * of_find_node_by_phandle - Find a node given a phandle
  920. * @handle: phandle of the node to find
  921. *
  922. * Returns a node pointer with refcount incremented, use
  923. * of_node_put() on it when done.
  924. */
  925. struct device_node *of_find_node_by_phandle(phandle handle)
  926. {
  927. struct device_node *np;
  928. unsigned long flags;
  929. raw_spin_lock_irqsave(&devtree_lock, flags);
  930. for (np = of_allnodes; np; np = np->allnext)
  931. if (np->phandle == handle)
  932. break;
  933. of_node_get(np);
  934. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  935. return np;
  936. }
  937. EXPORT_SYMBOL(of_find_node_by_phandle);
  938. /**
  939. * of_property_count_elems_of_size - Count the number of elements in a property
  940. *
  941. * @np: device node from which the property value is to be read.
  942. * @propname: name of the property to be searched.
  943. * @elem_size: size of the individual element
  944. *
  945. * Search for a property in a device node and count the number of elements of
  946. * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
  947. * property does not exist or its length does not match a multiple of elem_size
  948. * and -ENODATA if the property does not have a value.
  949. */
  950. int of_property_count_elems_of_size(const struct device_node *np,
  951. const char *propname, int elem_size)
  952. {
  953. struct property *prop = of_find_property(np, propname, NULL);
  954. if (!prop)
  955. return -EINVAL;
  956. if (!prop->value)
  957. return -ENODATA;
  958. if (prop->length % elem_size != 0) {
  959. pr_err("size of %s in node %s is not a multiple of %d\n",
  960. propname, np->full_name, elem_size);
  961. return -EINVAL;
  962. }
  963. return prop->length / elem_size;
  964. }
  965. EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
  966. /**
  967. * of_find_property_value_of_size
  968. *
  969. * @np: device node from which the property value is to be read.
  970. * @propname: name of the property to be searched.
  971. * @len: requested length of property value
  972. *
  973. * Search for a property in a device node and valid the requested size.
  974. * Returns the property value on success, -EINVAL if the property does not
  975. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  976. * property data isn't large enough.
  977. *
  978. */
  979. static void *of_find_property_value_of_size(const struct device_node *np,
  980. const char *propname, u32 len)
  981. {
  982. struct property *prop = of_find_property(np, propname, NULL);
  983. if (!prop)
  984. return ERR_PTR(-EINVAL);
  985. if (!prop->value)
  986. return ERR_PTR(-ENODATA);
  987. if (len > prop->length)
  988. return ERR_PTR(-EOVERFLOW);
  989. return prop->value;
  990. }
  991. /**
  992. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  993. *
  994. * @np: device node from which the property value is to be read.
  995. * @propname: name of the property to be searched.
  996. * @index: index of the u32 in the list of values
  997. * @out_value: pointer to return value, modified only if no error.
  998. *
  999. * Search for a property in a device node and read nth 32-bit value from
  1000. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1001. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1002. * property data isn't large enough.
  1003. *
  1004. * The out_value is modified only if a valid u32 value can be decoded.
  1005. */
  1006. int of_property_read_u32_index(const struct device_node *np,
  1007. const char *propname,
  1008. u32 index, u32 *out_value)
  1009. {
  1010. const u32 *val = of_find_property_value_of_size(np, propname,
  1011. ((index + 1) * sizeof(*out_value)));
  1012. if (IS_ERR(val))
  1013. return PTR_ERR(val);
  1014. *out_value = be32_to_cpup(((__be32 *)val) + index);
  1015. return 0;
  1016. }
  1017. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  1018. /**
  1019. * of_property_read_u8_array - Find and read an array of u8 from a property.
  1020. *
  1021. * @np: device node from which the property value is to be read.
  1022. * @propname: name of the property to be searched.
  1023. * @out_values: pointer to return value, modified only if return value is 0.
  1024. * @sz: number of array elements to read
  1025. *
  1026. * Search for a property in a device node and read 8-bit value(s) from
  1027. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1028. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1029. * property data isn't large enough.
  1030. *
  1031. * dts entry of array should be like:
  1032. * property = /bits/ 8 <0x50 0x60 0x70>;
  1033. *
  1034. * The out_values is modified only if a valid u8 value can be decoded.
  1035. */
  1036. int of_property_read_u8_array(const struct device_node *np,
  1037. const char *propname, u8 *out_values, size_t sz)
  1038. {
  1039. const u8 *val = of_find_property_value_of_size(np, propname,
  1040. (sz * sizeof(*out_values)));
  1041. if (IS_ERR(val))
  1042. return PTR_ERR(val);
  1043. while (sz--)
  1044. *out_values++ = *val++;
  1045. return 0;
  1046. }
  1047. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  1048. /**
  1049. * of_property_read_u16_array - Find and read an array of u16 from a property.
  1050. *
  1051. * @np: device node from which the property value is to be read.
  1052. * @propname: name of the property to be searched.
  1053. * @out_values: pointer to return value, modified only if return value is 0.
  1054. * @sz: number of array elements to read
  1055. *
  1056. * Search for a property in a device node and read 16-bit value(s) from
  1057. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1058. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1059. * property data isn't large enough.
  1060. *
  1061. * dts entry of array should be like:
  1062. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  1063. *
  1064. * The out_values is modified only if a valid u16 value can be decoded.
  1065. */
  1066. int of_property_read_u16_array(const struct device_node *np,
  1067. const char *propname, u16 *out_values, size_t sz)
  1068. {
  1069. const __be16 *val = of_find_property_value_of_size(np, propname,
  1070. (sz * sizeof(*out_values)));
  1071. if (IS_ERR(val))
  1072. return PTR_ERR(val);
  1073. while (sz--)
  1074. *out_values++ = be16_to_cpup(val++);
  1075. return 0;
  1076. }
  1077. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  1078. /**
  1079. * of_property_read_u32_array - Find and read an array of 32 bit integers
  1080. * from a property.
  1081. *
  1082. * @np: device node from which the property value is to be read.
  1083. * @propname: name of the property to be searched.
  1084. * @out_values: pointer to return value, modified only if return value is 0.
  1085. * @sz: number of array elements to read
  1086. *
  1087. * Search for a property in a device node and read 32-bit value(s) from
  1088. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1089. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1090. * property data isn't large enough.
  1091. *
  1092. * The out_values is modified only if a valid u32 value can be decoded.
  1093. */
  1094. int of_property_read_u32_array(const struct device_node *np,
  1095. const char *propname, u32 *out_values,
  1096. size_t sz)
  1097. {
  1098. const __be32 *val = of_find_property_value_of_size(np, propname,
  1099. (sz * sizeof(*out_values)));
  1100. if (IS_ERR(val))
  1101. return PTR_ERR(val);
  1102. while (sz--)
  1103. *out_values++ = be32_to_cpup(val++);
  1104. return 0;
  1105. }
  1106. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  1107. /**
  1108. * of_property_read_u64 - Find and read a 64 bit integer from a property
  1109. * @np: device node from which the property value is to be read.
  1110. * @propname: name of the property to be searched.
  1111. * @out_value: pointer to return value, modified only if return value is 0.
  1112. *
  1113. * Search for a property in a device node and read a 64-bit value from
  1114. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1115. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1116. * property data isn't large enough.
  1117. *
  1118. * The out_value is modified only if a valid u64 value can be decoded.
  1119. */
  1120. int of_property_read_u64(const struct device_node *np, const char *propname,
  1121. u64 *out_value)
  1122. {
  1123. const __be32 *val = of_find_property_value_of_size(np, propname,
  1124. sizeof(*out_value));
  1125. if (IS_ERR(val))
  1126. return PTR_ERR(val);
  1127. *out_value = of_read_number(val, 2);
  1128. return 0;
  1129. }
  1130. EXPORT_SYMBOL_GPL(of_property_read_u64);
  1131. /**
  1132. * of_property_read_string - Find and read a string from a property
  1133. * @np: device node from which the property value is to be read.
  1134. * @propname: name of the property to be searched.
  1135. * @out_string: pointer to null terminated return string, modified only if
  1136. * return value is 0.
  1137. *
  1138. * Search for a property in a device tree node and retrieve a null
  1139. * terminated string value (pointer to data, not a copy). Returns 0 on
  1140. * success, -EINVAL if the property does not exist, -ENODATA if property
  1141. * does not have a value, and -EILSEQ if the string is not null-terminated
  1142. * within the length of the property data.
  1143. *
  1144. * The out_string pointer is modified only if a valid string can be decoded.
  1145. */
  1146. int of_property_read_string(struct device_node *np, const char *propname,
  1147. const char **out_string)
  1148. {
  1149. struct property *prop = of_find_property(np, propname, NULL);
  1150. if (!prop)
  1151. return -EINVAL;
  1152. if (!prop->value)
  1153. return -ENODATA;
  1154. if (strnlen(prop->value, prop->length) >= prop->length)
  1155. return -EILSEQ;
  1156. *out_string = prop->value;
  1157. return 0;
  1158. }
  1159. EXPORT_SYMBOL_GPL(of_property_read_string);
  1160. /**
  1161. * of_property_read_string_index - Find and read a string from a multiple
  1162. * strings property.
  1163. * @np: device node from which the property value is to be read.
  1164. * @propname: name of the property to be searched.
  1165. * @index: index of the string in the list of strings
  1166. * @out_string: pointer to null terminated return string, modified only if
  1167. * return value is 0.
  1168. *
  1169. * Search for a property in a device tree node and retrieve a null
  1170. * terminated string value (pointer to data, not a copy) in the list of strings
  1171. * contained in that property.
  1172. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  1173. * property does not have a value, and -EILSEQ if the string is not
  1174. * null-terminated within the length of the property data.
  1175. *
  1176. * The out_string pointer is modified only if a valid string can be decoded.
  1177. */
  1178. int of_property_read_string_index(struct device_node *np, const char *propname,
  1179. int index, const char **output)
  1180. {
  1181. struct property *prop = of_find_property(np, propname, NULL);
  1182. int i = 0;
  1183. size_t l = 0, total = 0;
  1184. const char *p;
  1185. if (!prop)
  1186. return -EINVAL;
  1187. if (!prop->value)
  1188. return -ENODATA;
  1189. if (strnlen(prop->value, prop->length) >= prop->length)
  1190. return -EILSEQ;
  1191. p = prop->value;
  1192. for (i = 0; total < prop->length; total += l, p += l) {
  1193. l = strlen(p) + 1;
  1194. if (i++ == index) {
  1195. *output = p;
  1196. return 0;
  1197. }
  1198. }
  1199. return -ENODATA;
  1200. }
  1201. EXPORT_SYMBOL_GPL(of_property_read_string_index);
  1202. /**
  1203. * of_property_match_string() - Find string in a list and return index
  1204. * @np: pointer to node containing string list property
  1205. * @propname: string list property name
  1206. * @string: pointer to string to search for in string list
  1207. *
  1208. * This function searches a string list property and returns the index
  1209. * of a specific string value.
  1210. */
  1211. int of_property_match_string(struct device_node *np, const char *propname,
  1212. const char *string)
  1213. {
  1214. struct property *prop = of_find_property(np, propname, NULL);
  1215. size_t l;
  1216. int i;
  1217. const char *p, *end;
  1218. if (!prop)
  1219. return -EINVAL;
  1220. if (!prop->value)
  1221. return -ENODATA;
  1222. p = prop->value;
  1223. end = p + prop->length;
  1224. for (i = 0; p < end; i++, p += l) {
  1225. l = strlen(p) + 1;
  1226. if (p + l > end)
  1227. return -EILSEQ;
  1228. pr_debug("comparing %s with %s\n", string, p);
  1229. if (strcmp(string, p) == 0)
  1230. return i; /* Found it; return index */
  1231. }
  1232. return -ENODATA;
  1233. }
  1234. EXPORT_SYMBOL_GPL(of_property_match_string);
  1235. /**
  1236. * of_property_count_strings - Find and return the number of strings from a
  1237. * multiple strings property.
  1238. * @np: device node from which the property value is to be read.
  1239. * @propname: name of the property to be searched.
  1240. *
  1241. * Search for a property in a device tree node and retrieve the number of null
  1242. * terminated string contain in it. Returns the number of strings on
  1243. * success, -EINVAL if the property does not exist, -ENODATA if property
  1244. * does not have a value, and -EILSEQ if the string is not null-terminated
  1245. * within the length of the property data.
  1246. */
  1247. int of_property_count_strings(struct device_node *np, const char *propname)
  1248. {
  1249. struct property *prop = of_find_property(np, propname, NULL);
  1250. int i = 0;
  1251. size_t l = 0, total = 0;
  1252. const char *p;
  1253. if (!prop)
  1254. return -EINVAL;
  1255. if (!prop->value)
  1256. return -ENODATA;
  1257. if (strnlen(prop->value, prop->length) >= prop->length)
  1258. return -EILSEQ;
  1259. p = prop->value;
  1260. for (i = 0; total < prop->length; total += l, p += l, i++)
  1261. l = strlen(p) + 1;
  1262. return i;
  1263. }
  1264. EXPORT_SYMBOL_GPL(of_property_count_strings);
  1265. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1266. {
  1267. int i;
  1268. printk("%s %s", msg, of_node_full_name(args->np));
  1269. for (i = 0; i < args->args_count; i++)
  1270. printk(i ? ",%08x" : ":%08x", args->args[i]);
  1271. printk("\n");
  1272. }
  1273. static int __of_parse_phandle_with_args(const struct device_node *np,
  1274. const char *list_name,
  1275. const char *cells_name,
  1276. int cell_count, int index,
  1277. struct of_phandle_args *out_args)
  1278. {
  1279. const __be32 *list, *list_end;
  1280. int rc = 0, size, cur_index = 0;
  1281. uint32_t count = 0;
  1282. struct device_node *node = NULL;
  1283. phandle phandle;
  1284. /* Retrieve the phandle list property */
  1285. list = of_get_property(np, list_name, &size);
  1286. if (!list)
  1287. return -ENOENT;
  1288. list_end = list + size / sizeof(*list);
  1289. /* Loop over the phandles until all the requested entry is found */
  1290. while (list < list_end) {
  1291. rc = -EINVAL;
  1292. count = 0;
  1293. /*
  1294. * If phandle is 0, then it is an empty entry with no
  1295. * arguments. Skip forward to the next entry.
  1296. */
  1297. phandle = be32_to_cpup(list++);
  1298. if (phandle) {
  1299. /*
  1300. * Find the provider node and parse the #*-cells
  1301. * property to determine the argument length.
  1302. *
  1303. * This is not needed if the cell count is hard-coded
  1304. * (i.e. cells_name not set, but cell_count is set),
  1305. * except when we're going to return the found node
  1306. * below.
  1307. */
  1308. if (cells_name || cur_index == index) {
  1309. node = of_find_node_by_phandle(phandle);
  1310. if (!node) {
  1311. pr_err("%s: could not find phandle\n",
  1312. np->full_name);
  1313. goto err;
  1314. }
  1315. }
  1316. if (cells_name) {
  1317. if (of_property_read_u32(node, cells_name,
  1318. &count)) {
  1319. pr_err("%s: could not get %s for %s\n",
  1320. np->full_name, cells_name,
  1321. node->full_name);
  1322. goto err;
  1323. }
  1324. } else {
  1325. count = cell_count;
  1326. }
  1327. /*
  1328. * Make sure that the arguments actually fit in the
  1329. * remaining property data length
  1330. */
  1331. if (list + count > list_end) {
  1332. pr_err("%s: arguments longer than property\n",
  1333. np->full_name);
  1334. goto err;
  1335. }
  1336. }
  1337. /*
  1338. * All of the error cases above bail out of the loop, so at
  1339. * this point, the parsing is successful. If the requested
  1340. * index matches, then fill the out_args structure and return,
  1341. * or return -ENOENT for an empty entry.
  1342. */
  1343. rc = -ENOENT;
  1344. if (cur_index == index) {
  1345. if (!phandle)
  1346. goto err;
  1347. if (out_args) {
  1348. int i;
  1349. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  1350. count = MAX_PHANDLE_ARGS;
  1351. out_args->np = node;
  1352. out_args->args_count = count;
  1353. for (i = 0; i < count; i++)
  1354. out_args->args[i] = be32_to_cpup(list++);
  1355. } else {
  1356. of_node_put(node);
  1357. }
  1358. /* Found it! return success */
  1359. return 0;
  1360. }
  1361. of_node_put(node);
  1362. node = NULL;
  1363. list += count;
  1364. cur_index++;
  1365. }
  1366. /*
  1367. * Unlock node before returning result; will be one of:
  1368. * -ENOENT : index is for empty phandle
  1369. * -EINVAL : parsing error on data
  1370. * [1..n] : Number of phandle (count mode; when index = -1)
  1371. */
  1372. rc = index < 0 ? cur_index : -ENOENT;
  1373. err:
  1374. if (node)
  1375. of_node_put(node);
  1376. return rc;
  1377. }
  1378. /**
  1379. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1380. * @np: Pointer to device node holding phandle property
  1381. * @phandle_name: Name of property holding a phandle value
  1382. * @index: For properties holding a table of phandles, this is the index into
  1383. * the table
  1384. *
  1385. * Returns the device_node pointer with refcount incremented. Use
  1386. * of_node_put() on it when done.
  1387. */
  1388. struct device_node *of_parse_phandle(const struct device_node *np,
  1389. const char *phandle_name, int index)
  1390. {
  1391. struct of_phandle_args args;
  1392. if (index < 0)
  1393. return NULL;
  1394. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1395. index, &args))
  1396. return NULL;
  1397. return args.np;
  1398. }
  1399. EXPORT_SYMBOL(of_parse_phandle);
  1400. /**
  1401. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1402. * @np: pointer to a device tree node containing a list
  1403. * @list_name: property name that contains a list
  1404. * @cells_name: property name that specifies phandles' arguments count
  1405. * @index: index of a phandle to parse out
  1406. * @out_args: optional pointer to output arguments structure (will be filled)
  1407. *
  1408. * This function is useful to parse lists of phandles and their arguments.
  1409. * Returns 0 on success and fills out_args, on error returns appropriate
  1410. * errno value.
  1411. *
  1412. * Caller is responsible to call of_node_put() on the returned out_args->node
  1413. * pointer.
  1414. *
  1415. * Example:
  1416. *
  1417. * phandle1: node1 {
  1418. * #list-cells = <2>;
  1419. * }
  1420. *
  1421. * phandle2: node2 {
  1422. * #list-cells = <1>;
  1423. * }
  1424. *
  1425. * node3 {
  1426. * list = <&phandle1 1 2 &phandle2 3>;
  1427. * }
  1428. *
  1429. * To get a device_node of the `node2' node you may call this:
  1430. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1431. */
  1432. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1433. const char *cells_name, int index,
  1434. struct of_phandle_args *out_args)
  1435. {
  1436. if (index < 0)
  1437. return -EINVAL;
  1438. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1439. index, out_args);
  1440. }
  1441. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1442. /**
  1443. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1444. * @np: pointer to a device tree node containing a list
  1445. * @list_name: property name that contains a list
  1446. * @cell_count: number of argument cells following the phandle
  1447. * @index: index of a phandle to parse out
  1448. * @out_args: optional pointer to output arguments structure (will be filled)
  1449. *
  1450. * This function is useful to parse lists of phandles and their arguments.
  1451. * Returns 0 on success and fills out_args, on error returns appropriate
  1452. * errno value.
  1453. *
  1454. * Caller is responsible to call of_node_put() on the returned out_args->node
  1455. * pointer.
  1456. *
  1457. * Example:
  1458. *
  1459. * phandle1: node1 {
  1460. * }
  1461. *
  1462. * phandle2: node2 {
  1463. * }
  1464. *
  1465. * node3 {
  1466. * list = <&phandle1 0 2 &phandle2 2 3>;
  1467. * }
  1468. *
  1469. * To get a device_node of the `node2' node you may call this:
  1470. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1471. */
  1472. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1473. const char *list_name, int cell_count,
  1474. int index, struct of_phandle_args *out_args)
  1475. {
  1476. if (index < 0)
  1477. return -EINVAL;
  1478. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1479. index, out_args);
  1480. }
  1481. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1482. /**
  1483. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1484. * @np: pointer to a device tree node containing a list
  1485. * @list_name: property name that contains a list
  1486. * @cells_name: property name that specifies phandles' arguments count
  1487. *
  1488. * Returns the number of phandle + argument tuples within a property. It
  1489. * is a typical pattern to encode a list of phandle and variable
  1490. * arguments into a single property. The number of arguments is encoded
  1491. * by a property in the phandle-target node. For example, a gpios
  1492. * property would contain a list of GPIO specifies consisting of a
  1493. * phandle and 1 or more arguments. The number of arguments are
  1494. * determined by the #gpio-cells property in the node pointed to by the
  1495. * phandle.
  1496. */
  1497. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1498. const char *cells_name)
  1499. {
  1500. return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
  1501. NULL);
  1502. }
  1503. EXPORT_SYMBOL(of_count_phandle_with_args);
  1504. #if defined(CONFIG_OF_DYNAMIC)
  1505. static int of_property_notify(int action, struct device_node *np,
  1506. struct property *prop)
  1507. {
  1508. struct of_prop_reconfig pr;
  1509. /* only call notifiers if the node is attached */
  1510. if (!of_node_is_attached(np))
  1511. return 0;
  1512. pr.dn = np;
  1513. pr.prop = prop;
  1514. return of_reconfig_notify(action, &pr);
  1515. }
  1516. #else
  1517. static int of_property_notify(int action, struct device_node *np,
  1518. struct property *prop)
  1519. {
  1520. return 0;
  1521. }
  1522. #endif
  1523. /**
  1524. * __of_add_property - Add a property to a node without lock operations
  1525. */
  1526. static int __of_add_property(struct device_node *np, struct property *prop)
  1527. {
  1528. struct property **next;
  1529. prop->next = NULL;
  1530. next = &np->properties;
  1531. while (*next) {
  1532. if (strcmp(prop->name, (*next)->name) == 0)
  1533. /* duplicate ! don't insert it */
  1534. return -EEXIST;
  1535. next = &(*next)->next;
  1536. }
  1537. *next = prop;
  1538. return 0;
  1539. }
  1540. /**
  1541. * of_add_property - Add a property to a node
  1542. */
  1543. int of_add_property(struct device_node *np, struct property *prop)
  1544. {
  1545. unsigned long flags;
  1546. int rc;
  1547. rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
  1548. if (rc)
  1549. return rc;
  1550. raw_spin_lock_irqsave(&devtree_lock, flags);
  1551. rc = __of_add_property(np, prop);
  1552. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1553. if (rc)
  1554. return rc;
  1555. if (of_node_is_attached(np))
  1556. __of_add_property_sysfs(np, prop);
  1557. return rc;
  1558. }
  1559. /**
  1560. * of_remove_property - Remove a property from a node.
  1561. *
  1562. * Note that we don't actually remove it, since we have given out
  1563. * who-knows-how-many pointers to the data using get-property.
  1564. * Instead we just move the property to the "dead properties"
  1565. * list, so it won't be found any more.
  1566. */
  1567. int of_remove_property(struct device_node *np, struct property *prop)
  1568. {
  1569. struct property **next;
  1570. unsigned long flags;
  1571. int found = 0;
  1572. int rc;
  1573. rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  1574. if (rc)
  1575. return rc;
  1576. raw_spin_lock_irqsave(&devtree_lock, flags);
  1577. next = &np->properties;
  1578. while (*next) {
  1579. if (*next == prop) {
  1580. /* found the node */
  1581. *next = prop->next;
  1582. prop->next = np->deadprops;
  1583. np->deadprops = prop;
  1584. found = 1;
  1585. break;
  1586. }
  1587. next = &(*next)->next;
  1588. }
  1589. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1590. if (!found)
  1591. return -ENODEV;
  1592. /* at early boot, bail hear and defer setup to of_init() */
  1593. if (!of_kset)
  1594. return 0;
  1595. sysfs_remove_bin_file(&np->kobj, &prop->attr);
  1596. return 0;
  1597. }
  1598. /*
  1599. * of_update_property - Update a property in a node, if the property does
  1600. * not exist, add it.
  1601. *
  1602. * Note that we don't actually remove it, since we have given out
  1603. * who-knows-how-many pointers to the data using get-property.
  1604. * Instead we just move the property to the "dead properties" list,
  1605. * and add the new property to the property list
  1606. */
  1607. int of_update_property(struct device_node *np, struct property *newprop)
  1608. {
  1609. struct property **next, *oldprop;
  1610. unsigned long flags;
  1611. int rc, found = 0;
  1612. rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
  1613. if (rc)
  1614. return rc;
  1615. if (!newprop->name)
  1616. return -EINVAL;
  1617. oldprop = of_find_property(np, newprop->name, NULL);
  1618. if (!oldprop)
  1619. return of_add_property(np, newprop);
  1620. raw_spin_lock_irqsave(&devtree_lock, flags);
  1621. next = &np->properties;
  1622. while (*next) {
  1623. if (*next == oldprop) {
  1624. /* found the node */
  1625. newprop->next = oldprop->next;
  1626. *next = newprop;
  1627. oldprop->next = np->deadprops;
  1628. np->deadprops = oldprop;
  1629. found = 1;
  1630. break;
  1631. }
  1632. next = &(*next)->next;
  1633. }
  1634. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1635. if (!found)
  1636. return -ENODEV;
  1637. /* At early boot, bail out and defer setup to of_init() */
  1638. if (!of_kset)
  1639. return found ? 0 : -ENODEV;
  1640. /* Update the sysfs attribute */
  1641. sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
  1642. __of_add_property_sysfs(np, newprop);
  1643. return 0;
  1644. }
  1645. #if defined(CONFIG_OF_DYNAMIC)
  1646. /*
  1647. * Support for dynamic device trees.
  1648. *
  1649. * On some platforms, the device tree can be manipulated at runtime.
  1650. * The routines in this section support adding, removing and changing
  1651. * device tree nodes.
  1652. */
  1653. static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
  1654. int of_reconfig_notifier_register(struct notifier_block *nb)
  1655. {
  1656. return blocking_notifier_chain_register(&of_reconfig_chain, nb);
  1657. }
  1658. EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
  1659. int of_reconfig_notifier_unregister(struct notifier_block *nb)
  1660. {
  1661. return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
  1662. }
  1663. EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
  1664. int of_reconfig_notify(unsigned long action, void *p)
  1665. {
  1666. int rc;
  1667. rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
  1668. return notifier_to_errno(rc);
  1669. }
  1670. /**
  1671. * of_attach_node - Plug a device node into the tree and global list.
  1672. */
  1673. int of_attach_node(struct device_node *np)
  1674. {
  1675. unsigned long flags;
  1676. int rc;
  1677. rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
  1678. if (rc)
  1679. return rc;
  1680. raw_spin_lock_irqsave(&devtree_lock, flags);
  1681. np->sibling = np->parent->child;
  1682. np->allnext = of_allnodes;
  1683. np->parent->child = np;
  1684. of_allnodes = np;
  1685. of_node_clear_flag(np, OF_DETACHED);
  1686. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1687. of_node_add(np);
  1688. return 0;
  1689. }
  1690. /**
  1691. * of_detach_node - "Unplug" a node from the device tree.
  1692. *
  1693. * The caller must hold a reference to the node. The memory associated with
  1694. * the node is not freed until its refcount goes to zero.
  1695. */
  1696. int of_detach_node(struct device_node *np)
  1697. {
  1698. struct device_node *parent;
  1699. unsigned long flags;
  1700. int rc = 0;
  1701. rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
  1702. if (rc)
  1703. return rc;
  1704. raw_spin_lock_irqsave(&devtree_lock, flags);
  1705. if (of_node_check_flag(np, OF_DETACHED)) {
  1706. /* someone already detached it */
  1707. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1708. return rc;
  1709. }
  1710. parent = np->parent;
  1711. if (!parent) {
  1712. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1713. return rc;
  1714. }
  1715. if (of_allnodes == np)
  1716. of_allnodes = np->allnext;
  1717. else {
  1718. struct device_node *prev;
  1719. for (prev = of_allnodes;
  1720. prev->allnext != np;
  1721. prev = prev->allnext)
  1722. ;
  1723. prev->allnext = np->allnext;
  1724. }
  1725. if (parent->child == np)
  1726. parent->child = np->sibling;
  1727. else {
  1728. struct device_node *prevsib;
  1729. for (prevsib = np->parent->child;
  1730. prevsib->sibling != np;
  1731. prevsib = prevsib->sibling)
  1732. ;
  1733. prevsib->sibling = np->sibling;
  1734. }
  1735. of_node_set_flag(np, OF_DETACHED);
  1736. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1737. of_node_remove(np);
  1738. return rc;
  1739. }
  1740. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1741. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1742. int id, const char *stem, int stem_len)
  1743. {
  1744. ap->np = np;
  1745. ap->id = id;
  1746. strncpy(ap->stem, stem, stem_len);
  1747. ap->stem[stem_len] = 0;
  1748. list_add_tail(&ap->link, &aliases_lookup);
  1749. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1750. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1751. }
  1752. /**
  1753. * of_alias_scan - Scan all properties of 'aliases' node
  1754. *
  1755. * The function scans all the properties of 'aliases' node and populate
  1756. * the the global lookup table with the properties. It returns the
  1757. * number of alias_prop found, or error code in error case.
  1758. *
  1759. * @dt_alloc: An allocator that provides a virtual address to memory
  1760. * for the resulting tree
  1761. */
  1762. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1763. {
  1764. struct property *pp;
  1765. of_chosen = of_find_node_by_path("/chosen");
  1766. if (of_chosen == NULL)
  1767. of_chosen = of_find_node_by_path("/chosen@0");
  1768. if (of_chosen) {
  1769. const char *name = of_get_property(of_chosen, "stdout-path", NULL);
  1770. if (!name)
  1771. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  1772. if (name)
  1773. of_stdout = of_find_node_by_path(name);
  1774. }
  1775. of_aliases = of_find_node_by_path("/aliases");
  1776. if (!of_aliases)
  1777. return;
  1778. for_each_property_of_node(of_aliases, pp) {
  1779. const char *start = pp->name;
  1780. const char *end = start + strlen(start);
  1781. struct device_node *np;
  1782. struct alias_prop *ap;
  1783. int id, len;
  1784. /* Skip those we do not want to proceed */
  1785. if (!strcmp(pp->name, "name") ||
  1786. !strcmp(pp->name, "phandle") ||
  1787. !strcmp(pp->name, "linux,phandle"))
  1788. continue;
  1789. np = of_find_node_by_path(pp->value);
  1790. if (!np)
  1791. continue;
  1792. /* walk the alias backwards to extract the id and work out
  1793. * the 'stem' string */
  1794. while (isdigit(*(end-1)) && end > start)
  1795. end--;
  1796. len = end - start;
  1797. if (kstrtoint(end, 10, &id) < 0)
  1798. continue;
  1799. /* Allocate an alias_prop with enough space for the stem */
  1800. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1801. if (!ap)
  1802. continue;
  1803. memset(ap, 0, sizeof(*ap) + len + 1);
  1804. ap->alias = start;
  1805. of_alias_add(ap, np, id, start, len);
  1806. }
  1807. }
  1808. /**
  1809. * of_alias_get_id - Get alias id for the given device_node
  1810. * @np: Pointer to the given device_node
  1811. * @stem: Alias stem of the given device_node
  1812. *
  1813. * The function travels the lookup table to get alias id for the given
  1814. * device_node and alias stem. It returns the alias id if find it.
  1815. */
  1816. int of_alias_get_id(struct device_node *np, const char *stem)
  1817. {
  1818. struct alias_prop *app;
  1819. int id = -ENODEV;
  1820. mutex_lock(&of_aliases_mutex);
  1821. list_for_each_entry(app, &aliases_lookup, link) {
  1822. if (strcmp(app->stem, stem) != 0)
  1823. continue;
  1824. if (np == app->np) {
  1825. id = app->id;
  1826. break;
  1827. }
  1828. }
  1829. mutex_unlock(&of_aliases_mutex);
  1830. return id;
  1831. }
  1832. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1833. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1834. u32 *pu)
  1835. {
  1836. const void *curv = cur;
  1837. if (!prop)
  1838. return NULL;
  1839. if (!cur) {
  1840. curv = prop->value;
  1841. goto out_val;
  1842. }
  1843. curv += sizeof(*cur);
  1844. if (curv >= prop->value + prop->length)
  1845. return NULL;
  1846. out_val:
  1847. *pu = be32_to_cpup(curv);
  1848. return curv;
  1849. }
  1850. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1851. const char *of_prop_next_string(struct property *prop, const char *cur)
  1852. {
  1853. const void *curv = cur;
  1854. if (!prop)
  1855. return NULL;
  1856. if (!cur)
  1857. return prop->value;
  1858. curv += strlen(cur) + 1;
  1859. if (curv >= prop->value + prop->length)
  1860. return NULL;
  1861. return curv;
  1862. }
  1863. EXPORT_SYMBOL_GPL(of_prop_next_string);
  1864. /**
  1865. * of_device_is_stdout_path - check if a device node matches the
  1866. * linux,stdout-path property
  1867. *
  1868. * Check if this device node matches the linux,stdout-path property
  1869. * in the chosen node. return true if yes, false otherwise.
  1870. */
  1871. int of_device_is_stdout_path(struct device_node *dn)
  1872. {
  1873. if (!of_stdout)
  1874. return false;
  1875. return of_stdout == dn;
  1876. }
  1877. EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
  1878. /**
  1879. * of_find_next_cache_node - Find a node's subsidiary cache
  1880. * @np: node of type "cpu" or "cache"
  1881. *
  1882. * Returns a node pointer with refcount incremented, use
  1883. * of_node_put() on it when done. Caller should hold a reference
  1884. * to np.
  1885. */
  1886. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1887. {
  1888. struct device_node *child;
  1889. const phandle *handle;
  1890. handle = of_get_property(np, "l2-cache", NULL);
  1891. if (!handle)
  1892. handle = of_get_property(np, "next-level-cache", NULL);
  1893. if (handle)
  1894. return of_find_node_by_phandle(be32_to_cpup(handle));
  1895. /* OF on pmac has nodes instead of properties named "l2-cache"
  1896. * beneath CPU nodes.
  1897. */
  1898. if (!strcmp(np->type, "cpu"))
  1899. for_each_child_of_node(np, child)
  1900. if (!strcmp(child->type, "cache"))
  1901. return child;
  1902. return NULL;
  1903. }
  1904. /**
  1905. * of_graph_parse_endpoint() - parse common endpoint node properties
  1906. * @node: pointer to endpoint device_node
  1907. * @endpoint: pointer to the OF endpoint data structure
  1908. *
  1909. * The caller should hold a reference to @node.
  1910. */
  1911. int of_graph_parse_endpoint(const struct device_node *node,
  1912. struct of_endpoint *endpoint)
  1913. {
  1914. struct device_node *port_node = of_get_parent(node);
  1915. WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
  1916. __func__, node->full_name);
  1917. memset(endpoint, 0, sizeof(*endpoint));
  1918. endpoint->local_node = node;
  1919. /*
  1920. * It doesn't matter whether the two calls below succeed.
  1921. * If they don't then the default value 0 is used.
  1922. */
  1923. of_property_read_u32(port_node, "reg", &endpoint->port);
  1924. of_property_read_u32(node, "reg", &endpoint->id);
  1925. of_node_put(port_node);
  1926. return 0;
  1927. }
  1928. EXPORT_SYMBOL(of_graph_parse_endpoint);
  1929. /**
  1930. * of_graph_get_next_endpoint() - get next endpoint node
  1931. * @parent: pointer to the parent device node
  1932. * @prev: previous endpoint node, or NULL to get first
  1933. *
  1934. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  1935. * of the passed @prev node is not decremented, the caller have to use
  1936. * of_node_put() on it when done.
  1937. */
  1938. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  1939. struct device_node *prev)
  1940. {
  1941. struct device_node *endpoint;
  1942. struct device_node *port;
  1943. if (!parent)
  1944. return NULL;
  1945. /*
  1946. * Start by locating the port node. If no previous endpoint is specified
  1947. * search for the first port node, otherwise get the previous endpoint
  1948. * parent port node.
  1949. */
  1950. if (!prev) {
  1951. struct device_node *node;
  1952. node = of_get_child_by_name(parent, "ports");
  1953. if (node)
  1954. parent = node;
  1955. port = of_get_child_by_name(parent, "port");
  1956. of_node_put(node);
  1957. if (!port) {
  1958. pr_err("%s(): no port node found in %s\n",
  1959. __func__, parent->full_name);
  1960. return NULL;
  1961. }
  1962. } else {
  1963. port = of_get_parent(prev);
  1964. if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
  1965. __func__, prev->full_name))
  1966. return NULL;
  1967. /*
  1968. * Avoid dropping prev node refcount to 0 when getting the next
  1969. * child below.
  1970. */
  1971. of_node_get(prev);
  1972. }
  1973. while (1) {
  1974. /*
  1975. * Now that we have a port node, get the next endpoint by
  1976. * getting the next child. If the previous endpoint is NULL this
  1977. * will return the first child.
  1978. */
  1979. endpoint = of_get_next_child(port, prev);
  1980. if (endpoint) {
  1981. of_node_put(port);
  1982. return endpoint;
  1983. }
  1984. /* No more endpoints under this port, try the next one. */
  1985. prev = NULL;
  1986. do {
  1987. port = of_get_next_child(parent, port);
  1988. if (!port)
  1989. return NULL;
  1990. } while (of_node_cmp(port->name, "port"));
  1991. }
  1992. }
  1993. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  1994. /**
  1995. * of_graph_get_remote_port_parent() - get remote port's parent node
  1996. * @node: pointer to a local endpoint device_node
  1997. *
  1998. * Return: Remote device node associated with remote endpoint node linked
  1999. * to @node. Use of_node_put() on it when done.
  2000. */
  2001. struct device_node *of_graph_get_remote_port_parent(
  2002. const struct device_node *node)
  2003. {
  2004. struct device_node *np;
  2005. unsigned int depth;
  2006. /* Get remote endpoint node. */
  2007. np = of_parse_phandle(node, "remote-endpoint", 0);
  2008. /* Walk 3 levels up only if there is 'ports' node. */
  2009. for (depth = 3; depth && np; depth--) {
  2010. np = of_get_next_parent(np);
  2011. if (depth == 2 && of_node_cmp(np->name, "ports"))
  2012. break;
  2013. }
  2014. return np;
  2015. }
  2016. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  2017. /**
  2018. * of_graph_get_remote_port() - get remote port node
  2019. * @node: pointer to a local endpoint device_node
  2020. *
  2021. * Return: Remote port node associated with remote endpoint node linked
  2022. * to @node. Use of_node_put() on it when done.
  2023. */
  2024. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  2025. {
  2026. struct device_node *np;
  2027. /* Get remote endpoint node. */
  2028. np = of_parse_phandle(node, "remote-endpoint", 0);
  2029. if (!np)
  2030. return NULL;
  2031. return of_get_next_parent(np);
  2032. }
  2033. EXPORT_SYMBOL(of_graph_get_remote_port);