base.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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_OF_DYNAMIC)
  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_OF_DYNAMIC */
  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; pp = pp->next) {
  157. if (of_prop_cmp(pp->name, name) == 0) {
  158. if (lenp)
  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_get_next_available_child - Find the next available child node
  325. * @node: parent node
  326. * @prev: previous child of the parent node, or NULL to get first
  327. *
  328. * This function is like of_get_next_child(), except that it
  329. * automatically skips any disabled nodes (i.e. status = "disabled").
  330. */
  331. struct device_node *of_get_next_available_child(const struct device_node *node,
  332. struct device_node *prev)
  333. {
  334. struct device_node *next;
  335. read_lock(&devtree_lock);
  336. next = prev ? prev->sibling : node->child;
  337. for (; next; next = next->sibling) {
  338. if (!of_device_is_available(next))
  339. continue;
  340. if (of_node_get(next))
  341. break;
  342. }
  343. of_node_put(prev);
  344. read_unlock(&devtree_lock);
  345. return next;
  346. }
  347. EXPORT_SYMBOL(of_get_next_available_child);
  348. /**
  349. * of_get_child_by_name - Find the child node by name for a given parent
  350. * @node: parent node
  351. * @name: child name to look for.
  352. *
  353. * This function looks for child node for given matching name
  354. *
  355. * Returns a node pointer if found, with refcount incremented, use
  356. * of_node_put() on it when done.
  357. * Returns NULL if node is not found.
  358. */
  359. struct device_node *of_get_child_by_name(const struct device_node *node,
  360. const char *name)
  361. {
  362. struct device_node *child;
  363. for_each_child_of_node(node, child)
  364. if (child->name && (of_node_cmp(child->name, name) == 0))
  365. break;
  366. return child;
  367. }
  368. EXPORT_SYMBOL(of_get_child_by_name);
  369. /**
  370. * of_find_node_by_path - Find a node matching a full OF path
  371. * @path: The full path to match
  372. *
  373. * Returns a node pointer with refcount incremented, use
  374. * of_node_put() on it when done.
  375. */
  376. struct device_node *of_find_node_by_path(const char *path)
  377. {
  378. struct device_node *np = allnodes;
  379. read_lock(&devtree_lock);
  380. for (; np; np = np->allnext) {
  381. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  382. && of_node_get(np))
  383. break;
  384. }
  385. read_unlock(&devtree_lock);
  386. return np;
  387. }
  388. EXPORT_SYMBOL(of_find_node_by_path);
  389. /**
  390. * of_find_node_by_name - Find a node by its "name" property
  391. * @from: The node to start searching from or NULL, the node
  392. * you pass will not be searched, only the next one
  393. * will; typically, you pass what the previous call
  394. * returned. of_node_put() will be called on it
  395. * @name: The name string to match against
  396. *
  397. * Returns a node pointer with refcount incremented, use
  398. * of_node_put() on it when done.
  399. */
  400. struct device_node *of_find_node_by_name(struct device_node *from,
  401. const char *name)
  402. {
  403. struct device_node *np;
  404. read_lock(&devtree_lock);
  405. np = from ? from->allnext : allnodes;
  406. for (; np; np = np->allnext)
  407. if (np->name && (of_node_cmp(np->name, name) == 0)
  408. && of_node_get(np))
  409. break;
  410. of_node_put(from);
  411. read_unlock(&devtree_lock);
  412. return np;
  413. }
  414. EXPORT_SYMBOL(of_find_node_by_name);
  415. /**
  416. * of_find_node_by_type - Find a node by its "device_type" property
  417. * @from: The node to start searching from, or NULL to start searching
  418. * the entire device tree. The node you pass will not be
  419. * searched, only the next one will; typically, you pass
  420. * what the previous call returned. of_node_put() will be
  421. * called on from for you.
  422. * @type: The type string to match against
  423. *
  424. * Returns a node pointer with refcount incremented, use
  425. * of_node_put() on it when done.
  426. */
  427. struct device_node *of_find_node_by_type(struct device_node *from,
  428. const char *type)
  429. {
  430. struct device_node *np;
  431. read_lock(&devtree_lock);
  432. np = from ? from->allnext : allnodes;
  433. for (; np; np = np->allnext)
  434. if (np->type && (of_node_cmp(np->type, type) == 0)
  435. && of_node_get(np))
  436. break;
  437. of_node_put(from);
  438. read_unlock(&devtree_lock);
  439. return np;
  440. }
  441. EXPORT_SYMBOL(of_find_node_by_type);
  442. /**
  443. * of_find_compatible_node - Find a node based on type and one of the
  444. * tokens in its "compatible" property
  445. * @from: The node to start searching from or NULL, the node
  446. * you pass will not be searched, only the next one
  447. * will; typically, you pass what the previous call
  448. * returned. of_node_put() will be called on it
  449. * @type: The type string to match "device_type" or NULL to ignore
  450. * @compatible: The string to match to one of the tokens in the device
  451. * "compatible" list.
  452. *
  453. * Returns a node pointer with refcount incremented, use
  454. * of_node_put() on it when done.
  455. */
  456. struct device_node *of_find_compatible_node(struct device_node *from,
  457. const char *type, const char *compatible)
  458. {
  459. struct device_node *np;
  460. read_lock(&devtree_lock);
  461. np = from ? from->allnext : allnodes;
  462. for (; np; np = np->allnext) {
  463. if (type
  464. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  465. continue;
  466. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  467. break;
  468. }
  469. of_node_put(from);
  470. read_unlock(&devtree_lock);
  471. return np;
  472. }
  473. EXPORT_SYMBOL(of_find_compatible_node);
  474. /**
  475. * of_find_node_with_property - Find a node which has a property with
  476. * the given name.
  477. * @from: The node to start searching from or NULL, the node
  478. * you pass will not be searched, only the next one
  479. * will; typically, you pass what the previous call
  480. * returned. of_node_put() will be called on it
  481. * @prop_name: The name of the property to look for.
  482. *
  483. * Returns a node pointer with refcount incremented, use
  484. * of_node_put() on it when done.
  485. */
  486. struct device_node *of_find_node_with_property(struct device_node *from,
  487. const char *prop_name)
  488. {
  489. struct device_node *np;
  490. struct property *pp;
  491. read_lock(&devtree_lock);
  492. np = from ? from->allnext : allnodes;
  493. for (; np; np = np->allnext) {
  494. for (pp = np->properties; pp; pp = pp->next) {
  495. if (of_prop_cmp(pp->name, prop_name) == 0) {
  496. of_node_get(np);
  497. goto out;
  498. }
  499. }
  500. }
  501. out:
  502. of_node_put(from);
  503. read_unlock(&devtree_lock);
  504. return np;
  505. }
  506. EXPORT_SYMBOL(of_find_node_with_property);
  507. /**
  508. * of_match_node - Tell if an device_node has a matching of_match structure
  509. * @matches: array of of device match structures to search in
  510. * @node: the of device structure to match against
  511. *
  512. * Low level utility function used by device matching.
  513. */
  514. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  515. const struct device_node *node)
  516. {
  517. if (!matches)
  518. return NULL;
  519. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  520. int match = 1;
  521. if (matches->name[0])
  522. match &= node->name
  523. && !strcmp(matches->name, node->name);
  524. if (matches->type[0])
  525. match &= node->type
  526. && !strcmp(matches->type, node->type);
  527. if (matches->compatible[0])
  528. match &= of_device_is_compatible(node,
  529. matches->compatible);
  530. if (match)
  531. return matches;
  532. matches++;
  533. }
  534. return NULL;
  535. }
  536. EXPORT_SYMBOL(of_match_node);
  537. /**
  538. * of_find_matching_node - Find a node based on an of_device_id match
  539. * table.
  540. * @from: The node to start searching from or NULL, the node
  541. * you pass will not be searched, only the next one
  542. * will; typically, you pass what the previous call
  543. * returned. of_node_put() will be called on it
  544. * @matches: array of of device match structures to search in
  545. *
  546. * Returns a node pointer with refcount incremented, use
  547. * of_node_put() on it when done.
  548. */
  549. struct device_node *of_find_matching_node(struct device_node *from,
  550. const struct of_device_id *matches)
  551. {
  552. struct device_node *np;
  553. read_lock(&devtree_lock);
  554. np = from ? from->allnext : allnodes;
  555. for (; np; np = np->allnext) {
  556. if (of_match_node(matches, np) && of_node_get(np))
  557. break;
  558. }
  559. of_node_put(from);
  560. read_unlock(&devtree_lock);
  561. return np;
  562. }
  563. EXPORT_SYMBOL(of_find_matching_node);
  564. /**
  565. * of_modalias_node - Lookup appropriate modalias for a device node
  566. * @node: pointer to a device tree node
  567. * @modalias: Pointer to buffer that modalias value will be copied into
  568. * @len: Length of modalias value
  569. *
  570. * Based on the value of the compatible property, this routine will attempt
  571. * to choose an appropriate modalias value for a particular device tree node.
  572. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  573. * from the first entry in the compatible list property.
  574. *
  575. * This routine returns 0 on success, <0 on failure.
  576. */
  577. int of_modalias_node(struct device_node *node, char *modalias, int len)
  578. {
  579. const char *compatible, *p;
  580. int cplen;
  581. compatible = of_get_property(node, "compatible", &cplen);
  582. if (!compatible || strlen(compatible) > cplen)
  583. return -ENODEV;
  584. p = strchr(compatible, ',');
  585. strlcpy(modalias, p ? p + 1 : compatible, len);
  586. return 0;
  587. }
  588. EXPORT_SYMBOL_GPL(of_modalias_node);
  589. /**
  590. * of_find_node_by_phandle - Find a node given a phandle
  591. * @handle: phandle of the node to find
  592. *
  593. * Returns a node pointer with refcount incremented, use
  594. * of_node_put() on it when done.
  595. */
  596. struct device_node *of_find_node_by_phandle(phandle handle)
  597. {
  598. struct device_node *np;
  599. read_lock(&devtree_lock);
  600. for (np = allnodes; np; np = np->allnext)
  601. if (np->phandle == handle)
  602. break;
  603. of_node_get(np);
  604. read_unlock(&devtree_lock);
  605. return np;
  606. }
  607. EXPORT_SYMBOL(of_find_node_by_phandle);
  608. /**
  609. * of_property_read_u32_array - Find and read an array of 32 bit integers
  610. * from a property.
  611. *
  612. * @np: device node from which the property value is to be read.
  613. * @propname: name of the property to be searched.
  614. * @out_value: pointer to return value, modified only if return value is 0.
  615. *
  616. * Search for a property in a device node and read 32-bit value(s) from
  617. * it. Returns 0 on success, -EINVAL if the property does not exist,
  618. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  619. * property data isn't large enough.
  620. *
  621. * The out_value is modified only if a valid u32 value can be decoded.
  622. */
  623. int of_property_read_u32_array(const struct device_node *np,
  624. const char *propname, u32 *out_values,
  625. size_t sz)
  626. {
  627. struct property *prop = of_find_property(np, propname, NULL);
  628. const __be32 *val;
  629. if (!prop)
  630. return -EINVAL;
  631. if (!prop->value)
  632. return -ENODATA;
  633. if ((sz * sizeof(*out_values)) > prop->length)
  634. return -EOVERFLOW;
  635. val = prop->value;
  636. while (sz--)
  637. *out_values++ = be32_to_cpup(val++);
  638. return 0;
  639. }
  640. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  641. /**
  642. * of_property_read_u64 - Find and read a 64 bit integer from a property
  643. * @np: device node from which the property value is to be read.
  644. * @propname: name of the property to be searched.
  645. * @out_value: pointer to return value, modified only if return value is 0.
  646. *
  647. * Search for a property in a device node and read a 64-bit value from
  648. * it. Returns 0 on success, -EINVAL if the property does not exist,
  649. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  650. * property data isn't large enough.
  651. *
  652. * The out_value is modified only if a valid u64 value can be decoded.
  653. */
  654. int of_property_read_u64(const struct device_node *np, const char *propname,
  655. u64 *out_value)
  656. {
  657. struct property *prop = of_find_property(np, propname, NULL);
  658. if (!prop)
  659. return -EINVAL;
  660. if (!prop->value)
  661. return -ENODATA;
  662. if (sizeof(*out_value) > prop->length)
  663. return -EOVERFLOW;
  664. *out_value = of_read_number(prop->value, 2);
  665. return 0;
  666. }
  667. EXPORT_SYMBOL_GPL(of_property_read_u64);
  668. /**
  669. * of_property_read_string - Find and read a string from a property
  670. * @np: device node from which the property value is to be read.
  671. * @propname: name of the property to be searched.
  672. * @out_string: pointer to null terminated return string, modified only if
  673. * return value is 0.
  674. *
  675. * Search for a property in a device tree node and retrieve a null
  676. * terminated string value (pointer to data, not a copy). Returns 0 on
  677. * success, -EINVAL if the property does not exist, -ENODATA if property
  678. * does not have a value, and -EILSEQ if the string is not null-terminated
  679. * within the length of the property data.
  680. *
  681. * The out_string pointer is modified only if a valid string can be decoded.
  682. */
  683. int of_property_read_string(struct device_node *np, const char *propname,
  684. const char **out_string)
  685. {
  686. struct property *prop = of_find_property(np, propname, NULL);
  687. if (!prop)
  688. return -EINVAL;
  689. if (!prop->value)
  690. return -ENODATA;
  691. if (strnlen(prop->value, prop->length) >= prop->length)
  692. return -EILSEQ;
  693. *out_string = prop->value;
  694. return 0;
  695. }
  696. EXPORT_SYMBOL_GPL(of_property_read_string);
  697. /**
  698. * of_property_read_string_index - Find and read a string from a multiple
  699. * strings property.
  700. * @np: device node from which the property value is to be read.
  701. * @propname: name of the property to be searched.
  702. * @index: index of the string in the list of strings
  703. * @out_string: pointer to null terminated return string, modified only if
  704. * return value is 0.
  705. *
  706. * Search for a property in a device tree node and retrieve a null
  707. * terminated string value (pointer to data, not a copy) in the list of strings
  708. * contained in that property.
  709. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  710. * property does not have a value, and -EILSEQ if the string is not
  711. * null-terminated within the length of the property data.
  712. *
  713. * The out_string pointer is modified only if a valid string can be decoded.
  714. */
  715. int of_property_read_string_index(struct device_node *np, const char *propname,
  716. int index, const char **output)
  717. {
  718. struct property *prop = of_find_property(np, propname, NULL);
  719. int i = 0;
  720. size_t l = 0, total = 0;
  721. const char *p;
  722. if (!prop)
  723. return -EINVAL;
  724. if (!prop->value)
  725. return -ENODATA;
  726. if (strnlen(prop->value, prop->length) >= prop->length)
  727. return -EILSEQ;
  728. p = prop->value;
  729. for (i = 0; total < prop->length; total += l, p += l) {
  730. l = strlen(p) + 1;
  731. if (i++ == index) {
  732. *output = p;
  733. return 0;
  734. }
  735. }
  736. return -ENODATA;
  737. }
  738. EXPORT_SYMBOL_GPL(of_property_read_string_index);
  739. /**
  740. * of_property_match_string() - Find string in a list and return index
  741. * @np: pointer to node containing string list property
  742. * @propname: string list property name
  743. * @string: pointer to string to search for in string list
  744. *
  745. * This function searches a string list property and returns the index
  746. * of a specific string value.
  747. */
  748. int of_property_match_string(struct device_node *np, const char *propname,
  749. const char *string)
  750. {
  751. struct property *prop = of_find_property(np, propname, NULL);
  752. size_t l;
  753. int i;
  754. const char *p, *end;
  755. if (!prop)
  756. return -EINVAL;
  757. if (!prop->value)
  758. return -ENODATA;
  759. p = prop->value;
  760. end = p + prop->length;
  761. for (i = 0; p < end; i++, p += l) {
  762. l = strlen(p) + 1;
  763. if (p + l > end)
  764. return -EILSEQ;
  765. pr_debug("comparing %s with %s\n", string, p);
  766. if (strcmp(string, p) == 0)
  767. return i; /* Found it; return index */
  768. }
  769. return -ENODATA;
  770. }
  771. EXPORT_SYMBOL_GPL(of_property_match_string);
  772. /**
  773. * of_property_count_strings - Find and return the number of strings from a
  774. * multiple strings property.
  775. * @np: device node from which the property value is to be read.
  776. * @propname: name of the property to be searched.
  777. *
  778. * Search for a property in a device tree node and retrieve the number of null
  779. * terminated string contain in it. Returns the number of strings on
  780. * success, -EINVAL if the property does not exist, -ENODATA if property
  781. * does not have a value, and -EILSEQ if the string is not null-terminated
  782. * within the length of the property data.
  783. */
  784. int of_property_count_strings(struct device_node *np, const char *propname)
  785. {
  786. struct property *prop = of_find_property(np, propname, NULL);
  787. int i = 0;
  788. size_t l = 0, total = 0;
  789. const char *p;
  790. if (!prop)
  791. return -EINVAL;
  792. if (!prop->value)
  793. return -ENODATA;
  794. if (strnlen(prop->value, prop->length) >= prop->length)
  795. return -EILSEQ;
  796. p = prop->value;
  797. for (i = 0; total < prop->length; total += l, p += l, i++)
  798. l = strlen(p) + 1;
  799. return i;
  800. }
  801. EXPORT_SYMBOL_GPL(of_property_count_strings);
  802. /**
  803. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  804. * @np: Pointer to device node holding phandle property
  805. * @phandle_name: Name of property holding a phandle value
  806. * @index: For properties holding a table of phandles, this is the index into
  807. * the table
  808. *
  809. * Returns the device_node pointer with refcount incremented. Use
  810. * of_node_put() on it when done.
  811. */
  812. struct device_node *
  813. of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
  814. {
  815. const __be32 *phandle;
  816. int size;
  817. phandle = of_get_property(np, phandle_name, &size);
  818. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  819. return NULL;
  820. return of_find_node_by_phandle(be32_to_cpup(phandle + index));
  821. }
  822. EXPORT_SYMBOL(of_parse_phandle);
  823. /**
  824. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  825. * @np: pointer to a device tree node containing a list
  826. * @list_name: property name that contains a list
  827. * @cells_name: property name that specifies phandles' arguments count
  828. * @index: index of a phandle to parse out
  829. * @out_args: optional pointer to output arguments structure (will be filled)
  830. *
  831. * This function is useful to parse lists of phandles and their arguments.
  832. * Returns 0 on success and fills out_args, on error returns appropriate
  833. * errno value.
  834. *
  835. * Caller is responsible to call of_node_put() on the returned out_args->node
  836. * pointer.
  837. *
  838. * Example:
  839. *
  840. * phandle1: node1 {
  841. * #list-cells = <2>;
  842. * }
  843. *
  844. * phandle2: node2 {
  845. * #list-cells = <1>;
  846. * }
  847. *
  848. * node3 {
  849. * list = <&phandle1 1 2 &phandle2 3>;
  850. * }
  851. *
  852. * To get a device_node of the `node2' node you may call this:
  853. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  854. */
  855. int of_parse_phandle_with_args(struct device_node *np, const char *list_name,
  856. const char *cells_name, int index,
  857. struct of_phandle_args *out_args)
  858. {
  859. const __be32 *list, *list_end;
  860. int size, cur_index = 0;
  861. uint32_t count = 0;
  862. struct device_node *node = NULL;
  863. phandle phandle;
  864. /* Retrieve the phandle list property */
  865. list = of_get_property(np, list_name, &size);
  866. if (!list)
  867. return -ENOENT;
  868. list_end = list + size / sizeof(*list);
  869. /* Loop over the phandles until all the requested entry is found */
  870. while (list < list_end) {
  871. count = 0;
  872. /*
  873. * If phandle is 0, then it is an empty entry with no
  874. * arguments. Skip forward to the next entry.
  875. */
  876. phandle = be32_to_cpup(list++);
  877. if (phandle) {
  878. /*
  879. * Find the provider node and parse the #*-cells
  880. * property to determine the argument length
  881. */
  882. node = of_find_node_by_phandle(phandle);
  883. if (!node) {
  884. pr_err("%s: could not find phandle\n",
  885. np->full_name);
  886. break;
  887. }
  888. if (of_property_read_u32(node, cells_name, &count)) {
  889. pr_err("%s: could not get %s for %s\n",
  890. np->full_name, cells_name,
  891. node->full_name);
  892. break;
  893. }
  894. /*
  895. * Make sure that the arguments actually fit in the
  896. * remaining property data length
  897. */
  898. if (list + count > list_end) {
  899. pr_err("%s: arguments longer than property\n",
  900. np->full_name);
  901. break;
  902. }
  903. }
  904. /*
  905. * All of the error cases above bail out of the loop, so at
  906. * this point, the parsing is successful. If the requested
  907. * index matches, then fill the out_args structure and return,
  908. * or return -ENOENT for an empty entry.
  909. */
  910. if (cur_index == index) {
  911. if (!phandle)
  912. return -ENOENT;
  913. if (out_args) {
  914. int i;
  915. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  916. count = MAX_PHANDLE_ARGS;
  917. out_args->np = node;
  918. out_args->args_count = count;
  919. for (i = 0; i < count; i++)
  920. out_args->args[i] = be32_to_cpup(list++);
  921. }
  922. return 0;
  923. }
  924. of_node_put(node);
  925. node = NULL;
  926. list += count;
  927. cur_index++;
  928. }
  929. /* Loop exited without finding a valid entry; return an error */
  930. if (node)
  931. of_node_put(node);
  932. return -EINVAL;
  933. }
  934. EXPORT_SYMBOL(of_parse_phandle_with_args);
  935. /**
  936. * prom_add_property - Add a property to a node
  937. */
  938. int prom_add_property(struct device_node *np, struct property *prop)
  939. {
  940. struct property **next;
  941. unsigned long flags;
  942. prop->next = NULL;
  943. write_lock_irqsave(&devtree_lock, flags);
  944. next = &np->properties;
  945. while (*next) {
  946. if (strcmp(prop->name, (*next)->name) == 0) {
  947. /* duplicate ! don't insert it */
  948. write_unlock_irqrestore(&devtree_lock, flags);
  949. return -1;
  950. }
  951. next = &(*next)->next;
  952. }
  953. *next = prop;
  954. write_unlock_irqrestore(&devtree_lock, flags);
  955. #ifdef CONFIG_PROC_DEVICETREE
  956. /* try to add to proc as well if it was initialized */
  957. if (np->pde)
  958. proc_device_tree_add_prop(np->pde, prop);
  959. #endif /* CONFIG_PROC_DEVICETREE */
  960. return 0;
  961. }
  962. /**
  963. * prom_remove_property - Remove a property from a node.
  964. *
  965. * Note that we don't actually remove it, since we have given out
  966. * who-knows-how-many pointers to the data using get-property.
  967. * Instead we just move the property to the "dead properties"
  968. * list, so it won't be found any more.
  969. */
  970. int prom_remove_property(struct device_node *np, struct property *prop)
  971. {
  972. struct property **next;
  973. unsigned long flags;
  974. int found = 0;
  975. write_lock_irqsave(&devtree_lock, flags);
  976. next = &np->properties;
  977. while (*next) {
  978. if (*next == prop) {
  979. /* found the node */
  980. *next = prop->next;
  981. prop->next = np->deadprops;
  982. np->deadprops = prop;
  983. found = 1;
  984. break;
  985. }
  986. next = &(*next)->next;
  987. }
  988. write_unlock_irqrestore(&devtree_lock, flags);
  989. if (!found)
  990. return -ENODEV;
  991. #ifdef CONFIG_PROC_DEVICETREE
  992. /* try to remove the proc node as well */
  993. if (np->pde)
  994. proc_device_tree_remove_prop(np->pde, prop);
  995. #endif /* CONFIG_PROC_DEVICETREE */
  996. return 0;
  997. }
  998. /*
  999. * prom_update_property - Update a property in a node, if the property does
  1000. * not exist, add it.
  1001. *
  1002. * Note that we don't actually remove it, since we have given out
  1003. * who-knows-how-many pointers to the data using get-property.
  1004. * Instead we just move the property to the "dead properties" list,
  1005. * and add the new property to the property list
  1006. */
  1007. int prom_update_property(struct device_node *np,
  1008. struct property *newprop)
  1009. {
  1010. struct property **next, *oldprop;
  1011. unsigned long flags;
  1012. int found = 0;
  1013. if (!newprop->name)
  1014. return -EINVAL;
  1015. oldprop = of_find_property(np, newprop->name, NULL);
  1016. if (!oldprop)
  1017. return prom_add_property(np, newprop);
  1018. write_lock_irqsave(&devtree_lock, flags);
  1019. next = &np->properties;
  1020. while (*next) {
  1021. if (*next == oldprop) {
  1022. /* found the node */
  1023. newprop->next = oldprop->next;
  1024. *next = newprop;
  1025. oldprop->next = np->deadprops;
  1026. np->deadprops = oldprop;
  1027. found = 1;
  1028. break;
  1029. }
  1030. next = &(*next)->next;
  1031. }
  1032. write_unlock_irqrestore(&devtree_lock, flags);
  1033. if (!found)
  1034. return -ENODEV;
  1035. #ifdef CONFIG_PROC_DEVICETREE
  1036. /* try to add to proc as well if it was initialized */
  1037. if (np->pde)
  1038. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1039. #endif /* CONFIG_PROC_DEVICETREE */
  1040. return 0;
  1041. }
  1042. #if defined(CONFIG_OF_DYNAMIC)
  1043. /*
  1044. * Support for dynamic device trees.
  1045. *
  1046. * On some platforms, the device tree can be manipulated at runtime.
  1047. * The routines in this section support adding, removing and changing
  1048. * device tree nodes.
  1049. */
  1050. /**
  1051. * of_attach_node - Plug a device node into the tree and global list.
  1052. */
  1053. void of_attach_node(struct device_node *np)
  1054. {
  1055. unsigned long flags;
  1056. write_lock_irqsave(&devtree_lock, flags);
  1057. np->sibling = np->parent->child;
  1058. np->allnext = allnodes;
  1059. np->parent->child = np;
  1060. allnodes = np;
  1061. write_unlock_irqrestore(&devtree_lock, flags);
  1062. }
  1063. /**
  1064. * of_detach_node - "Unplug" a node from the device tree.
  1065. *
  1066. * The caller must hold a reference to the node. The memory associated with
  1067. * the node is not freed until its refcount goes to zero.
  1068. */
  1069. void of_detach_node(struct device_node *np)
  1070. {
  1071. struct device_node *parent;
  1072. unsigned long flags;
  1073. write_lock_irqsave(&devtree_lock, flags);
  1074. parent = np->parent;
  1075. if (!parent)
  1076. goto out_unlock;
  1077. if (allnodes == np)
  1078. allnodes = np->allnext;
  1079. else {
  1080. struct device_node *prev;
  1081. for (prev = allnodes;
  1082. prev->allnext != np;
  1083. prev = prev->allnext)
  1084. ;
  1085. prev->allnext = np->allnext;
  1086. }
  1087. if (parent->child == np)
  1088. parent->child = np->sibling;
  1089. else {
  1090. struct device_node *prevsib;
  1091. for (prevsib = np->parent->child;
  1092. prevsib->sibling != np;
  1093. prevsib = prevsib->sibling)
  1094. ;
  1095. prevsib->sibling = np->sibling;
  1096. }
  1097. of_node_set_flag(np, OF_DETACHED);
  1098. out_unlock:
  1099. write_unlock_irqrestore(&devtree_lock, flags);
  1100. }
  1101. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1102. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1103. int id, const char *stem, int stem_len)
  1104. {
  1105. ap->np = np;
  1106. ap->id = id;
  1107. strncpy(ap->stem, stem, stem_len);
  1108. ap->stem[stem_len] = 0;
  1109. list_add_tail(&ap->link, &aliases_lookup);
  1110. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1111. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1112. }
  1113. /**
  1114. * of_alias_scan - Scan all properties of 'aliases' node
  1115. *
  1116. * The function scans all the properties of 'aliases' node and populate
  1117. * the the global lookup table with the properties. It returns the
  1118. * number of alias_prop found, or error code in error case.
  1119. *
  1120. * @dt_alloc: An allocator that provides a virtual address to memory
  1121. * for the resulting tree
  1122. */
  1123. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1124. {
  1125. struct property *pp;
  1126. of_chosen = of_find_node_by_path("/chosen");
  1127. if (of_chosen == NULL)
  1128. of_chosen = of_find_node_by_path("/chosen@0");
  1129. of_aliases = of_find_node_by_path("/aliases");
  1130. if (!of_aliases)
  1131. return;
  1132. for_each_property_of_node(of_aliases, pp) {
  1133. const char *start = pp->name;
  1134. const char *end = start + strlen(start);
  1135. struct device_node *np;
  1136. struct alias_prop *ap;
  1137. int id, len;
  1138. /* Skip those we do not want to proceed */
  1139. if (!strcmp(pp->name, "name") ||
  1140. !strcmp(pp->name, "phandle") ||
  1141. !strcmp(pp->name, "linux,phandle"))
  1142. continue;
  1143. np = of_find_node_by_path(pp->value);
  1144. if (!np)
  1145. continue;
  1146. /* walk the alias backwards to extract the id and work out
  1147. * the 'stem' string */
  1148. while (isdigit(*(end-1)) && end > start)
  1149. end--;
  1150. len = end - start;
  1151. if (kstrtoint(end, 10, &id) < 0)
  1152. continue;
  1153. /* Allocate an alias_prop with enough space for the stem */
  1154. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1155. if (!ap)
  1156. continue;
  1157. ap->alias = start;
  1158. of_alias_add(ap, np, id, start, len);
  1159. }
  1160. }
  1161. /**
  1162. * of_alias_get_id - Get alias id for the given device_node
  1163. * @np: Pointer to the given device_node
  1164. * @stem: Alias stem of the given device_node
  1165. *
  1166. * The function travels the lookup table to get alias id for the given
  1167. * device_node and alias stem. It returns the alias id if find it.
  1168. */
  1169. int of_alias_get_id(struct device_node *np, const char *stem)
  1170. {
  1171. struct alias_prop *app;
  1172. int id = -ENODEV;
  1173. mutex_lock(&of_aliases_mutex);
  1174. list_for_each_entry(app, &aliases_lookup, link) {
  1175. if (strcmp(app->stem, stem) != 0)
  1176. continue;
  1177. if (np == app->np) {
  1178. id = app->id;
  1179. break;
  1180. }
  1181. }
  1182. mutex_unlock(&of_aliases_mutex);
  1183. return id;
  1184. }
  1185. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1186. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1187. u32 *pu)
  1188. {
  1189. const void *curv = cur;
  1190. if (!prop)
  1191. return NULL;
  1192. if (!cur) {
  1193. curv = prop->value;
  1194. goto out_val;
  1195. }
  1196. curv += sizeof(*cur);
  1197. if (curv >= prop->value + prop->length)
  1198. return NULL;
  1199. out_val:
  1200. *pu = be32_to_cpup(curv);
  1201. return curv;
  1202. }
  1203. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1204. const char *of_prop_next_string(struct property *prop, const char *cur)
  1205. {
  1206. const void *curv = cur;
  1207. if (!prop)
  1208. return NULL;
  1209. if (!cur)
  1210. return prop->value;
  1211. curv += strlen(cur) + 1;
  1212. if (curv >= prop->value + prop->length)
  1213. return NULL;
  1214. return curv;
  1215. }
  1216. EXPORT_SYMBOL_GPL(of_prop_next_string);