base.c 45 KB

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