ipv6: flowlabel: fl6_sock_lookup() must use atomic_inc_not_zero
[linux/fpc-iii.git] / drivers / base / property.c
blob7b313b567f4c3b71c81eb48ee740eac79d636511
1 /*
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>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/property.h>
19 #include <linux/etherdevice.h>
20 #include <linux/phy.h>
22 struct property_set {
23 struct device *dev;
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,
40 const char *name)
42 struct property_entry *prop;
44 if (!pset || !pset->properties)
45 return NULL;
47 for (prop = pset->properties; prop->name; prop++)
48 if (!strcmp(name, prop->name))
49 return prop;
51 return NULL;
54 static void *pset_prop_find(struct property_set *pset, const char *propname,
55 size_t length)
57 struct property_entry *prop;
58 void *pointer;
60 prop = pset_prop_get(pset, propname);
61 if (!prop)
62 return ERR_PTR(-EINVAL);
63 if (prop->is_array)
64 pointer = prop->pointer.raw_data;
65 else
66 pointer = &prop->value.raw_data;
67 if (!pointer)
68 return ERR_PTR(-ENODATA);
69 if (length > prop->length)
70 return ERR_PTR(-EOVERFLOW);
71 return pointer;
74 static int pset_prop_read_u8_array(struct property_set *pset,
75 const char *propname,
76 u8 *values, size_t nval)
78 void *pointer;
79 size_t length = nval * sizeof(*values);
81 pointer = pset_prop_find(pset, propname, length);
82 if (IS_ERR(pointer))
83 return PTR_ERR(pointer);
85 memcpy(values, pointer, length);
86 return 0;
89 static int pset_prop_read_u16_array(struct property_set *pset,
90 const char *propname,
91 u16 *values, size_t nval)
93 void *pointer;
94 size_t length = nval * sizeof(*values);
96 pointer = pset_prop_find(pset, propname, length);
97 if (IS_ERR(pointer))
98 return PTR_ERR(pointer);
100 memcpy(values, pointer, length);
101 return 0;
104 static int pset_prop_read_u32_array(struct property_set *pset,
105 const char *propname,
106 u32 *values, size_t nval)
108 void *pointer;
109 size_t length = nval * sizeof(*values);
111 pointer = pset_prop_find(pset, propname, length);
112 if (IS_ERR(pointer))
113 return PTR_ERR(pointer);
115 memcpy(values, pointer, length);
116 return 0;
119 static int pset_prop_read_u64_array(struct property_set *pset,
120 const char *propname,
121 u64 *values, size_t nval)
123 void *pointer;
124 size_t length = nval * sizeof(*values);
126 pointer = pset_prop_find(pset, propname, length);
127 if (IS_ERR(pointer))
128 return PTR_ERR(pointer);
130 memcpy(values, pointer, length);
131 return 0;
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);
140 if (!prop)
141 return -EINVAL;
143 return prop->length / length;
146 static int pset_prop_read_string_array(struct property_set *pset,
147 const char *propname,
148 const char **strings, size_t nval)
150 void *pointer;
151 size_t length = nval * sizeof(*strings);
153 pointer = pset_prop_find(pset, propname, length);
154 if (IS_ERR(pointer))
155 return PTR_ERR(pointer);
157 memcpy(strings, pointer, length);
158 return 0;
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);
168 if (!prop)
169 return -EINVAL;
170 if (!prop->is_string)
171 return -EILSEQ;
172 if (prop->is_array) {
173 pointer = prop->pointer.str;
174 if (!pointer)
175 return -ENODATA;
176 } else {
177 pointer = &prop->value.str;
178 if (*pointer && strnlen(*pointer, prop->length) >= prop->length)
179 return -EILSEQ;
182 *strings = *pointer;
183 return 0;
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);
215 return false;
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)
225 bool ret;
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);
231 return ret;
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,
376 const char **val)
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
389 * index back.
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,
398 const char *string)
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_) \
413 ({ \
414 int _ret_; \
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_, \
420 _val_, _nval_); \
421 else if (is_pset_node(_fwnode_)) \
422 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
423 _type_, _val_, _nval_); \
424 else \
425 _ret_ = -ENXIO; \
426 _ret_; \
429 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
430 ({ \
431 int _ret_; \
432 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \
433 _val_, _nval_); \
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_); \
438 _ret_; \
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
449 * @val if found.
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,
463 val, nval);
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
475 * @val if found.
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,
489 val, nval);
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
501 * @val if found.
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,
515 val, nval);
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
527 * @val if found.
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,
541 val, nval);
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))
550 return val ?
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,
556 val, nval);
557 else if (is_pset_node(fwnode))
558 return val ?
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),
562 propname,
563 sizeof(const char *));
564 return -ENXIO;
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,
574 val, 1);
575 else if (is_pset_node(fwnode))
576 return pset_prop_read_string(to_pset_node(fwnode), propname, val);
577 return -ENXIO;
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,
600 size_t nval)
602 int ret;
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);
609 return ret;
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)
631 int ret;
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,
637 propname, val);
638 return ret;
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
649 * index back.
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)
660 const char **values;
661 int nval, ret;
663 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
664 if (nval < 0)
665 return nval;
667 if (nval == 0)
668 return -ENODATA;
670 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
671 if (!values)
672 return -ENOMEM;
674 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
675 if (ret < 0)
676 goto out;
678 ret = match_string(values, nval, string);
679 if (ret < 0)
680 ret = -ENODATA;
681 out:
682 kfree(values);
683 return ret;
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;
697 size_t i, nval;
699 if (!pset)
700 return;
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);
713 kfree(prop->name);
716 kfree(pset->properties);
717 kfree(pset);
720 static int pset_copy_entry(struct property_entry *dst,
721 const struct property_entry *src)
723 const char **d, **s;
724 size_t i, nval;
726 dst->name = kstrdup(src->name, GFP_KERNEL);
727 if (!dst->name)
728 return -ENOMEM;
730 if (src->is_array) {
731 if (!src->length)
732 return -ENODATA;
734 if (src->is_string) {
735 nval = src->length / sizeof(const char *);
736 dst->pointer.str = kcalloc(nval, sizeof(const char *),
737 GFP_KERNEL);
738 if (!dst->pointer.str)
739 return -ENOMEM;
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);
745 if (!d[i] && s[i])
746 return -ENOMEM;
748 } else {
749 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
750 src->length, GFP_KERNEL);
751 if (!dst->pointer.raw_data)
752 return -ENOMEM;
754 } else if (src->is_string) {
755 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
756 if (!dst->value.str && src->value.str)
757 return -ENOMEM;
758 } else {
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;
766 return 0;
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;
783 size_t i, n = 0;
785 p = kzalloc(sizeof(*p), GFP_KERNEL);
786 if (!p)
787 return ERR_PTR(-ENOMEM);
789 while (pset->properties[n].name)
790 n++;
792 p->properties = kcalloc(n + 1, sizeof(*entry), GFP_KERNEL);
793 if (!p->properties) {
794 kfree(p);
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]);
801 if (ret) {
802 pset_free_set(p);
803 return ERR_PTR(ret);
807 return p;
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);
824 if (!fwnode)
825 return;
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);
832 if (pset) {
833 set_primary_fwnode(dev, NULL);
834 } else {
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)
840 pset_free_set(pset);
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
851 * @properties.
853 int device_add_properties(struct device *dev, struct property_entry *properties)
855 struct property_set *p, pset;
857 if (!properties)
858 return -EINVAL;
860 pset.properties = properties;
862 p = pset_copy_set(&pset);
863 if (IS_ERR(p))
864 return PTR_ERR(p);
866 p->fwnode.type = FWNODE_PDATA;
867 set_secondary_fwnode(dev, &p->fwnode);
868 p->dev = dev;
869 return 0;
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));
885 if (node)
886 return &node->fwnode;
887 } else if (IS_ENABLED(CONFIG_ACPI)) {
888 return acpi_get_next_subnode(dev, child);
890 return NULL;
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))
911 return child;
912 } else if (is_acpi_data_node(child)) {
913 if (acpi_data_node_match(child, childname))
914 return child;
918 return NULL;
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
928 * behind.
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)
947 count++;
949 return count;
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)
960 return true;
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;
973 else
974 attr = DEV_DMA_NON_COHERENT;
975 } else
976 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
978 return attr;
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
988 * error case.
990 int device_get_phy_mode(struct device *dev)
992 const char *pm;
993 int err, i;
995 err = device_property_read_string(dev, "phy-mode", &pm);
996 if (err < 0)
997 err = device_property_read_string(dev,
998 "phy-connection-type", &pm);
999 if (err < 0)
1000 return err;
1002 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1003 if (!strcasecmp(pm, phy_modes(i)))
1004 return i;
1006 return -ENODEV;
1008 EXPORT_SYMBOL_GPL(device_get_phy_mode);
1010 static void *device_get_mac_addr(struct device *dev,
1011 const char *name, char *addr,
1012 int alen)
1014 int ret = device_property_read_u8_array(dev, name, addr, alen);
1016 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
1017 return addr;
1018 return NULL;
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
1035 * MAC address.
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)
1046 char *res;
1048 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1049 if (res)
1050 return res;
1052 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1053 if (res)
1054 return res;
1056 return device_get_mac_addr(dev, "address", addr, alen);
1058 EXPORT_SYMBOL(device_get_mac_address);