base.c 34 KB

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