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