property.c 40 KB

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