property.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. const 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 const struct property_entry *pset_prop_get(struct property_set *pset,
  34. const char *name)
  35. {
  36. const 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 const void *pset_prop_find(struct property_set *pset,
  45. const char *propname, size_t length)
  46. {
  47. const struct property_entry *prop;
  48. const 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. const 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. const 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. const 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. const 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. const 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. const 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. const struct property_entry *prop;
  135. const char * const *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. static int property_copy_string_array(struct property_entry *dst,
  622. const struct property_entry *src)
  623. {
  624. char **d;
  625. size_t nval = src->length / sizeof(*d);
  626. int i;
  627. d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
  628. if (!d)
  629. return -ENOMEM;
  630. for (i = 0; i < nval; i++) {
  631. d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
  632. if (!d[i] && src->pointer.str[i]) {
  633. while (--i >= 0)
  634. kfree(d[i]);
  635. kfree(d);
  636. return -ENOMEM;
  637. }
  638. }
  639. dst->pointer.raw_data = d;
  640. return 0;
  641. }
  642. static int property_entry_copy_data(struct property_entry *dst,
  643. const struct property_entry *src)
  644. {
  645. int error;
  646. dst->name = kstrdup(src->name, GFP_KERNEL);
  647. if (!dst->name)
  648. return -ENOMEM;
  649. if (src->is_array) {
  650. if (!src->length) {
  651. error = -ENODATA;
  652. goto out_free_name;
  653. }
  654. if (src->is_string) {
  655. error = property_copy_string_array(dst, src);
  656. if (error)
  657. goto out_free_name;
  658. } else {
  659. dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
  660. src->length, GFP_KERNEL);
  661. if (!dst->pointer.raw_data) {
  662. error = -ENOMEM;
  663. goto out_free_name;
  664. }
  665. }
  666. } else if (src->is_string) {
  667. dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
  668. if (!dst->value.str && src->value.str) {
  669. error = -ENOMEM;
  670. goto out_free_name;
  671. }
  672. } else {
  673. dst->value.raw_data = src->value.raw_data;
  674. }
  675. dst->length = src->length;
  676. dst->is_array = src->is_array;
  677. dst->is_string = src->is_string;
  678. return 0;
  679. out_free_name:
  680. kfree(dst->name);
  681. return error;
  682. }
  683. static void property_entry_free_data(const struct property_entry *p)
  684. {
  685. size_t i, nval;
  686. if (p->is_array) {
  687. if (p->is_string && p->pointer.str) {
  688. nval = p->length / sizeof(const char *);
  689. for (i = 0; i < nval; i++)
  690. kfree(p->pointer.str[i]);
  691. }
  692. kfree(p->pointer.raw_data);
  693. } else if (p->is_string) {
  694. kfree(p->value.str);
  695. }
  696. kfree(p->name);
  697. }
  698. /**
  699. * property_entries_dup - duplicate array of properties
  700. * @properties: array of properties to copy
  701. *
  702. * This function creates a deep copy of the given NULL-terminated array
  703. * of property entries.
  704. */
  705. struct property_entry *
  706. property_entries_dup(const struct property_entry *properties)
  707. {
  708. struct property_entry *p;
  709. int i, n = 0;
  710. while (properties[n].name)
  711. n++;
  712. p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
  713. if (!p)
  714. return ERR_PTR(-ENOMEM);
  715. for (i = 0; i < n; i++) {
  716. int ret = property_entry_copy_data(&p[i], &properties[i]);
  717. if (ret) {
  718. while (--i >= 0)
  719. property_entry_free_data(&p[i]);
  720. kfree(p);
  721. return ERR_PTR(ret);
  722. }
  723. }
  724. return p;
  725. }
  726. EXPORT_SYMBOL_GPL(property_entries_dup);
  727. /**
  728. * property_entries_free - free previously allocated array of properties
  729. * @properties: array of properties to destroy
  730. *
  731. * This function frees given NULL-terminated array of property entries,
  732. * along with their data.
  733. */
  734. void property_entries_free(const struct property_entry *properties)
  735. {
  736. const struct property_entry *p;
  737. for (p = properties; p->name; p++)
  738. property_entry_free_data(p);
  739. kfree(properties);
  740. }
  741. EXPORT_SYMBOL_GPL(property_entries_free);
  742. /**
  743. * pset_free_set - releases memory allocated for copied property set
  744. * @pset: Property set to release
  745. *
  746. * Function takes previously copied property set and releases all the
  747. * memory allocated to it.
  748. */
  749. static void pset_free_set(struct property_set *pset)
  750. {
  751. if (!pset)
  752. return;
  753. property_entries_free(pset->properties);
  754. kfree(pset);
  755. }
  756. /**
  757. * pset_copy_set - copies property set
  758. * @pset: Property set to copy
  759. *
  760. * This function takes a deep copy of the given property set and returns
  761. * pointer to the copy. Call device_free_property_set() to free resources
  762. * allocated in this function.
  763. *
  764. * Return: Pointer to the new property set or error pointer.
  765. */
  766. static struct property_set *pset_copy_set(const struct property_set *pset)
  767. {
  768. struct property_entry *properties;
  769. struct property_set *p;
  770. p = kzalloc(sizeof(*p), GFP_KERNEL);
  771. if (!p)
  772. return ERR_PTR(-ENOMEM);
  773. properties = property_entries_dup(pset->properties);
  774. if (IS_ERR(properties)) {
  775. kfree(p);
  776. return ERR_CAST(properties);
  777. }
  778. p->properties = properties;
  779. return p;
  780. }
  781. /**
  782. * device_remove_properties - Remove properties from a device object.
  783. * @dev: Device whose properties to remove.
  784. *
  785. * The function removes properties previously associated to the device
  786. * secondary firmware node with device_add_properties(). Memory allocated
  787. * to the properties will also be released.
  788. */
  789. void device_remove_properties(struct device *dev)
  790. {
  791. struct fwnode_handle *fwnode;
  792. fwnode = dev_fwnode(dev);
  793. if (!fwnode)
  794. return;
  795. /*
  796. * Pick either primary or secondary node depending which one holds
  797. * the pset. If there is no real firmware node (ACPI/DT) primary
  798. * will hold the pset.
  799. */
  800. if (is_pset_node(fwnode)) {
  801. set_primary_fwnode(dev, NULL);
  802. pset_free_set(to_pset_node(fwnode));
  803. } else {
  804. fwnode = fwnode->secondary;
  805. if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
  806. set_secondary_fwnode(dev, NULL);
  807. pset_free_set(to_pset_node(fwnode));
  808. }
  809. }
  810. }
  811. EXPORT_SYMBOL_GPL(device_remove_properties);
  812. /**
  813. * device_add_properties - Add a collection of properties to a device object.
  814. * @dev: Device to add properties to.
  815. * @properties: Collection of properties to add.
  816. *
  817. * Associate a collection of device properties represented by @properties with
  818. * @dev as its secondary firmware node. The function takes a copy of
  819. * @properties.
  820. */
  821. int device_add_properties(struct device *dev,
  822. const struct property_entry *properties)
  823. {
  824. struct property_set *p, pset;
  825. if (!properties)
  826. return -EINVAL;
  827. pset.properties = properties;
  828. p = pset_copy_set(&pset);
  829. if (IS_ERR(p))
  830. return PTR_ERR(p);
  831. p->fwnode.type = FWNODE_PDATA;
  832. set_secondary_fwnode(dev, &p->fwnode);
  833. return 0;
  834. }
  835. EXPORT_SYMBOL_GPL(device_add_properties);
  836. /**
  837. * device_get_next_child_node - Return the next child node handle for a device
  838. * @dev: Device to find the next child node for.
  839. * @child: Handle to one of the device's child nodes or a null handle.
  840. */
  841. struct fwnode_handle *device_get_next_child_node(struct device *dev,
  842. struct fwnode_handle *child)
  843. {
  844. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  845. struct device_node *node;
  846. node = of_get_next_available_child(dev->of_node, to_of_node(child));
  847. if (node)
  848. return &node->fwnode;
  849. } else if (IS_ENABLED(CONFIG_ACPI)) {
  850. return acpi_get_next_subnode(dev, child);
  851. }
  852. return NULL;
  853. }
  854. EXPORT_SYMBOL_GPL(device_get_next_child_node);
  855. /**
  856. * device_get_named_child_node - Return first matching named child node handle
  857. * @dev: Device to find the named child node for.
  858. * @childname: String to match child node name against.
  859. */
  860. struct fwnode_handle *device_get_named_child_node(struct device *dev,
  861. const char *childname)
  862. {
  863. struct fwnode_handle *child;
  864. /*
  865. * Find first matching named child node of this device.
  866. * For ACPI this will be a data only sub-node.
  867. */
  868. device_for_each_child_node(dev, child) {
  869. if (is_of_node(child)) {
  870. if (!of_node_cmp(to_of_node(child)->name, childname))
  871. return child;
  872. } else if (is_acpi_data_node(child)) {
  873. if (acpi_data_node_match(child, childname))
  874. return child;
  875. }
  876. }
  877. return NULL;
  878. }
  879. EXPORT_SYMBOL_GPL(device_get_named_child_node);
  880. /**
  881. * fwnode_handle_put - Drop reference to a device node
  882. * @fwnode: Pointer to the device node to drop the reference to.
  883. *
  884. * This has to be used when terminating device_for_each_child_node() iteration
  885. * with break or return to prevent stale device node references from being left
  886. * behind.
  887. */
  888. void fwnode_handle_put(struct fwnode_handle *fwnode)
  889. {
  890. if (is_of_node(fwnode))
  891. of_node_put(to_of_node(fwnode));
  892. }
  893. EXPORT_SYMBOL_GPL(fwnode_handle_put);
  894. /**
  895. * device_get_child_node_count - return the number of child nodes for device
  896. * @dev: Device to cound the child nodes for
  897. */
  898. unsigned int device_get_child_node_count(struct device *dev)
  899. {
  900. struct fwnode_handle *child;
  901. unsigned int count = 0;
  902. device_for_each_child_node(dev, child)
  903. count++;
  904. return count;
  905. }
  906. EXPORT_SYMBOL_GPL(device_get_child_node_count);
  907. bool device_dma_supported(struct device *dev)
  908. {
  909. /* For DT, this is always supported.
  910. * For ACPI, this depends on CCA, which
  911. * is determined by the acpi_dma_supported().
  912. */
  913. if (IS_ENABLED(CONFIG_OF) && dev->of_node)
  914. return true;
  915. return acpi_dma_supported(ACPI_COMPANION(dev));
  916. }
  917. EXPORT_SYMBOL_GPL(device_dma_supported);
  918. enum dev_dma_attr device_get_dma_attr(struct device *dev)
  919. {
  920. enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
  921. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  922. if (of_dma_is_coherent(dev->of_node))
  923. attr = DEV_DMA_COHERENT;
  924. else
  925. attr = DEV_DMA_NON_COHERENT;
  926. } else
  927. attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
  928. return attr;
  929. }
  930. EXPORT_SYMBOL_GPL(device_get_dma_attr);
  931. /**
  932. * device_get_phy_mode - Get phy mode for given device
  933. * @dev: Pointer to the given device
  934. *
  935. * The function gets phy interface string from property 'phy-mode' or
  936. * 'phy-connection-type', and return its index in phy_modes table, or errno in
  937. * error case.
  938. */
  939. int device_get_phy_mode(struct device *dev)
  940. {
  941. const char *pm;
  942. int err, i;
  943. err = device_property_read_string(dev, "phy-mode", &pm);
  944. if (err < 0)
  945. err = device_property_read_string(dev,
  946. "phy-connection-type", &pm);
  947. if (err < 0)
  948. return err;
  949. for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
  950. if (!strcasecmp(pm, phy_modes(i)))
  951. return i;
  952. return -ENODEV;
  953. }
  954. EXPORT_SYMBOL_GPL(device_get_phy_mode);
  955. static void *device_get_mac_addr(struct device *dev,
  956. const char *name, char *addr,
  957. int alen)
  958. {
  959. int ret = device_property_read_u8_array(dev, name, addr, alen);
  960. if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
  961. return addr;
  962. return NULL;
  963. }
  964. /**
  965. * device_get_mac_address - Get the MAC for a given device
  966. * @dev: Pointer to the device
  967. * @addr: Address of buffer to store the MAC in
  968. * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
  969. *
  970. * Search the firmware node for the best MAC address to use. 'mac-address' is
  971. * checked first, because that is supposed to contain to "most recent" MAC
  972. * address. If that isn't set, then 'local-mac-address' is checked next,
  973. * because that is the default address. If that isn't set, then the obsolete
  974. * 'address' is checked, just in case we're using an old device tree.
  975. *
  976. * Note that the 'address' property is supposed to contain a virtual address of
  977. * the register set, but some DTS files have redefined that property to be the
  978. * MAC address.
  979. *
  980. * All-zero MAC addresses are rejected, because those could be properties that
  981. * exist in the firmware tables, but were not updated by the firmware. For
  982. * example, the DTS could define 'mac-address' and 'local-mac-address', with
  983. * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
  984. * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
  985. * exists but is all zeros.
  986. */
  987. void *device_get_mac_address(struct device *dev, char *addr, int alen)
  988. {
  989. char *res;
  990. res = device_get_mac_addr(dev, "mac-address", addr, alen);
  991. if (res)
  992. return res;
  993. res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
  994. if (res)
  995. return res;
  996. return device_get_mac_addr(dev, "address", addr, alen);
  997. }
  998. EXPORT_SYMBOL(device_get_mac_address);