base.c 45 KB

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