property.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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/property.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/phy.h>
  20. /**
  21. * device_add_property_set - Add a collection of properties to a device object.
  22. * @dev: Device to add properties to.
  23. * @pset: Collection of properties to add.
  24. *
  25. * Associate a collection of device properties represented by @pset with @dev
  26. * as its secondary firmware node.
  27. */
  28. void device_add_property_set(struct device *dev, struct property_set *pset)
  29. {
  30. if (!pset)
  31. return;
  32. pset->fwnode.type = FWNODE_PDATA;
  33. set_secondary_fwnode(dev, &pset->fwnode);
  34. }
  35. EXPORT_SYMBOL_GPL(device_add_property_set);
  36. static inline bool is_pset_node(struct fwnode_handle *fwnode)
  37. {
  38. return fwnode && fwnode->type == FWNODE_PDATA;
  39. }
  40. static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
  41. {
  42. return is_pset_node(fwnode) ?
  43. container_of(fwnode, struct property_set, fwnode) : NULL;
  44. }
  45. static struct property_entry *pset_prop_get(struct property_set *pset,
  46. const char *name)
  47. {
  48. struct property_entry *prop;
  49. if (!pset || !pset->properties)
  50. return NULL;
  51. for (prop = pset->properties; prop->name; prop++)
  52. if (!strcmp(name, prop->name))
  53. return prop;
  54. return NULL;
  55. }
  56. static void *pset_prop_find(struct property_set *pset, const char *propname,
  57. size_t length)
  58. {
  59. struct property_entry *prop;
  60. void *pointer;
  61. prop = pset_prop_get(pset, propname);
  62. if (!prop)
  63. return ERR_PTR(-EINVAL);
  64. if (prop->is_array)
  65. pointer = prop->pointer.raw_data;
  66. else
  67. pointer = &prop->value.raw_data;
  68. if (!pointer)
  69. return ERR_PTR(-ENODATA);
  70. if (length > prop->length)
  71. return ERR_PTR(-EOVERFLOW);
  72. return pointer;
  73. }
  74. static int pset_prop_read_u8_array(struct property_set *pset,
  75. const char *propname,
  76. u8 *values, size_t nval)
  77. {
  78. void *pointer;
  79. size_t length = nval * sizeof(*values);
  80. pointer = pset_prop_find(pset, propname, length);
  81. if (IS_ERR(pointer))
  82. return PTR_ERR(pointer);
  83. memcpy(values, pointer, length);
  84. return 0;
  85. }
  86. static int pset_prop_read_u16_array(struct property_set *pset,
  87. const char *propname,
  88. u16 *values, size_t nval)
  89. {
  90. void *pointer;
  91. size_t length = nval * sizeof(*values);
  92. pointer = pset_prop_find(pset, propname, length);
  93. if (IS_ERR(pointer))
  94. return PTR_ERR(pointer);
  95. memcpy(values, pointer, length);
  96. return 0;
  97. }
  98. static int pset_prop_read_u32_array(struct property_set *pset,
  99. const char *propname,
  100. u32 *values, size_t nval)
  101. {
  102. void *pointer;
  103. size_t length = nval * sizeof(*values);
  104. pointer = pset_prop_find(pset, propname, length);
  105. if (IS_ERR(pointer))
  106. return PTR_ERR(pointer);
  107. memcpy(values, pointer, length);
  108. return 0;
  109. }
  110. static int pset_prop_read_u64_array(struct property_set *pset,
  111. const char *propname,
  112. u64 *values, size_t nval)
  113. {
  114. void *pointer;
  115. size_t length = nval * sizeof(*values);
  116. pointer = pset_prop_find(pset, propname, length);
  117. if (IS_ERR(pointer))
  118. return PTR_ERR(pointer);
  119. memcpy(values, pointer, length);
  120. return 0;
  121. }
  122. static int pset_prop_count_elems_of_size(struct property_set *pset,
  123. const char *propname, size_t length)
  124. {
  125. struct property_entry *prop;
  126. prop = pset_prop_get(pset, propname);
  127. if (!prop)
  128. return -EINVAL;
  129. return prop->length / length;
  130. }
  131. static int pset_prop_read_string_array(struct property_set *pset,
  132. const char *propname,
  133. const char **strings, size_t nval)
  134. {
  135. void *pointer;
  136. size_t length = nval * sizeof(*strings);
  137. pointer = pset_prop_find(pset, propname, length);
  138. if (IS_ERR(pointer))
  139. return PTR_ERR(pointer);
  140. memcpy(strings, pointer, length);
  141. return 0;
  142. }
  143. static int pset_prop_read_string(struct property_set *pset,
  144. const char *propname, const char **strings)
  145. {
  146. struct property_entry *prop;
  147. const char **pointer;
  148. prop = pset_prop_get(pset, propname);
  149. if (!prop)
  150. return -EINVAL;
  151. if (!prop->is_string)
  152. return -EILSEQ;
  153. if (prop->is_array) {
  154. pointer = prop->pointer.str;
  155. if (!pointer)
  156. return -ENODATA;
  157. } else {
  158. pointer = &prop->value.str;
  159. if (*pointer && strnlen(*pointer, prop->length) >= prop->length)
  160. return -EILSEQ;
  161. }
  162. *strings = *pointer;
  163. return 0;
  164. }
  165. static inline struct fwnode_handle *dev_fwnode(struct device *dev)
  166. {
  167. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  168. &dev->of_node->fwnode : dev->fwnode;
  169. }
  170. /**
  171. * device_property_present - check if a property of a device is present
  172. * @dev: Device whose property is being checked
  173. * @propname: Name of the property
  174. *
  175. * Check if property @propname is present in the device firmware description.
  176. */
  177. bool device_property_present(struct device *dev, const char *propname)
  178. {
  179. return fwnode_property_present(dev_fwnode(dev), propname);
  180. }
  181. EXPORT_SYMBOL_GPL(device_property_present);
  182. /**
  183. * fwnode_property_present - check if a property of a firmware node is present
  184. * @fwnode: Firmware node whose property to check
  185. * @propname: Name of the property
  186. */
  187. bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
  188. {
  189. if (is_of_node(fwnode))
  190. return of_property_read_bool(to_of_node(fwnode), propname);
  191. else if (is_acpi_node(fwnode))
  192. return !acpi_node_prop_get(fwnode, propname, NULL);
  193. else if (is_pset_node(fwnode))
  194. return !!pset_prop_get(to_pset_node(fwnode), propname);
  195. return false;
  196. }
  197. EXPORT_SYMBOL_GPL(fwnode_property_present);
  198. /**
  199. * device_property_read_u8_array - return a u8 array property of a device
  200. * @dev: Device to get the property of
  201. * @propname: Name of the property
  202. * @val: The values are stored here or %NULL to return the number of values
  203. * @nval: Size of the @val array
  204. *
  205. * Function reads an array of u8 properties with @propname from the device
  206. * firmware description and stores them to @val if found.
  207. *
  208. * Return: number of values if @val was %NULL,
  209. * %0 if the property was found (success),
  210. * %-EINVAL if given arguments are not valid,
  211. * %-ENODATA if the property does not have a value,
  212. * %-EPROTO if the property is not an array of numbers,
  213. * %-EOVERFLOW if the size of the property is not as expected.
  214. * %-ENXIO if no suitable firmware interface is present.
  215. */
  216. int device_property_read_u8_array(struct device *dev, const char *propname,
  217. u8 *val, size_t nval)
  218. {
  219. return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
  220. }
  221. EXPORT_SYMBOL_GPL(device_property_read_u8_array);
  222. /**
  223. * device_property_read_u16_array - return a u16 array property of a device
  224. * @dev: Device to get the property of
  225. * @propname: Name of the property
  226. * @val: The values are stored here or %NULL to return the number of values
  227. * @nval: Size of the @val array
  228. *
  229. * Function reads an array of u16 properties with @propname from the device
  230. * firmware description and stores them to @val if found.
  231. *
  232. * Return: number of values if @val was %NULL,
  233. * %0 if the property was found (success),
  234. * %-EINVAL if given arguments are not valid,
  235. * %-ENODATA if the property does not have a value,
  236. * %-EPROTO if the property is not an array of numbers,
  237. * %-EOVERFLOW if the size of the property is not as expected.
  238. * %-ENXIO if no suitable firmware interface is present.
  239. */
  240. int device_property_read_u16_array(struct device *dev, const char *propname,
  241. u16 *val, size_t nval)
  242. {
  243. return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
  244. }
  245. EXPORT_SYMBOL_GPL(device_property_read_u16_array);
  246. /**
  247. * device_property_read_u32_array - return a u32 array property of a device
  248. * @dev: Device to get the property of
  249. * @propname: Name of the property
  250. * @val: The values are stored here or %NULL to return the number of values
  251. * @nval: Size of the @val array
  252. *
  253. * Function reads an array of u32 properties with @propname from the device
  254. * firmware description and stores them to @val if found.
  255. *
  256. * Return: number of values if @val was %NULL,
  257. * %0 if the property was found (success),
  258. * %-EINVAL if given arguments are not valid,
  259. * %-ENODATA if the property does not have a value,
  260. * %-EPROTO if the property is not an array of numbers,
  261. * %-EOVERFLOW if the size of the property is not as expected.
  262. * %-ENXIO if no suitable firmware interface is present.
  263. */
  264. int device_property_read_u32_array(struct device *dev, const char *propname,
  265. u32 *val, size_t nval)
  266. {
  267. return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
  268. }
  269. EXPORT_SYMBOL_GPL(device_property_read_u32_array);
  270. /**
  271. * device_property_read_u64_array - return a u64 array property of a device
  272. * @dev: Device to get the property of
  273. * @propname: Name of the property
  274. * @val: The values are stored here or %NULL to return the number of values
  275. * @nval: Size of the @val array
  276. *
  277. * Function reads an array of u64 properties with @propname from the device
  278. * firmware description and stores them to @val if found.
  279. *
  280. * Return: number of values if @val was %NULL,
  281. * %0 if the property was found (success),
  282. * %-EINVAL if given arguments are not valid,
  283. * %-ENODATA if the property does not have a value,
  284. * %-EPROTO if the property is not an array of numbers,
  285. * %-EOVERFLOW if the size of the property is not as expected.
  286. * %-ENXIO if no suitable firmware interface is present.
  287. */
  288. int device_property_read_u64_array(struct device *dev, const char *propname,
  289. u64 *val, size_t nval)
  290. {
  291. return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
  292. }
  293. EXPORT_SYMBOL_GPL(device_property_read_u64_array);
  294. /**
  295. * device_property_read_string_array - return a string array property of device
  296. * @dev: Device to get the property of
  297. * @propname: Name of the property
  298. * @val: The values are stored here or %NULL to return the number of values
  299. * @nval: Size of the @val array
  300. *
  301. * Function reads an array of string properties with @propname from the device
  302. * firmware description and stores them to @val if found.
  303. *
  304. * Return: number of values if @val was %NULL,
  305. * %0 if the property was found (success),
  306. * %-EINVAL if given arguments are not valid,
  307. * %-ENODATA if the property does not have a value,
  308. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  309. * %-EOVERFLOW if the size of the property is not as expected.
  310. * %-ENXIO if no suitable firmware interface is present.
  311. */
  312. int device_property_read_string_array(struct device *dev, const char *propname,
  313. const char **val, size_t nval)
  314. {
  315. return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
  316. }
  317. EXPORT_SYMBOL_GPL(device_property_read_string_array);
  318. /**
  319. * device_property_read_string - return a string property of a device
  320. * @dev: Device to get the property of
  321. * @propname: Name of the property
  322. * @val: The value is stored here
  323. *
  324. * Function reads property @propname from the device firmware description and
  325. * stores the value into @val if found. The value is checked to be a string.
  326. *
  327. * Return: %0 if the property was found (success),
  328. * %-EINVAL if given arguments are not valid,
  329. * %-ENODATA if the property does not have a value,
  330. * %-EPROTO or %-EILSEQ if the property type is not a string.
  331. * %-ENXIO if no suitable firmware interface is present.
  332. */
  333. int device_property_read_string(struct device *dev, const char *propname,
  334. const char **val)
  335. {
  336. return fwnode_property_read_string(dev_fwnode(dev), propname, val);
  337. }
  338. EXPORT_SYMBOL_GPL(device_property_read_string);
  339. /**
  340. * device_property_match_string - find a string in an array and return index
  341. * @dev: Device to get the property of
  342. * @propname: Name of the property holding the array
  343. * @string: String to look for
  344. *
  345. * Find a given string in a string array and if it is found return the
  346. * index back.
  347. *
  348. * Return: %0 if the property was found (success),
  349. * %-EINVAL if given arguments are not valid,
  350. * %-ENODATA if the property does not have a value,
  351. * %-EPROTO if the property is not an array of strings,
  352. * %-ENXIO if no suitable firmware interface is present.
  353. */
  354. int device_property_match_string(struct device *dev, const char *propname,
  355. const char *string)
  356. {
  357. return fwnode_property_match_string(dev_fwnode(dev), propname, string);
  358. }
  359. EXPORT_SYMBOL_GPL(device_property_match_string);
  360. #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
  361. (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
  362. : of_property_count_elems_of_size((node), (propname), sizeof(type))
  363. #define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
  364. (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
  365. : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
  366. #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
  367. ({ \
  368. int _ret_; \
  369. if (is_of_node(_fwnode_)) \
  370. _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
  371. _type_, _val_, _nval_); \
  372. else if (is_acpi_node(_fwnode_)) \
  373. _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
  374. _val_, _nval_); \
  375. else if (is_pset_node(_fwnode_)) \
  376. _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
  377. _type_, _val_, _nval_); \
  378. else \
  379. _ret_ = -ENXIO; \
  380. _ret_; \
  381. })
  382. /**
  383. * fwnode_property_read_u8_array - return a u8 array property of firmware node
  384. * @fwnode: Firmware node to get the property of
  385. * @propname: Name of the property
  386. * @val: The values are stored here or %NULL to return the number of values
  387. * @nval: Size of the @val array
  388. *
  389. * Read an array of u8 properties with @propname from @fwnode and stores them to
  390. * @val if found.
  391. *
  392. * Return: number of values if @val was %NULL,
  393. * %0 if the property was found (success),
  394. * %-EINVAL if given arguments are not valid,
  395. * %-ENODATA if the property does not have a value,
  396. * %-EPROTO if the property is not an array of numbers,
  397. * %-EOVERFLOW if the size of the property is not as expected,
  398. * %-ENXIO if no suitable firmware interface is present.
  399. */
  400. int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
  401. const char *propname, u8 *val, size_t nval)
  402. {
  403. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
  404. val, nval);
  405. }
  406. EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
  407. /**
  408. * fwnode_property_read_u16_array - return a u16 array property of firmware node
  409. * @fwnode: Firmware node to get the property of
  410. * @propname: Name of the property
  411. * @val: The values are stored here or %NULL to return the number of values
  412. * @nval: Size of the @val array
  413. *
  414. * Read an array of u16 properties with @propname from @fwnode and store them to
  415. * @val if found.
  416. *
  417. * Return: number of values if @val was %NULL,
  418. * %0 if the property was found (success),
  419. * %-EINVAL if given arguments are not valid,
  420. * %-ENODATA if the property does not have a value,
  421. * %-EPROTO if the property is not an array of numbers,
  422. * %-EOVERFLOW if the size of the property is not as expected,
  423. * %-ENXIO if no suitable firmware interface is present.
  424. */
  425. int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
  426. const char *propname, u16 *val, size_t nval)
  427. {
  428. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
  429. val, nval);
  430. }
  431. EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
  432. /**
  433. * fwnode_property_read_u32_array - return a u32 array property of firmware node
  434. * @fwnode: Firmware node to get the property of
  435. * @propname: Name of the property
  436. * @val: The values are stored here or %NULL to return the number of values
  437. * @nval: Size of the @val array
  438. *
  439. * Read an array of u32 properties with @propname from @fwnode store them to
  440. * @val if found.
  441. *
  442. * Return: number of values if @val was %NULL,
  443. * %0 if the property was found (success),
  444. * %-EINVAL if given arguments are not valid,
  445. * %-ENODATA if the property does not have a value,
  446. * %-EPROTO if the property is not an array of numbers,
  447. * %-EOVERFLOW if the size of the property is not as expected,
  448. * %-ENXIO if no suitable firmware interface is present.
  449. */
  450. int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
  451. const char *propname, u32 *val, size_t nval)
  452. {
  453. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
  454. val, nval);
  455. }
  456. EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
  457. /**
  458. * fwnode_property_read_u64_array - return a u64 array property firmware node
  459. * @fwnode: Firmware node to get the property of
  460. * @propname: Name of the property
  461. * @val: The values are stored here or %NULL to return the number of values
  462. * @nval: Size of the @val array
  463. *
  464. * Read an array of u64 properties with @propname from @fwnode and store them to
  465. * @val if found.
  466. *
  467. * Return: number of values if @val was %NULL,
  468. * %0 if the property was found (success),
  469. * %-EINVAL if given arguments are not valid,
  470. * %-ENODATA if the property does not have a value,
  471. * %-EPROTO if the property is not an array of numbers,
  472. * %-EOVERFLOW if the size of the property is not as expected,
  473. * %-ENXIO if no suitable firmware interface is present.
  474. */
  475. int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
  476. const char *propname, u64 *val, size_t nval)
  477. {
  478. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
  479. val, nval);
  480. }
  481. EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
  482. /**
  483. * fwnode_property_read_string_array - return string array property of a node
  484. * @fwnode: Firmware node to get the property of
  485. * @propname: Name of the property
  486. * @val: The values are stored here or %NULL to return the number of values
  487. * @nval: Size of the @val array
  488. *
  489. * Read an string list property @propname from the given firmware node and store
  490. * them to @val if found.
  491. *
  492. * Return: number of values if @val was %NULL,
  493. * %0 if the property was found (success),
  494. * %-EINVAL if given arguments are not valid,
  495. * %-ENODATA if the property does not have a value,
  496. * %-EPROTO if the property is not an array of strings,
  497. * %-EOVERFLOW if the size of the property is not as expected,
  498. * %-ENXIO if no suitable firmware interface is present.
  499. */
  500. int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
  501. const char *propname, const char **val,
  502. size_t nval)
  503. {
  504. if (is_of_node(fwnode))
  505. return val ?
  506. of_property_read_string_array(to_of_node(fwnode),
  507. propname, val, nval) :
  508. of_property_count_strings(to_of_node(fwnode), propname);
  509. else if (is_acpi_node(fwnode))
  510. return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
  511. val, nval);
  512. else if (is_pset_node(fwnode))
  513. return val ?
  514. pset_prop_read_string_array(to_pset_node(fwnode),
  515. propname, val, nval) :
  516. pset_prop_count_elems_of_size(to_pset_node(fwnode),
  517. propname,
  518. sizeof(const char *));
  519. return -ENXIO;
  520. }
  521. EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
  522. /**
  523. * fwnode_property_read_string - return a string property of a firmware node
  524. * @fwnode: Firmware node to get the property of
  525. * @propname: Name of the property
  526. * @val: The value is stored here
  527. *
  528. * Read property @propname from the given firmware node and store the value into
  529. * @val if found. The value is checked to be a string.
  530. *
  531. * Return: %0 if the property was found (success),
  532. * %-EINVAL if given arguments are not valid,
  533. * %-ENODATA if the property does not have a value,
  534. * %-EPROTO or %-EILSEQ if the property is not a string,
  535. * %-ENXIO if no suitable firmware interface is present.
  536. */
  537. int fwnode_property_read_string(struct fwnode_handle *fwnode,
  538. const char *propname, const char **val)
  539. {
  540. if (is_of_node(fwnode))
  541. return of_property_read_string(to_of_node(fwnode), propname, val);
  542. else if (is_acpi_node(fwnode))
  543. return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
  544. val, 1);
  545. else if (is_pset_node(fwnode))
  546. return pset_prop_read_string(to_pset_node(fwnode), propname, val);
  547. return -ENXIO;
  548. }
  549. EXPORT_SYMBOL_GPL(fwnode_property_read_string);
  550. /**
  551. * fwnode_property_match_string - find a string in an array and return index
  552. * @fwnode: Firmware node to get the property of
  553. * @propname: Name of the property holding the array
  554. * @string: String to look for
  555. *
  556. * Find a given string in a string array and if it is found return the
  557. * index back.
  558. *
  559. * Return: %0 if the property was found (success),
  560. * %-EINVAL if given arguments are not valid,
  561. * %-ENODATA if the property does not have a value,
  562. * %-EPROTO if the property is not an array of strings,
  563. * %-ENXIO if no suitable firmware interface is present.
  564. */
  565. int fwnode_property_match_string(struct fwnode_handle *fwnode,
  566. const char *propname, const char *string)
  567. {
  568. const char **values;
  569. int nval, ret, i;
  570. nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
  571. if (nval < 0)
  572. return nval;
  573. values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
  574. if (!values)
  575. return -ENOMEM;
  576. ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
  577. if (ret < 0)
  578. goto out;
  579. ret = -ENODATA;
  580. for (i = 0; i < nval; i++) {
  581. if (!strcmp(values[i], string)) {
  582. ret = i;
  583. break;
  584. }
  585. }
  586. out:
  587. kfree(values);
  588. return ret;
  589. }
  590. EXPORT_SYMBOL_GPL(fwnode_property_match_string);
  591. /**
  592. * device_get_next_child_node - Return the next child node handle for a device
  593. * @dev: Device to find the next child node for.
  594. * @child: Handle to one of the device's child nodes or a null handle.
  595. */
  596. struct fwnode_handle *device_get_next_child_node(struct device *dev,
  597. struct fwnode_handle *child)
  598. {
  599. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  600. struct device_node *node;
  601. node = of_get_next_available_child(dev->of_node, to_of_node(child));
  602. if (node)
  603. return &node->fwnode;
  604. } else if (IS_ENABLED(CONFIG_ACPI)) {
  605. return acpi_get_next_subnode(dev, child);
  606. }
  607. return NULL;
  608. }
  609. EXPORT_SYMBOL_GPL(device_get_next_child_node);
  610. /**
  611. * fwnode_handle_put - Drop reference to a device node
  612. * @fwnode: Pointer to the device node to drop the reference to.
  613. *
  614. * This has to be used when terminating device_for_each_child_node() iteration
  615. * with break or return to prevent stale device node references from being left
  616. * behind.
  617. */
  618. void fwnode_handle_put(struct fwnode_handle *fwnode)
  619. {
  620. if (is_of_node(fwnode))
  621. of_node_put(to_of_node(fwnode));
  622. }
  623. EXPORT_SYMBOL_GPL(fwnode_handle_put);
  624. /**
  625. * device_get_child_node_count - return the number of child nodes for device
  626. * @dev: Device to cound the child nodes for
  627. */
  628. unsigned int device_get_child_node_count(struct device *dev)
  629. {
  630. struct fwnode_handle *child;
  631. unsigned int count = 0;
  632. device_for_each_child_node(dev, child)
  633. count++;
  634. return count;
  635. }
  636. EXPORT_SYMBOL_GPL(device_get_child_node_count);
  637. bool device_dma_supported(struct device *dev)
  638. {
  639. /* For DT, this is always supported.
  640. * For ACPI, this depends on CCA, which
  641. * is determined by the acpi_dma_supported().
  642. */
  643. if (IS_ENABLED(CONFIG_OF) && dev->of_node)
  644. return true;
  645. return acpi_dma_supported(ACPI_COMPANION(dev));
  646. }
  647. EXPORT_SYMBOL_GPL(device_dma_supported);
  648. enum dev_dma_attr device_get_dma_attr(struct device *dev)
  649. {
  650. enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
  651. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  652. if (of_dma_is_coherent(dev->of_node))
  653. attr = DEV_DMA_COHERENT;
  654. else
  655. attr = DEV_DMA_NON_COHERENT;
  656. } else
  657. attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
  658. return attr;
  659. }
  660. EXPORT_SYMBOL_GPL(device_get_dma_attr);
  661. /**
  662. * device_get_phy_mode - Get phy mode for given device
  663. * @dev: Pointer to the given device
  664. *
  665. * The function gets phy interface string from property 'phy-mode' or
  666. * 'phy-connection-type', and return its index in phy_modes table, or errno in
  667. * error case.
  668. */
  669. int device_get_phy_mode(struct device *dev)
  670. {
  671. const char *pm;
  672. int err, i;
  673. err = device_property_read_string(dev, "phy-mode", &pm);
  674. if (err < 0)
  675. err = device_property_read_string(dev,
  676. "phy-connection-type", &pm);
  677. if (err < 0)
  678. return err;
  679. for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
  680. if (!strcasecmp(pm, phy_modes(i)))
  681. return i;
  682. return -ENODEV;
  683. }
  684. EXPORT_SYMBOL_GPL(device_get_phy_mode);
  685. static void *device_get_mac_addr(struct device *dev,
  686. const char *name, char *addr,
  687. int alen)
  688. {
  689. int ret = device_property_read_u8_array(dev, name, addr, alen);
  690. if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
  691. return addr;
  692. return NULL;
  693. }
  694. /**
  695. * device_get_mac_address - Get the MAC for a given device
  696. * @dev: Pointer to the device
  697. * @addr: Address of buffer to store the MAC in
  698. * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
  699. *
  700. * Search the firmware node for the best MAC address to use. 'mac-address' is
  701. * checked first, because that is supposed to contain to "most recent" MAC
  702. * address. If that isn't set, then 'local-mac-address' is checked next,
  703. * because that is the default address. If that isn't set, then the obsolete
  704. * 'address' is checked, just in case we're using an old device tree.
  705. *
  706. * Note that the 'address' property is supposed to contain a virtual address of
  707. * the register set, but some DTS files have redefined that property to be the
  708. * MAC address.
  709. *
  710. * All-zero MAC addresses are rejected, because those could be properties that
  711. * exist in the firmware tables, but were not updated by the firmware. For
  712. * example, the DTS could define 'mac-address' and 'local-mac-address', with
  713. * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
  714. * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
  715. * exists but is all zeros.
  716. */
  717. void *device_get_mac_address(struct device *dev, char *addr, int alen)
  718. {
  719. char *res;
  720. res = device_get_mac_addr(dev, "mac-address", addr, alen);
  721. if (res)
  722. return res;
  723. res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
  724. if (res)
  725. return res;
  726. return device_get_mac_addr(dev, "address", addr, alen);
  727. }
  728. EXPORT_SYMBOL(device_get_mac_address);