base.c 46 KB

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