base.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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. /**
  27. * struct alias_prop - Alias property in 'aliases' node
  28. * @link: List node to link the structure in aliases_lookup list
  29. * @alias: Alias property name
  30. * @np: Pointer to device_node that the alias stands for
  31. * @id: Index value from end of alias name
  32. * @stem: Alias string without the index
  33. *
  34. * The structure represents one alias property of 'aliases' node as
  35. * an entry in aliases_lookup list.
  36. */
  37. struct alias_prop {
  38. struct list_head link;
  39. const char *alias;
  40. struct device_node *np;
  41. int id;
  42. char stem[0];
  43. };
  44. static LIST_HEAD(aliases_lookup);
  45. struct device_node *allnodes;
  46. struct device_node *of_chosen;
  47. struct device_node *of_aliases;
  48. static DEFINE_MUTEX(of_aliases_mutex);
  49. /* use when traversing tree through the allnext, child, sibling,
  50. * or parent members of struct device_node.
  51. */
  52. DEFINE_RWLOCK(devtree_lock);
  53. int of_n_addr_cells(struct device_node *np)
  54. {
  55. const __be32 *ip;
  56. do {
  57. if (np->parent)
  58. np = np->parent;
  59. ip = of_get_property(np, "#address-cells", NULL);
  60. if (ip)
  61. return be32_to_cpup(ip);
  62. } while (np->parent);
  63. /* No #address-cells property for the root node */
  64. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  65. }
  66. EXPORT_SYMBOL(of_n_addr_cells);
  67. int of_n_size_cells(struct device_node *np)
  68. {
  69. const __be32 *ip;
  70. do {
  71. if (np->parent)
  72. np = np->parent;
  73. ip = of_get_property(np, "#size-cells", NULL);
  74. if (ip)
  75. return be32_to_cpup(ip);
  76. } while (np->parent);
  77. /* No #size-cells property for the root node */
  78. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  79. }
  80. EXPORT_SYMBOL(of_n_size_cells);
  81. #if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */
  82. /**
  83. * of_node_get - Increment refcount of a node
  84. * @node: Node to inc refcount, NULL is supported to
  85. * simplify writing of callers
  86. *
  87. * Returns node.
  88. */
  89. struct device_node *of_node_get(struct device_node *node)
  90. {
  91. if (node)
  92. kref_get(&node->kref);
  93. return node;
  94. }
  95. EXPORT_SYMBOL(of_node_get);
  96. static inline struct device_node *kref_to_device_node(struct kref *kref)
  97. {
  98. return container_of(kref, struct device_node, kref);
  99. }
  100. /**
  101. * of_node_release - release a dynamically allocated node
  102. * @kref: kref element of the node to be released
  103. *
  104. * In of_node_put() this function is passed to kref_put()
  105. * as the destructor.
  106. */
  107. static void of_node_release(struct kref *kref)
  108. {
  109. struct device_node *node = kref_to_device_node(kref);
  110. struct property *prop = node->properties;
  111. /* We should never be releasing nodes that haven't been detached. */
  112. if (!of_node_check_flag(node, OF_DETACHED)) {
  113. pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
  114. dump_stack();
  115. kref_init(&node->kref);
  116. return;
  117. }
  118. if (!of_node_check_flag(node, OF_DYNAMIC))
  119. return;
  120. while (prop) {
  121. struct property *next = prop->next;
  122. kfree(prop->name);
  123. kfree(prop->value);
  124. kfree(prop);
  125. prop = next;
  126. if (!prop) {
  127. prop = node->deadprops;
  128. node->deadprops = NULL;
  129. }
  130. }
  131. kfree(node->full_name);
  132. kfree(node->data);
  133. kfree(node);
  134. }
  135. /**
  136. * of_node_put - Decrement refcount of a node
  137. * @node: Node to dec refcount, NULL is supported to
  138. * simplify writing of callers
  139. *
  140. */
  141. void of_node_put(struct device_node *node)
  142. {
  143. if (node)
  144. kref_put(&node->kref, of_node_release);
  145. }
  146. EXPORT_SYMBOL(of_node_put);
  147. #endif /* !CONFIG_SPARC */
  148. struct property *of_find_property(const struct device_node *np,
  149. const char *name,
  150. int *lenp)
  151. {
  152. struct property *pp;
  153. if (!np)
  154. return NULL;
  155. read_lock(&devtree_lock);
  156. for (pp = np->properties; pp != 0; pp = pp->next) {
  157. if (of_prop_cmp(pp->name, name) == 0) {
  158. if (lenp != 0)
  159. *lenp = pp->length;
  160. break;
  161. }
  162. }
  163. read_unlock(&devtree_lock);
  164. return pp;
  165. }
  166. EXPORT_SYMBOL(of_find_property);
  167. /**
  168. * of_find_all_nodes - Get next node in global list
  169. * @prev: Previous node or NULL to start iteration
  170. * of_node_put() will be called on it
  171. *
  172. * Returns a node pointer with refcount incremented, use
  173. * of_node_put() on it when done.
  174. */
  175. struct device_node *of_find_all_nodes(struct device_node *prev)
  176. {
  177. struct device_node *np;
  178. read_lock(&devtree_lock);
  179. np = prev ? prev->allnext : allnodes;
  180. for (; np != NULL; np = np->allnext)
  181. if (of_node_get(np))
  182. break;
  183. of_node_put(prev);
  184. read_unlock(&devtree_lock);
  185. return np;
  186. }
  187. EXPORT_SYMBOL(of_find_all_nodes);
  188. /*
  189. * Find a property with a given name for a given node
  190. * and return the value.
  191. */
  192. const void *of_get_property(const struct device_node *np, const char *name,
  193. int *lenp)
  194. {
  195. struct property *pp = of_find_property(np, name, lenp);
  196. return pp ? pp->value : NULL;
  197. }
  198. EXPORT_SYMBOL(of_get_property);
  199. /** Checks if the given "compat" string matches one of the strings in
  200. * the device's "compatible" property
  201. */
  202. int of_device_is_compatible(const struct device_node *device,
  203. const char *compat)
  204. {
  205. const char* cp;
  206. int cplen, l;
  207. cp = of_get_property(device, "compatible", &cplen);
  208. if (cp == NULL)
  209. return 0;
  210. while (cplen > 0) {
  211. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  212. return 1;
  213. l = strlen(cp) + 1;
  214. cp += l;
  215. cplen -= l;
  216. }
  217. return 0;
  218. }
  219. EXPORT_SYMBOL(of_device_is_compatible);
  220. /**
  221. * of_machine_is_compatible - Test root of device tree for a given compatible value
  222. * @compat: compatible string to look for in root node's compatible property.
  223. *
  224. * Returns true if the root node has the given value in its
  225. * compatible property.
  226. */
  227. int of_machine_is_compatible(const char *compat)
  228. {
  229. struct device_node *root;
  230. int rc = 0;
  231. root = of_find_node_by_path("/");
  232. if (root) {
  233. rc = of_device_is_compatible(root, compat);
  234. of_node_put(root);
  235. }
  236. return rc;
  237. }
  238. EXPORT_SYMBOL(of_machine_is_compatible);
  239. /**
  240. * of_device_is_available - check if a device is available for use
  241. *
  242. * @device: Node to check for availability
  243. *
  244. * Returns 1 if the status property is absent or set to "okay" or "ok",
  245. * 0 otherwise
  246. */
  247. int of_device_is_available(const struct device_node *device)
  248. {
  249. const char *status;
  250. int statlen;
  251. status = of_get_property(device, "status", &statlen);
  252. if (status == NULL)
  253. return 1;
  254. if (statlen > 0) {
  255. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  256. return 1;
  257. }
  258. return 0;
  259. }
  260. EXPORT_SYMBOL(of_device_is_available);
  261. /**
  262. * of_get_parent - Get a node's parent if any
  263. * @node: Node to get parent
  264. *
  265. * Returns a node pointer with refcount incremented, use
  266. * of_node_put() on it when done.
  267. */
  268. struct device_node *of_get_parent(const struct device_node *node)
  269. {
  270. struct device_node *np;
  271. if (!node)
  272. return NULL;
  273. read_lock(&devtree_lock);
  274. np = of_node_get(node->parent);
  275. read_unlock(&devtree_lock);
  276. return np;
  277. }
  278. EXPORT_SYMBOL(of_get_parent);
  279. /**
  280. * of_get_next_parent - Iterate to a node's parent
  281. * @node: Node to get parent of
  282. *
  283. * This is like of_get_parent() except that it drops the
  284. * refcount on the passed node, making it suitable for iterating
  285. * through a node's parents.
  286. *
  287. * Returns a node pointer with refcount incremented, use
  288. * of_node_put() on it when done.
  289. */
  290. struct device_node *of_get_next_parent(struct device_node *node)
  291. {
  292. struct device_node *parent;
  293. if (!node)
  294. return NULL;
  295. read_lock(&devtree_lock);
  296. parent = of_node_get(node->parent);
  297. of_node_put(node);
  298. read_unlock(&devtree_lock);
  299. return parent;
  300. }
  301. /**
  302. * of_get_next_child - Iterate a node childs
  303. * @node: parent node
  304. * @prev: previous child of the parent node, or NULL to get first
  305. *
  306. * Returns a node pointer with refcount incremented, use
  307. * of_node_put() on it when done.
  308. */
  309. struct device_node *of_get_next_child(const struct device_node *node,
  310. struct device_node *prev)
  311. {
  312. struct device_node *next;
  313. read_lock(&devtree_lock);
  314. next = prev ? prev->sibling : node->child;
  315. for (; next; next = next->sibling)
  316. if (of_node_get(next))
  317. break;
  318. of_node_put(prev);
  319. read_unlock(&devtree_lock);
  320. return next;
  321. }
  322. EXPORT_SYMBOL(of_get_next_child);
  323. /**
  324. * of_find_node_by_path - Find a node matching a full OF path
  325. * @path: The full path to match
  326. *
  327. * Returns a node pointer with refcount incremented, use
  328. * of_node_put() on it when done.
  329. */
  330. struct device_node *of_find_node_by_path(const char *path)
  331. {
  332. struct device_node *np = allnodes;
  333. read_lock(&devtree_lock);
  334. for (; np; np = np->allnext) {
  335. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  336. && of_node_get(np))
  337. break;
  338. }
  339. read_unlock(&devtree_lock);
  340. return np;
  341. }
  342. EXPORT_SYMBOL(of_find_node_by_path);
  343. /**
  344. * of_find_node_by_name - Find a node by its "name" property
  345. * @from: The node to start searching from or NULL, the node
  346. * you pass will not be searched, only the next one
  347. * will; typically, you pass what the previous call
  348. * returned. of_node_put() will be called on it
  349. * @name: The name string to match against
  350. *
  351. * Returns a node pointer with refcount incremented, use
  352. * of_node_put() on it when done.
  353. */
  354. struct device_node *of_find_node_by_name(struct device_node *from,
  355. const char *name)
  356. {
  357. struct device_node *np;
  358. read_lock(&devtree_lock);
  359. np = from ? from->allnext : allnodes;
  360. for (; np; np = np->allnext)
  361. if (np->name && (of_node_cmp(np->name, name) == 0)
  362. && of_node_get(np))
  363. break;
  364. of_node_put(from);
  365. read_unlock(&devtree_lock);
  366. return np;
  367. }
  368. EXPORT_SYMBOL(of_find_node_by_name);
  369. /**
  370. * of_find_node_by_type - Find a node by its "device_type" property
  371. * @from: The node to start searching from, or NULL to start searching
  372. * the entire device tree. The node you pass will not be
  373. * searched, only the next one will; typically, you pass
  374. * what the previous call returned. of_node_put() will be
  375. * called on from for you.
  376. * @type: The type string to match against
  377. *
  378. * Returns a node pointer with refcount incremented, use
  379. * of_node_put() on it when done.
  380. */
  381. struct device_node *of_find_node_by_type(struct device_node *from,
  382. const char *type)
  383. {
  384. struct device_node *np;
  385. read_lock(&devtree_lock);
  386. np = from ? from->allnext : allnodes;
  387. for (; np; np = np->allnext)
  388. if (np->type && (of_node_cmp(np->type, type) == 0)
  389. && of_node_get(np))
  390. break;
  391. of_node_put(from);
  392. read_unlock(&devtree_lock);
  393. return np;
  394. }
  395. EXPORT_SYMBOL(of_find_node_by_type);
  396. /**
  397. * of_find_compatible_node - Find a node based on type and one of the
  398. * tokens in its "compatible" property
  399. * @from: The node to start searching from or NULL, the node
  400. * you pass will not be searched, only the next one
  401. * will; typically, you pass what the previous call
  402. * returned. of_node_put() will be called on it
  403. * @type: The type string to match "device_type" or NULL to ignore
  404. * @compatible: The string to match to one of the tokens in the device
  405. * "compatible" list.
  406. *
  407. * Returns a node pointer with refcount incremented, use
  408. * of_node_put() on it when done.
  409. */
  410. struct device_node *of_find_compatible_node(struct device_node *from,
  411. const char *type, const char *compatible)
  412. {
  413. struct device_node *np;
  414. read_lock(&devtree_lock);
  415. np = from ? from->allnext : allnodes;
  416. for (; np; np = np->allnext) {
  417. if (type
  418. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  419. continue;
  420. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  421. break;
  422. }
  423. of_node_put(from);
  424. read_unlock(&devtree_lock);
  425. return np;
  426. }
  427. EXPORT_SYMBOL(of_find_compatible_node);
  428. /**
  429. * of_find_node_with_property - Find a node which has a property with
  430. * the given name.
  431. * @from: The node to start searching from or NULL, the node
  432. * you pass will not be searched, only the next one
  433. * will; typically, you pass what the previous call
  434. * returned. of_node_put() will be called on it
  435. * @prop_name: The name of the property to look for.
  436. *
  437. * Returns a node pointer with refcount incremented, use
  438. * of_node_put() on it when done.
  439. */
  440. struct device_node *of_find_node_with_property(struct device_node *from,
  441. const char *prop_name)
  442. {
  443. struct device_node *np;
  444. struct property *pp;
  445. read_lock(&devtree_lock);
  446. np = from ? from->allnext : allnodes;
  447. for (; np; np = np->allnext) {
  448. for (pp = np->properties; pp != 0; pp = pp->next) {
  449. if (of_prop_cmp(pp->name, prop_name) == 0) {
  450. of_node_get(np);
  451. goto out;
  452. }
  453. }
  454. }
  455. out:
  456. of_node_put(from);
  457. read_unlock(&devtree_lock);
  458. return np;
  459. }
  460. EXPORT_SYMBOL(of_find_node_with_property);
  461. /**
  462. * of_match_node - Tell if an device_node has a matching of_match structure
  463. * @matches: array of of device match structures to search in
  464. * @node: the of device structure to match against
  465. *
  466. * Low level utility function used by device matching.
  467. */
  468. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  469. const struct device_node *node)
  470. {
  471. if (!matches)
  472. return NULL;
  473. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  474. int match = 1;
  475. if (matches->name[0])
  476. match &= node->name
  477. && !strcmp(matches->name, node->name);
  478. if (matches->type[0])
  479. match &= node->type
  480. && !strcmp(matches->type, node->type);
  481. if (matches->compatible[0])
  482. match &= of_device_is_compatible(node,
  483. matches->compatible);
  484. if (match)
  485. return matches;
  486. matches++;
  487. }
  488. return NULL;
  489. }
  490. EXPORT_SYMBOL(of_match_node);
  491. /**
  492. * of_find_matching_node - Find a node based on an of_device_id match
  493. * table.
  494. * @from: The node to start searching from or NULL, the node
  495. * you pass will not be searched, only the next one
  496. * will; typically, you pass what the previous call
  497. * returned. of_node_put() will be called on it
  498. * @matches: array of of device match structures to search in
  499. *
  500. * Returns a node pointer with refcount incremented, use
  501. * of_node_put() on it when done.
  502. */
  503. struct device_node *of_find_matching_node(struct device_node *from,
  504. const struct of_device_id *matches)
  505. {
  506. struct device_node *np;
  507. read_lock(&devtree_lock);
  508. np = from ? from->allnext : allnodes;
  509. for (; np; np = np->allnext) {
  510. if (of_match_node(matches, np) && of_node_get(np))
  511. break;
  512. }
  513. of_node_put(from);
  514. read_unlock(&devtree_lock);
  515. return np;
  516. }
  517. EXPORT_SYMBOL(of_find_matching_node);
  518. /**
  519. * of_modalias_node - Lookup appropriate modalias for a device node
  520. * @node: pointer to a device tree node
  521. * @modalias: Pointer to buffer that modalias value will be copied into
  522. * @len: Length of modalias value
  523. *
  524. * Based on the value of the compatible property, this routine will attempt
  525. * to choose an appropriate modalias value for a particular device tree node.
  526. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  527. * from the first entry in the compatible list property.
  528. *
  529. * This routine returns 0 on success, <0 on failure.
  530. */
  531. int of_modalias_node(struct device_node *node, char *modalias, int len)
  532. {
  533. const char *compatible, *p;
  534. int cplen;
  535. compatible = of_get_property(node, "compatible", &cplen);
  536. if (!compatible || strlen(compatible) > cplen)
  537. return -ENODEV;
  538. p = strchr(compatible, ',');
  539. strlcpy(modalias, p ? p + 1 : compatible, len);
  540. return 0;
  541. }
  542. EXPORT_SYMBOL_GPL(of_modalias_node);
  543. /**
  544. * of_find_node_by_phandle - Find a node given a phandle
  545. * @handle: phandle of the node to find
  546. *
  547. * Returns a node pointer with refcount incremented, use
  548. * of_node_put() on it when done.
  549. */
  550. struct device_node *of_find_node_by_phandle(phandle handle)
  551. {
  552. struct device_node *np;
  553. read_lock(&devtree_lock);
  554. for (np = allnodes; np; np = np->allnext)
  555. if (np->phandle == handle)
  556. break;
  557. of_node_get(np);
  558. read_unlock(&devtree_lock);
  559. return np;
  560. }
  561. EXPORT_SYMBOL(of_find_node_by_phandle);
  562. /**
  563. * of_property_read_u32_array - Find and read an array of 32 bit integers
  564. * from a property.
  565. *
  566. * @np: device node from which the property value is to be read.
  567. * @propname: name of the property to be searched.
  568. * @out_value: pointer to return value, modified only if return value is 0.
  569. *
  570. * Search for a property in a device node and read 32-bit value(s) from
  571. * it. Returns 0 on success, -EINVAL if the property does not exist,
  572. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  573. * property data isn't large enough.
  574. *
  575. * The out_value is modified only if a valid u32 value can be decoded.
  576. */
  577. int of_property_read_u32_array(const struct device_node *np,
  578. const char *propname, u32 *out_values,
  579. size_t sz)
  580. {
  581. struct property *prop = of_find_property(np, propname, NULL);
  582. const __be32 *val;
  583. if (!prop)
  584. return -EINVAL;
  585. if (!prop->value)
  586. return -ENODATA;
  587. if ((sz * sizeof(*out_values)) > prop->length)
  588. return -EOVERFLOW;
  589. val = prop->value;
  590. while (sz--)
  591. *out_values++ = be32_to_cpup(val++);
  592. return 0;
  593. }
  594. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  595. /**
  596. * of_property_read_u64 - Find and read a 64 bit integer from a property
  597. * @np: device node from which the property value is to be read.
  598. * @propname: name of the property to be searched.
  599. * @out_value: pointer to return value, modified only if return value is 0.
  600. *
  601. * Search for a property in a device node and read a 64-bit value from
  602. * it. Returns 0 on success, -EINVAL if the property does not exist,
  603. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  604. * property data isn't large enough.
  605. *
  606. * The out_value is modified only if a valid u64 value can be decoded.
  607. */
  608. int of_property_read_u64(const struct device_node *np, const char *propname,
  609. u64 *out_value)
  610. {
  611. struct property *prop = of_find_property(np, propname, NULL);
  612. if (!prop)
  613. return -EINVAL;
  614. if (!prop->value)
  615. return -ENODATA;
  616. if (sizeof(*out_value) > prop->length)
  617. return -EOVERFLOW;
  618. *out_value = of_read_number(prop->value, 2);
  619. return 0;
  620. }
  621. EXPORT_SYMBOL_GPL(of_property_read_u64);
  622. /**
  623. * of_property_read_string - Find and read a string from a property
  624. * @np: device node from which the property value is to be read.
  625. * @propname: name of the property to be searched.
  626. * @out_string: pointer to null terminated return string, modified only if
  627. * return value is 0.
  628. *
  629. * Search for a property in a device tree node and retrieve a null
  630. * terminated string value (pointer to data, not a copy). Returns 0 on
  631. * success, -EINVAL if the property does not exist, -ENODATA if property
  632. * does not have a value, and -EILSEQ if the string is not null-terminated
  633. * within the length of the property data.
  634. *
  635. * The out_string pointer is modified only if a valid string can be decoded.
  636. */
  637. int of_property_read_string(struct device_node *np, const char *propname,
  638. const char **out_string)
  639. {
  640. struct property *prop = of_find_property(np, propname, NULL);
  641. if (!prop)
  642. return -EINVAL;
  643. if (!prop->value)
  644. return -ENODATA;
  645. if (strnlen(prop->value, prop->length) >= prop->length)
  646. return -EILSEQ;
  647. *out_string = prop->value;
  648. return 0;
  649. }
  650. EXPORT_SYMBOL_GPL(of_property_read_string);
  651. /**
  652. * of_property_read_string_index - Find and read a string from a multiple
  653. * strings property.
  654. * @np: device node from which the property value is to be read.
  655. * @propname: name of the property to be searched.
  656. * @index: index of the string in the list of strings
  657. * @out_string: pointer to null terminated return string, modified only if
  658. * return value is 0.
  659. *
  660. * Search for a property in a device tree node and retrieve a null
  661. * terminated string value (pointer to data, not a copy) in the list of strings
  662. * contained in that property.
  663. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  664. * property does not have a value, and -EILSEQ if the string is not
  665. * null-terminated within the length of the property data.
  666. *
  667. * The out_string pointer is modified only if a valid string can be decoded.
  668. */
  669. int of_property_read_string_index(struct device_node *np, const char *propname,
  670. int index, const char **output)
  671. {
  672. struct property *prop = of_find_property(np, propname, NULL);
  673. int i = 0;
  674. size_t l = 0, total = 0;
  675. const char *p;
  676. if (!prop)
  677. return -EINVAL;
  678. if (!prop->value)
  679. return -ENODATA;
  680. if (strnlen(prop->value, prop->length) >= prop->length)
  681. return -EILSEQ;
  682. p = prop->value;
  683. for (i = 0; total < prop->length; total += l, p += l) {
  684. l = strlen(p) + 1;
  685. if ((*p != 0) && (i++ == index)) {
  686. *output = p;
  687. return 0;
  688. }
  689. }
  690. return -ENODATA;
  691. }
  692. EXPORT_SYMBOL_GPL(of_property_read_string_index);
  693. /**
  694. * of_property_count_strings - Find and return the number of strings from a
  695. * multiple strings property.
  696. * @np: device node from which the property value is to be read.
  697. * @propname: name of the property to be searched.
  698. *
  699. * Search for a property in a device tree node and retrieve the number of null
  700. * terminated string contain in it. Returns the number of strings on
  701. * success, -EINVAL if the property does not exist, -ENODATA if property
  702. * does not have a value, and -EILSEQ if the string is not null-terminated
  703. * within the length of the property data.
  704. */
  705. int of_property_count_strings(struct device_node *np, const char *propname)
  706. {
  707. struct property *prop = of_find_property(np, propname, NULL);
  708. int i = 0;
  709. size_t l = 0, total = 0;
  710. const char *p;
  711. if (!prop)
  712. return -EINVAL;
  713. if (!prop->value)
  714. return -ENODATA;
  715. if (strnlen(prop->value, prop->length) >= prop->length)
  716. return -EILSEQ;
  717. p = prop->value;
  718. for (i = 0; total < prop->length; total += l, p += l) {
  719. l = strlen(p) + 1;
  720. if (*p != 0)
  721. i++;
  722. }
  723. return i;
  724. }
  725. EXPORT_SYMBOL_GPL(of_property_count_strings);
  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(pp, of_aliases->properties) {
  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. ap->alias = start;
  1077. of_alias_add(ap, np, id, start, len);
  1078. }
  1079. }
  1080. /**
  1081. * of_alias_get_id - Get alias id for the given device_node
  1082. * @np: Pointer to the given device_node
  1083. * @stem: Alias stem of the given device_node
  1084. *
  1085. * The function travels the lookup table to get alias id for the given
  1086. * device_node and alias stem. It returns the alias id if find it.
  1087. */
  1088. int of_alias_get_id(struct device_node *np, const char *stem)
  1089. {
  1090. struct alias_prop *app;
  1091. int id = -ENODEV;
  1092. mutex_lock(&of_aliases_mutex);
  1093. list_for_each_entry(app, &aliases_lookup, link) {
  1094. if (strcmp(app->stem, stem) != 0)
  1095. continue;
  1096. if (np == app->np) {
  1097. id = app->id;
  1098. break;
  1099. }
  1100. }
  1101. mutex_unlock(&of_aliases_mutex);
  1102. return id;
  1103. }
  1104. EXPORT_SYMBOL_GPL(of_alias_get_id);