base.c 38 KB

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