property.c 32 KB

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