property.c 31 KB

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