base.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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/module.h>
  22. #include <linux/of.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/slab.h>
  25. #include <linux/proc_fs.h>
  26. #include "of_private.h"
  27. LIST_HEAD(aliases_lookup);
  28. struct device_node *allnodes;
  29. struct device_node *of_chosen;
  30. struct device_node *of_aliases;
  31. DEFINE_MUTEX(of_aliases_mutex);
  32. /* use when traversing tree through the allnext, child, sibling,
  33. * or parent members of struct device_node.
  34. */
  35. DEFINE_RWLOCK(devtree_lock);
  36. int of_n_addr_cells(struct device_node *np)
  37. {
  38. const __be32 *ip;
  39. do {
  40. if (np->parent)
  41. np = np->parent;
  42. ip = of_get_property(np, "#address-cells", NULL);
  43. if (ip)
  44. return be32_to_cpup(ip);
  45. } while (np->parent);
  46. /* No #address-cells property for the root node */
  47. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  48. }
  49. EXPORT_SYMBOL(of_n_addr_cells);
  50. int of_n_size_cells(struct device_node *np)
  51. {
  52. const __be32 *ip;
  53. do {
  54. if (np->parent)
  55. np = np->parent;
  56. ip = of_get_property(np, "#size-cells", NULL);
  57. if (ip)
  58. return be32_to_cpup(ip);
  59. } while (np->parent);
  60. /* No #size-cells property for the root node */
  61. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  62. }
  63. EXPORT_SYMBOL(of_n_size_cells);
  64. #if defined(CONFIG_OF_DYNAMIC)
  65. /**
  66. * of_node_get - Increment refcount of a node
  67. * @node: Node to inc refcount, NULL is supported to
  68. * simplify writing of callers
  69. *
  70. * Returns node.
  71. */
  72. struct device_node *of_node_get(struct device_node *node)
  73. {
  74. if (node)
  75. kref_get(&node->kref);
  76. return node;
  77. }
  78. EXPORT_SYMBOL(of_node_get);
  79. static inline struct device_node *kref_to_device_node(struct kref *kref)
  80. {
  81. return container_of(kref, struct device_node, kref);
  82. }
  83. /**
  84. * of_node_release - release a dynamically allocated node
  85. * @kref: kref element of the node to be released
  86. *
  87. * In of_node_put() this function is passed to kref_put()
  88. * as the destructor.
  89. */
  90. static void of_node_release(struct kref *kref)
  91. {
  92. struct device_node *node = kref_to_device_node(kref);
  93. struct property *prop = node->properties;
  94. /* We should never be releasing nodes that haven't been detached. */
  95. if (!of_node_check_flag(node, OF_DETACHED)) {
  96. pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
  97. dump_stack();
  98. kref_init(&node->kref);
  99. return;
  100. }
  101. if (!of_node_check_flag(node, OF_DYNAMIC))
  102. return;
  103. while (prop) {
  104. struct property *next = prop->next;
  105. kfree(prop->name);
  106. kfree(prop->value);
  107. kfree(prop);
  108. prop = next;
  109. if (!prop) {
  110. prop = node->deadprops;
  111. node->deadprops = NULL;
  112. }
  113. }
  114. kfree(node->full_name);
  115. kfree(node->data);
  116. kfree(node);
  117. }
  118. /**
  119. * of_node_put - Decrement refcount of a node
  120. * @node: Node to dec refcount, NULL is supported to
  121. * simplify writing of callers
  122. *
  123. */
  124. void of_node_put(struct device_node *node)
  125. {
  126. if (node)
  127. kref_put(&node->kref, of_node_release);
  128. }
  129. EXPORT_SYMBOL(of_node_put);
  130. #endif /* CONFIG_OF_DYNAMIC */
  131. struct property *of_find_property(const struct device_node *np,
  132. const char *name,
  133. int *lenp)
  134. {
  135. struct property *pp;
  136. if (!np)
  137. return NULL;
  138. read_lock(&devtree_lock);
  139. for (pp = np->properties; pp != 0; pp = pp->next) {
  140. if (of_prop_cmp(pp->name, name) == 0) {
  141. if (lenp != 0)
  142. *lenp = pp->length;
  143. break;
  144. }
  145. }
  146. read_unlock(&devtree_lock);
  147. return pp;
  148. }
  149. EXPORT_SYMBOL(of_find_property);
  150. /**
  151. * of_find_all_nodes - Get next node in global list
  152. * @prev: Previous node or NULL to start iteration
  153. * of_node_put() will be called on it
  154. *
  155. * Returns a node pointer with refcount incremented, use
  156. * of_node_put() on it when done.
  157. */
  158. struct device_node *of_find_all_nodes(struct device_node *prev)
  159. {
  160. struct device_node *np;
  161. read_lock(&devtree_lock);
  162. np = prev ? prev->allnext : allnodes;
  163. for (; np != NULL; np = np->allnext)
  164. if (of_node_get(np))
  165. break;
  166. of_node_put(prev);
  167. read_unlock(&devtree_lock);
  168. return np;
  169. }
  170. EXPORT_SYMBOL(of_find_all_nodes);
  171. /*
  172. * Find a property with a given name for a given node
  173. * and return the value.
  174. */
  175. const void *of_get_property(const struct device_node *np, const char *name,
  176. int *lenp)
  177. {
  178. struct property *pp = of_find_property(np, name, lenp);
  179. return pp ? pp->value : NULL;
  180. }
  181. EXPORT_SYMBOL(of_get_property);
  182. /** Checks if the given "compat" string matches one of the strings in
  183. * the device's "compatible" property
  184. */
  185. int of_device_is_compatible(const struct device_node *device,
  186. const char *compat)
  187. {
  188. const char* cp;
  189. int cplen, l;
  190. cp = of_get_property(device, "compatible", &cplen);
  191. if (cp == NULL)
  192. return 0;
  193. while (cplen > 0) {
  194. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  195. return 1;
  196. l = strlen(cp) + 1;
  197. cp += l;
  198. cplen -= l;
  199. }
  200. return 0;
  201. }
  202. EXPORT_SYMBOL(of_device_is_compatible);
  203. /**
  204. * of_machine_is_compatible - Test root of device tree for a given compatible value
  205. * @compat: compatible string to look for in root node's compatible property.
  206. *
  207. * Returns true if the root node has the given value in its
  208. * compatible property.
  209. */
  210. int of_machine_is_compatible(const char *compat)
  211. {
  212. struct device_node *root;
  213. int rc = 0;
  214. root = of_find_node_by_path("/");
  215. if (root) {
  216. rc = of_device_is_compatible(root, compat);
  217. of_node_put(root);
  218. }
  219. return rc;
  220. }
  221. EXPORT_SYMBOL(of_machine_is_compatible);
  222. /**
  223. * of_device_is_available - check if a device is available for use
  224. *
  225. * @device: Node to check for availability
  226. *
  227. * Returns 1 if the status property is absent or set to "okay" or "ok",
  228. * 0 otherwise
  229. */
  230. int of_device_is_available(const struct device_node *device)
  231. {
  232. const char *status;
  233. int statlen;
  234. status = of_get_property(device, "status", &statlen);
  235. if (status == NULL)
  236. return 1;
  237. if (statlen > 0) {
  238. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. EXPORT_SYMBOL(of_device_is_available);
  244. /**
  245. * of_get_parent - Get a node's parent if any
  246. * @node: Node to get parent
  247. *
  248. * Returns a node pointer with refcount incremented, use
  249. * of_node_put() on it when done.
  250. */
  251. struct device_node *of_get_parent(const struct device_node *node)
  252. {
  253. struct device_node *np;
  254. if (!node)
  255. return NULL;
  256. read_lock(&devtree_lock);
  257. np = of_node_get(node->parent);
  258. read_unlock(&devtree_lock);
  259. return np;
  260. }
  261. EXPORT_SYMBOL(of_get_parent);
  262. /**
  263. * of_get_next_parent - Iterate to a node's parent
  264. * @node: Node to get parent of
  265. *
  266. * This is like of_get_parent() except that it drops the
  267. * refcount on the passed node, making it suitable for iterating
  268. * through a node's parents.
  269. *
  270. * Returns a node pointer with refcount incremented, use
  271. * of_node_put() on it when done.
  272. */
  273. struct device_node *of_get_next_parent(struct device_node *node)
  274. {
  275. struct device_node *parent;
  276. if (!node)
  277. return NULL;
  278. read_lock(&devtree_lock);
  279. parent = of_node_get(node->parent);
  280. of_node_put(node);
  281. read_unlock(&devtree_lock);
  282. return parent;
  283. }
  284. /**
  285. * of_get_next_child - Iterate a node childs
  286. * @node: parent node
  287. * @prev: previous child of the parent node, or NULL to get first
  288. *
  289. * Returns a node pointer with refcount incremented, use
  290. * of_node_put() on it when done.
  291. */
  292. struct device_node *of_get_next_child(const struct device_node *node,
  293. struct device_node *prev)
  294. {
  295. struct device_node *next;
  296. read_lock(&devtree_lock);
  297. next = prev ? prev->sibling : node->child;
  298. for (; next; next = next->sibling)
  299. if (of_node_get(next))
  300. break;
  301. of_node_put(prev);
  302. read_unlock(&devtree_lock);
  303. return next;
  304. }
  305. EXPORT_SYMBOL(of_get_next_child);
  306. /**
  307. * of_get_next_available_child - Find the next available child node
  308. * @node: parent node
  309. * @prev: previous child of the parent node, or NULL to get first
  310. *
  311. * This function is like of_get_next_child(), except that it
  312. * automatically skips any disabled nodes (i.e. status = "disabled").
  313. */
  314. struct device_node *of_get_next_available_child(const struct device_node *node,
  315. struct device_node *prev)
  316. {
  317. struct device_node *next;
  318. read_lock(&devtree_lock);
  319. next = prev ? prev->sibling : node->child;
  320. for (; next; next = next->sibling) {
  321. if (!of_device_is_available(next))
  322. continue;
  323. if (of_node_get(next))
  324. break;
  325. }
  326. of_node_put(prev);
  327. read_unlock(&devtree_lock);
  328. return next;
  329. }
  330. EXPORT_SYMBOL(of_get_next_available_child);
  331. /**
  332. * of_find_node_by_path - Find a node matching a full OF path
  333. * @path: The full path to match
  334. *
  335. * Returns a node pointer with refcount incremented, use
  336. * of_node_put() on it when done.
  337. */
  338. struct device_node *of_find_node_by_path(const char *path)
  339. {
  340. struct device_node *np = allnodes;
  341. read_lock(&devtree_lock);
  342. for (; np; np = np->allnext) {
  343. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  344. && of_node_get(np))
  345. break;
  346. }
  347. read_unlock(&devtree_lock);
  348. return np;
  349. }
  350. EXPORT_SYMBOL(of_find_node_by_path);
  351. /**
  352. * of_find_node_by_name - Find a node by its "name" property
  353. * @from: The node to start searching from or NULL, the node
  354. * you pass will not be searched, only the next one
  355. * will; typically, you pass what the previous call
  356. * returned. of_node_put() will be called on it
  357. * @name: The name string to match against
  358. *
  359. * Returns a node pointer with refcount incremented, use
  360. * of_node_put() on it when done.
  361. */
  362. struct device_node *of_find_node_by_name(struct device_node *from,
  363. const char *name)
  364. {
  365. struct device_node *np;
  366. read_lock(&devtree_lock);
  367. np = from ? from->allnext : allnodes;
  368. for (; np; np = np->allnext)
  369. if (np->name && (of_node_cmp(np->name, name) == 0)
  370. && of_node_get(np))
  371. break;
  372. of_node_put(from);
  373. read_unlock(&devtree_lock);
  374. return np;
  375. }
  376. EXPORT_SYMBOL(of_find_node_by_name);
  377. /**
  378. * of_find_node_by_type - Find a node by its "device_type" property
  379. * @from: The node to start searching from, or NULL to start searching
  380. * the entire device tree. The node you pass will not be
  381. * searched, only the next one will; typically, you pass
  382. * what the previous call returned. of_node_put() will be
  383. * called on from for you.
  384. * @type: The type string to match against
  385. *
  386. * Returns a node pointer with refcount incremented, use
  387. * of_node_put() on it when done.
  388. */
  389. struct device_node *of_find_node_by_type(struct device_node *from,
  390. const char *type)
  391. {
  392. struct device_node *np;
  393. read_lock(&devtree_lock);
  394. np = from ? from->allnext : allnodes;
  395. for (; np; np = np->allnext)
  396. if (np->type && (of_node_cmp(np->type, type) == 0)
  397. && of_node_get(np))
  398. break;
  399. of_node_put(from);
  400. read_unlock(&devtree_lock);
  401. return np;
  402. }
  403. EXPORT_SYMBOL(of_find_node_by_type);
  404. /**
  405. * of_find_compatible_node - Find a node based on type and one of the
  406. * tokens in its "compatible" property
  407. * @from: The node to start searching from or NULL, the node
  408. * you pass will not be searched, only the next one
  409. * will; typically, you pass what the previous call
  410. * returned. of_node_put() will be called on it
  411. * @type: The type string to match "device_type" or NULL to ignore
  412. * @compatible: The string to match to one of the tokens in the device
  413. * "compatible" list.
  414. *
  415. * Returns a node pointer with refcount incremented, use
  416. * of_node_put() on it when done.
  417. */
  418. struct device_node *of_find_compatible_node(struct device_node *from,
  419. const char *type, const char *compatible)
  420. {
  421. struct device_node *np;
  422. read_lock(&devtree_lock);
  423. np = from ? from->allnext : allnodes;
  424. for (; np; np = np->allnext) {
  425. if (type
  426. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  427. continue;
  428. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  429. break;
  430. }
  431. of_node_put(from);
  432. read_unlock(&devtree_lock);
  433. return np;
  434. }
  435. EXPORT_SYMBOL(of_find_compatible_node);
  436. /**
  437. * of_find_node_with_property - Find a node which has a property with
  438. * the given name.
  439. * @from: The node to start searching from or NULL, the node
  440. * you pass will not be searched, only the next one
  441. * will; typically, you pass what the previous call
  442. * returned. of_node_put() will be called on it
  443. * @prop_name: The name of the property to look for.
  444. *
  445. * Returns a node pointer with refcount incremented, use
  446. * of_node_put() on it when done.
  447. */
  448. struct device_node *of_find_node_with_property(struct device_node *from,
  449. const char *prop_name)
  450. {
  451. struct device_node *np;
  452. struct property *pp;
  453. read_lock(&devtree_lock);
  454. np = from ? from->allnext : allnodes;
  455. for (; np; np = np->allnext) {
  456. for (pp = np->properties; pp != 0; pp = pp->next) {
  457. if (of_prop_cmp(pp->name, prop_name) == 0) {
  458. of_node_get(np);
  459. goto out;
  460. }
  461. }
  462. }
  463. out:
  464. of_node_put(from);
  465. read_unlock(&devtree_lock);
  466. return np;
  467. }
  468. EXPORT_SYMBOL(of_find_node_with_property);
  469. /**
  470. * of_match_node - Tell if an device_node has a matching of_match structure
  471. * @matches: array of of device match structures to search in
  472. * @node: the of device structure to match against
  473. *
  474. * Low level utility function used by device matching.
  475. */
  476. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  477. const struct device_node *node)
  478. {
  479. if (!matches)
  480. return NULL;
  481. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  482. int match = 1;
  483. if (matches->name[0])
  484. match &= node->name
  485. && !strcmp(matches->name, node->name);
  486. if (matches->type[0])
  487. match &= node->type
  488. && !strcmp(matches->type, node->type);
  489. if (matches->compatible[0])
  490. match &= of_device_is_compatible(node,
  491. matches->compatible);
  492. if (match)
  493. return matches;
  494. matches++;
  495. }
  496. return NULL;
  497. }
  498. EXPORT_SYMBOL(of_match_node);
  499. /**
  500. * of_find_matching_node - Find a node based on an of_device_id match
  501. * table.
  502. * @from: The node to start searching from or NULL, the node
  503. * you pass will not be searched, only the next one
  504. * will; typically, you pass what the previous call
  505. * returned. of_node_put() will be called on it
  506. * @matches: array of of device match structures to search in
  507. *
  508. * Returns a node pointer with refcount incremented, use
  509. * of_node_put() on it when done.
  510. */
  511. struct device_node *of_find_matching_node(struct device_node *from,
  512. const struct of_device_id *matches)
  513. {
  514. struct device_node *np;
  515. read_lock(&devtree_lock);
  516. np = from ? from->allnext : allnodes;
  517. for (; np; np = np->allnext) {
  518. if (of_match_node(matches, np) && of_node_get(np))
  519. break;
  520. }
  521. of_node_put(from);
  522. read_unlock(&devtree_lock);
  523. return np;
  524. }
  525. EXPORT_SYMBOL(of_find_matching_node);
  526. /**
  527. * of_modalias_node - Lookup appropriate modalias for a device node
  528. * @node: pointer to a device tree node
  529. * @modalias: Pointer to buffer that modalias value will be copied into
  530. * @len: Length of modalias value
  531. *
  532. * Based on the value of the compatible property, this routine will attempt
  533. * to choose an appropriate modalias value for a particular device tree node.
  534. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  535. * from the first entry in the compatible list property.
  536. *
  537. * This routine returns 0 on success, <0 on failure.
  538. */
  539. int of_modalias_node(struct device_node *node, char *modalias, int len)
  540. {
  541. const char *compatible, *p;
  542. int cplen;
  543. compatible = of_get_property(node, "compatible", &cplen);
  544. if (!compatible || strlen(compatible) > cplen)
  545. return -ENODEV;
  546. p = strchr(compatible, ',');
  547. strlcpy(modalias, p ? p + 1 : compatible, len);
  548. return 0;
  549. }
  550. EXPORT_SYMBOL_GPL(of_modalias_node);
  551. /**
  552. * of_find_node_by_phandle - Find a node given a phandle
  553. * @handle: phandle of the node to find
  554. *
  555. * Returns a node pointer with refcount incremented, use
  556. * of_node_put() on it when done.
  557. */
  558. struct device_node *of_find_node_by_phandle(phandle handle)
  559. {
  560. struct device_node *np;
  561. read_lock(&devtree_lock);
  562. for (np = allnodes; np; np = np->allnext)
  563. if (np->phandle == handle)
  564. break;
  565. of_node_get(np);
  566. read_unlock(&devtree_lock);
  567. return np;
  568. }
  569. EXPORT_SYMBOL(of_find_node_by_phandle);
  570. /**
  571. * of_property_read_u32_array - Find and read an array of 32 bit integers
  572. * from a property.
  573. *
  574. * @np: device node from which the property value is to be read.
  575. * @propname: name of the property to be searched.
  576. * @out_value: pointer to return value, modified only if return value is 0.
  577. *
  578. * Search for a property in a device node and read 32-bit value(s) from
  579. * it. Returns 0 on success, -EINVAL if the property does not exist,
  580. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  581. * property data isn't large enough.
  582. *
  583. * The out_value is modified only if a valid u32 value can be decoded.
  584. */
  585. int of_property_read_u32_array(const struct device_node *np,
  586. const char *propname, u32 *out_values,
  587. size_t sz)
  588. {
  589. struct property *prop = of_find_property(np, propname, NULL);
  590. const __be32 *val;
  591. if (!prop)
  592. return -EINVAL;
  593. if (!prop->value)
  594. return -ENODATA;
  595. if ((sz * sizeof(*out_values)) > prop->length)
  596. return -EOVERFLOW;
  597. val = prop->value;
  598. while (sz--)
  599. *out_values++ = be32_to_cpup(val++);
  600. return 0;
  601. }
  602. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  603. /**
  604. * of_property_read_u64 - Find and read a 64 bit integer from a property
  605. * @np: device node from which the property value is to be read.
  606. * @propname: name of the property to be searched.
  607. * @out_value: pointer to return value, modified only if return value is 0.
  608. *
  609. * Search for a property in a device node and read a 64-bit value from
  610. * it. Returns 0 on success, -EINVAL if the property does not exist,
  611. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  612. * property data isn't large enough.
  613. *
  614. * The out_value is modified only if a valid u64 value can be decoded.
  615. */
  616. int of_property_read_u64(const struct device_node *np, const char *propname,
  617. u64 *out_value)
  618. {
  619. struct property *prop = of_find_property(np, propname, NULL);
  620. if (!prop)
  621. return -EINVAL;
  622. if (!prop->value)
  623. return -ENODATA;
  624. if (sizeof(*out_value) > prop->length)
  625. return -EOVERFLOW;
  626. *out_value = of_read_number(prop->value, 2);
  627. return 0;
  628. }
  629. EXPORT_SYMBOL_GPL(of_property_read_u64);
  630. /**
  631. * of_property_read_string - Find and read a string from a property
  632. * @np: device node from which the property value is to be read.
  633. * @propname: name of the property to be searched.
  634. * @out_string: pointer to null terminated return string, modified only if
  635. * return value is 0.
  636. *
  637. * Search for a property in a device tree node and retrieve a null
  638. * terminated string value (pointer to data, not a copy). Returns 0 on
  639. * success, -EINVAL if the property does not exist, -ENODATA if property
  640. * does not have a value, and -EILSEQ if the string is not null-terminated
  641. * within the length of the property data.
  642. *
  643. * The out_string pointer is modified only if a valid string can be decoded.
  644. */
  645. int of_property_read_string(struct device_node *np, const char *propname,
  646. const char **out_string)
  647. {
  648. struct property *prop = of_find_property(np, propname, NULL);
  649. if (!prop)
  650. return -EINVAL;
  651. if (!prop->value)
  652. return -ENODATA;
  653. if (strnlen(prop->value, prop->length) >= prop->length)
  654. return -EILSEQ;
  655. *out_string = prop->value;
  656. return 0;
  657. }
  658. EXPORT_SYMBOL_GPL(of_property_read_string);
  659. /**
  660. * of_property_match_string() - Find string in a list and return index
  661. * @np: pointer to node containing string list property
  662. * @propname: string list property name
  663. * @string: pointer to string to search for in string list
  664. *
  665. * This function searches a string list property and returns the index
  666. * of a specific string value.
  667. */
  668. int of_property_match_string(struct device_node *np, const char *propname,
  669. const char *string)
  670. {
  671. struct property *prop = of_find_property(np, propname, NULL);
  672. size_t l;
  673. int i;
  674. const char *p, *end;
  675. if (!prop)
  676. return -EINVAL;
  677. if (!prop->value)
  678. return -ENODATA;
  679. p = prop->value;
  680. end = p + prop->length;
  681. for (i = 0; p < end; i++, p += l) {
  682. l = strnlen(p, end - p) + 1;
  683. if (p + l > end)
  684. return -EILSEQ;
  685. pr_debug("comparing %s with %s\n", string, p);
  686. if (strcmp(string, p) == 0)
  687. return i; /* Found it; return index */
  688. }
  689. return -ENODATA;
  690. }
  691. EXPORT_SYMBOL_GPL(of_property_match_string);
  692. /**
  693. * of_property_read_string_util() - Utility helper for parsing string properties
  694. * @np: device node from which the property value is to be read.
  695. * @propname: name of the property to be searched.
  696. * @out_strs: output array of string pointers.
  697. * @sz: number of array elements to read.
  698. * @skip: Number of strings to skip over at beginning of list.
  699. *
  700. * Don't call this function directly. It is a utility helper for the
  701. * of_property_read_string*() family of functions.
  702. */
  703. int of_property_read_string_helper(struct device_node *np, const char *propname,
  704. const char **out_strs, size_t sz, int skip)
  705. {
  706. struct property *prop = of_find_property(np, propname, NULL);
  707. int l = 0, i = 0;
  708. const char *p, *end;
  709. if (!prop)
  710. return -EINVAL;
  711. if (!prop->value)
  712. return -ENODATA;
  713. p = prop->value;
  714. end = p + prop->length;
  715. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  716. l = strnlen(p, end - p) + 1;
  717. if (p + l > end)
  718. return -EILSEQ;
  719. if (out_strs && i >= skip)
  720. *out_strs++ = p;
  721. }
  722. i -= skip;
  723. return i <= 0 ? -ENODATA : i;
  724. }
  725. EXPORT_SYMBOL_GPL(of_property_read_string_helper);
  726. /**
  727. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  728. * @np: Pointer to device node holding phandle property
  729. * @phandle_name: Name of property holding a phandle value
  730. * @index: For properties holding a table of phandles, this is the index into
  731. * the table
  732. *
  733. * Returns the device_node pointer with refcount incremented. Use
  734. * of_node_put() on it when done.
  735. */
  736. struct device_node *
  737. of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
  738. {
  739. const __be32 *phandle;
  740. int size;
  741. phandle = of_get_property(np, phandle_name, &size);
  742. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  743. return NULL;
  744. return of_find_node_by_phandle(be32_to_cpup(phandle + index));
  745. }
  746. EXPORT_SYMBOL(of_parse_phandle);
  747. /**
  748. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  749. * @np: pointer to a device tree node containing a list
  750. * @list_name: property name that contains a list
  751. * @cells_name: property name that specifies phandles' arguments count
  752. * @index: index of a phandle to parse out
  753. * @out_args: optional pointer to output arguments structure (will be filled)
  754. *
  755. * This function is useful to parse lists of phandles and their arguments.
  756. * Returns 0 on success and fills out_args, on error returns appropriate
  757. * errno value.
  758. *
  759. * Caller is responsible to call of_node_put() on the returned out_args->node
  760. * pointer.
  761. *
  762. * Example:
  763. *
  764. * phandle1: node1 {
  765. * #list-cells = <2>;
  766. * }
  767. *
  768. * phandle2: node2 {
  769. * #list-cells = <1>;
  770. * }
  771. *
  772. * node3 {
  773. * list = <&phandle1 1 2 &phandle2 3>;
  774. * }
  775. *
  776. * To get a device_node of the `node2' node you may call this:
  777. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  778. */
  779. int of_parse_phandle_with_args(struct device_node *np, const char *list_name,
  780. const char *cells_name, int index,
  781. struct of_phandle_args *out_args)
  782. {
  783. const __be32 *list, *list_end;
  784. int size, cur_index = 0;
  785. uint32_t count = 0;
  786. struct device_node *node = NULL;
  787. phandle phandle;
  788. /* Retrieve the phandle list property */
  789. list = of_get_property(np, list_name, &size);
  790. if (!list)
  791. return -EINVAL;
  792. list_end = list + size / sizeof(*list);
  793. /* Loop over the phandles until all the requested entry is found */
  794. while (list < list_end) {
  795. count = 0;
  796. /*
  797. * If phandle is 0, then it is an empty entry with no
  798. * arguments. Skip forward to the next entry.
  799. */
  800. phandle = be32_to_cpup(list++);
  801. if (phandle) {
  802. /*
  803. * Find the provider node and parse the #*-cells
  804. * property to determine the argument length
  805. */
  806. node = of_find_node_by_phandle(phandle);
  807. if (!node) {
  808. pr_err("%s: could not find phandle\n",
  809. np->full_name);
  810. break;
  811. }
  812. if (of_property_read_u32(node, cells_name, &count)) {
  813. pr_err("%s: could not get %s for %s\n",
  814. np->full_name, cells_name,
  815. node->full_name);
  816. break;
  817. }
  818. /*
  819. * Make sure that the arguments actually fit in the
  820. * remaining property data length
  821. */
  822. if (list + count > list_end) {
  823. pr_err("%s: arguments longer than property\n",
  824. np->full_name);
  825. break;
  826. }
  827. }
  828. /*
  829. * All of the error cases above bail out of the loop, so at
  830. * this point, the parsing is successful. If the requested
  831. * index matches, then fill the out_args structure and return,
  832. * or return -ENOENT for an empty entry.
  833. */
  834. if (cur_index == index) {
  835. if (!phandle)
  836. return -ENOENT;
  837. if (out_args) {
  838. int i;
  839. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  840. count = MAX_PHANDLE_ARGS;
  841. out_args->np = node;
  842. out_args->args_count = count;
  843. for (i = 0; i < count; i++)
  844. out_args->args[i] = be32_to_cpup(list++);
  845. }
  846. return 0;
  847. }
  848. of_node_put(node);
  849. node = NULL;
  850. list += count;
  851. cur_index++;
  852. }
  853. /* Loop exited without finding a valid entry; return an error */
  854. if (node)
  855. of_node_put(node);
  856. return -EINVAL;
  857. }
  858. EXPORT_SYMBOL(of_parse_phandle_with_args);
  859. /**
  860. * prom_add_property - Add a property to a node
  861. */
  862. int prom_add_property(struct device_node *np, struct property *prop)
  863. {
  864. struct property **next;
  865. unsigned long flags;
  866. prop->next = NULL;
  867. write_lock_irqsave(&devtree_lock, flags);
  868. next = &np->properties;
  869. while (*next) {
  870. if (strcmp(prop->name, (*next)->name) == 0) {
  871. /* duplicate ! don't insert it */
  872. write_unlock_irqrestore(&devtree_lock, flags);
  873. return -1;
  874. }
  875. next = &(*next)->next;
  876. }
  877. *next = prop;
  878. write_unlock_irqrestore(&devtree_lock, flags);
  879. #ifdef CONFIG_PROC_DEVICETREE
  880. /* try to add to proc as well if it was initialized */
  881. if (np->pde)
  882. proc_device_tree_add_prop(np->pde, prop);
  883. #endif /* CONFIG_PROC_DEVICETREE */
  884. return 0;
  885. }
  886. /**
  887. * prom_remove_property - Remove a property from a node.
  888. *
  889. * Note that we don't actually remove it, since we have given out
  890. * who-knows-how-many pointers to the data using get-property.
  891. * Instead we just move the property to the "dead properties"
  892. * list, so it won't be found any more.
  893. */
  894. int prom_remove_property(struct device_node *np, struct property *prop)
  895. {
  896. struct property **next;
  897. unsigned long flags;
  898. int found = 0;
  899. write_lock_irqsave(&devtree_lock, flags);
  900. next = &np->properties;
  901. while (*next) {
  902. if (*next == prop) {
  903. /* found the node */
  904. *next = prop->next;
  905. prop->next = np->deadprops;
  906. np->deadprops = prop;
  907. found = 1;
  908. break;
  909. }
  910. next = &(*next)->next;
  911. }
  912. write_unlock_irqrestore(&devtree_lock, flags);
  913. if (!found)
  914. return -ENODEV;
  915. #ifdef CONFIG_PROC_DEVICETREE
  916. /* try to remove the proc node as well */
  917. if (np->pde)
  918. proc_device_tree_remove_prop(np->pde, prop);
  919. #endif /* CONFIG_PROC_DEVICETREE */
  920. return 0;
  921. }
  922. /*
  923. * prom_update_property - Update a property in a node.
  924. *
  925. * Note that we don't actually remove it, since we have given out
  926. * who-knows-how-many pointers to the data using get-property.
  927. * Instead we just move the property to the "dead properties" list,
  928. * and add the new property to the property list
  929. */
  930. int prom_update_property(struct device_node *np,
  931. struct property *newprop,
  932. struct property *oldprop)
  933. {
  934. struct property **next;
  935. unsigned long flags;
  936. int found = 0;
  937. write_lock_irqsave(&devtree_lock, flags);
  938. next = &np->properties;
  939. while (*next) {
  940. if (*next == oldprop) {
  941. /* found the node */
  942. newprop->next = oldprop->next;
  943. *next = newprop;
  944. oldprop->next = np->deadprops;
  945. np->deadprops = oldprop;
  946. found = 1;
  947. break;
  948. }
  949. next = &(*next)->next;
  950. }
  951. write_unlock_irqrestore(&devtree_lock, flags);
  952. if (!found)
  953. return -ENODEV;
  954. #ifdef CONFIG_PROC_DEVICETREE
  955. /* try to add to proc as well if it was initialized */
  956. if (np->pde)
  957. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  958. #endif /* CONFIG_PROC_DEVICETREE */
  959. return 0;
  960. }
  961. #if defined(CONFIG_OF_DYNAMIC)
  962. /*
  963. * Support for dynamic device trees.
  964. *
  965. * On some platforms, the device tree can be manipulated at runtime.
  966. * The routines in this section support adding, removing and changing
  967. * device tree nodes.
  968. */
  969. /**
  970. * of_attach_node - Plug a device node into the tree and global list.
  971. */
  972. void of_attach_node(struct device_node *np)
  973. {
  974. unsigned long flags;
  975. write_lock_irqsave(&devtree_lock, flags);
  976. np->sibling = np->parent->child;
  977. np->allnext = allnodes;
  978. np->parent->child = np;
  979. allnodes = np;
  980. write_unlock_irqrestore(&devtree_lock, flags);
  981. }
  982. /**
  983. * of_detach_node - "Unplug" a node from the device tree.
  984. *
  985. * The caller must hold a reference to the node. The memory associated with
  986. * the node is not freed until its refcount goes to zero.
  987. */
  988. void of_detach_node(struct device_node *np)
  989. {
  990. struct device_node *parent;
  991. unsigned long flags;
  992. write_lock_irqsave(&devtree_lock, flags);
  993. parent = np->parent;
  994. if (!parent)
  995. goto out_unlock;
  996. if (allnodes == np)
  997. allnodes = np->allnext;
  998. else {
  999. struct device_node *prev;
  1000. for (prev = allnodes;
  1001. prev->allnext != np;
  1002. prev = prev->allnext)
  1003. ;
  1004. prev->allnext = np->allnext;
  1005. }
  1006. if (parent->child == np)
  1007. parent->child = np->sibling;
  1008. else {
  1009. struct device_node *prevsib;
  1010. for (prevsib = np->parent->child;
  1011. prevsib->sibling != np;
  1012. prevsib = prevsib->sibling)
  1013. ;
  1014. prevsib->sibling = np->sibling;
  1015. }
  1016. of_node_set_flag(np, OF_DETACHED);
  1017. out_unlock:
  1018. write_unlock_irqrestore(&devtree_lock, flags);
  1019. }
  1020. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1021. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1022. int id, const char *stem, int stem_len)
  1023. {
  1024. ap->np = np;
  1025. ap->id = id;
  1026. strncpy(ap->stem, stem, stem_len);
  1027. ap->stem[stem_len] = 0;
  1028. list_add_tail(&ap->link, &aliases_lookup);
  1029. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1030. ap->alias, ap->stem, ap->id, np ? np->full_name : NULL);
  1031. }
  1032. /**
  1033. * of_alias_scan - Scan all properties of 'aliases' node
  1034. *
  1035. * The function scans all the properties of 'aliases' node and populate
  1036. * the the global lookup table with the properties. It returns the
  1037. * number of alias_prop found, or error code in error case.
  1038. *
  1039. * @dt_alloc: An allocator that provides a virtual address to memory
  1040. * for the resulting tree
  1041. */
  1042. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1043. {
  1044. struct property *pp;
  1045. of_chosen = of_find_node_by_path("/chosen");
  1046. if (of_chosen == NULL)
  1047. of_chosen = of_find_node_by_path("/chosen@0");
  1048. of_aliases = of_find_node_by_path("/aliases");
  1049. if (!of_aliases)
  1050. return;
  1051. for_each_property_of_node(of_aliases, pp) {
  1052. const char *start = pp->name;
  1053. const char *end = start + strlen(start);
  1054. struct device_node *np;
  1055. struct alias_prop *ap;
  1056. int id, len;
  1057. /* Skip those we do not want to proceed */
  1058. if (!strcmp(pp->name, "name") ||
  1059. !strcmp(pp->name, "phandle") ||
  1060. !strcmp(pp->name, "linux,phandle"))
  1061. continue;
  1062. np = of_find_node_by_path(pp->value);
  1063. if (!np)
  1064. continue;
  1065. /* walk the alias backwards to extract the id and work out
  1066. * the 'stem' string */
  1067. while (isdigit(*(end-1)) && end > start)
  1068. end--;
  1069. len = end - start;
  1070. if (kstrtoint(end, 10, &id) < 0)
  1071. continue;
  1072. /* Allocate an alias_prop with enough space for the stem */
  1073. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1074. if (!ap)
  1075. continue;
  1076. memset(ap, 0, sizeof(*ap) + len + 1);
  1077. ap->alias = start;
  1078. of_alias_add(ap, np, id, start, len);
  1079. }
  1080. }
  1081. /**
  1082. * of_alias_get_id - Get alias id for the given device_node
  1083. * @np: Pointer to the given device_node
  1084. * @stem: Alias stem of the given device_node
  1085. *
  1086. * The function travels the lookup table to get alias id for the given
  1087. * device_node and alias stem. It returns the alias id if find it.
  1088. */
  1089. int of_alias_get_id(struct device_node *np, const char *stem)
  1090. {
  1091. struct alias_prop *app;
  1092. int id = -ENODEV;
  1093. mutex_lock(&of_aliases_mutex);
  1094. list_for_each_entry(app, &aliases_lookup, link) {
  1095. if (strcmp(app->stem, stem) != 0)
  1096. continue;
  1097. if (np == app->np) {
  1098. id = app->id;
  1099. break;
  1100. }
  1101. }
  1102. mutex_unlock(&of_aliases_mutex);
  1103. return id;
  1104. }
  1105. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1106. #ifdef CONFIG_OF_SUBCMDLINE_PARSE
  1107. int of_parse_args_on_subcmdline(const char *name, char *buf)
  1108. {
  1109. struct device_node *node;
  1110. static const char *subcmdline;
  1111. char *param_start, *param_end;
  1112. int len = 0, name_len, cmd_len;
  1113. node = of_find_node_by_path("/chosen");
  1114. if (!node) {
  1115. pr_err("%s: get chosen node failed\n", __func__);
  1116. return -ENODEV;
  1117. }
  1118. subcmdline = of_get_property(node, "subcmdline", &len);
  1119. if (!subcmdline || len <= 0) {
  1120. pr_err("%s: get bootargs failed\n", __func__);
  1121. return -ENODEV;
  1122. }
  1123. name_len = strlen(name);
  1124. cmd_len = strlen(subcmdline);
  1125. param_start = strnstr(subcmdline, name, cmd_len);
  1126. if (!param_start) {
  1127. pr_err("%s: %s not found within cmdline\n", __func__, name);
  1128. return -1;
  1129. }
  1130. param_start += name_len;
  1131. param_end = strnstr(param_start, " ", 100);
  1132. if (!param_end) {
  1133. pr_err("%s: no space after %s found within cmdline\n", __func__, name);
  1134. return -1;
  1135. }
  1136. strlcpy(buf, param_start, param_end-param_start+1);
  1137. /* All parsed OK. */
  1138. return 0;
  1139. }
  1140. EXPORT_SYMBOL(of_parse_args_on_subcmdline);
  1141. #endif