]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/base/property.c
device property: Add support for remote endpoints
[karo-tx-linux.git] / drivers / base / property.c
1 /*
2  * property.c - Unified device property interface.
3  *
4  * Copyright (C) 2014, Intel Corporation
5  * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6  *          Mika Westerberg <mika.westerberg@linux.intel.com>
7  *
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.
11  */
12
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/of_graph.h>
19 #include <linux/property.h>
20 #include <linux/etherdevice.h>
21 #include <linux/phy.h>
22
23 struct property_set {
24         struct fwnode_handle fwnode;
25         const struct property_entry *properties;
26 };
27
28 static inline bool is_pset_node(struct fwnode_handle *fwnode)
29 {
30         return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA;
31 }
32
33 static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
34 {
35         return is_pset_node(fwnode) ?
36                 container_of(fwnode, struct property_set, fwnode) : NULL;
37 }
38
39 static const struct property_entry *pset_prop_get(struct property_set *pset,
40                                                   const char *name)
41 {
42         const struct property_entry *prop;
43
44         if (!pset || !pset->properties)
45                 return NULL;
46
47         for (prop = pset->properties; prop->name; prop++)
48                 if (!strcmp(name, prop->name))
49                         return prop;
50
51         return NULL;
52 }
53
54 static const void *pset_prop_find(struct property_set *pset,
55                                   const char *propname, size_t length)
56 {
57         const struct property_entry *prop;
58         const void *pointer;
59
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;
72 }
73
74 static int pset_prop_read_u8_array(struct property_set *pset,
75                                    const char *propname,
76                                    u8 *values, size_t nval)
77 {
78         const void *pointer;
79         size_t length = nval * sizeof(*values);
80
81         pointer = pset_prop_find(pset, propname, length);
82         if (IS_ERR(pointer))
83                 return PTR_ERR(pointer);
84
85         memcpy(values, pointer, length);
86         return 0;
87 }
88
89 static int pset_prop_read_u16_array(struct property_set *pset,
90                                     const char *propname,
91                                     u16 *values, size_t nval)
92 {
93         const void *pointer;
94         size_t length = nval * sizeof(*values);
95
96         pointer = pset_prop_find(pset, propname, length);
97         if (IS_ERR(pointer))
98                 return PTR_ERR(pointer);
99
100         memcpy(values, pointer, length);
101         return 0;
102 }
103
104 static int pset_prop_read_u32_array(struct property_set *pset,
105                                     const char *propname,
106                                     u32 *values, size_t nval)
107 {
108         const void *pointer;
109         size_t length = nval * sizeof(*values);
110
111         pointer = pset_prop_find(pset, propname, length);
112         if (IS_ERR(pointer))
113                 return PTR_ERR(pointer);
114
115         memcpy(values, pointer, length);
116         return 0;
117 }
118
119 static int pset_prop_read_u64_array(struct property_set *pset,
120                                     const char *propname,
121                                     u64 *values, size_t nval)
122 {
123         const void *pointer;
124         size_t length = nval * sizeof(*values);
125
126         pointer = pset_prop_find(pset, propname, length);
127         if (IS_ERR(pointer))
128                 return PTR_ERR(pointer);
129
130         memcpy(values, pointer, length);
131         return 0;
132 }
133
134 static int pset_prop_count_elems_of_size(struct property_set *pset,
135                                          const char *propname, size_t length)
136 {
137         const struct property_entry *prop;
138
139         prop = pset_prop_get(pset, propname);
140         if (!prop)
141                 return -EINVAL;
142
143         return prop->length / length;
144 }
145
146 static int pset_prop_read_string_array(struct property_set *pset,
147                                        const char *propname,
148                                        const char **strings, size_t nval)
149 {
150         const void *pointer;
151         size_t length = nval * sizeof(*strings);
152
153         pointer = pset_prop_find(pset, propname, length);
154         if (IS_ERR(pointer))
155                 return PTR_ERR(pointer);
156
157         memcpy(strings, pointer, length);
158         return 0;
159 }
160
161 static int pset_prop_read_string(struct property_set *pset,
162                                  const char *propname, const char **strings)
163 {
164         const struct property_entry *prop;
165         const char * const *pointer;
166
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;
180         }
181
182         *strings = *pointer;
183         return 0;
184 }
185
186 static inline struct fwnode_handle *dev_fwnode(struct device *dev)
187 {
188         return IS_ENABLED(CONFIG_OF) && dev->of_node ?
189                 &dev->of_node->fwnode : dev->fwnode;
190 }
191
192 /**
193  * device_property_present - check if a property of a device is present
194  * @dev: Device whose property is being checked
195  * @propname: Name of the property
196  *
197  * Check if property @propname is present in the device firmware description.
198  */
199 bool device_property_present(struct device *dev, const char *propname)
200 {
201         return fwnode_property_present(dev_fwnode(dev), propname);
202 }
203 EXPORT_SYMBOL_GPL(device_property_present);
204
205 static bool __fwnode_property_present(struct fwnode_handle *fwnode,
206                                       const char *propname)
207 {
208         if (is_of_node(fwnode))
209                 return of_property_read_bool(to_of_node(fwnode), propname);
210         else if (is_acpi_node(fwnode))
211                 return !acpi_node_prop_get(fwnode, propname, NULL);
212         else if (is_pset_node(fwnode))
213                 return !!pset_prop_get(to_pset_node(fwnode), propname);
214         return false;
215 }
216
217 /**
218  * fwnode_property_present - check if a property of a firmware node is present
219  * @fwnode: Firmware node whose property to check
220  * @propname: Name of the property
221  */
222 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
223 {
224         bool ret;
225
226         ret = __fwnode_property_present(fwnode, propname);
227         if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
228             !IS_ERR_OR_NULL(fwnode->secondary))
229                 ret = __fwnode_property_present(fwnode->secondary, propname);
230         return ret;
231 }
232 EXPORT_SYMBOL_GPL(fwnode_property_present);
233
234 /**
235  * device_property_read_u8_array - return a u8 array property of a device
236  * @dev: Device to get the property of
237  * @propname: Name of the property
238  * @val: The values are stored here or %NULL to return the number of values
239  * @nval: Size of the @val array
240  *
241  * Function reads an array of u8 properties with @propname from the device
242  * firmware description and stores them to @val if found.
243  *
244  * Return: number of values if @val was %NULL,
245  *         %0 if the property was found (success),
246  *         %-EINVAL if given arguments are not valid,
247  *         %-ENODATA if the property does not have a value,
248  *         %-EPROTO if the property is not an array of numbers,
249  *         %-EOVERFLOW if the size of the property is not as expected.
250  *         %-ENXIO if no suitable firmware interface is present.
251  */
252 int device_property_read_u8_array(struct device *dev, const char *propname,
253                                   u8 *val, size_t nval)
254 {
255         return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
256 }
257 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
258
259 /**
260  * device_property_read_u16_array - return a u16 array property of a device
261  * @dev: Device to get the property of
262  * @propname: Name of the property
263  * @val: The values are stored here or %NULL to return the number of values
264  * @nval: Size of the @val array
265  *
266  * Function reads an array of u16 properties with @propname from the device
267  * firmware description and stores them to @val if found.
268  *
269  * Return: number of values if @val was %NULL,
270  *         %0 if the property was found (success),
271  *         %-EINVAL if given arguments are not valid,
272  *         %-ENODATA if the property does not have a value,
273  *         %-EPROTO if the property is not an array of numbers,
274  *         %-EOVERFLOW if the size of the property is not as expected.
275  *         %-ENXIO if no suitable firmware interface is present.
276  */
277 int device_property_read_u16_array(struct device *dev, const char *propname,
278                                    u16 *val, size_t nval)
279 {
280         return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
281 }
282 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
283
284 /**
285  * device_property_read_u32_array - return a u32 array property of a device
286  * @dev: Device to get the property of
287  * @propname: Name of the property
288  * @val: The values are stored here or %NULL to return the number of values
289  * @nval: Size of the @val array
290  *
291  * Function reads an array of u32 properties with @propname from the device
292  * firmware description and stores them to @val if found.
293  *
294  * Return: number of values if @val was %NULL,
295  *         %0 if the property was found (success),
296  *         %-EINVAL if given arguments are not valid,
297  *         %-ENODATA if the property does not have a value,
298  *         %-EPROTO if the property is not an array of numbers,
299  *         %-EOVERFLOW if the size of the property is not as expected.
300  *         %-ENXIO if no suitable firmware interface is present.
301  */
302 int device_property_read_u32_array(struct device *dev, const char *propname,
303                                    u32 *val, size_t nval)
304 {
305         return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
306 }
307 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
308
309 /**
310  * device_property_read_u64_array - return a u64 array property of a device
311  * @dev: Device to get the property of
312  * @propname: Name of the property
313  * @val: The values are stored here or %NULL to return the number of values
314  * @nval: Size of the @val array
315  *
316  * Function reads an array of u64 properties with @propname from the device
317  * firmware description and stores them to @val if found.
318  *
319  * Return: number of values if @val was %NULL,
320  *         %0 if the property was found (success),
321  *         %-EINVAL if given arguments are not valid,
322  *         %-ENODATA if the property does not have a value,
323  *         %-EPROTO if the property is not an array of numbers,
324  *         %-EOVERFLOW if the size of the property is not as expected.
325  *         %-ENXIO if no suitable firmware interface is present.
326  */
327 int device_property_read_u64_array(struct device *dev, const char *propname,
328                                    u64 *val, size_t nval)
329 {
330         return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
331 }
332 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
333
334 /**
335  * device_property_read_string_array - return a string array property of device
336  * @dev: Device to get the property of
337  * @propname: Name of the property
338  * @val: The values are stored here or %NULL to return the number of values
339  * @nval: Size of the @val array
340  *
341  * Function reads an array of string properties with @propname from the device
342  * firmware description and stores them to @val if found.
343  *
344  * Return: number of values if @val was %NULL,
345  *         %0 if the property was found (success),
346  *         %-EINVAL if given arguments are not valid,
347  *         %-ENODATA if the property does not have a value,
348  *         %-EPROTO or %-EILSEQ if the property is not an array of strings,
349  *         %-EOVERFLOW if the size of the property is not as expected.
350  *         %-ENXIO if no suitable firmware interface is present.
351  */
352 int device_property_read_string_array(struct device *dev, const char *propname,
353                                       const char **val, size_t nval)
354 {
355         return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
356 }
357 EXPORT_SYMBOL_GPL(device_property_read_string_array);
358
359 /**
360  * device_property_read_string - return a string property of a device
361  * @dev: Device to get the property of
362  * @propname: Name of the property
363  * @val: The value is stored here
364  *
365  * Function reads property @propname from the device firmware description and
366  * stores the value into @val if found. The value is checked to be a string.
367  *
368  * Return: %0 if the property was found (success),
369  *         %-EINVAL if given arguments are not valid,
370  *         %-ENODATA if the property does not have a value,
371  *         %-EPROTO or %-EILSEQ if the property type is not a string.
372  *         %-ENXIO if no suitable firmware interface is present.
373  */
374 int device_property_read_string(struct device *dev, const char *propname,
375                                 const char **val)
376 {
377         return fwnode_property_read_string(dev_fwnode(dev), propname, val);
378 }
379 EXPORT_SYMBOL_GPL(device_property_read_string);
380
381 /**
382  * device_property_match_string - find a string in an array and return index
383  * @dev: Device to get the property of
384  * @propname: Name of the property holding the array
385  * @string: String to look for
386  *
387  * Find a given string in a string array and if it is found return the
388  * index back.
389  *
390  * Return: %0 if the property was found (success),
391  *         %-EINVAL if given arguments are not valid,
392  *         %-ENODATA if the property does not have a value,
393  *         %-EPROTO if the property is not an array of strings,
394  *         %-ENXIO if no suitable firmware interface is present.
395  */
396 int device_property_match_string(struct device *dev, const char *propname,
397                                  const char *string)
398 {
399         return fwnode_property_match_string(dev_fwnode(dev), propname, string);
400 }
401 EXPORT_SYMBOL_GPL(device_property_match_string);
402
403 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval)                         \
404         (val) ? of_property_read_##type##_array((node), (propname), (val), (nval))      \
405               : of_property_count_elems_of_size((node), (propname), sizeof(type))
406
407 #define PSET_PROP_READ_ARRAY(node, propname, type, val, nval)                           \
408         (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval))        \
409               : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
410
411 #define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_)       \
412 ({                                                                                      \
413         int _ret_;                                                                      \
414         if (is_of_node(_fwnode_))                                                       \
415                 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_,        \
416                                                _type_, _val_, _nval_);                  \
417         else if (is_acpi_node(_fwnode_))                                                \
418                 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_,           \
419                                             _val_, _nval_);                             \
420         else if (is_pset_node(_fwnode_))                                                \
421                 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_,        \
422                                              _type_, _val_, _nval_);                    \
423         else                                                                            \
424                 _ret_ = -ENXIO;                                                         \
425         _ret_;                                                                          \
426 })
427
428 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
429 ({                                                                                      \
430         int _ret_;                                                                      \
431         _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_,              \
432                                  _val_, _nval_);                                        \
433         if (_ret_ == -EINVAL && !IS_ERR_OR_NULL(_fwnode_) &&                            \
434             !IS_ERR_OR_NULL(_fwnode_->secondary))                                       \
435                 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_,       \
436                                 _proptype_, _val_, _nval_);                             \
437         _ret_;                                                                          \
438 })
439
440 /**
441  * fwnode_property_read_u8_array - return a u8 array property of firmware node
442  * @fwnode: Firmware node to get the property of
443  * @propname: Name of the property
444  * @val: The values are stored here or %NULL to return the number of values
445  * @nval: Size of the @val array
446  *
447  * Read an array of u8 properties with @propname from @fwnode and stores them to
448  * @val if found.
449  *
450  * Return: number of values if @val was %NULL,
451  *         %0 if the property was found (success),
452  *         %-EINVAL if given arguments are not valid,
453  *         %-ENODATA if the property does not have a value,
454  *         %-EPROTO if the property is not an array of numbers,
455  *         %-EOVERFLOW if the size of the property is not as expected,
456  *         %-ENXIO if no suitable firmware interface is present.
457  */
458 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
459                                   const char *propname, u8 *val, size_t nval)
460 {
461         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
462                                       val, nval);
463 }
464 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
465
466 /**
467  * fwnode_property_read_u16_array - return a u16 array property of firmware node
468  * @fwnode: Firmware node to get the property of
469  * @propname: Name of the property
470  * @val: The values are stored here or %NULL to return the number of values
471  * @nval: Size of the @val array
472  *
473  * Read an array of u16 properties with @propname from @fwnode and store them to
474  * @val if found.
475  *
476  * Return: number of values if @val was %NULL,
477  *         %0 if the property was found (success),
478  *         %-EINVAL if given arguments are not valid,
479  *         %-ENODATA if the property does not have a value,
480  *         %-EPROTO if the property is not an array of numbers,
481  *         %-EOVERFLOW if the size of the property is not as expected,
482  *         %-ENXIO if no suitable firmware interface is present.
483  */
484 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
485                                    const char *propname, u16 *val, size_t nval)
486 {
487         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
488                                       val, nval);
489 }
490 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
491
492 /**
493  * fwnode_property_read_u32_array - return a u32 array property of firmware node
494  * @fwnode: Firmware node to get the property of
495  * @propname: Name of the property
496  * @val: The values are stored here or %NULL to return the number of values
497  * @nval: Size of the @val array
498  *
499  * Read an array of u32 properties with @propname from @fwnode store them to
500  * @val if found.
501  *
502  * Return: number of values if @val was %NULL,
503  *         %0 if the property was found (success),
504  *         %-EINVAL if given arguments are not valid,
505  *         %-ENODATA if the property does not have a value,
506  *         %-EPROTO if the property is not an array of numbers,
507  *         %-EOVERFLOW if the size of the property is not as expected,
508  *         %-ENXIO if no suitable firmware interface is present.
509  */
510 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
511                                    const char *propname, u32 *val, size_t nval)
512 {
513         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
514                                       val, nval);
515 }
516 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
517
518 /**
519  * fwnode_property_read_u64_array - return a u64 array property firmware node
520  * @fwnode: Firmware node to get the property of
521  * @propname: Name of the property
522  * @val: The values are stored here or %NULL to return the number of values
523  * @nval: Size of the @val array
524  *
525  * Read an array of u64 properties with @propname from @fwnode and store them to
526  * @val if found.
527  *
528  * Return: number of values if @val was %NULL,
529  *         %0 if the property was found (success),
530  *         %-EINVAL if given arguments are not valid,
531  *         %-ENODATA if the property does not have a value,
532  *         %-EPROTO if the property is not an array of numbers,
533  *         %-EOVERFLOW if the size of the property is not as expected,
534  *         %-ENXIO if no suitable firmware interface is present.
535  */
536 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
537                                    const char *propname, u64 *val, size_t nval)
538 {
539         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
540                                       val, nval);
541 }
542 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
543
544 static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode,
545                                                const char *propname,
546                                                const char **val, size_t nval)
547 {
548         if (is_of_node(fwnode))
549                 return val ?
550                         of_property_read_string_array(to_of_node(fwnode),
551                                                       propname, val, nval) :
552                         of_property_count_strings(to_of_node(fwnode), propname);
553         else if (is_acpi_node(fwnode))
554                 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
555                                            val, nval);
556         else if (is_pset_node(fwnode))
557                 return val ?
558                         pset_prop_read_string_array(to_pset_node(fwnode),
559                                                     propname, val, nval) :
560                         pset_prop_count_elems_of_size(to_pset_node(fwnode),
561                                                       propname,
562                                                       sizeof(const char *));
563         return -ENXIO;
564 }
565
566 static int __fwnode_property_read_string(struct fwnode_handle *fwnode,
567                                          const char *propname, const char **val)
568 {
569         if (is_of_node(fwnode))
570                 return of_property_read_string(to_of_node(fwnode), propname, val);
571         else if (is_acpi_node(fwnode))
572                 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
573                                            val, 1);
574         else if (is_pset_node(fwnode))
575                 return pset_prop_read_string(to_pset_node(fwnode), propname, val);
576         return -ENXIO;
577 }
578
579 /**
580  * fwnode_property_read_string_array - return string array property of a node
581  * @fwnode: Firmware node to get the property of
582  * @propname: Name of the property
583  * @val: The values are stored here or %NULL to return the number of values
584  * @nval: Size of the @val array
585  *
586  * Read an string list property @propname from the given firmware node and store
587  * them to @val if found.
588  *
589  * Return: number of values if @val was %NULL,
590  *         %0 if the property was found (success),
591  *         %-EINVAL if given arguments are not valid,
592  *         %-ENODATA if the property does not have a value,
593  *         %-EPROTO if the property is not an array of strings,
594  *         %-EOVERFLOW if the size of the property is not as expected,
595  *         %-ENXIO if no suitable firmware interface is present.
596  */
597 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
598                                       const char *propname, const char **val,
599                                       size_t nval)
600 {
601         int ret;
602
603         ret = __fwnode_property_read_string_array(fwnode, propname, val, nval);
604         if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
605             !IS_ERR_OR_NULL(fwnode->secondary))
606                 ret = __fwnode_property_read_string_array(fwnode->secondary,
607                                                           propname, val, nval);
608         return ret;
609 }
610 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
611
612 /**
613  * fwnode_property_read_string - return a string property of a firmware node
614  * @fwnode: Firmware node to get the property of
615  * @propname: Name of the property
616  * @val: The value is stored here
617  *
618  * Read property @propname from the given firmware node and store the value into
619  * @val if found.  The value is checked to be a string.
620  *
621  * Return: %0 if the property was found (success),
622  *         %-EINVAL if given arguments are not valid,
623  *         %-ENODATA if the property does not have a value,
624  *         %-EPROTO or %-EILSEQ if the property is not a string,
625  *         %-ENXIO if no suitable firmware interface is present.
626  */
627 int fwnode_property_read_string(struct fwnode_handle *fwnode,
628                                 const char *propname, const char **val)
629 {
630         int ret;
631
632         ret = __fwnode_property_read_string(fwnode, propname, val);
633         if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
634             !IS_ERR_OR_NULL(fwnode->secondary))
635                 ret = __fwnode_property_read_string(fwnode->secondary,
636                                                     propname, val);
637         return ret;
638 }
639 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
640
641 /**
642  * fwnode_property_match_string - find a string in an array and return index
643  * @fwnode: Firmware node to get the property of
644  * @propname: Name of the property holding the array
645  * @string: String to look for
646  *
647  * Find a given string in a string array and if it is found return the
648  * index back.
649  *
650  * Return: %0 if the property was found (success),
651  *         %-EINVAL if given arguments are not valid,
652  *         %-ENODATA if the property does not have a value,
653  *         %-EPROTO if the property is not an array of strings,
654  *         %-ENXIO if no suitable firmware interface is present.
655  */
656 int fwnode_property_match_string(struct fwnode_handle *fwnode,
657         const char *propname, const char *string)
658 {
659         const char **values;
660         int nval, ret;
661
662         nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
663         if (nval < 0)
664                 return nval;
665
666         if (nval == 0)
667                 return -ENODATA;
668
669         values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
670         if (!values)
671                 return -ENOMEM;
672
673         ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
674         if (ret < 0)
675                 goto out;
676
677         ret = match_string(values, nval, string);
678         if (ret < 0)
679                 ret = -ENODATA;
680 out:
681         kfree(values);
682         return ret;
683 }
684 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
685
686 static int property_copy_string_array(struct property_entry *dst,
687                                       const struct property_entry *src)
688 {
689         char **d;
690         size_t nval = src->length / sizeof(*d);
691         int i;
692
693         d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
694         if (!d)
695                 return -ENOMEM;
696
697         for (i = 0; i < nval; i++) {
698                 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
699                 if (!d[i] && src->pointer.str[i]) {
700                         while (--i >= 0)
701                                 kfree(d[i]);
702                         kfree(d);
703                         return -ENOMEM;
704                 }
705         }
706
707         dst->pointer.raw_data = d;
708         return 0;
709 }
710
711 static int property_entry_copy_data(struct property_entry *dst,
712                                     const struct property_entry *src)
713 {
714         int error;
715
716         dst->name = kstrdup(src->name, GFP_KERNEL);
717         if (!dst->name)
718                 return -ENOMEM;
719
720         if (src->is_array) {
721                 if (!src->length) {
722                         error = -ENODATA;
723                         goto out_free_name;
724                 }
725
726                 if (src->is_string) {
727                         error = property_copy_string_array(dst, src);
728                         if (error)
729                                 goto out_free_name;
730                 } else {
731                         dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
732                                                         src->length, GFP_KERNEL);
733                         if (!dst->pointer.raw_data) {
734                                 error = -ENOMEM;
735                                 goto out_free_name;
736                         }
737                 }
738         } else if (src->is_string) {
739                 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
740                 if (!dst->value.str && src->value.str) {
741                         error = -ENOMEM;
742                         goto out_free_name;
743                 }
744         } else {
745                 dst->value.raw_data = src->value.raw_data;
746         }
747
748         dst->length = src->length;
749         dst->is_array = src->is_array;
750         dst->is_string = src->is_string;
751
752         return 0;
753
754 out_free_name:
755         kfree(dst->name);
756         return error;
757 }
758
759 static void property_entry_free_data(const struct property_entry *p)
760 {
761         size_t i, nval;
762
763         if (p->is_array) {
764                 if (p->is_string && p->pointer.str) {
765                         nval = p->length / sizeof(const char *);
766                         for (i = 0; i < nval; i++)
767                                 kfree(p->pointer.str[i]);
768                 }
769                 kfree(p->pointer.raw_data);
770         } else if (p->is_string) {
771                 kfree(p->value.str);
772         }
773         kfree(p->name);
774 }
775
776 /**
777  * property_entries_dup - duplicate array of properties
778  * @properties: array of properties to copy
779  *
780  * This function creates a deep copy of the given NULL-terminated array
781  * of property entries.
782  */
783 struct property_entry *
784 property_entries_dup(const struct property_entry *properties)
785 {
786         struct property_entry *p;
787         int i, n = 0;
788
789         while (properties[n].name)
790                 n++;
791
792         p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
793         if (!p)
794                 return ERR_PTR(-ENOMEM);
795
796         for (i = 0; i < n; i++) {
797                 int ret = property_entry_copy_data(&p[i], &properties[i]);
798                 if (ret) {
799                         while (--i >= 0)
800                                 property_entry_free_data(&p[i]);
801                         kfree(p);
802                         return ERR_PTR(ret);
803                 }
804         }
805
806         return p;
807 }
808 EXPORT_SYMBOL_GPL(property_entries_dup);
809
810 /**
811  * property_entries_free - free previously allocated array of properties
812  * @properties: array of properties to destroy
813  *
814  * This function frees given NULL-terminated array of property entries,
815  * along with their data.
816  */
817 void property_entries_free(const struct property_entry *properties)
818 {
819         const struct property_entry *p;
820
821         for (p = properties; p->name; p++)
822                 property_entry_free_data(p);
823
824         kfree(properties);
825 }
826 EXPORT_SYMBOL_GPL(property_entries_free);
827
828 /**
829  * pset_free_set - releases memory allocated for copied property set
830  * @pset: Property set to release
831  *
832  * Function takes previously copied property set and releases all the
833  * memory allocated to it.
834  */
835 static void pset_free_set(struct property_set *pset)
836 {
837         if (!pset)
838                 return;
839
840         property_entries_free(pset->properties);
841         kfree(pset);
842 }
843
844 /**
845  * pset_copy_set - copies property set
846  * @pset: Property set to copy
847  *
848  * This function takes a deep copy of the given property set and returns
849  * pointer to the copy. Call device_free_property_set() to free resources
850  * allocated in this function.
851  *
852  * Return: Pointer to the new property set or error pointer.
853  */
854 static struct property_set *pset_copy_set(const struct property_set *pset)
855 {
856         struct property_entry *properties;
857         struct property_set *p;
858
859         p = kzalloc(sizeof(*p), GFP_KERNEL);
860         if (!p)
861                 return ERR_PTR(-ENOMEM);
862
863         properties = property_entries_dup(pset->properties);
864         if (IS_ERR(properties)) {
865                 kfree(p);
866                 return ERR_CAST(properties);
867         }
868
869         p->properties = properties;
870         return p;
871 }
872
873 /**
874  * device_remove_properties - Remove properties from a device object.
875  * @dev: Device whose properties to remove.
876  *
877  * The function removes properties previously associated to the device
878  * secondary firmware node with device_add_properties(). Memory allocated
879  * to the properties will also be released.
880  */
881 void device_remove_properties(struct device *dev)
882 {
883         struct fwnode_handle *fwnode;
884
885         fwnode = dev_fwnode(dev);
886         if (!fwnode)
887                 return;
888         /*
889          * Pick either primary or secondary node depending which one holds
890          * the pset. If there is no real firmware node (ACPI/DT) primary
891          * will hold the pset.
892          */
893         if (is_pset_node(fwnode)) {
894                 set_primary_fwnode(dev, NULL);
895                 pset_free_set(to_pset_node(fwnode));
896         } else {
897                 fwnode = fwnode->secondary;
898                 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
899                         set_secondary_fwnode(dev, NULL);
900                         pset_free_set(to_pset_node(fwnode));
901                 }
902         }
903 }
904 EXPORT_SYMBOL_GPL(device_remove_properties);
905
906 /**
907  * device_add_properties - Add a collection of properties to a device object.
908  * @dev: Device to add properties to.
909  * @properties: Collection of properties to add.
910  *
911  * Associate a collection of device properties represented by @properties with
912  * @dev as its secondary firmware node. The function takes a copy of
913  * @properties.
914  */
915 int device_add_properties(struct device *dev,
916                           const struct property_entry *properties)
917 {
918         struct property_set *p, pset;
919
920         if (!properties)
921                 return -EINVAL;
922
923         pset.properties = properties;
924
925         p = pset_copy_set(&pset);
926         if (IS_ERR(p))
927                 return PTR_ERR(p);
928
929         p->fwnode.type = FWNODE_PDATA;
930         set_secondary_fwnode(dev, &p->fwnode);
931         return 0;
932 }
933 EXPORT_SYMBOL_GPL(device_add_properties);
934
935 /**
936  * fwnode_get_parent - Return parent firwmare node
937  * @fwnode: Firmware whose parent is retrieved
938  *
939  * Return parent firmware node of the given node if possible or %NULL if no
940  * parent was available.
941  */
942 struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode)
943 {
944         struct fwnode_handle *parent = NULL;
945
946         if (is_of_node(fwnode)) {
947                 struct device_node *node;
948
949                 node = of_get_parent(to_of_node(fwnode));
950                 if (node)
951                         parent = &node->fwnode;
952         } else if (is_acpi_node(fwnode)) {
953                 parent = acpi_node_get_parent(fwnode);
954         }
955
956         return parent;
957 }
958 EXPORT_SYMBOL_GPL(fwnode_get_parent);
959
960 /**
961  * fwnode_get_next_child_node - Return the next child node handle for a node
962  * @fwnode: Firmware node to find the next child node for.
963  * @child: Handle to one of the node's child nodes or a %NULL handle.
964  */
965 struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode,
966                                                  struct fwnode_handle *child)
967 {
968         if (is_of_node(fwnode)) {
969                 struct device_node *node;
970
971                 node = of_get_next_available_child(to_of_node(fwnode),
972                                                    to_of_node(child));
973                 if (node)
974                         return &node->fwnode;
975         } else if (is_acpi_node(fwnode)) {
976                 return acpi_get_next_subnode(fwnode, child);
977         }
978
979         return NULL;
980 }
981 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
982
983 /**
984  * device_get_next_child_node - Return the next child node handle for a device
985  * @dev: Device to find the next child node for.
986  * @child: Handle to one of the device's child nodes or a null handle.
987  */
988 struct fwnode_handle *device_get_next_child_node(struct device *dev,
989                                                  struct fwnode_handle *child)
990 {
991         struct acpi_device *adev = ACPI_COMPANION(dev);
992         struct fwnode_handle *fwnode = NULL;
993
994         if (dev->of_node)
995                 fwnode = &dev->of_node->fwnode;
996         else if (adev)
997                 fwnode = acpi_fwnode_handle(adev);
998
999         return fwnode_get_next_child_node(fwnode, child);
1000 }
1001 EXPORT_SYMBOL_GPL(device_get_next_child_node);
1002
1003 /**
1004  * fwnode_get_named_child_node - Return first matching named child node handle
1005  * @fwnode: Firmware node to find the named child node for.
1006  * @childname: String to match child node name against.
1007  */
1008 struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode,
1009                                                   const char *childname)
1010 {
1011         struct fwnode_handle *child;
1012
1013         /*
1014          * Find first matching named child node of this fwnode.
1015          * For ACPI this will be a data only sub-node.
1016          */
1017         fwnode_for_each_child_node(fwnode, child) {
1018                 if (is_of_node(child)) {
1019                         if (!of_node_cmp(to_of_node(child)->name, childname))
1020                                 return child;
1021                 } else if (is_acpi_data_node(child)) {
1022                         if (acpi_data_node_match(child, childname))
1023                                 return child;
1024                 }
1025         }
1026
1027         return NULL;
1028 }
1029 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1030
1031 /**
1032  * device_get_named_child_node - Return first matching named child node handle
1033  * @dev: Device to find the named child node for.
1034  * @childname: String to match child node name against.
1035  */
1036 struct fwnode_handle *device_get_named_child_node(struct device *dev,
1037                                                   const char *childname)
1038 {
1039         return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1040 }
1041 EXPORT_SYMBOL_GPL(device_get_named_child_node);
1042
1043 /**
1044  * fwnode_handle_put - Drop reference to a device node
1045  * @fwnode: Pointer to the device node to drop the reference to.
1046  *
1047  * This has to be used when terminating device_for_each_child_node() iteration
1048  * with break or return to prevent stale device node references from being left
1049  * behind.
1050  */
1051 void fwnode_handle_put(struct fwnode_handle *fwnode)
1052 {
1053         if (is_of_node(fwnode))
1054                 of_node_put(to_of_node(fwnode));
1055 }
1056 EXPORT_SYMBOL_GPL(fwnode_handle_put);
1057
1058 /**
1059  * device_get_child_node_count - return the number of child nodes for device
1060  * @dev: Device to cound the child nodes for
1061  */
1062 unsigned int device_get_child_node_count(struct device *dev)
1063 {
1064         struct fwnode_handle *child;
1065         unsigned int count = 0;
1066
1067         device_for_each_child_node(dev, child)
1068                 count++;
1069
1070         return count;
1071 }
1072 EXPORT_SYMBOL_GPL(device_get_child_node_count);
1073
1074 bool device_dma_supported(struct device *dev)
1075 {
1076         /* For DT, this is always supported.
1077          * For ACPI, this depends on CCA, which
1078          * is determined by the acpi_dma_supported().
1079          */
1080         if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1081                 return true;
1082
1083         return acpi_dma_supported(ACPI_COMPANION(dev));
1084 }
1085 EXPORT_SYMBOL_GPL(device_dma_supported);
1086
1087 enum dev_dma_attr device_get_dma_attr(struct device *dev)
1088 {
1089         enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1090
1091         if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1092                 if (of_dma_is_coherent(dev->of_node))
1093                         attr = DEV_DMA_COHERENT;
1094                 else
1095                         attr = DEV_DMA_NON_COHERENT;
1096         } else
1097                 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1098
1099         return attr;
1100 }
1101 EXPORT_SYMBOL_GPL(device_get_dma_attr);
1102
1103 /**
1104  * device_get_phy_mode - Get phy mode for given device
1105  * @dev:        Pointer to the given device
1106  *
1107  * The function gets phy interface string from property 'phy-mode' or
1108  * 'phy-connection-type', and return its index in phy_modes table, or errno in
1109  * error case.
1110  */
1111 int device_get_phy_mode(struct device *dev)
1112 {
1113         const char *pm;
1114         int err, i;
1115
1116         err = device_property_read_string(dev, "phy-mode", &pm);
1117         if (err < 0)
1118                 err = device_property_read_string(dev,
1119                                                   "phy-connection-type", &pm);
1120         if (err < 0)
1121                 return err;
1122
1123         for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1124                 if (!strcasecmp(pm, phy_modes(i)))
1125                         return i;
1126
1127         return -ENODEV;
1128 }
1129 EXPORT_SYMBOL_GPL(device_get_phy_mode);
1130
1131 static void *device_get_mac_addr(struct device *dev,
1132                                  const char *name, char *addr,
1133                                  int alen)
1134 {
1135         int ret = device_property_read_u8_array(dev, name, addr, alen);
1136
1137         if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
1138                 return addr;
1139         return NULL;
1140 }
1141
1142 /**
1143  * device_get_mac_address - Get the MAC for a given device
1144  * @dev:        Pointer to the device
1145  * @addr:       Address of buffer to store the MAC in
1146  * @alen:       Length of the buffer pointed to by addr, should be ETH_ALEN
1147  *
1148  * Search the firmware node for the best MAC address to use.  'mac-address' is
1149  * checked first, because that is supposed to contain to "most recent" MAC
1150  * address. If that isn't set, then 'local-mac-address' is checked next,
1151  * because that is the default address.  If that isn't set, then the obsolete
1152  * 'address' is checked, just in case we're using an old device tree.
1153  *
1154  * Note that the 'address' property is supposed to contain a virtual address of
1155  * the register set, but some DTS files have redefined that property to be the
1156  * MAC address.
1157  *
1158  * All-zero MAC addresses are rejected, because those could be properties that
1159  * exist in the firmware tables, but were not updated by the firmware.  For
1160  * example, the DTS could define 'mac-address' and 'local-mac-address', with
1161  * zero MAC addresses.  Some older U-Boots only initialized 'local-mac-address'.
1162  * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1163  * exists but is all zeros.
1164 */
1165 void *device_get_mac_address(struct device *dev, char *addr, int alen)
1166 {
1167         char *res;
1168
1169         res = device_get_mac_addr(dev, "mac-address", addr, alen);
1170         if (res)
1171                 return res;
1172
1173         res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1174         if (res)
1175                 return res;
1176
1177         return device_get_mac_addr(dev, "address", addr, alen);
1178 }
1179 EXPORT_SYMBOL(device_get_mac_address);
1180
1181 /**
1182  * device_graph_get_next_endpoint - Get next endpoint firmware node
1183  * @fwnode: Pointer to the parent firmware node
1184  * @prev: Previous endpoint node or %NULL to get the first
1185  *
1186  * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1187  * are available.
1188  */
1189 struct fwnode_handle *
1190 fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode,
1191                                struct fwnode_handle *prev)
1192 {
1193         struct fwnode_handle *endpoint = NULL;
1194
1195         if (is_of_node(fwnode)) {
1196                 struct device_node *node;
1197
1198                 node = of_graph_get_next_endpoint(to_of_node(fwnode),
1199                                                   to_of_node(prev));
1200
1201                 if (node)
1202                         endpoint = &node->fwnode;
1203         } else if (is_acpi_node(fwnode)) {
1204                 endpoint = acpi_graph_get_next_endpoint(fwnode, prev);
1205                 if (IS_ERR(endpoint))
1206                         endpoint = NULL;
1207         }
1208
1209         return endpoint;
1210
1211 }
1212 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1213
1214 /**
1215  * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1216  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1217  *
1218  * Extracts firmware node of a remote device the @fwnode points to.
1219  */
1220 struct fwnode_handle *
1221 fwnode_graph_get_remote_port_parent(struct fwnode_handle *fwnode)
1222 {
1223         struct fwnode_handle *parent = NULL;
1224
1225         if (is_of_node(fwnode)) {
1226                 struct device_node *node;
1227
1228                 node = of_graph_get_remote_port_parent(to_of_node(fwnode));
1229                 if (node)
1230                         parent = &node->fwnode;
1231         } else if (is_acpi_node(fwnode)) {
1232                 int ret;
1233
1234                 ret = acpi_graph_get_remote_endpoint(fwnode, &parent, NULL,
1235                                                      NULL);
1236                 if (ret)
1237                         return NULL;
1238         }
1239
1240         return parent;
1241 }
1242 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1243
1244 /**
1245  * fwnode_graph_get_remote_port - Return fwnode of a remote port
1246  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1247  *
1248  * Extracts firmware node of a remote port the @fwnode points to.
1249  */
1250 struct fwnode_handle *fwnode_graph_get_remote_port(struct fwnode_handle *fwnode)
1251 {
1252         struct fwnode_handle *port = NULL;
1253
1254         if (is_of_node(fwnode)) {
1255                 struct device_node *node;
1256
1257                 node = of_graph_get_remote_port(to_of_node(fwnode));
1258                 if (node)
1259                         port = &node->fwnode;
1260         } else if (is_acpi_node(fwnode)) {
1261                 int ret;
1262
1263                 ret = acpi_graph_get_remote_endpoint(fwnode, NULL, &port, NULL);
1264                 if (ret)
1265                         return NULL;
1266         }
1267
1268         return port;
1269 }
1270 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1271
1272 /**
1273  * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1274  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1275  *
1276  * Extracts firmware node of a remote endpoint the @fwnode points to.
1277  */
1278 struct fwnode_handle *
1279 fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode)
1280 {
1281         struct fwnode_handle *endpoint = NULL;
1282
1283         if (is_of_node(fwnode)) {
1284                 struct device_node *node;
1285
1286                 node = of_parse_phandle(to_of_node(fwnode), "remote-endpoint",
1287                                         0);
1288                 if (node)
1289                         endpoint = &node->fwnode;
1290         } else if (is_acpi_node(fwnode)) {
1291                 int ret;
1292
1293                 ret = acpi_graph_get_remote_endpoint(fwnode, NULL, NULL,
1294                                                      &endpoint);
1295                 if (ret)
1296                         return NULL;
1297         }
1298
1299         return endpoint;
1300 }
1301 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);