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