of_access.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Originally from Linux v4.9
  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. * Modified for U-Boot
  16. * Copyright (c) 2017 Google, Inc
  17. *
  18. * This file follows drivers/of/base.c with functions in the same order as the
  19. * Linux version.
  20. */
  21. #include <common.h>
  22. #include <log.h>
  23. #include <malloc.h>
  24. #include <asm/global_data.h>
  25. #include <linux/bug.h>
  26. #include <linux/libfdt.h>
  27. #include <dm/of_access.h>
  28. #include <linux/ctype.h>
  29. #include <linux/err.h>
  30. #include <linux/ioport.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. /* list of struct alias_prop aliases */
  33. LIST_HEAD(aliases_lookup);
  34. /* "/aliaes" node */
  35. static struct device_node *of_aliases;
  36. /* "/chosen" node */
  37. static struct device_node *of_chosen;
  38. /* node pointed to by the stdout-path alias */
  39. static struct device_node *of_stdout;
  40. /* pointer to options given after the alias (separated by :) or NULL if none */
  41. static const char *of_stdout_options;
  42. /**
  43. * struct alias_prop - Alias property in 'aliases' node
  44. *
  45. * The structure represents one alias property of 'aliases' node as
  46. * an entry in aliases_lookup list.
  47. *
  48. * @link: List node to link the structure in aliases_lookup list
  49. * @alias: Alias property name
  50. * @np: Pointer to device_node that the alias stands for
  51. * @id: Index value from end of alias name
  52. * @stem: Alias string without the index
  53. */
  54. struct alias_prop {
  55. struct list_head link;
  56. const char *alias;
  57. struct device_node *np;
  58. int id;
  59. char stem[0];
  60. };
  61. int of_n_addr_cells(const struct device_node *np)
  62. {
  63. const __be32 *ip;
  64. do {
  65. if (np->parent)
  66. np = np->parent;
  67. ip = of_get_property(np, "#address-cells", NULL);
  68. if (ip)
  69. return be32_to_cpup(ip);
  70. } while (np->parent);
  71. /* No #address-cells property for the root node */
  72. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  73. }
  74. int of_n_size_cells(const struct device_node *np)
  75. {
  76. const __be32 *ip;
  77. do {
  78. if (np->parent)
  79. np = np->parent;
  80. ip = of_get_property(np, "#size-cells", NULL);
  81. if (ip)
  82. return be32_to_cpup(ip);
  83. } while (np->parent);
  84. /* No #size-cells property for the root node */
  85. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  86. }
  87. int of_simple_addr_cells(const struct device_node *np)
  88. {
  89. const __be32 *ip;
  90. ip = of_get_property(np, "#address-cells", NULL);
  91. if (ip)
  92. return be32_to_cpup(ip);
  93. /* Return a default of 2 to match fdt_address_cells()*/
  94. return 2;
  95. }
  96. int of_simple_size_cells(const struct device_node *np)
  97. {
  98. const __be32 *ip;
  99. ip = of_get_property(np, "#size-cells", NULL);
  100. if (ip)
  101. return be32_to_cpup(ip);
  102. /* Return a default of 2 to match fdt_size_cells()*/
  103. return 2;
  104. }
  105. struct property *of_find_property(const struct device_node *np,
  106. const char *name, int *lenp)
  107. {
  108. struct property *pp;
  109. if (!np)
  110. return NULL;
  111. for (pp = np->properties; pp; pp = pp->next) {
  112. if (strcmp(pp->name, name) == 0) {
  113. if (lenp)
  114. *lenp = pp->length;
  115. break;
  116. }
  117. }
  118. if (!pp && lenp)
  119. *lenp = -FDT_ERR_NOTFOUND;
  120. return pp;
  121. }
  122. struct device_node *of_find_all_nodes(struct device_node *prev)
  123. {
  124. struct device_node *np;
  125. if (!prev) {
  126. np = gd->of_root;
  127. } else if (prev->child) {
  128. np = prev->child;
  129. } else {
  130. /*
  131. * Walk back up looking for a sibling, or the end of the
  132. * structure
  133. */
  134. np = prev;
  135. while (np->parent && !np->sibling)
  136. np = np->parent;
  137. np = np->sibling; /* Might be null at the end of the tree */
  138. }
  139. return np;
  140. }
  141. const void *of_get_property(const struct device_node *np, const char *name,
  142. int *lenp)
  143. {
  144. struct property *pp = of_find_property(np, name, lenp);
  145. return pp ? pp->value : NULL;
  146. }
  147. const struct property *of_get_first_property(const struct device_node *np)
  148. {
  149. if (!np)
  150. return NULL;
  151. return np->properties;
  152. }
  153. const struct property *of_get_next_property(const struct device_node *np,
  154. const struct property *property)
  155. {
  156. if (!np)
  157. return NULL;
  158. return property->next;
  159. }
  160. const void *of_get_property_by_prop(const struct device_node *np,
  161. const struct property *property,
  162. const char **name,
  163. int *lenp)
  164. {
  165. if (!np || !property)
  166. return NULL;
  167. if (name)
  168. *name = property->name;
  169. if (lenp)
  170. *lenp = property->length;
  171. return property->value;
  172. }
  173. static const char *of_prop_next_string(struct property *prop, const char *cur)
  174. {
  175. const void *curv = cur;
  176. if (!prop)
  177. return NULL;
  178. if (!cur)
  179. return prop->value;
  180. curv += strlen(cur) + 1;
  181. if (curv >= prop->value + prop->length)
  182. return NULL;
  183. return curv;
  184. }
  185. int of_device_is_compatible(const struct device_node *device,
  186. const char *compat, const char *type,
  187. const char *name)
  188. {
  189. struct property *prop;
  190. const char *cp;
  191. int index = 0, score = 0;
  192. /* Compatible match has highest priority */
  193. if (compat && compat[0]) {
  194. prop = of_find_property(device, "compatible", NULL);
  195. for (cp = of_prop_next_string(prop, NULL); cp;
  196. cp = of_prop_next_string(prop, cp), index++) {
  197. if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
  198. score = INT_MAX/2 - (index << 2);
  199. break;
  200. }
  201. }
  202. if (!score)
  203. return 0;
  204. }
  205. /* Matching type is better than matching name */
  206. if (type && type[0]) {
  207. if (!device->type || of_node_cmp(type, device->type))
  208. return 0;
  209. score += 2;
  210. }
  211. /* Matching name is a bit better than not */
  212. if (name && name[0]) {
  213. if (!device->name || of_node_cmp(name, device->name))
  214. return 0;
  215. score++;
  216. }
  217. return score;
  218. }
  219. bool of_device_is_available(const struct device_node *device)
  220. {
  221. const char *status;
  222. int statlen;
  223. if (!device)
  224. return false;
  225. status = of_get_property(device, "status", &statlen);
  226. if (status == NULL)
  227. return true;
  228. if (statlen > 0) {
  229. if (!strcmp(status, "okay"))
  230. return true;
  231. }
  232. return false;
  233. }
  234. struct device_node *of_get_parent(const struct device_node *node)
  235. {
  236. const struct device_node *np;
  237. if (!node)
  238. return NULL;
  239. np = of_node_get(node->parent);
  240. return (struct device_node *)np;
  241. }
  242. static struct device_node *__of_get_next_child(const struct device_node *node,
  243. struct device_node *prev)
  244. {
  245. struct device_node *next;
  246. if (!node)
  247. return NULL;
  248. next = prev ? prev->sibling : node->child;
  249. /*
  250. * coverity[dead_error_line : FALSE]
  251. * Dead code here since our current implementation of of_node_get()
  252. * always returns NULL (Coverity CID 163245). But we leave it as is
  253. * since we may want to implement get/put later.
  254. */
  255. for (; next; next = next->sibling)
  256. if (of_node_get(next))
  257. break;
  258. of_node_put(prev);
  259. return next;
  260. }
  261. #define __for_each_child_of_node(parent, child) \
  262. for (child = __of_get_next_child(parent, NULL); child != NULL; \
  263. child = __of_get_next_child(parent, child))
  264. static struct device_node *__of_find_node_by_path(struct device_node *parent,
  265. const char *path)
  266. {
  267. struct device_node *child;
  268. int len;
  269. len = strcspn(path, "/:");
  270. if (!len)
  271. return NULL;
  272. __for_each_child_of_node(parent, child) {
  273. const char *name = strrchr(child->full_name, '/');
  274. name++;
  275. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  276. return child;
  277. }
  278. return NULL;
  279. }
  280. #define for_each_property_of_node(dn, pp) \
  281. for (pp = dn->properties; pp != NULL; pp = pp->next)
  282. struct device_node *of_find_node_opts_by_path(const char *path,
  283. const char **opts)
  284. {
  285. struct device_node *np = NULL;
  286. struct property *pp;
  287. const char *separator = strchr(path, ':');
  288. if (opts)
  289. *opts = separator ? separator + 1 : NULL;
  290. if (strcmp(path, "/") == 0)
  291. return of_node_get(gd->of_root);
  292. /* The path could begin with an alias */
  293. if (*path != '/') {
  294. int len;
  295. const char *p = separator;
  296. if (!p)
  297. p = strchrnul(path, '/');
  298. len = p - path;
  299. /* of_aliases must not be NULL */
  300. if (!of_aliases)
  301. return NULL;
  302. for_each_property_of_node(of_aliases, pp) {
  303. if (strlen(pp->name) == len && !strncmp(pp->name, path,
  304. len)) {
  305. np = of_find_node_by_path(pp->value);
  306. break;
  307. }
  308. }
  309. if (!np)
  310. return NULL;
  311. path = p;
  312. }
  313. /* Step down the tree matching path components */
  314. if (!np)
  315. np = of_node_get(gd->of_root);
  316. while (np && *path == '/') {
  317. struct device_node *tmp = np;
  318. path++; /* Increment past '/' delimiter */
  319. np = __of_find_node_by_path(np, path);
  320. of_node_put(tmp);
  321. path = strchrnul(path, '/');
  322. if (separator && separator < path)
  323. break;
  324. }
  325. return np;
  326. }
  327. struct device_node *of_find_compatible_node(struct device_node *from,
  328. const char *type, const char *compatible)
  329. {
  330. struct device_node *np;
  331. for_each_of_allnodes_from(from, np)
  332. if (of_device_is_compatible(np, compatible, type, NULL) &&
  333. of_node_get(np))
  334. break;
  335. of_node_put(from);
  336. return np;
  337. }
  338. static int of_device_has_prop_value(const struct device_node *device,
  339. const char *propname, const void *propval,
  340. int proplen)
  341. {
  342. struct property *prop = of_find_property(device, propname, NULL);
  343. if (!prop || !prop->value || prop->length != proplen)
  344. return 0;
  345. return !memcmp(prop->value, propval, proplen);
  346. }
  347. struct device_node *of_find_node_by_prop_value(struct device_node *from,
  348. const char *propname,
  349. const void *propval, int proplen)
  350. {
  351. struct device_node *np;
  352. for_each_of_allnodes_from(from, np) {
  353. if (of_device_has_prop_value(np, propname, propval, proplen) &&
  354. of_node_get(np))
  355. break;
  356. }
  357. of_node_put(from);
  358. return np;
  359. }
  360. struct device_node *of_find_node_by_phandle(phandle handle)
  361. {
  362. struct device_node *np;
  363. if (!handle)
  364. return NULL;
  365. for_each_of_allnodes(np)
  366. if (np->phandle == handle)
  367. break;
  368. (void)of_node_get(np);
  369. return np;
  370. }
  371. /**
  372. * of_find_property_value_of_size() - find property of given size
  373. *
  374. * Search for a property in a device node and validate the requested size.
  375. *
  376. * @np: device node from which the property value is to be read.
  377. * @propname: name of the property to be searched.
  378. * @len: requested length of property value
  379. *
  380. * @return the property value on success, -EINVAL if the property does not
  381. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  382. * property data isn't large enough.
  383. */
  384. static void *of_find_property_value_of_size(const struct device_node *np,
  385. const char *propname, u32 len)
  386. {
  387. struct property *prop = of_find_property(np, propname, NULL);
  388. if (!prop)
  389. return ERR_PTR(-EINVAL);
  390. if (!prop->value)
  391. return ERR_PTR(-ENODATA);
  392. if (len > prop->length)
  393. return ERR_PTR(-EOVERFLOW);
  394. return prop->value;
  395. }
  396. int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
  397. {
  398. return of_read_u32_index(np, propname, 0, outp);
  399. }
  400. int of_read_u32_array(const struct device_node *np, const char *propname,
  401. u32 *out_values, size_t sz)
  402. {
  403. const __be32 *val;
  404. debug("%s: %s: ", __func__, propname);
  405. val = of_find_property_value_of_size(np, propname,
  406. sz * sizeof(*out_values));
  407. if (IS_ERR(val))
  408. return PTR_ERR(val);
  409. debug("size %zd\n", sz);
  410. while (sz--)
  411. *out_values++ = be32_to_cpup(val++);
  412. return 0;
  413. }
  414. int of_read_u32_index(const struct device_node *np, const char *propname,
  415. int index, u32 *outp)
  416. {
  417. const __be32 *val;
  418. debug("%s: %s: ", __func__, propname);
  419. if (!np)
  420. return -EINVAL;
  421. val = of_find_property_value_of_size(np, propname,
  422. sizeof(*outp) * (index + 1));
  423. if (IS_ERR(val)) {
  424. debug("(not found)\n");
  425. return PTR_ERR(val);
  426. }
  427. *outp = be32_to_cpup(val + index);
  428. debug("%#x (%d)\n", *outp, *outp);
  429. return 0;
  430. }
  431. int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
  432. {
  433. const __be64 *val;
  434. debug("%s: %s: ", __func__, propname);
  435. if (!np)
  436. return -EINVAL;
  437. val = of_find_property_value_of_size(np, propname, sizeof(*outp));
  438. if (IS_ERR(val)) {
  439. debug("(not found)\n");
  440. return PTR_ERR(val);
  441. }
  442. *outp = be64_to_cpup(val);
  443. debug("%#llx (%lld)\n", (unsigned long long)*outp,
  444. (unsigned long long)*outp);
  445. return 0;
  446. }
  447. int of_property_match_string(const struct device_node *np, const char *propname,
  448. const char *string)
  449. {
  450. const struct property *prop = of_find_property(np, propname, NULL);
  451. size_t l;
  452. int i;
  453. const char *p, *end;
  454. if (!prop)
  455. return -EINVAL;
  456. if (!prop->value)
  457. return -ENODATA;
  458. p = prop->value;
  459. end = p + prop->length;
  460. for (i = 0; p < end; i++, p += l) {
  461. l = strnlen(p, end - p) + 1;
  462. if (p + l > end)
  463. return -EILSEQ;
  464. debug("comparing %s with %s\n", string, p);
  465. if (strcmp(string, p) == 0)
  466. return i; /* Found it; return index */
  467. }
  468. return -ENODATA;
  469. }
  470. /**
  471. * of_property_read_string_helper() - Utility helper for parsing string properties
  472. * @np: device node from which the property value is to be read.
  473. * @propname: name of the property to be searched.
  474. * @out_strs: output array of string pointers.
  475. * @sz: number of array elements to read.
  476. * @skip: Number of strings to skip over at beginning of list.
  477. *
  478. * Don't call this function directly. It is a utility helper for the
  479. * of_property_read_string*() family of functions.
  480. */
  481. int of_property_read_string_helper(const struct device_node *np,
  482. const char *propname, const char **out_strs,
  483. size_t sz, int skip)
  484. {
  485. const struct property *prop = of_find_property(np, propname, NULL);
  486. int l = 0, i = 0;
  487. const char *p, *end;
  488. if (!prop)
  489. return -EINVAL;
  490. if (!prop->value)
  491. return -ENODATA;
  492. p = prop->value;
  493. end = p + prop->length;
  494. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  495. l = strnlen(p, end - p) + 1;
  496. if (p + l > end)
  497. return -EILSEQ;
  498. if (out_strs && i >= skip)
  499. *out_strs++ = p;
  500. }
  501. i -= skip;
  502. return i <= 0 ? -ENODATA : i;
  503. }
  504. static int __of_parse_phandle_with_args(const struct device_node *np,
  505. const char *list_name,
  506. const char *cells_name,
  507. int cell_count, int index,
  508. struct of_phandle_args *out_args)
  509. {
  510. const __be32 *list, *list_end;
  511. int rc = 0, cur_index = 0;
  512. uint32_t count;
  513. struct device_node *node = NULL;
  514. phandle phandle;
  515. int size;
  516. /* Retrieve the phandle list property */
  517. list = of_get_property(np, list_name, &size);
  518. if (!list)
  519. return -ENOENT;
  520. list_end = list + size / sizeof(*list);
  521. /* Loop over the phandles until all the requested entry is found */
  522. while (list < list_end) {
  523. rc = -EINVAL;
  524. count = 0;
  525. /*
  526. * If phandle is 0, then it is an empty entry with no
  527. * arguments. Skip forward to the next entry.
  528. */
  529. phandle = be32_to_cpup(list++);
  530. if (phandle) {
  531. /*
  532. * Find the provider node and parse the #*-cells
  533. * property to determine the argument length.
  534. *
  535. * This is not needed if the cell count is hard-coded
  536. * (i.e. cells_name not set, but cell_count is set),
  537. * except when we're going to return the found node
  538. * below.
  539. */
  540. if (cells_name || cur_index == index) {
  541. node = of_find_node_by_phandle(phandle);
  542. if (!node) {
  543. debug("%s: could not find phandle\n",
  544. np->full_name);
  545. goto err;
  546. }
  547. }
  548. if (cells_name) {
  549. if (of_read_u32(node, cells_name, &count)) {
  550. debug("%s: could not get %s for %s\n",
  551. np->full_name, cells_name,
  552. node->full_name);
  553. goto err;
  554. }
  555. } else {
  556. count = cell_count;
  557. }
  558. /*
  559. * Make sure that the arguments actually fit in the
  560. * remaining property data length
  561. */
  562. if (list + count > list_end) {
  563. debug("%s: arguments longer than property\n",
  564. np->full_name);
  565. goto err;
  566. }
  567. }
  568. /*
  569. * All of the error cases above bail out of the loop, so at
  570. * this point, the parsing is successful. If the requested
  571. * index matches, then fill the out_args structure and return,
  572. * or return -ENOENT for an empty entry.
  573. */
  574. rc = -ENOENT;
  575. if (cur_index == index) {
  576. if (!phandle)
  577. goto err;
  578. if (out_args) {
  579. int i;
  580. if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
  581. count = OF_MAX_PHANDLE_ARGS;
  582. out_args->np = node;
  583. out_args->args_count = count;
  584. for (i = 0; i < count; i++)
  585. out_args->args[i] =
  586. be32_to_cpup(list++);
  587. } else {
  588. of_node_put(node);
  589. }
  590. /* Found it! return success */
  591. return 0;
  592. }
  593. of_node_put(node);
  594. node = NULL;
  595. list += count;
  596. cur_index++;
  597. }
  598. /*
  599. * Unlock node before returning result; will be one of:
  600. * -ENOENT : index is for empty phandle
  601. * -EINVAL : parsing error on data
  602. * [1..n] : Number of phandle (count mode; when index = -1)
  603. */
  604. rc = index < 0 ? cur_index : -ENOENT;
  605. err:
  606. if (node)
  607. of_node_put(node);
  608. return rc;
  609. }
  610. struct device_node *of_parse_phandle(const struct device_node *np,
  611. const char *phandle_name, int index)
  612. {
  613. struct of_phandle_args args;
  614. if (index < 0)
  615. return NULL;
  616. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
  617. &args))
  618. return NULL;
  619. return args.np;
  620. }
  621. int of_parse_phandle_with_args(const struct device_node *np,
  622. const char *list_name, const char *cells_name,
  623. int cell_count, int index,
  624. struct of_phandle_args *out_args)
  625. {
  626. if (index < 0)
  627. return -EINVAL;
  628. return __of_parse_phandle_with_args(np, list_name, cells_name,
  629. cell_count, index, out_args);
  630. }
  631. int of_count_phandle_with_args(const struct device_node *np,
  632. const char *list_name, const char *cells_name,
  633. int cell_count)
  634. {
  635. return __of_parse_phandle_with_args(np, list_name, cells_name,
  636. cell_count, -1, NULL);
  637. }
  638. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  639. int id, const char *stem, int stem_len)
  640. {
  641. ap->np = np;
  642. ap->id = id;
  643. strncpy(ap->stem, stem, stem_len);
  644. ap->stem[stem_len] = 0;
  645. list_add_tail(&ap->link, &aliases_lookup);
  646. debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  647. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  648. }
  649. int of_alias_scan(void)
  650. {
  651. struct property *pp;
  652. of_aliases = of_find_node_by_path("/aliases");
  653. of_chosen = of_find_node_by_path("/chosen");
  654. if (of_chosen == NULL)
  655. of_chosen = of_find_node_by_path("/chosen@0");
  656. if (of_chosen) {
  657. const char *name;
  658. name = of_get_property(of_chosen, "stdout-path", NULL);
  659. if (name)
  660. of_stdout = of_find_node_opts_by_path(name,
  661. &of_stdout_options);
  662. }
  663. if (!of_aliases)
  664. return 0;
  665. for_each_property_of_node(of_aliases, pp) {
  666. const char *start = pp->name;
  667. const char *end = start + strlen(start);
  668. struct device_node *np;
  669. struct alias_prop *ap;
  670. ulong id;
  671. int len;
  672. /* Skip those we do not want to proceed */
  673. if (!strcmp(pp->name, "name") ||
  674. !strcmp(pp->name, "phandle") ||
  675. !strcmp(pp->name, "linux,phandle"))
  676. continue;
  677. np = of_find_node_by_path(pp->value);
  678. if (!np)
  679. continue;
  680. /*
  681. * walk the alias backwards to extract the id and work out
  682. * the 'stem' string
  683. */
  684. while (isdigit(*(end-1)) && end > start)
  685. end--;
  686. len = end - start;
  687. if (strict_strtoul(end, 10, &id) < 0)
  688. continue;
  689. /* Allocate an alias_prop with enough space for the stem */
  690. ap = malloc(sizeof(*ap) + len + 1);
  691. if (!ap)
  692. return -ENOMEM;
  693. memset(ap, 0, sizeof(*ap) + len + 1);
  694. ap->alias = start;
  695. of_alias_add(ap, np, id, start, len);
  696. }
  697. return 0;
  698. }
  699. int of_alias_get_id(const struct device_node *np, const char *stem)
  700. {
  701. struct alias_prop *app;
  702. int id = -ENODEV;
  703. mutex_lock(&of_mutex);
  704. list_for_each_entry(app, &aliases_lookup, link) {
  705. if (strcmp(app->stem, stem) != 0)
  706. continue;
  707. if (np == app->np) {
  708. id = app->id;
  709. break;
  710. }
  711. }
  712. mutex_unlock(&of_mutex);
  713. return id;
  714. }
  715. int of_alias_get_highest_id(const char *stem)
  716. {
  717. struct alias_prop *app;
  718. int id = -1;
  719. mutex_lock(&of_mutex);
  720. list_for_each_entry(app, &aliases_lookup, link) {
  721. if (strcmp(app->stem, stem) != 0)
  722. continue;
  723. if (app->id > id)
  724. id = app->id;
  725. }
  726. mutex_unlock(&of_mutex);
  727. return id;
  728. }
  729. struct device_node *of_get_stdout(void)
  730. {
  731. return of_stdout;
  732. }