property.c 23 KB

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