property.c 44 KB

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