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>
25 struct fwnode_handle fwnode
;
26 const struct property_entry
*properties
;
29 static const struct fwnode_operations pset_fwnode_ops
;
31 static inline bool is_pset_node(const struct fwnode_handle
*fwnode
)
33 return !IS_ERR_OR_NULL(fwnode
) && fwnode
->ops
== &pset_fwnode_ops
;
36 #define to_pset_node(__fwnode) \
38 typeof(__fwnode) __to_pset_node_fwnode = __fwnode; \
40 is_pset_node(__to_pset_node_fwnode) ? \
41 container_of(__to_pset_node_fwnode, \
42 struct property_set, fwnode) : \
46 static const struct property_entry
*
47 pset_prop_get(const struct property_set
*pset
, const char *name
)
49 const struct property_entry
*prop
;
51 if (!pset
|| !pset
->properties
)
54 for (prop
= pset
->properties
; prop
->name
; prop
++)
55 if (!strcmp(name
, prop
->name
))
61 static const void *pset_prop_find(const struct property_set
*pset
,
62 const char *propname
, size_t length
)
64 const struct property_entry
*prop
;
67 prop
= pset_prop_get(pset
, propname
);
69 return ERR_PTR(-EINVAL
);
71 pointer
= prop
->pointer
.raw_data
;
73 pointer
= &prop
->value
.raw_data
;
75 return ERR_PTR(-ENODATA
);
76 if (length
> prop
->length
)
77 return ERR_PTR(-EOVERFLOW
);
81 static int pset_prop_read_u8_array(const struct property_set
*pset
,
83 u8
*values
, size_t nval
)
86 size_t length
= nval
* sizeof(*values
);
88 pointer
= pset_prop_find(pset
, propname
, length
);
90 return PTR_ERR(pointer
);
92 memcpy(values
, pointer
, length
);
96 static int pset_prop_read_u16_array(const struct property_set
*pset
,
98 u16
*values
, size_t nval
)
101 size_t length
= nval
* sizeof(*values
);
103 pointer
= pset_prop_find(pset
, propname
, length
);
105 return PTR_ERR(pointer
);
107 memcpy(values
, pointer
, length
);
111 static int pset_prop_read_u32_array(const struct property_set
*pset
,
112 const char *propname
,
113 u32
*values
, size_t nval
)
116 size_t length
= nval
* sizeof(*values
);
118 pointer
= pset_prop_find(pset
, propname
, length
);
120 return PTR_ERR(pointer
);
122 memcpy(values
, pointer
, length
);
126 static int pset_prop_read_u64_array(const struct property_set
*pset
,
127 const char *propname
,
128 u64
*values
, size_t nval
)
131 size_t length
= nval
* sizeof(*values
);
133 pointer
= pset_prop_find(pset
, propname
, length
);
135 return PTR_ERR(pointer
);
137 memcpy(values
, pointer
, length
);
141 static int pset_prop_count_elems_of_size(const struct property_set
*pset
,
142 const char *propname
, size_t length
)
144 const struct property_entry
*prop
;
146 prop
= pset_prop_get(pset
, propname
);
150 return prop
->length
/ length
;
153 static int pset_prop_read_string_array(const struct property_set
*pset
,
154 const char *propname
,
155 const char **strings
, size_t nval
)
157 const struct property_entry
*prop
;
159 size_t array_len
, length
;
161 /* Find out the array length. */
162 prop
= pset_prop_get(pset
, propname
);
167 /* The array length for a non-array string property is 1. */
170 /* Find the length of an array. */
171 array_len
= pset_prop_count_elems_of_size(pset
, propname
,
172 sizeof(const char *));
174 /* Return how many there are if strings is NULL. */
178 array_len
= min(nval
, array_len
);
179 length
= array_len
* sizeof(*strings
);
181 pointer
= pset_prop_find(pset
, propname
, length
);
183 return PTR_ERR(pointer
);
185 memcpy(strings
, pointer
, length
);
190 struct fwnode_handle
*dev_fwnode(struct device
*dev
)
192 return IS_ENABLED(CONFIG_OF
) && dev
->of_node
?
193 &dev
->of_node
->fwnode
: dev
->fwnode
;
195 EXPORT_SYMBOL_GPL(dev_fwnode
);
197 static bool pset_fwnode_property_present(const struct fwnode_handle
*fwnode
,
198 const char *propname
)
200 return !!pset_prop_get(to_pset_node(fwnode
), propname
);
203 static int pset_fwnode_read_int_array(const struct fwnode_handle
*fwnode
,
204 const char *propname
,
205 unsigned int elem_size
, void *val
,
208 const struct property_set
*node
= to_pset_node(fwnode
);
211 return pset_prop_count_elems_of_size(node
, propname
, elem_size
);
215 return pset_prop_read_u8_array(node
, propname
, val
, nval
);
217 return pset_prop_read_u16_array(node
, propname
, val
, nval
);
219 return pset_prop_read_u32_array(node
, propname
, val
, nval
);
221 return pset_prop_read_u64_array(node
, propname
, val
, nval
);
228 pset_fwnode_property_read_string_array(const struct fwnode_handle
*fwnode
,
229 const char *propname
,
230 const char **val
, size_t nval
)
232 return pset_prop_read_string_array(to_pset_node(fwnode
), propname
,
236 static const struct fwnode_operations pset_fwnode_ops
= {
237 .property_present
= pset_fwnode_property_present
,
238 .property_read_int_array
= pset_fwnode_read_int_array
,
239 .property_read_string_array
= pset_fwnode_property_read_string_array
,
243 * device_property_present - check if a property of a device is present
244 * @dev: Device whose property is being checked
245 * @propname: Name of the property
247 * Check if property @propname is present in the device firmware description.
249 bool device_property_present(struct device
*dev
, const char *propname
)
251 return fwnode_property_present(dev_fwnode(dev
), propname
);
253 EXPORT_SYMBOL_GPL(device_property_present
);
256 * fwnode_property_present - check if a property of a firmware node is present
257 * @fwnode: Firmware node whose property to check
258 * @propname: Name of the property
260 bool fwnode_property_present(const struct fwnode_handle
*fwnode
,
261 const char *propname
)
265 ret
= fwnode_call_bool_op(fwnode
, property_present
, propname
);
266 if (ret
== false && !IS_ERR_OR_NULL(fwnode
) &&
267 !IS_ERR_OR_NULL(fwnode
->secondary
))
268 ret
= fwnode_call_bool_op(fwnode
->secondary
, property_present
,
272 EXPORT_SYMBOL_GPL(fwnode_property_present
);
275 * device_property_read_u8_array - return a u8 array property of a device
276 * @dev: Device to get the property of
277 * @propname: Name of the property
278 * @val: The values are stored here or %NULL to return the number of values
279 * @nval: Size of the @val array
281 * Function reads an array of u8 properties with @propname from the device
282 * firmware description and stores them to @val if found.
284 * Return: number of values if @val was %NULL,
285 * %0 if the property was found (success),
286 * %-EINVAL if given arguments are not valid,
287 * %-ENODATA if the property does not have a value,
288 * %-EPROTO if the property is not an array of numbers,
289 * %-EOVERFLOW if the size of the property is not as expected.
290 * %-ENXIO if no suitable firmware interface is present.
292 int device_property_read_u8_array(struct device
*dev
, const char *propname
,
293 u8
*val
, size_t nval
)
295 return fwnode_property_read_u8_array(dev_fwnode(dev
), propname
, val
, nval
);
297 EXPORT_SYMBOL_GPL(device_property_read_u8_array
);
300 * device_property_read_u16_array - return a u16 array property of a device
301 * @dev: Device to get the property of
302 * @propname: Name of the property
303 * @val: The values are stored here or %NULL to return the number of values
304 * @nval: Size of the @val array
306 * Function reads an array of u16 properties with @propname from the device
307 * firmware description and stores them to @val if found.
309 * Return: number of values if @val was %NULL,
310 * %0 if the property was found (success),
311 * %-EINVAL if given arguments are not valid,
312 * %-ENODATA if the property does not have a value,
313 * %-EPROTO if the property is not an array of numbers,
314 * %-EOVERFLOW if the size of the property is not as expected.
315 * %-ENXIO if no suitable firmware interface is present.
317 int device_property_read_u16_array(struct device
*dev
, const char *propname
,
318 u16
*val
, size_t nval
)
320 return fwnode_property_read_u16_array(dev_fwnode(dev
), propname
, val
, nval
);
322 EXPORT_SYMBOL_GPL(device_property_read_u16_array
);
325 * device_property_read_u32_array - return a u32 array property of a device
326 * @dev: Device to get the property of
327 * @propname: Name of the property
328 * @val: The values are stored here or %NULL to return the number of values
329 * @nval: Size of the @val array
331 * Function reads an array of u32 properties with @propname from the device
332 * firmware description and stores them to @val if found.
334 * Return: number of values if @val was %NULL,
335 * %0 if the property was found (success),
336 * %-EINVAL if given arguments are not valid,
337 * %-ENODATA if the property does not have a value,
338 * %-EPROTO if the property is not an array of numbers,
339 * %-EOVERFLOW if the size of the property is not as expected.
340 * %-ENXIO if no suitable firmware interface is present.
342 int device_property_read_u32_array(struct device
*dev
, const char *propname
,
343 u32
*val
, size_t nval
)
345 return fwnode_property_read_u32_array(dev_fwnode(dev
), propname
, val
, nval
);
347 EXPORT_SYMBOL_GPL(device_property_read_u32_array
);
350 * device_property_read_u64_array - return a u64 array property of a device
351 * @dev: Device to get the property of
352 * @propname: Name of the property
353 * @val: The values are stored here or %NULL to return the number of values
354 * @nval: Size of the @val array
356 * Function reads an array of u64 properties with @propname from the device
357 * firmware description and stores them to @val if found.
359 * Return: number of values if @val was %NULL,
360 * %0 if the property was found (success),
361 * %-EINVAL if given arguments are not valid,
362 * %-ENODATA if the property does not have a value,
363 * %-EPROTO if the property is not an array of numbers,
364 * %-EOVERFLOW if the size of the property is not as expected.
365 * %-ENXIO if no suitable firmware interface is present.
367 int device_property_read_u64_array(struct device
*dev
, const char *propname
,
368 u64
*val
, size_t nval
)
370 return fwnode_property_read_u64_array(dev_fwnode(dev
), propname
, val
, nval
);
372 EXPORT_SYMBOL_GPL(device_property_read_u64_array
);
375 * device_property_read_string_array - return a string array property of device
376 * @dev: Device to get the property of
377 * @propname: Name of the property
378 * @val: The values are stored here or %NULL to return the number of values
379 * @nval: Size of the @val array
381 * Function reads an array of string properties with @propname from the device
382 * firmware description and stores them to @val if found.
384 * Return: number of values read on success if @val is non-NULL,
385 * number of values available on success if @val is NULL,
386 * %-EINVAL if given arguments are not valid,
387 * %-ENODATA if the property does not have a value,
388 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
389 * %-EOVERFLOW if the size of the property is not as expected.
390 * %-ENXIO if no suitable firmware interface is present.
392 int device_property_read_string_array(struct device
*dev
, const char *propname
,
393 const char **val
, size_t nval
)
395 return fwnode_property_read_string_array(dev_fwnode(dev
), propname
, val
, nval
);
397 EXPORT_SYMBOL_GPL(device_property_read_string_array
);
400 * device_property_read_string - return a string property of a device
401 * @dev: Device to get the property of
402 * @propname: Name of the property
403 * @val: The value is stored here
405 * Function reads property @propname from the device firmware description and
406 * stores the value into @val if found. The value is checked to be a string.
408 * Return: %0 if the property was found (success),
409 * %-EINVAL if given arguments are not valid,
410 * %-ENODATA if the property does not have a value,
411 * %-EPROTO or %-EILSEQ if the property type is not a string.
412 * %-ENXIO if no suitable firmware interface is present.
414 int device_property_read_string(struct device
*dev
, const char *propname
,
417 return fwnode_property_read_string(dev_fwnode(dev
), propname
, val
);
419 EXPORT_SYMBOL_GPL(device_property_read_string
);
422 * device_property_match_string - find a string in an array and return index
423 * @dev: Device to get the property of
424 * @propname: Name of the property holding the array
425 * @string: String to look for
427 * Find a given string in a string array and if it is found return the
430 * Return: %0 if the property was found (success),
431 * %-EINVAL if given arguments are not valid,
432 * %-ENODATA if the property does not have a value,
433 * %-EPROTO if the property is not an array of strings,
434 * %-ENXIO if no suitable firmware interface is present.
436 int device_property_match_string(struct device
*dev
, const char *propname
,
439 return fwnode_property_match_string(dev_fwnode(dev
), propname
, string
);
441 EXPORT_SYMBOL_GPL(device_property_match_string
);
443 static int fwnode_property_read_int_array(const struct fwnode_handle
*fwnode
,
444 const char *propname
,
445 unsigned int elem_size
, void *val
,
450 ret
= fwnode_call_int_op(fwnode
, property_read_int_array
, propname
,
451 elem_size
, val
, nval
);
452 if (ret
== -EINVAL
&& !IS_ERR_OR_NULL(fwnode
) &&
453 !IS_ERR_OR_NULL(fwnode
->secondary
))
454 ret
= fwnode_call_int_op(
455 fwnode
->secondary
, property_read_int_array
, propname
,
456 elem_size
, val
, nval
);
462 * fwnode_property_read_u8_array - return a u8 array property of firmware node
463 * @fwnode: Firmware node to get the property of
464 * @propname: Name of the property
465 * @val: The values are stored here or %NULL to return the number of values
466 * @nval: Size of the @val array
468 * Read an array of u8 properties with @propname from @fwnode and stores them to
471 * Return: number of values if @val was %NULL,
472 * %0 if the property was found (success),
473 * %-EINVAL if given arguments are not valid,
474 * %-ENODATA if the property does not have a value,
475 * %-EPROTO if the property is not an array of numbers,
476 * %-EOVERFLOW if the size of the property is not as expected,
477 * %-ENXIO if no suitable firmware interface is present.
479 int fwnode_property_read_u8_array(const struct fwnode_handle
*fwnode
,
480 const char *propname
, u8
*val
, size_t nval
)
482 return fwnode_property_read_int_array(fwnode
, propname
, sizeof(u8
),
485 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array
);
488 * fwnode_property_read_u16_array - return a u16 array property of firmware node
489 * @fwnode: Firmware node to get the property of
490 * @propname: Name of the property
491 * @val: The values are stored here or %NULL to return the number of values
492 * @nval: Size of the @val array
494 * Read an array of u16 properties with @propname from @fwnode and store them to
497 * Return: number of values if @val was %NULL,
498 * %0 if the property was found (success),
499 * %-EINVAL if given arguments are not valid,
500 * %-ENODATA if the property does not have a value,
501 * %-EPROTO if the property is not an array of numbers,
502 * %-EOVERFLOW if the size of the property is not as expected,
503 * %-ENXIO if no suitable firmware interface is present.
505 int fwnode_property_read_u16_array(const struct fwnode_handle
*fwnode
,
506 const char *propname
, u16
*val
, size_t nval
)
508 return fwnode_property_read_int_array(fwnode
, propname
, sizeof(u16
),
511 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array
);
514 * fwnode_property_read_u32_array - return a u32 array property of firmware node
515 * @fwnode: Firmware node to get the property of
516 * @propname: Name of the property
517 * @val: The values are stored here or %NULL to return the number of values
518 * @nval: Size of the @val array
520 * Read an array of u32 properties with @propname from @fwnode store them to
523 * Return: number of values if @val was %NULL,
524 * %0 if the property was found (success),
525 * %-EINVAL if given arguments are not valid,
526 * %-ENODATA if the property does not have a value,
527 * %-EPROTO if the property is not an array of numbers,
528 * %-EOVERFLOW if the size of the property is not as expected,
529 * %-ENXIO if no suitable firmware interface is present.
531 int fwnode_property_read_u32_array(const struct fwnode_handle
*fwnode
,
532 const char *propname
, u32
*val
, size_t nval
)
534 return fwnode_property_read_int_array(fwnode
, propname
, sizeof(u32
),
537 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array
);
540 * fwnode_property_read_u64_array - return a u64 array property firmware node
541 * @fwnode: Firmware node to get the property of
542 * @propname: Name of the property
543 * @val: The values are stored here or %NULL to return the number of values
544 * @nval: Size of the @val array
546 * Read an array of u64 properties with @propname from @fwnode and store them to
549 * Return: number of values if @val was %NULL,
550 * %0 if the property was found (success),
551 * %-EINVAL if given arguments are not valid,
552 * %-ENODATA if the property does not have a value,
553 * %-EPROTO if the property is not an array of numbers,
554 * %-EOVERFLOW if the size of the property is not as expected,
555 * %-ENXIO if no suitable firmware interface is present.
557 int fwnode_property_read_u64_array(const struct fwnode_handle
*fwnode
,
558 const char *propname
, u64
*val
, size_t nval
)
560 return fwnode_property_read_int_array(fwnode
, propname
, sizeof(u64
),
563 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array
);
566 * fwnode_property_read_string_array - return string array property of a node
567 * @fwnode: Firmware node to get the property of
568 * @propname: Name of the property
569 * @val: The values are stored here or %NULL to return the number of values
570 * @nval: Size of the @val array
572 * Read an string list property @propname from the given firmware node and store
573 * them to @val if found.
575 * Return: number of values read on success if @val is non-NULL,
576 * number of values available on success if @val is NULL,
577 * %-EINVAL if given arguments are not valid,
578 * %-ENODATA if the property does not have a value,
579 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
580 * %-EOVERFLOW if the size of the property is not as expected,
581 * %-ENXIO if no suitable firmware interface is present.
583 int fwnode_property_read_string_array(const struct fwnode_handle
*fwnode
,
584 const char *propname
, const char **val
,
589 ret
= fwnode_call_int_op(fwnode
, property_read_string_array
, propname
,
591 if (ret
== -EINVAL
&& !IS_ERR_OR_NULL(fwnode
) &&
592 !IS_ERR_OR_NULL(fwnode
->secondary
))
593 ret
= fwnode_call_int_op(fwnode
->secondary
,
594 property_read_string_array
, propname
,
598 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array
);
601 * fwnode_property_read_string - return a string property of a firmware node
602 * @fwnode: Firmware node to get the property of
603 * @propname: Name of the property
604 * @val: The value is stored here
606 * Read property @propname from the given firmware node and store the value into
607 * @val if found. The value is checked to be a string.
609 * Return: %0 if the property was found (success),
610 * %-EINVAL if given arguments are not valid,
611 * %-ENODATA if the property does not have a value,
612 * %-EPROTO or %-EILSEQ if the property is not a string,
613 * %-ENXIO if no suitable firmware interface is present.
615 int fwnode_property_read_string(const struct fwnode_handle
*fwnode
,
616 const char *propname
, const char **val
)
618 int ret
= fwnode_property_read_string_array(fwnode
, propname
, val
, 1);
620 return ret
< 0 ? ret
: 0;
622 EXPORT_SYMBOL_GPL(fwnode_property_read_string
);
625 * fwnode_property_match_string - find a string in an array and return index
626 * @fwnode: Firmware node to get the property of
627 * @propname: Name of the property holding the array
628 * @string: String to look for
630 * Find a given string in a string array and if it is found return the
633 * Return: %0 if the property was found (success),
634 * %-EINVAL if given arguments are not valid,
635 * %-ENODATA if the property does not have a value,
636 * %-EPROTO if the property is not an array of strings,
637 * %-ENXIO if no suitable firmware interface is present.
639 int fwnode_property_match_string(const struct fwnode_handle
*fwnode
,
640 const char *propname
, const char *string
)
645 nval
= fwnode_property_read_string_array(fwnode
, propname
, NULL
, 0);
652 values
= kcalloc(nval
, sizeof(*values
), GFP_KERNEL
);
656 ret
= fwnode_property_read_string_array(fwnode
, propname
, values
, nval
);
660 ret
= match_string(values
, nval
, string
);
667 EXPORT_SYMBOL_GPL(fwnode_property_match_string
);
670 * fwnode_property_get_reference_args() - Find a reference with arguments
671 * @fwnode: Firmware node where to look for the reference
672 * @prop: The name of the property
673 * @nargs_prop: The name of the property telling the number of
674 * arguments in the referred node. NULL if @nargs is known,
675 * otherwise @nargs is ignored. Only relevant on OF.
676 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
677 * @index: Index of the reference, from zero onwards.
678 * @args: Result structure with reference and integer arguments.
680 * Obtain a reference based on a named property in an fwnode, with
683 * Caller is responsible to call fwnode_handle_put() on the returned
684 * args->fwnode pointer.
686 * Returns: %0 on success
687 * %-ENOENT when the index is out of bounds, the index has an empty
688 * reference or the property was not found
689 * %-EINVAL on parse error
691 int fwnode_property_get_reference_args(const struct fwnode_handle
*fwnode
,
692 const char *prop
, const char *nargs_prop
,
693 unsigned int nargs
, unsigned int index
,
694 struct fwnode_reference_args
*args
)
696 return fwnode_call_int_op(fwnode
, get_reference_args
, prop
, nargs_prop
,
699 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args
);
701 static int property_copy_string_array(struct property_entry
*dst
,
702 const struct property_entry
*src
)
705 size_t nval
= src
->length
/ sizeof(*d
);
708 d
= kcalloc(nval
, sizeof(*d
), GFP_KERNEL
);
712 for (i
= 0; i
< nval
; i
++) {
713 d
[i
] = kstrdup(src
->pointer
.str
[i
], GFP_KERNEL
);
714 if (!d
[i
] && src
->pointer
.str
[i
]) {
722 dst
->pointer
.raw_data
= d
;
726 static int property_entry_copy_data(struct property_entry
*dst
,
727 const struct property_entry
*src
)
731 dst
->name
= kstrdup(src
->name
, GFP_KERNEL
);
741 if (src
->is_string
) {
742 error
= property_copy_string_array(dst
, src
);
746 dst
->pointer
.raw_data
= kmemdup(src
->pointer
.raw_data
,
747 src
->length
, GFP_KERNEL
);
748 if (!dst
->pointer
.raw_data
) {
753 } else if (src
->is_string
) {
754 dst
->value
.str
= kstrdup(src
->value
.str
, GFP_KERNEL
);
755 if (!dst
->value
.str
&& src
->value
.str
) {
760 dst
->value
.raw_data
= src
->value
.raw_data
;
763 dst
->length
= src
->length
;
764 dst
->is_array
= src
->is_array
;
765 dst
->is_string
= src
->is_string
;
774 static void property_entry_free_data(const struct property_entry
*p
)
779 if (p
->is_string
&& p
->pointer
.str
) {
780 nval
= p
->length
/ sizeof(const char *);
781 for (i
= 0; i
< nval
; i
++)
782 kfree(p
->pointer
.str
[i
]);
784 kfree(p
->pointer
.raw_data
);
785 } else if (p
->is_string
) {
792 * property_entries_dup - duplicate array of properties
793 * @properties: array of properties to copy
795 * This function creates a deep copy of the given NULL-terminated array
796 * of property entries.
798 struct property_entry
*
799 property_entries_dup(const struct property_entry
*properties
)
801 struct property_entry
*p
;
804 while (properties
[n
].name
)
807 p
= kcalloc(n
+ 1, sizeof(*p
), GFP_KERNEL
);
809 return ERR_PTR(-ENOMEM
);
811 for (i
= 0; i
< n
; i
++) {
812 int ret
= property_entry_copy_data(&p
[i
], &properties
[i
]);
815 property_entry_free_data(&p
[i
]);
823 EXPORT_SYMBOL_GPL(property_entries_dup
);
826 * property_entries_free - free previously allocated array of properties
827 * @properties: array of properties to destroy
829 * This function frees given NULL-terminated array of property entries,
830 * along with their data.
832 void property_entries_free(const struct property_entry
*properties
)
834 const struct property_entry
*p
;
836 for (p
= properties
; p
->name
; p
++)
837 property_entry_free_data(p
);
841 EXPORT_SYMBOL_GPL(property_entries_free
);
844 * pset_free_set - releases memory allocated for copied property set
845 * @pset: Property set to release
847 * Function takes previously copied property set and releases all the
848 * memory allocated to it.
850 static void pset_free_set(struct property_set
*pset
)
855 property_entries_free(pset
->properties
);
860 * pset_copy_set - copies property set
861 * @pset: Property set to copy
863 * This function takes a deep copy of the given property set and returns
864 * pointer to the copy. Call device_free_property_set() to free resources
865 * allocated in this function.
867 * Return: Pointer to the new property set or error pointer.
869 static struct property_set
*pset_copy_set(const struct property_set
*pset
)
871 struct property_entry
*properties
;
872 struct property_set
*p
;
874 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
876 return ERR_PTR(-ENOMEM
);
878 properties
= property_entries_dup(pset
->properties
);
879 if (IS_ERR(properties
)) {
881 return ERR_CAST(properties
);
884 p
->properties
= properties
;
889 * device_remove_properties - Remove properties from a device object.
890 * @dev: Device whose properties to remove.
892 * The function removes properties previously associated to the device
893 * secondary firmware node with device_add_properties(). Memory allocated
894 * to the properties will also be released.
896 void device_remove_properties(struct device
*dev
)
898 struct fwnode_handle
*fwnode
;
899 struct property_set
*pset
;
901 fwnode
= dev_fwnode(dev
);
905 * Pick either primary or secondary node depending which one holds
906 * the pset. If there is no real firmware node (ACPI/DT) primary
907 * will hold the pset.
909 pset
= to_pset_node(fwnode
);
911 set_primary_fwnode(dev
, NULL
);
913 pset
= to_pset_node(fwnode
->secondary
);
914 if (pset
&& dev
== pset
->dev
)
915 set_secondary_fwnode(dev
, NULL
);
917 if (pset
&& dev
== pset
->dev
)
920 EXPORT_SYMBOL_GPL(device_remove_properties
);
923 * device_add_properties - Add a collection of properties to a device object.
924 * @dev: Device to add properties to.
925 * @properties: Collection of properties to add.
927 * Associate a collection of device properties represented by @properties with
928 * @dev as its secondary firmware node. The function takes a copy of
931 int device_add_properties(struct device
*dev
,
932 const struct property_entry
*properties
)
934 struct property_set
*p
, pset
;
939 pset
.properties
= properties
;
941 p
= pset_copy_set(&pset
);
945 p
->fwnode
.ops
= &pset_fwnode_ops
;
946 set_secondary_fwnode(dev
, &p
->fwnode
);
950 EXPORT_SYMBOL_GPL(device_add_properties
);
953 * fwnode_get_next_parent - Iterate to the node's parent
954 * @fwnode: Firmware whose parent is retrieved
956 * This is like fwnode_get_parent() except that it drops the refcount
957 * on the passed node, making it suitable for iterating through a
960 * Returns a node pointer with refcount incremented, use
961 * fwnode_handle_node() on it when done.
963 struct fwnode_handle
*fwnode_get_next_parent(struct fwnode_handle
*fwnode
)
965 struct fwnode_handle
*parent
= fwnode_get_parent(fwnode
);
967 fwnode_handle_put(fwnode
);
971 EXPORT_SYMBOL_GPL(fwnode_get_next_parent
);
974 * fwnode_get_parent - Return parent firwmare node
975 * @fwnode: Firmware whose parent is retrieved
977 * Return parent firmware node of the given node if possible or %NULL if no
978 * parent was available.
980 struct fwnode_handle
*fwnode_get_parent(const struct fwnode_handle
*fwnode
)
982 return fwnode_call_ptr_op(fwnode
, get_parent
);
984 EXPORT_SYMBOL_GPL(fwnode_get_parent
);
987 * fwnode_get_next_child_node - Return the next child node handle for a node
988 * @fwnode: Firmware node to find the next child node for.
989 * @child: Handle to one of the node's child nodes or a %NULL handle.
991 struct fwnode_handle
*
992 fwnode_get_next_child_node(const struct fwnode_handle
*fwnode
,
993 struct fwnode_handle
*child
)
995 return fwnode_call_ptr_op(fwnode
, get_next_child_node
, child
);
997 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node
);
1000 * device_get_next_child_node - Return the next child node handle for a device
1001 * @dev: Device to find the next child node for.
1002 * @child: Handle to one of the device's child nodes or a null handle.
1004 struct fwnode_handle
*device_get_next_child_node(struct device
*dev
,
1005 struct fwnode_handle
*child
)
1007 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
1008 struct fwnode_handle
*fwnode
= NULL
;
1011 fwnode
= &dev
->of_node
->fwnode
;
1013 fwnode
= acpi_fwnode_handle(adev
);
1015 return fwnode_get_next_child_node(fwnode
, child
);
1017 EXPORT_SYMBOL_GPL(device_get_next_child_node
);
1020 * fwnode_get_named_child_node - Return first matching named child node handle
1021 * @fwnode: Firmware node to find the named child node for.
1022 * @childname: String to match child node name against.
1024 struct fwnode_handle
*
1025 fwnode_get_named_child_node(const struct fwnode_handle
*fwnode
,
1026 const char *childname
)
1028 return fwnode_call_ptr_op(fwnode
, get_named_child_node
, childname
);
1030 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node
);
1033 * device_get_named_child_node - Return first matching named child node handle
1034 * @dev: Device to find the named child node for.
1035 * @childname: String to match child node name against.
1037 struct fwnode_handle
*device_get_named_child_node(struct device
*dev
,
1038 const char *childname
)
1040 return fwnode_get_named_child_node(dev_fwnode(dev
), childname
);
1042 EXPORT_SYMBOL_GPL(device_get_named_child_node
);
1045 * fwnode_handle_get - Obtain a reference to a device node
1046 * @fwnode: Pointer to the device node to obtain the reference to.
1048 void fwnode_handle_get(struct fwnode_handle
*fwnode
)
1050 fwnode_call_void_op(fwnode
, get
);
1052 EXPORT_SYMBOL_GPL(fwnode_handle_get
);
1055 * fwnode_handle_put - Drop reference to a device node
1056 * @fwnode: Pointer to the device node to drop the reference to.
1058 * This has to be used when terminating device_for_each_child_node() iteration
1059 * with break or return to prevent stale device node references from being left
1062 void fwnode_handle_put(struct fwnode_handle
*fwnode
)
1064 fwnode_call_void_op(fwnode
, put
);
1066 EXPORT_SYMBOL_GPL(fwnode_handle_put
);
1069 * fwnode_device_is_available - check if a device is available for use
1070 * @fwnode: Pointer to the fwnode of the device.
1072 bool fwnode_device_is_available(const struct fwnode_handle
*fwnode
)
1074 return fwnode_call_bool_op(fwnode
, device_is_available
);
1076 EXPORT_SYMBOL_GPL(fwnode_device_is_available
);
1079 * device_get_child_node_count - return the number of child nodes for device
1080 * @dev: Device to cound the child nodes for
1082 unsigned int device_get_child_node_count(struct device
*dev
)
1084 struct fwnode_handle
*child
;
1085 unsigned int count
= 0;
1087 device_for_each_child_node(dev
, child
)
1092 EXPORT_SYMBOL_GPL(device_get_child_node_count
);
1094 bool device_dma_supported(struct device
*dev
)
1096 /* For DT, this is always supported.
1097 * For ACPI, this depends on CCA, which
1098 * is determined by the acpi_dma_supported().
1100 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
)
1103 return acpi_dma_supported(ACPI_COMPANION(dev
));
1105 EXPORT_SYMBOL_GPL(device_dma_supported
);
1107 enum dev_dma_attr
device_get_dma_attr(struct device
*dev
)
1109 enum dev_dma_attr attr
= DEV_DMA_NOT_SUPPORTED
;
1111 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
1112 if (of_dma_is_coherent(dev
->of_node
))
1113 attr
= DEV_DMA_COHERENT
;
1115 attr
= DEV_DMA_NON_COHERENT
;
1117 attr
= acpi_get_dma_attr(ACPI_COMPANION(dev
));
1121 EXPORT_SYMBOL_GPL(device_get_dma_attr
);
1124 * device_get_phy_mode - Get phy mode for given device
1125 * @dev: Pointer to the given device
1127 * The function gets phy interface string from property 'phy-mode' or
1128 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1131 int device_get_phy_mode(struct device
*dev
)
1136 err
= device_property_read_string(dev
, "phy-mode", &pm
);
1138 err
= device_property_read_string(dev
,
1139 "phy-connection-type", &pm
);
1143 for (i
= 0; i
< PHY_INTERFACE_MODE_MAX
; i
++)
1144 if (!strcasecmp(pm
, phy_modes(i
)))
1149 EXPORT_SYMBOL_GPL(device_get_phy_mode
);
1151 static void *device_get_mac_addr(struct device
*dev
,
1152 const char *name
, char *addr
,
1155 int ret
= device_property_read_u8_array(dev
, name
, addr
, alen
);
1157 if (ret
== 0 && alen
== ETH_ALEN
&& is_valid_ether_addr(addr
))
1163 * device_get_mac_address - Get the MAC for a given device
1164 * @dev: Pointer to the device
1165 * @addr: Address of buffer to store the MAC in
1166 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1168 * Search the firmware node for the best MAC address to use. 'mac-address' is
1169 * checked first, because that is supposed to contain to "most recent" MAC
1170 * address. If that isn't set, then 'local-mac-address' is checked next,
1171 * because that is the default address. If that isn't set, then the obsolete
1172 * 'address' is checked, just in case we're using an old device tree.
1174 * Note that the 'address' property is supposed to contain a virtual address of
1175 * the register set, but some DTS files have redefined that property to be the
1178 * All-zero MAC addresses are rejected, because those could be properties that
1179 * exist in the firmware tables, but were not updated by the firmware. For
1180 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1181 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1182 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1183 * exists but is all zeros.
1185 void *device_get_mac_address(struct device
*dev
, char *addr
, int alen
)
1189 res
= device_get_mac_addr(dev
, "mac-address", addr
, alen
);
1193 res
= device_get_mac_addr(dev
, "local-mac-address", addr
, alen
);
1197 return device_get_mac_addr(dev
, "address", addr
, alen
);
1199 EXPORT_SYMBOL(device_get_mac_address
);
1202 * device_graph_get_next_endpoint - Get next endpoint firmware node
1203 * @fwnode: Pointer to the parent firmware node
1204 * @prev: Previous endpoint node or %NULL to get the first
1206 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1209 struct fwnode_handle
*
1210 fwnode_graph_get_next_endpoint(const struct fwnode_handle
*fwnode
,
1211 struct fwnode_handle
*prev
)
1213 return fwnode_call_ptr_op(fwnode
, graph_get_next_endpoint
, prev
);
1215 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint
);
1218 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1219 * @endpoint: Endpoint firmware node of the port
1221 * Return: the firmware node of the device the @endpoint belongs to.
1223 struct fwnode_handle
*
1224 fwnode_graph_get_port_parent(const struct fwnode_handle
*endpoint
)
1226 struct fwnode_handle
*port
, *parent
;
1228 port
= fwnode_get_parent(endpoint
);
1229 parent
= fwnode_call_ptr_op(port
, graph_get_port_parent
);
1231 fwnode_handle_put(port
);
1235 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent
);
1238 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1239 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1241 * Extracts firmware node of a remote device the @fwnode points to.
1243 struct fwnode_handle
*
1244 fwnode_graph_get_remote_port_parent(const struct fwnode_handle
*fwnode
)
1246 struct fwnode_handle
*endpoint
, *parent
;
1248 endpoint
= fwnode_graph_get_remote_endpoint(fwnode
);
1249 parent
= fwnode_graph_get_port_parent(endpoint
);
1251 fwnode_handle_put(endpoint
);
1255 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent
);
1258 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1259 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1261 * Extracts firmware node of a remote port the @fwnode points to.
1263 struct fwnode_handle
*
1264 fwnode_graph_get_remote_port(const struct fwnode_handle
*fwnode
)
1266 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode
));
1268 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port
);
1271 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1272 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1274 * Extracts firmware node of a remote endpoint the @fwnode points to.
1276 struct fwnode_handle
*
1277 fwnode_graph_get_remote_endpoint(const struct fwnode_handle
*fwnode
)
1279 return fwnode_call_ptr_op(fwnode
, graph_get_remote_endpoint
);
1281 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint
);
1284 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1285 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1286 * @port_id: identifier of the parent port node
1287 * @endpoint_id: identifier of the endpoint node
1289 * Return: Remote fwnode handle associated with remote endpoint node linked
1290 * to @node. Use fwnode_node_put() on it when done.
1292 struct fwnode_handle
*
1293 fwnode_graph_get_remote_node(const struct fwnode_handle
*fwnode
, u32 port_id
,
1296 struct fwnode_handle
*endpoint
= NULL
;
1298 while ((endpoint
= fwnode_graph_get_next_endpoint(fwnode
, endpoint
))) {
1299 struct fwnode_endpoint fwnode_ep
;
1300 struct fwnode_handle
*remote
;
1303 ret
= fwnode_graph_parse_endpoint(endpoint
, &fwnode_ep
);
1307 if (fwnode_ep
.port
!= port_id
|| fwnode_ep
.id
!= endpoint_id
)
1310 remote
= fwnode_graph_get_remote_port_parent(endpoint
);
1314 return fwnode_device_is_available(remote
) ? remote
: NULL
;
1319 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node
);
1322 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1323 * @fwnode: pointer to endpoint fwnode_handle
1324 * @endpoint: pointer to the fwnode endpoint data structure
1326 * Parse @fwnode representing a graph endpoint node and store the
1327 * information in @endpoint. The caller must hold a reference to
1330 int fwnode_graph_parse_endpoint(const struct fwnode_handle
*fwnode
,
1331 struct fwnode_endpoint
*endpoint
)
1333 memset(endpoint
, 0, sizeof(*endpoint
));
1335 return fwnode_call_int_op(fwnode
, graph_parse_endpoint
, endpoint
);
1337 EXPORT_SYMBOL(fwnode_graph_parse_endpoint
);