base.c 34 KB

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