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