base.c 43 KB

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