base.c 46 KB

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