base.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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_and_match);
  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(const 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. #if defined(CONFIG_OF_DYNAMIC)
  1015. static int of_property_notify(int action, struct device_node *np,
  1016. struct property *prop)
  1017. {
  1018. struct of_prop_reconfig pr;
  1019. pr.dn = np;
  1020. pr.prop = prop;
  1021. return of_reconfig_notify(action, &pr);
  1022. }
  1023. #else
  1024. static int of_property_notify(int action, struct device_node *np,
  1025. struct property *prop)
  1026. {
  1027. return 0;
  1028. }
  1029. #endif
  1030. /**
  1031. * of_add_property - Add a property to a node
  1032. */
  1033. int of_add_property(struct device_node *np, struct property *prop)
  1034. {
  1035. struct property **next;
  1036. unsigned long flags;
  1037. int rc;
  1038. rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
  1039. if (rc)
  1040. return rc;
  1041. prop->next = NULL;
  1042. write_lock_irqsave(&devtree_lock, flags);
  1043. next = &np->properties;
  1044. while (*next) {
  1045. if (strcmp(prop->name, (*next)->name) == 0) {
  1046. /* duplicate ! don't insert it */
  1047. write_unlock_irqrestore(&devtree_lock, flags);
  1048. return -1;
  1049. }
  1050. next = &(*next)->next;
  1051. }
  1052. *next = prop;
  1053. write_unlock_irqrestore(&devtree_lock, flags);
  1054. #ifdef CONFIG_PROC_DEVICETREE
  1055. /* try to add to proc as well if it was initialized */
  1056. if (np->pde)
  1057. proc_device_tree_add_prop(np->pde, prop);
  1058. #endif /* CONFIG_PROC_DEVICETREE */
  1059. return 0;
  1060. }
  1061. /**
  1062. * of_remove_property - Remove a property from a node.
  1063. *
  1064. * Note that we don't actually remove it, since we have given out
  1065. * who-knows-how-many pointers to the data using get-property.
  1066. * Instead we just move the property to the "dead properties"
  1067. * list, so it won't be found any more.
  1068. */
  1069. int of_remove_property(struct device_node *np, struct property *prop)
  1070. {
  1071. struct property **next;
  1072. unsigned long flags;
  1073. int found = 0;
  1074. int rc;
  1075. rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  1076. if (rc)
  1077. return rc;
  1078. write_lock_irqsave(&devtree_lock, flags);
  1079. next = &np->properties;
  1080. while (*next) {
  1081. if (*next == prop) {
  1082. /* found the node */
  1083. *next = prop->next;
  1084. prop->next = np->deadprops;
  1085. np->deadprops = prop;
  1086. found = 1;
  1087. break;
  1088. }
  1089. next = &(*next)->next;
  1090. }
  1091. write_unlock_irqrestore(&devtree_lock, flags);
  1092. if (!found)
  1093. return -ENODEV;
  1094. #ifdef CONFIG_PROC_DEVICETREE
  1095. /* try to remove the proc node as well */
  1096. if (np->pde)
  1097. proc_device_tree_remove_prop(np->pde, prop);
  1098. #endif /* CONFIG_PROC_DEVICETREE */
  1099. return 0;
  1100. }
  1101. /*
  1102. * of_update_property - Update a property in a node, if the property does
  1103. * not exist, add it.
  1104. *
  1105. * Note that we don't actually remove it, since we have given out
  1106. * who-knows-how-many pointers to the data using get-property.
  1107. * Instead we just move the property to the "dead properties" list,
  1108. * and add the new property to the property list
  1109. */
  1110. int of_update_property(struct device_node *np, struct property *newprop)
  1111. {
  1112. struct property **next, *oldprop;
  1113. unsigned long flags;
  1114. int rc, found = 0;
  1115. rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
  1116. if (rc)
  1117. return rc;
  1118. if (!newprop->name)
  1119. return -EINVAL;
  1120. oldprop = of_find_property(np, newprop->name, NULL);
  1121. if (!oldprop)
  1122. return of_add_property(np, newprop);
  1123. write_lock_irqsave(&devtree_lock, flags);
  1124. next = &np->properties;
  1125. while (*next) {
  1126. if (*next == oldprop) {
  1127. /* found the node */
  1128. newprop->next = oldprop->next;
  1129. *next = newprop;
  1130. oldprop->next = np->deadprops;
  1131. np->deadprops = oldprop;
  1132. found = 1;
  1133. break;
  1134. }
  1135. next = &(*next)->next;
  1136. }
  1137. write_unlock_irqrestore(&devtree_lock, flags);
  1138. if (!found)
  1139. return -ENODEV;
  1140. #ifdef CONFIG_PROC_DEVICETREE
  1141. /* try to add to proc as well if it was initialized */
  1142. if (np->pde)
  1143. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1144. #endif /* CONFIG_PROC_DEVICETREE */
  1145. return 0;
  1146. }
  1147. #if defined(CONFIG_OF_DYNAMIC)
  1148. /*
  1149. * Support for dynamic device trees.
  1150. *
  1151. * On some platforms, the device tree can be manipulated at runtime.
  1152. * The routines in this section support adding, removing and changing
  1153. * device tree nodes.
  1154. */
  1155. static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
  1156. int of_reconfig_notifier_register(struct notifier_block *nb)
  1157. {
  1158. return blocking_notifier_chain_register(&of_reconfig_chain, nb);
  1159. }
  1160. EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
  1161. int of_reconfig_notifier_unregister(struct notifier_block *nb)
  1162. {
  1163. return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
  1164. }
  1165. EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
  1166. int of_reconfig_notify(unsigned long action, void *p)
  1167. {
  1168. int rc;
  1169. rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
  1170. return notifier_to_errno(rc);
  1171. }
  1172. #ifdef CONFIG_PROC_DEVICETREE
  1173. static void of_add_proc_dt_entry(struct device_node *dn)
  1174. {
  1175. struct proc_dir_entry *ent;
  1176. ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
  1177. if (ent)
  1178. proc_device_tree_add_node(dn, ent);
  1179. }
  1180. #else
  1181. static void of_add_proc_dt_entry(struct device_node *dn)
  1182. {
  1183. return;
  1184. }
  1185. #endif
  1186. /**
  1187. * of_attach_node - Plug a device node into the tree and global list.
  1188. */
  1189. int of_attach_node(struct device_node *np)
  1190. {
  1191. unsigned long flags;
  1192. int rc;
  1193. rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
  1194. if (rc)
  1195. return rc;
  1196. write_lock_irqsave(&devtree_lock, flags);
  1197. np->sibling = np->parent->child;
  1198. np->allnext = of_allnodes;
  1199. np->parent->child = np;
  1200. of_allnodes = np;
  1201. write_unlock_irqrestore(&devtree_lock, flags);
  1202. of_add_proc_dt_entry(np);
  1203. return 0;
  1204. }
  1205. #ifdef CONFIG_PROC_DEVICETREE
  1206. static void of_remove_proc_dt_entry(struct device_node *dn)
  1207. {
  1208. struct device_node *parent = dn->parent;
  1209. struct property *prop = dn->properties;
  1210. while (prop) {
  1211. remove_proc_entry(prop->name, dn->pde);
  1212. prop = prop->next;
  1213. }
  1214. if (dn->pde)
  1215. remove_proc_entry(dn->pde->name, parent->pde);
  1216. }
  1217. #else
  1218. static void of_remove_proc_dt_entry(struct device_node *dn)
  1219. {
  1220. return;
  1221. }
  1222. #endif
  1223. /**
  1224. * of_detach_node - "Unplug" a node from the device tree.
  1225. *
  1226. * The caller must hold a reference to the node. The memory associated with
  1227. * the node is not freed until its refcount goes to zero.
  1228. */
  1229. int of_detach_node(struct device_node *np)
  1230. {
  1231. struct device_node *parent;
  1232. unsigned long flags;
  1233. int rc = 0;
  1234. rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
  1235. if (rc)
  1236. return rc;
  1237. write_lock_irqsave(&devtree_lock, flags);
  1238. if (of_node_check_flag(np, OF_DETACHED)) {
  1239. /* someone already detached it */
  1240. write_unlock_irqrestore(&devtree_lock, flags);
  1241. return rc;
  1242. }
  1243. parent = np->parent;
  1244. if (!parent) {
  1245. write_unlock_irqrestore(&devtree_lock, flags);
  1246. return rc;
  1247. }
  1248. if (of_allnodes == np)
  1249. of_allnodes = np->allnext;
  1250. else {
  1251. struct device_node *prev;
  1252. for (prev = of_allnodes;
  1253. prev->allnext != np;
  1254. prev = prev->allnext)
  1255. ;
  1256. prev->allnext = np->allnext;
  1257. }
  1258. if (parent->child == np)
  1259. parent->child = np->sibling;
  1260. else {
  1261. struct device_node *prevsib;
  1262. for (prevsib = np->parent->child;
  1263. prevsib->sibling != np;
  1264. prevsib = prevsib->sibling)
  1265. ;
  1266. prevsib->sibling = np->sibling;
  1267. }
  1268. of_node_set_flag(np, OF_DETACHED);
  1269. write_unlock_irqrestore(&devtree_lock, flags);
  1270. of_remove_proc_dt_entry(np);
  1271. return rc;
  1272. }
  1273. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1274. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1275. int id, const char *stem, int stem_len)
  1276. {
  1277. ap->np = np;
  1278. ap->id = id;
  1279. strncpy(ap->stem, stem, stem_len);
  1280. ap->stem[stem_len] = 0;
  1281. list_add_tail(&ap->link, &aliases_lookup);
  1282. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1283. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1284. }
  1285. /**
  1286. * of_alias_scan - Scan all properties of 'aliases' node
  1287. *
  1288. * The function scans all the properties of 'aliases' node and populate
  1289. * the the global lookup table with the properties. It returns the
  1290. * number of alias_prop found, or error code in error case.
  1291. *
  1292. * @dt_alloc: An allocator that provides a virtual address to memory
  1293. * for the resulting tree
  1294. */
  1295. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1296. {
  1297. struct property *pp;
  1298. of_chosen = of_find_node_by_path("/chosen");
  1299. if (of_chosen == NULL)
  1300. of_chosen = of_find_node_by_path("/chosen@0");
  1301. of_aliases = of_find_node_by_path("/aliases");
  1302. if (!of_aliases)
  1303. return;
  1304. for_each_property_of_node(of_aliases, pp) {
  1305. const char *start = pp->name;
  1306. const char *end = start + strlen(start);
  1307. struct device_node *np;
  1308. struct alias_prop *ap;
  1309. int id, len;
  1310. /* Skip those we do not want to proceed */
  1311. if (!strcmp(pp->name, "name") ||
  1312. !strcmp(pp->name, "phandle") ||
  1313. !strcmp(pp->name, "linux,phandle"))
  1314. continue;
  1315. np = of_find_node_by_path(pp->value);
  1316. if (!np)
  1317. continue;
  1318. /* walk the alias backwards to extract the id and work out
  1319. * the 'stem' string */
  1320. while (isdigit(*(end-1)) && end > start)
  1321. end--;
  1322. len = end - start;
  1323. if (kstrtoint(end, 10, &id) < 0)
  1324. continue;
  1325. /* Allocate an alias_prop with enough space for the stem */
  1326. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1327. if (!ap)
  1328. continue;
  1329. ap->alias = start;
  1330. of_alias_add(ap, np, id, start, len);
  1331. }
  1332. }
  1333. /**
  1334. * of_alias_get_id - Get alias id for the given device_node
  1335. * @np: Pointer to the given device_node
  1336. * @stem: Alias stem of the given device_node
  1337. *
  1338. * The function travels the lookup table to get alias id for the given
  1339. * device_node and alias stem. It returns the alias id if find it.
  1340. */
  1341. int of_alias_get_id(struct device_node *np, const char *stem)
  1342. {
  1343. struct alias_prop *app;
  1344. int id = -ENODEV;
  1345. mutex_lock(&of_aliases_mutex);
  1346. list_for_each_entry(app, &aliases_lookup, link) {
  1347. if (strcmp(app->stem, stem) != 0)
  1348. continue;
  1349. if (np == app->np) {
  1350. id = app->id;
  1351. break;
  1352. }
  1353. }
  1354. mutex_unlock(&of_aliases_mutex);
  1355. return id;
  1356. }
  1357. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1358. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1359. u32 *pu)
  1360. {
  1361. const void *curv = cur;
  1362. if (!prop)
  1363. return NULL;
  1364. if (!cur) {
  1365. curv = prop->value;
  1366. goto out_val;
  1367. }
  1368. curv += sizeof(*cur);
  1369. if (curv >= prop->value + prop->length)
  1370. return NULL;
  1371. out_val:
  1372. *pu = be32_to_cpup(curv);
  1373. return curv;
  1374. }
  1375. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1376. const char *of_prop_next_string(struct property *prop, const char *cur)
  1377. {
  1378. const void *curv = cur;
  1379. if (!prop)
  1380. return NULL;
  1381. if (!cur)
  1382. return prop->value;
  1383. curv += strlen(cur) + 1;
  1384. if (curv >= prop->value + prop->length)
  1385. return NULL;
  1386. return curv;
  1387. }
  1388. EXPORT_SYMBOL_GPL(of_prop_next_string);