property.c 31 KB

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