base.c 41 KB

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