base.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  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. static struct property *__of_find_property(const struct device_node *np,
  150. const char *name, int *lenp)
  151. {
  152. struct property *pp;
  153. if (!np)
  154. return NULL;
  155. for (pp = np->properties; pp; pp = pp->next) {
  156. if (of_prop_cmp(pp->name, name) == 0) {
  157. if (lenp)
  158. *lenp = pp->length;
  159. break;
  160. }
  161. }
  162. return pp;
  163. }
  164. struct property *of_find_property(const struct device_node *np,
  165. const char *name,
  166. int *lenp)
  167. {
  168. struct property *pp;
  169. read_lock(&devtree_lock);
  170. pp = __of_find_property(np, name, lenp);
  171. read_unlock(&devtree_lock);
  172. return pp;
  173. }
  174. EXPORT_SYMBOL(of_find_property);
  175. /**
  176. * of_find_all_nodes - Get next node in global list
  177. * @prev: Previous node or NULL to start iteration
  178. * of_node_put() will be called on it
  179. *
  180. * Returns a node pointer with refcount incremented, use
  181. * of_node_put() on it when done.
  182. */
  183. struct device_node *of_find_all_nodes(struct device_node *prev)
  184. {
  185. struct device_node *np;
  186. read_lock(&devtree_lock);
  187. np = prev ? prev->allnext : of_allnodes;
  188. for (; np != NULL; np = np->allnext)
  189. if (of_node_get(np))
  190. break;
  191. of_node_put(prev);
  192. read_unlock(&devtree_lock);
  193. return np;
  194. }
  195. EXPORT_SYMBOL(of_find_all_nodes);
  196. /*
  197. * Find a property with a given name for a given node
  198. * and return the value.
  199. */
  200. static const void *__of_get_property(const struct device_node *np,
  201. const char *name, int *lenp)
  202. {
  203. struct property *pp = __of_find_property(np, name, lenp);
  204. return pp ? pp->value : NULL;
  205. }
  206. /*
  207. * Find a property with a given name for a given node
  208. * and return the value.
  209. */
  210. const void *of_get_property(const struct device_node *np, const char *name,
  211. int *lenp)
  212. {
  213. struct property *pp = of_find_property(np, name, lenp);
  214. return pp ? pp->value : NULL;
  215. }
  216. EXPORT_SYMBOL(of_get_property);
  217. /** Checks if the given "compat" string matches one of the strings in
  218. * the device's "compatible" property
  219. */
  220. static int __of_device_is_compatible(const struct device_node *device,
  221. const char *compat)
  222. {
  223. const char* cp;
  224. int cplen, l;
  225. cp = __of_get_property(device, "compatible", &cplen);
  226. if (cp == NULL)
  227. return 0;
  228. while (cplen > 0) {
  229. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  230. return 1;
  231. l = strlen(cp) + 1;
  232. cp += l;
  233. cplen -= l;
  234. }
  235. return 0;
  236. }
  237. /** Checks if the given "compat" string matches one of the strings in
  238. * the device's "compatible" property
  239. */
  240. int of_device_is_compatible(const struct device_node *device,
  241. const char *compat)
  242. {
  243. int res;
  244. read_lock(&devtree_lock);
  245. res = __of_device_is_compatible(device, compat);
  246. read_unlock(&devtree_lock);
  247. return res;
  248. }
  249. EXPORT_SYMBOL(of_device_is_compatible);
  250. /**
  251. * of_machine_is_compatible - Test root of device tree for a given compatible value
  252. * @compat: compatible string to look for in root node's compatible property.
  253. *
  254. * Returns true if the root node has the given value in its
  255. * compatible property.
  256. */
  257. int of_machine_is_compatible(const char *compat)
  258. {
  259. struct device_node *root;
  260. int rc = 0;
  261. root = of_find_node_by_path("/");
  262. if (root) {
  263. rc = of_device_is_compatible(root, compat);
  264. of_node_put(root);
  265. }
  266. return rc;
  267. }
  268. EXPORT_SYMBOL(of_machine_is_compatible);
  269. /**
  270. * of_device_is_available - check if a device is available for use
  271. *
  272. * @device: Node to check for availability
  273. *
  274. * Returns 1 if the status property is absent or set to "okay" or "ok",
  275. * 0 otherwise
  276. */
  277. int of_device_is_available(const struct device_node *device)
  278. {
  279. const char *status;
  280. int statlen;
  281. status = of_get_property(device, "status", &statlen);
  282. if (status == NULL)
  283. return 1;
  284. if (statlen > 0) {
  285. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  286. return 1;
  287. }
  288. return 0;
  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_get_child_by_name - Find the child node by name for a given parent
  380. * @node: parent node
  381. * @name: child name to look for.
  382. *
  383. * This function looks for child node for given matching name
  384. *
  385. * Returns a node pointer if found, with refcount incremented, use
  386. * of_node_put() on it when done.
  387. * Returns NULL if node is not found.
  388. */
  389. struct device_node *of_get_child_by_name(const struct device_node *node,
  390. const char *name)
  391. {
  392. struct device_node *child;
  393. for_each_child_of_node(node, child)
  394. if (child->name && (of_node_cmp(child->name, name) == 0))
  395. break;
  396. return child;
  397. }
  398. EXPORT_SYMBOL(of_get_child_by_name);
  399. /**
  400. * of_find_node_by_path - Find a node matching a full OF path
  401. * @path: The full path to match
  402. *
  403. * Returns a node pointer with refcount incremented, use
  404. * of_node_put() on it when done.
  405. */
  406. struct device_node *of_find_node_by_path(const char *path)
  407. {
  408. struct device_node *np = of_allnodes;
  409. read_lock(&devtree_lock);
  410. for (; np; np = np->allnext) {
  411. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  412. && of_node_get(np))
  413. break;
  414. }
  415. read_unlock(&devtree_lock);
  416. return np;
  417. }
  418. EXPORT_SYMBOL(of_find_node_by_path);
  419. /**
  420. * of_find_node_by_name - Find a node by its "name" property
  421. * @from: The node to start searching from or NULL, the node
  422. * you pass will not be searched, only the next one
  423. * will; typically, you pass what the previous call
  424. * returned. of_node_put() will be called on it
  425. * @name: The name string to match against
  426. *
  427. * Returns a node pointer with refcount incremented, use
  428. * of_node_put() on it when done.
  429. */
  430. struct device_node *of_find_node_by_name(struct device_node *from,
  431. const char *name)
  432. {
  433. struct device_node *np;
  434. read_lock(&devtree_lock);
  435. np = from ? from->allnext : of_allnodes;
  436. for (; np; np = np->allnext)
  437. if (np->name && (of_node_cmp(np->name, name) == 0)
  438. && of_node_get(np))
  439. break;
  440. of_node_put(from);
  441. read_unlock(&devtree_lock);
  442. return np;
  443. }
  444. EXPORT_SYMBOL(of_find_node_by_name);
  445. /**
  446. * of_find_node_by_type - Find a node by its "device_type" property
  447. * @from: The node to start searching from, or NULL to start searching
  448. * the entire device tree. The node you pass will not be
  449. * searched, only the next one will; typically, you pass
  450. * what the previous call returned. of_node_put() will be
  451. * called on from for you.
  452. * @type: The type string to match against
  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_node_by_type(struct device_node *from,
  458. const char *type)
  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 (np->type && (of_node_cmp(np->type, type) == 0)
  465. && of_node_get(np))
  466. break;
  467. of_node_put(from);
  468. read_unlock(&devtree_lock);
  469. return np;
  470. }
  471. EXPORT_SYMBOL(of_find_node_by_type);
  472. /**
  473. * of_find_compatible_node - Find a node based on type and one of the
  474. * tokens in its "compatible" property
  475. * @from: The node to start searching from or NULL, the node
  476. * you pass will not be searched, only the next one
  477. * will; typically, you pass what the previous call
  478. * returned. of_node_put() will be called on it
  479. * @type: The type string to match "device_type" or NULL to ignore
  480. * @compatible: The string to match to one of the tokens in the device
  481. * "compatible" list.
  482. *
  483. * Returns a node pointer with refcount incremented, use
  484. * of_node_put() on it when done.
  485. */
  486. struct device_node *of_find_compatible_node(struct device_node *from,
  487. const char *type, const char *compatible)
  488. {
  489. struct device_node *np;
  490. read_lock(&devtree_lock);
  491. np = from ? from->allnext : of_allnodes;
  492. for (; np; np = np->allnext) {
  493. if (type
  494. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  495. continue;
  496. if (__of_device_is_compatible(np, compatible) &&
  497. of_node_get(np))
  498. break;
  499. }
  500. of_node_put(from);
  501. read_unlock(&devtree_lock);
  502. return np;
  503. }
  504. EXPORT_SYMBOL(of_find_compatible_node);
  505. /**
  506. * of_find_node_with_property - Find a node which has a property with
  507. * the given name.
  508. * @from: The node to start searching from or NULL, the node
  509. * you pass will not be searched, only the next one
  510. * will; typically, you pass what the previous call
  511. * returned. of_node_put() will be called on it
  512. * @prop_name: The name of the property to look for.
  513. *
  514. * Returns a node pointer with refcount incremented, use
  515. * of_node_put() on it when done.
  516. */
  517. struct device_node *of_find_node_with_property(struct device_node *from,
  518. const char *prop_name)
  519. {
  520. struct device_node *np;
  521. struct property *pp;
  522. read_lock(&devtree_lock);
  523. np = from ? from->allnext : of_allnodes;
  524. for (; np; np = np->allnext) {
  525. for (pp = np->properties; pp; pp = pp->next) {
  526. if (of_prop_cmp(pp->name, prop_name) == 0) {
  527. of_node_get(np);
  528. goto out;
  529. }
  530. }
  531. }
  532. out:
  533. of_node_put(from);
  534. read_unlock(&devtree_lock);
  535. return np;
  536. }
  537. EXPORT_SYMBOL(of_find_node_with_property);
  538. static
  539. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  540. const struct device_node *node)
  541. {
  542. if (!matches)
  543. return NULL;
  544. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  545. int match = 1;
  546. if (matches->name[0])
  547. match &= node->name
  548. && !strcmp(matches->name, node->name);
  549. if (matches->type[0])
  550. match &= node->type
  551. && !strcmp(matches->type, node->type);
  552. if (matches->compatible[0])
  553. match &= __of_device_is_compatible(node,
  554. matches->compatible);
  555. if (match)
  556. return matches;
  557. matches++;
  558. }
  559. return NULL;
  560. }
  561. /**
  562. * of_match_node - Tell if an device_node has a matching of_match structure
  563. * @matches: array of of device match structures to search in
  564. * @node: the of device structure to match against
  565. *
  566. * Low level utility function used by device matching.
  567. */
  568. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  569. const struct device_node *node)
  570. {
  571. const struct of_device_id *match;
  572. read_lock(&devtree_lock);
  573. match = __of_match_node(matches, node);
  574. read_unlock(&devtree_lock);
  575. return match;
  576. }
  577. EXPORT_SYMBOL(of_match_node);
  578. /**
  579. * of_find_matching_node_and_match - Find a node based on an of_device_id
  580. * match table.
  581. * @from: The node to start searching from or NULL, the node
  582. * you pass will not be searched, only the next one
  583. * will; typically, you pass what the previous call
  584. * returned. of_node_put() will be called on it
  585. * @matches: array of of device match structures to search in
  586. * @match Updated to point at the matches entry which matched
  587. *
  588. * Returns a node pointer with refcount incremented, use
  589. * of_node_put() on it when done.
  590. */
  591. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  592. const struct of_device_id *matches,
  593. const struct of_device_id **match)
  594. {
  595. struct device_node *np;
  596. const struct of_device_id *m;
  597. if (match)
  598. *match = NULL;
  599. read_lock(&devtree_lock);
  600. np = from ? from->allnext : of_allnodes;
  601. for (; np; np = np->allnext) {
  602. m = __of_match_node(matches, np);
  603. if (m && of_node_get(np)) {
  604. if (match)
  605. *match = m;
  606. break;
  607. }
  608. }
  609. of_node_put(from);
  610. read_unlock(&devtree_lock);
  611. return np;
  612. }
  613. EXPORT_SYMBOL(of_find_matching_node_and_match);
  614. /**
  615. * of_modalias_node - Lookup appropriate modalias for a device node
  616. * @node: pointer to a device tree node
  617. * @modalias: Pointer to buffer that modalias value will be copied into
  618. * @len: Length of modalias value
  619. *
  620. * Based on the value of the compatible property, this routine will attempt
  621. * to choose an appropriate modalias value for a particular device tree node.
  622. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  623. * from the first entry in the compatible list property.
  624. *
  625. * This routine returns 0 on success, <0 on failure.
  626. */
  627. int of_modalias_node(struct device_node *node, char *modalias, int len)
  628. {
  629. const char *compatible, *p;
  630. int cplen;
  631. compatible = of_get_property(node, "compatible", &cplen);
  632. if (!compatible || strlen(compatible) > cplen)
  633. return -ENODEV;
  634. p = strchr(compatible, ',');
  635. strlcpy(modalias, p ? p + 1 : compatible, len);
  636. return 0;
  637. }
  638. EXPORT_SYMBOL_GPL(of_modalias_node);
  639. /**
  640. * of_find_node_by_phandle - Find a node given a phandle
  641. * @handle: phandle of the node to find
  642. *
  643. * Returns a node pointer with refcount incremented, use
  644. * of_node_put() on it when done.
  645. */
  646. struct device_node *of_find_node_by_phandle(phandle handle)
  647. {
  648. struct device_node *np;
  649. read_lock(&devtree_lock);
  650. for (np = of_allnodes; np; np = np->allnext)
  651. if (np->phandle == handle)
  652. break;
  653. of_node_get(np);
  654. read_unlock(&devtree_lock);
  655. return np;
  656. }
  657. EXPORT_SYMBOL(of_find_node_by_phandle);
  658. /**
  659. * of_property_read_u8_array - Find and read an array of u8 from a property.
  660. *
  661. * @np: device node from which the property value is to be read.
  662. * @propname: name of the property to be searched.
  663. * @out_value: pointer to return value, modified only if return value is 0.
  664. * @sz: number of array elements to read
  665. *
  666. * Search for a property in a device node and read 8-bit value(s) from
  667. * it. Returns 0 on success, -EINVAL if the property does not exist,
  668. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  669. * property data isn't large enough.
  670. *
  671. * dts entry of array should be like:
  672. * property = /bits/ 8 <0x50 0x60 0x70>;
  673. *
  674. * The out_value is modified only if a valid u8 value can be decoded.
  675. */
  676. int of_property_read_u8_array(const struct device_node *np,
  677. const char *propname, u8 *out_values, size_t sz)
  678. {
  679. struct property *prop = of_find_property(np, propname, NULL);
  680. const u8 *val;
  681. if (!prop)
  682. return -EINVAL;
  683. if (!prop->value)
  684. return -ENODATA;
  685. if ((sz * sizeof(*out_values)) > prop->length)
  686. return -EOVERFLOW;
  687. val = prop->value;
  688. while (sz--)
  689. *out_values++ = *val++;
  690. return 0;
  691. }
  692. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  693. /**
  694. * of_property_read_u16_array - Find and read an array of u16 from a property.
  695. *
  696. * @np: device node from which the property value is to be read.
  697. * @propname: name of the property to be searched.
  698. * @out_value: pointer to return value, modified only if return value is 0.
  699. * @sz: number of array elements to read
  700. *
  701. * Search for a property in a device node and read 16-bit value(s) from
  702. * it. Returns 0 on success, -EINVAL if the property does not exist,
  703. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  704. * property data isn't large enough.
  705. *
  706. * dts entry of array should be like:
  707. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  708. *
  709. * The out_value is modified only if a valid u16 value can be decoded.
  710. */
  711. int of_property_read_u16_array(const struct device_node *np,
  712. const char *propname, u16 *out_values, size_t sz)
  713. {
  714. struct property *prop = of_find_property(np, propname, NULL);
  715. const __be16 *val;
  716. if (!prop)
  717. return -EINVAL;
  718. if (!prop->value)
  719. return -ENODATA;
  720. if ((sz * sizeof(*out_values)) > prop->length)
  721. return -EOVERFLOW;
  722. val = prop->value;
  723. while (sz--)
  724. *out_values++ = be16_to_cpup(val++);
  725. return 0;
  726. }
  727. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  728. /**
  729. * of_property_read_u32_array - Find and read an array of 32 bit integers
  730. * from a property.
  731. *
  732. * @np: device node from which the property value is to be read.
  733. * @propname: name of the property to be searched.
  734. * @out_value: pointer to return value, modified only if return value is 0.
  735. * @sz: number of array elements to read
  736. *
  737. * Search for a property in a device node and read 32-bit value(s) from
  738. * it. Returns 0 on success, -EINVAL if the property does not exist,
  739. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  740. * property data isn't large enough.
  741. *
  742. * The out_value is modified only if a valid u32 value can be decoded.
  743. */
  744. int of_property_read_u32_array(const struct device_node *np,
  745. const char *propname, u32 *out_values,
  746. size_t sz)
  747. {
  748. struct property *prop = of_find_property(np, propname, NULL);
  749. const __be32 *val;
  750. if (!prop)
  751. return -EINVAL;
  752. if (!prop->value)
  753. return -ENODATA;
  754. if ((sz * sizeof(*out_values)) > prop->length)
  755. return -EOVERFLOW;
  756. val = prop->value;
  757. while (sz--)
  758. *out_values++ = be32_to_cpup(val++);
  759. return 0;
  760. }
  761. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  762. /**
  763. * of_property_read_u64 - Find and read a 64 bit integer from a property
  764. * @np: device node from which the property value is to be read.
  765. * @propname: name of the property to be searched.
  766. * @out_value: pointer to return value, modified only if return value is 0.
  767. *
  768. * Search for a property in a device node and read a 64-bit value from
  769. * it. Returns 0 on success, -EINVAL if the property does not exist,
  770. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  771. * property data isn't large enough.
  772. *
  773. * The out_value is modified only if a valid u64 value can be decoded.
  774. */
  775. int of_property_read_u64(const struct device_node *np, const char *propname,
  776. u64 *out_value)
  777. {
  778. struct property *prop = of_find_property(np, propname, NULL);
  779. if (!prop)
  780. return -EINVAL;
  781. if (!prop->value)
  782. return -ENODATA;
  783. if (sizeof(*out_value) > prop->length)
  784. return -EOVERFLOW;
  785. *out_value = of_read_number(prop->value, 2);
  786. return 0;
  787. }
  788. EXPORT_SYMBOL_GPL(of_property_read_u64);
  789. /**
  790. * of_property_read_string - Find and read a string from a property
  791. * @np: device node from which the property value is to be read.
  792. * @propname: name of the property to be searched.
  793. * @out_string: pointer to null terminated return string, modified only if
  794. * return value is 0.
  795. *
  796. * Search for a property in a device tree node and retrieve a null
  797. * terminated string value (pointer to data, not a copy). Returns 0 on
  798. * success, -EINVAL if the property does not exist, -ENODATA if property
  799. * does not have a value, and -EILSEQ if the string is not null-terminated
  800. * within the length of the property data.
  801. *
  802. * The out_string pointer is modified only if a valid string can be decoded.
  803. */
  804. int of_property_read_string(struct device_node *np, const char *propname,
  805. const char **out_string)
  806. {
  807. struct property *prop = of_find_property(np, propname, NULL);
  808. if (!prop)
  809. return -EINVAL;
  810. if (!prop->value)
  811. return -ENODATA;
  812. if (strnlen(prop->value, prop->length) >= prop->length)
  813. return -EILSEQ;
  814. *out_string = prop->value;
  815. return 0;
  816. }
  817. EXPORT_SYMBOL_GPL(of_property_read_string);
  818. /**
  819. * of_property_read_string_index - Find and read a string from a multiple
  820. * strings property.
  821. * @np: device node from which the property value is to be read.
  822. * @propname: name of the property to be searched.
  823. * @index: index of the string in the list of strings
  824. * @out_string: pointer to null terminated return string, modified only if
  825. * return value is 0.
  826. *
  827. * Search for a property in a device tree node and retrieve a null
  828. * terminated string value (pointer to data, not a copy) in the list of strings
  829. * contained in that property.
  830. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  831. * property does not have a value, and -EILSEQ if the string is not
  832. * null-terminated within the length of the property data.
  833. *
  834. * The out_string pointer is modified only if a valid string can be decoded.
  835. */
  836. int of_property_read_string_index(struct device_node *np, const char *propname,
  837. int index, const char **output)
  838. {
  839. struct property *prop = of_find_property(np, propname, NULL);
  840. int i = 0;
  841. size_t l = 0, total = 0;
  842. const char *p;
  843. if (!prop)
  844. return -EINVAL;
  845. if (!prop->value)
  846. return -ENODATA;
  847. if (strnlen(prop->value, prop->length) >= prop->length)
  848. return -EILSEQ;
  849. p = prop->value;
  850. for (i = 0; total < prop->length; total += l, p += l) {
  851. l = strlen(p) + 1;
  852. if (i++ == index) {
  853. *output = p;
  854. return 0;
  855. }
  856. }
  857. return -ENODATA;
  858. }
  859. EXPORT_SYMBOL_GPL(of_property_read_string_index);
  860. /**
  861. * of_property_match_string() - Find string in a list and return index
  862. * @np: pointer to node containing string list property
  863. * @propname: string list property name
  864. * @string: pointer to string to search for in string list
  865. *
  866. * This function searches a string list property and returns the index
  867. * of a specific string value.
  868. */
  869. int of_property_match_string(struct device_node *np, const char *propname,
  870. const char *string)
  871. {
  872. struct property *prop = of_find_property(np, propname, NULL);
  873. size_t l;
  874. int i;
  875. const char *p, *end;
  876. if (!prop)
  877. return -EINVAL;
  878. if (!prop->value)
  879. return -ENODATA;
  880. p = prop->value;
  881. end = p + prop->length;
  882. for (i = 0; p < end; i++, p += l) {
  883. l = strlen(p) + 1;
  884. if (p + l > end)
  885. return -EILSEQ;
  886. pr_debug("comparing %s with %s\n", string, p);
  887. if (strcmp(string, p) == 0)
  888. return i; /* Found it; return index */
  889. }
  890. return -ENODATA;
  891. }
  892. EXPORT_SYMBOL_GPL(of_property_match_string);
  893. /**
  894. * of_property_count_strings - Find and return the number of strings from a
  895. * multiple strings property.
  896. * @np: device node from which the property value is to be read.
  897. * @propname: name of the property to be searched.
  898. *
  899. * Search for a property in a device tree node and retrieve the number of null
  900. * terminated string contain in it. Returns the number of strings on
  901. * success, -EINVAL if the property does not exist, -ENODATA if property
  902. * does not have a value, and -EILSEQ if the string is not null-terminated
  903. * within the length of the property data.
  904. */
  905. int of_property_count_strings(struct device_node *np, const char *propname)
  906. {
  907. struct property *prop = of_find_property(np, propname, NULL);
  908. int i = 0;
  909. size_t l = 0, total = 0;
  910. const char *p;
  911. if (!prop)
  912. return -EINVAL;
  913. if (!prop->value)
  914. return -ENODATA;
  915. if (strnlen(prop->value, prop->length) >= prop->length)
  916. return -EILSEQ;
  917. p = prop->value;
  918. for (i = 0; total < prop->length; total += l, p += l, i++)
  919. l = strlen(p) + 1;
  920. return i;
  921. }
  922. EXPORT_SYMBOL_GPL(of_property_count_strings);
  923. /**
  924. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  925. * @np: Pointer to device node holding phandle property
  926. * @phandle_name: Name of property holding a phandle value
  927. * @index: For properties holding a table of phandles, this is the index into
  928. * the table
  929. *
  930. * Returns the device_node pointer with refcount incremented. Use
  931. * of_node_put() on it when done.
  932. */
  933. struct device_node *of_parse_phandle(const struct device_node *np,
  934. const char *phandle_name, int index)
  935. {
  936. const __be32 *phandle;
  937. int size;
  938. phandle = of_get_property(np, phandle_name, &size);
  939. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  940. return NULL;
  941. return of_find_node_by_phandle(be32_to_cpup(phandle + index));
  942. }
  943. EXPORT_SYMBOL(of_parse_phandle);
  944. /**
  945. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  946. * @np: pointer to a device tree node containing a list
  947. * @list_name: property name that contains a list
  948. * @cells_name: property name that specifies phandles' arguments count
  949. * @index: index of a phandle to parse out
  950. * @out_args: optional pointer to output arguments structure (will be filled)
  951. *
  952. * This function is useful to parse lists of phandles and their arguments.
  953. * Returns 0 on success and fills out_args, on error returns appropriate
  954. * errno value.
  955. *
  956. * Caller is responsible to call of_node_put() on the returned out_args->node
  957. * pointer.
  958. *
  959. * Example:
  960. *
  961. * phandle1: node1 {
  962. * #list-cells = <2>;
  963. * }
  964. *
  965. * phandle2: node2 {
  966. * #list-cells = <1>;
  967. * }
  968. *
  969. * node3 {
  970. * list = <&phandle1 1 2 &phandle2 3>;
  971. * }
  972. *
  973. * To get a device_node of the `node2' node you may call this:
  974. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  975. */
  976. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  977. const char *cells_name, int index,
  978. struct of_phandle_args *out_args)
  979. {
  980. const __be32 *list, *list_end;
  981. int size, cur_index = 0;
  982. uint32_t count = 0;
  983. struct device_node *node = NULL;
  984. phandle phandle;
  985. /* Retrieve the phandle list property */
  986. list = of_get_property(np, list_name, &size);
  987. if (!list)
  988. return -ENOENT;
  989. list_end = list + size / sizeof(*list);
  990. /* Loop over the phandles until all the requested entry is found */
  991. while (list < list_end) {
  992. count = 0;
  993. /*
  994. * If phandle is 0, then it is an empty entry with no
  995. * arguments. Skip forward to the next entry.
  996. */
  997. phandle = be32_to_cpup(list++);
  998. if (phandle) {
  999. /*
  1000. * Find the provider node and parse the #*-cells
  1001. * property to determine the argument length
  1002. */
  1003. node = of_find_node_by_phandle(phandle);
  1004. if (!node) {
  1005. pr_err("%s: could not find phandle\n",
  1006. np->full_name);
  1007. break;
  1008. }
  1009. if (of_property_read_u32(node, cells_name, &count)) {
  1010. pr_err("%s: could not get %s for %s\n",
  1011. np->full_name, cells_name,
  1012. node->full_name);
  1013. break;
  1014. }
  1015. /*
  1016. * Make sure that the arguments actually fit in the
  1017. * remaining property data length
  1018. */
  1019. if (list + count > list_end) {
  1020. pr_err("%s: arguments longer than property\n",
  1021. np->full_name);
  1022. break;
  1023. }
  1024. }
  1025. /*
  1026. * All of the error cases above bail out of the loop, so at
  1027. * this point, the parsing is successful. If the requested
  1028. * index matches, then fill the out_args structure and return,
  1029. * or return -ENOENT for an empty entry.
  1030. */
  1031. if (cur_index == index) {
  1032. if (!phandle)
  1033. return -ENOENT;
  1034. if (out_args) {
  1035. int i;
  1036. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  1037. count = MAX_PHANDLE_ARGS;
  1038. out_args->np = node;
  1039. out_args->args_count = count;
  1040. for (i = 0; i < count; i++)
  1041. out_args->args[i] = be32_to_cpup(list++);
  1042. }
  1043. return 0;
  1044. }
  1045. of_node_put(node);
  1046. node = NULL;
  1047. list += count;
  1048. cur_index++;
  1049. }
  1050. /* Loop exited without finding a valid entry; return an error */
  1051. if (node)
  1052. of_node_put(node);
  1053. return -EINVAL;
  1054. }
  1055. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1056. #if defined(CONFIG_OF_DYNAMIC)
  1057. static int of_property_notify(int action, struct device_node *np,
  1058. struct property *prop)
  1059. {
  1060. struct of_prop_reconfig pr;
  1061. pr.dn = np;
  1062. pr.prop = prop;
  1063. return of_reconfig_notify(action, &pr);
  1064. }
  1065. #else
  1066. static int of_property_notify(int action, struct device_node *np,
  1067. struct property *prop)
  1068. {
  1069. return 0;
  1070. }
  1071. #endif
  1072. /**
  1073. * of_add_property - Add a property to a node
  1074. */
  1075. int of_add_property(struct device_node *np, struct property *prop)
  1076. {
  1077. struct property **next;
  1078. unsigned long flags;
  1079. int rc;
  1080. rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
  1081. if (rc)
  1082. return rc;
  1083. prop->next = NULL;
  1084. write_lock_irqsave(&devtree_lock, flags);
  1085. next = &np->properties;
  1086. while (*next) {
  1087. if (strcmp(prop->name, (*next)->name) == 0) {
  1088. /* duplicate ! don't insert it */
  1089. write_unlock_irqrestore(&devtree_lock, flags);
  1090. return -1;
  1091. }
  1092. next = &(*next)->next;
  1093. }
  1094. *next = prop;
  1095. write_unlock_irqrestore(&devtree_lock, flags);
  1096. #ifdef CONFIG_PROC_DEVICETREE
  1097. /* try to add to proc as well if it was initialized */
  1098. if (np->pde)
  1099. proc_device_tree_add_prop(np->pde, prop);
  1100. #endif /* CONFIG_PROC_DEVICETREE */
  1101. return 0;
  1102. }
  1103. /**
  1104. * of_remove_property - Remove a property from a node.
  1105. *
  1106. * Note that we don't actually remove it, since we have given out
  1107. * who-knows-how-many pointers to the data using get-property.
  1108. * Instead we just move the property to the "dead properties"
  1109. * list, so it won't be found any more.
  1110. */
  1111. int of_remove_property(struct device_node *np, struct property *prop)
  1112. {
  1113. struct property **next;
  1114. unsigned long flags;
  1115. int found = 0;
  1116. int rc;
  1117. rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  1118. if (rc)
  1119. return rc;
  1120. write_lock_irqsave(&devtree_lock, flags);
  1121. next = &np->properties;
  1122. while (*next) {
  1123. if (*next == prop) {
  1124. /* found the node */
  1125. *next = prop->next;
  1126. prop->next = np->deadprops;
  1127. np->deadprops = prop;
  1128. found = 1;
  1129. break;
  1130. }
  1131. next = &(*next)->next;
  1132. }
  1133. write_unlock_irqrestore(&devtree_lock, flags);
  1134. if (!found)
  1135. return -ENODEV;
  1136. #ifdef CONFIG_PROC_DEVICETREE
  1137. /* try to remove the proc node as well */
  1138. if (np->pde)
  1139. proc_device_tree_remove_prop(np->pde, prop);
  1140. #endif /* CONFIG_PROC_DEVICETREE */
  1141. return 0;
  1142. }
  1143. /*
  1144. * of_update_property - Update a property in a node, if the property does
  1145. * not exist, add it.
  1146. *
  1147. * Note that we don't actually remove it, since we have given out
  1148. * who-knows-how-many pointers to the data using get-property.
  1149. * Instead we just move the property to the "dead properties" list,
  1150. * and add the new property to the property list
  1151. */
  1152. int of_update_property(struct device_node *np, struct property *newprop)
  1153. {
  1154. struct property **next, *oldprop;
  1155. unsigned long flags;
  1156. int rc, found = 0;
  1157. rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
  1158. if (rc)
  1159. return rc;
  1160. if (!newprop->name)
  1161. return -EINVAL;
  1162. oldprop = of_find_property(np, newprop->name, NULL);
  1163. if (!oldprop)
  1164. return of_add_property(np, newprop);
  1165. write_lock_irqsave(&devtree_lock, flags);
  1166. next = &np->properties;
  1167. while (*next) {
  1168. if (*next == oldprop) {
  1169. /* found the node */
  1170. newprop->next = oldprop->next;
  1171. *next = newprop;
  1172. oldprop->next = np->deadprops;
  1173. np->deadprops = oldprop;
  1174. found = 1;
  1175. break;
  1176. }
  1177. next = &(*next)->next;
  1178. }
  1179. write_unlock_irqrestore(&devtree_lock, flags);
  1180. if (!found)
  1181. return -ENODEV;
  1182. #ifdef CONFIG_PROC_DEVICETREE
  1183. /* try to add to proc as well if it was initialized */
  1184. if (np->pde)
  1185. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1186. #endif /* CONFIG_PROC_DEVICETREE */
  1187. return 0;
  1188. }
  1189. #if defined(CONFIG_OF_DYNAMIC)
  1190. /*
  1191. * Support for dynamic device trees.
  1192. *
  1193. * On some platforms, the device tree can be manipulated at runtime.
  1194. * The routines in this section support adding, removing and changing
  1195. * device tree nodes.
  1196. */
  1197. static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
  1198. int of_reconfig_notifier_register(struct notifier_block *nb)
  1199. {
  1200. return blocking_notifier_chain_register(&of_reconfig_chain, nb);
  1201. }
  1202. EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
  1203. int of_reconfig_notifier_unregister(struct notifier_block *nb)
  1204. {
  1205. return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
  1206. }
  1207. EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
  1208. int of_reconfig_notify(unsigned long action, void *p)
  1209. {
  1210. int rc;
  1211. rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
  1212. return notifier_to_errno(rc);
  1213. }
  1214. #ifdef CONFIG_PROC_DEVICETREE
  1215. static void of_add_proc_dt_entry(struct device_node *dn)
  1216. {
  1217. struct proc_dir_entry *ent;
  1218. ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
  1219. if (ent)
  1220. proc_device_tree_add_node(dn, ent);
  1221. }
  1222. #else
  1223. static void of_add_proc_dt_entry(struct device_node *dn)
  1224. {
  1225. return;
  1226. }
  1227. #endif
  1228. /**
  1229. * of_attach_node - Plug a device node into the tree and global list.
  1230. */
  1231. int of_attach_node(struct device_node *np)
  1232. {
  1233. unsigned long flags;
  1234. int rc;
  1235. rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
  1236. if (rc)
  1237. return rc;
  1238. write_lock_irqsave(&devtree_lock, flags);
  1239. np->sibling = np->parent->child;
  1240. np->allnext = of_allnodes;
  1241. np->parent->child = np;
  1242. of_allnodes = np;
  1243. write_unlock_irqrestore(&devtree_lock, flags);
  1244. of_add_proc_dt_entry(np);
  1245. return 0;
  1246. }
  1247. #ifdef CONFIG_PROC_DEVICETREE
  1248. static void of_remove_proc_dt_entry(struct device_node *dn)
  1249. {
  1250. struct device_node *parent = dn->parent;
  1251. struct property *prop = dn->properties;
  1252. while (prop) {
  1253. remove_proc_entry(prop->name, dn->pde);
  1254. prop = prop->next;
  1255. }
  1256. if (dn->pde)
  1257. remove_proc_entry(dn->pde->name, parent->pde);
  1258. }
  1259. #else
  1260. static void of_remove_proc_dt_entry(struct device_node *dn)
  1261. {
  1262. return;
  1263. }
  1264. #endif
  1265. /**
  1266. * of_detach_node - "Unplug" a node from the device tree.
  1267. *
  1268. * The caller must hold a reference to the node. The memory associated with
  1269. * the node is not freed until its refcount goes to zero.
  1270. */
  1271. int of_detach_node(struct device_node *np)
  1272. {
  1273. struct device_node *parent;
  1274. unsigned long flags;
  1275. int rc = 0;
  1276. rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
  1277. if (rc)
  1278. return rc;
  1279. write_lock_irqsave(&devtree_lock, flags);
  1280. if (of_node_check_flag(np, OF_DETACHED)) {
  1281. /* someone already detached it */
  1282. write_unlock_irqrestore(&devtree_lock, flags);
  1283. return rc;
  1284. }
  1285. parent = np->parent;
  1286. if (!parent) {
  1287. write_unlock_irqrestore(&devtree_lock, flags);
  1288. return rc;
  1289. }
  1290. if (of_allnodes == np)
  1291. of_allnodes = np->allnext;
  1292. else {
  1293. struct device_node *prev;
  1294. for (prev = of_allnodes;
  1295. prev->allnext != np;
  1296. prev = prev->allnext)
  1297. ;
  1298. prev->allnext = np->allnext;
  1299. }
  1300. if (parent->child == np)
  1301. parent->child = np->sibling;
  1302. else {
  1303. struct device_node *prevsib;
  1304. for (prevsib = np->parent->child;
  1305. prevsib->sibling != np;
  1306. prevsib = prevsib->sibling)
  1307. ;
  1308. prevsib->sibling = np->sibling;
  1309. }
  1310. of_node_set_flag(np, OF_DETACHED);
  1311. write_unlock_irqrestore(&devtree_lock, flags);
  1312. of_remove_proc_dt_entry(np);
  1313. return rc;
  1314. }
  1315. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1316. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1317. int id, const char *stem, int stem_len)
  1318. {
  1319. ap->np = np;
  1320. ap->id = id;
  1321. strncpy(ap->stem, stem, stem_len);
  1322. ap->stem[stem_len] = 0;
  1323. list_add_tail(&ap->link, &aliases_lookup);
  1324. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1325. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1326. }
  1327. /**
  1328. * of_alias_scan - Scan all properties of 'aliases' node
  1329. *
  1330. * The function scans all the properties of 'aliases' node and populate
  1331. * the the global lookup table with the properties. It returns the
  1332. * number of alias_prop found, or error code in error case.
  1333. *
  1334. * @dt_alloc: An allocator that provides a virtual address to memory
  1335. * for the resulting tree
  1336. */
  1337. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1338. {
  1339. struct property *pp;
  1340. of_chosen = of_find_node_by_path("/chosen");
  1341. if (of_chosen == NULL)
  1342. of_chosen = of_find_node_by_path("/chosen@0");
  1343. of_aliases = of_find_node_by_path("/aliases");
  1344. if (!of_aliases)
  1345. return;
  1346. for_each_property_of_node(of_aliases, pp) {
  1347. const char *start = pp->name;
  1348. const char *end = start + strlen(start);
  1349. struct device_node *np;
  1350. struct alias_prop *ap;
  1351. int id, len;
  1352. /* Skip those we do not want to proceed */
  1353. if (!strcmp(pp->name, "name") ||
  1354. !strcmp(pp->name, "phandle") ||
  1355. !strcmp(pp->name, "linux,phandle"))
  1356. continue;
  1357. np = of_find_node_by_path(pp->value);
  1358. if (!np)
  1359. continue;
  1360. /* walk the alias backwards to extract the id and work out
  1361. * the 'stem' string */
  1362. while (isdigit(*(end-1)) && end > start)
  1363. end--;
  1364. len = end - start;
  1365. if (kstrtoint(end, 10, &id) < 0)
  1366. continue;
  1367. /* Allocate an alias_prop with enough space for the stem */
  1368. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1369. if (!ap)
  1370. continue;
  1371. ap->alias = start;
  1372. of_alias_add(ap, np, id, start, len);
  1373. }
  1374. }
  1375. /**
  1376. * of_alias_get_id - Get alias id for the given device_node
  1377. * @np: Pointer to the given device_node
  1378. * @stem: Alias stem of the given device_node
  1379. *
  1380. * The function travels the lookup table to get alias id for the given
  1381. * device_node and alias stem. It returns the alias id if find it.
  1382. */
  1383. int of_alias_get_id(struct device_node *np, const char *stem)
  1384. {
  1385. struct alias_prop *app;
  1386. int id = -ENODEV;
  1387. mutex_lock(&of_aliases_mutex);
  1388. list_for_each_entry(app, &aliases_lookup, link) {
  1389. if (strcmp(app->stem, stem) != 0)
  1390. continue;
  1391. if (np == app->np) {
  1392. id = app->id;
  1393. break;
  1394. }
  1395. }
  1396. mutex_unlock(&of_aliases_mutex);
  1397. return id;
  1398. }
  1399. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1400. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1401. u32 *pu)
  1402. {
  1403. const void *curv = cur;
  1404. if (!prop)
  1405. return NULL;
  1406. if (!cur) {
  1407. curv = prop->value;
  1408. goto out_val;
  1409. }
  1410. curv += sizeof(*cur);
  1411. if (curv >= prop->value + prop->length)
  1412. return NULL;
  1413. out_val:
  1414. *pu = be32_to_cpup(curv);
  1415. return curv;
  1416. }
  1417. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1418. const char *of_prop_next_string(struct property *prop, const char *cur)
  1419. {
  1420. const void *curv = cur;
  1421. if (!prop)
  1422. return NULL;
  1423. if (!cur)
  1424. return prop->value;
  1425. curv += strlen(cur) + 1;
  1426. if (curv >= prop->value + prop->length)
  1427. return NULL;
  1428. return curv;
  1429. }
  1430. EXPORT_SYMBOL_GPL(of_prop_next_string);