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
)
150 const struct property_entry
*prop
;
152 size_t array_len
, length
;
154 /* Find out the array length. */
155 prop
= pset_prop_get(pset
, propname
);
160 /* The array length for a non-array string property is 1. */
163 /* Find the length of an array. */
164 array_len
= pset_prop_count_elems_of_size(pset
, propname
,
165 sizeof(const char *));
167 /* Return how many there are if strings is NULL. */
171 array_len
= min(nval
, array_len
);
172 length
= array_len
* sizeof(*strings
);
174 pointer
= pset_prop_find(pset
, propname
, length
);
176 return PTR_ERR(pointer
);
178 memcpy(strings
, pointer
, length
);
183 struct fwnode_handle
*dev_fwnode(struct device
*dev
)
185 return IS_ENABLED(CONFIG_OF
) && dev
->of_node
?
186 &dev
->of_node
->fwnode
: dev
->fwnode
;
188 EXPORT_SYMBOL_GPL(dev_fwnode
);
190 static bool pset_fwnode_property_present(struct fwnode_handle
*fwnode
,
191 const char *propname
)
193 return !!pset_prop_get(to_pset_node(fwnode
), propname
);
196 static int pset_fwnode_read_int_array(struct fwnode_handle
*fwnode
,
197 const char *propname
,
198 unsigned int elem_size
, void *val
,
201 struct property_set
*node
= to_pset_node(fwnode
);
204 return pset_prop_count_elems_of_size(node
, propname
, elem_size
);
208 return pset_prop_read_u8_array(node
, propname
, val
, nval
);
210 return pset_prop_read_u16_array(node
, propname
, val
, nval
);
212 return pset_prop_read_u32_array(node
, propname
, val
, nval
);
214 return pset_prop_read_u64_array(node
, propname
, val
, nval
);
220 static int pset_fwnode_property_read_string_array(struct fwnode_handle
*fwnode
,
221 const char *propname
,
222 const char **val
, size_t nval
)
224 return pset_prop_read_string_array(to_pset_node(fwnode
), propname
,
228 static const struct fwnode_operations pset_fwnode_ops
= {
229 .property_present
= pset_fwnode_property_present
,
230 .property_read_int_array
= pset_fwnode_read_int_array
,
231 .property_read_string_array
= pset_fwnode_property_read_string_array
,
235 * device_property_present - check if a property of a device is present
236 * @dev: Device whose property is being checked
237 * @propname: Name of the property
239 * Check if property @propname is present in the device firmware description.
241 bool device_property_present(struct device
*dev
, const char *propname
)
243 return fwnode_property_present(dev_fwnode(dev
), propname
);
245 EXPORT_SYMBOL_GPL(device_property_present
);
248 * fwnode_property_present - check if a property of a firmware node is present
249 * @fwnode: Firmware node whose property to check
250 * @propname: Name of the property
252 bool fwnode_property_present(struct fwnode_handle
*fwnode
, const char *propname
)
256 ret
= fwnode_call_bool_op(fwnode
, property_present
, propname
);
257 if (ret
== false && !IS_ERR_OR_NULL(fwnode
) &&
258 !IS_ERR_OR_NULL(fwnode
->secondary
))
259 ret
= fwnode_call_bool_op(fwnode
->secondary
, property_present
,
263 EXPORT_SYMBOL_GPL(fwnode_property_present
);
266 * device_property_read_u8_array - return a u8 array property of a device
267 * @dev: Device to get the property of
268 * @propname: Name of the property
269 * @val: The values are stored here or %NULL to return the number of values
270 * @nval: Size of the @val array
272 * Function reads an array of u8 properties with @propname from the device
273 * firmware description and stores them to @val if found.
275 * Return: number of values if @val was %NULL,
276 * %0 if the property was found (success),
277 * %-EINVAL if given arguments are not valid,
278 * %-ENODATA if the property does not have a value,
279 * %-EPROTO if the property is not an array of numbers,
280 * %-EOVERFLOW if the size of the property is not as expected.
281 * %-ENXIO if no suitable firmware interface is present.
283 int device_property_read_u8_array(struct device
*dev
, const char *propname
,
284 u8
*val
, size_t nval
)
286 return fwnode_property_read_u8_array(dev_fwnode(dev
), propname
, val
, nval
);
288 EXPORT_SYMBOL_GPL(device_property_read_u8_array
);
291 * device_property_read_u16_array - return a u16 array property of a device
292 * @dev: Device to get the property of
293 * @propname: Name of the property
294 * @val: The values are stored here or %NULL to return the number of values
295 * @nval: Size of the @val array
297 * Function reads an array of u16 properties with @propname from the device
298 * firmware description and stores them to @val if found.
300 * Return: number of values if @val was %NULL,
301 * %0 if the property was found (success),
302 * %-EINVAL if given arguments are not valid,
303 * %-ENODATA if the property does not have a value,
304 * %-EPROTO if the property is not an array of numbers,
305 * %-EOVERFLOW if the size of the property is not as expected.
306 * %-ENXIO if no suitable firmware interface is present.
308 int device_property_read_u16_array(struct device
*dev
, const char *propname
,
309 u16
*val
, size_t nval
)
311 return fwnode_property_read_u16_array(dev_fwnode(dev
), propname
, val
, nval
);
313 EXPORT_SYMBOL_GPL(device_property_read_u16_array
);
316 * device_property_read_u32_array - return a u32 array property of a device
317 * @dev: Device to get the property of
318 * @propname: Name of the property
319 * @val: The values are stored here or %NULL to return the number of values
320 * @nval: Size of the @val array
322 * Function reads an array of u32 properties with @propname from the device
323 * firmware description and stores them to @val if found.
325 * Return: number of values if @val was %NULL,
326 * %0 if the property was found (success),
327 * %-EINVAL if given arguments are not valid,
328 * %-ENODATA if the property does not have a value,
329 * %-EPROTO if the property is not an array of numbers,
330 * %-EOVERFLOW if the size of the property is not as expected.
331 * %-ENXIO if no suitable firmware interface is present.
333 int device_property_read_u32_array(struct device
*dev
, const char *propname
,
334 u32
*val
, size_t nval
)
336 return fwnode_property_read_u32_array(dev_fwnode(dev
), propname
, val
, nval
);
338 EXPORT_SYMBOL_GPL(device_property_read_u32_array
);
341 * device_property_read_u64_array - return a u64 array property of a device
342 * @dev: Device to get the property of
343 * @propname: Name of the property
344 * @val: The values are stored here or %NULL to return the number of values
345 * @nval: Size of the @val array
347 * Function reads an array of u64 properties with @propname from the device
348 * firmware description and stores them to @val if found.
350 * Return: number of values if @val was %NULL,
351 * %0 if the property was found (success),
352 * %-EINVAL if given arguments are not valid,
353 * %-ENODATA if the property does not have a value,
354 * %-EPROTO if the property is not an array of numbers,
355 * %-EOVERFLOW if the size of the property is not as expected.
356 * %-ENXIO if no suitable firmware interface is present.
358 int device_property_read_u64_array(struct device
*dev
, const char *propname
,
359 u64
*val
, size_t nval
)
361 return fwnode_property_read_u64_array(dev_fwnode(dev
), propname
, val
, nval
);
363 EXPORT_SYMBOL_GPL(device_property_read_u64_array
);
366 * device_property_read_string_array - return a string array property of device
367 * @dev: Device to get the property of
368 * @propname: Name of the property
369 * @val: The values are stored here or %NULL to return the number of values
370 * @nval: Size of the @val array
372 * Function reads an array of string properties with @propname from the device
373 * firmware description and stores them to @val if found.
375 * Return: number of values read on success if @val is non-NULL,
376 * number of values available on success if @val is NULL,
377 * %-EINVAL if given arguments are not valid,
378 * %-ENODATA if the property does not have a value,
379 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
380 * %-EOVERFLOW if the size of the property is not as expected.
381 * %-ENXIO if no suitable firmware interface is present.
383 int device_property_read_string_array(struct device
*dev
, const char *propname
,
384 const char **val
, size_t nval
)
386 return fwnode_property_read_string_array(dev_fwnode(dev
), propname
, val
, nval
);
388 EXPORT_SYMBOL_GPL(device_property_read_string_array
);
391 * device_property_read_string - return a string property of a device
392 * @dev: Device to get the property of
393 * @propname: Name of the property
394 * @val: The value is stored here
396 * Function reads property @propname from the device firmware description and
397 * stores the value into @val if found. The value is checked to be a string.
399 * Return: %0 if the property was found (success),
400 * %-EINVAL if given arguments are not valid,
401 * %-ENODATA if the property does not have a value,
402 * %-EPROTO or %-EILSEQ if the property type is not a string.
403 * %-ENXIO if no suitable firmware interface is present.
405 int device_property_read_string(struct device
*dev
, const char *propname
,
408 return fwnode_property_read_string(dev_fwnode(dev
), propname
, val
);
410 EXPORT_SYMBOL_GPL(device_property_read_string
);
413 * device_property_match_string - find a string in an array and return index
414 * @dev: Device to get the property of
415 * @propname: Name of the property holding the array
416 * @string: String to look for
418 * Find a given string in a string array and if it is found return the
421 * Return: %0 if the property was found (success),
422 * %-EINVAL if given arguments are not valid,
423 * %-ENODATA if the property does not have a value,
424 * %-EPROTO if the property is not an array of strings,
425 * %-ENXIO if no suitable firmware interface is present.
427 int device_property_match_string(struct device
*dev
, const char *propname
,
430 return fwnode_property_match_string(dev_fwnode(dev
), propname
, string
);
432 EXPORT_SYMBOL_GPL(device_property_match_string
);
434 static int fwnode_property_read_int_array(struct fwnode_handle
*fwnode
,
435 const char *propname
,
436 unsigned int elem_size
, void *val
,
441 ret
= fwnode_call_int_op(fwnode
, property_read_int_array
, propname
,
442 elem_size
, val
, nval
);
443 if (ret
== -EINVAL
&& !IS_ERR_OR_NULL(fwnode
) &&
444 !IS_ERR_OR_NULL(fwnode
->secondary
))
445 ret
= fwnode_call_int_op(
446 fwnode
->secondary
, property_read_int_array
, propname
,
447 elem_size
, val
, nval
);
453 * fwnode_property_read_u8_array - return a u8 array property of firmware node
454 * @fwnode: Firmware node to get the property of
455 * @propname: Name of the property
456 * @val: The values are stored here or %NULL to return the number of values
457 * @nval: Size of the @val array
459 * Read an array of u8 properties with @propname from @fwnode and stores them to
462 * Return: number of values if @val was %NULL,
463 * %0 if the property was found (success),
464 * %-EINVAL if given arguments are not valid,
465 * %-ENODATA if the property does not have a value,
466 * %-EPROTO if the property is not an array of numbers,
467 * %-EOVERFLOW if the size of the property is not as expected,
468 * %-ENXIO if no suitable firmware interface is present.
470 int fwnode_property_read_u8_array(struct fwnode_handle
*fwnode
,
471 const char *propname
, u8
*val
, size_t nval
)
473 return fwnode_property_read_int_array(fwnode
, propname
, sizeof(u8
),
476 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array
);
479 * fwnode_property_read_u16_array - return a u16 array property of firmware node
480 * @fwnode: Firmware node to get the property of
481 * @propname: Name of the property
482 * @val: The values are stored here or %NULL to return the number of values
483 * @nval: Size of the @val array
485 * Read an array of u16 properties with @propname from @fwnode and store them to
488 * Return: number of values if @val was %NULL,
489 * %0 if the property was found (success),
490 * %-EINVAL if given arguments are not valid,
491 * %-ENODATA if the property does not have a value,
492 * %-EPROTO if the property is not an array of numbers,
493 * %-EOVERFLOW if the size of the property is not as expected,
494 * %-ENXIO if no suitable firmware interface is present.
496 int fwnode_property_read_u16_array(struct fwnode_handle
*fwnode
,
497 const char *propname
, u16
*val
, size_t nval
)
499 return fwnode_property_read_int_array(fwnode
, propname
, sizeof(u16
),
502 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array
);
505 * fwnode_property_read_u32_array - return a u32 array property of firmware node
506 * @fwnode: Firmware node to get the property of
507 * @propname: Name of the property
508 * @val: The values are stored here or %NULL to return the number of values
509 * @nval: Size of the @val array
511 * Read an array of u32 properties with @propname from @fwnode store them to
514 * Return: number of values if @val was %NULL,
515 * %0 if the property was found (success),
516 * %-EINVAL if given arguments are not valid,
517 * %-ENODATA if the property does not have a value,
518 * %-EPROTO if the property is not an array of numbers,
519 * %-EOVERFLOW if the size of the property is not as expected,
520 * %-ENXIO if no suitable firmware interface is present.
522 int fwnode_property_read_u32_array(struct fwnode_handle
*fwnode
,
523 const char *propname
, u32
*val
, size_t nval
)
525 return fwnode_property_read_int_array(fwnode
, propname
, sizeof(u32
),
528 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array
);
531 * fwnode_property_read_u64_array - return a u64 array property firmware node
532 * @fwnode: Firmware node to get the property of
533 * @propname: Name of the property
534 * @val: The values are stored here or %NULL to return the number of values
535 * @nval: Size of the @val array
537 * Read an array of u64 properties with @propname from @fwnode and store them to
540 * Return: number of values if @val was %NULL,
541 * %0 if the property was found (success),
542 * %-EINVAL if given arguments are not valid,
543 * %-ENODATA if the property does not have a value,
544 * %-EPROTO if the property is not an array of numbers,
545 * %-EOVERFLOW if the size of the property is not as expected,
546 * %-ENXIO if no suitable firmware interface is present.
548 int fwnode_property_read_u64_array(struct fwnode_handle
*fwnode
,
549 const char *propname
, u64
*val
, size_t nval
)
551 return fwnode_property_read_int_array(fwnode
, propname
, sizeof(u64
),
554 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array
);
557 * fwnode_property_read_string_array - return string array property of a node
558 * @fwnode: Firmware node to get the property of
559 * @propname: Name of the property
560 * @val: The values are stored here or %NULL to return the number of values
561 * @nval: Size of the @val array
563 * Read an string list property @propname from the given firmware node and store
564 * them to @val if found.
566 * Return: number of values read on success if @val is non-NULL,
567 * number of values available on success if @val is NULL,
568 * %-EINVAL if given arguments are not valid,
569 * %-ENODATA if the property does not have a value,
570 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
571 * %-EOVERFLOW if the size of the property is not as expected,
572 * %-ENXIO if no suitable firmware interface is present.
574 int fwnode_property_read_string_array(struct fwnode_handle
*fwnode
,
575 const char *propname
, const char **val
,
580 ret
= fwnode_call_int_op(fwnode
, property_read_string_array
, propname
,
582 if (ret
== -EINVAL
&& !IS_ERR_OR_NULL(fwnode
) &&
583 !IS_ERR_OR_NULL(fwnode
->secondary
))
584 ret
= fwnode_call_int_op(fwnode
->secondary
,
585 property_read_string_array
, propname
,
589 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array
);
592 * fwnode_property_read_string - return a string property of a firmware node
593 * @fwnode: Firmware node to get the property of
594 * @propname: Name of the property
595 * @val: The value is stored here
597 * Read property @propname from the given firmware node and store the value into
598 * @val if found. The value is checked to be a string.
600 * Return: %0 if the property was found (success),
601 * %-EINVAL if given arguments are not valid,
602 * %-ENODATA if the property does not have a value,
603 * %-EPROTO or %-EILSEQ if the property is not a string,
604 * %-ENXIO if no suitable firmware interface is present.
606 int fwnode_property_read_string(struct fwnode_handle
*fwnode
,
607 const char *propname
, const char **val
)
609 int ret
= fwnode_property_read_string_array(fwnode
, propname
, val
, 1);
611 return ret
< 0 ? ret
: 0;
613 EXPORT_SYMBOL_GPL(fwnode_property_read_string
);
616 * fwnode_property_match_string - find a string in an array and return index
617 * @fwnode: Firmware node to get the property of
618 * @propname: Name of the property holding the array
619 * @string: String to look for
621 * Find a given string in a string array and if it is found return the
624 * Return: %0 if the property was found (success),
625 * %-EINVAL if given arguments are not valid,
626 * %-ENODATA if the property does not have a value,
627 * %-EPROTO if the property is not an array of strings,
628 * %-ENXIO if no suitable firmware interface is present.
630 int fwnode_property_match_string(struct fwnode_handle
*fwnode
,
631 const char *propname
, const char *string
)
636 nval
= fwnode_property_read_string_array(fwnode
, propname
, NULL
, 0);
643 values
= kcalloc(nval
, sizeof(*values
), GFP_KERNEL
);
647 ret
= fwnode_property_read_string_array(fwnode
, propname
, values
, nval
);
651 ret
= match_string(values
, nval
, string
);
658 EXPORT_SYMBOL_GPL(fwnode_property_match_string
);
660 static int property_copy_string_array(struct property_entry
*dst
,
661 const struct property_entry
*src
)
664 size_t nval
= src
->length
/ sizeof(*d
);
667 d
= kcalloc(nval
, sizeof(*d
), GFP_KERNEL
);
671 for (i
= 0; i
< nval
; i
++) {
672 d
[i
] = kstrdup(src
->pointer
.str
[i
], GFP_KERNEL
);
673 if (!d
[i
] && src
->pointer
.str
[i
]) {
681 dst
->pointer
.raw_data
= d
;
685 static int property_entry_copy_data(struct property_entry
*dst
,
686 const struct property_entry
*src
)
690 dst
->name
= kstrdup(src
->name
, GFP_KERNEL
);
700 if (src
->is_string
) {
701 error
= property_copy_string_array(dst
, src
);
705 dst
->pointer
.raw_data
= kmemdup(src
->pointer
.raw_data
,
706 src
->length
, GFP_KERNEL
);
707 if (!dst
->pointer
.raw_data
) {
712 } else if (src
->is_string
) {
713 dst
->value
.str
= kstrdup(src
->value
.str
, GFP_KERNEL
);
714 if (!dst
->value
.str
&& src
->value
.str
) {
719 dst
->value
.raw_data
= src
->value
.raw_data
;
722 dst
->length
= src
->length
;
723 dst
->is_array
= src
->is_array
;
724 dst
->is_string
= src
->is_string
;
733 static void property_entry_free_data(const struct property_entry
*p
)
738 if (p
->is_string
&& p
->pointer
.str
) {
739 nval
= p
->length
/ sizeof(const char *);
740 for (i
= 0; i
< nval
; i
++)
741 kfree(p
->pointer
.str
[i
]);
743 kfree(p
->pointer
.raw_data
);
744 } else if (p
->is_string
) {
751 * property_entries_dup - duplicate array of properties
752 * @properties: array of properties to copy
754 * This function creates a deep copy of the given NULL-terminated array
755 * of property entries.
757 struct property_entry
*
758 property_entries_dup(const struct property_entry
*properties
)
760 struct property_entry
*p
;
763 while (properties
[n
].name
)
766 p
= kcalloc(n
+ 1, sizeof(*p
), GFP_KERNEL
);
768 return ERR_PTR(-ENOMEM
);
770 for (i
= 0; i
< n
; i
++) {
771 int ret
= property_entry_copy_data(&p
[i
], &properties
[i
]);
774 property_entry_free_data(&p
[i
]);
782 EXPORT_SYMBOL_GPL(property_entries_dup
);
785 * property_entries_free - free previously allocated array of properties
786 * @properties: array of properties to destroy
788 * This function frees given NULL-terminated array of property entries,
789 * along with their data.
791 void property_entries_free(const struct property_entry
*properties
)
793 const struct property_entry
*p
;
795 for (p
= properties
; p
->name
; p
++)
796 property_entry_free_data(p
);
800 EXPORT_SYMBOL_GPL(property_entries_free
);
803 * pset_free_set - releases memory allocated for copied property set
804 * @pset: Property set to release
806 * Function takes previously copied property set and releases all the
807 * memory allocated to it.
809 static void pset_free_set(struct property_set
*pset
)
814 property_entries_free(pset
->properties
);
819 * pset_copy_set - copies property set
820 * @pset: Property set to copy
822 * This function takes a deep copy of the given property set and returns
823 * pointer to the copy. Call device_free_property_set() to free resources
824 * allocated in this function.
826 * Return: Pointer to the new property set or error pointer.
828 static struct property_set
*pset_copy_set(const struct property_set
*pset
)
830 struct property_entry
*properties
;
831 struct property_set
*p
;
833 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
835 return ERR_PTR(-ENOMEM
);
837 properties
= property_entries_dup(pset
->properties
);
838 if (IS_ERR(properties
)) {
840 return ERR_CAST(properties
);
843 p
->properties
= properties
;
848 * device_remove_properties - Remove properties from a device object.
849 * @dev: Device whose properties to remove.
851 * The function removes properties previously associated to the device
852 * secondary firmware node with device_add_properties(). Memory allocated
853 * to the properties will also be released.
855 void device_remove_properties(struct device
*dev
)
857 struct fwnode_handle
*fwnode
;
859 fwnode
= dev_fwnode(dev
);
863 * Pick either primary or secondary node depending which one holds
864 * the pset. If there is no real firmware node (ACPI/DT) primary
865 * will hold the pset.
867 if (is_pset_node(fwnode
)) {
868 set_primary_fwnode(dev
, NULL
);
869 pset_free_set(to_pset_node(fwnode
));
871 fwnode
= fwnode
->secondary
;
872 if (!IS_ERR(fwnode
) && is_pset_node(fwnode
)) {
873 set_secondary_fwnode(dev
, NULL
);
874 pset_free_set(to_pset_node(fwnode
));
878 EXPORT_SYMBOL_GPL(device_remove_properties
);
881 * device_add_properties - Add a collection of properties to a device object.
882 * @dev: Device to add properties to.
883 * @properties: Collection of properties to add.
885 * Associate a collection of device properties represented by @properties with
886 * @dev as its secondary firmware node. The function takes a copy of
889 int device_add_properties(struct device
*dev
,
890 const struct property_entry
*properties
)
892 struct property_set
*p
, pset
;
897 pset
.properties
= properties
;
899 p
= pset_copy_set(&pset
);
903 p
->fwnode
.type
= FWNODE_PDATA
;
904 p
->fwnode
.ops
= &pset_fwnode_ops
;
905 set_secondary_fwnode(dev
, &p
->fwnode
);
908 EXPORT_SYMBOL_GPL(device_add_properties
);
911 * fwnode_get_next_parent - Iterate to the node's parent
912 * @fwnode: Firmware whose parent is retrieved
914 * This is like fwnode_get_parent() except that it drops the refcount
915 * on the passed node, making it suitable for iterating through a
918 * Returns a node pointer with refcount incremented, use
919 * fwnode_handle_node() on it when done.
921 struct fwnode_handle
*fwnode_get_next_parent(struct fwnode_handle
*fwnode
)
923 struct fwnode_handle
*parent
= fwnode_get_parent(fwnode
);
925 fwnode_handle_put(fwnode
);
929 EXPORT_SYMBOL_GPL(fwnode_get_next_parent
);
932 * fwnode_get_parent - Return parent firwmare node
933 * @fwnode: Firmware whose parent is retrieved
935 * Return parent firmware node of the given node if possible or %NULL if no
936 * parent was available.
938 struct fwnode_handle
*fwnode_get_parent(struct fwnode_handle
*fwnode
)
940 return fwnode_call_ptr_op(fwnode
, get_parent
);
942 EXPORT_SYMBOL_GPL(fwnode_get_parent
);
945 * fwnode_get_next_child_node - Return the next child node handle for a node
946 * @fwnode: Firmware node to find the next child node for.
947 * @child: Handle to one of the node's child nodes or a %NULL handle.
949 struct fwnode_handle
*fwnode_get_next_child_node(struct fwnode_handle
*fwnode
,
950 struct fwnode_handle
*child
)
952 return fwnode_call_ptr_op(fwnode
, get_next_child_node
, child
);
954 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node
);
957 * device_get_next_child_node - Return the next child node handle for a device
958 * @dev: Device to find the next child node for.
959 * @child: Handle to one of the device's child nodes or a null handle.
961 struct fwnode_handle
*device_get_next_child_node(struct device
*dev
,
962 struct fwnode_handle
*child
)
964 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
965 struct fwnode_handle
*fwnode
= NULL
;
968 fwnode
= &dev
->of_node
->fwnode
;
970 fwnode
= acpi_fwnode_handle(adev
);
972 return fwnode_get_next_child_node(fwnode
, child
);
974 EXPORT_SYMBOL_GPL(device_get_next_child_node
);
977 * fwnode_get_named_child_node - Return first matching named child node handle
978 * @fwnode: Firmware node to find the named child node for.
979 * @childname: String to match child node name against.
981 struct fwnode_handle
*fwnode_get_named_child_node(struct fwnode_handle
*fwnode
,
982 const char *childname
)
984 return fwnode_call_ptr_op(fwnode
, get_named_child_node
, childname
);
986 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node
);
989 * device_get_named_child_node - Return first matching named child node handle
990 * @dev: Device to find the named child node for.
991 * @childname: String to match child node name against.
993 struct fwnode_handle
*device_get_named_child_node(struct device
*dev
,
994 const char *childname
)
996 return fwnode_get_named_child_node(dev_fwnode(dev
), childname
);
998 EXPORT_SYMBOL_GPL(device_get_named_child_node
);
1001 * fwnode_handle_get - Obtain a reference to a device node
1002 * @fwnode: Pointer to the device node to obtain the reference to.
1004 void fwnode_handle_get(struct fwnode_handle
*fwnode
)
1006 fwnode_call_void_op(fwnode
, get
);
1008 EXPORT_SYMBOL_GPL(fwnode_handle_get
);
1011 * fwnode_handle_put - Drop reference to a device node
1012 * @fwnode: Pointer to the device node to drop the reference to.
1014 * This has to be used when terminating device_for_each_child_node() iteration
1015 * with break or return to prevent stale device node references from being left
1018 void fwnode_handle_put(struct fwnode_handle
*fwnode
)
1020 fwnode_call_void_op(fwnode
, put
);
1022 EXPORT_SYMBOL_GPL(fwnode_handle_put
);
1025 * fwnode_device_is_available - check if a device is available for use
1026 * @fwnode: Pointer to the fwnode of the device.
1028 bool fwnode_device_is_available(struct fwnode_handle
*fwnode
)
1030 return fwnode_call_bool_op(fwnode
, device_is_available
);
1032 EXPORT_SYMBOL_GPL(fwnode_device_is_available
);
1035 * device_get_child_node_count - return the number of child nodes for device
1036 * @dev: Device to cound the child nodes for
1038 unsigned int device_get_child_node_count(struct device
*dev
)
1040 struct fwnode_handle
*child
;
1041 unsigned int count
= 0;
1043 device_for_each_child_node(dev
, child
)
1048 EXPORT_SYMBOL_GPL(device_get_child_node_count
);
1050 bool device_dma_supported(struct device
*dev
)
1052 /* For DT, this is always supported.
1053 * For ACPI, this depends on CCA, which
1054 * is determined by the acpi_dma_supported().
1056 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
)
1059 return acpi_dma_supported(ACPI_COMPANION(dev
));
1061 EXPORT_SYMBOL_GPL(device_dma_supported
);
1063 enum dev_dma_attr
device_get_dma_attr(struct device
*dev
)
1065 enum dev_dma_attr attr
= DEV_DMA_NOT_SUPPORTED
;
1067 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
1068 if (of_dma_is_coherent(dev
->of_node
))
1069 attr
= DEV_DMA_COHERENT
;
1071 attr
= DEV_DMA_NON_COHERENT
;
1073 attr
= acpi_get_dma_attr(ACPI_COMPANION(dev
));
1077 EXPORT_SYMBOL_GPL(device_get_dma_attr
);
1080 * device_get_phy_mode - Get phy mode for given device
1081 * @dev: Pointer to the given device
1083 * The function gets phy interface string from property 'phy-mode' or
1084 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1087 int device_get_phy_mode(struct device
*dev
)
1092 err
= device_property_read_string(dev
, "phy-mode", &pm
);
1094 err
= device_property_read_string(dev
,
1095 "phy-connection-type", &pm
);
1099 for (i
= 0; i
< PHY_INTERFACE_MODE_MAX
; i
++)
1100 if (!strcasecmp(pm
, phy_modes(i
)))
1105 EXPORT_SYMBOL_GPL(device_get_phy_mode
);
1107 static void *device_get_mac_addr(struct device
*dev
,
1108 const char *name
, char *addr
,
1111 int ret
= device_property_read_u8_array(dev
, name
, addr
, alen
);
1113 if (ret
== 0 && alen
== ETH_ALEN
&& is_valid_ether_addr(addr
))
1119 * device_get_mac_address - Get the MAC for a given device
1120 * @dev: Pointer to the device
1121 * @addr: Address of buffer to store the MAC in
1122 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1124 * Search the firmware node for the best MAC address to use. 'mac-address' is
1125 * checked first, because that is supposed to contain to "most recent" MAC
1126 * address. If that isn't set, then 'local-mac-address' is checked next,
1127 * because that is the default address. If that isn't set, then the obsolete
1128 * 'address' is checked, just in case we're using an old device tree.
1130 * Note that the 'address' property is supposed to contain a virtual address of
1131 * the register set, but some DTS files have redefined that property to be the
1134 * All-zero MAC addresses are rejected, because those could be properties that
1135 * exist in the firmware tables, but were not updated by the firmware. For
1136 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1137 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1138 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1139 * exists but is all zeros.
1141 void *device_get_mac_address(struct device
*dev
, char *addr
, int alen
)
1145 res
= device_get_mac_addr(dev
, "mac-address", addr
, alen
);
1149 res
= device_get_mac_addr(dev
, "local-mac-address", addr
, alen
);
1153 return device_get_mac_addr(dev
, "address", addr
, alen
);
1155 EXPORT_SYMBOL(device_get_mac_address
);
1158 * device_graph_get_next_endpoint - Get next endpoint firmware node
1159 * @fwnode: Pointer to the parent firmware node
1160 * @prev: Previous endpoint node or %NULL to get the first
1162 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1165 struct fwnode_handle
*
1166 fwnode_graph_get_next_endpoint(struct fwnode_handle
*fwnode
,
1167 struct fwnode_handle
*prev
)
1169 return fwnode_call_ptr_op(fwnode
, graph_get_next_endpoint
, prev
);
1171 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint
);
1174 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1175 * @endpoint: Endpoint firmware node of the port
1177 * Return: the firmware node of the device the @endpoint belongs to.
1179 struct fwnode_handle
*
1180 fwnode_graph_get_port_parent(struct fwnode_handle
*endpoint
)
1182 struct fwnode_handle
*port
, *parent
;
1184 port
= fwnode_get_parent(endpoint
);
1185 parent
= fwnode_call_ptr_op(port
, graph_get_port_parent
);
1187 fwnode_handle_put(port
);
1191 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent
);
1194 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1195 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1197 * Extracts firmware node of a remote device the @fwnode points to.
1199 struct fwnode_handle
*
1200 fwnode_graph_get_remote_port_parent(struct fwnode_handle
*fwnode
)
1202 struct fwnode_handle
*endpoint
, *parent
;
1204 endpoint
= fwnode_graph_get_remote_endpoint(fwnode
);
1205 parent
= fwnode_graph_get_port_parent(endpoint
);
1207 fwnode_handle_put(endpoint
);
1211 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent
);
1214 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1215 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1217 * Extracts firmware node of a remote port the @fwnode points to.
1219 struct fwnode_handle
*fwnode_graph_get_remote_port(struct fwnode_handle
*fwnode
)
1221 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode
));
1223 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port
);
1226 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1227 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1229 * Extracts firmware node of a remote endpoint the @fwnode points to.
1231 struct fwnode_handle
*
1232 fwnode_graph_get_remote_endpoint(struct fwnode_handle
*fwnode
)
1234 return fwnode_call_ptr_op(fwnode
, graph_get_remote_endpoint
);
1236 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint
);
1239 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1240 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1241 * @port_id: identifier of the parent port node
1242 * @endpoint_id: identifier of the endpoint node
1244 * Return: Remote fwnode handle associated with remote endpoint node linked
1245 * to @node. Use fwnode_node_put() on it when done.
1247 struct fwnode_handle
*fwnode_graph_get_remote_node(struct fwnode_handle
*fwnode
,
1248 u32 port_id
, u32 endpoint_id
)
1250 struct fwnode_handle
*endpoint
= NULL
;
1252 while ((endpoint
= fwnode_graph_get_next_endpoint(fwnode
, endpoint
))) {
1253 struct fwnode_endpoint fwnode_ep
;
1254 struct fwnode_handle
*remote
;
1257 ret
= fwnode_graph_parse_endpoint(endpoint
, &fwnode_ep
);
1261 if (fwnode_ep
.port
!= port_id
|| fwnode_ep
.id
!= endpoint_id
)
1264 remote
= fwnode_graph_get_remote_port_parent(endpoint
);
1268 return fwnode_device_is_available(remote
) ? remote
: NULL
;
1273 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node
);
1276 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1277 * @fwnode: pointer to endpoint fwnode_handle
1278 * @endpoint: pointer to the fwnode endpoint data structure
1280 * Parse @fwnode representing a graph endpoint node and store the
1281 * information in @endpoint. The caller must hold a reference to
1284 int fwnode_graph_parse_endpoint(struct fwnode_handle
*fwnode
,
1285 struct fwnode_endpoint
*endpoint
)
1287 memset(endpoint
, 0, sizeof(*endpoint
));
1289 return fwnode_call_int_op(fwnode
, graph_parse_endpoint
, endpoint
);
1291 EXPORT_SYMBOL(fwnode_graph_parse_endpoint
);