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>
24 struct fwnode_handle fwnode
;
25 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 struct property_entry
*pset_prop_get(struct property_set
*pset
,
42 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 void *pset_prop_find(struct property_set
*pset
, const char *propname
,
57 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 struct property_entry
*prop
;
139 prop
= pset_prop_get(pset
, propname
);
143 return prop
->length
/ length
;
146 static int pset_prop_read_string_array(struct property_set
*pset
,
147 const char *propname
,
148 const char **strings
, size_t nval
)
151 size_t length
= nval
* sizeof(*strings
);
153 pointer
= pset_prop_find(pset
, propname
, length
);
155 return PTR_ERR(pointer
);
157 memcpy(strings
, pointer
, length
);
161 static int pset_prop_read_string(struct property_set
*pset
,
162 const char *propname
, const char **strings
)
164 struct property_entry
*prop
;
165 const char **pointer
;
167 prop
= pset_prop_get(pset
, propname
);
170 if (!prop
->is_string
)
172 if (prop
->is_array
) {
173 pointer
= prop
->pointer
.str
;
177 pointer
= &prop
->value
.str
;
178 if (*pointer
&& strnlen(*pointer
, prop
->length
) >= prop
->length
)
186 struct fwnode_handle
*dev_fwnode(struct device
*dev
)
188 return IS_ENABLED(CONFIG_OF
) && dev
->of_node
?
189 &dev
->of_node
->fwnode
: dev
->fwnode
;
191 EXPORT_SYMBOL_GPL(dev_fwnode
);
194 * device_property_present - check if a property of a device is present
195 * @dev: Device whose property is being checked
196 * @propname: Name of the property
198 * Check if property @propname is present in the device firmware description.
200 bool device_property_present(struct device
*dev
, const char *propname
)
202 return fwnode_property_present(dev_fwnode(dev
), propname
);
204 EXPORT_SYMBOL_GPL(device_property_present
);
206 static bool __fwnode_property_present(struct fwnode_handle
*fwnode
,
207 const char *propname
)
209 if (is_of_node(fwnode
))
210 return of_property_read_bool(to_of_node(fwnode
), propname
);
211 else if (is_acpi_node(fwnode
))
212 return !acpi_node_prop_get(fwnode
, propname
, NULL
);
213 else if (is_pset_node(fwnode
))
214 return !!pset_prop_get(to_pset_node(fwnode
), propname
);
219 * fwnode_property_present - check if a property of a firmware node is present
220 * @fwnode: Firmware node whose property to check
221 * @propname: Name of the property
223 bool fwnode_property_present(struct fwnode_handle
*fwnode
, const char *propname
)
227 ret
= __fwnode_property_present(fwnode
, propname
);
228 if (ret
== false && !IS_ERR_OR_NULL(fwnode
) &&
229 !IS_ERR_OR_NULL(fwnode
->secondary
))
230 ret
= __fwnode_property_present(fwnode
->secondary
, propname
);
233 EXPORT_SYMBOL_GPL(fwnode_property_present
);
236 * device_property_read_u8_array - return a u8 array property of a device
237 * @dev: Device to get the property of
238 * @propname: Name of the property
239 * @val: The values are stored here or %NULL to return the number of values
240 * @nval: Size of the @val array
242 * Function reads an array of u8 properties with @propname from the device
243 * firmware description and stores them to @val if found.
245 * Return: number of values if @val was %NULL,
246 * %0 if the property was found (success),
247 * %-EINVAL if given arguments are not valid,
248 * %-ENODATA if the property does not have a value,
249 * %-EPROTO if the property is not an array of numbers,
250 * %-EOVERFLOW if the size of the property is not as expected.
251 * %-ENXIO if no suitable firmware interface is present.
253 int device_property_read_u8_array(struct device
*dev
, const char *propname
,
254 u8
*val
, size_t nval
)
256 return fwnode_property_read_u8_array(dev_fwnode(dev
), propname
, val
, nval
);
258 EXPORT_SYMBOL_GPL(device_property_read_u8_array
);
261 * device_property_read_u16_array - return a u16 array property of a device
262 * @dev: Device to get the property of
263 * @propname: Name of the property
264 * @val: The values are stored here or %NULL to return the number of values
265 * @nval: Size of the @val array
267 * Function reads an array of u16 properties with @propname from the device
268 * firmware description and stores them to @val if found.
270 * Return: number of values if @val was %NULL,
271 * %0 if the property was found (success),
272 * %-EINVAL if given arguments are not valid,
273 * %-ENODATA if the property does not have a value,
274 * %-EPROTO if the property is not an array of numbers,
275 * %-EOVERFLOW if the size of the property is not as expected.
276 * %-ENXIO if no suitable firmware interface is present.
278 int device_property_read_u16_array(struct device
*dev
, const char *propname
,
279 u16
*val
, size_t nval
)
281 return fwnode_property_read_u16_array(dev_fwnode(dev
), propname
, val
, nval
);
283 EXPORT_SYMBOL_GPL(device_property_read_u16_array
);
286 * device_property_read_u32_array - return a u32 array property of a device
287 * @dev: Device to get the property of
288 * @propname: Name of the property
289 * @val: The values are stored here or %NULL to return the number of values
290 * @nval: Size of the @val array
292 * Function reads an array of u32 properties with @propname from the device
293 * firmware description and stores them to @val if found.
295 * Return: number of values if @val was %NULL,
296 * %0 if the property was found (success),
297 * %-EINVAL if given arguments are not valid,
298 * %-ENODATA if the property does not have a value,
299 * %-EPROTO if the property is not an array of numbers,
300 * %-EOVERFLOW if the size of the property is not as expected.
301 * %-ENXIO if no suitable firmware interface is present.
303 int device_property_read_u32_array(struct device
*dev
, const char *propname
,
304 u32
*val
, size_t nval
)
306 return fwnode_property_read_u32_array(dev_fwnode(dev
), propname
, val
, nval
);
308 EXPORT_SYMBOL_GPL(device_property_read_u32_array
);
311 * device_property_read_u64_array - return a u64 array property of a device
312 * @dev: Device to get the property of
313 * @propname: Name of the property
314 * @val: The values are stored here or %NULL to return the number of values
315 * @nval: Size of the @val array
317 * Function reads an array of u64 properties with @propname from the device
318 * firmware description and stores them to @val if found.
320 * Return: number of values if @val was %NULL,
321 * %0 if the property was found (success),
322 * %-EINVAL if given arguments are not valid,
323 * %-ENODATA if the property does not have a value,
324 * %-EPROTO if the property is not an array of numbers,
325 * %-EOVERFLOW if the size of the property is not as expected.
326 * %-ENXIO if no suitable firmware interface is present.
328 int device_property_read_u64_array(struct device
*dev
, const char *propname
,
329 u64
*val
, size_t nval
)
331 return fwnode_property_read_u64_array(dev_fwnode(dev
), propname
, val
, nval
);
333 EXPORT_SYMBOL_GPL(device_property_read_u64_array
);
336 * device_property_read_string_array - return a string array property of device
337 * @dev: Device to get the property of
338 * @propname: Name of the property
339 * @val: The values are stored here or %NULL to return the number of values
340 * @nval: Size of the @val array
342 * Function reads an array of string properties with @propname from the device
343 * firmware description and stores them to @val if found.
345 * Return: number of values if @val was %NULL,
346 * %0 if the property was found (success),
347 * %-EINVAL if given arguments are not valid,
348 * %-ENODATA if the property does not have a value,
349 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
350 * %-EOVERFLOW if the size of the property is not as expected.
351 * %-ENXIO if no suitable firmware interface is present.
353 int device_property_read_string_array(struct device
*dev
, const char *propname
,
354 const char **val
, size_t nval
)
356 return fwnode_property_read_string_array(dev_fwnode(dev
), propname
, val
, nval
);
358 EXPORT_SYMBOL_GPL(device_property_read_string_array
);
361 * device_property_read_string - return a string property of a device
362 * @dev: Device to get the property of
363 * @propname: Name of the property
364 * @val: The value is stored here
366 * Function reads property @propname from the device firmware description and
367 * stores the value into @val if found. The value is checked to be a string.
369 * Return: %0 if the property was found (success),
370 * %-EINVAL if given arguments are not valid,
371 * %-ENODATA if the property does not have a value,
372 * %-EPROTO or %-EILSEQ if the property type is not a string.
373 * %-ENXIO if no suitable firmware interface is present.
375 int device_property_read_string(struct device
*dev
, const char *propname
,
378 return fwnode_property_read_string(dev_fwnode(dev
), propname
, val
);
380 EXPORT_SYMBOL_GPL(device_property_read_string
);
383 * device_property_match_string - find a string in an array and return index
384 * @dev: Device to get the property of
385 * @propname: Name of the property holding the array
386 * @string: String to look for
388 * Find a given string in a string array and if it is found return the
391 * Return: %0 if the property was found (success),
392 * %-EINVAL if given arguments are not valid,
393 * %-ENODATA if the property does not have a value,
394 * %-EPROTO if the property is not an array of strings,
395 * %-ENXIO if no suitable firmware interface is present.
397 int device_property_match_string(struct device
*dev
, const char *propname
,
400 return fwnode_property_match_string(dev_fwnode(dev
), propname
, string
);
402 EXPORT_SYMBOL_GPL(device_property_match_string
);
404 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
405 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
406 : of_property_count_elems_of_size((node), (propname), sizeof(type))
408 #define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
409 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
410 : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
412 #define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
415 if (is_of_node(_fwnode_)) \
416 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
417 _type_, _val_, _nval_); \
418 else if (is_acpi_node(_fwnode_)) \
419 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
421 else if (is_pset_node(_fwnode_)) \
422 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
423 _type_, _val_, _nval_); \
429 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
432 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \
434 if (_ret_ == -EINVAL && !IS_ERR_OR_NULL(_fwnode_) && \
435 !IS_ERR_OR_NULL(_fwnode_->secondary)) \
436 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \
437 _proptype_, _val_, _nval_); \
442 * fwnode_property_read_u8_array - return a u8 array property of firmware node
443 * @fwnode: Firmware node to get the property of
444 * @propname: Name of the property
445 * @val: The values are stored here or %NULL to return the number of values
446 * @nval: Size of the @val array
448 * Read an array of u8 properties with @propname from @fwnode and stores them to
451 * Return: number of values if @val was %NULL,
452 * %0 if the property was found (success),
453 * %-EINVAL if given arguments are not valid,
454 * %-ENODATA if the property does not have a value,
455 * %-EPROTO if the property is not an array of numbers,
456 * %-EOVERFLOW if the size of the property is not as expected,
457 * %-ENXIO if no suitable firmware interface is present.
459 int fwnode_property_read_u8_array(struct fwnode_handle
*fwnode
,
460 const char *propname
, u8
*val
, size_t nval
)
462 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u8
, DEV_PROP_U8
,
465 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array
);
468 * fwnode_property_read_u16_array - return a u16 array property of firmware node
469 * @fwnode: Firmware node to get the property of
470 * @propname: Name of the property
471 * @val: The values are stored here or %NULL to return the number of values
472 * @nval: Size of the @val array
474 * Read an array of u16 properties with @propname from @fwnode and store them to
477 * Return: number of values if @val was %NULL,
478 * %0 if the property was found (success),
479 * %-EINVAL if given arguments are not valid,
480 * %-ENODATA if the property does not have a value,
481 * %-EPROTO if the property is not an array of numbers,
482 * %-EOVERFLOW if the size of the property is not as expected,
483 * %-ENXIO if no suitable firmware interface is present.
485 int fwnode_property_read_u16_array(struct fwnode_handle
*fwnode
,
486 const char *propname
, u16
*val
, size_t nval
)
488 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u16
, DEV_PROP_U16
,
491 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array
);
494 * fwnode_property_read_u32_array - return a u32 array property of firmware node
495 * @fwnode: Firmware node to get the property of
496 * @propname: Name of the property
497 * @val: The values are stored here or %NULL to return the number of values
498 * @nval: Size of the @val array
500 * Read an array of u32 properties with @propname from @fwnode store them to
503 * Return: number of values if @val was %NULL,
504 * %0 if the property was found (success),
505 * %-EINVAL if given arguments are not valid,
506 * %-ENODATA if the property does not have a value,
507 * %-EPROTO if the property is not an array of numbers,
508 * %-EOVERFLOW if the size of the property is not as expected,
509 * %-ENXIO if no suitable firmware interface is present.
511 int fwnode_property_read_u32_array(struct fwnode_handle
*fwnode
,
512 const char *propname
, u32
*val
, size_t nval
)
514 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u32
, DEV_PROP_U32
,
517 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array
);
520 * fwnode_property_read_u64_array - return a u64 array property firmware node
521 * @fwnode: Firmware node to get the property of
522 * @propname: Name of the property
523 * @val: The values are stored here or %NULL to return the number of values
524 * @nval: Size of the @val array
526 * Read an array of u64 properties with @propname from @fwnode and store them to
529 * Return: number of values if @val was %NULL,
530 * %0 if the property was found (success),
531 * %-EINVAL if given arguments are not valid,
532 * %-ENODATA if the property does not have a value,
533 * %-EPROTO if the property is not an array of numbers,
534 * %-EOVERFLOW if the size of the property is not as expected,
535 * %-ENXIO if no suitable firmware interface is present.
537 int fwnode_property_read_u64_array(struct fwnode_handle
*fwnode
,
538 const char *propname
, u64
*val
, size_t nval
)
540 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u64
, DEV_PROP_U64
,
543 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array
);
545 static int __fwnode_property_read_string_array(struct fwnode_handle
*fwnode
,
546 const char *propname
,
547 const char **val
, size_t nval
)
549 if (is_of_node(fwnode
))
551 of_property_read_string_array(to_of_node(fwnode
),
552 propname
, val
, nval
) :
553 of_property_count_strings(to_of_node(fwnode
), propname
);
554 else if (is_acpi_node(fwnode
))
555 return acpi_node_prop_read(fwnode
, propname
, DEV_PROP_STRING
,
557 else if (is_pset_node(fwnode
))
559 pset_prop_read_string_array(to_pset_node(fwnode
),
560 propname
, val
, nval
) :
561 pset_prop_count_elems_of_size(to_pset_node(fwnode
),
563 sizeof(const char *));
567 static int __fwnode_property_read_string(struct fwnode_handle
*fwnode
,
568 const char *propname
, const char **val
)
570 if (is_of_node(fwnode
))
571 return of_property_read_string(to_of_node(fwnode
), propname
, val
);
572 else if (is_acpi_node(fwnode
))
573 return acpi_node_prop_read(fwnode
, propname
, DEV_PROP_STRING
,
575 else if (is_pset_node(fwnode
))
576 return pset_prop_read_string(to_pset_node(fwnode
), propname
, val
);
581 * fwnode_property_read_string_array - return string array property of a node
582 * @fwnode: Firmware node to get the property of
583 * @propname: Name of the property
584 * @val: The values are stored here or %NULL to return the number of values
585 * @nval: Size of the @val array
587 * Read an string list property @propname from the given firmware node and store
588 * them to @val if found.
590 * Return: number of values if @val was %NULL,
591 * %0 if the property was found (success),
592 * %-EINVAL if given arguments are not valid,
593 * %-ENODATA if the property does not have a value,
594 * %-EPROTO if the property is not an array of strings,
595 * %-EOVERFLOW if the size of the property is not as expected,
596 * %-ENXIO if no suitable firmware interface is present.
598 int fwnode_property_read_string_array(struct fwnode_handle
*fwnode
,
599 const char *propname
, const char **val
,
604 ret
= __fwnode_property_read_string_array(fwnode
, propname
, val
, nval
);
605 if (ret
== -EINVAL
&& !IS_ERR_OR_NULL(fwnode
) &&
606 !IS_ERR_OR_NULL(fwnode
->secondary
))
607 ret
= __fwnode_property_read_string_array(fwnode
->secondary
,
608 propname
, val
, nval
);
611 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array
);
614 * fwnode_property_read_string - return a string property of a firmware node
615 * @fwnode: Firmware node to get the property of
616 * @propname: Name of the property
617 * @val: The value is stored here
619 * Read property @propname from the given firmware node and store the value into
620 * @val if found. The value is checked to be a string.
622 * Return: %0 if the property was found (success),
623 * %-EINVAL if given arguments are not valid,
624 * %-ENODATA if the property does not have a value,
625 * %-EPROTO or %-EILSEQ if the property is not a string,
626 * %-ENXIO if no suitable firmware interface is present.
628 int fwnode_property_read_string(struct fwnode_handle
*fwnode
,
629 const char *propname
, const char **val
)
633 ret
= __fwnode_property_read_string(fwnode
, propname
, val
);
634 if (ret
== -EINVAL
&& !IS_ERR_OR_NULL(fwnode
) &&
635 !IS_ERR_OR_NULL(fwnode
->secondary
))
636 ret
= __fwnode_property_read_string(fwnode
->secondary
,
640 EXPORT_SYMBOL_GPL(fwnode_property_read_string
);
643 * fwnode_property_match_string - find a string in an array and return index
644 * @fwnode: Firmware node to get the property of
645 * @propname: Name of the property holding the array
646 * @string: String to look for
648 * Find a given string in a string array and if it is found return the
651 * Return: %0 if the property was found (success),
652 * %-EINVAL if given arguments are not valid,
653 * %-ENODATA if the property does not have a value,
654 * %-EPROTO if the property is not an array of strings,
655 * %-ENXIO if no suitable firmware interface is present.
657 int fwnode_property_match_string(struct fwnode_handle
*fwnode
,
658 const char *propname
, const char *string
)
663 nval
= fwnode_property_read_string_array(fwnode
, propname
, NULL
, 0);
670 values
= kcalloc(nval
, sizeof(*values
), GFP_KERNEL
);
674 ret
= fwnode_property_read_string_array(fwnode
, propname
, values
, nval
);
678 ret
= match_string(values
, nval
, string
);
685 EXPORT_SYMBOL_GPL(fwnode_property_match_string
);
688 * pset_free_set - releases memory allocated for copied property set
689 * @pset: Property set to release
691 * Function takes previously copied property set and releases all the
692 * memory allocated to it.
694 static void pset_free_set(struct property_set
*pset
)
696 const struct property_entry
*prop
;
702 for (prop
= pset
->properties
; prop
->name
; prop
++) {
703 if (prop
->is_array
) {
704 if (prop
->is_string
&& prop
->pointer
.str
) {
705 nval
= prop
->length
/ sizeof(const char *);
706 for (i
= 0; i
< nval
; i
++)
707 kfree(prop
->pointer
.str
[i
]);
709 kfree(prop
->pointer
.raw_data
);
710 } else if (prop
->is_string
) {
711 kfree(prop
->value
.str
);
716 kfree(pset
->properties
);
720 static int pset_copy_entry(struct property_entry
*dst
,
721 const struct property_entry
*src
)
726 dst
->name
= kstrdup(src
->name
, GFP_KERNEL
);
734 if (src
->is_string
) {
735 nval
= src
->length
/ sizeof(const char *);
736 dst
->pointer
.str
= kcalloc(nval
, sizeof(const char *),
738 if (!dst
->pointer
.str
)
741 d
= dst
->pointer
.str
;
742 s
= src
->pointer
.str
;
743 for (i
= 0; i
< nval
; i
++) {
744 d
[i
] = kstrdup(s
[i
], GFP_KERNEL
);
749 dst
->pointer
.raw_data
= kmemdup(src
->pointer
.raw_data
,
750 src
->length
, GFP_KERNEL
);
751 if (!dst
->pointer
.raw_data
)
754 } else if (src
->is_string
) {
755 dst
->value
.str
= kstrdup(src
->value
.str
, GFP_KERNEL
);
756 if (!dst
->value
.str
&& src
->value
.str
)
759 dst
->value
.raw_data
= src
->value
.raw_data
;
762 dst
->length
= src
->length
;
763 dst
->is_array
= src
->is_array
;
764 dst
->is_string
= src
->is_string
;
770 * pset_copy_set - copies property set
771 * @pset: Property set to copy
773 * This function takes a deep copy of the given property set and returns
774 * pointer to the copy. Call device_free_property_set() to free resources
775 * allocated in this function.
777 * Return: Pointer to the new property set or error pointer.
779 static struct property_set
*pset_copy_set(const struct property_set
*pset
)
781 const struct property_entry
*entry
;
782 struct property_set
*p
;
785 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
787 return ERR_PTR(-ENOMEM
);
789 while (pset
->properties
[n
].name
)
792 p
->properties
= kcalloc(n
+ 1, sizeof(*entry
), GFP_KERNEL
);
793 if (!p
->properties
) {
795 return ERR_PTR(-ENOMEM
);
798 for (i
= 0; i
< n
; i
++) {
799 int ret
= pset_copy_entry(&p
->properties
[i
],
800 &pset
->properties
[i
]);
811 * device_remove_properties - Remove properties from a device object.
812 * @dev: Device whose properties to remove.
814 * The function removes properties previously associated to the device
815 * secondary firmware node with device_add_properties(). Memory allocated
816 * to the properties will also be released.
818 void device_remove_properties(struct device
*dev
)
820 struct fwnode_handle
*fwnode
;
821 struct property_set
*pset
;
823 fwnode
= dev_fwnode(dev
);
827 * Pick either primary or secondary node depending which one holds
828 * the pset. If there is no real firmware node (ACPI/DT) primary
829 * will hold the pset.
831 pset
= to_pset_node(fwnode
);
833 set_primary_fwnode(dev
, NULL
);
835 pset
= to_pset_node(fwnode
->secondary
);
836 if (pset
&& dev
== pset
->dev
)
837 set_secondary_fwnode(dev
, NULL
);
839 if (pset
&& dev
== pset
->dev
)
842 EXPORT_SYMBOL_GPL(device_remove_properties
);
845 * device_add_properties - Add a collection of properties to a device object.
846 * @dev: Device to add properties to.
847 * @properties: Collection of properties to add.
849 * Associate a collection of device properties represented by @properties with
850 * @dev as its secondary firmware node. The function takes a copy of
853 int device_add_properties(struct device
*dev
, struct property_entry
*properties
)
855 struct property_set
*p
, pset
;
860 pset
.properties
= properties
;
862 p
= pset_copy_set(&pset
);
866 p
->fwnode
.type
= FWNODE_PDATA
;
867 set_secondary_fwnode(dev
, &p
->fwnode
);
871 EXPORT_SYMBOL_GPL(device_add_properties
);
874 * device_get_next_child_node - Return the next child node handle for a device
875 * @dev: Device to find the next child node for.
876 * @child: Handle to one of the device's child nodes or a null handle.
878 struct fwnode_handle
*device_get_next_child_node(struct device
*dev
,
879 struct fwnode_handle
*child
)
881 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
882 struct device_node
*node
;
884 node
= of_get_next_available_child(dev
->of_node
, to_of_node(child
));
886 return &node
->fwnode
;
887 } else if (IS_ENABLED(CONFIG_ACPI
)) {
888 return acpi_get_next_subnode(dev
, child
);
892 EXPORT_SYMBOL_GPL(device_get_next_child_node
);
895 * device_get_named_child_node - Return first matching named child node handle
896 * @dev: Device to find the named child node for.
897 * @childname: String to match child node name against.
899 struct fwnode_handle
*device_get_named_child_node(struct device
*dev
,
900 const char *childname
)
902 struct fwnode_handle
*child
;
905 * Find first matching named child node of this device.
906 * For ACPI this will be a data only sub-node.
908 device_for_each_child_node(dev
, child
) {
909 if (is_of_node(child
)) {
910 if (!of_node_cmp(to_of_node(child
)->name
, childname
))
912 } else if (is_acpi_data_node(child
)) {
913 if (acpi_data_node_match(child
, childname
))
920 EXPORT_SYMBOL_GPL(device_get_named_child_node
);
923 * fwnode_handle_put - Drop reference to a device node
924 * @fwnode: Pointer to the device node to drop the reference to.
926 * This has to be used when terminating device_for_each_child_node() iteration
927 * with break or return to prevent stale device node references from being left
930 void fwnode_handle_put(struct fwnode_handle
*fwnode
)
932 if (is_of_node(fwnode
))
933 of_node_put(to_of_node(fwnode
));
935 EXPORT_SYMBOL_GPL(fwnode_handle_put
);
938 * device_get_child_node_count - return the number of child nodes for device
939 * @dev: Device to cound the child nodes for
941 unsigned int device_get_child_node_count(struct device
*dev
)
943 struct fwnode_handle
*child
;
944 unsigned int count
= 0;
946 device_for_each_child_node(dev
, child
)
951 EXPORT_SYMBOL_GPL(device_get_child_node_count
);
953 bool device_dma_supported(struct device
*dev
)
955 /* For DT, this is always supported.
956 * For ACPI, this depends on CCA, which
957 * is determined by the acpi_dma_supported().
959 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
)
962 return acpi_dma_supported(ACPI_COMPANION(dev
));
964 EXPORT_SYMBOL_GPL(device_dma_supported
);
966 enum dev_dma_attr
device_get_dma_attr(struct device
*dev
)
968 enum dev_dma_attr attr
= DEV_DMA_NOT_SUPPORTED
;
970 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
971 if (of_dma_is_coherent(dev
->of_node
))
972 attr
= DEV_DMA_COHERENT
;
974 attr
= DEV_DMA_NON_COHERENT
;
976 attr
= acpi_get_dma_attr(ACPI_COMPANION(dev
));
980 EXPORT_SYMBOL_GPL(device_get_dma_attr
);
983 * device_get_phy_mode - Get phy mode for given device
984 * @dev: Pointer to the given device
986 * The function gets phy interface string from property 'phy-mode' or
987 * 'phy-connection-type', and return its index in phy_modes table, or errno in
990 int device_get_phy_mode(struct device
*dev
)
995 err
= device_property_read_string(dev
, "phy-mode", &pm
);
997 err
= device_property_read_string(dev
,
998 "phy-connection-type", &pm
);
1002 for (i
= 0; i
< PHY_INTERFACE_MODE_MAX
; i
++)
1003 if (!strcasecmp(pm
, phy_modes(i
)))
1008 EXPORT_SYMBOL_GPL(device_get_phy_mode
);
1010 static void *device_get_mac_addr(struct device
*dev
,
1011 const char *name
, char *addr
,
1014 int ret
= device_property_read_u8_array(dev
, name
, addr
, alen
);
1016 if (ret
== 0 && alen
== ETH_ALEN
&& is_valid_ether_addr(addr
))
1022 * device_get_mac_address - Get the MAC for a given device
1023 * @dev: Pointer to the device
1024 * @addr: Address of buffer to store the MAC in
1025 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1027 * Search the firmware node for the best MAC address to use. 'mac-address' is
1028 * checked first, because that is supposed to contain to "most recent" MAC
1029 * address. If that isn't set, then 'local-mac-address' is checked next,
1030 * because that is the default address. If that isn't set, then the obsolete
1031 * 'address' is checked, just in case we're using an old device tree.
1033 * Note that the 'address' property is supposed to contain a virtual address of
1034 * the register set, but some DTS files have redefined that property to be the
1037 * All-zero MAC addresses are rejected, because those could be properties that
1038 * exist in the firmware tables, but were not updated by the firmware. For
1039 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1040 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1041 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1042 * exists but is all zeros.
1044 void *device_get_mac_address(struct device
*dev
, char *addr
, int alen
)
1048 res
= device_get_mac_addr(dev
, "mac-address", addr
, alen
);
1052 res
= device_get_mac_addr(dev
, "local-mac-address", addr
, alen
);
1056 return device_get_mac_addr(dev
, "address", addr
, alen
);
1058 EXPORT_SYMBOL(device_get_mac_address
);