[media] s5h1420: fix a buffer overflow when checking userspace params
[linux-2.6/btrfs-unstable.git] / drivers / base / property.c
blob1d0b116cae959041682eaf6bb7ea91895b390e58
1 /*
2 * property.c - Unified device property interface.
4 * Copyright (C) 2014, Intel Corporation
5 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
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.
13 #include <linux/acpi.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
16 #include <linux/of.h>
17 #include <linux/property.h>
19 /**
20 * device_add_property_set - Add a collection of properties to a device object.
21 * @dev: Device to add properties to.
22 * @pset: Collection of properties to add.
24 * Associate a collection of device properties represented by @pset with @dev
25 * as its secondary firmware node.
27 void device_add_property_set(struct device *dev, struct property_set *pset)
29 if (pset)
30 pset->fwnode.type = FWNODE_PDATA;
32 set_secondary_fwnode(dev, &pset->fwnode);
34 EXPORT_SYMBOL_GPL(device_add_property_set);
36 static inline bool is_pset(struct fwnode_handle *fwnode)
38 return fwnode && fwnode->type == FWNODE_PDATA;
41 static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
43 return is_pset(fwnode) ?
44 container_of(fwnode, struct property_set, fwnode) : NULL;
47 static struct property_entry *pset_prop_get(struct property_set *pset,
48 const char *name)
50 struct property_entry *prop;
52 if (!pset || !pset->properties)
53 return NULL;
55 for (prop = pset->properties; prop->name; prop++)
56 if (!strcmp(name, prop->name))
57 return prop;
59 return NULL;
62 static int pset_prop_read_array(struct property_set *pset, const char *name,
63 enum dev_prop_type type, void *val, size_t nval)
65 struct property_entry *prop;
66 unsigned int item_size;
68 prop = pset_prop_get(pset, name);
69 if (!prop)
70 return -ENODATA;
72 if (prop->type != type)
73 return -EPROTO;
75 if (!val)
76 return prop->nval;
78 if (prop->nval < nval)
79 return -EOVERFLOW;
81 switch (type) {
82 case DEV_PROP_U8:
83 item_size = sizeof(u8);
84 break;
85 case DEV_PROP_U16:
86 item_size = sizeof(u16);
87 break;
88 case DEV_PROP_U32:
89 item_size = sizeof(u32);
90 break;
91 case DEV_PROP_U64:
92 item_size = sizeof(u64);
93 break;
94 case DEV_PROP_STRING:
95 item_size = sizeof(const char *);
96 break;
97 default:
98 return -EINVAL;
100 memcpy(val, prop->value.raw_data, nval * item_size);
101 return 0;
104 static inline struct fwnode_handle *dev_fwnode(struct device *dev)
106 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
107 &dev->of_node->fwnode : dev->fwnode;
111 * device_property_present - check if a property of a device is present
112 * @dev: Device whose property is being checked
113 * @propname: Name of the property
115 * Check if property @propname is present in the device firmware description.
117 bool device_property_present(struct device *dev, const char *propname)
119 return fwnode_property_present(dev_fwnode(dev), propname);
121 EXPORT_SYMBOL_GPL(device_property_present);
124 * fwnode_property_present - check if a property of a firmware node is present
125 * @fwnode: Firmware node whose property to check
126 * @propname: Name of the property
128 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
130 if (is_of_node(fwnode))
131 return of_property_read_bool(of_node(fwnode), propname);
132 else if (is_acpi_node(fwnode))
133 return !acpi_dev_prop_get(acpi_node(fwnode), propname, NULL);
135 return !!pset_prop_get(to_pset(fwnode), propname);
137 EXPORT_SYMBOL_GPL(fwnode_property_present);
140 * device_property_read_u8_array - return a u8 array property of a device
141 * @dev: Device to get the property of
142 * @propname: Name of the property
143 * @val: The values are stored here or %NULL to return the number of values
144 * @nval: Size of the @val array
146 * Function reads an array of u8 properties with @propname from the device
147 * firmware description and stores them to @val if found.
149 * Return: number of values if @val was %NULL,
150 * %0 if the property was found (success),
151 * %-EINVAL if given arguments are not valid,
152 * %-ENODATA if the property does not have a value,
153 * %-EPROTO if the property is not an array of numbers,
154 * %-EOVERFLOW if the size of the property is not as expected.
156 int device_property_read_u8_array(struct device *dev, const char *propname,
157 u8 *val, size_t nval)
159 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
161 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
164 * device_property_read_u16_array - return a u16 array property of a device
165 * @dev: Device to get the property of
166 * @propname: Name of the property
167 * @val: The values are stored here or %NULL to return the number of values
168 * @nval: Size of the @val array
170 * Function reads an array of u16 properties with @propname from the device
171 * firmware description and stores them to @val if found.
173 * Return: number of values if @val was %NULL,
174 * %0 if the property was found (success),
175 * %-EINVAL if given arguments are not valid,
176 * %-ENODATA if the property does not have a value,
177 * %-EPROTO if the property is not an array of numbers,
178 * %-EOVERFLOW if the size of the property is not as expected.
180 int device_property_read_u16_array(struct device *dev, const char *propname,
181 u16 *val, size_t nval)
183 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
185 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
188 * device_property_read_u32_array - return a u32 array property of a device
189 * @dev: Device to get the property of
190 * @propname: Name of the property
191 * @val: The values are stored here or %NULL to return the number of values
192 * @nval: Size of the @val array
194 * Function reads an array of u32 properties with @propname from the device
195 * firmware description and stores them to @val if found.
197 * Return: number of values if @val was %NULL,
198 * %0 if the property was found (success),
199 * %-EINVAL if given arguments are not valid,
200 * %-ENODATA if the property does not have a value,
201 * %-EPROTO if the property is not an array of numbers,
202 * %-EOVERFLOW if the size of the property is not as expected.
204 int device_property_read_u32_array(struct device *dev, const char *propname,
205 u32 *val, size_t nval)
207 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
209 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
212 * device_property_read_u64_array - return a u64 array property of a device
213 * @dev: Device to get the property of
214 * @propname: Name of the property
215 * @val: The values are stored here or %NULL to return the number of values
216 * @nval: Size of the @val array
218 * Function reads an array of u64 properties with @propname from the device
219 * firmware description and stores them to @val if found.
221 * Return: number of values if @val was %NULL,
222 * %0 if the property was found (success),
223 * %-EINVAL if given arguments are not valid,
224 * %-ENODATA if the property does not have a value,
225 * %-EPROTO if the property is not an array of numbers,
226 * %-EOVERFLOW if the size of the property is not as expected.
228 int device_property_read_u64_array(struct device *dev, const char *propname,
229 u64 *val, size_t nval)
231 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
233 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
236 * device_property_read_string_array - return a string array property of 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
242 * Function reads an array of string properties with @propname from the device
243 * firmware description and stores them to @val if found.
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 or %-EILSEQ if the property is not an array of strings,
250 * %-EOVERFLOW if the size of the property is not as expected.
252 int device_property_read_string_array(struct device *dev, const char *propname,
253 const char **val, size_t nval)
255 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
257 EXPORT_SYMBOL_GPL(device_property_read_string_array);
260 * device_property_read_string - return a string property of a device
261 * @dev: Device to get the property of
262 * @propname: Name of the property
263 * @val: The value is stored here
265 * Function reads property @propname from the device firmware description and
266 * stores the value into @val if found. The value is checked to be a string.
268 * Return: %0 if the property was found (success),
269 * %-EINVAL if given arguments are not valid,
270 * %-ENODATA if the property does not have a value,
271 * %-EPROTO or %-EILSEQ if the property type is not a string.
273 int device_property_read_string(struct device *dev, const char *propname,
274 const char **val)
276 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
278 EXPORT_SYMBOL_GPL(device_property_read_string);
280 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
281 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
282 : of_property_count_elems_of_size((node), (propname), sizeof(type))
284 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
285 ({ \
286 int _ret_; \
287 if (is_of_node(_fwnode_)) \
288 _ret_ = OF_DEV_PROP_READ_ARRAY(of_node(_fwnode_), _propname_, \
289 _type_, _val_, _nval_); \
290 else if (is_acpi_node(_fwnode_)) \
291 _ret_ = acpi_dev_prop_read(acpi_node(_fwnode_), _propname_, \
292 _proptype_, _val_, _nval_); \
293 else \
294 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
295 _proptype_, _val_, _nval_); \
296 _ret_; \
300 * fwnode_property_read_u8_array - return a u8 array property of firmware node
301 * @fwnode: Firmware node to get the property of
302 * @propname: Name of the property
303 * @val: The values are stored here or %NULL to return the number of values
304 * @nval: Size of the @val array
306 * Read an array of u8 properties with @propname from @fwnode and stores them to
307 * @val if found.
309 * Return: number of values if @val was %NULL,
310 * %0 if the property was found (success),
311 * %-EINVAL if given arguments are not valid,
312 * %-ENODATA if the property does not have a value,
313 * %-EPROTO if the property is not an array of numbers,
314 * %-EOVERFLOW if the size of the property is not as expected,
315 * %-ENXIO if no suitable firmware interface is present.
317 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
318 const char *propname, u8 *val, size_t nval)
320 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
321 val, nval);
323 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
326 * fwnode_property_read_u16_array - return a u16 array property of firmware node
327 * @fwnode: Firmware node to get the property of
328 * @propname: Name of the property
329 * @val: The values are stored here or %NULL to return the number of values
330 * @nval: Size of the @val array
332 * Read an array of u16 properties with @propname from @fwnode and store them to
333 * @val if found.
335 * Return: number of values if @val was %NULL,
336 * %0 if the property was found (success),
337 * %-EINVAL if given arguments are not valid,
338 * %-ENODATA if the property does not have a value,
339 * %-EPROTO if the property is not an array of numbers,
340 * %-EOVERFLOW if the size of the property is not as expected,
341 * %-ENXIO if no suitable firmware interface is present.
343 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
344 const char *propname, u16 *val, size_t nval)
346 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
347 val, nval);
349 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
352 * fwnode_property_read_u32_array - return a u32 array property of firmware node
353 * @fwnode: Firmware node to get the property of
354 * @propname: Name of the property
355 * @val: The values are stored here or %NULL to return the number of values
356 * @nval: Size of the @val array
358 * Read an array of u32 properties with @propname from @fwnode store them to
359 * @val if found.
361 * Return: number of values if @val was %NULL,
362 * %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 if the property is not an array of numbers,
366 * %-EOVERFLOW if the size of the property is not as expected,
367 * %-ENXIO if no suitable firmware interface is present.
369 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
370 const char *propname, u32 *val, size_t nval)
372 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
373 val, nval);
375 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
378 * fwnode_property_read_u64_array - return a u64 array property firmware node
379 * @fwnode: Firmware node to get the property of
380 * @propname: Name of the property
381 * @val: The values are stored here or %NULL to return the number of values
382 * @nval: Size of the @val array
384 * Read an array of u64 properties with @propname from @fwnode and store them to
385 * @val if found.
387 * Return: number of values if @val was %NULL,
388 * %0 if the property was found (success),
389 * %-EINVAL if given arguments are not valid,
390 * %-ENODATA if the property does not have a value,
391 * %-EPROTO if the property is not an array of numbers,
392 * %-EOVERFLOW if the size of the property is not as expected,
393 * %-ENXIO if no suitable firmware interface is present.
395 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
396 const char *propname, u64 *val, size_t nval)
398 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
399 val, nval);
401 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
404 * fwnode_property_read_string_array - return string array property of a node
405 * @fwnode: Firmware node to get the property of
406 * @propname: Name of the property
407 * @val: The values are stored here or %NULL to return the number of values
408 * @nval: Size of the @val array
410 * Read an string list property @propname from the given firmware node and store
411 * them to @val if found.
413 * Return: number of values if @val was %NULL,
414 * %0 if the property was found (success),
415 * %-EINVAL if given arguments are not valid,
416 * %-ENODATA if the property does not have a value,
417 * %-EPROTO if the property is not an array of strings,
418 * %-EOVERFLOW if the size of the property is not as expected,
419 * %-ENXIO if no suitable firmware interface is present.
421 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
422 const char *propname, const char **val,
423 size_t nval)
425 if (is_of_node(fwnode))
426 return val ?
427 of_property_read_string_array(of_node(fwnode), propname,
428 val, nval) :
429 of_property_count_strings(of_node(fwnode), propname);
430 else if (is_acpi_node(fwnode))
431 return acpi_dev_prop_read(acpi_node(fwnode), propname,
432 DEV_PROP_STRING, val, nval);
434 return pset_prop_read_array(to_pset(fwnode), propname,
435 DEV_PROP_STRING, val, nval);
437 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
440 * fwnode_property_read_string - return a string property of a firmware node
441 * @fwnode: Firmware node to get the property of
442 * @propname: Name of the property
443 * @val: The value is stored here
445 * Read property @propname from the given firmware node and store the value into
446 * @val if found. The value is checked to be a string.
448 * Return: %0 if the property was found (success),
449 * %-EINVAL if given arguments are not valid,
450 * %-ENODATA if the property does not have a value,
451 * %-EPROTO or %-EILSEQ if the property is not a string,
452 * %-ENXIO if no suitable firmware interface is present.
454 int fwnode_property_read_string(struct fwnode_handle *fwnode,
455 const char *propname, const char **val)
457 if (is_of_node(fwnode))
458 return of_property_read_string(of_node(fwnode), propname, val);
459 else if (is_acpi_node(fwnode))
460 return acpi_dev_prop_read(acpi_node(fwnode), propname,
461 DEV_PROP_STRING, val, 1);
463 return -ENXIO;
465 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
468 * device_get_next_child_node - Return the next child node handle for a device
469 * @dev: Device to find the next child node for.
470 * @child: Handle to one of the device's child nodes or a null handle.
472 struct fwnode_handle *device_get_next_child_node(struct device *dev,
473 struct fwnode_handle *child)
475 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
476 struct device_node *node;
478 node = of_get_next_available_child(dev->of_node, of_node(child));
479 if (node)
480 return &node->fwnode;
481 } else if (IS_ENABLED(CONFIG_ACPI)) {
482 struct acpi_device *node;
484 node = acpi_get_next_child(dev, acpi_node(child));
485 if (node)
486 return acpi_fwnode_handle(node);
488 return NULL;
490 EXPORT_SYMBOL_GPL(device_get_next_child_node);
493 * fwnode_handle_put - Drop reference to a device node
494 * @fwnode: Pointer to the device node to drop the reference to.
496 * This has to be used when terminating device_for_each_child_node() iteration
497 * with break or return to prevent stale device node references from being left
498 * behind.
500 void fwnode_handle_put(struct fwnode_handle *fwnode)
502 if (is_of_node(fwnode))
503 of_node_put(of_node(fwnode));
505 EXPORT_SYMBOL_GPL(fwnode_handle_put);
508 * device_get_child_node_count - return the number of child nodes for device
509 * @dev: Device to cound the child nodes for
511 unsigned int device_get_child_node_count(struct device *dev)
513 struct fwnode_handle *child;
514 unsigned int count = 0;
516 device_for_each_child_node(dev, child)
517 count++;
519 return count;
521 EXPORT_SYMBOL_GPL(device_get_child_node_count);