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