base.c 43 KB

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