base.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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 *of_allnodes;
  46. EXPORT_SYMBOL(of_allnodes);
  47. struct device_node *of_chosen;
  48. struct device_node *of_aliases;
  49. static DEFINE_MUTEX(of_aliases_mutex);
  50. /* use when traversing tree through the allnext, child, sibling,
  51. * or parent members of struct device_node.
  52. */
  53. DEFINE_RWLOCK(devtree_lock);
  54. int of_n_addr_cells(struct device_node *np)
  55. {
  56. const __be32 *ip;
  57. do {
  58. if (np->parent)
  59. np = np->parent;
  60. ip = of_get_property(np, "#address-cells", NULL);
  61. if (ip)
  62. return be32_to_cpup(ip);
  63. } while (np->parent);
  64. /* No #address-cells property for the root node */
  65. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  66. }
  67. EXPORT_SYMBOL(of_n_addr_cells);
  68. int of_n_size_cells(struct device_node *np)
  69. {
  70. const __be32 *ip;
  71. do {
  72. if (np->parent)
  73. np = np->parent;
  74. ip = of_get_property(np, "#size-cells", NULL);
  75. if (ip)
  76. return be32_to_cpup(ip);
  77. } while (np->parent);
  78. /* No #size-cells property for the root node */
  79. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  80. }
  81. EXPORT_SYMBOL(of_n_size_cells);
  82. #if defined(CONFIG_OF_DYNAMIC)
  83. /**
  84. * of_node_get - Increment refcount of a node
  85. * @node: Node to inc refcount, NULL is supported to
  86. * simplify writing of callers
  87. *
  88. * Returns node.
  89. */
  90. struct device_node *of_node_get(struct device_node *node)
  91. {
  92. if (node)
  93. kref_get(&node->kref);
  94. return node;
  95. }
  96. EXPORT_SYMBOL(of_node_get);
  97. static inline struct device_node *kref_to_device_node(struct kref *kref)
  98. {
  99. return container_of(kref, struct device_node, kref);
  100. }
  101. /**
  102. * of_node_release - release a dynamically allocated node
  103. * @kref: kref element of the node to be released
  104. *
  105. * In of_node_put() this function is passed to kref_put()
  106. * as the destructor.
  107. */
  108. static void of_node_release(struct kref *kref)
  109. {
  110. struct device_node *node = kref_to_device_node(kref);
  111. struct property *prop = node->properties;
  112. /* We should never be releasing nodes that haven't been detached. */
  113. if (!of_node_check_flag(node, OF_DETACHED)) {
  114. pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
  115. dump_stack();
  116. kref_init(&node->kref);
  117. return;
  118. }
  119. if (!of_node_check_flag(node, OF_DYNAMIC))
  120. return;
  121. while (prop) {
  122. struct property *next = prop->next;
  123. kfree(prop->name);
  124. kfree(prop->value);
  125. kfree(prop);
  126. prop = next;
  127. if (!prop) {
  128. prop = node->deadprops;
  129. node->deadprops = NULL;
  130. }
  131. }
  132. kfree(node->full_name);
  133. kfree(node->data);
  134. kfree(node);
  135. }
  136. /**
  137. * of_node_put - Decrement refcount of a node
  138. * @node: Node to dec refcount, NULL is supported to
  139. * simplify writing of callers
  140. *
  141. */
  142. void of_node_put(struct device_node *node)
  143. {
  144. if (node)
  145. kref_put(&node->kref, of_node_release);
  146. }
  147. EXPORT_SYMBOL(of_node_put);
  148. #endif /* CONFIG_OF_DYNAMIC */
  149. struct property *of_find_property(const struct device_node *np,
  150. const char *name,
  151. int *lenp)
  152. {
  153. struct property *pp;
  154. if (!np)
  155. return NULL;
  156. read_lock(&devtree_lock);
  157. for (pp = np->properties; pp; pp = pp->next) {
  158. if (of_prop_cmp(pp->name, name) == 0) {
  159. if (lenp)
  160. *lenp = pp->length;
  161. break;
  162. }
  163. }
  164. read_unlock(&devtree_lock);
  165. return pp;
  166. }
  167. EXPORT_SYMBOL(of_find_property);
  168. /**
  169. * of_find_all_nodes - Get next node in global list
  170. * @prev: Previous node or NULL to start iteration
  171. * of_node_put() will be called on it
  172. *
  173. * Returns a node pointer with refcount incremented, use
  174. * of_node_put() on it when done.
  175. */
  176. struct device_node *of_find_all_nodes(struct device_node *prev)
  177. {
  178. struct device_node *np;
  179. read_lock(&devtree_lock);
  180. np = prev ? prev->allnext : of_allnodes;
  181. for (; np != NULL; np = np->allnext)
  182. if (of_node_get(np))
  183. break;
  184. of_node_put(prev);
  185. read_unlock(&devtree_lock);
  186. return np;
  187. }
  188. EXPORT_SYMBOL(of_find_all_nodes);
  189. /*
  190. * Find a property with a given name for a given node
  191. * and return the value.
  192. */
  193. const void *of_get_property(const struct device_node *np, const char *name,
  194. int *lenp)
  195. {
  196. struct property *pp = of_find_property(np, name, lenp);
  197. return pp ? pp->value : NULL;
  198. }
  199. EXPORT_SYMBOL(of_get_property);
  200. /** Checks if the given "compat" string matches one of the strings in
  201. * the device's "compatible" property
  202. */
  203. int of_device_is_compatible(const struct device_node *device,
  204. const char *compat)
  205. {
  206. const char* cp;
  207. int cplen, l;
  208. cp = of_get_property(device, "compatible", &cplen);
  209. if (cp == NULL)
  210. return 0;
  211. while (cplen > 0) {
  212. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  213. return 1;
  214. l = strlen(cp) + 1;
  215. cp += l;
  216. cplen -= l;
  217. }
  218. return 0;
  219. }
  220. EXPORT_SYMBOL(of_device_is_compatible);
  221. /**
  222. * of_machine_is_compatible - Test root of device tree for a given compatible value
  223. * @compat: compatible string to look for in root node's compatible property.
  224. *
  225. * Returns true if the root node has the given value in its
  226. * compatible property.
  227. */
  228. int of_machine_is_compatible(const char *compat)
  229. {
  230. struct device_node *root;
  231. int rc = 0;
  232. root = of_find_node_by_path("/");
  233. if (root) {
  234. rc = of_device_is_compatible(root, compat);
  235. of_node_put(root);
  236. }
  237. return rc;
  238. }
  239. EXPORT_SYMBOL(of_machine_is_compatible);
  240. /**
  241. * of_device_is_available - check if a device is available for use
  242. *
  243. * @device: Node to check for availability
  244. *
  245. * Returns 1 if the status property is absent or set to "okay" or "ok",
  246. * 0 otherwise
  247. */
  248. int of_device_is_available(const struct device_node *device)
  249. {
  250. const char *status;
  251. int statlen;
  252. status = of_get_property(device, "status", &statlen);
  253. if (status == NULL)
  254. return 1;
  255. if (statlen > 0) {
  256. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. EXPORT_SYMBOL(of_device_is_available);
  262. /**
  263. * of_get_parent - Get a node's parent if any
  264. * @node: Node to get parent
  265. *
  266. * Returns a node pointer with refcount incremented, use
  267. * of_node_put() on it when done.
  268. */
  269. struct device_node *of_get_parent(const struct device_node *node)
  270. {
  271. struct device_node *np;
  272. if (!node)
  273. return NULL;
  274. read_lock(&devtree_lock);
  275. np = of_node_get(node->parent);
  276. read_unlock(&devtree_lock);
  277. return np;
  278. }
  279. EXPORT_SYMBOL(of_get_parent);
  280. /**
  281. * of_get_next_parent - Iterate to a node's parent
  282. * @node: Node to get parent of
  283. *
  284. * This is like of_get_parent() except that it drops the
  285. * refcount on the passed node, making it suitable for iterating
  286. * through a node's parents.
  287. *
  288. * Returns a node pointer with refcount incremented, use
  289. * of_node_put() on it when done.
  290. */
  291. struct device_node *of_get_next_parent(struct device_node *node)
  292. {
  293. struct device_node *parent;
  294. if (!node)
  295. return NULL;
  296. read_lock(&devtree_lock);
  297. parent = of_node_get(node->parent);
  298. of_node_put(node);
  299. read_unlock(&devtree_lock);
  300. return parent;
  301. }
  302. /**
  303. * of_get_next_child - Iterate a node childs
  304. * @node: parent node
  305. * @prev: previous child of the parent node, or NULL to get first
  306. *
  307. * Returns a node pointer with refcount incremented, use
  308. * of_node_put() on it when done.
  309. */
  310. struct device_node *of_get_next_child(const struct device_node *node,
  311. struct device_node *prev)
  312. {
  313. struct device_node *next;
  314. read_lock(&devtree_lock);
  315. next = prev ? prev->sibling : node->child;
  316. for (; next; next = next->sibling)
  317. if (of_node_get(next))
  318. break;
  319. of_node_put(prev);
  320. read_unlock(&devtree_lock);
  321. return next;
  322. }
  323. EXPORT_SYMBOL(of_get_next_child);
  324. /**
  325. * of_get_next_available_child - Find the next available child node
  326. * @node: parent node
  327. * @prev: previous child of the parent node, or NULL to get first
  328. *
  329. * This function is like of_get_next_child(), except that it
  330. * automatically skips any disabled nodes (i.e. status = "disabled").
  331. */
  332. struct device_node *of_get_next_available_child(const struct device_node *node,
  333. struct device_node *prev)
  334. {
  335. struct device_node *next;
  336. read_lock(&devtree_lock);
  337. next = prev ? prev->sibling : node->child;
  338. for (; next; next = next->sibling) {
  339. if (!of_device_is_available(next))
  340. continue;
  341. if (of_node_get(next))
  342. break;
  343. }
  344. of_node_put(prev);
  345. read_unlock(&devtree_lock);
  346. return next;
  347. }
  348. EXPORT_SYMBOL(of_get_next_available_child);
  349. /**
  350. * of_get_child_by_name - Find the child node by name for a given parent
  351. * @node: parent node
  352. * @name: child name to look for.
  353. *
  354. * This function looks for child node for given matching name
  355. *
  356. * Returns a node pointer if found, with refcount incremented, use
  357. * of_node_put() on it when done.
  358. * Returns NULL if node is not found.
  359. */
  360. struct device_node *of_get_child_by_name(const struct device_node *node,
  361. const char *name)
  362. {
  363. struct device_node *child;
  364. for_each_child_of_node(node, child)
  365. if (child->name && (of_node_cmp(child->name, name) == 0))
  366. break;
  367. return child;
  368. }
  369. EXPORT_SYMBOL(of_get_child_by_name);
  370. /**
  371. * of_find_node_by_path - Find a node matching a full OF path
  372. * @path: The full path to match
  373. *
  374. * Returns a node pointer with refcount incremented, use
  375. * of_node_put() on it when done.
  376. */
  377. struct device_node *of_find_node_by_path(const char *path)
  378. {
  379. struct device_node *np = of_allnodes;
  380. read_lock(&devtree_lock);
  381. for (; np; np = np->allnext) {
  382. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  383. && of_node_get(np))
  384. break;
  385. }
  386. read_unlock(&devtree_lock);
  387. return np;
  388. }
  389. EXPORT_SYMBOL(of_find_node_by_path);
  390. /**
  391. * of_find_node_by_name - Find a node by its "name" property
  392. * @from: The node to start searching from or NULL, the node
  393. * you pass will not be searched, only the next one
  394. * will; typically, you pass what the previous call
  395. * returned. of_node_put() will be called on it
  396. * @name: The name string to match against
  397. *
  398. * Returns a node pointer with refcount incremented, use
  399. * of_node_put() on it when done.
  400. */
  401. struct device_node *of_find_node_by_name(struct device_node *from,
  402. const char *name)
  403. {
  404. struct device_node *np;
  405. read_lock(&devtree_lock);
  406. np = from ? from->allnext : of_allnodes;
  407. for (; np; np = np->allnext)
  408. if (np->name && (of_node_cmp(np->name, name) == 0)
  409. && of_node_get(np))
  410. break;
  411. of_node_put(from);
  412. read_unlock(&devtree_lock);
  413. return np;
  414. }
  415. EXPORT_SYMBOL(of_find_node_by_name);
  416. /**
  417. * of_find_node_by_type - Find a node by its "device_type" property
  418. * @from: The node to start searching from, or NULL to start searching
  419. * the entire device tree. The node you pass will not be
  420. * searched, only the next one will; typically, you pass
  421. * what the previous call returned. of_node_put() will be
  422. * called on from for you.
  423. * @type: The type string to match against
  424. *
  425. * Returns a node pointer with refcount incremented, use
  426. * of_node_put() on it when done.
  427. */
  428. struct device_node *of_find_node_by_type(struct device_node *from,
  429. const char *type)
  430. {
  431. struct device_node *np;
  432. read_lock(&devtree_lock);
  433. np = from ? from->allnext : of_allnodes;
  434. for (; np; np = np->allnext)
  435. if (np->type && (of_node_cmp(np->type, type) == 0)
  436. && of_node_get(np))
  437. break;
  438. of_node_put(from);
  439. read_unlock(&devtree_lock);
  440. return np;
  441. }
  442. EXPORT_SYMBOL(of_find_node_by_type);
  443. /**
  444. * of_find_compatible_node - Find a node based on type and one of the
  445. * tokens in its "compatible" property
  446. * @from: The node to start searching from or NULL, the node
  447. * you pass will not be searched, only the next one
  448. * will; typically, you pass what the previous call
  449. * returned. of_node_put() will be called on it
  450. * @type: The type string to match "device_type" or NULL to ignore
  451. * @compatible: The string to match to one of the tokens in the device
  452. * "compatible" list.
  453. *
  454. * Returns a node pointer with refcount incremented, use
  455. * of_node_put() on it when done.
  456. */
  457. struct device_node *of_find_compatible_node(struct device_node *from,
  458. const char *type, const char *compatible)
  459. {
  460. struct device_node *np;
  461. read_lock(&devtree_lock);
  462. np = from ? from->allnext : of_allnodes;
  463. for (; np; np = np->allnext) {
  464. if (type
  465. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  466. continue;
  467. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  468. break;
  469. }
  470. of_node_put(from);
  471. read_unlock(&devtree_lock);
  472. return np;
  473. }
  474. EXPORT_SYMBOL(of_find_compatible_node);
  475. /**
  476. * of_find_node_with_property - Find a node which has a property with
  477. * the given name.
  478. * @from: The node to start searching from or NULL, the node
  479. * you pass will not be searched, only the next one
  480. * will; typically, you pass what the previous call
  481. * returned. of_node_put() will be called on it
  482. * @prop_name: The name of the property to look for.
  483. *
  484. * Returns a node pointer with refcount incremented, use
  485. * of_node_put() on it when done.
  486. */
  487. struct device_node *of_find_node_with_property(struct device_node *from,
  488. const char *prop_name)
  489. {
  490. struct device_node *np;
  491. struct property *pp;
  492. read_lock(&devtree_lock);
  493. np = from ? from->allnext : of_allnodes;
  494. for (; np; np = np->allnext) {
  495. for (pp = np->properties; pp; pp = pp->next) {
  496. if (of_prop_cmp(pp->name, prop_name) == 0) {
  497. of_node_get(np);
  498. goto out;
  499. }
  500. }
  501. }
  502. out:
  503. of_node_put(from);
  504. read_unlock(&devtree_lock);
  505. return np;
  506. }
  507. EXPORT_SYMBOL(of_find_node_with_property);
  508. /**
  509. * of_match_node - Tell if an device_node has a matching of_match structure
  510. * @matches: array of of device match structures to search in
  511. * @node: the of device structure to match against
  512. *
  513. * Low level utility function used by device matching.
  514. */
  515. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  516. const struct device_node *node)
  517. {
  518. if (!matches)
  519. return NULL;
  520. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  521. int match = 1;
  522. if (matches->name[0])
  523. match &= node->name
  524. && !strcmp(matches->name, node->name);
  525. if (matches->type[0])
  526. match &= node->type
  527. && !strcmp(matches->type, node->type);
  528. if (matches->compatible[0])
  529. match &= of_device_is_compatible(node,
  530. matches->compatible);
  531. if (match)
  532. return matches;
  533. matches++;
  534. }
  535. return NULL;
  536. }
  537. EXPORT_SYMBOL(of_match_node);
  538. /**
  539. * of_find_matching_node_and_match - Find a node based on an of_device_id
  540. * match table.
  541. * @from: The node to start searching from or NULL, the node
  542. * you pass will not be searched, only the next one
  543. * will; typically, you pass what the previous call
  544. * returned. of_node_put() will be called on it
  545. * @matches: array of of device match structures to search in
  546. * @match Updated to point at the matches entry which matched
  547. *
  548. * Returns a node pointer with refcount incremented, use
  549. * of_node_put() on it when done.
  550. */
  551. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  552. const struct of_device_id *matches,
  553. const struct of_device_id **match)
  554. {
  555. struct device_node *np;
  556. if (match)
  557. *match = NULL;
  558. read_lock(&devtree_lock);
  559. np = from ? from->allnext : of_allnodes;
  560. for (; np; np = np->allnext) {
  561. if (of_match_node(matches, np) && of_node_get(np)) {
  562. if (match)
  563. *match = matches;
  564. break;
  565. }
  566. }
  567. of_node_put(from);
  568. read_unlock(&devtree_lock);
  569. return np;
  570. }
  571. EXPORT_SYMBOL(of_find_matching_node);
  572. /**
  573. * of_modalias_node - Lookup appropriate modalias for a device node
  574. * @node: pointer to a device tree node
  575. * @modalias: Pointer to buffer that modalias value will be copied into
  576. * @len: Length of modalias value
  577. *
  578. * Based on the value of the compatible property, this routine will attempt
  579. * to choose an appropriate modalias value for a particular device tree node.
  580. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  581. * from the first entry in the compatible list property.
  582. *
  583. * This routine returns 0 on success, <0 on failure.
  584. */
  585. int of_modalias_node(struct device_node *node, char *modalias, int len)
  586. {
  587. const char *compatible, *p;
  588. int cplen;
  589. compatible = of_get_property(node, "compatible", &cplen);
  590. if (!compatible || strlen(compatible) > cplen)
  591. return -ENODEV;
  592. p = strchr(compatible, ',');
  593. strlcpy(modalias, p ? p + 1 : compatible, len);
  594. return 0;
  595. }
  596. EXPORT_SYMBOL_GPL(of_modalias_node);
  597. /**
  598. * of_find_node_by_phandle - Find a node given a phandle
  599. * @handle: phandle of the node to find
  600. *
  601. * Returns a node pointer with refcount incremented, use
  602. * of_node_put() on it when done.
  603. */
  604. struct device_node *of_find_node_by_phandle(phandle handle)
  605. {
  606. struct device_node *np;
  607. read_lock(&devtree_lock);
  608. for (np = of_allnodes; np; np = np->allnext)
  609. if (np->phandle == handle)
  610. break;
  611. of_node_get(np);
  612. read_unlock(&devtree_lock);
  613. return np;
  614. }
  615. EXPORT_SYMBOL(of_find_node_by_phandle);
  616. /**
  617. * of_property_read_u8_array - Find and read an array of u8 from a property.
  618. *
  619. * @np: device node from which the property value is to be read.
  620. * @propname: name of the property to be searched.
  621. * @out_value: pointer to return value, modified only if return value is 0.
  622. * @sz: number of array elements to read
  623. *
  624. * Search for a property in a device node and read 8-bit value(s) from
  625. * it. Returns 0 on success, -EINVAL if the property does not exist,
  626. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  627. * property data isn't large enough.
  628. *
  629. * dts entry of array should be like:
  630. * property = /bits/ 8 <0x50 0x60 0x70>;
  631. *
  632. * The out_value is modified only if a valid u8 value can be decoded.
  633. */
  634. int of_property_read_u8_array(const struct device_node *np,
  635. const char *propname, u8 *out_values, size_t sz)
  636. {
  637. struct property *prop = of_find_property(np, propname, NULL);
  638. const u8 *val;
  639. if (!prop)
  640. return -EINVAL;
  641. if (!prop->value)
  642. return -ENODATA;
  643. if ((sz * sizeof(*out_values)) > prop->length)
  644. return -EOVERFLOW;
  645. val = prop->value;
  646. while (sz--)
  647. *out_values++ = *val++;
  648. return 0;
  649. }
  650. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  651. /**
  652. * of_property_read_u16_array - Find and read an array of u16 from a property.
  653. *
  654. * @np: device node from which the property value is to be read.
  655. * @propname: name of the property to be searched.
  656. * @out_value: pointer to return value, modified only if return value is 0.
  657. * @sz: number of array elements to read
  658. *
  659. * Search for a property in a device node and read 16-bit value(s) from
  660. * it. Returns 0 on success, -EINVAL if the property does not exist,
  661. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  662. * property data isn't large enough.
  663. *
  664. * dts entry of array should be like:
  665. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  666. *
  667. * The out_value is modified only if a valid u16 value can be decoded.
  668. */
  669. int of_property_read_u16_array(const struct device_node *np,
  670. const char *propname, u16 *out_values, size_t sz)
  671. {
  672. struct property *prop = of_find_property(np, propname, NULL);
  673. const __be16 *val;
  674. if (!prop)
  675. return -EINVAL;
  676. if (!prop->value)
  677. return -ENODATA;
  678. if ((sz * sizeof(*out_values)) > prop->length)
  679. return -EOVERFLOW;
  680. val = prop->value;
  681. while (sz--)
  682. *out_values++ = be16_to_cpup(val++);
  683. return 0;
  684. }
  685. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  686. /**
  687. * of_property_read_u32_array - Find and read an array of 32 bit integers
  688. * from a property.
  689. *
  690. * @np: device node from which the property value is to be read.
  691. * @propname: name of the property to be searched.
  692. * @out_value: pointer to return value, modified only if return value is 0.
  693. * @sz: number of array elements to read
  694. *
  695. * Search for a property in a device node and read 32-bit value(s) from
  696. * it. Returns 0 on success, -EINVAL if the property does not exist,
  697. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  698. * property data isn't large enough.
  699. *
  700. * The out_value is modified only if a valid u32 value can be decoded.
  701. */
  702. int of_property_read_u32_array(const struct device_node *np,
  703. const char *propname, u32 *out_values,
  704. size_t sz)
  705. {
  706. struct property *prop = of_find_property(np, propname, NULL);
  707. const __be32 *val;
  708. if (!prop)
  709. return -EINVAL;
  710. if (!prop->value)
  711. return -ENODATA;
  712. if ((sz * sizeof(*out_values)) > prop->length)
  713. return -EOVERFLOW;
  714. val = prop->value;
  715. while (sz--)
  716. *out_values++ = be32_to_cpup(val++);
  717. return 0;
  718. }
  719. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  720. /**
  721. * of_property_read_u64 - Find and read a 64 bit integer from a property
  722. * @np: device node from which the property value is to be read.
  723. * @propname: name of the property to be searched.
  724. * @out_value: pointer to return value, modified only if return value is 0.
  725. *
  726. * Search for a property in a device node and read a 64-bit value from
  727. * it. Returns 0 on success, -EINVAL if the property does not exist,
  728. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  729. * property data isn't large enough.
  730. *
  731. * The out_value is modified only if a valid u64 value can be decoded.
  732. */
  733. int of_property_read_u64(const struct device_node *np, const char *propname,
  734. u64 *out_value)
  735. {
  736. struct property *prop = of_find_property(np, propname, NULL);
  737. if (!prop)
  738. return -EINVAL;
  739. if (!prop->value)
  740. return -ENODATA;
  741. if (sizeof(*out_value) > prop->length)
  742. return -EOVERFLOW;
  743. *out_value = of_read_number(prop->value, 2);
  744. return 0;
  745. }
  746. EXPORT_SYMBOL_GPL(of_property_read_u64);
  747. /**
  748. * of_property_read_string - Find and read a string from a property
  749. * @np: device node from which the property value is to be read.
  750. * @propname: name of the property to be searched.
  751. * @out_string: pointer to null terminated return string, modified only if
  752. * return value is 0.
  753. *
  754. * Search for a property in a device tree node and retrieve a null
  755. * terminated string value (pointer to data, not a copy). Returns 0 on
  756. * success, -EINVAL if the property does not exist, -ENODATA if property
  757. * does not have a value, and -EILSEQ if the string is not null-terminated
  758. * within the length of the property data.
  759. *
  760. * The out_string pointer is modified only if a valid string can be decoded.
  761. */
  762. int of_property_read_string(struct device_node *np, const char *propname,
  763. const char **out_string)
  764. {
  765. struct property *prop = of_find_property(np, propname, NULL);
  766. if (!prop)
  767. return -EINVAL;
  768. if (!prop->value)
  769. return -ENODATA;
  770. if (strnlen(prop->value, prop->length) >= prop->length)
  771. return -EILSEQ;
  772. *out_string = prop->value;
  773. return 0;
  774. }
  775. EXPORT_SYMBOL_GPL(of_property_read_string);
  776. /**
  777. * of_property_read_string_index - Find and read a string from a multiple
  778. * strings property.
  779. * @np: device node from which the property value is to be read.
  780. * @propname: name of the property to be searched.
  781. * @index: index of the string in the list of strings
  782. * @out_string: pointer to null terminated return string, modified only if
  783. * return value is 0.
  784. *
  785. * Search for a property in a device tree node and retrieve a null
  786. * terminated string value (pointer to data, not a copy) in the list of strings
  787. * contained in that property.
  788. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  789. * property does not have a value, and -EILSEQ if the string is not
  790. * null-terminated within the length of the property data.
  791. *
  792. * The out_string pointer is modified only if a valid string can be decoded.
  793. */
  794. int of_property_read_string_index(struct device_node *np, const char *propname,
  795. int index, const char **output)
  796. {
  797. struct property *prop = of_find_property(np, propname, NULL);
  798. int i = 0;
  799. size_t l = 0, total = 0;
  800. const char *p;
  801. if (!prop)
  802. return -EINVAL;
  803. if (!prop->value)
  804. return -ENODATA;
  805. if (strnlen(prop->value, prop->length) >= prop->length)
  806. return -EILSEQ;
  807. p = prop->value;
  808. for (i = 0; total < prop->length; total += l, p += l) {
  809. l = strlen(p) + 1;
  810. if (i++ == index) {
  811. *output = p;
  812. return 0;
  813. }
  814. }
  815. return -ENODATA;
  816. }
  817. EXPORT_SYMBOL_GPL(of_property_read_string_index);
  818. /**
  819. * of_property_match_string() - Find string in a list and return index
  820. * @np: pointer to node containing string list property
  821. * @propname: string list property name
  822. * @string: pointer to string to search for in string list
  823. *
  824. * This function searches a string list property and returns the index
  825. * of a specific string value.
  826. */
  827. int of_property_match_string(struct device_node *np, const char *propname,
  828. const char *string)
  829. {
  830. struct property *prop = of_find_property(np, propname, NULL);
  831. size_t l;
  832. int i;
  833. const char *p, *end;
  834. if (!prop)
  835. return -EINVAL;
  836. if (!prop->value)
  837. return -ENODATA;
  838. p = prop->value;
  839. end = p + prop->length;
  840. for (i = 0; p < end; i++, p += l) {
  841. l = strlen(p) + 1;
  842. if (p + l > end)
  843. return -EILSEQ;
  844. pr_debug("comparing %s with %s\n", string, p);
  845. if (strcmp(string, p) == 0)
  846. return i; /* Found it; return index */
  847. }
  848. return -ENODATA;
  849. }
  850. EXPORT_SYMBOL_GPL(of_property_match_string);
  851. /**
  852. * of_property_count_strings - Find and return the number of strings from a
  853. * multiple strings property.
  854. * @np: device node from which the property value is to be read.
  855. * @propname: name of the property to be searched.
  856. *
  857. * Search for a property in a device tree node and retrieve the number of null
  858. * terminated string contain in it. Returns the number of strings on
  859. * success, -EINVAL if the property does not exist, -ENODATA if property
  860. * does not have a value, and -EILSEQ if the string is not null-terminated
  861. * within the length of the property data.
  862. */
  863. int of_property_count_strings(struct device_node *np, const char *propname)
  864. {
  865. struct property *prop = of_find_property(np, propname, NULL);
  866. int i = 0;
  867. size_t l = 0, total = 0;
  868. const char *p;
  869. if (!prop)
  870. return -EINVAL;
  871. if (!prop->value)
  872. return -ENODATA;
  873. if (strnlen(prop->value, prop->length) >= prop->length)
  874. return -EILSEQ;
  875. p = prop->value;
  876. for (i = 0; total < prop->length; total += l, p += l, i++)
  877. l = strlen(p) + 1;
  878. return i;
  879. }
  880. EXPORT_SYMBOL_GPL(of_property_count_strings);
  881. /**
  882. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  883. * @np: Pointer to device node holding phandle property
  884. * @phandle_name: Name of property holding a phandle value
  885. * @index: For properties holding a table of phandles, this is the index into
  886. * the table
  887. *
  888. * Returns the device_node pointer with refcount incremented. Use
  889. * of_node_put() on it when done.
  890. */
  891. struct device_node *of_parse_phandle(const struct device_node *np,
  892. const char *phandle_name, int index)
  893. {
  894. const __be32 *phandle;
  895. int size;
  896. phandle = of_get_property(np, phandle_name, &size);
  897. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  898. return NULL;
  899. return of_find_node_by_phandle(be32_to_cpup(phandle + index));
  900. }
  901. EXPORT_SYMBOL(of_parse_phandle);
  902. /**
  903. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  904. * @np: pointer to a device tree node containing a list
  905. * @list_name: property name that contains a list
  906. * @cells_name: property name that specifies phandles' arguments count
  907. * @index: index of a phandle to parse out
  908. * @out_args: optional pointer to output arguments structure (will be filled)
  909. *
  910. * This function is useful to parse lists of phandles and their arguments.
  911. * Returns 0 on success and fills out_args, on error returns appropriate
  912. * errno value.
  913. *
  914. * Caller is responsible to call of_node_put() on the returned out_args->node
  915. * pointer.
  916. *
  917. * Example:
  918. *
  919. * phandle1: node1 {
  920. * #list-cells = <2>;
  921. * }
  922. *
  923. * phandle2: node2 {
  924. * #list-cells = <1>;
  925. * }
  926. *
  927. * node3 {
  928. * list = <&phandle1 1 2 &phandle2 3>;
  929. * }
  930. *
  931. * To get a device_node of the `node2' node you may call this:
  932. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  933. */
  934. int of_parse_phandle_with_args(struct device_node *np, const char *list_name,
  935. const char *cells_name, int index,
  936. struct of_phandle_args *out_args)
  937. {
  938. const __be32 *list, *list_end;
  939. int size, cur_index = 0;
  940. uint32_t count = 0;
  941. struct device_node *node = NULL;
  942. phandle phandle;
  943. /* Retrieve the phandle list property */
  944. list = of_get_property(np, list_name, &size);
  945. if (!list)
  946. return -ENOENT;
  947. list_end = list + size / sizeof(*list);
  948. /* Loop over the phandles until all the requested entry is found */
  949. while (list < list_end) {
  950. count = 0;
  951. /*
  952. * If phandle is 0, then it is an empty entry with no
  953. * arguments. Skip forward to the next entry.
  954. */
  955. phandle = be32_to_cpup(list++);
  956. if (phandle) {
  957. /*
  958. * Find the provider node and parse the #*-cells
  959. * property to determine the argument length
  960. */
  961. node = of_find_node_by_phandle(phandle);
  962. if (!node) {
  963. pr_err("%s: could not find phandle\n",
  964. np->full_name);
  965. break;
  966. }
  967. if (of_property_read_u32(node, cells_name, &count)) {
  968. pr_err("%s: could not get %s for %s\n",
  969. np->full_name, cells_name,
  970. node->full_name);
  971. break;
  972. }
  973. /*
  974. * Make sure that the arguments actually fit in the
  975. * remaining property data length
  976. */
  977. if (list + count > list_end) {
  978. pr_err("%s: arguments longer than property\n",
  979. np->full_name);
  980. break;
  981. }
  982. }
  983. /*
  984. * All of the error cases above bail out of the loop, so at
  985. * this point, the parsing is successful. If the requested
  986. * index matches, then fill the out_args structure and return,
  987. * or return -ENOENT for an empty entry.
  988. */
  989. if (cur_index == index) {
  990. if (!phandle)
  991. return -ENOENT;
  992. if (out_args) {
  993. int i;
  994. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  995. count = MAX_PHANDLE_ARGS;
  996. out_args->np = node;
  997. out_args->args_count = count;
  998. for (i = 0; i < count; i++)
  999. out_args->args[i] = be32_to_cpup(list++);
  1000. }
  1001. return 0;
  1002. }
  1003. of_node_put(node);
  1004. node = NULL;
  1005. list += count;
  1006. cur_index++;
  1007. }
  1008. /* Loop exited without finding a valid entry; return an error */
  1009. if (node)
  1010. of_node_put(node);
  1011. return -EINVAL;
  1012. }
  1013. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1014. /**
  1015. * prom_add_property - Add a property to a node
  1016. */
  1017. int prom_add_property(struct device_node *np, struct property *prop)
  1018. {
  1019. struct property **next;
  1020. unsigned long flags;
  1021. prop->next = NULL;
  1022. write_lock_irqsave(&devtree_lock, flags);
  1023. next = &np->properties;
  1024. while (*next) {
  1025. if (strcmp(prop->name, (*next)->name) == 0) {
  1026. /* duplicate ! don't insert it */
  1027. write_unlock_irqrestore(&devtree_lock, flags);
  1028. return -1;
  1029. }
  1030. next = &(*next)->next;
  1031. }
  1032. *next = prop;
  1033. write_unlock_irqrestore(&devtree_lock, flags);
  1034. #ifdef CONFIG_PROC_DEVICETREE
  1035. /* try to add to proc as well if it was initialized */
  1036. if (np->pde)
  1037. proc_device_tree_add_prop(np->pde, prop);
  1038. #endif /* CONFIG_PROC_DEVICETREE */
  1039. return 0;
  1040. }
  1041. /**
  1042. * prom_remove_property - Remove a property from a node.
  1043. *
  1044. * Note that we don't actually remove it, since we have given out
  1045. * who-knows-how-many pointers to the data using get-property.
  1046. * Instead we just move the property to the "dead properties"
  1047. * list, so it won't be found any more.
  1048. */
  1049. int prom_remove_property(struct device_node *np, struct property *prop)
  1050. {
  1051. struct property **next;
  1052. unsigned long flags;
  1053. int found = 0;
  1054. write_lock_irqsave(&devtree_lock, flags);
  1055. next = &np->properties;
  1056. while (*next) {
  1057. if (*next == prop) {
  1058. /* found the node */
  1059. *next = prop->next;
  1060. prop->next = np->deadprops;
  1061. np->deadprops = prop;
  1062. found = 1;
  1063. break;
  1064. }
  1065. next = &(*next)->next;
  1066. }
  1067. write_unlock_irqrestore(&devtree_lock, flags);
  1068. if (!found)
  1069. return -ENODEV;
  1070. #ifdef CONFIG_PROC_DEVICETREE
  1071. /* try to remove the proc node as well */
  1072. if (np->pde)
  1073. proc_device_tree_remove_prop(np->pde, prop);
  1074. #endif /* CONFIG_PROC_DEVICETREE */
  1075. return 0;
  1076. }
  1077. /*
  1078. * prom_update_property - Update a property in a node, if the property does
  1079. * not exist, add it.
  1080. *
  1081. * Note that we don't actually remove it, since we have given out
  1082. * who-knows-how-many pointers to the data using get-property.
  1083. * Instead we just move the property to the "dead properties" list,
  1084. * and add the new property to the property list
  1085. */
  1086. int prom_update_property(struct device_node *np,
  1087. struct property *newprop)
  1088. {
  1089. struct property **next, *oldprop;
  1090. unsigned long flags;
  1091. int found = 0;
  1092. if (!newprop->name)
  1093. return -EINVAL;
  1094. oldprop = of_find_property(np, newprop->name, NULL);
  1095. if (!oldprop)
  1096. return prom_add_property(np, newprop);
  1097. write_lock_irqsave(&devtree_lock, flags);
  1098. next = &np->properties;
  1099. while (*next) {
  1100. if (*next == oldprop) {
  1101. /* found the node */
  1102. newprop->next = oldprop->next;
  1103. *next = newprop;
  1104. oldprop->next = np->deadprops;
  1105. np->deadprops = oldprop;
  1106. found = 1;
  1107. break;
  1108. }
  1109. next = &(*next)->next;
  1110. }
  1111. write_unlock_irqrestore(&devtree_lock, flags);
  1112. if (!found)
  1113. return -ENODEV;
  1114. #ifdef CONFIG_PROC_DEVICETREE
  1115. /* try to add to proc as well if it was initialized */
  1116. if (np->pde)
  1117. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1118. #endif /* CONFIG_PROC_DEVICETREE */
  1119. return 0;
  1120. }
  1121. #if defined(CONFIG_OF_DYNAMIC)
  1122. /*
  1123. * Support for dynamic device trees.
  1124. *
  1125. * On some platforms, the device tree can be manipulated at runtime.
  1126. * The routines in this section support adding, removing and changing
  1127. * device tree nodes.
  1128. */
  1129. /**
  1130. * of_attach_node - Plug a device node into the tree and global list.
  1131. */
  1132. void of_attach_node(struct device_node *np)
  1133. {
  1134. unsigned long flags;
  1135. write_lock_irqsave(&devtree_lock, flags);
  1136. np->sibling = np->parent->child;
  1137. np->allnext = of_allnodes;
  1138. np->parent->child = np;
  1139. of_allnodes = np;
  1140. write_unlock_irqrestore(&devtree_lock, flags);
  1141. }
  1142. /**
  1143. * of_detach_node - "Unplug" a node from the device tree.
  1144. *
  1145. * The caller must hold a reference to the node. The memory associated with
  1146. * the node is not freed until its refcount goes to zero.
  1147. */
  1148. void of_detach_node(struct device_node *np)
  1149. {
  1150. struct device_node *parent;
  1151. unsigned long flags;
  1152. write_lock_irqsave(&devtree_lock, flags);
  1153. parent = np->parent;
  1154. if (!parent)
  1155. goto out_unlock;
  1156. if (of_allnodes == np)
  1157. of_allnodes = np->allnext;
  1158. else {
  1159. struct device_node *prev;
  1160. for (prev = of_allnodes;
  1161. prev->allnext != np;
  1162. prev = prev->allnext)
  1163. ;
  1164. prev->allnext = np->allnext;
  1165. }
  1166. if (parent->child == np)
  1167. parent->child = np->sibling;
  1168. else {
  1169. struct device_node *prevsib;
  1170. for (prevsib = np->parent->child;
  1171. prevsib->sibling != np;
  1172. prevsib = prevsib->sibling)
  1173. ;
  1174. prevsib->sibling = np->sibling;
  1175. }
  1176. of_node_set_flag(np, OF_DETACHED);
  1177. out_unlock:
  1178. write_unlock_irqrestore(&devtree_lock, flags);
  1179. }
  1180. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1181. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1182. int id, const char *stem, int stem_len)
  1183. {
  1184. ap->np = np;
  1185. ap->id = id;
  1186. strncpy(ap->stem, stem, stem_len);
  1187. ap->stem[stem_len] = 0;
  1188. list_add_tail(&ap->link, &aliases_lookup);
  1189. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1190. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1191. }
  1192. /**
  1193. * of_alias_scan - Scan all properties of 'aliases' node
  1194. *
  1195. * The function scans all the properties of 'aliases' node and populate
  1196. * the the global lookup table with the properties. It returns the
  1197. * number of alias_prop found, or error code in error case.
  1198. *
  1199. * @dt_alloc: An allocator that provides a virtual address to memory
  1200. * for the resulting tree
  1201. */
  1202. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1203. {
  1204. struct property *pp;
  1205. of_chosen = of_find_node_by_path("/chosen");
  1206. if (of_chosen == NULL)
  1207. of_chosen = of_find_node_by_path("/chosen@0");
  1208. of_aliases = of_find_node_by_path("/aliases");
  1209. if (!of_aliases)
  1210. return;
  1211. for_each_property_of_node(of_aliases, pp) {
  1212. const char *start = pp->name;
  1213. const char *end = start + strlen(start);
  1214. struct device_node *np;
  1215. struct alias_prop *ap;
  1216. int id, len;
  1217. /* Skip those we do not want to proceed */
  1218. if (!strcmp(pp->name, "name") ||
  1219. !strcmp(pp->name, "phandle") ||
  1220. !strcmp(pp->name, "linux,phandle"))
  1221. continue;
  1222. np = of_find_node_by_path(pp->value);
  1223. if (!np)
  1224. continue;
  1225. /* walk the alias backwards to extract the id and work out
  1226. * the 'stem' string */
  1227. while (isdigit(*(end-1)) && end > start)
  1228. end--;
  1229. len = end - start;
  1230. if (kstrtoint(end, 10, &id) < 0)
  1231. continue;
  1232. /* Allocate an alias_prop with enough space for the stem */
  1233. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1234. if (!ap)
  1235. continue;
  1236. ap->alias = start;
  1237. of_alias_add(ap, np, id, start, len);
  1238. }
  1239. }
  1240. /**
  1241. * of_alias_get_id - Get alias id for the given device_node
  1242. * @np: Pointer to the given device_node
  1243. * @stem: Alias stem of the given device_node
  1244. *
  1245. * The function travels the lookup table to get alias id for the given
  1246. * device_node and alias stem. It returns the alias id if find it.
  1247. */
  1248. int of_alias_get_id(struct device_node *np, const char *stem)
  1249. {
  1250. struct alias_prop *app;
  1251. int id = -ENODEV;
  1252. mutex_lock(&of_aliases_mutex);
  1253. list_for_each_entry(app, &aliases_lookup, link) {
  1254. if (strcmp(app->stem, stem) != 0)
  1255. continue;
  1256. if (np == app->np) {
  1257. id = app->id;
  1258. break;
  1259. }
  1260. }
  1261. mutex_unlock(&of_aliases_mutex);
  1262. return id;
  1263. }
  1264. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1265. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1266. u32 *pu)
  1267. {
  1268. const void *curv = cur;
  1269. if (!prop)
  1270. return NULL;
  1271. if (!cur) {
  1272. curv = prop->value;
  1273. goto out_val;
  1274. }
  1275. curv += sizeof(*cur);
  1276. if (curv >= prop->value + prop->length)
  1277. return NULL;
  1278. out_val:
  1279. *pu = be32_to_cpup(curv);
  1280. return curv;
  1281. }
  1282. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1283. const char *of_prop_next_string(struct property *prop, const char *cur)
  1284. {
  1285. const void *curv = cur;
  1286. if (!prop)
  1287. return NULL;
  1288. if (!cur)
  1289. return prop->value;
  1290. curv += strlen(cur) + 1;
  1291. if (curv >= prop->value + prop->length)
  1292. return NULL;
  1293. return curv;
  1294. }
  1295. EXPORT_SYMBOL_GPL(of_prop_next_string);