2 * property.c - Unified device property interface.
4 * Copyright (C) 2014, Intel Corporation
5 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/acpi.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
17 #include <linux/of_address.h>
18 #include <linux/property.h>
19 #include <linux/etherdevice.h>
20 #include <linux/phy.h>
22 static inline bool is_pset_node(struct fwnode_handle
*fwnode
)
24 return fwnode
&& fwnode
->type
== FWNODE_PDATA
;
27 static inline struct property_set
*to_pset_node(struct fwnode_handle
*fwnode
)
29 return is_pset_node(fwnode
) ?
30 container_of(fwnode
, struct property_set
, fwnode
) : NULL
;
33 static struct property_entry
*pset_prop_get(struct property_set
*pset
,
36 struct property_entry
*prop
;
38 if (!pset
|| !pset
->properties
)
41 for (prop
= pset
->properties
; prop
->name
; prop
++)
42 if (!strcmp(name
, prop
->name
))
48 static void *pset_prop_find(struct property_set
*pset
, const char *propname
,
51 struct property_entry
*prop
;
54 prop
= pset_prop_get(pset
, propname
);
56 return ERR_PTR(-EINVAL
);
58 pointer
= prop
->pointer
.raw_data
;
60 pointer
= &prop
->value
.raw_data
;
62 return ERR_PTR(-ENODATA
);
63 if (length
> prop
->length
)
64 return ERR_PTR(-EOVERFLOW
);
68 static int pset_prop_read_u8_array(struct property_set
*pset
,
70 u8
*values
, size_t nval
)
73 size_t length
= nval
* sizeof(*values
);
75 pointer
= pset_prop_find(pset
, propname
, length
);
77 return PTR_ERR(pointer
);
79 memcpy(values
, pointer
, length
);
83 static int pset_prop_read_u16_array(struct property_set
*pset
,
85 u16
*values
, size_t nval
)
88 size_t length
= nval
* sizeof(*values
);
90 pointer
= pset_prop_find(pset
, propname
, length
);
92 return PTR_ERR(pointer
);
94 memcpy(values
, pointer
, length
);
98 static int pset_prop_read_u32_array(struct property_set
*pset
,
100 u32
*values
, size_t nval
)
103 size_t length
= nval
* sizeof(*values
);
105 pointer
= pset_prop_find(pset
, propname
, length
);
107 return PTR_ERR(pointer
);
109 memcpy(values
, pointer
, length
);
113 static int pset_prop_read_u64_array(struct property_set
*pset
,
114 const char *propname
,
115 u64
*values
, size_t nval
)
118 size_t length
= nval
* sizeof(*values
);
120 pointer
= pset_prop_find(pset
, propname
, length
);
122 return PTR_ERR(pointer
);
124 memcpy(values
, pointer
, length
);
128 static int pset_prop_count_elems_of_size(struct property_set
*pset
,
129 const char *propname
, size_t length
)
131 struct property_entry
*prop
;
133 prop
= pset_prop_get(pset
, propname
);
137 return prop
->length
/ length
;
140 static int pset_prop_read_string_array(struct property_set
*pset
,
141 const char *propname
,
142 const char **strings
, size_t nval
)
145 size_t length
= nval
* sizeof(*strings
);
147 pointer
= pset_prop_find(pset
, propname
, length
);
149 return PTR_ERR(pointer
);
151 memcpy(strings
, pointer
, length
);
155 static int pset_prop_read_string(struct property_set
*pset
,
156 const char *propname
, const char **strings
)
158 struct property_entry
*prop
;
159 const char **pointer
;
161 prop
= pset_prop_get(pset
, propname
);
164 if (!prop
->is_string
)
166 if (prop
->is_array
) {
167 pointer
= prop
->pointer
.str
;
171 pointer
= &prop
->value
.str
;
172 if (*pointer
&& strnlen(*pointer
, prop
->length
) >= prop
->length
)
180 static inline struct fwnode_handle
*dev_fwnode(struct device
*dev
)
182 return IS_ENABLED(CONFIG_OF
) && dev
->of_node
?
183 &dev
->of_node
->fwnode
: dev
->fwnode
;
187 * device_property_present - check if a property of a device is present
188 * @dev: Device whose property is being checked
189 * @propname: Name of the property
191 * Check if property @propname is present in the device firmware description.
193 bool device_property_present(struct device
*dev
, const char *propname
)
195 return fwnode_property_present(dev_fwnode(dev
), propname
);
197 EXPORT_SYMBOL_GPL(device_property_present
);
199 static bool __fwnode_property_present(struct fwnode_handle
*fwnode
,
200 const char *propname
)
202 if (is_of_node(fwnode
))
203 return of_property_read_bool(to_of_node(fwnode
), propname
);
204 else if (is_acpi_node(fwnode
))
205 return !acpi_node_prop_get(fwnode
, propname
, NULL
);
206 else if (is_pset_node(fwnode
))
207 return !!pset_prop_get(to_pset_node(fwnode
), propname
);
212 * fwnode_property_present - check if a property of a firmware node is present
213 * @fwnode: Firmware node whose property to check
214 * @propname: Name of the property
216 bool fwnode_property_present(struct fwnode_handle
*fwnode
, const char *propname
)
220 ret
= __fwnode_property_present(fwnode
, propname
);
221 if (ret
== false && fwnode
&& fwnode
->secondary
)
222 ret
= __fwnode_property_present(fwnode
->secondary
, propname
);
225 EXPORT_SYMBOL_GPL(fwnode_property_present
);
228 * device_property_read_u8_array - return a u8 array property of a device
229 * @dev: Device to get the property of
230 * @propname: Name of the property
231 * @val: The values are stored here or %NULL to return the number of values
232 * @nval: Size of the @val array
234 * Function reads an array of u8 properties with @propname from the device
235 * firmware description and stores them to @val if found.
237 * Return: number of values if @val was %NULL,
238 * %0 if the property was found (success),
239 * %-EINVAL if given arguments are not valid,
240 * %-ENODATA if the property does not have a value,
241 * %-EPROTO if the property is not an array of numbers,
242 * %-EOVERFLOW if the size of the property is not as expected.
243 * %-ENXIO if no suitable firmware interface is present.
245 int device_property_read_u8_array(struct device
*dev
, const char *propname
,
246 u8
*val
, size_t nval
)
248 return fwnode_property_read_u8_array(dev_fwnode(dev
), propname
, val
, nval
);
250 EXPORT_SYMBOL_GPL(device_property_read_u8_array
);
253 * device_property_read_u16_array - return a u16 array property of a device
254 * @dev: Device to get the property of
255 * @propname: Name of the property
256 * @val: The values are stored here or %NULL to return the number of values
257 * @nval: Size of the @val array
259 * Function reads an array of u16 properties with @propname from the device
260 * firmware description and stores them to @val if found.
262 * Return: number of values if @val was %NULL,
263 * %0 if the property was found (success),
264 * %-EINVAL if given arguments are not valid,
265 * %-ENODATA if the property does not have a value,
266 * %-EPROTO if the property is not an array of numbers,
267 * %-EOVERFLOW if the size of the property is not as expected.
268 * %-ENXIO if no suitable firmware interface is present.
270 int device_property_read_u16_array(struct device
*dev
, const char *propname
,
271 u16
*val
, size_t nval
)
273 return fwnode_property_read_u16_array(dev_fwnode(dev
), propname
, val
, nval
);
275 EXPORT_SYMBOL_GPL(device_property_read_u16_array
);
278 * device_property_read_u32_array - return a u32 array property of a device
279 * @dev: Device to get the property of
280 * @propname: Name of the property
281 * @val: The values are stored here or %NULL to return the number of values
282 * @nval: Size of the @val array
284 * Function reads an array of u32 properties with @propname from the device
285 * firmware description and stores them to @val if found.
287 * Return: number of values if @val was %NULL,
288 * %0 if the property was found (success),
289 * %-EINVAL if given arguments are not valid,
290 * %-ENODATA if the property does not have a value,
291 * %-EPROTO if the property is not an array of numbers,
292 * %-EOVERFLOW if the size of the property is not as expected.
293 * %-ENXIO if no suitable firmware interface is present.
295 int device_property_read_u32_array(struct device
*dev
, const char *propname
,
296 u32
*val
, size_t nval
)
298 return fwnode_property_read_u32_array(dev_fwnode(dev
), propname
, val
, nval
);
300 EXPORT_SYMBOL_GPL(device_property_read_u32_array
);
303 * device_property_read_u64_array - return a u64 array property of a device
304 * @dev: Device to get the property of
305 * @propname: Name of the property
306 * @val: The values are stored here or %NULL to return the number of values
307 * @nval: Size of the @val array
309 * Function reads an array of u64 properties with @propname from the device
310 * firmware description and stores them to @val if found.
312 * Return: number of values if @val was %NULL,
313 * %0 if the property was found (success),
314 * %-EINVAL if given arguments are not valid,
315 * %-ENODATA if the property does not have a value,
316 * %-EPROTO if the property is not an array of numbers,
317 * %-EOVERFLOW if the size of the property is not as expected.
318 * %-ENXIO if no suitable firmware interface is present.
320 int device_property_read_u64_array(struct device
*dev
, const char *propname
,
321 u64
*val
, size_t nval
)
323 return fwnode_property_read_u64_array(dev_fwnode(dev
), propname
, val
, nval
);
325 EXPORT_SYMBOL_GPL(device_property_read_u64_array
);
328 * device_property_read_string_array - return a string array property of device
329 * @dev: Device to get the property of
330 * @propname: Name of the property
331 * @val: The values are stored here or %NULL to return the number of values
332 * @nval: Size of the @val array
334 * Function reads an array of string properties with @propname from the device
335 * firmware description and stores them to @val if found.
337 * Return: number of values if @val was %NULL,
338 * %0 if the property was found (success),
339 * %-EINVAL if given arguments are not valid,
340 * %-ENODATA if the property does not have a value,
341 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
342 * %-EOVERFLOW if the size of the property is not as expected.
343 * %-ENXIO if no suitable firmware interface is present.
345 int device_property_read_string_array(struct device
*dev
, const char *propname
,
346 const char **val
, size_t nval
)
348 return fwnode_property_read_string_array(dev_fwnode(dev
), propname
, val
, nval
);
350 EXPORT_SYMBOL_GPL(device_property_read_string_array
);
353 * device_property_read_string - return a string property of a device
354 * @dev: Device to get the property of
355 * @propname: Name of the property
356 * @val: The value is stored here
358 * Function reads property @propname from the device firmware description and
359 * stores the value into @val if found. The value is checked to be a string.
361 * Return: %0 if the property was found (success),
362 * %-EINVAL if given arguments are not valid,
363 * %-ENODATA if the property does not have a value,
364 * %-EPROTO or %-EILSEQ if the property type is not a string.
365 * %-ENXIO if no suitable firmware interface is present.
367 int device_property_read_string(struct device
*dev
, const char *propname
,
370 return fwnode_property_read_string(dev_fwnode(dev
), propname
, val
);
372 EXPORT_SYMBOL_GPL(device_property_read_string
);
375 * device_property_match_string - find a string in an array and return index
376 * @dev: Device to get the property of
377 * @propname: Name of the property holding the array
378 * @string: String to look for
380 * Find a given string in a string array and if it is found return the
383 * Return: %0 if the property was found (success),
384 * %-EINVAL if given arguments are not valid,
385 * %-ENODATA if the property does not have a value,
386 * %-EPROTO if the property is not an array of strings,
387 * %-ENXIO if no suitable firmware interface is present.
389 int device_property_match_string(struct device
*dev
, const char *propname
,
392 return fwnode_property_match_string(dev_fwnode(dev
), propname
, string
);
394 EXPORT_SYMBOL_GPL(device_property_match_string
);
396 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
397 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
398 : of_property_count_elems_of_size((node), (propname), sizeof(type))
400 #define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
401 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
402 : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
404 #define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
407 if (is_of_node(_fwnode_)) \
408 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
409 _type_, _val_, _nval_); \
410 else if (is_acpi_node(_fwnode_)) \
411 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
413 else if (is_pset_node(_fwnode_)) \
414 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
415 _type_, _val_, _nval_); \
421 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
424 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \
426 if (_ret_ == -EINVAL && _fwnode_ && _fwnode_->secondary) \
427 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \
428 _proptype_, _val_, _nval_); \
433 * fwnode_property_read_u8_array - return a u8 array property of firmware node
434 * @fwnode: Firmware node to get the property of
435 * @propname: Name of the property
436 * @val: The values are stored here or %NULL to return the number of values
437 * @nval: Size of the @val array
439 * Read an array of u8 properties with @propname from @fwnode and stores them to
442 * Return: number of values if @val was %NULL,
443 * %0 if the property was found (success),
444 * %-EINVAL if given arguments are not valid,
445 * %-ENODATA if the property does not have a value,
446 * %-EPROTO if the property is not an array of numbers,
447 * %-EOVERFLOW if the size of the property is not as expected,
448 * %-ENXIO if no suitable firmware interface is present.
450 int fwnode_property_read_u8_array(struct fwnode_handle
*fwnode
,
451 const char *propname
, u8
*val
, size_t nval
)
453 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u8
, DEV_PROP_U8
,
456 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array
);
459 * fwnode_property_read_u16_array - return a u16 array property of firmware node
460 * @fwnode: Firmware node to get the property of
461 * @propname: Name of the property
462 * @val: The values are stored here or %NULL to return the number of values
463 * @nval: Size of the @val array
465 * Read an array of u16 properties with @propname from @fwnode and store them to
468 * Return: number of values if @val was %NULL,
469 * %0 if the property was found (success),
470 * %-EINVAL if given arguments are not valid,
471 * %-ENODATA if the property does not have a value,
472 * %-EPROTO if the property is not an array of numbers,
473 * %-EOVERFLOW if the size of the property is not as expected,
474 * %-ENXIO if no suitable firmware interface is present.
476 int fwnode_property_read_u16_array(struct fwnode_handle
*fwnode
,
477 const char *propname
, u16
*val
, size_t nval
)
479 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u16
, DEV_PROP_U16
,
482 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array
);
485 * fwnode_property_read_u32_array - return a u32 array property of firmware node
486 * @fwnode: Firmware node to get the property of
487 * @propname: Name of the property
488 * @val: The values are stored here or %NULL to return the number of values
489 * @nval: Size of the @val array
491 * Read an array of u32 properties with @propname from @fwnode store them to
494 * Return: number of values if @val was %NULL,
495 * %0 if the property was found (success),
496 * %-EINVAL if given arguments are not valid,
497 * %-ENODATA if the property does not have a value,
498 * %-EPROTO if the property is not an array of numbers,
499 * %-EOVERFLOW if the size of the property is not as expected,
500 * %-ENXIO if no suitable firmware interface is present.
502 int fwnode_property_read_u32_array(struct fwnode_handle
*fwnode
,
503 const char *propname
, u32
*val
, size_t nval
)
505 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u32
, DEV_PROP_U32
,
508 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array
);
511 * fwnode_property_read_u64_array - return a u64 array property firmware node
512 * @fwnode: Firmware node to get the property of
513 * @propname: Name of the property
514 * @val: The values are stored here or %NULL to return the number of values
515 * @nval: Size of the @val array
517 * Read an array of u64 properties with @propname from @fwnode and store them to
520 * Return: number of values if @val was %NULL,
521 * %0 if the property was found (success),
522 * %-EINVAL if given arguments are not valid,
523 * %-ENODATA if the property does not have a value,
524 * %-EPROTO if the property is not an array of numbers,
525 * %-EOVERFLOW if the size of the property is not as expected,
526 * %-ENXIO if no suitable firmware interface is present.
528 int fwnode_property_read_u64_array(struct fwnode_handle
*fwnode
,
529 const char *propname
, u64
*val
, size_t nval
)
531 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u64
, DEV_PROP_U64
,
534 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array
);
536 static int __fwnode_property_read_string_array(struct fwnode_handle
*fwnode
,
537 const char *propname
,
538 const char **val
, size_t nval
)
540 if (is_of_node(fwnode
))
542 of_property_read_string_array(to_of_node(fwnode
),
543 propname
, val
, nval
) :
544 of_property_count_strings(to_of_node(fwnode
), propname
);
545 else if (is_acpi_node(fwnode
))
546 return acpi_node_prop_read(fwnode
, propname
, DEV_PROP_STRING
,
548 else if (is_pset_node(fwnode
))
550 pset_prop_read_string_array(to_pset_node(fwnode
),
551 propname
, val
, nval
) :
552 pset_prop_count_elems_of_size(to_pset_node(fwnode
),
554 sizeof(const char *));
558 static int __fwnode_property_read_string(struct fwnode_handle
*fwnode
,
559 const char *propname
, const char **val
)
561 if (is_of_node(fwnode
))
562 return of_property_read_string(to_of_node(fwnode
), propname
, val
);
563 else if (is_acpi_node(fwnode
))
564 return acpi_node_prop_read(fwnode
, propname
, DEV_PROP_STRING
,
566 else if (is_pset_node(fwnode
))
567 return pset_prop_read_string(to_pset_node(fwnode
), propname
, val
);
572 * fwnode_property_read_string_array - return string array property of a node
573 * @fwnode: Firmware node to get the property of
574 * @propname: Name of the property
575 * @val: The values are stored here or %NULL to return the number of values
576 * @nval: Size of the @val array
578 * Read an string list property @propname from the given firmware node and store
579 * them to @val if found.
581 * Return: number of values if @val was %NULL,
582 * %0 if the property was found (success),
583 * %-EINVAL if given arguments are not valid,
584 * %-ENODATA if the property does not have a value,
585 * %-EPROTO if the property is not an array of strings,
586 * %-EOVERFLOW if the size of the property is not as expected,
587 * %-ENXIO if no suitable firmware interface is present.
589 int fwnode_property_read_string_array(struct fwnode_handle
*fwnode
,
590 const char *propname
, const char **val
,
595 ret
= __fwnode_property_read_string_array(fwnode
, propname
, val
, nval
);
596 if (ret
== -EINVAL
&& fwnode
&& fwnode
->secondary
)
597 ret
= __fwnode_property_read_string_array(fwnode
->secondary
,
598 propname
, val
, nval
);
601 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array
);
604 * fwnode_property_read_string - return a string property of a firmware node
605 * @fwnode: Firmware node to get the property of
606 * @propname: Name of the property
607 * @val: The value is stored here
609 * Read property @propname from the given firmware node and store the value into
610 * @val if found. The value is checked to be a string.
612 * Return: %0 if the property was found (success),
613 * %-EINVAL if given arguments are not valid,
614 * %-ENODATA if the property does not have a value,
615 * %-EPROTO or %-EILSEQ if the property is not a string,
616 * %-ENXIO if no suitable firmware interface is present.
618 int fwnode_property_read_string(struct fwnode_handle
*fwnode
,
619 const char *propname
, const char **val
)
623 ret
= __fwnode_property_read_string(fwnode
, propname
, val
);
624 if (ret
== -EINVAL
&& fwnode
&& fwnode
->secondary
)
625 ret
= __fwnode_property_read_string(fwnode
->secondary
,
629 EXPORT_SYMBOL_GPL(fwnode_property_read_string
);
632 * fwnode_property_match_string - find a string in an array and return index
633 * @fwnode: Firmware node to get the property of
634 * @propname: Name of the property holding the array
635 * @string: String to look for
637 * Find a given string in a string array and if it is found return the
640 * Return: %0 if the property was found (success),
641 * %-EINVAL if given arguments are not valid,
642 * %-ENODATA if the property does not have a value,
643 * %-EPROTO if the property is not an array of strings,
644 * %-ENXIO if no suitable firmware interface is present.
646 int fwnode_property_match_string(struct fwnode_handle
*fwnode
,
647 const char *propname
, const char *string
)
652 nval
= fwnode_property_read_string_array(fwnode
, propname
, NULL
, 0);
659 values
= kcalloc(nval
, sizeof(*values
), GFP_KERNEL
);
663 ret
= fwnode_property_read_string_array(fwnode
, propname
, values
, nval
);
668 for (i
= 0; i
< nval
; i
++) {
669 if (!strcmp(values
[i
], string
)) {
678 EXPORT_SYMBOL_GPL(fwnode_property_match_string
);
681 * pset_free_set - releases memory allocated for copied property set
682 * @pset: Property set to release
684 * Function takes previously copied property set and releases all the
685 * memory allocated to it.
687 static void pset_free_set(struct property_set
*pset
)
689 const struct property_entry
*prop
;
695 for (prop
= pset
->properties
; prop
->name
; prop
++) {
696 if (prop
->is_array
) {
697 if (prop
->is_string
&& prop
->pointer
.str
) {
698 nval
= prop
->length
/ sizeof(const char *);
699 for (i
= 0; i
< nval
; i
++)
700 kfree(prop
->pointer
.str
[i
]);
702 kfree(prop
->pointer
.raw_data
);
703 } else if (prop
->is_string
) {
704 kfree(prop
->value
.str
);
709 kfree(pset
->properties
);
713 static int pset_copy_entry(struct property_entry
*dst
,
714 const struct property_entry
*src
)
719 dst
->name
= kstrdup(src
->name
, GFP_KERNEL
);
727 if (src
->is_string
) {
728 nval
= src
->length
/ sizeof(const char *);
729 dst
->pointer
.str
= kcalloc(nval
, sizeof(const char *),
731 if (!dst
->pointer
.str
)
734 d
= dst
->pointer
.str
;
735 s
= src
->pointer
.str
;
736 for (i
= 0; i
< nval
; i
++) {
737 d
[i
] = kstrdup(s
[i
], GFP_KERNEL
);
742 dst
->pointer
.raw_data
= kmemdup(src
->pointer
.raw_data
,
743 src
->length
, GFP_KERNEL
);
744 if (!dst
->pointer
.raw_data
)
747 } else if (src
->is_string
) {
748 dst
->value
.str
= kstrdup(src
->value
.str
, GFP_KERNEL
);
749 if (!dst
->value
.str
&& src
->value
.str
)
752 dst
->value
.raw_data
= src
->value
.raw_data
;
755 dst
->length
= src
->length
;
756 dst
->is_array
= src
->is_array
;
757 dst
->is_string
= src
->is_string
;
763 * pset_copy_set - copies property set
764 * @pset: Property set to copy
766 * This function takes a deep copy of the given property set and returns
767 * pointer to the copy. Call device_free_property_set() to free resources
768 * allocated in this function.
770 * Return: Pointer to the new property set or error pointer.
772 static struct property_set
*pset_copy_set(const struct property_set
*pset
)
774 const struct property_entry
*entry
;
775 struct property_set
*p
;
778 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
780 return ERR_PTR(-ENOMEM
);
782 while (pset
->properties
[n
].name
)
785 p
->properties
= kcalloc(n
+ 1, sizeof(*entry
), GFP_KERNEL
);
786 if (!p
->properties
) {
788 return ERR_PTR(-ENOMEM
);
791 for (i
= 0; i
< n
; i
++) {
792 int ret
= pset_copy_entry(&p
->properties
[i
],
793 &pset
->properties
[i
]);
804 * device_remove_property_set - Remove properties from a device object.
805 * @dev: Device whose properties to remove.
807 * The function removes properties previously associated to the device
808 * secondary firmware node with device_add_property_set(). Memory allocated
809 * to the properties will also be released.
811 void device_remove_property_set(struct device
*dev
)
813 struct fwnode_handle
*fwnode
;
815 fwnode
= dev_fwnode(dev
);
819 * Pick either primary or secondary node depending which one holds
820 * the pset. If there is no real firmware node (ACPI/DT) primary
821 * will hold the pset.
823 if (!is_pset_node(fwnode
))
824 fwnode
= fwnode
->secondary
;
825 if (!IS_ERR(fwnode
) && is_pset_node(fwnode
))
826 pset_free_set(to_pset_node(fwnode
));
827 set_secondary_fwnode(dev
, NULL
);
829 EXPORT_SYMBOL_GPL(device_remove_property_set
);
832 * device_add_property_set - Add a collection of properties to a device object.
833 * @dev: Device to add properties to.
834 * @pset: Collection of properties to add.
836 * Associate a collection of device properties represented by @pset with @dev
837 * as its secondary firmware node. The function takes a copy of @pset.
839 int device_add_property_set(struct device
*dev
, const struct property_set
*pset
)
841 struct property_set
*p
;
846 p
= pset_copy_set(pset
);
850 p
->fwnode
.type
= FWNODE_PDATA
;
851 set_secondary_fwnode(dev
, &p
->fwnode
);
854 EXPORT_SYMBOL_GPL(device_add_property_set
);
857 * device_get_next_child_node - Return the next child node handle for a device
858 * @dev: Device to find the next child node for.
859 * @child: Handle to one of the device's child nodes or a null handle.
861 struct fwnode_handle
*device_get_next_child_node(struct device
*dev
,
862 struct fwnode_handle
*child
)
864 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
865 struct device_node
*node
;
867 node
= of_get_next_available_child(dev
->of_node
, to_of_node(child
));
869 return &node
->fwnode
;
870 } else if (IS_ENABLED(CONFIG_ACPI
)) {
871 return acpi_get_next_subnode(dev
, child
);
875 EXPORT_SYMBOL_GPL(device_get_next_child_node
);
878 * fwnode_handle_put - Drop reference to a device node
879 * @fwnode: Pointer to the device node to drop the reference to.
881 * This has to be used when terminating device_for_each_child_node() iteration
882 * with break or return to prevent stale device node references from being left
885 void fwnode_handle_put(struct fwnode_handle
*fwnode
)
887 if (is_of_node(fwnode
))
888 of_node_put(to_of_node(fwnode
));
890 EXPORT_SYMBOL_GPL(fwnode_handle_put
);
893 * device_get_child_node_count - return the number of child nodes for device
894 * @dev: Device to cound the child nodes for
896 unsigned int device_get_child_node_count(struct device
*dev
)
898 struct fwnode_handle
*child
;
899 unsigned int count
= 0;
901 device_for_each_child_node(dev
, child
)
906 EXPORT_SYMBOL_GPL(device_get_child_node_count
);
908 bool device_dma_supported(struct device
*dev
)
910 /* For DT, this is always supported.
911 * For ACPI, this depends on CCA, which
912 * is determined by the acpi_dma_supported().
914 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
)
917 return acpi_dma_supported(ACPI_COMPANION(dev
));
919 EXPORT_SYMBOL_GPL(device_dma_supported
);
921 enum dev_dma_attr
device_get_dma_attr(struct device
*dev
)
923 enum dev_dma_attr attr
= DEV_DMA_NOT_SUPPORTED
;
925 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
926 if (of_dma_is_coherent(dev
->of_node
))
927 attr
= DEV_DMA_COHERENT
;
929 attr
= DEV_DMA_NON_COHERENT
;
931 attr
= acpi_get_dma_attr(ACPI_COMPANION(dev
));
935 EXPORT_SYMBOL_GPL(device_get_dma_attr
);
938 * device_get_phy_mode - Get phy mode for given device
939 * @dev: Pointer to the given device
941 * The function gets phy interface string from property 'phy-mode' or
942 * 'phy-connection-type', and return its index in phy_modes table, or errno in
945 int device_get_phy_mode(struct device
*dev
)
950 err
= device_property_read_string(dev
, "phy-mode", &pm
);
952 err
= device_property_read_string(dev
,
953 "phy-connection-type", &pm
);
957 for (i
= 0; i
< PHY_INTERFACE_MODE_MAX
; i
++)
958 if (!strcasecmp(pm
, phy_modes(i
)))
963 EXPORT_SYMBOL_GPL(device_get_phy_mode
);
965 static void *device_get_mac_addr(struct device
*dev
,
966 const char *name
, char *addr
,
969 int ret
= device_property_read_u8_array(dev
, name
, addr
, alen
);
971 if (ret
== 0 && alen
== ETH_ALEN
&& is_valid_ether_addr(addr
))
977 * device_get_mac_address - Get the MAC for a given device
978 * @dev: Pointer to the device
979 * @addr: Address of buffer to store the MAC in
980 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
982 * Search the firmware node for the best MAC address to use. 'mac-address' is
983 * checked first, because that is supposed to contain to "most recent" MAC
984 * address. If that isn't set, then 'local-mac-address' is checked next,
985 * because that is the default address. If that isn't set, then the obsolete
986 * 'address' is checked, just in case we're using an old device tree.
988 * Note that the 'address' property is supposed to contain a virtual address of
989 * the register set, but some DTS files have redefined that property to be the
992 * All-zero MAC addresses are rejected, because those could be properties that
993 * exist in the firmware tables, but were not updated by the firmware. For
994 * example, the DTS could define 'mac-address' and 'local-mac-address', with
995 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
996 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
997 * exists but is all zeros.
999 void *device_get_mac_address(struct device
*dev
, char *addr
, int alen
)
1003 res
= device_get_mac_addr(dev
, "mac-address", addr
, alen
);
1007 res
= device_get_mac_addr(dev
, "local-mac-address", addr
, alen
);
1011 return device_get_mac_addr(dev
, "address", addr
, alen
);
1013 EXPORT_SYMBOL(device_get_mac_address
);