property.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. * property.c - Unified device property interface.
  3. *
  4. * Copyright (C) 2014, Intel Corporation
  5. * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  6. * Mika Westerberg <mika.westerberg@linux.intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/export.h>
  14. #include <linux/kernel.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_graph.h>
  18. #include <linux/property.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/phy.h>
  21. struct property_set {
  22. struct fwnode_handle fwnode;
  23. const struct property_entry *properties;
  24. };
  25. static const struct fwnode_operations pset_fwnode_ops;
  26. static inline bool is_pset_node(const struct fwnode_handle *fwnode)
  27. {
  28. return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &pset_fwnode_ops;
  29. }
  30. #define to_pset_node(__fwnode) \
  31. ({ \
  32. typeof(__fwnode) __to_pset_node_fwnode = __fwnode; \
  33. \
  34. is_pset_node(__to_pset_node_fwnode) ? \
  35. container_of(__to_pset_node_fwnode, \
  36. struct property_set, fwnode) : \
  37. NULL; \
  38. })
  39. static const struct property_entry *
  40. pset_prop_get(const struct property_set *pset, const char *name)
  41. {
  42. const struct property_entry *prop;
  43. if (!pset || !pset->properties)
  44. return NULL;
  45. for (prop = pset->properties; prop->name; prop++)
  46. if (!strcmp(name, prop->name))
  47. return prop;
  48. return NULL;
  49. }
  50. static const void *pset_prop_find(const struct property_set *pset,
  51. const char *propname, size_t length)
  52. {
  53. const struct property_entry *prop;
  54. const void *pointer;
  55. prop = pset_prop_get(pset, propname);
  56. if (!prop)
  57. return ERR_PTR(-EINVAL);
  58. if (prop->is_array)
  59. pointer = prop->pointer.raw_data;
  60. else
  61. pointer = &prop->value.raw_data;
  62. if (!pointer)
  63. return ERR_PTR(-ENODATA);
  64. if (length > prop->length)
  65. return ERR_PTR(-EOVERFLOW);
  66. return pointer;
  67. }
  68. static int pset_prop_read_u8_array(const struct property_set *pset,
  69. const char *propname,
  70. u8 *values, size_t nval)
  71. {
  72. const void *pointer;
  73. size_t length = nval * sizeof(*values);
  74. pointer = pset_prop_find(pset, propname, length);
  75. if (IS_ERR(pointer))
  76. return PTR_ERR(pointer);
  77. memcpy(values, pointer, length);
  78. return 0;
  79. }
  80. static int pset_prop_read_u16_array(const struct property_set *pset,
  81. const char *propname,
  82. u16 *values, size_t nval)
  83. {
  84. const void *pointer;
  85. size_t length = nval * sizeof(*values);
  86. pointer = pset_prop_find(pset, propname, length);
  87. if (IS_ERR(pointer))
  88. return PTR_ERR(pointer);
  89. memcpy(values, pointer, length);
  90. return 0;
  91. }
  92. static int pset_prop_read_u32_array(const struct property_set *pset,
  93. const char *propname,
  94. u32 *values, size_t nval)
  95. {
  96. const void *pointer;
  97. size_t length = nval * sizeof(*values);
  98. pointer = pset_prop_find(pset, propname, length);
  99. if (IS_ERR(pointer))
  100. return PTR_ERR(pointer);
  101. memcpy(values, pointer, length);
  102. return 0;
  103. }
  104. static int pset_prop_read_u64_array(const struct property_set *pset,
  105. const char *propname,
  106. u64 *values, size_t nval)
  107. {
  108. const void *pointer;
  109. size_t length = nval * sizeof(*values);
  110. pointer = pset_prop_find(pset, propname, length);
  111. if (IS_ERR(pointer))
  112. return PTR_ERR(pointer);
  113. memcpy(values, pointer, length);
  114. return 0;
  115. }
  116. static int pset_prop_count_elems_of_size(const struct property_set *pset,
  117. const char *propname, size_t length)
  118. {
  119. const struct property_entry *prop;
  120. prop = pset_prop_get(pset, propname);
  121. if (!prop)
  122. return -EINVAL;
  123. return prop->length / length;
  124. }
  125. static int pset_prop_read_string_array(const struct property_set *pset,
  126. const char *propname,
  127. const char **strings, size_t nval)
  128. {
  129. const struct property_entry *prop;
  130. const void *pointer;
  131. size_t array_len, length;
  132. /* Find out the array length. */
  133. prop = pset_prop_get(pset, propname);
  134. if (!prop)
  135. return -EINVAL;
  136. if (!prop->is_array)
  137. /* The array length for a non-array string property is 1. */
  138. array_len = 1;
  139. else
  140. /* Find the length of an array. */
  141. array_len = pset_prop_count_elems_of_size(pset, propname,
  142. sizeof(const char *));
  143. /* Return how many there are if strings is NULL. */
  144. if (!strings)
  145. return array_len;
  146. array_len = min(nval, array_len);
  147. length = array_len * sizeof(*strings);
  148. pointer = pset_prop_find(pset, propname, length);
  149. if (IS_ERR(pointer))
  150. return PTR_ERR(pointer);
  151. memcpy(strings, pointer, length);
  152. return array_len;
  153. }
  154. struct fwnode_handle *dev_fwnode(struct device *dev)
  155. {
  156. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  157. &dev->of_node->fwnode : dev->fwnode;
  158. }
  159. EXPORT_SYMBOL_GPL(dev_fwnode);
  160. static bool pset_fwnode_property_present(const struct fwnode_handle *fwnode,
  161. const char *propname)
  162. {
  163. return !!pset_prop_get(to_pset_node(fwnode), propname);
  164. }
  165. static int pset_fwnode_read_int_array(const struct fwnode_handle *fwnode,
  166. const char *propname,
  167. unsigned int elem_size, void *val,
  168. size_t nval)
  169. {
  170. const struct property_set *node = to_pset_node(fwnode);
  171. if (!val)
  172. return pset_prop_count_elems_of_size(node, propname, elem_size);
  173. switch (elem_size) {
  174. case sizeof(u8):
  175. return pset_prop_read_u8_array(node, propname, val, nval);
  176. case sizeof(u16):
  177. return pset_prop_read_u16_array(node, propname, val, nval);
  178. case sizeof(u32):
  179. return pset_prop_read_u32_array(node, propname, val, nval);
  180. case sizeof(u64):
  181. return pset_prop_read_u64_array(node, propname, val, nval);
  182. }
  183. return -ENXIO;
  184. }
  185. static int
  186. pset_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  187. const char *propname,
  188. const char **val, size_t nval)
  189. {
  190. return pset_prop_read_string_array(to_pset_node(fwnode), propname,
  191. val, nval);
  192. }
  193. static const struct fwnode_operations pset_fwnode_ops = {
  194. .property_present = pset_fwnode_property_present,
  195. .property_read_int_array = pset_fwnode_read_int_array,
  196. .property_read_string_array = pset_fwnode_property_read_string_array,
  197. };
  198. /**
  199. * device_property_present - check if a property of a device is present
  200. * @dev: Device whose property is being checked
  201. * @propname: Name of the property
  202. *
  203. * Check if property @propname is present in the device firmware description.
  204. */
  205. bool device_property_present(struct device *dev, const char *propname)
  206. {
  207. return fwnode_property_present(dev_fwnode(dev), propname);
  208. }
  209. EXPORT_SYMBOL_GPL(device_property_present);
  210. /**
  211. * fwnode_property_present - check if a property of a firmware node is present
  212. * @fwnode: Firmware node whose property to check
  213. * @propname: Name of the property
  214. */
  215. bool fwnode_property_present(const struct fwnode_handle *fwnode,
  216. const char *propname)
  217. {
  218. bool ret;
  219. ret = fwnode_call_bool_op(fwnode, property_present, propname);
  220. if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
  221. !IS_ERR_OR_NULL(fwnode->secondary))
  222. ret = fwnode_call_bool_op(fwnode->secondary, property_present,
  223. propname);
  224. return ret;
  225. }
  226. EXPORT_SYMBOL_GPL(fwnode_property_present);
  227. /**
  228. * device_property_read_u8_array - return a u8 array property of a device
  229. * @dev: Device to get the property of
  230. * @propname: Name of the property
  231. * @val: The values are stored here or %NULL to return the number of values
  232. * @nval: Size of the @val array
  233. *
  234. * Function reads an array of u8 properties with @propname from the device
  235. * firmware description and stores them to @val if found.
  236. *
  237. * Return: number of values if @val was %NULL,
  238. * %0 if the property was found (success),
  239. * %-EINVAL if given arguments are not valid,
  240. * %-ENODATA if the property does not have a value,
  241. * %-EPROTO if the property is not an array of numbers,
  242. * %-EOVERFLOW if the size of the property is not as expected.
  243. * %-ENXIO if no suitable firmware interface is present.
  244. */
  245. int device_property_read_u8_array(struct device *dev, const char *propname,
  246. u8 *val, size_t nval)
  247. {
  248. return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
  249. }
  250. EXPORT_SYMBOL_GPL(device_property_read_u8_array);
  251. /**
  252. * device_property_read_u16_array - return a u16 array property of a device
  253. * @dev: Device to get the property of
  254. * @propname: Name of the property
  255. * @val: The values are stored here or %NULL to return the number of values
  256. * @nval: Size of the @val array
  257. *
  258. * Function reads an array of u16 properties with @propname from the device
  259. * firmware description and stores them to @val if found.
  260. *
  261. * Return: number of values if @val was %NULL,
  262. * %0 if the property was found (success),
  263. * %-EINVAL if given arguments are not valid,
  264. * %-ENODATA if the property does not have a value,
  265. * %-EPROTO if the property is not an array of numbers,
  266. * %-EOVERFLOW if the size of the property is not as expected.
  267. * %-ENXIO if no suitable firmware interface is present.
  268. */
  269. int device_property_read_u16_array(struct device *dev, const char *propname,
  270. u16 *val, size_t nval)
  271. {
  272. return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
  273. }
  274. EXPORT_SYMBOL_GPL(device_property_read_u16_array);
  275. /**
  276. * device_property_read_u32_array - return a u32 array property of a device
  277. * @dev: Device to get the property of
  278. * @propname: Name of the property
  279. * @val: The values are stored here or %NULL to return the number of values
  280. * @nval: Size of the @val array
  281. *
  282. * Function reads an array of u32 properties with @propname from the device
  283. * firmware description and stores them to @val if found.
  284. *
  285. * Return: number of values if @val was %NULL,
  286. * %0 if the property was found (success),
  287. * %-EINVAL if given arguments are not valid,
  288. * %-ENODATA if the property does not have a value,
  289. * %-EPROTO if the property is not an array of numbers,
  290. * %-EOVERFLOW if the size of the property is not as expected.
  291. * %-ENXIO if no suitable firmware interface is present.
  292. */
  293. int device_property_read_u32_array(struct device *dev, const char *propname,
  294. u32 *val, size_t nval)
  295. {
  296. return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
  297. }
  298. EXPORT_SYMBOL_GPL(device_property_read_u32_array);
  299. /**
  300. * device_property_read_u64_array - return a u64 array property of a device
  301. * @dev: Device to get the property of
  302. * @propname: Name of the property
  303. * @val: The values are stored here or %NULL to return the number of values
  304. * @nval: Size of the @val array
  305. *
  306. * Function reads an array of u64 properties with @propname from the device
  307. * firmware description and stores them to @val if found.
  308. *
  309. * Return: number of values if @val was %NULL,
  310. * %0 if the property was found (success),
  311. * %-EINVAL if given arguments are not valid,
  312. * %-ENODATA if the property does not have a value,
  313. * %-EPROTO if the property is not an array of numbers,
  314. * %-EOVERFLOW if the size of the property is not as expected.
  315. * %-ENXIO if no suitable firmware interface is present.
  316. */
  317. int device_property_read_u64_array(struct device *dev, const char *propname,
  318. u64 *val, size_t nval)
  319. {
  320. return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
  321. }
  322. EXPORT_SYMBOL_GPL(device_property_read_u64_array);
  323. /**
  324. * device_property_read_string_array - return a string array property of device
  325. * @dev: Device to get the property of
  326. * @propname: Name of the property
  327. * @val: The values are stored here or %NULL to return the number of values
  328. * @nval: Size of the @val array
  329. *
  330. * Function reads an array of string properties with @propname from the device
  331. * firmware description and stores them to @val if found.
  332. *
  333. * Return: number of values read on success if @val is non-NULL,
  334. * number of values available on success if @val is NULL,
  335. * %-EINVAL if given arguments are not valid,
  336. * %-ENODATA if the property does not have a value,
  337. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  338. * %-EOVERFLOW if the size of the property is not as expected.
  339. * %-ENXIO if no suitable firmware interface is present.
  340. */
  341. int device_property_read_string_array(struct device *dev, const char *propname,
  342. const char **val, size_t nval)
  343. {
  344. return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
  345. }
  346. EXPORT_SYMBOL_GPL(device_property_read_string_array);
  347. /**
  348. * device_property_read_string - return a string property of a device
  349. * @dev: Device to get the property of
  350. * @propname: Name of the property
  351. * @val: The value is stored here
  352. *
  353. * Function reads property @propname from the device firmware description and
  354. * stores the value into @val if found. The value is checked to be a string.
  355. *
  356. * Return: %0 if the property was found (success),
  357. * %-EINVAL if given arguments are not valid,
  358. * %-ENODATA if the property does not have a value,
  359. * %-EPROTO or %-EILSEQ if the property type is not a string.
  360. * %-ENXIO if no suitable firmware interface is present.
  361. */
  362. int device_property_read_string(struct device *dev, const char *propname,
  363. const char **val)
  364. {
  365. return fwnode_property_read_string(dev_fwnode(dev), propname, val);
  366. }
  367. EXPORT_SYMBOL_GPL(device_property_read_string);
  368. /**
  369. * device_property_match_string - find a string in an array and return index
  370. * @dev: Device to get the property of
  371. * @propname: Name of the property holding the array
  372. * @string: String to look for
  373. *
  374. * Find a given string in a string array and if it is found return the
  375. * index back.
  376. *
  377. * Return: %0 if the property was found (success),
  378. * %-EINVAL if given arguments are not valid,
  379. * %-ENODATA if the property does not have a value,
  380. * %-EPROTO if the property is not an array of strings,
  381. * %-ENXIO if no suitable firmware interface is present.
  382. */
  383. int device_property_match_string(struct device *dev, const char *propname,
  384. const char *string)
  385. {
  386. return fwnode_property_match_string(dev_fwnode(dev), propname, string);
  387. }
  388. EXPORT_SYMBOL_GPL(device_property_match_string);
  389. static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
  390. const char *propname,
  391. unsigned int elem_size, void *val,
  392. size_t nval)
  393. {
  394. int ret;
  395. ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
  396. elem_size, val, nval);
  397. if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
  398. !IS_ERR_OR_NULL(fwnode->secondary))
  399. ret = fwnode_call_int_op(
  400. fwnode->secondary, property_read_int_array, propname,
  401. elem_size, val, nval);
  402. return ret;
  403. }
  404. /**
  405. * fwnode_property_read_u8_array - return a u8 array property of firmware node
  406. * @fwnode: Firmware node to get the property of
  407. * @propname: Name of the property
  408. * @val: The values are stored here or %NULL to return the number of values
  409. * @nval: Size of the @val array
  410. *
  411. * Read an array of u8 properties with @propname from @fwnode and stores them to
  412. * @val if found.
  413. *
  414. * Return: number of values if @val was %NULL,
  415. * %0 if the property was found (success),
  416. * %-EINVAL if given arguments are not valid,
  417. * %-ENODATA if the property does not have a value,
  418. * %-EPROTO if the property is not an array of numbers,
  419. * %-EOVERFLOW if the size of the property is not as expected,
  420. * %-ENXIO if no suitable firmware interface is present.
  421. */
  422. int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
  423. const char *propname, u8 *val, size_t nval)
  424. {
  425. return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
  426. val, nval);
  427. }
  428. EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
  429. /**
  430. * fwnode_property_read_u16_array - return a u16 array property of firmware node
  431. * @fwnode: Firmware node to get the property of
  432. * @propname: Name of the property
  433. * @val: The values are stored here or %NULL to return the number of values
  434. * @nval: Size of the @val array
  435. *
  436. * Read an array of u16 properties with @propname from @fwnode and store them to
  437. * @val if found.
  438. *
  439. * Return: number of values if @val was %NULL,
  440. * %0 if the property was found (success),
  441. * %-EINVAL if given arguments are not valid,
  442. * %-ENODATA if the property does not have a value,
  443. * %-EPROTO if the property is not an array of numbers,
  444. * %-EOVERFLOW if the size of the property is not as expected,
  445. * %-ENXIO if no suitable firmware interface is present.
  446. */
  447. int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
  448. const char *propname, u16 *val, size_t nval)
  449. {
  450. return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
  451. val, nval);
  452. }
  453. EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
  454. /**
  455. * fwnode_property_read_u32_array - return a u32 array property of firmware node
  456. * @fwnode: Firmware node to get the property of
  457. * @propname: Name of the property
  458. * @val: The values are stored here or %NULL to return the number of values
  459. * @nval: Size of the @val array
  460. *
  461. * Read an array of u32 properties with @propname from @fwnode store them to
  462. * @val if found.
  463. *
  464. * Return: number of values if @val was %NULL,
  465. * %0 if the property was found (success),
  466. * %-EINVAL if given arguments are not valid,
  467. * %-ENODATA if the property does not have a value,
  468. * %-EPROTO if the property is not an array of numbers,
  469. * %-EOVERFLOW if the size of the property is not as expected,
  470. * %-ENXIO if no suitable firmware interface is present.
  471. */
  472. int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
  473. const char *propname, u32 *val, size_t nval)
  474. {
  475. return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
  476. val, nval);
  477. }
  478. EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
  479. /**
  480. * fwnode_property_read_u64_array - return a u64 array property firmware node
  481. * @fwnode: Firmware node to get the property of
  482. * @propname: Name of the property
  483. * @val: The values are stored here or %NULL to return the number of values
  484. * @nval: Size of the @val array
  485. *
  486. * Read an array of u64 properties with @propname from @fwnode and store them to
  487. * @val if found.
  488. *
  489. * Return: number of values if @val was %NULL,
  490. * %0 if the property was found (success),
  491. * %-EINVAL if given arguments are not valid,
  492. * %-ENODATA if the property does not have a value,
  493. * %-EPROTO if the property is not an array of numbers,
  494. * %-EOVERFLOW if the size of the property is not as expected,
  495. * %-ENXIO if no suitable firmware interface is present.
  496. */
  497. int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
  498. const char *propname, u64 *val, size_t nval)
  499. {
  500. return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
  501. val, nval);
  502. }
  503. EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
  504. /**
  505. * fwnode_property_read_string_array - return string array property of a node
  506. * @fwnode: Firmware node to get the property of
  507. * @propname: Name of the property
  508. * @val: The values are stored here or %NULL to return the number of values
  509. * @nval: Size of the @val array
  510. *
  511. * Read an string list property @propname from the given firmware node and store
  512. * them to @val if found.
  513. *
  514. * Return: number of values read on success if @val is non-NULL,
  515. * number of values available on success if @val is NULL,
  516. * %-EINVAL if given arguments are not valid,
  517. * %-ENODATA if the property does not have a value,
  518. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  519. * %-EOVERFLOW if the size of the property is not as expected,
  520. * %-ENXIO if no suitable firmware interface is present.
  521. */
  522. int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  523. const char *propname, const char **val,
  524. size_t nval)
  525. {
  526. int ret;
  527. ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
  528. val, nval);
  529. if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
  530. !IS_ERR_OR_NULL(fwnode->secondary))
  531. ret = fwnode_call_int_op(fwnode->secondary,
  532. property_read_string_array, propname,
  533. val, nval);
  534. return ret;
  535. }
  536. EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
  537. /**
  538. * fwnode_property_read_string - return a string property of a firmware node
  539. * @fwnode: Firmware node to get the property of
  540. * @propname: Name of the property
  541. * @val: The value is stored here
  542. *
  543. * Read property @propname from the given firmware node and store the value into
  544. * @val if found. The value is checked to be a string.
  545. *
  546. * Return: %0 if the property was found (success),
  547. * %-EINVAL if given arguments are not valid,
  548. * %-ENODATA if the property does not have a value,
  549. * %-EPROTO or %-EILSEQ if the property is not a string,
  550. * %-ENXIO if no suitable firmware interface is present.
  551. */
  552. int fwnode_property_read_string(const struct fwnode_handle *fwnode,
  553. const char *propname, const char **val)
  554. {
  555. int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
  556. return ret < 0 ? ret : 0;
  557. }
  558. EXPORT_SYMBOL_GPL(fwnode_property_read_string);
  559. /**
  560. * fwnode_property_match_string - find a string in an array and return index
  561. * @fwnode: Firmware node to get the property of
  562. * @propname: Name of the property holding the array
  563. * @string: String to look for
  564. *
  565. * Find a given string in a string array and if it is found return the
  566. * index back.
  567. *
  568. * Return: %0 if the property was found (success),
  569. * %-EINVAL if given arguments are not valid,
  570. * %-ENODATA if the property does not have a value,
  571. * %-EPROTO if the property is not an array of strings,
  572. * %-ENXIO if no suitable firmware interface is present.
  573. */
  574. int fwnode_property_match_string(const struct fwnode_handle *fwnode,
  575. const char *propname, const char *string)
  576. {
  577. const char **values;
  578. int nval, ret;
  579. nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
  580. if (nval < 0)
  581. return nval;
  582. if (nval == 0)
  583. return -ENODATA;
  584. values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
  585. if (!values)
  586. return -ENOMEM;
  587. ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
  588. if (ret < 0)
  589. goto out;
  590. ret = match_string(values, nval, string);
  591. if (ret < 0)
  592. ret = -ENODATA;
  593. out:
  594. kfree(values);
  595. return ret;
  596. }
  597. EXPORT_SYMBOL_GPL(fwnode_property_match_string);
  598. /**
  599. * fwnode_property_get_reference_args() - Find a reference with arguments
  600. * @fwnode: Firmware node where to look for the reference
  601. * @prop: The name of the property
  602. * @nargs_prop: The name of the property telling the number of
  603. * arguments in the referred node. NULL if @nargs is known,
  604. * otherwise @nargs is ignored. Only relevant on OF.
  605. * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
  606. * @index: Index of the reference, from zero onwards.
  607. * @args: Result structure with reference and integer arguments.
  608. *
  609. * Obtain a reference based on a named property in an fwnode, with
  610. * integer arguments.
  611. *
  612. * Caller is responsible to call fwnode_handle_put() on the returned
  613. * args->fwnode pointer.
  614. *
  615. */
  616. int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
  617. const char *prop, const char *nargs_prop,
  618. unsigned int nargs, unsigned int index,
  619. struct fwnode_reference_args *args)
  620. {
  621. return fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
  622. nargs, index, args);
  623. }
  624. EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
  625. static int property_copy_string_array(struct property_entry *dst,
  626. const struct property_entry *src)
  627. {
  628. char **d;
  629. size_t nval = src->length / sizeof(*d);
  630. int i;
  631. d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
  632. if (!d)
  633. return -ENOMEM;
  634. for (i = 0; i < nval; i++) {
  635. d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
  636. if (!d[i] && src->pointer.str[i]) {
  637. while (--i >= 0)
  638. kfree(d[i]);
  639. kfree(d);
  640. return -ENOMEM;
  641. }
  642. }
  643. dst->pointer.raw_data = d;
  644. return 0;
  645. }
  646. static int property_entry_copy_data(struct property_entry *dst,
  647. const struct property_entry *src)
  648. {
  649. int error;
  650. dst->name = kstrdup(src->name, GFP_KERNEL);
  651. if (!dst->name)
  652. return -ENOMEM;
  653. if (src->is_array) {
  654. if (!src->length) {
  655. error = -ENODATA;
  656. goto out_free_name;
  657. }
  658. if (src->is_string) {
  659. error = property_copy_string_array(dst, src);
  660. if (error)
  661. goto out_free_name;
  662. } else {
  663. dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
  664. src->length, GFP_KERNEL);
  665. if (!dst->pointer.raw_data) {
  666. error = -ENOMEM;
  667. goto out_free_name;
  668. }
  669. }
  670. } else if (src->is_string) {
  671. dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
  672. if (!dst->value.str && src->value.str) {
  673. error = -ENOMEM;
  674. goto out_free_name;
  675. }
  676. } else {
  677. dst->value.raw_data = src->value.raw_data;
  678. }
  679. dst->length = src->length;
  680. dst->is_array = src->is_array;
  681. dst->is_string = src->is_string;
  682. return 0;
  683. out_free_name:
  684. kfree(dst->name);
  685. return error;
  686. }
  687. static void property_entry_free_data(const struct property_entry *p)
  688. {
  689. size_t i, nval;
  690. if (p->is_array) {
  691. if (p->is_string && p->pointer.str) {
  692. nval = p->length / sizeof(const char *);
  693. for (i = 0; i < nval; i++)
  694. kfree(p->pointer.str[i]);
  695. }
  696. kfree(p->pointer.raw_data);
  697. } else if (p->is_string) {
  698. kfree(p->value.str);
  699. }
  700. kfree(p->name);
  701. }
  702. /**
  703. * property_entries_dup - duplicate array of properties
  704. * @properties: array of properties to copy
  705. *
  706. * This function creates a deep copy of the given NULL-terminated array
  707. * of property entries.
  708. */
  709. struct property_entry *
  710. property_entries_dup(const struct property_entry *properties)
  711. {
  712. struct property_entry *p;
  713. int i, n = 0;
  714. while (properties[n].name)
  715. n++;
  716. p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
  717. if (!p)
  718. return ERR_PTR(-ENOMEM);
  719. for (i = 0; i < n; i++) {
  720. int ret = property_entry_copy_data(&p[i], &properties[i]);
  721. if (ret) {
  722. while (--i >= 0)
  723. property_entry_free_data(&p[i]);
  724. kfree(p);
  725. return ERR_PTR(ret);
  726. }
  727. }
  728. return p;
  729. }
  730. EXPORT_SYMBOL_GPL(property_entries_dup);
  731. /**
  732. * property_entries_free - free previously allocated array of properties
  733. * @properties: array of properties to destroy
  734. *
  735. * This function frees given NULL-terminated array of property entries,
  736. * along with their data.
  737. */
  738. void property_entries_free(const struct property_entry *properties)
  739. {
  740. const struct property_entry *p;
  741. for (p = properties; p->name; p++)
  742. property_entry_free_data(p);
  743. kfree(properties);
  744. }
  745. EXPORT_SYMBOL_GPL(property_entries_free);
  746. /**
  747. * pset_free_set - releases memory allocated for copied property set
  748. * @pset: Property set to release
  749. *
  750. * Function takes previously copied property set and releases all the
  751. * memory allocated to it.
  752. */
  753. static void pset_free_set(struct property_set *pset)
  754. {
  755. if (!pset)
  756. return;
  757. property_entries_free(pset->properties);
  758. kfree(pset);
  759. }
  760. /**
  761. * pset_copy_set - copies property set
  762. * @pset: Property set to copy
  763. *
  764. * This function takes a deep copy of the given property set and returns
  765. * pointer to the copy. Call device_free_property_set() to free resources
  766. * allocated in this function.
  767. *
  768. * Return: Pointer to the new property set or error pointer.
  769. */
  770. static struct property_set *pset_copy_set(const struct property_set *pset)
  771. {
  772. struct property_entry *properties;
  773. struct property_set *p;
  774. p = kzalloc(sizeof(*p), GFP_KERNEL);
  775. if (!p)
  776. return ERR_PTR(-ENOMEM);
  777. properties = property_entries_dup(pset->properties);
  778. if (IS_ERR(properties)) {
  779. kfree(p);
  780. return ERR_CAST(properties);
  781. }
  782. p->properties = properties;
  783. return p;
  784. }
  785. /**
  786. * device_remove_properties - Remove properties from a device object.
  787. * @dev: Device whose properties to remove.
  788. *
  789. * The function removes properties previously associated to the device
  790. * secondary firmware node with device_add_properties(). Memory allocated
  791. * to the properties will also be released.
  792. */
  793. void device_remove_properties(struct device *dev)
  794. {
  795. struct fwnode_handle *fwnode;
  796. fwnode = dev_fwnode(dev);
  797. if (!fwnode)
  798. return;
  799. /*
  800. * Pick either primary or secondary node depending which one holds
  801. * the pset. If there is no real firmware node (ACPI/DT) primary
  802. * will hold the pset.
  803. */
  804. if (is_pset_node(fwnode)) {
  805. set_primary_fwnode(dev, NULL);
  806. pset_free_set(to_pset_node(fwnode));
  807. } else {
  808. fwnode = fwnode->secondary;
  809. if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
  810. set_secondary_fwnode(dev, NULL);
  811. pset_free_set(to_pset_node(fwnode));
  812. }
  813. }
  814. }
  815. EXPORT_SYMBOL_GPL(device_remove_properties);
  816. /**
  817. * device_add_properties - Add a collection of properties to a device object.
  818. * @dev: Device to add properties to.
  819. * @properties: Collection of properties to add.
  820. *
  821. * Associate a collection of device properties represented by @properties with
  822. * @dev as its secondary firmware node. The function takes a copy of
  823. * @properties.
  824. */
  825. int device_add_properties(struct device *dev,
  826. const struct property_entry *properties)
  827. {
  828. struct property_set *p, pset;
  829. if (!properties)
  830. return -EINVAL;
  831. pset.properties = properties;
  832. p = pset_copy_set(&pset);
  833. if (IS_ERR(p))
  834. return PTR_ERR(p);
  835. p->fwnode.ops = &pset_fwnode_ops;
  836. set_secondary_fwnode(dev, &p->fwnode);
  837. return 0;
  838. }
  839. EXPORT_SYMBOL_GPL(device_add_properties);
  840. /**
  841. * fwnode_get_next_parent - Iterate to the node's parent
  842. * @fwnode: Firmware whose parent is retrieved
  843. *
  844. * This is like fwnode_get_parent() except that it drops the refcount
  845. * on the passed node, making it suitable for iterating through a
  846. * node's parents.
  847. *
  848. * Returns a node pointer with refcount incremented, use
  849. * fwnode_handle_node() on it when done.
  850. */
  851. struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
  852. {
  853. struct fwnode_handle *parent = fwnode_get_parent(fwnode);
  854. fwnode_handle_put(fwnode);
  855. return parent;
  856. }
  857. EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
  858. /**
  859. * fwnode_get_parent - Return parent firwmare node
  860. * @fwnode: Firmware whose parent is retrieved
  861. *
  862. * Return parent firmware node of the given node if possible or %NULL if no
  863. * parent was available.
  864. */
  865. struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
  866. {
  867. return fwnode_call_ptr_op(fwnode, get_parent);
  868. }
  869. EXPORT_SYMBOL_GPL(fwnode_get_parent);
  870. /**
  871. * fwnode_get_next_child_node - Return the next child node handle for a node
  872. * @fwnode: Firmware node to find the next child node for.
  873. * @child: Handle to one of the node's child nodes or a %NULL handle.
  874. */
  875. struct fwnode_handle *
  876. fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
  877. struct fwnode_handle *child)
  878. {
  879. return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
  880. }
  881. EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
  882. /**
  883. * device_get_next_child_node - Return the next child node handle for a device
  884. * @dev: Device to find the next child node for.
  885. * @child: Handle to one of the device's child nodes or a null handle.
  886. */
  887. struct fwnode_handle *device_get_next_child_node(struct device *dev,
  888. struct fwnode_handle *child)
  889. {
  890. struct acpi_device *adev = ACPI_COMPANION(dev);
  891. struct fwnode_handle *fwnode = NULL;
  892. if (dev->of_node)
  893. fwnode = &dev->of_node->fwnode;
  894. else if (adev)
  895. fwnode = acpi_fwnode_handle(adev);
  896. return fwnode_get_next_child_node(fwnode, child);
  897. }
  898. EXPORT_SYMBOL_GPL(device_get_next_child_node);
  899. /**
  900. * fwnode_get_named_child_node - Return first matching named child node handle
  901. * @fwnode: Firmware node to find the named child node for.
  902. * @childname: String to match child node name against.
  903. */
  904. struct fwnode_handle *
  905. fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
  906. const char *childname)
  907. {
  908. return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
  909. }
  910. EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
  911. /**
  912. * device_get_named_child_node - Return first matching named child node handle
  913. * @dev: Device to find the named child node for.
  914. * @childname: String to match child node name against.
  915. */
  916. struct fwnode_handle *device_get_named_child_node(struct device *dev,
  917. const char *childname)
  918. {
  919. return fwnode_get_named_child_node(dev_fwnode(dev), childname);
  920. }
  921. EXPORT_SYMBOL_GPL(device_get_named_child_node);
  922. /**
  923. * fwnode_handle_get - Obtain a reference to a device node
  924. * @fwnode: Pointer to the device node to obtain the reference to.
  925. */
  926. void fwnode_handle_get(struct fwnode_handle *fwnode)
  927. {
  928. fwnode_call_void_op(fwnode, get);
  929. }
  930. EXPORT_SYMBOL_GPL(fwnode_handle_get);
  931. /**
  932. * fwnode_handle_put - Drop reference to a device node
  933. * @fwnode: Pointer to the device node to drop the reference to.
  934. *
  935. * This has to be used when terminating device_for_each_child_node() iteration
  936. * with break or return to prevent stale device node references from being left
  937. * behind.
  938. */
  939. void fwnode_handle_put(struct fwnode_handle *fwnode)
  940. {
  941. fwnode_call_void_op(fwnode, put);
  942. }
  943. EXPORT_SYMBOL_GPL(fwnode_handle_put);
  944. /**
  945. * fwnode_device_is_available - check if a device is available for use
  946. * @fwnode: Pointer to the fwnode of the device.
  947. */
  948. bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
  949. {
  950. return fwnode_call_bool_op(fwnode, device_is_available);
  951. }
  952. EXPORT_SYMBOL_GPL(fwnode_device_is_available);
  953. /**
  954. * device_get_child_node_count - return the number of child nodes for device
  955. * @dev: Device to cound the child nodes for
  956. */
  957. unsigned int device_get_child_node_count(struct device *dev)
  958. {
  959. struct fwnode_handle *child;
  960. unsigned int count = 0;
  961. device_for_each_child_node(dev, child)
  962. count++;
  963. return count;
  964. }
  965. EXPORT_SYMBOL_GPL(device_get_child_node_count);
  966. bool device_dma_supported(struct device *dev)
  967. {
  968. /* For DT, this is always supported.
  969. * For ACPI, this depends on CCA, which
  970. * is determined by the acpi_dma_supported().
  971. */
  972. if (IS_ENABLED(CONFIG_OF) && dev->of_node)
  973. return true;
  974. return acpi_dma_supported(ACPI_COMPANION(dev));
  975. }
  976. EXPORT_SYMBOL_GPL(device_dma_supported);
  977. enum dev_dma_attr device_get_dma_attr(struct device *dev)
  978. {
  979. enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
  980. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  981. if (of_dma_is_coherent(dev->of_node))
  982. attr = DEV_DMA_COHERENT;
  983. else
  984. attr = DEV_DMA_NON_COHERENT;
  985. } else
  986. attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
  987. return attr;
  988. }
  989. EXPORT_SYMBOL_GPL(device_get_dma_attr);
  990. /**
  991. * device_get_phy_mode - Get phy mode for given device
  992. * @dev: Pointer to the given device
  993. *
  994. * The function gets phy interface string from property 'phy-mode' or
  995. * 'phy-connection-type', and return its index in phy_modes table, or errno in
  996. * error case.
  997. */
  998. int device_get_phy_mode(struct device *dev)
  999. {
  1000. const char *pm;
  1001. int err, i;
  1002. err = device_property_read_string(dev, "phy-mode", &pm);
  1003. if (err < 0)
  1004. err = device_property_read_string(dev,
  1005. "phy-connection-type", &pm);
  1006. if (err < 0)
  1007. return err;
  1008. for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
  1009. if (!strcasecmp(pm, phy_modes(i)))
  1010. return i;
  1011. return -ENODEV;
  1012. }
  1013. EXPORT_SYMBOL_GPL(device_get_phy_mode);
  1014. static void *device_get_mac_addr(struct device *dev,
  1015. const char *name, char *addr,
  1016. int alen)
  1017. {
  1018. int ret = device_property_read_u8_array(dev, name, addr, alen);
  1019. if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
  1020. return addr;
  1021. return NULL;
  1022. }
  1023. /**
  1024. * device_get_mac_address - Get the MAC for a given device
  1025. * @dev: Pointer to the device
  1026. * @addr: Address of buffer to store the MAC in
  1027. * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
  1028. *
  1029. * Search the firmware node for the best MAC address to use. 'mac-address' is
  1030. * checked first, because that is supposed to contain to "most recent" MAC
  1031. * address. If that isn't set, then 'local-mac-address' is checked next,
  1032. * because that is the default address. If that isn't set, then the obsolete
  1033. * 'address' is checked, just in case we're using an old device tree.
  1034. *
  1035. * Note that the 'address' property is supposed to contain a virtual address of
  1036. * the register set, but some DTS files have redefined that property to be the
  1037. * MAC address.
  1038. *
  1039. * All-zero MAC addresses are rejected, because those could be properties that
  1040. * exist in the firmware tables, but were not updated by the firmware. For
  1041. * example, the DTS could define 'mac-address' and 'local-mac-address', with
  1042. * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
  1043. * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
  1044. * exists but is all zeros.
  1045. */
  1046. void *device_get_mac_address(struct device *dev, char *addr, int alen)
  1047. {
  1048. char *res;
  1049. res = device_get_mac_addr(dev, "mac-address", addr, alen);
  1050. if (res)
  1051. return res;
  1052. res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
  1053. if (res)
  1054. return res;
  1055. return device_get_mac_addr(dev, "address", addr, alen);
  1056. }
  1057. EXPORT_SYMBOL(device_get_mac_address);
  1058. /**
  1059. * device_graph_get_next_endpoint - Get next endpoint firmware node
  1060. * @fwnode: Pointer to the parent firmware node
  1061. * @prev: Previous endpoint node or %NULL to get the first
  1062. *
  1063. * Returns an endpoint firmware node pointer or %NULL if no more endpoints
  1064. * are available.
  1065. */
  1066. struct fwnode_handle *
  1067. fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
  1068. struct fwnode_handle *prev)
  1069. {
  1070. return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
  1071. }
  1072. EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
  1073. /**
  1074. * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
  1075. * @endpoint: Endpoint firmware node of the port
  1076. *
  1077. * Return: the firmware node of the device the @endpoint belongs to.
  1078. */
  1079. struct fwnode_handle *
  1080. fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
  1081. {
  1082. struct fwnode_handle *port, *parent;
  1083. port = fwnode_get_parent(endpoint);
  1084. parent = fwnode_call_ptr_op(port, graph_get_port_parent);
  1085. fwnode_handle_put(port);
  1086. return parent;
  1087. }
  1088. EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
  1089. /**
  1090. * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
  1091. * @fwnode: Endpoint firmware node pointing to the remote endpoint
  1092. *
  1093. * Extracts firmware node of a remote device the @fwnode points to.
  1094. */
  1095. struct fwnode_handle *
  1096. fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
  1097. {
  1098. struct fwnode_handle *endpoint, *parent;
  1099. endpoint = fwnode_graph_get_remote_endpoint(fwnode);
  1100. parent = fwnode_graph_get_port_parent(endpoint);
  1101. fwnode_handle_put(endpoint);
  1102. return parent;
  1103. }
  1104. EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
  1105. /**
  1106. * fwnode_graph_get_remote_port - Return fwnode of a remote port
  1107. * @fwnode: Endpoint firmware node pointing to the remote endpoint
  1108. *
  1109. * Extracts firmware node of a remote port the @fwnode points to.
  1110. */
  1111. struct fwnode_handle *
  1112. fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
  1113. {
  1114. return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
  1115. }
  1116. EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
  1117. /**
  1118. * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
  1119. * @fwnode: Endpoint firmware node pointing to the remote endpoint
  1120. *
  1121. * Extracts firmware node of a remote endpoint the @fwnode points to.
  1122. */
  1123. struct fwnode_handle *
  1124. fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
  1125. {
  1126. return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
  1127. }
  1128. EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
  1129. /**
  1130. * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
  1131. * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
  1132. * @port_id: identifier of the parent port node
  1133. * @endpoint_id: identifier of the endpoint node
  1134. *
  1135. * Return: Remote fwnode handle associated with remote endpoint node linked
  1136. * to @node. Use fwnode_node_put() on it when done.
  1137. */
  1138. struct fwnode_handle *
  1139. fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
  1140. u32 endpoint_id)
  1141. {
  1142. struct fwnode_handle *endpoint = NULL;
  1143. while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
  1144. struct fwnode_endpoint fwnode_ep;
  1145. struct fwnode_handle *remote;
  1146. int ret;
  1147. ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
  1148. if (ret < 0)
  1149. continue;
  1150. if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
  1151. continue;
  1152. remote = fwnode_graph_get_remote_port_parent(endpoint);
  1153. if (!remote)
  1154. return NULL;
  1155. return fwnode_device_is_available(remote) ? remote : NULL;
  1156. }
  1157. return NULL;
  1158. }
  1159. EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
  1160. /**
  1161. * fwnode_graph_parse_endpoint - parse common endpoint node properties
  1162. * @fwnode: pointer to endpoint fwnode_handle
  1163. * @endpoint: pointer to the fwnode endpoint data structure
  1164. *
  1165. * Parse @fwnode representing a graph endpoint node and store the
  1166. * information in @endpoint. The caller must hold a reference to
  1167. * @fwnode.
  1168. */
  1169. int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  1170. struct fwnode_endpoint *endpoint)
  1171. {
  1172. memset(endpoint, 0, sizeof(*endpoint));
  1173. return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
  1174. }
  1175. EXPORT_SYMBOL(fwnode_graph_parse_endpoint);