Merge tag 'pm+acpi-4.6-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[deliverable/linux.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 23{
0224a4a3 24 return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA;
16ba08d5
RW
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);
0d67e0fa
HK
221 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
222 !IS_ERR_OR_NULL(fwnode->secondary))
362c0b30
AS
223 ret = __fwnode_property_present(fwnode->secondary, propname);
224 return ret;
225}
8a0662d9
RW
226EXPORT_SYMBOL_GPL(fwnode_property_present);
227
b31384fa
RW
228/**
229 * device_property_read_u8_array - return a u8 array property of a device
230 * @dev: Device to get the property of
231 * @propname: Name of the property
5c0acf3b 232 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
233 * @nval: Size of the @val array
234 *
235 * Function reads an array of u8 properties with @propname from the device
236 * firmware description and stores them to @val if found.
237 *
5c0acf3b
AH
238 * Return: number of values if @val was %NULL,
239 * %0 if the property was found (success),
b31384fa
RW
240 * %-EINVAL if given arguments are not valid,
241 * %-ENODATA if the property does not have a value,
242 * %-EPROTO if the property is not an array of numbers,
243 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 244 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
245 */
246int device_property_read_u8_array(struct device *dev, const char *propname,
247 u8 *val, size_t nval)
248{
9017f252 249 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
250}
251EXPORT_SYMBOL_GPL(device_property_read_u8_array);
252
253/**
254 * device_property_read_u16_array - return a u16 array property of a device
255 * @dev: Device to get the property of
256 * @propname: Name of the property
5c0acf3b 257 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
258 * @nval: Size of the @val array
259 *
260 * Function reads an array of u16 properties with @propname from the device
261 * firmware description and stores them to @val if found.
262 *
5c0acf3b
AH
263 * Return: number of values if @val was %NULL,
264 * %0 if the property was found (success),
b31384fa
RW
265 * %-EINVAL if given arguments are not valid,
266 * %-ENODATA if the property does not have a value,
267 * %-EPROTO if the property is not an array of numbers,
268 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 269 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
270 */
271int device_property_read_u16_array(struct device *dev, const char *propname,
272 u16 *val, size_t nval)
273{
9017f252 274 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
275}
276EXPORT_SYMBOL_GPL(device_property_read_u16_array);
277
278/**
279 * device_property_read_u32_array - return a u32 array property of a device
280 * @dev: Device to get the property of
281 * @propname: Name of the property
5c0acf3b 282 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
283 * @nval: Size of the @val array
284 *
285 * Function reads an array of u32 properties with @propname from the device
286 * firmware description and stores them to @val if found.
287 *
5c0acf3b
AH
288 * Return: number of values if @val was %NULL,
289 * %0 if the property was found (success),
b31384fa
RW
290 * %-EINVAL if given arguments are not valid,
291 * %-ENODATA if the property does not have a value,
292 * %-EPROTO if the property is not an array of numbers,
293 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 294 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
295 */
296int device_property_read_u32_array(struct device *dev, const char *propname,
297 u32 *val, size_t nval)
298{
9017f252 299 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
300}
301EXPORT_SYMBOL_GPL(device_property_read_u32_array);
302
303/**
304 * device_property_read_u64_array - return a u64 array property of a device
305 * @dev: Device to get the property of
306 * @propname: Name of the property
5c0acf3b 307 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
308 * @nval: Size of the @val array
309 *
310 * Function reads an array of u64 properties with @propname from the device
311 * firmware description and stores them to @val if found.
312 *
5c0acf3b
AH
313 * Return: number of values if @val was %NULL,
314 * %0 if the property was found (success),
b31384fa
RW
315 * %-EINVAL if given arguments are not valid,
316 * %-ENODATA if the property does not have a value,
317 * %-EPROTO if the property is not an array of numbers,
318 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 319 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
320 */
321int device_property_read_u64_array(struct device *dev, const char *propname,
322 u64 *val, size_t nval)
323{
9017f252 324 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
325}
326EXPORT_SYMBOL_GPL(device_property_read_u64_array);
327
328/**
329 * device_property_read_string_array - return a string array property of device
330 * @dev: Device to get the property of
331 * @propname: Name of the property
5c0acf3b 332 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
333 * @nval: Size of the @val array
334 *
335 * Function reads an array of string properties with @propname from the device
336 * firmware description and stores them to @val if found.
337 *
5c0acf3b
AH
338 * Return: number of values if @val was %NULL,
339 * %0 if the property was found (success),
b31384fa
RW
340 * %-EINVAL if given arguments are not valid,
341 * %-ENODATA if the property does not have a value,
342 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
343 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 344 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
345 */
346int device_property_read_string_array(struct device *dev, const char *propname,
347 const char **val, size_t nval)
348{
9017f252 349 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
350}
351EXPORT_SYMBOL_GPL(device_property_read_string_array);
352
353/**
354 * device_property_read_string - return a string property of a device
355 * @dev: Device to get the property of
356 * @propname: Name of the property
357 * @val: The value is stored here
358 *
359 * Function reads property @propname from the device firmware description and
360 * stores the value into @val if found. The value is checked to be a string.
361 *
362 * Return: %0 if the property was found (success),
363 * %-EINVAL if given arguments are not valid,
364 * %-ENODATA if the property does not have a value,
365 * %-EPROTO or %-EILSEQ if the property type is not a string.
4fa7508e 366 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
367 */
368int device_property_read_string(struct device *dev, const char *propname,
369 const char **val)
370{
9017f252 371 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
b31384fa
RW
372}
373EXPORT_SYMBOL_GPL(device_property_read_string);
8a0662d9 374
3f5c8d31
MW
375/**
376 * device_property_match_string - find a string in an array and return index
377 * @dev: Device to get the property of
378 * @propname: Name of the property holding the array
379 * @string: String to look for
380 *
381 * Find a given string in a string array and if it is found return the
382 * index back.
383 *
384 * Return: %0 if the property was found (success),
385 * %-EINVAL if given arguments are not valid,
386 * %-ENODATA if the property does not have a value,
387 * %-EPROTO if the property is not an array of strings,
388 * %-ENXIO if no suitable firmware interface is present.
389 */
390int device_property_match_string(struct device *dev, const char *propname,
391 const char *string)
392{
393 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
394}
395EXPORT_SYMBOL_GPL(device_property_match_string);
396
1d656fb7
AS
397#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
398 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
9017f252
RW
399 : of_property_count_elems_of_size((node), (propname), sizeof(type))
400
318a1971
AS
401#define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
402 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
403 : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
404
362c0b30 405#define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
1d656fb7
AS
406({ \
407 int _ret_; \
408 if (is_of_node(_fwnode_)) \
409 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
410 _type_, _val_, _nval_); \
411 else if (is_acpi_node(_fwnode_)) \
412 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
413 _val_, _nval_); \
61f5e294 414 else if (is_pset_node(_fwnode_)) \
318a1971
AS
415 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
416 _type_, _val_, _nval_); \
1d656fb7
AS
417 else \
418 _ret_ = -ENXIO; \
419 _ret_; \
8a0662d9
RW
420})
421
362c0b30
AS
422#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
423({ \
424 int _ret_; \
425 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \
426 _val_, _nval_); \
0d67e0fa
HK
427 if (_ret_ == -EINVAL && !IS_ERR_OR_NULL(_fwnode_) && \
428 !IS_ERR_OR_NULL(_fwnode_->secondary)) \
362c0b30
AS
429 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \
430 _proptype_, _val_, _nval_); \
431 _ret_; \
432})
433
8a0662d9
RW
434/**
435 * fwnode_property_read_u8_array - return a u8 array property of firmware node
436 * @fwnode: Firmware node to get the property of
437 * @propname: Name of the property
5c0acf3b 438 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
439 * @nval: Size of the @val array
440 *
441 * Read an array of u8 properties with @propname from @fwnode and stores them to
442 * @val if found.
443 *
5c0acf3b
AH
444 * Return: number of values if @val was %NULL,
445 * %0 if the property was found (success),
8a0662d9
RW
446 * %-EINVAL if given arguments are not valid,
447 * %-ENODATA if the property does not have a value,
448 * %-EPROTO if the property is not an array of numbers,
449 * %-EOVERFLOW if the size of the property is not as expected,
450 * %-ENXIO if no suitable firmware interface is present.
451 */
452int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
453 const char *propname, u8 *val, size_t nval)
454{
455 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
456 val, nval);
457}
458EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
459
460/**
461 * fwnode_property_read_u16_array - return a u16 array property of firmware node
462 * @fwnode: Firmware node to get the property of
463 * @propname: Name of the property
5c0acf3b 464 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
465 * @nval: Size of the @val array
466 *
467 * Read an array of u16 properties with @propname from @fwnode and store them to
468 * @val if found.
469 *
5c0acf3b
AH
470 * Return: number of values if @val was %NULL,
471 * %0 if the property was found (success),
8a0662d9
RW
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 */
478int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
479 const char *propname, u16 *val, size_t nval)
480{
481 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
482 val, nval);
483}
484EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
485
486/**
487 * fwnode_property_read_u32_array - return a u32 array property of firmware node
488 * @fwnode: Firmware node to get the property of
489 * @propname: Name of the property
5c0acf3b 490 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
491 * @nval: Size of the @val array
492 *
493 * Read an array of u32 properties with @propname from @fwnode store them to
494 * @val if found.
495 *
5c0acf3b
AH
496 * Return: number of values if @val was %NULL,
497 * %0 if the property was found (success),
8a0662d9
RW
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 */
504int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
505 const char *propname, u32 *val, size_t nval)
506{
507 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
508 val, nval);
509}
510EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
511
512/**
513 * fwnode_property_read_u64_array - return a u64 array property firmware node
514 * @fwnode: Firmware node to get the property of
515 * @propname: Name of the property
5c0acf3b 516 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
517 * @nval: Size of the @val array
518 *
519 * Read an array of u64 properties with @propname from @fwnode and store them to
520 * @val if found.
521 *
5c0acf3b
AH
522 * Return: number of values if @val was %NULL,
523 * %0 if the property was found (success),
8a0662d9
RW
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 */
530int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
531 const char *propname, u64 *val, size_t nval)
532{
533 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
534 val, nval);
535}
536EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
537
362c0b30
AS
538static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode,
539 const char *propname,
540 const char **val, size_t nval)
541{
542 if (is_of_node(fwnode))
543 return val ?
544 of_property_read_string_array(to_of_node(fwnode),
545 propname, val, nval) :
546 of_property_count_strings(to_of_node(fwnode), propname);
547 else if (is_acpi_node(fwnode))
548 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
549 val, nval);
550 else if (is_pset_node(fwnode))
551 return val ?
552 pset_prop_read_string_array(to_pset_node(fwnode),
553 propname, val, nval) :
554 pset_prop_count_elems_of_size(to_pset_node(fwnode),
555 propname,
556 sizeof(const char *));
557 return -ENXIO;
558}
559
560static int __fwnode_property_read_string(struct fwnode_handle *fwnode,
561 const char *propname, const char **val)
562{
563 if (is_of_node(fwnode))
564 return of_property_read_string(to_of_node(fwnode), propname, val);
565 else if (is_acpi_node(fwnode))
566 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
567 val, 1);
568 else if (is_pset_node(fwnode))
569 return pset_prop_read_string(to_pset_node(fwnode), propname, val);
570 return -ENXIO;
571}
572
8a0662d9
RW
573/**
574 * fwnode_property_read_string_array - return string array property of a node
575 * @fwnode: Firmware node to get the property of
576 * @propname: Name of the property
5c0acf3b 577 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
578 * @nval: Size of the @val array
579 *
580 * Read an string list property @propname from the given firmware node and store
581 * them to @val if found.
582 *
5c0acf3b
AH
583 * Return: number of values if @val was %NULL,
584 * %0 if the property was found (success),
8a0662d9
RW
585 * %-EINVAL if given arguments are not valid,
586 * %-ENODATA if the property does not have a value,
587 * %-EPROTO if the property is not an array of strings,
588 * %-EOVERFLOW if the size of the property is not as expected,
589 * %-ENXIO if no suitable firmware interface is present.
590 */
591int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
592 const char *propname, const char **val,
593 size_t nval)
594{
362c0b30
AS
595 int ret;
596
597 ret = __fwnode_property_read_string_array(fwnode, propname, val, nval);
0d67e0fa
HK
598 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
599 !IS_ERR_OR_NULL(fwnode->secondary))
362c0b30
AS
600 ret = __fwnode_property_read_string_array(fwnode->secondary,
601 propname, val, nval);
602 return ret;
8a0662d9
RW
603}
604EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
605
606/**
607 * fwnode_property_read_string - return a string property of a firmware node
608 * @fwnode: Firmware node to get the property of
609 * @propname: Name of the property
610 * @val: The value is stored here
611 *
612 * Read property @propname from the given firmware node and store the value into
613 * @val if found. The value is checked to be a string.
614 *
615 * Return: %0 if the property was found (success),
616 * %-EINVAL if given arguments are not valid,
617 * %-ENODATA if the property does not have a value,
618 * %-EPROTO or %-EILSEQ if the property is not a string,
619 * %-ENXIO if no suitable firmware interface is present.
620 */
621int fwnode_property_read_string(struct fwnode_handle *fwnode,
622 const char *propname, const char **val)
623{
362c0b30
AS
624 int ret;
625
626 ret = __fwnode_property_read_string(fwnode, propname, val);
0d67e0fa
HK
627 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
628 !IS_ERR_OR_NULL(fwnode->secondary))
362c0b30
AS
629 ret = __fwnode_property_read_string(fwnode->secondary,
630 propname, val);
631 return ret;
8a0662d9
RW
632}
633EXPORT_SYMBOL_GPL(fwnode_property_read_string);
634
3f5c8d31
MW
635/**
636 * fwnode_property_match_string - find a string in an array and return index
637 * @fwnode: Firmware node to get the property of
638 * @propname: Name of the property holding the array
639 * @string: String to look for
640 *
641 * Find a given string in a string array and if it is found return the
642 * index back.
643 *
644 * Return: %0 if the property was found (success),
645 * %-EINVAL if given arguments are not valid,
646 * %-ENODATA if the property does not have a value,
647 * %-EPROTO if the property is not an array of strings,
648 * %-ENXIO if no suitable firmware interface is present.
649 */
650int fwnode_property_match_string(struct fwnode_handle *fwnode,
651 const char *propname, const char *string)
652{
653 const char **values;
a7c1d0a9 654 int nval, ret;
3f5c8d31
MW
655
656 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
657 if (nval < 0)
658 return nval;
659
f6740c18
AS
660 if (nval == 0)
661 return -ENODATA;
662
3f5c8d31
MW
663 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
664 if (!values)
665 return -ENOMEM;
666
667 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
668 if (ret < 0)
669 goto out;
670
a7c1d0a9
AS
671 ret = match_string(values, nval, string);
672 if (ret < 0)
673 ret = -ENODATA;
3f5c8d31
MW
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 */
0d67e0fa
HK
823 if (is_pset_node(fwnode)) {
824 set_primary_fwnode(dev, NULL);
13141e1c 825 pset_free_set(to_pset_node(fwnode));
0d67e0fa
HK
826 } else {
827 fwnode = fwnode->secondary;
828 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
829 set_secondary_fwnode(dev, NULL);
830 pset_free_set(to_pset_node(fwnode));
831 }
832 }
13141e1c
MW
833}
834EXPORT_SYMBOL_GPL(device_remove_property_set);
835
836/**
837 * device_add_property_set - Add a collection of properties to a device object.
838 * @dev: Device to add properties to.
839 * @pset: Collection of properties to add.
840 *
841 * Associate a collection of device properties represented by @pset with @dev
842 * as its secondary firmware node. The function takes a copy of @pset.
843 */
844int device_add_property_set(struct device *dev, const struct property_set *pset)
845{
846 struct property_set *p;
847
848 if (!pset)
849 return -EINVAL;
850
851 p = pset_copy_set(pset);
852 if (IS_ERR(p))
853 return PTR_ERR(p);
854
855 p->fwnode.type = FWNODE_PDATA;
856 set_secondary_fwnode(dev, &p->fwnode);
857 return 0;
858}
859EXPORT_SYMBOL_GPL(device_add_property_set);
860
8a0662d9
RW
861/**
862 * device_get_next_child_node - Return the next child node handle for a device
863 * @dev: Device to find the next child node for.
864 * @child: Handle to one of the device's child nodes or a null handle.
865 */
866struct fwnode_handle *device_get_next_child_node(struct device *dev,
867 struct fwnode_handle *child)
868{
869 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
870 struct device_node *node;
871
c181fb3e 872 node = of_get_next_available_child(dev->of_node, to_of_node(child));
8a0662d9
RW
873 if (node)
874 return &node->fwnode;
875 } else if (IS_ENABLED(CONFIG_ACPI)) {
504a3374 876 return acpi_get_next_subnode(dev, child);
8a0662d9
RW
877 }
878 return NULL;
879}
880EXPORT_SYMBOL_GPL(device_get_next_child_node);
881
882/**
883 * fwnode_handle_put - Drop reference to a device node
884 * @fwnode: Pointer to the device node to drop the reference to.
885 *
886 * This has to be used when terminating device_for_each_child_node() iteration
887 * with break or return to prevent stale device node references from being left
888 * behind.
889 */
890void fwnode_handle_put(struct fwnode_handle *fwnode)
891{
892 if (is_of_node(fwnode))
c181fb3e 893 of_node_put(to_of_node(fwnode));
8a0662d9
RW
894}
895EXPORT_SYMBOL_GPL(fwnode_handle_put);
896
897/**
898 * device_get_child_node_count - return the number of child nodes for device
899 * @dev: Device to cound the child nodes for
900 */
901unsigned int device_get_child_node_count(struct device *dev)
902{
903 struct fwnode_handle *child;
904 unsigned int count = 0;
905
906 device_for_each_child_node(dev, child)
907 count++;
908
909 return count;
910}
911EXPORT_SYMBOL_GPL(device_get_child_node_count);
05ca5560 912
e5e55864
SS
913bool device_dma_supported(struct device *dev)
914{
915 /* For DT, this is always supported.
916 * For ACPI, this depends on CCA, which
917 * is determined by the acpi_dma_supported().
918 */
919 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
920 return true;
921
922 return acpi_dma_supported(ACPI_COMPANION(dev));
923}
924EXPORT_SYMBOL_GPL(device_dma_supported);
925
926enum dev_dma_attr device_get_dma_attr(struct device *dev)
927{
928 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
929
930 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
931 if (of_dma_is_coherent(dev->of_node))
932 attr = DEV_DMA_COHERENT;
933 else
934 attr = DEV_DMA_NON_COHERENT;
935 } else
936 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
937
938 return attr;
939}
940EXPORT_SYMBOL_GPL(device_get_dma_attr);
941
4c96b7dc 942/**
2f710a3a 943 * device_get_phy_mode - Get phy mode for given device
4c96b7dc
JL
944 * @dev: Pointer to the given device
945 *
946 * The function gets phy interface string from property 'phy-mode' or
947 * 'phy-connection-type', and return its index in phy_modes table, or errno in
948 * error case.
949 */
950int device_get_phy_mode(struct device *dev)
951{
952 const char *pm;
953 int err, i;
954
955 err = device_property_read_string(dev, "phy-mode", &pm);
956 if (err < 0)
957 err = device_property_read_string(dev,
958 "phy-connection-type", &pm);
959 if (err < 0)
960 return err;
961
962 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
963 if (!strcasecmp(pm, phy_modes(i)))
964 return i;
965
966 return -ENODEV;
967}
968EXPORT_SYMBOL_GPL(device_get_phy_mode);
969
970static void *device_get_mac_addr(struct device *dev,
971 const char *name, char *addr,
972 int alen)
973{
974 int ret = device_property_read_u8_array(dev, name, addr, alen);
975
2f710a3a 976 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
4c96b7dc
JL
977 return addr;
978 return NULL;
979}
980
981/**
2f710a3a
JL
982 * device_get_mac_address - Get the MAC for a given device
983 * @dev: Pointer to the device
984 * @addr: Address of buffer to store the MAC in
985 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
986 *
987 * Search the firmware node for the best MAC address to use. 'mac-address' is
4c96b7dc
JL
988 * checked first, because that is supposed to contain to "most recent" MAC
989 * address. If that isn't set, then 'local-mac-address' is checked next,
990 * because that is the default address. If that isn't set, then the obsolete
991 * 'address' is checked, just in case we're using an old device tree.
992 *
993 * Note that the 'address' property is supposed to contain a virtual address of
994 * the register set, but some DTS files have redefined that property to be the
995 * MAC address.
996 *
997 * All-zero MAC addresses are rejected, because those could be properties that
2f710a3a
JL
998 * exist in the firmware tables, but were not updated by the firmware. For
999 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1000 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1001 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1002 * exists but is all zeros.
4c96b7dc
JL
1003*/
1004void *device_get_mac_address(struct device *dev, char *addr, int alen)
1005{
5b902d6f 1006 char *res;
4c96b7dc 1007
5b902d6f
JG
1008 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1009 if (res)
1010 return res;
1011
1012 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1013 if (res)
1014 return res;
4c96b7dc
JL
1015
1016 return device_get_mac_addr(dev, "address", addr, alen);
1017}
1018EXPORT_SYMBOL(device_get_mac_address);
This page took 0.139175 seconds and 5 git commands to generate.