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