]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/base/property.c
Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[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/property.h>
19 #include <linux/etherdevice.h>
20 #include <linux/phy.h>
21
22 static inline bool is_pset_node(struct fwnode_handle *fwnode)
23 {
24         return fwnode && fwnode->type == FWNODE_PDATA;
25 }
26
27 static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
28 {
29         return is_pset_node(fwnode) ?
30                 container_of(fwnode, struct property_set, fwnode) : NULL;
31 }
32
33 static struct property_entry *pset_prop_get(struct property_set *pset,
34                                             const char *name)
35 {
36         struct property_entry *prop;
37
38         if (!pset || !pset->properties)
39                 return NULL;
40
41         for (prop = pset->properties; prop->name; prop++)
42                 if (!strcmp(name, prop->name))
43                         return prop;
44
45         return NULL;
46 }
47
48 static void *pset_prop_find(struct property_set *pset, const char *propname,
49                             size_t length)
50 {
51         struct property_entry *prop;
52         void *pointer;
53
54         prop = pset_prop_get(pset, propname);
55         if (!prop)
56                 return ERR_PTR(-EINVAL);
57         if (prop->is_array)
58                 pointer = prop->pointer.raw_data;
59         else
60                 pointer = &prop->value.raw_data;
61         if (!pointer)
62                 return ERR_PTR(-ENODATA);
63         if (length > prop->length)
64                 return ERR_PTR(-EOVERFLOW);
65         return pointer;
66 }
67
68 static int pset_prop_read_u8_array(struct property_set *pset,
69                                    const char *propname,
70                                    u8 *values, size_t nval)
71 {
72         void *pointer;
73         size_t length = nval * sizeof(*values);
74
75         pointer = pset_prop_find(pset, propname, length);
76         if (IS_ERR(pointer))
77                 return PTR_ERR(pointer);
78
79         memcpy(values, pointer, length);
80         return 0;
81 }
82
83 static int pset_prop_read_u16_array(struct property_set *pset,
84                                     const char *propname,
85                                     u16 *values, size_t nval)
86 {
87         void *pointer;
88         size_t length = nval * sizeof(*values);
89
90         pointer = pset_prop_find(pset, propname, length);
91         if (IS_ERR(pointer))
92                 return PTR_ERR(pointer);
93
94         memcpy(values, pointer, length);
95         return 0;
96 }
97
98 static int pset_prop_read_u32_array(struct property_set *pset,
99                                     const char *propname,
100                                     u32 *values, size_t nval)
101 {
102         void *pointer;
103         size_t length = nval * sizeof(*values);
104
105         pointer = pset_prop_find(pset, propname, length);
106         if (IS_ERR(pointer))
107                 return PTR_ERR(pointer);
108
109         memcpy(values, pointer, length);
110         return 0;
111 }
112
113 static int pset_prop_read_u64_array(struct property_set *pset,
114                                     const char *propname,
115                                     u64 *values, size_t nval)
116 {
117         void *pointer;
118         size_t length = nval * sizeof(*values);
119
120         pointer = pset_prop_find(pset, propname, length);
121         if (IS_ERR(pointer))
122                 return PTR_ERR(pointer);
123
124         memcpy(values, pointer, length);
125         return 0;
126 }
127
128 static int pset_prop_count_elems_of_size(struct property_set *pset,
129                                          const char *propname, size_t length)
130 {
131         struct property_entry *prop;
132
133         prop = pset_prop_get(pset, propname);
134         if (!prop)
135                 return -EINVAL;
136
137         return prop->length / length;
138 }
139
140 static int pset_prop_read_string_array(struct property_set *pset,
141                                        const char *propname,
142                                        const char **strings, size_t nval)
143 {
144         void *pointer;
145         size_t length = nval * sizeof(*strings);
146
147         pointer = pset_prop_find(pset, propname, length);
148         if (IS_ERR(pointer))
149                 return PTR_ERR(pointer);
150
151         memcpy(strings, pointer, length);
152         return 0;
153 }
154
155 static int pset_prop_read_string(struct property_set *pset,
156                                  const char *propname, const char **strings)
157 {
158         struct property_entry *prop;
159         const char **pointer;
160
161         prop = pset_prop_get(pset, propname);
162         if (!prop)
163                 return -EINVAL;
164         if (!prop->is_string)
165                 return -EILSEQ;
166         if (prop->is_array) {
167                 pointer = prop->pointer.str;
168                 if (!pointer)
169                         return -ENODATA;
170         } else {
171                 pointer = &prop->value.str;
172                 if (*pointer && strnlen(*pointer, prop->length) >= prop->length)
173                         return -EILSEQ;
174         }
175
176         *strings = *pointer;
177         return 0;
178 }
179
180 static inline struct fwnode_handle *dev_fwnode(struct device *dev)
181 {
182         return IS_ENABLED(CONFIG_OF) && dev->of_node ?
183                 &dev->of_node->fwnode : dev->fwnode;
184 }
185
186 /**
187  * device_property_present - check if a property of a device is present
188  * @dev: Device whose property is being checked
189  * @propname: Name of the property
190  *
191  * Check if property @propname is present in the device firmware description.
192  */
193 bool device_property_present(struct device *dev, const char *propname)
194 {
195         return fwnode_property_present(dev_fwnode(dev), propname);
196 }
197 EXPORT_SYMBOL_GPL(device_property_present);
198
199 static bool __fwnode_property_present(struct fwnode_handle *fwnode,
200                                       const char *propname)
201 {
202         if (is_of_node(fwnode))
203                 return of_property_read_bool(to_of_node(fwnode), propname);
204         else if (is_acpi_node(fwnode))
205                 return !acpi_node_prop_get(fwnode, propname, NULL);
206         else if (is_pset_node(fwnode))
207                 return !!pset_prop_get(to_pset_node(fwnode), propname);
208         return false;
209 }
210
211 /**
212  * fwnode_property_present - check if a property of a firmware node is present
213  * @fwnode: Firmware node whose property to check
214  * @propname: Name of the property
215  */
216 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
217 {
218         bool ret;
219
220         ret = __fwnode_property_present(fwnode, propname);
221         if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
222             !IS_ERR_OR_NULL(fwnode->secondary))
223                 ret = __fwnode_property_present(fwnode->secondary, propname);
224         return ret;
225 }
226 EXPORT_SYMBOL_GPL(fwnode_property_present);
227
228 /**
229  * device_property_read_u8_array - return a u8 array property of a device
230  * @dev: Device to get the property of
231  * @propname: Name of the property
232  * @val: The values are stored here or %NULL to return the number of values
233  * @nval: Size of the @val array
234  *
235  * Function reads an array of u8 properties with @propname from the device
236  * firmware description and stores them to @val if found.
237  *
238  * Return: number of values if @val was %NULL,
239  *         %0 if the property was found (success),
240  *         %-EINVAL if given arguments are not valid,
241  *         %-ENODATA if the property does not have a value,
242  *         %-EPROTO if the property is not an array of numbers,
243  *         %-EOVERFLOW if the size of the property is not as expected.
244  *         %-ENXIO if no suitable firmware interface is present.
245  */
246 int device_property_read_u8_array(struct device *dev, const char *propname,
247                                   u8 *val, size_t nval)
248 {
249         return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
250 }
251 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
252
253 /**
254  * device_property_read_u16_array - return a u16 array property of a device
255  * @dev: Device to get the property of
256  * @propname: Name of the property
257  * @val: The values are stored here or %NULL to return the number of values
258  * @nval: Size of the @val array
259  *
260  * Function reads an array of u16 properties with @propname from the device
261  * firmware description and stores them to @val if found.
262  *
263  * Return: number of values if @val was %NULL,
264  *         %0 if the property was found (success),
265  *         %-EINVAL if given arguments are not valid,
266  *         %-ENODATA if the property does not have a value,
267  *         %-EPROTO if the property is not an array of numbers,
268  *         %-EOVERFLOW if the size of the property is not as expected.
269  *         %-ENXIO if no suitable firmware interface is present.
270  */
271 int device_property_read_u16_array(struct device *dev, const char *propname,
272                                    u16 *val, size_t nval)
273 {
274         return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
275 }
276 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
277
278 /**
279  * device_property_read_u32_array - return a u32 array property of a device
280  * @dev: Device to get the property of
281  * @propname: Name of the property
282  * @val: The values are stored here or %NULL to return the number of values
283  * @nval: Size of the @val array
284  *
285  * Function reads an array of u32 properties with @propname from the device
286  * firmware description and stores them to @val if found.
287  *
288  * Return: number of values if @val was %NULL,
289  *         %0 if the property was found (success),
290  *         %-EINVAL if given arguments are not valid,
291  *         %-ENODATA if the property does not have a value,
292  *         %-EPROTO if the property is not an array of numbers,
293  *         %-EOVERFLOW if the size of the property is not as expected.
294  *         %-ENXIO if no suitable firmware interface is present.
295  */
296 int device_property_read_u32_array(struct device *dev, const char *propname,
297                                    u32 *val, size_t nval)
298 {
299         return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
300 }
301 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
302
303 /**
304  * device_property_read_u64_array - return a u64 array property of a device
305  * @dev: Device to get the property of
306  * @propname: Name of the property
307  * @val: The values are stored here or %NULL to return the number of values
308  * @nval: Size of the @val array
309  *
310  * Function reads an array of u64 properties with @propname from the device
311  * firmware description and stores them to @val if found.
312  *
313  * Return: number of values if @val was %NULL,
314  *         %0 if the property was found (success),
315  *         %-EINVAL if given arguments are not valid,
316  *         %-ENODATA if the property does not have a value,
317  *         %-EPROTO if the property is not an array of numbers,
318  *         %-EOVERFLOW if the size of the property is not as expected.
319  *         %-ENXIO if no suitable firmware interface is present.
320  */
321 int device_property_read_u64_array(struct device *dev, const char *propname,
322                                    u64 *val, size_t nval)
323 {
324         return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
325 }
326 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
327
328 /**
329  * device_property_read_string_array - return a string array property of device
330  * @dev: Device to get the property of
331  * @propname: Name of the property
332  * @val: The values are stored here or %NULL to return the number of values
333  * @nval: Size of the @val array
334  *
335  * Function reads an array of string properties with @propname from the device
336  * firmware description and stores them to @val if found.
337  *
338  * Return: number of values if @val was %NULL,
339  *         %0 if the property was found (success),
340  *         %-EINVAL if given arguments are not valid,
341  *         %-ENODATA if the property does not have a value,
342  *         %-EPROTO or %-EILSEQ if the property is not an array of strings,
343  *         %-EOVERFLOW if the size of the property is not as expected.
344  *         %-ENXIO if no suitable firmware interface is present.
345  */
346 int device_property_read_string_array(struct device *dev, const char *propname,
347                                       const char **val, size_t nval)
348 {
349         return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
350 }
351 EXPORT_SYMBOL_GPL(device_property_read_string_array);
352
353 /**
354  * device_property_read_string - return a string property of a device
355  * @dev: Device to get the property of
356  * @propname: Name of the property
357  * @val: The value is stored here
358  *
359  * Function reads property @propname from the device firmware description and
360  * stores the value into @val if found. The value is checked to be a string.
361  *
362  * Return: %0 if the property was found (success),
363  *         %-EINVAL if given arguments are not valid,
364  *         %-ENODATA if the property does not have a value,
365  *         %-EPROTO or %-EILSEQ if the property type is not a string.
366  *         %-ENXIO if no suitable firmware interface is present.
367  */
368 int device_property_read_string(struct device *dev, const char *propname,
369                                 const char **val)
370 {
371         return fwnode_property_read_string(dev_fwnode(dev), propname, val);
372 }
373 EXPORT_SYMBOL_GPL(device_property_read_string);
374
375 /**
376  * device_property_match_string - find a string in an array and return index
377  * @dev: Device to get the property of
378  * @propname: Name of the property holding the array
379  * @string: String to look for
380  *
381  * Find a given string in a string array and if it is found return the
382  * index back.
383  *
384  * Return: %0 if the property was found (success),
385  *         %-EINVAL if given arguments are not valid,
386  *         %-ENODATA if the property does not have a value,
387  *         %-EPROTO if the property is not an array of strings,
388  *         %-ENXIO if no suitable firmware interface is present.
389  */
390 int device_property_match_string(struct device *dev, const char *propname,
391                                  const char *string)
392 {
393         return fwnode_property_match_string(dev_fwnode(dev), propname, string);
394 }
395 EXPORT_SYMBOL_GPL(device_property_match_string);
396
397 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval)                         \
398         (val) ? of_property_read_##type##_array((node), (propname), (val), (nval))      \
399               : of_property_count_elems_of_size((node), (propname), sizeof(type))
400
401 #define PSET_PROP_READ_ARRAY(node, propname, type, val, nval)                           \
402         (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval))        \
403               : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
404
405 #define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_)       \
406 ({                                                                                      \
407         int _ret_;                                                                      \
408         if (is_of_node(_fwnode_))                                                       \
409                 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_,        \
410                                                _type_, _val_, _nval_);                  \
411         else if (is_acpi_node(_fwnode_))                                                \
412                 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_,           \
413                                             _val_, _nval_);                             \
414         else if (is_pset_node(_fwnode_))                                                \
415                 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_,        \
416                                              _type_, _val_, _nval_);                    \
417         else                                                                            \
418                 _ret_ = -ENXIO;                                                         \
419         _ret_;                                                                          \
420 })
421
422 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
423 ({                                                                                      \
424         int _ret_;                                                                      \
425         _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_,              \
426                                  _val_, _nval_);                                        \
427         if (_ret_ == -EINVAL && !IS_ERR_OR_NULL(_fwnode_) &&                            \
428             !IS_ERR_OR_NULL(_fwnode_->secondary))                                       \
429                 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_,       \
430                                 _proptype_, _val_, _nval_);                             \
431         _ret_;                                                                          \
432 })
433
434 /**
435  * fwnode_property_read_u8_array - return a u8 array property of firmware node
436  * @fwnode: Firmware node to get the property of
437  * @propname: Name of the property
438  * @val: The values are stored here or %NULL to return the number of values
439  * @nval: Size of the @val array
440  *
441  * Read an array of u8 properties with @propname from @fwnode and stores them to
442  * @val if found.
443  *
444  * Return: number of values if @val was %NULL,
445  *         %0 if the property was found (success),
446  *         %-EINVAL if given arguments are not valid,
447  *         %-ENODATA if the property does not have a value,
448  *         %-EPROTO if the property is not an array of numbers,
449  *         %-EOVERFLOW if the size of the property is not as expected,
450  *         %-ENXIO if no suitable firmware interface is present.
451  */
452 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
453                                   const char *propname, u8 *val, size_t nval)
454 {
455         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
456                                       val, nval);
457 }
458 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
459
460 /**
461  * fwnode_property_read_u16_array - return a u16 array property of firmware node
462  * @fwnode: Firmware node to get the property of
463  * @propname: Name of the property
464  * @val: The values are stored here or %NULL to return the number of values
465  * @nval: Size of the @val array
466  *
467  * Read an array of u16 properties with @propname from @fwnode and store them to
468  * @val if found.
469  *
470  * Return: number of values if @val was %NULL,
471  *         %0 if the property was found (success),
472  *         %-EINVAL if given arguments are not valid,
473  *         %-ENODATA if the property does not have a value,
474  *         %-EPROTO if the property is not an array of numbers,
475  *         %-EOVERFLOW if the size of the property is not as expected,
476  *         %-ENXIO if no suitable firmware interface is present.
477  */
478 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
479                                    const char *propname, u16 *val, size_t nval)
480 {
481         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
482                                       val, nval);
483 }
484 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
485
486 /**
487  * fwnode_property_read_u32_array - return a u32 array property of firmware node
488  * @fwnode: Firmware node to get the property of
489  * @propname: Name of the property
490  * @val: The values are stored here or %NULL to return the number of values
491  * @nval: Size of the @val array
492  *
493  * Read an array of u32 properties with @propname from @fwnode store them to
494  * @val if found.
495  *
496  * Return: number of values if @val was %NULL,
497  *         %0 if the property was found (success),
498  *         %-EINVAL if given arguments are not valid,
499  *         %-ENODATA if the property does not have a value,
500  *         %-EPROTO if the property is not an array of numbers,
501  *         %-EOVERFLOW if the size of the property is not as expected,
502  *         %-ENXIO if no suitable firmware interface is present.
503  */
504 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
505                                    const char *propname, u32 *val, size_t nval)
506 {
507         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
508                                       val, nval);
509 }
510 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
511
512 /**
513  * fwnode_property_read_u64_array - return a u64 array property firmware node
514  * @fwnode: Firmware node to get the property of
515  * @propname: Name of the property
516  * @val: The values are stored here or %NULL to return the number of values
517  * @nval: Size of the @val array
518  *
519  * Read an array of u64 properties with @propname from @fwnode and store them to
520  * @val if found.
521  *
522  * Return: number of values if @val was %NULL,
523  *         %0 if the property was found (success),
524  *         %-EINVAL if given arguments are not valid,
525  *         %-ENODATA if the property does not have a value,
526  *         %-EPROTO if the property is not an array of numbers,
527  *         %-EOVERFLOW if the size of the property is not as expected,
528  *         %-ENXIO if no suitable firmware interface is present.
529  */
530 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
531                                    const char *propname, u64 *val, size_t nval)
532 {
533         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
534                                       val, nval);
535 }
536 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
537
538 static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode,
539                                                const char *propname,
540                                                const char **val, size_t nval)
541 {
542         if (is_of_node(fwnode))
543                 return val ?
544                         of_property_read_string_array(to_of_node(fwnode),
545                                                       propname, val, nval) :
546                         of_property_count_strings(to_of_node(fwnode), propname);
547         else if (is_acpi_node(fwnode))
548                 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
549                                            val, nval);
550         else if (is_pset_node(fwnode))
551                 return val ?
552                         pset_prop_read_string_array(to_pset_node(fwnode),
553                                                     propname, val, nval) :
554                         pset_prop_count_elems_of_size(to_pset_node(fwnode),
555                                                       propname,
556                                                       sizeof(const char *));
557         return -ENXIO;
558 }
559
560 static int __fwnode_property_read_string(struct fwnode_handle *fwnode,
561                                          const char *propname, const char **val)
562 {
563         if (is_of_node(fwnode))
564                 return of_property_read_string(to_of_node(fwnode), propname, val);
565         else if (is_acpi_node(fwnode))
566                 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
567                                            val, 1);
568         else if (is_pset_node(fwnode))
569                 return pset_prop_read_string(to_pset_node(fwnode), propname, val);
570         return -ENXIO;
571 }
572
573 /**
574  * fwnode_property_read_string_array - return string array property of a node
575  * @fwnode: Firmware node to get the property of
576  * @propname: Name of the property
577  * @val: The values are stored here or %NULL to return the number of values
578  * @nval: Size of the @val array
579  *
580  * Read an string list property @propname from the given firmware node and store
581  * them to @val if found.
582  *
583  * Return: number of values if @val was %NULL,
584  *         %0 if the property was found (success),
585  *         %-EINVAL if given arguments are not valid,
586  *         %-ENODATA if the property does not have a value,
587  *         %-EPROTO if the property is not an array of strings,
588  *         %-EOVERFLOW if the size of the property is not as expected,
589  *         %-ENXIO if no suitable firmware interface is present.
590  */
591 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
592                                       const char *propname, const char **val,
593                                       size_t nval)
594 {
595         int ret;
596
597         ret = __fwnode_property_read_string_array(fwnode, propname, val, nval);
598         if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
599             !IS_ERR_OR_NULL(fwnode->secondary))
600                 ret = __fwnode_property_read_string_array(fwnode->secondary,
601                                                           propname, val, nval);
602         return ret;
603 }
604 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
605
606 /**
607  * fwnode_property_read_string - return a string property of a firmware node
608  * @fwnode: Firmware node to get the property of
609  * @propname: Name of the property
610  * @val: The value is stored here
611  *
612  * Read property @propname from the given firmware node and store the value into
613  * @val if found.  The value is checked to be a string.
614  *
615  * Return: %0 if the property was found (success),
616  *         %-EINVAL if given arguments are not valid,
617  *         %-ENODATA if the property does not have a value,
618  *         %-EPROTO or %-EILSEQ if the property is not a string,
619  *         %-ENXIO if no suitable firmware interface is present.
620  */
621 int fwnode_property_read_string(struct fwnode_handle *fwnode,
622                                 const char *propname, const char **val)
623 {
624         int ret;
625
626         ret = __fwnode_property_read_string(fwnode, propname, val);
627         if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
628             !IS_ERR_OR_NULL(fwnode->secondary))
629                 ret = __fwnode_property_read_string(fwnode->secondary,
630                                                     propname, val);
631         return ret;
632 }
633 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
634
635 /**
636  * fwnode_property_match_string - find a string in an array and return index
637  * @fwnode: Firmware node to get the property of
638  * @propname: Name of the property holding the array
639  * @string: String to look for
640  *
641  * Find a given string in a string array and if it is found return the
642  * index back.
643  *
644  * Return: %0 if the property was found (success),
645  *         %-EINVAL if given arguments are not valid,
646  *         %-ENODATA if the property does not have a value,
647  *         %-EPROTO if the property is not an array of strings,
648  *         %-ENXIO if no suitable firmware interface is present.
649  */
650 int fwnode_property_match_string(struct fwnode_handle *fwnode,
651         const char *propname, const char *string)
652 {
653         const char **values;
654         int nval, ret, i;
655
656         nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
657         if (nval < 0)
658                 return nval;
659
660         if (nval == 0)
661                 return -ENODATA;
662
663         values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
664         if (!values)
665                 return -ENOMEM;
666
667         ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
668         if (ret < 0)
669                 goto out;
670
671         ret = -ENODATA;
672         for (i = 0; i < nval; i++) {
673                 if (!strcmp(values[i], string)) {
674                         ret = i;
675                         break;
676                 }
677         }
678 out:
679         kfree(values);
680         return ret;
681 }
682 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
683
684 /**
685  * pset_free_set - releases memory allocated for copied property set
686  * @pset: Property set to release
687  *
688  * Function takes previously copied property set and releases all the
689  * memory allocated to it.
690  */
691 static void pset_free_set(struct property_set *pset)
692 {
693         const struct property_entry *prop;
694         size_t i, nval;
695
696         if (!pset)
697                 return;
698
699         for (prop = pset->properties; prop->name; prop++) {
700                 if (prop->is_array) {
701                         if (prop->is_string && prop->pointer.str) {
702                                 nval = prop->length / sizeof(const char *);
703                                 for (i = 0; i < nval; i++)
704                                         kfree(prop->pointer.str[i]);
705                         }
706                         kfree(prop->pointer.raw_data);
707                 } else if (prop->is_string) {
708                         kfree(prop->value.str);
709                 }
710                 kfree(prop->name);
711         }
712
713         kfree(pset->properties);
714         kfree(pset);
715 }
716
717 static int pset_copy_entry(struct property_entry *dst,
718                            const struct property_entry *src)
719 {
720         const char **d, **s;
721         size_t i, nval;
722
723         dst->name = kstrdup(src->name, GFP_KERNEL);
724         if (!dst->name)
725                 return -ENOMEM;
726
727         if (src->is_array) {
728                 if (!src->length)
729                         return -ENODATA;
730
731                 if (src->is_string) {
732                         nval = src->length / sizeof(const char *);
733                         dst->pointer.str = kcalloc(nval, sizeof(const char *),
734                                                    GFP_KERNEL);
735                         if (!dst->pointer.str)
736                                 return -ENOMEM;
737
738                         d = dst->pointer.str;
739                         s = src->pointer.str;
740                         for (i = 0; i < nval; i++) {
741                                 d[i] = kstrdup(s[i], GFP_KERNEL);
742                                 if (!d[i] && s[i])
743                                         return -ENOMEM;
744                         }
745                 } else {
746                         dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
747                                                         src->length, GFP_KERNEL);
748                         if (!dst->pointer.raw_data)
749                                 return -ENOMEM;
750                 }
751         } else if (src->is_string) {
752                 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
753                 if (!dst->value.str && src->value.str)
754                         return -ENOMEM;
755         } else {
756                 dst->value.raw_data = src->value.raw_data;
757         }
758
759         dst->length = src->length;
760         dst->is_array = src->is_array;
761         dst->is_string = src->is_string;
762
763         return 0;
764 }
765
766 /**
767  * pset_copy_set - copies property set
768  * @pset: Property set to copy
769  *
770  * This function takes a deep copy of the given property set and returns
771  * pointer to the copy. Call device_free_property_set() to free resources
772  * allocated in this function.
773  *
774  * Return: Pointer to the new property set or error pointer.
775  */
776 static struct property_set *pset_copy_set(const struct property_set *pset)
777 {
778         const struct property_entry *entry;
779         struct property_set *p;
780         size_t i, n = 0;
781
782         p = kzalloc(sizeof(*p), GFP_KERNEL);
783         if (!p)
784                 return ERR_PTR(-ENOMEM);
785
786         while (pset->properties[n].name)
787                 n++;
788
789         p->properties = kcalloc(n + 1, sizeof(*entry), GFP_KERNEL);
790         if (!p->properties) {
791                 kfree(p);
792                 return ERR_PTR(-ENOMEM);
793         }
794
795         for (i = 0; i < n; i++) {
796                 int ret = pset_copy_entry(&p->properties[i],
797                                           &pset->properties[i]);
798                 if (ret) {
799                         pset_free_set(p);
800                         return ERR_PTR(ret);
801                 }
802         }
803
804         return p;
805 }
806
807 /**
808  * device_remove_property_set - Remove properties from a device object.
809  * @dev: Device whose properties to remove.
810  *
811  * The function removes properties previously associated to the device
812  * secondary firmware node with device_add_property_set(). Memory allocated
813  * to the properties will also be released.
814  */
815 void device_remove_property_set(struct device *dev)
816 {
817         struct fwnode_handle *fwnode;
818
819         fwnode = dev_fwnode(dev);
820         if (!fwnode)
821                 return;
822         /*
823          * Pick either primary or secondary node depending which one holds
824          * the pset. If there is no real firmware node (ACPI/DT) primary
825          * will hold the pset.
826          */
827         if (is_pset_node(fwnode)) {
828                 set_primary_fwnode(dev, NULL);
829                 pset_free_set(to_pset_node(fwnode));
830         } else {
831                 fwnode = fwnode->secondary;
832                 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
833                         set_secondary_fwnode(dev, NULL);
834                         pset_free_set(to_pset_node(fwnode));
835                 }
836         }
837 }
838 EXPORT_SYMBOL_GPL(device_remove_property_set);
839
840 /**
841  * device_add_property_set - Add a collection of properties to a device object.
842  * @dev: Device to add properties to.
843  * @pset: Collection of properties to add.
844  *
845  * Associate a collection of device properties represented by @pset with @dev
846  * as its secondary firmware node. The function takes a copy of @pset.
847  */
848 int device_add_property_set(struct device *dev, const struct property_set *pset)
849 {
850         struct property_set *p;
851
852         if (!pset)
853                 return -EINVAL;
854
855         p = pset_copy_set(pset);
856         if (IS_ERR(p))
857                 return PTR_ERR(p);
858
859         p->fwnode.type = FWNODE_PDATA;
860         set_secondary_fwnode(dev, &p->fwnode);
861         return 0;
862 }
863 EXPORT_SYMBOL_GPL(device_add_property_set);
864
865 /**
866  * device_get_next_child_node - Return the next child node handle for a device
867  * @dev: Device to find the next child node for.
868  * @child: Handle to one of the device's child nodes or a null handle.
869  */
870 struct fwnode_handle *device_get_next_child_node(struct device *dev,
871                                                  struct fwnode_handle *child)
872 {
873         if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
874                 struct device_node *node;
875
876                 node = of_get_next_available_child(dev->of_node, to_of_node(child));
877                 if (node)
878                         return &node->fwnode;
879         } else if (IS_ENABLED(CONFIG_ACPI)) {
880                 return acpi_get_next_subnode(dev, child);
881         }
882         return NULL;
883 }
884 EXPORT_SYMBOL_GPL(device_get_next_child_node);
885
886 /**
887  * fwnode_handle_put - Drop reference to a device node
888  * @fwnode: Pointer to the device node to drop the reference to.
889  *
890  * This has to be used when terminating device_for_each_child_node() iteration
891  * with break or return to prevent stale device node references from being left
892  * behind.
893  */
894 void fwnode_handle_put(struct fwnode_handle *fwnode)
895 {
896         if (is_of_node(fwnode))
897                 of_node_put(to_of_node(fwnode));
898 }
899 EXPORT_SYMBOL_GPL(fwnode_handle_put);
900
901 /**
902  * device_get_child_node_count - return the number of child nodes for device
903  * @dev: Device to cound the child nodes for
904  */
905 unsigned int device_get_child_node_count(struct device *dev)
906 {
907         struct fwnode_handle *child;
908         unsigned int count = 0;
909
910         device_for_each_child_node(dev, child)
911                 count++;
912
913         return count;
914 }
915 EXPORT_SYMBOL_GPL(device_get_child_node_count);
916
917 bool device_dma_supported(struct device *dev)
918 {
919         /* For DT, this is always supported.
920          * For ACPI, this depends on CCA, which
921          * is determined by the acpi_dma_supported().
922          */
923         if (IS_ENABLED(CONFIG_OF) && dev->of_node)
924                 return true;
925
926         return acpi_dma_supported(ACPI_COMPANION(dev));
927 }
928 EXPORT_SYMBOL_GPL(device_dma_supported);
929
930 enum dev_dma_attr device_get_dma_attr(struct device *dev)
931 {
932         enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
933
934         if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
935                 if (of_dma_is_coherent(dev->of_node))
936                         attr = DEV_DMA_COHERENT;
937                 else
938                         attr = DEV_DMA_NON_COHERENT;
939         } else
940                 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
941
942         return attr;
943 }
944 EXPORT_SYMBOL_GPL(device_get_dma_attr);
945
946 /**
947  * device_get_phy_mode - Get phy mode for given device
948  * @dev:        Pointer to the given device
949  *
950  * The function gets phy interface string from property 'phy-mode' or
951  * 'phy-connection-type', and return its index in phy_modes table, or errno in
952  * error case.
953  */
954 int device_get_phy_mode(struct device *dev)
955 {
956         const char *pm;
957         int err, i;
958
959         err = device_property_read_string(dev, "phy-mode", &pm);
960         if (err < 0)
961                 err = device_property_read_string(dev,
962                                                   "phy-connection-type", &pm);
963         if (err < 0)
964                 return err;
965
966         for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
967                 if (!strcasecmp(pm, phy_modes(i)))
968                         return i;
969
970         return -ENODEV;
971 }
972 EXPORT_SYMBOL_GPL(device_get_phy_mode);
973
974 static void *device_get_mac_addr(struct device *dev,
975                                  const char *name, char *addr,
976                                  int alen)
977 {
978         int ret = device_property_read_u8_array(dev, name, addr, alen);
979
980         if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
981                 return addr;
982         return NULL;
983 }
984
985 /**
986  * device_get_mac_address - Get the MAC for a given device
987  * @dev:        Pointer to the device
988  * @addr:       Address of buffer to store the MAC in
989  * @alen:       Length of the buffer pointed to by addr, should be ETH_ALEN
990  *
991  * Search the firmware node for the best MAC address to use.  'mac-address' is
992  * checked first, because that is supposed to contain to "most recent" MAC
993  * address. If that isn't set, then 'local-mac-address' is checked next,
994  * because that is the default address.  If that isn't set, then the obsolete
995  * 'address' is checked, just in case we're using an old device tree.
996  *
997  * Note that the 'address' property is supposed to contain a virtual address of
998  * the register set, but some DTS files have redefined that property to be the
999  * MAC address.
1000  *
1001  * All-zero MAC addresses are rejected, because those could be properties that
1002  * exist in the firmware tables, but were not updated by the firmware.  For
1003  * example, the DTS could define 'mac-address' and 'local-mac-address', with
1004  * zero MAC addresses.  Some older U-Boots only initialized 'local-mac-address'.
1005  * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1006  * exists but is all zeros.
1007 */
1008 void *device_get_mac_address(struct device *dev, char *addr, int alen)
1009 {
1010         char *res;
1011
1012         res = device_get_mac_addr(dev, "mac-address", addr, alen);
1013         if (res)
1014                 return res;
1015
1016         res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1017         if (res)
1018                 return res;
1019
1020         return device_get_mac_addr(dev, "address", addr, alen);
1021 }
1022 EXPORT_SYMBOL(device_get_mac_address);