]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/base/property.c
2e01f3f1b53bcf310a68344dcafd763f4dcb11da
[linux-beck.git] / drivers / base / property.c
1 /*
2  * property.c - Unified device property interface.
3  *
4  * Copyright (C) 2014, Intel Corporation
5  * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6  *          Mika Westerberg <mika.westerberg@linux.intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/acpi.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/property.h>
19 #include <linux/etherdevice.h>
20 #include <linux/phy.h>
21
22 /**
23  * device_add_property_set - Add a collection of properties to a device object.
24  * @dev: Device to add properties to.
25  * @pset: Collection of properties to add.
26  *
27  * Associate a collection of device properties represented by @pset with @dev
28  * as its secondary firmware node.
29  */
30 void device_add_property_set(struct device *dev, struct property_set *pset)
31 {
32         if (!pset)
33                 return;
34
35         pset->fwnode.type = FWNODE_PDATA;
36         set_secondary_fwnode(dev, &pset->fwnode);
37 }
38 EXPORT_SYMBOL_GPL(device_add_property_set);
39
40 static inline bool is_pset_node(struct fwnode_handle *fwnode)
41 {
42         return fwnode && fwnode->type == FWNODE_PDATA;
43 }
44
45 static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
46 {
47         return is_pset_node(fwnode) ?
48                 container_of(fwnode, struct property_set, fwnode) : NULL;
49 }
50
51 static struct property_entry *pset_prop_get(struct property_set *pset,
52                                             const char *name)
53 {
54         struct property_entry *prop;
55
56         if (!pset || !pset->properties)
57                 return NULL;
58
59         for (prop = pset->properties; prop->name; prop++)
60                 if (!strcmp(name, prop->name))
61                         return prop;
62
63         return NULL;
64 }
65
66 static int pset_prop_read_array(struct property_set *pset, const char *name,
67                                 enum dev_prop_type type, void *val, size_t nval)
68 {
69         struct property_entry *prop;
70         unsigned int item_size;
71
72         prop = pset_prop_get(pset, name);
73         if (!prop)
74                 return -ENODATA;
75
76         if (prop->type != type)
77                 return -EPROTO;
78
79         if (!val)
80                 return prop->nval;
81
82         if (prop->nval < nval)
83                 return -EOVERFLOW;
84
85         switch (type) {
86         case DEV_PROP_U8:
87                 item_size = sizeof(u8);
88                 break;
89         case DEV_PROP_U16:
90                 item_size = sizeof(u16);
91                 break;
92         case DEV_PROP_U32:
93                 item_size = sizeof(u32);
94                 break;
95         case DEV_PROP_U64:
96                 item_size = sizeof(u64);
97                 break;
98         case DEV_PROP_STRING:
99                 item_size = sizeof(const char *);
100                 break;
101         default:
102                 return -EINVAL;
103         }
104         memcpy(val, prop->value.raw_data, nval * item_size);
105         return 0;
106 }
107
108 static inline struct fwnode_handle *dev_fwnode(struct device *dev)
109 {
110         return IS_ENABLED(CONFIG_OF) && dev->of_node ?
111                 &dev->of_node->fwnode : dev->fwnode;
112 }
113
114 /**
115  * device_property_present - check if a property of a device is present
116  * @dev: Device whose property is being checked
117  * @propname: Name of the property
118  *
119  * Check if property @propname is present in the device firmware description.
120  */
121 bool device_property_present(struct device *dev, const char *propname)
122 {
123         return fwnode_property_present(dev_fwnode(dev), propname);
124 }
125 EXPORT_SYMBOL_GPL(device_property_present);
126
127 /**
128  * fwnode_property_present - check if a property of a firmware node is present
129  * @fwnode: Firmware node whose property to check
130  * @propname: Name of the property
131  */
132 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
133 {
134         if (is_of_node(fwnode))
135                 return of_property_read_bool(to_of_node(fwnode), propname);
136         else if (is_acpi_node(fwnode))
137                 return !acpi_node_prop_get(fwnode, propname, NULL);
138         else if (is_pset_node(fwnode))
139                 return !!pset_prop_get(to_pset_node(fwnode), propname);
140         return false;
141 }
142 EXPORT_SYMBOL_GPL(fwnode_property_present);
143
144 /**
145  * device_property_read_u8_array - return a u8 array property of a device
146  * @dev: Device to get the property of
147  * @propname: Name of the property
148  * @val: The values are stored here or %NULL to return the number of values
149  * @nval: Size of the @val array
150  *
151  * Function reads an array of u8 properties with @propname from the device
152  * firmware description and stores them to @val if found.
153  *
154  * Return: number of values if @val was %NULL,
155  *         %0 if the property was found (success),
156  *         %-EINVAL if given arguments are not valid,
157  *         %-ENODATA if the property does not have a value,
158  *         %-EPROTO if the property is not an array of numbers,
159  *         %-EOVERFLOW if the size of the property is not as expected.
160  *         %-ENXIO if no suitable firmware interface is present.
161  */
162 int device_property_read_u8_array(struct device *dev, const char *propname,
163                                   u8 *val, size_t nval)
164 {
165         return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
166 }
167 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
168
169 /**
170  * device_property_read_u16_array - return a u16 array property of a device
171  * @dev: Device to get the property of
172  * @propname: Name of the property
173  * @val: The values are stored here or %NULL to return the number of values
174  * @nval: Size of the @val array
175  *
176  * Function reads an array of u16 properties with @propname from the device
177  * firmware description and stores them to @val if found.
178  *
179  * Return: number of values if @val was %NULL,
180  *         %0 if the property was found (success),
181  *         %-EINVAL if given arguments are not valid,
182  *         %-ENODATA if the property does not have a value,
183  *         %-EPROTO if the property is not an array of numbers,
184  *         %-EOVERFLOW if the size of the property is not as expected.
185  *         %-ENXIO if no suitable firmware interface is present.
186  */
187 int device_property_read_u16_array(struct device *dev, const char *propname,
188                                    u16 *val, size_t nval)
189 {
190         return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
191 }
192 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
193
194 /**
195  * device_property_read_u32_array - return a u32 array property of a device
196  * @dev: Device to get the property of
197  * @propname: Name of the property
198  * @val: The values are stored here or %NULL to return the number of values
199  * @nval: Size of the @val array
200  *
201  * Function reads an array of u32 properties with @propname from the device
202  * firmware description and stores them to @val if found.
203  *
204  * Return: number of values if @val was %NULL,
205  *         %0 if the property was found (success),
206  *         %-EINVAL if given arguments are not valid,
207  *         %-ENODATA if the property does not have a value,
208  *         %-EPROTO if the property is not an array of numbers,
209  *         %-EOVERFLOW if the size of the property is not as expected.
210  *         %-ENXIO if no suitable firmware interface is present.
211  */
212 int device_property_read_u32_array(struct device *dev, const char *propname,
213                                    u32 *val, size_t nval)
214 {
215         return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
216 }
217 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
218
219 /**
220  * device_property_read_u64_array - return a u64 array property of a device
221  * @dev: Device to get the property of
222  * @propname: Name of the property
223  * @val: The values are stored here or %NULL to return the number of values
224  * @nval: Size of the @val array
225  *
226  * Function reads an array of u64 properties with @propname from the device
227  * firmware description and stores them to @val if found.
228  *
229  * Return: number of values if @val was %NULL,
230  *         %0 if the property was found (success),
231  *         %-EINVAL if given arguments are not valid,
232  *         %-ENODATA if the property does not have a value,
233  *         %-EPROTO if the property is not an array of numbers,
234  *         %-EOVERFLOW if the size of the property is not as expected.
235  *         %-ENXIO if no suitable firmware interface is present.
236  */
237 int device_property_read_u64_array(struct device *dev, const char *propname,
238                                    u64 *val, size_t nval)
239 {
240         return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
241 }
242 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
243
244 /**
245  * device_property_read_string_array - return a string array property of device
246  * @dev: Device to get the property of
247  * @propname: Name of the property
248  * @val: The values are stored here or %NULL to return the number of values
249  * @nval: Size of the @val array
250  *
251  * Function reads an array of string properties with @propname from the device
252  * firmware description and stores them to @val if found.
253  *
254  * Return: number of values if @val was %NULL,
255  *         %0 if the property was found (success),
256  *         %-EINVAL if given arguments are not valid,
257  *         %-ENODATA if the property does not have a value,
258  *         %-EPROTO or %-EILSEQ if the property is not an array of strings,
259  *         %-EOVERFLOW if the size of the property is not as expected.
260  *         %-ENXIO if no suitable firmware interface is present.
261  */
262 int device_property_read_string_array(struct device *dev, const char *propname,
263                                       const char **val, size_t nval)
264 {
265         return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
266 }
267 EXPORT_SYMBOL_GPL(device_property_read_string_array);
268
269 /**
270  * device_property_read_string - return a string property of a device
271  * @dev: Device to get the property of
272  * @propname: Name of the property
273  * @val: The value is stored here
274  *
275  * Function reads property @propname from the device firmware description and
276  * stores the value into @val if found. The value is checked to be a string.
277  *
278  * Return: %0 if the property was found (success),
279  *         %-EINVAL if given arguments are not valid,
280  *         %-ENODATA if the property does not have a value,
281  *         %-EPROTO or %-EILSEQ if the property type is not a string.
282  *         %-ENXIO if no suitable firmware interface is present.
283  */
284 int device_property_read_string(struct device *dev, const char *propname,
285                                 const char **val)
286 {
287         return fwnode_property_read_string(dev_fwnode(dev), propname, val);
288 }
289 EXPORT_SYMBOL_GPL(device_property_read_string);
290
291 /**
292  * device_property_match_string - find a string in an array and return index
293  * @dev: Device to get the property of
294  * @propname: Name of the property holding the array
295  * @string: String to look for
296  *
297  * Find a given string in a string array and if it is found return the
298  * index back.
299  *
300  * Return: %0 if the property was found (success),
301  *         %-EINVAL if given arguments are not valid,
302  *         %-ENODATA if the property does not have a value,
303  *         %-EPROTO if the property is not an array of strings,
304  *         %-ENXIO if no suitable firmware interface is present.
305  */
306 int device_property_match_string(struct device *dev, const char *propname,
307                                  const char *string)
308 {
309         return fwnode_property_match_string(dev_fwnode(dev), propname, string);
310 }
311 EXPORT_SYMBOL_GPL(device_property_match_string);
312
313 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
314         (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
315               : of_property_count_elems_of_size((node), (propname), sizeof(type))
316
317 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
318 ({ \
319         int _ret_; \
320         if (is_of_node(_fwnode_)) \
321                 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
322                                                _type_, _val_, _nval_); \
323         else if (is_acpi_node(_fwnode_)) \
324                 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
325                                             _val_, _nval_); \
326         else if (is_pset_node(_fwnode_))                                                \
327                 _ret_ = pset_prop_read_array(to_pset_node(_fwnode_), _propname_,        \
328                                              _proptype_, _val_, _nval_); \
329         else \
330                 _ret_ = -ENXIO; \
331         _ret_; \
332 })
333
334 /**
335  * fwnode_property_read_u8_array - return a u8 array property of firmware node
336  * @fwnode: Firmware node to get the property of
337  * @propname: Name of the property
338  * @val: The values are stored here or %NULL to return the number of values
339  * @nval: Size of the @val array
340  *
341  * Read an array of u8 properties with @propname from @fwnode and stores them to
342  * @val if found.
343  *
344  * Return: number of values if @val was %NULL,
345  *         %0 if the property was found (success),
346  *         %-EINVAL if given arguments are not valid,
347  *         %-ENODATA if the property does not have a value,
348  *         %-EPROTO if the property is not an array of numbers,
349  *         %-EOVERFLOW if the size of the property is not as expected,
350  *         %-ENXIO if no suitable firmware interface is present.
351  */
352 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
353                                   const char *propname, u8 *val, size_t nval)
354 {
355         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
356                                       val, nval);
357 }
358 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
359
360 /**
361  * fwnode_property_read_u16_array - return a u16 array property of firmware node
362  * @fwnode: Firmware node to get the property of
363  * @propname: Name of the property
364  * @val: The values are stored here or %NULL to return the number of values
365  * @nval: Size of the @val array
366  *
367  * Read an array of u16 properties with @propname from @fwnode and store them to
368  * @val if found.
369  *
370  * Return: number of values if @val was %NULL,
371  *         %0 if the property was found (success),
372  *         %-EINVAL if given arguments are not valid,
373  *         %-ENODATA if the property does not have a value,
374  *         %-EPROTO if the property is not an array of numbers,
375  *         %-EOVERFLOW if the size of the property is not as expected,
376  *         %-ENXIO if no suitable firmware interface is present.
377  */
378 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
379                                    const char *propname, u16 *val, size_t nval)
380 {
381         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
382                                       val, nval);
383 }
384 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
385
386 /**
387  * fwnode_property_read_u32_array - return a u32 array property of firmware node
388  * @fwnode: Firmware node to get the property of
389  * @propname: Name of the property
390  * @val: The values are stored here or %NULL to return the number of values
391  * @nval: Size of the @val array
392  *
393  * Read an array of u32 properties with @propname from @fwnode store them to
394  * @val if found.
395  *
396  * Return: number of values if @val was %NULL,
397  *         %0 if the property was found (success),
398  *         %-EINVAL if given arguments are not valid,
399  *         %-ENODATA if the property does not have a value,
400  *         %-EPROTO if the property is not an array of numbers,
401  *         %-EOVERFLOW if the size of the property is not as expected,
402  *         %-ENXIO if no suitable firmware interface is present.
403  */
404 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
405                                    const char *propname, u32 *val, size_t nval)
406 {
407         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
408                                       val, nval);
409 }
410 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
411
412 /**
413  * fwnode_property_read_u64_array - return a u64 array property firmware node
414  * @fwnode: Firmware node to get the property of
415  * @propname: Name of the property
416  * @val: The values are stored here or %NULL to return the number of values
417  * @nval: Size of the @val array
418  *
419  * Read an array of u64 properties with @propname from @fwnode and store them to
420  * @val if found.
421  *
422  * Return: number of values if @val was %NULL,
423  *         %0 if the property was found (success),
424  *         %-EINVAL if given arguments are not valid,
425  *         %-ENODATA if the property does not have a value,
426  *         %-EPROTO if the property is not an array of numbers,
427  *         %-EOVERFLOW if the size of the property is not as expected,
428  *         %-ENXIO if no suitable firmware interface is present.
429  */
430 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
431                                    const char *propname, u64 *val, size_t nval)
432 {
433         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
434                                       val, nval);
435 }
436 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
437
438 /**
439  * fwnode_property_read_string_array - return string array property of a node
440  * @fwnode: Firmware node to get the property of
441  * @propname: Name of the property
442  * @val: The values are stored here or %NULL to return the number of values
443  * @nval: Size of the @val array
444  *
445  * Read an string list property @propname from the given firmware node and store
446  * them to @val if found.
447  *
448  * Return: number of values if @val was %NULL,
449  *         %0 if the property was found (success),
450  *         %-EINVAL if given arguments are not valid,
451  *         %-ENODATA if the property does not have a value,
452  *         %-EPROTO if the property is not an array of strings,
453  *         %-EOVERFLOW if the size of the property is not as expected,
454  *         %-ENXIO if no suitable firmware interface is present.
455  */
456 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
457                                       const char *propname, const char **val,
458                                       size_t nval)
459 {
460         if (is_of_node(fwnode))
461                 return val ?
462                         of_property_read_string_array(to_of_node(fwnode),
463                                                       propname, val, nval) :
464                         of_property_count_strings(to_of_node(fwnode), propname);
465         else if (is_acpi_node(fwnode))
466                 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
467                                            val, nval);
468         else if (is_pset_node(fwnode))
469                 return pset_prop_read_array(to_pset_node(fwnode), propname,
470                                             DEV_PROP_STRING, val, nval);
471         return -ENXIO;
472 }
473 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
474
475 /**
476  * fwnode_property_read_string - return a string property of a firmware node
477  * @fwnode: Firmware node to get the property of
478  * @propname: Name of the property
479  * @val: The value is stored here
480  *
481  * Read property @propname from the given firmware node and store the value into
482  * @val if found.  The value is checked to be a string.
483  *
484  * Return: %0 if the property was found (success),
485  *         %-EINVAL if given arguments are not valid,
486  *         %-ENODATA if the property does not have a value,
487  *         %-EPROTO or %-EILSEQ if the property is not a string,
488  *         %-ENXIO if no suitable firmware interface is present.
489  */
490 int fwnode_property_read_string(struct fwnode_handle *fwnode,
491                                 const char *propname, const char **val)
492 {
493         if (is_of_node(fwnode))
494                 return of_property_read_string(to_of_node(fwnode), propname, val);
495         else if (is_acpi_node(fwnode))
496                 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
497                                            val, 1);
498         else if (is_pset_node(fwnode))
499                 return pset_prop_read_array(to_pset_node(fwnode), propname,
500                                             DEV_PROP_STRING, val, 1);
501         return -ENXIO;
502 }
503 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
504
505 /**
506  * fwnode_property_match_string - find a string in an array and return index
507  * @fwnode: Firmware node to get the property of
508  * @propname: Name of the property holding the array
509  * @string: String to look for
510  *
511  * Find a given string in a string array and if it is found return the
512  * index back.
513  *
514  * Return: %0 if the property was found (success),
515  *         %-EINVAL if given arguments are not valid,
516  *         %-ENODATA if the property does not have a value,
517  *         %-EPROTO if the property is not an array of strings,
518  *         %-ENXIO if no suitable firmware interface is present.
519  */
520 int fwnode_property_match_string(struct fwnode_handle *fwnode,
521         const char *propname, const char *string)
522 {
523         const char **values;
524         int nval, ret, i;
525
526         nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
527         if (nval < 0)
528                 return nval;
529
530         values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
531         if (!values)
532                 return -ENOMEM;
533
534         ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
535         if (ret < 0)
536                 goto out;
537
538         ret = -ENODATA;
539         for (i = 0; i < nval; i++) {
540                 if (!strcmp(values[i], string)) {
541                         ret = i;
542                         break;
543                 }
544         }
545 out:
546         kfree(values);
547         return ret;
548 }
549 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
550
551 /**
552  * device_get_next_child_node - Return the next child node handle for a device
553  * @dev: Device to find the next child node for.
554  * @child: Handle to one of the device's child nodes or a null handle.
555  */
556 struct fwnode_handle *device_get_next_child_node(struct device *dev,
557                                                  struct fwnode_handle *child)
558 {
559         if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
560                 struct device_node *node;
561
562                 node = of_get_next_available_child(dev->of_node, to_of_node(child));
563                 if (node)
564                         return &node->fwnode;
565         } else if (IS_ENABLED(CONFIG_ACPI)) {
566                 return acpi_get_next_subnode(dev, child);
567         }
568         return NULL;
569 }
570 EXPORT_SYMBOL_GPL(device_get_next_child_node);
571
572 /**
573  * fwnode_handle_put - Drop reference to a device node
574  * @fwnode: Pointer to the device node to drop the reference to.
575  *
576  * This has to be used when terminating device_for_each_child_node() iteration
577  * with break or return to prevent stale device node references from being left
578  * behind.
579  */
580 void fwnode_handle_put(struct fwnode_handle *fwnode)
581 {
582         if (is_of_node(fwnode))
583                 of_node_put(to_of_node(fwnode));
584 }
585 EXPORT_SYMBOL_GPL(fwnode_handle_put);
586
587 /**
588  * device_get_child_node_count - return the number of child nodes for device
589  * @dev: Device to cound the child nodes for
590  */
591 unsigned int device_get_child_node_count(struct device *dev)
592 {
593         struct fwnode_handle *child;
594         unsigned int count = 0;
595
596         device_for_each_child_node(dev, child)
597                 count++;
598
599         return count;
600 }
601 EXPORT_SYMBOL_GPL(device_get_child_node_count);
602
603 bool device_dma_supported(struct device *dev)
604 {
605         /* For DT, this is always supported.
606          * For ACPI, this depends on CCA, which
607          * is determined by the acpi_dma_supported().
608          */
609         if (IS_ENABLED(CONFIG_OF) && dev->of_node)
610                 return true;
611
612         return acpi_dma_supported(ACPI_COMPANION(dev));
613 }
614 EXPORT_SYMBOL_GPL(device_dma_supported);
615
616 enum dev_dma_attr device_get_dma_attr(struct device *dev)
617 {
618         enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
619
620         if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
621                 if (of_dma_is_coherent(dev->of_node))
622                         attr = DEV_DMA_COHERENT;
623                 else
624                         attr = DEV_DMA_NON_COHERENT;
625         } else
626                 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
627
628         return attr;
629 }
630 EXPORT_SYMBOL_GPL(device_get_dma_attr);
631
632 /**
633  * device_get_phy_mode - Get phy mode for given device
634  * @dev:        Pointer to the given device
635  *
636  * The function gets phy interface string from property 'phy-mode' or
637  * 'phy-connection-type', and return its index in phy_modes table, or errno in
638  * error case.
639  */
640 int device_get_phy_mode(struct device *dev)
641 {
642         const char *pm;
643         int err, i;
644
645         err = device_property_read_string(dev, "phy-mode", &pm);
646         if (err < 0)
647                 err = device_property_read_string(dev,
648                                                   "phy-connection-type", &pm);
649         if (err < 0)
650                 return err;
651
652         for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
653                 if (!strcasecmp(pm, phy_modes(i)))
654                         return i;
655
656         return -ENODEV;
657 }
658 EXPORT_SYMBOL_GPL(device_get_phy_mode);
659
660 static void *device_get_mac_addr(struct device *dev,
661                                  const char *name, char *addr,
662                                  int alen)
663 {
664         int ret = device_property_read_u8_array(dev, name, addr, alen);
665
666         if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
667                 return addr;
668         return NULL;
669 }
670
671 /**
672  * device_get_mac_address - Get the MAC for a given device
673  * @dev:        Pointer to the device
674  * @addr:       Address of buffer to store the MAC in
675  * @alen:       Length of the buffer pointed to by addr, should be ETH_ALEN
676  *
677  * Search the firmware node for the best MAC address to use.  'mac-address' is
678  * checked first, because that is supposed to contain to "most recent" MAC
679  * address. If that isn't set, then 'local-mac-address' is checked next,
680  * because that is the default address.  If that isn't set, then the obsolete
681  * 'address' is checked, just in case we're using an old device tree.
682  *
683  * Note that the 'address' property is supposed to contain a virtual address of
684  * the register set, but some DTS files have redefined that property to be the
685  * MAC address.
686  *
687  * All-zero MAC addresses are rejected, because those could be properties that
688  * exist in the firmware tables, but were not updated by the firmware.  For
689  * example, the DTS could define 'mac-address' and 'local-mac-address', with
690  * zero MAC addresses.  Some older U-Boots only initialized 'local-mac-address'.
691  * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
692  * exists but is all zeros.
693 */
694 void *device_get_mac_address(struct device *dev, char *addr, int alen)
695 {
696         char *res;
697
698         res = device_get_mac_addr(dev, "mac-address", addr, alen);
699         if (res)
700                 return res;
701
702         res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
703         if (res)
704                 return res;
705
706         return device_get_mac_addr(dev, "address", addr, alen);
707 }
708 EXPORT_SYMBOL(device_get_mac_address);