property.c 32 KB

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