base.c 38 KB

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