property.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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_dev_prop_get(to_acpi_node(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. #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
  265. (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
  266. : of_property_count_elems_of_size((node), (propname), sizeof(type))
  267. #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
  268. ({ \
  269. int _ret_; \
  270. if (is_of_node(_fwnode_)) \
  271. _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
  272. _type_, _val_, _nval_); \
  273. else if (is_acpi_node(_fwnode_)) \
  274. _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
  275. _proptype_, _val_, _nval_); \
  276. else if (is_pset(_fwnode_)) \
  277. _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
  278. _proptype_, _val_, _nval_); \
  279. else \
  280. _ret_ = -ENXIO; \
  281. _ret_; \
  282. })
  283. /**
  284. * fwnode_property_read_u8_array - return a u8 array property of firmware node
  285. * @fwnode: Firmware node to get the property of
  286. * @propname: Name of the property
  287. * @val: The values are stored here or %NULL to return the number of values
  288. * @nval: Size of the @val array
  289. *
  290. * Read an array of u8 properties with @propname from @fwnode and stores them to
  291. * @val if found.
  292. *
  293. * Return: number of values if @val was %NULL,
  294. * %0 if the property was found (success),
  295. * %-EINVAL if given arguments are not valid,
  296. * %-ENODATA if the property does not have a value,
  297. * %-EPROTO if the property is not an array of numbers,
  298. * %-EOVERFLOW if the size of the property is not as expected,
  299. * %-ENXIO if no suitable firmware interface is present.
  300. */
  301. int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
  302. const char *propname, u8 *val, size_t nval)
  303. {
  304. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
  305. val, nval);
  306. }
  307. EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
  308. /**
  309. * fwnode_property_read_u16_array - return a u16 array property of firmware node
  310. * @fwnode: Firmware node to get the property of
  311. * @propname: Name of the property
  312. * @val: The values are stored here or %NULL to return the number of values
  313. * @nval: Size of the @val array
  314. *
  315. * Read an array of u16 properties with @propname from @fwnode and store them to
  316. * @val if found.
  317. *
  318. * Return: number of values if @val was %NULL,
  319. * %0 if the property was found (success),
  320. * %-EINVAL if given arguments are not valid,
  321. * %-ENODATA if the property does not have a value,
  322. * %-EPROTO if the property is not an array of numbers,
  323. * %-EOVERFLOW if the size of the property is not as expected,
  324. * %-ENXIO if no suitable firmware interface is present.
  325. */
  326. int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
  327. const char *propname, u16 *val, size_t nval)
  328. {
  329. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
  330. val, nval);
  331. }
  332. EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
  333. /**
  334. * fwnode_property_read_u32_array - return a u32 array property of firmware node
  335. * @fwnode: Firmware node to get the property of
  336. * @propname: Name of the property
  337. * @val: The values are stored here or %NULL to return the number of values
  338. * @nval: Size of the @val array
  339. *
  340. * Read an array of u32 properties with @propname from @fwnode store them to
  341. * @val if found.
  342. *
  343. * Return: number of values if @val was %NULL,
  344. * %0 if the property was found (success),
  345. * %-EINVAL if given arguments are not valid,
  346. * %-ENODATA if the property does not have a value,
  347. * %-EPROTO if the property is not an array of numbers,
  348. * %-EOVERFLOW if the size of the property is not as expected,
  349. * %-ENXIO if no suitable firmware interface is present.
  350. */
  351. int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
  352. const char *propname, u32 *val, size_t nval)
  353. {
  354. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
  355. val, nval);
  356. }
  357. EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
  358. /**
  359. * fwnode_property_read_u64_array - return a u64 array property firmware node
  360. * @fwnode: Firmware node to get the property of
  361. * @propname: Name of the property
  362. * @val: The values are stored here or %NULL to return the number of values
  363. * @nval: Size of the @val array
  364. *
  365. * Read an array of u64 properties with @propname from @fwnode and store them to
  366. * @val if found.
  367. *
  368. * Return: number of values if @val was %NULL,
  369. * %0 if the property was found (success),
  370. * %-EINVAL if given arguments are not valid,
  371. * %-ENODATA if the property does not have a value,
  372. * %-EPROTO if the property is not an array of numbers,
  373. * %-EOVERFLOW if the size of the property is not as expected,
  374. * %-ENXIO if no suitable firmware interface is present.
  375. */
  376. int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
  377. const char *propname, u64 *val, size_t nval)
  378. {
  379. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
  380. val, nval);
  381. }
  382. EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
  383. /**
  384. * fwnode_property_read_string_array - return string array property of a node
  385. * @fwnode: Firmware node 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. * Read an string list property @propname from the given firmware node and store
  391. * them to @val if found.
  392. *
  393. * Return: number of values if @val was %NULL,
  394. * %0 if the property was found (success),
  395. * %-EINVAL if given arguments are not valid,
  396. * %-ENODATA if the property does not have a value,
  397. * %-EPROTO 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 fwnode_property_read_string_array(struct fwnode_handle *fwnode,
  402. const char *propname, const char **val,
  403. size_t nval)
  404. {
  405. if (is_of_node(fwnode))
  406. return val ?
  407. of_property_read_string_array(to_of_node(fwnode),
  408. propname, val, nval) :
  409. of_property_count_strings(to_of_node(fwnode), propname);
  410. else if (is_acpi_node(fwnode))
  411. return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
  412. DEV_PROP_STRING, val, nval);
  413. else if (is_pset(fwnode))
  414. return pset_prop_read_array(to_pset(fwnode), propname,
  415. DEV_PROP_STRING, val, nval);
  416. return -ENXIO;
  417. }
  418. EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
  419. /**
  420. * fwnode_property_read_string - return a string property of a firmware node
  421. * @fwnode: Firmware node to get the property of
  422. * @propname: Name of the property
  423. * @val: The value is stored here
  424. *
  425. * Read property @propname from the given firmware node and store the value into
  426. * @val if found. The value is checked to be a string.
  427. *
  428. * Return: %0 if the property was found (success),
  429. * %-EINVAL if given arguments are not valid,
  430. * %-ENODATA if the property does not have a value,
  431. * %-EPROTO or %-EILSEQ if the property is not a string,
  432. * %-ENXIO if no suitable firmware interface is present.
  433. */
  434. int fwnode_property_read_string(struct fwnode_handle *fwnode,
  435. const char *propname, const char **val)
  436. {
  437. if (is_of_node(fwnode))
  438. return of_property_read_string(to_of_node(fwnode), propname, val);
  439. else if (is_acpi_node(fwnode))
  440. return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
  441. DEV_PROP_STRING, val, 1);
  442. return pset_prop_read_array(to_pset(fwnode), propname,
  443. DEV_PROP_STRING, val, 1);
  444. }
  445. EXPORT_SYMBOL_GPL(fwnode_property_read_string);
  446. /**
  447. * device_get_next_child_node - Return the next child node handle for a device
  448. * @dev: Device to find the next child node for.
  449. * @child: Handle to one of the device's child nodes or a null handle.
  450. */
  451. struct fwnode_handle *device_get_next_child_node(struct device *dev,
  452. struct fwnode_handle *child)
  453. {
  454. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  455. struct device_node *node;
  456. node = of_get_next_available_child(dev->of_node, to_of_node(child));
  457. if (node)
  458. return &node->fwnode;
  459. } else if (IS_ENABLED(CONFIG_ACPI)) {
  460. struct acpi_device *node;
  461. node = acpi_get_next_child(dev, to_acpi_node(child));
  462. if (node)
  463. return acpi_fwnode_handle(node);
  464. }
  465. return NULL;
  466. }
  467. EXPORT_SYMBOL_GPL(device_get_next_child_node);
  468. /**
  469. * fwnode_handle_put - Drop reference to a device node
  470. * @fwnode: Pointer to the device node to drop the reference to.
  471. *
  472. * This has to be used when terminating device_for_each_child_node() iteration
  473. * with break or return to prevent stale device node references from being left
  474. * behind.
  475. */
  476. void fwnode_handle_put(struct fwnode_handle *fwnode)
  477. {
  478. if (is_of_node(fwnode))
  479. of_node_put(to_of_node(fwnode));
  480. }
  481. EXPORT_SYMBOL_GPL(fwnode_handle_put);
  482. /**
  483. * device_get_child_node_count - return the number of child nodes for device
  484. * @dev: Device to cound the child nodes for
  485. */
  486. unsigned int device_get_child_node_count(struct device *dev)
  487. {
  488. struct fwnode_handle *child;
  489. unsigned int count = 0;
  490. device_for_each_child_node(dev, child)
  491. count++;
  492. return count;
  493. }
  494. EXPORT_SYMBOL_GPL(device_get_child_node_count);
  495. bool device_dma_is_coherent(struct device *dev)
  496. {
  497. bool coherent = false;
  498. if (IS_ENABLED(CONFIG_OF) && dev->of_node)
  499. coherent = of_dma_is_coherent(dev->of_node);
  500. else
  501. acpi_check_dma(ACPI_COMPANION(dev), &coherent);
  502. return coherent;
  503. }
  504. EXPORT_SYMBOL_GPL(device_dma_is_coherent);
  505. /**
  506. * device_get_phy_mode - Get phy mode for given device
  507. * @dev: Pointer to the given device
  508. *
  509. * The function gets phy interface string from property 'phy-mode' or
  510. * 'phy-connection-type', and return its index in phy_modes table, or errno in
  511. * error case.
  512. */
  513. int device_get_phy_mode(struct device *dev)
  514. {
  515. const char *pm;
  516. int err, i;
  517. err = device_property_read_string(dev, "phy-mode", &pm);
  518. if (err < 0)
  519. err = device_property_read_string(dev,
  520. "phy-connection-type", &pm);
  521. if (err < 0)
  522. return err;
  523. for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
  524. if (!strcasecmp(pm, phy_modes(i)))
  525. return i;
  526. return -ENODEV;
  527. }
  528. EXPORT_SYMBOL_GPL(device_get_phy_mode);
  529. static void *device_get_mac_addr(struct device *dev,
  530. const char *name, char *addr,
  531. int alen)
  532. {
  533. int ret = device_property_read_u8_array(dev, name, addr, alen);
  534. if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
  535. return addr;
  536. return NULL;
  537. }
  538. /**
  539. * device_get_mac_address - Get the MAC for a given device
  540. * @dev: Pointer to the device
  541. * @addr: Address of buffer to store the MAC in
  542. * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
  543. *
  544. * Search the firmware node for the best MAC address to use. 'mac-address' is
  545. * checked first, because that is supposed to contain to "most recent" MAC
  546. * address. If that isn't set, then 'local-mac-address' is checked next,
  547. * because that is the default address. If that isn't set, then the obsolete
  548. * 'address' is checked, just in case we're using an old device tree.
  549. *
  550. * Note that the 'address' property is supposed to contain a virtual address of
  551. * the register set, but some DTS files have redefined that property to be the
  552. * MAC address.
  553. *
  554. * All-zero MAC addresses are rejected, because those could be properties that
  555. * exist in the firmware tables, but were not updated by the firmware. For
  556. * example, the DTS could define 'mac-address' and 'local-mac-address', with
  557. * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
  558. * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
  559. * exists but is all zeros.
  560. */
  561. void *device_get_mac_address(struct device *dev, char *addr, int alen)
  562. {
  563. char *res;
  564. res = device_get_mac_addr(dev, "mac-address", addr, alen);
  565. if (res)
  566. return res;
  567. res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
  568. if (res)
  569. return res;
  570. return device_get_mac_addr(dev, "address", addr, alen);
  571. }
  572. EXPORT_SYMBOL(device_get_mac_address);