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/of_graph.h>
19 #include <linux/property.h>
20 #include <linux/etherdevice.h>
21 #include <linux/phy.h>
24 struct fwnode_handle fwnode;
25 const struct property_entry *properties;
28 static inline bool is_pset_node(struct fwnode_handle *fwnode)
30 return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA;
33 static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
35 return is_pset_node(fwnode) ?
36 container_of(fwnode, struct property_set, fwnode) : NULL;
39 static const struct property_entry *pset_prop_get(struct property_set *pset,
42 const struct property_entry *prop;
44 if (!pset || !pset->properties)
47 for (prop = pset->properties; prop->name; prop++)
48 if (!strcmp(name, prop->name))
54 static const void *pset_prop_find(struct property_set *pset,
55 const char *propname, size_t length)
57 const struct property_entry *prop;
60 prop = pset_prop_get(pset, propname);
62 return ERR_PTR(-EINVAL);
64 pointer = prop->pointer.raw_data;
66 pointer = &prop->value.raw_data;
68 return ERR_PTR(-ENODATA);
69 if (length > prop->length)
70 return ERR_PTR(-EOVERFLOW);
74 static int pset_prop_read_u8_array(struct property_set *pset,
76 u8 *values, size_t nval)
79 size_t length = nval * sizeof(*values);
81 pointer = pset_prop_find(pset, propname, length);
83 return PTR_ERR(pointer);
85 memcpy(values, pointer, length);
89 static int pset_prop_read_u16_array(struct property_set *pset,
91 u16 *values, size_t nval)
94 size_t length = nval * sizeof(*values);
96 pointer = pset_prop_find(pset, propname, length);
98 return PTR_ERR(pointer);
100 memcpy(values, pointer, length);
104 static int pset_prop_read_u32_array(struct property_set *pset,
105 const char *propname,
106 u32 *values, size_t nval)
109 size_t length = nval * sizeof(*values);
111 pointer = pset_prop_find(pset, propname, length);
113 return PTR_ERR(pointer);
115 memcpy(values, pointer, length);
119 static int pset_prop_read_u64_array(struct property_set *pset,
120 const char *propname,
121 u64 *values, size_t nval)
124 size_t length = nval * sizeof(*values);
126 pointer = pset_prop_find(pset, propname, length);
128 return PTR_ERR(pointer);
130 memcpy(values, pointer, length);
134 static int pset_prop_count_elems_of_size(struct property_set *pset,
135 const char *propname, size_t length)
137 const struct property_entry *prop;
139 prop = pset_prop_get(pset, propname);
143 return prop->length / length;
146 static int pset_prop_read_string_array(struct property_set *pset,
147 const char *propname,
148 const char **strings, size_t nval)
151 size_t length = nval * sizeof(*strings);
153 pointer = pset_prop_find(pset, propname, length);
155 return PTR_ERR(pointer);
157 memcpy(strings, pointer, length);
161 static int pset_prop_read_string(struct property_set *pset,
162 const char *propname, const char **strings)
164 const struct property_entry *prop;
165 const char * const *pointer;
167 prop = pset_prop_get(pset, propname);
170 if (!prop->is_string)
172 if (prop->is_array) {
173 pointer = prop->pointer.str;
177 pointer = &prop->value.str;
178 if (*pointer && strnlen(*pointer, prop->length) >= prop->length)
186 static inline struct fwnode_handle *dev_fwnode(struct device *dev)
188 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
189 &dev->of_node->fwnode : dev->fwnode;
193 * device_property_present - check if a property of a device is present
194 * @dev: Device whose property is being checked
195 * @propname: Name of the property
197 * Check if property @propname is present in the device firmware description.
199 bool device_property_present(struct device *dev, const char *propname)
201 return fwnode_property_present(dev_fwnode(dev), propname);
203 EXPORT_SYMBOL_GPL(device_property_present);
205 static bool __fwnode_property_present(struct fwnode_handle *fwnode,
206 const char *propname)
208 if (is_of_node(fwnode))
209 return of_property_read_bool(to_of_node(fwnode), propname);
210 else if (is_acpi_node(fwnode))
211 return !acpi_node_prop_get(fwnode, propname, NULL);
212 else if (is_pset_node(fwnode))
213 return !!pset_prop_get(to_pset_node(fwnode), propname);
218 * fwnode_property_present - check if a property of a firmware node is present
219 * @fwnode: Firmware node whose property to check
220 * @propname: Name of the property
222 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
226 ret = __fwnode_property_present(fwnode, propname);
227 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
228 !IS_ERR_OR_NULL(fwnode->secondary))
229 ret = __fwnode_property_present(fwnode->secondary, propname);
232 EXPORT_SYMBOL_GPL(fwnode_property_present);
235 * device_property_read_u8_array - return a u8 array property of a device
236 * @dev: Device to get the property of
237 * @propname: Name of the property
238 * @val: The values are stored here or %NULL to return the number of values
239 * @nval: Size of the @val array
241 * Function reads an array of u8 properties with @propname from the device
242 * firmware description and stores them to @val if found.
244 * Return: number of values if @val was %NULL,
245 * %0 if the property was found (success),
246 * %-EINVAL if given arguments are not valid,
247 * %-ENODATA if the property does not have a value,
248 * %-EPROTO if the property is not an array of numbers,
249 * %-EOVERFLOW if the size of the property is not as expected.
250 * %-ENXIO if no suitable firmware interface is present.
252 int device_property_read_u8_array(struct device *dev, const char *propname,
253 u8 *val, size_t nval)
255 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
257 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
260 * device_property_read_u16_array - return a u16 array property of a device
261 * @dev: Device to get the property of
262 * @propname: Name of the property
263 * @val: The values are stored here or %NULL to return the number of values
264 * @nval: Size of the @val array
266 * Function reads an array of u16 properties with @propname from the device
267 * firmware description and stores them to @val if found.
269 * Return: number of values if @val was %NULL,
270 * %0 if the property was found (success),
271 * %-EINVAL if given arguments are not valid,
272 * %-ENODATA if the property does not have a value,
273 * %-EPROTO if the property is not an array of numbers,
274 * %-EOVERFLOW if the size of the property is not as expected.
275 * %-ENXIO if no suitable firmware interface is present.
277 int device_property_read_u16_array(struct device *dev, const char *propname,
278 u16 *val, size_t nval)
280 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
282 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
285 * device_property_read_u32_array - return a u32 array property of a device
286 * @dev: Device to get the property of
287 * @propname: Name of the property
288 * @val: The values are stored here or %NULL to return the number of values
289 * @nval: Size of the @val array
291 * Function reads an array of u32 properties with @propname from the device
292 * firmware description and stores them to @val if found.
294 * Return: number of values if @val was %NULL,
295 * %0 if the property was found (success),
296 * %-EINVAL if given arguments are not valid,
297 * %-ENODATA if the property does not have a value,
298 * %-EPROTO if the property is not an array of numbers,
299 * %-EOVERFLOW if the size of the property is not as expected.
300 * %-ENXIO if no suitable firmware interface is present.
302 int device_property_read_u32_array(struct device *dev, const char *propname,
303 u32 *val, size_t nval)
305 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
307 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
310 * device_property_read_u64_array - return a u64 array property of a device
311 * @dev: Device to get the property of
312 * @propname: Name of the property
313 * @val: The values are stored here or %NULL to return the number of values
314 * @nval: Size of the @val array
316 * Function reads an array of u64 properties with @propname from the device
317 * firmware description and stores them to @val if found.
319 * Return: number of values if @val was %NULL,
320 * %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 if the property is not an array of numbers,
324 * %-EOVERFLOW if the size of the property is not as expected.
325 * %-ENXIO if no suitable firmware interface is present.
327 int device_property_read_u64_array(struct device *dev, const char *propname,
328 u64 *val, size_t nval)
330 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
332 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
335 * device_property_read_string_array - return a string array property of device
336 * @dev: Device to get the property of
337 * @propname: Name of the property
338 * @val: The values are stored here or %NULL to return the number of values
339 * @nval: Size of the @val array
341 * Function reads an array of string properties with @propname from the device
342 * firmware description and stores them to @val if found.
344 * Return: number of values if @val was %NULL,
345 * %0 if the property was found (success),
346 * %-EINVAL if given arguments are not valid,
347 * %-ENODATA if the property does not have a value,
348 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
349 * %-EOVERFLOW if the size of the property is not as expected.
350 * %-ENXIO if no suitable firmware interface is present.
352 int device_property_read_string_array(struct device *dev, const char *propname,
353 const char **val, size_t nval)
355 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
357 EXPORT_SYMBOL_GPL(device_property_read_string_array);
360 * device_property_read_string - return a string property of a device
361 * @dev: Device to get the property of
362 * @propname: Name of the property
363 * @val: The value is stored here
365 * Function reads property @propname from the device firmware description and
366 * stores the value into @val if found. The value is checked to be a string.
368 * Return: %0 if the property was found (success),
369 * %-EINVAL if given arguments are not valid,
370 * %-ENODATA if the property does not have a value,
371 * %-EPROTO or %-EILSEQ if the property type is not a string.
372 * %-ENXIO if no suitable firmware interface is present.
374 int device_property_read_string(struct device *dev, const char *propname,
377 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
379 EXPORT_SYMBOL_GPL(device_property_read_string);
382 * device_property_match_string - find a string in an array and return index
383 * @dev: Device to get the property of
384 * @propname: Name of the property holding the array
385 * @string: String to look for
387 * Find a given string in a string array and if it is found return the
390 * Return: %0 if the property was found (success),
391 * %-EINVAL if given arguments are not valid,
392 * %-ENODATA if the property does not have a value,
393 * %-EPROTO if the property is not an array of strings,
394 * %-ENXIO if no suitable firmware interface is present.
396 int device_property_match_string(struct device *dev, const char *propname,
399 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
401 EXPORT_SYMBOL_GPL(device_property_match_string);
403 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
404 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
405 : of_property_count_elems_of_size((node), (propname), sizeof(type))
407 #define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
408 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
409 : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
411 #define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
414 if (is_of_node(_fwnode_)) \
415 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
416 _type_, _val_, _nval_); \
417 else if (is_acpi_node(_fwnode_)) \
418 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
420 else if (is_pset_node(_fwnode_)) \
421 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
422 _type_, _val_, _nval_); \
428 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
431 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \
433 if (_ret_ == -EINVAL && !IS_ERR_OR_NULL(_fwnode_) && \
434 !IS_ERR_OR_NULL(_fwnode_->secondary)) \
435 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \
436 _proptype_, _val_, _nval_); \
441 * fwnode_property_read_u8_array - return a u8 array property of firmware node
442 * @fwnode: Firmware node to get the property of
443 * @propname: Name of the property
444 * @val: The values are stored here or %NULL to return the number of values
445 * @nval: Size of the @val array
447 * Read an array of u8 properties with @propname from @fwnode and stores them to
450 * Return: number of values if @val was %NULL,
451 * %0 if the property was found (success),
452 * %-EINVAL if given arguments are not valid,
453 * %-ENODATA if the property does not have a value,
454 * %-EPROTO if the property is not an array of numbers,
455 * %-EOVERFLOW if the size of the property is not as expected,
456 * %-ENXIO if no suitable firmware interface is present.
458 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
459 const char *propname, u8 *val, size_t nval)
461 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
464 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
467 * fwnode_property_read_u16_array - return a u16 array property of firmware node
468 * @fwnode: Firmware node to get the property of
469 * @propname: Name of the property
470 * @val: The values are stored here or %NULL to return the number of values
471 * @nval: Size of the @val array
473 * Read an array of u16 properties with @propname from @fwnode and store them to
476 * Return: number of values if @val was %NULL,
477 * %0 if the property was found (success),
478 * %-EINVAL if given arguments are not valid,
479 * %-ENODATA if the property does not have a value,
480 * %-EPROTO if the property is not an array of numbers,
481 * %-EOVERFLOW if the size of the property is not as expected,
482 * %-ENXIO if no suitable firmware interface is present.
484 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
485 const char *propname, u16 *val, size_t nval)
487 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
490 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
493 * fwnode_property_read_u32_array - return a u32 array property of firmware node
494 * @fwnode: Firmware node to get the property of
495 * @propname: Name of the property
496 * @val: The values are stored here or %NULL to return the number of values
497 * @nval: Size of the @val array
499 * Read an array of u32 properties with @propname from @fwnode store them to
502 * Return: number of values if @val was %NULL,
503 * %0 if the property was found (success),
504 * %-EINVAL if given arguments are not valid,
505 * %-ENODATA if the property does not have a value,
506 * %-EPROTO if the property is not an array of numbers,
507 * %-EOVERFLOW if the size of the property is not as expected,
508 * %-ENXIO if no suitable firmware interface is present.
510 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
511 const char *propname, u32 *val, size_t nval)
513 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
516 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
519 * fwnode_property_read_u64_array - return a u64 array property firmware 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
525 * Read an array of u64 properties with @propname from @fwnode and store them to
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 numbers,
533 * %-EOVERFLOW if the size of the property is not as expected,
534 * %-ENXIO if no suitable firmware interface is present.
536 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
537 const char *propname, u64 *val, size_t nval)
539 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
542 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
544 static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode,
545 const char *propname,
546 const char **val, size_t nval)
548 if (is_of_node(fwnode))
550 of_property_read_string_array(to_of_node(fwnode),
551 propname, val, nval) :
552 of_property_count_strings(to_of_node(fwnode), propname);
553 else if (is_acpi_node(fwnode))
554 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
556 else if (is_pset_node(fwnode))
558 pset_prop_read_string_array(to_pset_node(fwnode),
559 propname, val, nval) :
560 pset_prop_count_elems_of_size(to_pset_node(fwnode),
562 sizeof(const char *));
566 static int __fwnode_property_read_string(struct fwnode_handle *fwnode,
567 const char *propname, const char **val)
569 if (is_of_node(fwnode))
570 return of_property_read_string(to_of_node(fwnode), propname, val);
571 else if (is_acpi_node(fwnode))
572 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
574 else if (is_pset_node(fwnode))
575 return pset_prop_read_string(to_pset_node(fwnode), propname, val);
580 * fwnode_property_read_string_array - return string array property of a node
581 * @fwnode: Firmware node to get the property of
582 * @propname: Name of the property
583 * @val: The values are stored here or %NULL to return the number of values
584 * @nval: Size of the @val array
586 * Read an string list property @propname from the given firmware node and store
587 * them to @val if found.
589 * Return: number of values if @val was %NULL,
590 * %0 if the property was found (success),
591 * %-EINVAL if given arguments are not valid,
592 * %-ENODATA if the property does not have a value,
593 * %-EPROTO if the property is not an array of strings,
594 * %-EOVERFLOW if the size of the property is not as expected,
595 * %-ENXIO if no suitable firmware interface is present.
597 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
598 const char *propname, const char **val,
603 ret = __fwnode_property_read_string_array(fwnode, propname, val, nval);
604 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
605 !IS_ERR_OR_NULL(fwnode->secondary))
606 ret = __fwnode_property_read_string_array(fwnode->secondary,
607 propname, val, nval);
610 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
613 * fwnode_property_read_string - return a string property of a firmware node
614 * @fwnode: Firmware node to get the property of
615 * @propname: Name of the property
616 * @val: The value is stored here
618 * Read property @propname from the given firmware node and store the value into
619 * @val if found. The value is checked to be a string.
621 * Return: %0 if the property was found (success),
622 * %-EINVAL if given arguments are not valid,
623 * %-ENODATA if the property does not have a value,
624 * %-EPROTO or %-EILSEQ if the property is not a string,
625 * %-ENXIO if no suitable firmware interface is present.
627 int fwnode_property_read_string(struct fwnode_handle *fwnode,
628 const char *propname, const char **val)
632 ret = __fwnode_property_read_string(fwnode, propname, val);
633 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
634 !IS_ERR_OR_NULL(fwnode->secondary))
635 ret = __fwnode_property_read_string(fwnode->secondary,
639 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
642 * fwnode_property_match_string - find a string in an array and return index
643 * @fwnode: Firmware node to get the property of
644 * @propname: Name of the property holding the array
645 * @string: String to look for
647 * Find a given string in a string array and if it is found return the
650 * Return: %0 if the property was found (success),
651 * %-EINVAL if given arguments are not valid,
652 * %-ENODATA if the property does not have a value,
653 * %-EPROTO if the property is not an array of strings,
654 * %-ENXIO if no suitable firmware interface is present.
656 int fwnode_property_match_string(struct fwnode_handle *fwnode,
657 const char *propname, const char *string)
662 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
669 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
673 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
677 ret = match_string(values, nval, string);
684 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
686 static int property_copy_string_array(struct property_entry *dst,
687 const struct property_entry *src)
690 size_t nval = src->length / sizeof(*d);
693 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
697 for (i = 0; i < nval; i++) {
698 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
699 if (!d[i] && src->pointer.str[i]) {
707 dst->pointer.raw_data = d;
711 static int property_entry_copy_data(struct property_entry *dst,
712 const struct property_entry *src)
716 dst->name = kstrdup(src->name, GFP_KERNEL);
726 if (src->is_string) {
727 error = property_copy_string_array(dst, src);
731 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
732 src->length, GFP_KERNEL);
733 if (!dst->pointer.raw_data) {
738 } else if (src->is_string) {
739 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
740 if (!dst->value.str && src->value.str) {
745 dst->value.raw_data = src->value.raw_data;
748 dst->length = src->length;
749 dst->is_array = src->is_array;
750 dst->is_string = src->is_string;
759 static void property_entry_free_data(const struct property_entry *p)
764 if (p->is_string && p->pointer.str) {
765 nval = p->length / sizeof(const char *);
766 for (i = 0; i < nval; i++)
767 kfree(p->pointer.str[i]);
769 kfree(p->pointer.raw_data);
770 } else if (p->is_string) {
777 * property_entries_dup - duplicate array of properties
778 * @properties: array of properties to copy
780 * This function creates a deep copy of the given NULL-terminated array
781 * of property entries.
783 struct property_entry *
784 property_entries_dup(const struct property_entry *properties)
786 struct property_entry *p;
789 while (properties[n].name)
792 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
794 return ERR_PTR(-ENOMEM);
796 for (i = 0; i < n; i++) {
797 int ret = property_entry_copy_data(&p[i], &properties[i]);
800 property_entry_free_data(&p[i]);
808 EXPORT_SYMBOL_GPL(property_entries_dup);
811 * property_entries_free - free previously allocated array of properties
812 * @properties: array of properties to destroy
814 * This function frees given NULL-terminated array of property entries,
815 * along with their data.
817 void property_entries_free(const struct property_entry *properties)
819 const struct property_entry *p;
821 for (p = properties; p->name; p++)
822 property_entry_free_data(p);
826 EXPORT_SYMBOL_GPL(property_entries_free);
829 * pset_free_set - releases memory allocated for copied property set
830 * @pset: Property set to release
832 * Function takes previously copied property set and releases all the
833 * memory allocated to it.
835 static void pset_free_set(struct property_set *pset)
840 property_entries_free(pset->properties);
845 * pset_copy_set - copies property set
846 * @pset: Property set to copy
848 * This function takes a deep copy of the given property set and returns
849 * pointer to the copy. Call device_free_property_set() to free resources
850 * allocated in this function.
852 * Return: Pointer to the new property set or error pointer.
854 static struct property_set *pset_copy_set(const struct property_set *pset)
856 struct property_entry *properties;
857 struct property_set *p;
859 p = kzalloc(sizeof(*p), GFP_KERNEL);
861 return ERR_PTR(-ENOMEM);
863 properties = property_entries_dup(pset->properties);
864 if (IS_ERR(properties)) {
866 return ERR_CAST(properties);
869 p->properties = properties;
874 * device_remove_properties - Remove properties from a device object.
875 * @dev: Device whose properties to remove.
877 * The function removes properties previously associated to the device
878 * secondary firmware node with device_add_properties(). Memory allocated
879 * to the properties will also be released.
881 void device_remove_properties(struct device *dev)
883 struct fwnode_handle *fwnode;
885 fwnode = dev_fwnode(dev);
889 * Pick either primary or secondary node depending which one holds
890 * the pset. If there is no real firmware node (ACPI/DT) primary
891 * will hold the pset.
893 if (is_pset_node(fwnode)) {
894 set_primary_fwnode(dev, NULL);
895 pset_free_set(to_pset_node(fwnode));
897 fwnode = fwnode->secondary;
898 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
899 set_secondary_fwnode(dev, NULL);
900 pset_free_set(to_pset_node(fwnode));
904 EXPORT_SYMBOL_GPL(device_remove_properties);
907 * device_add_properties - Add a collection of properties to a device object.
908 * @dev: Device to add properties to.
909 * @properties: Collection of properties to add.
911 * Associate a collection of device properties represented by @properties with
912 * @dev as its secondary firmware node. The function takes a copy of
915 int device_add_properties(struct device *dev,
916 const struct property_entry *properties)
918 struct property_set *p, pset;
923 pset.properties = properties;
925 p = pset_copy_set(&pset);
929 p->fwnode.type = FWNODE_PDATA;
930 set_secondary_fwnode(dev, &p->fwnode);
933 EXPORT_SYMBOL_GPL(device_add_properties);
936 * fwnode_get_parent - Return parent firwmare node
937 * @fwnode: Firmware whose parent is retrieved
939 * Return parent firmware node of the given node if possible or %NULL if no
940 * parent was available.
942 struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode)
944 struct fwnode_handle *parent = NULL;
946 if (is_of_node(fwnode)) {
947 struct device_node *node;
949 node = of_get_parent(to_of_node(fwnode));
951 parent = &node->fwnode;
952 } else if (is_acpi_node(fwnode)) {
953 parent = acpi_node_get_parent(fwnode);
958 EXPORT_SYMBOL_GPL(fwnode_get_parent);
961 * fwnode_get_next_child_node - Return the next child node handle for a node
962 * @fwnode: Firmware node to find the next child node for.
963 * @child: Handle to one of the node's child nodes or a %NULL handle.
965 struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode,
966 struct fwnode_handle *child)
968 if (is_of_node(fwnode)) {
969 struct device_node *node;
971 node = of_get_next_available_child(to_of_node(fwnode),
974 return &node->fwnode;
975 } else if (is_acpi_node(fwnode)) {
976 return acpi_get_next_subnode(fwnode, child);
981 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
984 * device_get_next_child_node - Return the next child node handle for a device
985 * @dev: Device to find the next child node for.
986 * @child: Handle to one of the device's child nodes or a null handle.
988 struct fwnode_handle *device_get_next_child_node(struct device *dev,
989 struct fwnode_handle *child)
991 struct acpi_device *adev = ACPI_COMPANION(dev);
992 struct fwnode_handle *fwnode = NULL;
995 fwnode = &dev->of_node->fwnode;
997 fwnode = acpi_fwnode_handle(adev);
999 return fwnode_get_next_child_node(fwnode, child);
1001 EXPORT_SYMBOL_GPL(device_get_next_child_node);
1004 * fwnode_get_named_child_node - Return first matching named child node handle
1005 * @fwnode: Firmware node to find the named child node for.
1006 * @childname: String to match child node name against.
1008 struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode,
1009 const char *childname)
1011 struct fwnode_handle *child;
1014 * Find first matching named child node of this fwnode.
1015 * For ACPI this will be a data only sub-node.
1017 fwnode_for_each_child_node(fwnode, child) {
1018 if (is_of_node(child)) {
1019 if (!of_node_cmp(to_of_node(child)->name, childname))
1021 } else if (is_acpi_data_node(child)) {
1022 if (acpi_data_node_match(child, childname))
1029 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1032 * device_get_named_child_node - Return first matching named child node handle
1033 * @dev: Device to find the named child node for.
1034 * @childname: String to match child node name against.
1036 struct fwnode_handle *device_get_named_child_node(struct device *dev,
1037 const char *childname)
1039 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1041 EXPORT_SYMBOL_GPL(device_get_named_child_node);
1044 * fwnode_handle_put - Drop reference to a device node
1045 * @fwnode: Pointer to the device node to drop the reference to.
1047 * This has to be used when terminating device_for_each_child_node() iteration
1048 * with break or return to prevent stale device node references from being left
1051 void fwnode_handle_put(struct fwnode_handle *fwnode)
1053 if (is_of_node(fwnode))
1054 of_node_put(to_of_node(fwnode));
1056 EXPORT_SYMBOL_GPL(fwnode_handle_put);
1059 * device_get_child_node_count - return the number of child nodes for device
1060 * @dev: Device to cound the child nodes for
1062 unsigned int device_get_child_node_count(struct device *dev)
1064 struct fwnode_handle *child;
1065 unsigned int count = 0;
1067 device_for_each_child_node(dev, child)
1072 EXPORT_SYMBOL_GPL(device_get_child_node_count);
1074 bool device_dma_supported(struct device *dev)
1076 /* For DT, this is always supported.
1077 * For ACPI, this depends on CCA, which
1078 * is determined by the acpi_dma_supported().
1080 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1083 return acpi_dma_supported(ACPI_COMPANION(dev));
1085 EXPORT_SYMBOL_GPL(device_dma_supported);
1087 enum dev_dma_attr device_get_dma_attr(struct device *dev)
1089 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1091 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1092 if (of_dma_is_coherent(dev->of_node))
1093 attr = DEV_DMA_COHERENT;
1095 attr = DEV_DMA_NON_COHERENT;
1097 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1101 EXPORT_SYMBOL_GPL(device_get_dma_attr);
1104 * device_get_phy_mode - Get phy mode for given device
1105 * @dev: Pointer to the given device
1107 * The function gets phy interface string from property 'phy-mode' or
1108 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1111 int device_get_phy_mode(struct device *dev)
1116 err = device_property_read_string(dev, "phy-mode", &pm);
1118 err = device_property_read_string(dev,
1119 "phy-connection-type", &pm);
1123 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1124 if (!strcasecmp(pm, phy_modes(i)))
1129 EXPORT_SYMBOL_GPL(device_get_phy_mode);
1131 static void *device_get_mac_addr(struct device *dev,
1132 const char *name, char *addr,
1135 int ret = device_property_read_u8_array(dev, name, addr, alen);
1137 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
1143 * device_get_mac_address - Get the MAC for a given device
1144 * @dev: Pointer to the device
1145 * @addr: Address of buffer to store the MAC in
1146 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1148 * Search the firmware node for the best MAC address to use. 'mac-address' is
1149 * checked first, because that is supposed to contain to "most recent" MAC
1150 * address. If that isn't set, then 'local-mac-address' is checked next,
1151 * because that is the default address. If that isn't set, then the obsolete
1152 * 'address' is checked, just in case we're using an old device tree.
1154 * Note that the 'address' property is supposed to contain a virtual address of
1155 * the register set, but some DTS files have redefined that property to be the
1158 * All-zero MAC addresses are rejected, because those could be properties that
1159 * exist in the firmware tables, but were not updated by the firmware. For
1160 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1161 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1162 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1163 * exists but is all zeros.
1165 void *device_get_mac_address(struct device *dev, char *addr, int alen)
1169 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1173 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1177 return device_get_mac_addr(dev, "address", addr, alen);
1179 EXPORT_SYMBOL(device_get_mac_address);
1182 * device_graph_get_next_endpoint - Get next endpoint firmware node
1183 * @fwnode: Pointer to the parent firmware node
1184 * @prev: Previous endpoint node or %NULL to get the first
1186 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1189 struct fwnode_handle *
1190 fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode,
1191 struct fwnode_handle *prev)
1193 struct fwnode_handle *endpoint = NULL;
1195 if (is_of_node(fwnode)) {
1196 struct device_node *node;
1198 node = of_graph_get_next_endpoint(to_of_node(fwnode),
1202 endpoint = &node->fwnode;
1203 } else if (is_acpi_node(fwnode)) {
1204 endpoint = acpi_graph_get_next_endpoint(fwnode, prev);
1205 if (IS_ERR(endpoint))
1212 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1215 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1216 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1218 * Extracts firmware node of a remote device the @fwnode points to.
1220 struct fwnode_handle *
1221 fwnode_graph_get_remote_port_parent(struct fwnode_handle *fwnode)
1223 struct fwnode_handle *parent = NULL;
1225 if (is_of_node(fwnode)) {
1226 struct device_node *node;
1228 node = of_graph_get_remote_port_parent(to_of_node(fwnode));
1230 parent = &node->fwnode;
1231 } else if (is_acpi_node(fwnode)) {
1234 ret = acpi_graph_get_remote_endpoint(fwnode, &parent, NULL,
1242 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1245 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1246 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1248 * Extracts firmware node of a remote port the @fwnode points to.
1250 struct fwnode_handle *fwnode_graph_get_remote_port(struct fwnode_handle *fwnode)
1252 struct fwnode_handle *port = NULL;
1254 if (is_of_node(fwnode)) {
1255 struct device_node *node;
1257 node = of_graph_get_remote_port(to_of_node(fwnode));
1259 port = &node->fwnode;
1260 } else if (is_acpi_node(fwnode)) {
1263 ret = acpi_graph_get_remote_endpoint(fwnode, NULL, &port, NULL);
1270 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1273 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1274 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1276 * Extracts firmware node of a remote endpoint the @fwnode points to.
1278 struct fwnode_handle *
1279 fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode)
1281 struct fwnode_handle *endpoint = NULL;
1283 if (is_of_node(fwnode)) {
1284 struct device_node *node;
1286 node = of_parse_phandle(to_of_node(fwnode), "remote-endpoint",
1289 endpoint = &node->fwnode;
1290 } else if (is_acpi_node(fwnode)) {
1293 ret = acpi_graph_get_remote_endpoint(fwnode, NULL, NULL,
1301 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);