2 * property.c - Unified device property interface.
4 * Copyright (C) 2014, Intel Corporation
5 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
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.
13 #include <linux/acpi.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
17 #include <linux/of_address.h>
18 #include <linux/property.h>
19 #include <linux/etherdevice.h>
20 #include <linux/phy.h>
22 static inline bool is_pset_node(struct fwnode_handle *fwnode)
24 return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA;
27 static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
29 return is_pset_node(fwnode) ?
30 container_of(fwnode, struct property_set, fwnode) : NULL;
33 static struct property_entry *pset_prop_get(struct property_set *pset,
36 struct property_entry *prop;
38 if (!pset || !pset->properties)
41 for (prop = pset->properties; prop->name; prop++)
42 if (!strcmp(name, prop->name))
48 static void *pset_prop_find(struct property_set *pset, const char *propname,
51 struct property_entry *prop;
54 prop = pset_prop_get(pset, propname);
56 return ERR_PTR(-EINVAL);
58 pointer = prop->pointer.raw_data;
60 pointer = &prop->value.raw_data;
62 return ERR_PTR(-ENODATA);
63 if (length > prop->length)
64 return ERR_PTR(-EOVERFLOW);
68 static int pset_prop_read_u8_array(struct property_set *pset,
70 u8 *values, size_t nval)
73 size_t length = nval * sizeof(*values);
75 pointer = pset_prop_find(pset, propname, length);
77 return PTR_ERR(pointer);
79 memcpy(values, pointer, length);
83 static int pset_prop_read_u16_array(struct property_set *pset,
85 u16 *values, size_t nval)
88 size_t length = nval * sizeof(*values);
90 pointer = pset_prop_find(pset, propname, length);
92 return PTR_ERR(pointer);
94 memcpy(values, pointer, length);
98 static int pset_prop_read_u32_array(struct property_set *pset,
100 u32 *values, size_t nval)
103 size_t length = nval * sizeof(*values);
105 pointer = pset_prop_find(pset, propname, length);
107 return PTR_ERR(pointer);
109 memcpy(values, pointer, length);
113 static int pset_prop_read_u64_array(struct property_set *pset,
114 const char *propname,
115 u64 *values, size_t nval)
118 size_t length = nval * sizeof(*values);
120 pointer = pset_prop_find(pset, propname, length);
122 return PTR_ERR(pointer);
124 memcpy(values, pointer, length);
128 static int pset_prop_count_elems_of_size(struct property_set *pset,
129 const char *propname, size_t length)
131 struct property_entry *prop;
133 prop = pset_prop_get(pset, propname);
137 return prop->length / length;
140 static int pset_prop_read_string_array(struct property_set *pset,
141 const char *propname,
142 const char **strings, size_t nval)
145 size_t length = nval * sizeof(*strings);
147 pointer = pset_prop_find(pset, propname, length);
149 return PTR_ERR(pointer);
151 memcpy(strings, pointer, length);
155 static int pset_prop_read_string(struct property_set *pset,
156 const char *propname, const char **strings)
158 struct property_entry *prop;
159 const char **pointer;
161 prop = pset_prop_get(pset, propname);
164 if (!prop->is_string)
166 if (prop->is_array) {
167 pointer = prop->pointer.str;
171 pointer = &prop->value.str;
172 if (*pointer && strnlen(*pointer, prop->length) >= prop->length)
180 static inline struct fwnode_handle *dev_fwnode(struct device *dev)
182 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
183 &dev->of_node->fwnode : dev->fwnode;
187 * device_property_present - check if a property of a device is present
188 * @dev: Device whose property is being checked
189 * @propname: Name of the property
191 * Check if property @propname is present in the device firmware description.
193 bool device_property_present(struct device *dev, const char *propname)
195 return fwnode_property_present(dev_fwnode(dev), propname);
197 EXPORT_SYMBOL_GPL(device_property_present);
199 static bool __fwnode_property_present(struct fwnode_handle *fwnode,
200 const char *propname)
202 if (is_of_node(fwnode))
203 return of_property_read_bool(to_of_node(fwnode), propname);
204 else if (is_acpi_node(fwnode))
205 return !acpi_node_prop_get(fwnode, propname, NULL);
206 else if (is_pset_node(fwnode))
207 return !!pset_prop_get(to_pset_node(fwnode), propname);
212 * fwnode_property_present - check if a property of a firmware node is present
213 * @fwnode: Firmware node whose property to check
214 * @propname: Name of the property
216 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
220 ret = __fwnode_property_present(fwnode, propname);
221 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
222 !IS_ERR_OR_NULL(fwnode->secondary))
223 ret = __fwnode_property_present(fwnode->secondary, propname);
226 EXPORT_SYMBOL_GPL(fwnode_property_present);
229 * device_property_read_u8_array - return a u8 array property of a device
230 * @dev: Device to get the property of
231 * @propname: Name of the property
232 * @val: The values are stored here or %NULL to return the number of values
233 * @nval: Size of the @val array
235 * Function reads an array of u8 properties with @propname from the device
236 * firmware description and stores them to @val if found.
238 * Return: number of values if @val was %NULL,
239 * %0 if the property was found (success),
240 * %-EINVAL if given arguments are not valid,
241 * %-ENODATA if the property does not have a value,
242 * %-EPROTO if the property is not an array of numbers,
243 * %-EOVERFLOW if the size of the property is not as expected.
244 * %-ENXIO if no suitable firmware interface is present.
246 int device_property_read_u8_array(struct device *dev, const char *propname,
247 u8 *val, size_t nval)
249 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
251 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
254 * device_property_read_u16_array - return a u16 array property of a device
255 * @dev: Device to get the property of
256 * @propname: Name of the property
257 * @val: The values are stored here or %NULL to return the number of values
258 * @nval: Size of the @val array
260 * Function reads an array of u16 properties with @propname from the device
261 * firmware description and stores them to @val if found.
263 * Return: number of values if @val was %NULL,
264 * %0 if the property was found (success),
265 * %-EINVAL if given arguments are not valid,
266 * %-ENODATA if the property does not have a value,
267 * %-EPROTO if the property is not an array of numbers,
268 * %-EOVERFLOW if the size of the property is not as expected.
269 * %-ENXIO if no suitable firmware interface is present.
271 int device_property_read_u16_array(struct device *dev, const char *propname,
272 u16 *val, size_t nval)
274 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
276 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
279 * device_property_read_u32_array - return a u32 array property of a device
280 * @dev: Device to get the property of
281 * @propname: Name of the property
282 * @val: The values are stored here or %NULL to return the number of values
283 * @nval: Size of the @val array
285 * Function reads an array of u32 properties with @propname from the device
286 * firmware description and stores them to @val if found.
288 * Return: number of values if @val was %NULL,
289 * %0 if the property was found (success),
290 * %-EINVAL if given arguments are not valid,
291 * %-ENODATA if the property does not have a value,
292 * %-EPROTO if the property is not an array of numbers,
293 * %-EOVERFLOW if the size of the property is not as expected.
294 * %-ENXIO if no suitable firmware interface is present.
296 int device_property_read_u32_array(struct device *dev, const char *propname,
297 u32 *val, size_t nval)
299 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
301 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
304 * device_property_read_u64_array - return a u64 array property of a device
305 * @dev: Device to get the property of
306 * @propname: Name of the property
307 * @val: The values are stored here or %NULL to return the number of values
308 * @nval: Size of the @val array
310 * Function reads an array of u64 properties with @propname from the device
311 * firmware description and stores them to @val if found.
313 * Return: number of values if @val was %NULL,
314 * %0 if the property was found (success),
315 * %-EINVAL if given arguments are not valid,
316 * %-ENODATA if the property does not have a value,
317 * %-EPROTO if the property is not an array of numbers,
318 * %-EOVERFLOW if the size of the property is not as expected.
319 * %-ENXIO if no suitable firmware interface is present.
321 int device_property_read_u64_array(struct device *dev, const char *propname,
322 u64 *val, size_t nval)
324 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
326 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
329 * device_property_read_string_array - return a string array property of device
330 * @dev: Device to get the property of
331 * @propname: Name of the property
332 * @val: The values are stored here or %NULL to return the number of values
333 * @nval: Size of the @val array
335 * Function reads an array of string properties with @propname from the device
336 * firmware description and stores them to @val if found.
338 * Return: number of values if @val was %NULL,
339 * %0 if the property was found (success),
340 * %-EINVAL if given arguments are not valid,
341 * %-ENODATA if the property does not have a value,
342 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
343 * %-EOVERFLOW if the size of the property is not as expected.
344 * %-ENXIO if no suitable firmware interface is present.
346 int device_property_read_string_array(struct device *dev, const char *propname,
347 const char **val, size_t nval)
349 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
351 EXPORT_SYMBOL_GPL(device_property_read_string_array);
354 * device_property_read_string - return a string property of a device
355 * @dev: Device to get the property of
356 * @propname: Name of the property
357 * @val: The value is stored here
359 * Function reads property @propname from the device firmware description and
360 * stores the value into @val if found. The value is checked to be a string.
362 * Return: %0 if the property was found (success),
363 * %-EINVAL if given arguments are not valid,
364 * %-ENODATA if the property does not have a value,
365 * %-EPROTO or %-EILSEQ if the property type is not a string.
366 * %-ENXIO if no suitable firmware interface is present.
368 int device_property_read_string(struct device *dev, const char *propname,
371 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
373 EXPORT_SYMBOL_GPL(device_property_read_string);
376 * device_property_match_string - find a string in an array and return index
377 * @dev: Device to get the property of
378 * @propname: Name of the property holding the array
379 * @string: String to look for
381 * Find a given string in a string array and if it is found return the
384 * Return: %0 if the property was found (success),
385 * %-EINVAL if given arguments are not valid,
386 * %-ENODATA if the property does not have a value,
387 * %-EPROTO if the property is not an array of strings,
388 * %-ENXIO if no suitable firmware interface is present.
390 int device_property_match_string(struct device *dev, const char *propname,
393 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
395 EXPORT_SYMBOL_GPL(device_property_match_string);
397 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
398 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
399 : of_property_count_elems_of_size((node), (propname), sizeof(type))
401 #define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
402 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
403 : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
405 #define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
408 if (is_of_node(_fwnode_)) \
409 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
410 _type_, _val_, _nval_); \
411 else if (is_acpi_node(_fwnode_)) \
412 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
414 else if (is_pset_node(_fwnode_)) \
415 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
416 _type_, _val_, _nval_); \
422 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
425 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \
427 if (_ret_ == -EINVAL && !IS_ERR_OR_NULL(_fwnode_) && \
428 !IS_ERR_OR_NULL(_fwnode_->secondary)) \
429 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \
430 _proptype_, _val_, _nval_); \
435 * fwnode_property_read_u8_array - return a u8 array property of firmware node
436 * @fwnode: Firmware node to get the property of
437 * @propname: Name of the property
438 * @val: The values are stored here or %NULL to return the number of values
439 * @nval: Size of the @val array
441 * Read an array of u8 properties with @propname from @fwnode and stores them to
444 * Return: number of values if @val was %NULL,
445 * %0 if the property was found (success),
446 * %-EINVAL if given arguments are not valid,
447 * %-ENODATA if the property does not have a value,
448 * %-EPROTO if the property is not an array of numbers,
449 * %-EOVERFLOW if the size of the property is not as expected,
450 * %-ENXIO if no suitable firmware interface is present.
452 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
453 const char *propname, u8 *val, size_t nval)
455 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
458 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
461 * fwnode_property_read_u16_array - return a u16 array property of 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
467 * Read an array of u16 properties with @propname from @fwnode and store them to
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.
478 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
479 const char *propname, u16 *val, size_t nval)
481 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
484 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
487 * fwnode_property_read_u32_array - return a u32 array property of firmware node
488 * @fwnode: Firmware node to get the property of
489 * @propname: Name of the property
490 * @val: The values are stored here or %NULL to return the number of values
491 * @nval: Size of the @val array
493 * Read an array of u32 properties with @propname from @fwnode store them to
496 * Return: number of values if @val was %NULL,
497 * %0 if the property was found (success),
498 * %-EINVAL if given arguments are not valid,
499 * %-ENODATA if the property does not have a value,
500 * %-EPROTO if the property is not an array of numbers,
501 * %-EOVERFLOW if the size of the property is not as expected,
502 * %-ENXIO if no suitable firmware interface is present.
504 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
505 const char *propname, u32 *val, size_t nval)
507 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
510 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
513 * fwnode_property_read_u64_array - return a u64 array property firmware node
514 * @fwnode: Firmware node to get the property of
515 * @propname: Name of the property
516 * @val: The values are stored here or %NULL to return the number of values
517 * @nval: Size of the @val array
519 * Read an array of u64 properties with @propname from @fwnode and store them to
522 * Return: number of values if @val was %NULL,
523 * %0 if the property was found (success),
524 * %-EINVAL if given arguments are not valid,
525 * %-ENODATA if the property does not have a value,
526 * %-EPROTO if the property is not an array of numbers,
527 * %-EOVERFLOW if the size of the property is not as expected,
528 * %-ENXIO if no suitable firmware interface is present.
530 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
531 const char *propname, u64 *val, size_t nval)
533 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
536 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
538 static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode,
539 const char *propname,
540 const char **val, size_t nval)
542 if (is_of_node(fwnode))
544 of_property_read_string_array(to_of_node(fwnode),
545 propname, val, nval) :
546 of_property_count_strings(to_of_node(fwnode), propname);
547 else if (is_acpi_node(fwnode))
548 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
550 else if (is_pset_node(fwnode))
552 pset_prop_read_string_array(to_pset_node(fwnode),
553 propname, val, nval) :
554 pset_prop_count_elems_of_size(to_pset_node(fwnode),
556 sizeof(const char *));
560 static int __fwnode_property_read_string(struct fwnode_handle *fwnode,
561 const char *propname, const char **val)
563 if (is_of_node(fwnode))
564 return of_property_read_string(to_of_node(fwnode), propname, val);
565 else if (is_acpi_node(fwnode))
566 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
568 else if (is_pset_node(fwnode))
569 return pset_prop_read_string(to_pset_node(fwnode), propname, val);
574 * fwnode_property_read_string_array - return string array property of a node
575 * @fwnode: Firmware node to get the property of
576 * @propname: Name of the property
577 * @val: The values are stored here or %NULL to return the number of values
578 * @nval: Size of the @val array
580 * Read an string list property @propname from the given firmware node and store
581 * them to @val if found.
583 * Return: number of values if @val was %NULL,
584 * %0 if the property was found (success),
585 * %-EINVAL if given arguments are not valid,
586 * %-ENODATA if the property does not have a value,
587 * %-EPROTO if the property is not an array of strings,
588 * %-EOVERFLOW if the size of the property is not as expected,
589 * %-ENXIO if no suitable firmware interface is present.
591 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
592 const char *propname, const char **val,
597 ret = __fwnode_property_read_string_array(fwnode, propname, val, nval);
598 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
599 !IS_ERR_OR_NULL(fwnode->secondary))
600 ret = __fwnode_property_read_string_array(fwnode->secondary,
601 propname, val, nval);
604 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
607 * fwnode_property_read_string - return a string property of a firmware node
608 * @fwnode: Firmware node to get the property of
609 * @propname: Name of the property
610 * @val: The value is stored here
612 * Read property @propname from the given firmware node and store the value into
613 * @val if found. The value is checked to be a string.
615 * Return: %0 if the property was found (success),
616 * %-EINVAL if given arguments are not valid,
617 * %-ENODATA if the property does not have a value,
618 * %-EPROTO or %-EILSEQ if the property is not a string,
619 * %-ENXIO if no suitable firmware interface is present.
621 int fwnode_property_read_string(struct fwnode_handle *fwnode,
622 const char *propname, const char **val)
626 ret = __fwnode_property_read_string(fwnode, propname, val);
627 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
628 !IS_ERR_OR_NULL(fwnode->secondary))
629 ret = __fwnode_property_read_string(fwnode->secondary,
633 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
636 * fwnode_property_match_string - find a string in an array and return index
637 * @fwnode: Firmware node to get the property of
638 * @propname: Name of the property holding the array
639 * @string: String to look for
641 * Find a given string in a string array and if it is found return the
644 * Return: %0 if the property was found (success),
645 * %-EINVAL if given arguments are not valid,
646 * %-ENODATA if the property does not have a value,
647 * %-EPROTO if the property is not an array of strings,
648 * %-ENXIO if no suitable firmware interface is present.
650 int fwnode_property_match_string(struct fwnode_handle *fwnode,
651 const char *propname, const char *string)
656 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
663 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
667 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
671 ret = match_string(values, nval, string);
678 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
681 * pset_free_set - releases memory allocated for copied property set
682 * @pset: Property set to release
684 * Function takes previously copied property set and releases all the
685 * memory allocated to it.
687 static void pset_free_set(struct property_set *pset)
689 const struct property_entry *prop;
695 for (prop = pset->properties; prop->name; prop++) {
696 if (prop->is_array) {
697 if (prop->is_string && prop->pointer.str) {
698 nval = prop->length / sizeof(const char *);
699 for (i = 0; i < nval; i++)
700 kfree(prop->pointer.str[i]);
702 kfree(prop->pointer.raw_data);
703 } else if (prop->is_string) {
704 kfree(prop->value.str);
709 kfree(pset->properties);
713 static int pset_copy_entry(struct property_entry *dst,
714 const struct property_entry *src)
719 dst->name = kstrdup(src->name, GFP_KERNEL);
727 if (src->is_string) {
728 nval = src->length / sizeof(const char *);
729 dst->pointer.str = kcalloc(nval, sizeof(const char *),
731 if (!dst->pointer.str)
734 d = dst->pointer.str;
735 s = src->pointer.str;
736 for (i = 0; i < nval; i++) {
737 d[i] = kstrdup(s[i], GFP_KERNEL);
742 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
743 src->length, GFP_KERNEL);
744 if (!dst->pointer.raw_data)
747 } else if (src->is_string) {
748 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
749 if (!dst->value.str && src->value.str)
752 dst->value.raw_data = src->value.raw_data;
755 dst->length = src->length;
756 dst->is_array = src->is_array;
757 dst->is_string = src->is_string;
763 * pset_copy_set - copies property set
764 * @pset: Property set to copy
766 * This function takes a deep copy of the given property set and returns
767 * pointer to the copy. Call device_free_property_set() to free resources
768 * allocated in this function.
770 * Return: Pointer to the new property set or error pointer.
772 static struct property_set *pset_copy_set(const struct property_set *pset)
774 const struct property_entry *entry;
775 struct property_set *p;
778 p = kzalloc(sizeof(*p), GFP_KERNEL);
780 return ERR_PTR(-ENOMEM);
782 while (pset->properties[n].name)
785 p->properties = kcalloc(n + 1, sizeof(*entry), GFP_KERNEL);
786 if (!p->properties) {
788 return ERR_PTR(-ENOMEM);
791 for (i = 0; i < n; i++) {
792 int ret = pset_copy_entry(&p->properties[i],
793 &pset->properties[i]);
804 * device_remove_property_set - Remove properties from a device object.
805 * @dev: Device whose properties to remove.
807 * The function removes properties previously associated to the device
808 * secondary firmware node with device_add_property_set(). Memory allocated
809 * to the properties will also be released.
811 void device_remove_property_set(struct device *dev)
813 struct fwnode_handle *fwnode;
815 fwnode = dev_fwnode(dev);
819 * Pick either primary or secondary node depending which one holds
820 * the pset. If there is no real firmware node (ACPI/DT) primary
821 * will hold the pset.
823 if (is_pset_node(fwnode)) {
824 set_primary_fwnode(dev, NULL);
825 pset_free_set(to_pset_node(fwnode));
827 fwnode = fwnode->secondary;
828 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
829 set_secondary_fwnode(dev, NULL);
830 pset_free_set(to_pset_node(fwnode));
834 EXPORT_SYMBOL_GPL(device_remove_property_set);
837 * device_add_property_set - Add a collection of properties to a device object.
838 * @dev: Device to add properties to.
839 * @pset: Collection of properties to add.
841 * Associate a collection of device properties represented by @pset with @dev
842 * as its secondary firmware node. The function takes a copy of @pset.
844 int device_add_property_set(struct device *dev, const struct property_set *pset)
846 struct property_set *p;
851 p = pset_copy_set(pset);
855 p->fwnode.type = FWNODE_PDATA;
856 set_secondary_fwnode(dev, &p->fwnode);
859 EXPORT_SYMBOL_GPL(device_add_property_set);
862 * device_get_next_child_node - Return the next child node handle for a device
863 * @dev: Device to find the next child node for.
864 * @child: Handle to one of the device's child nodes or a null handle.
866 struct fwnode_handle *device_get_next_child_node(struct device *dev,
867 struct fwnode_handle *child)
869 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
870 struct device_node *node;
872 node = of_get_next_available_child(dev->of_node, to_of_node(child));
874 return &node->fwnode;
875 } else if (IS_ENABLED(CONFIG_ACPI)) {
876 return acpi_get_next_subnode(dev, child);
880 EXPORT_SYMBOL_GPL(device_get_next_child_node);
883 * fwnode_handle_put - Drop reference to a device node
884 * @fwnode: Pointer to the device node to drop the reference to.
886 * This has to be used when terminating device_for_each_child_node() iteration
887 * with break or return to prevent stale device node references from being left
890 void fwnode_handle_put(struct fwnode_handle *fwnode)
892 if (is_of_node(fwnode))
893 of_node_put(to_of_node(fwnode));
895 EXPORT_SYMBOL_GPL(fwnode_handle_put);
898 * device_get_child_node_count - return the number of child nodes for device
899 * @dev: Device to cound the child nodes for
901 unsigned int device_get_child_node_count(struct device *dev)
903 struct fwnode_handle *child;
904 unsigned int count = 0;
906 device_for_each_child_node(dev, child)
911 EXPORT_SYMBOL_GPL(device_get_child_node_count);
913 bool device_dma_supported(struct device *dev)
915 /* For DT, this is always supported.
916 * For ACPI, this depends on CCA, which
917 * is determined by the acpi_dma_supported().
919 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
922 return acpi_dma_supported(ACPI_COMPANION(dev));
924 EXPORT_SYMBOL_GPL(device_dma_supported);
926 enum dev_dma_attr device_get_dma_attr(struct device *dev)
928 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
930 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
931 if (of_dma_is_coherent(dev->of_node))
932 attr = DEV_DMA_COHERENT;
934 attr = DEV_DMA_NON_COHERENT;
936 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
940 EXPORT_SYMBOL_GPL(device_get_dma_attr);
943 * device_get_phy_mode - Get phy mode for given device
944 * @dev: Pointer to the given device
946 * The function gets phy interface string from property 'phy-mode' or
947 * 'phy-connection-type', and return its index in phy_modes table, or errno in
950 int device_get_phy_mode(struct device *dev)
955 err = device_property_read_string(dev, "phy-mode", &pm);
957 err = device_property_read_string(dev,
958 "phy-connection-type", &pm);
962 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
963 if (!strcasecmp(pm, phy_modes(i)))
968 EXPORT_SYMBOL_GPL(device_get_phy_mode);
970 static void *device_get_mac_addr(struct device *dev,
971 const char *name, char *addr,
974 int ret = device_property_read_u8_array(dev, name, addr, alen);
976 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
982 * device_get_mac_address - Get the MAC for a given device
983 * @dev: Pointer to the device
984 * @addr: Address of buffer to store the MAC in
985 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
987 * Search the firmware node for the best MAC address to use. 'mac-address' is
988 * checked first, because that is supposed to contain to "most recent" MAC
989 * address. If that isn't set, then 'local-mac-address' is checked next,
990 * because that is the default address. If that isn't set, then the obsolete
991 * 'address' is checked, just in case we're using an old device tree.
993 * Note that the 'address' property is supposed to contain a virtual address of
994 * the register set, but some DTS files have redefined that property to be the
997 * All-zero MAC addresses are rejected, because those could be properties that
998 * exist in the firmware tables, but were not updated by the firmware. For
999 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1000 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1001 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1002 * exists but is all zeros.
1004 void *device_get_mac_address(struct device *dev, char *addr, int alen)
1008 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1012 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1016 return device_get_mac_addr(dev, "address", addr, alen);
1018 EXPORT_SYMBOL(device_get_mac_address);