Skip to content

Commit b31384f

Browse files
committed
Driver core: Unified device properties interface for platform firmware
Add a uniform interface by which device drivers can request device properties from the platform firmware by providing a property name and the corresponding data type. The purpose of it is to help to write portable code that won't depend on any particular platform firmware interface. The following general helper functions are added: device_property_present() device_property_read_u8() device_property_read_u16() device_property_read_u32() device_property_read_u64() device_property_read_string() device_property_read_u8_array() device_property_read_u16_array() device_property_read_u32_array() device_property_read_u64_array() device_property_read_string_array() The first one allows the caller to check if the given property is present. The next 5 of them allow single-valued properties of various types to be retrieved in a uniform way. The remaining 5 are for reading properties with multiple values (arrays of either numbers or strings). The interface covers both ACPI and Device Trees. This change set includes material from Mika Westerberg and Aaron Lu. Signed-off-by: Aaron Lu <aaron.lu@intel.com> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Acked-by: Grant Likely <grant.likely@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent ffdcd95 commit b31384f

7 files changed

Lines changed: 514 additions & 1 deletion

File tree

‎drivers/acpi/property.c‎

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,181 @@ int acpi_dev_get_property_reference(struct acpi_device *adev, const char *name,
362362
return -EPROTO;
363363
}
364364
EXPORT_SYMBOL_GPL(acpi_dev_get_property_reference);
365+
366+
int acpi_dev_prop_get(struct acpi_device *adev, const char *propname,
367+
void **valptr)
368+
{
369+
return acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
370+
(const union acpi_object **)valptr);
371+
}
372+
373+
int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
374+
enum dev_prop_type proptype, void *val)
375+
{
376+
const union acpi_object *obj;
377+
int ret;
378+
379+
if (!val)
380+
return -EINVAL;
381+
382+
if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
383+
ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_INTEGER, &obj);
384+
if (ret)
385+
return ret;
386+
387+
switch (proptype) {
388+
case DEV_PROP_U8:
389+
if (obj->integer.value > U8_MAX)
390+
return -EOVERFLOW;
391+
*(u8 *)val = obj->integer.value;
392+
break;
393+
case DEV_PROP_U16:
394+
if (obj->integer.value > U16_MAX)
395+
return -EOVERFLOW;
396+
*(u16 *)val = obj->integer.value;
397+
break;
398+
case DEV_PROP_U32:
399+
if (obj->integer.value > U32_MAX)
400+
return -EOVERFLOW;
401+
*(u32 *)val = obj->integer.value;
402+
break;
403+
default:
404+
*(u64 *)val = obj->integer.value;
405+
break;
406+
}
407+
} else if (proptype == DEV_PROP_STRING) {
408+
ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_STRING, &obj);
409+
if (ret)
410+
return ret;
411+
412+
*(char **)val = obj->string.pointer;
413+
} else {
414+
ret = -EINVAL;
415+
}
416+
return ret;
417+
}
418+
419+
static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
420+
size_t nval)
421+
{
422+
int i;
423+
424+
for (i = 0; i < nval; i++) {
425+
if (items[i].type != ACPI_TYPE_INTEGER)
426+
return -EPROTO;
427+
if (items[i].integer.value > U8_MAX)
428+
return -EOVERFLOW;
429+
430+
val[i] = items[i].integer.value;
431+
}
432+
return 0;
433+
}
434+
435+
static int acpi_copy_property_array_u16(const union acpi_object *items,
436+
u16 *val, size_t nval)
437+
{
438+
int i;
439+
440+
for (i = 0; i < nval; i++) {
441+
if (items[i].type != ACPI_TYPE_INTEGER)
442+
return -EPROTO;
443+
if (items[i].integer.value > U16_MAX)
444+
return -EOVERFLOW;
445+
446+
val[i] = items[i].integer.value;
447+
}
448+
return 0;
449+
}
450+
451+
static int acpi_copy_property_array_u32(const union acpi_object *items,
452+
u32 *val, size_t nval)
453+
{
454+
int i;
455+
456+
for (i = 0; i < nval; i++) {
457+
if (items[i].type != ACPI_TYPE_INTEGER)
458+
return -EPROTO;
459+
if (items[i].integer.value > U32_MAX)
460+
return -EOVERFLOW;
461+
462+
val[i] = items[i].integer.value;
463+
}
464+
return 0;
465+
}
466+
467+
static int acpi_copy_property_array_u64(const union acpi_object *items,
468+
u64 *val, size_t nval)
469+
{
470+
int i;
471+
472+
for (i = 0; i < nval; i++) {
473+
if (items[i].type != ACPI_TYPE_INTEGER)
474+
return -EPROTO;
475+
476+
val[i] = items[i].integer.value;
477+
}
478+
return 0;
479+
}
480+
481+
static int acpi_copy_property_array_string(const union acpi_object *items,
482+
char **val, size_t nval)
483+
{
484+
int i;
485+
486+
for (i = 0; i < nval; i++) {
487+
if (items[i].type != ACPI_TYPE_STRING)
488+
return -EPROTO;
489+
490+
val[i] = items[i].string.pointer;
491+
}
492+
return 0;
493+
}
494+
495+
int acpi_dev_prop_read(struct acpi_device *adev, const char *propname,
496+
enum dev_prop_type proptype, void *val, size_t nval)
497+
{
498+
const union acpi_object *obj;
499+
const union acpi_object *items;
500+
int ret;
501+
502+
if (val && nval == 1) {
503+
ret = acpi_dev_prop_read_single(adev, propname, proptype, val);
504+
if (!ret)
505+
return ret;
506+
}
507+
508+
ret = acpi_dev_get_property_array(adev, propname, ACPI_TYPE_ANY, &obj);
509+
if (ret)
510+
return ret;
511+
512+
if (!val)
513+
return obj->package.count;
514+
else if (nval <= 0)
515+
return -EINVAL;
516+
517+
if (nval > obj->package.count)
518+
return -EOVERFLOW;
519+
520+
items = obj->package.elements;
521+
switch (proptype) {
522+
case DEV_PROP_U8:
523+
ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
524+
break;
525+
case DEV_PROP_U16:
526+
ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
527+
break;
528+
case DEV_PROP_U32:
529+
ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
530+
break;
531+
case DEV_PROP_U64:
532+
ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
533+
break;
534+
case DEV_PROP_STRING:
535+
ret = acpi_copy_property_array_string(items, (char **)val, nval);
536+
break;
537+
default:
538+
ret = -EINVAL;
539+
break;
540+
}
541+
return ret;
542+
}

‎drivers/base/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \
44
driver.o class.o platform.o \
55
cpu.o firmware.o init.o map.o devres.o \
66
attribute_container.o transport_class.o \
7-
topology.o container.o
7+
topology.o container.o property.o
88
obj-$(CONFIG_DEVTMPFS) += devtmpfs.o
99
obj-$(CONFIG_DMA_CMA) += dma-contiguous.o
1010
obj-y += power/

‎drivers/base/property.c‎

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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/property.h>
14+
#include <linux/export.h>
15+
#include <linux/acpi.h>
16+
#include <linux/of.h>
17+
18+
/**
19+
* device_property_present - check if a property of a device is present
20+
* @dev: Device whose property is being checked
21+
* @propname: Name of the property
22+
*
23+
* Check if property @propname is present in the device firmware description.
24+
*/
25+
bool device_property_present(struct device *dev, const char *propname)
26+
{
27+
if (IS_ENABLED(CONFIG_OF) && dev->of_node)
28+
return of_property_read_bool(dev->of_node, propname);
29+
30+
return !acpi_dev_prop_get(ACPI_COMPANION(dev), propname, NULL);
31+
}
32+
EXPORT_SYMBOL_GPL(device_property_present);
33+
34+
#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
35+
(val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
36+
: of_property_count_elems_of_size((node), (propname), sizeof(type))
37+
38+
#define DEV_PROP_READ_ARRAY(_dev_, _propname_, _type_, _proptype_, _val_, _nval_) \
39+
IS_ENABLED(CONFIG_OF) && _dev_->of_node ? \
40+
(OF_DEV_PROP_READ_ARRAY(_dev_->of_node, _propname_, _type_, \
41+
_val_, _nval_)) : \
42+
acpi_dev_prop_read(ACPI_COMPANION(_dev_), _propname_, \
43+
_proptype_, _val_, _nval_)
44+
45+
/**
46+
* device_property_read_u8_array - return a u8 array property of a device
47+
* @dev: Device to get the property of
48+
* @propname: Name of the property
49+
* @val: The values are stored here
50+
* @nval: Size of the @val array
51+
*
52+
* Function reads an array of u8 properties with @propname from the device
53+
* firmware description and stores them to @val if found.
54+
*
55+
* Return: %0 if the property was found (success),
56+
* %-EINVAL if given arguments are not valid,
57+
* %-ENODATA if the property does not have a value,
58+
* %-EPROTO if the property is not an array of numbers,
59+
* %-EOVERFLOW if the size of the property is not as expected.
60+
*/
61+
int device_property_read_u8_array(struct device *dev, const char *propname,
62+
u8 *val, size_t nval)
63+
{
64+
return DEV_PROP_READ_ARRAY(dev, propname, u8, DEV_PROP_U8, val, nval);
65+
}
66+
EXPORT_SYMBOL_GPL(device_property_read_u8_array);
67+
68+
/**
69+
* device_property_read_u16_array - return a u16 array property of a device
70+
* @dev: Device to get the property of
71+
* @propname: Name of the property
72+
* @val: The values are stored here
73+
* @nval: Size of the @val array
74+
*
75+
* Function reads an array of u16 properties with @propname from the device
76+
* firmware description and stores them to @val if found.
77+
*
78+
* Return: %0 if the property was found (success),
79+
* %-EINVAL if given arguments are not valid,
80+
* %-ENODATA if the property does not have a value,
81+
* %-EPROTO if the property is not an array of numbers,
82+
* %-EOVERFLOW if the size of the property is not as expected.
83+
*/
84+
int device_property_read_u16_array(struct device *dev, const char *propname,
85+
u16 *val, size_t nval)
86+
{
87+
return DEV_PROP_READ_ARRAY(dev, propname, u16, DEV_PROP_U16, val, nval);
88+
}
89+
EXPORT_SYMBOL_GPL(device_property_read_u16_array);
90+
91+
/**
92+
* device_property_read_u32_array - return a u32 array property of a device
93+
* @dev: Device to get the property of
94+
* @propname: Name of the property
95+
* @val: The values are stored here
96+
* @nval: Size of the @val array
97+
*
98+
* Function reads an array of u32 properties with @propname from the device
99+
* firmware description and stores them to @val if found.
100+
*
101+
* Return: %0 if the property was found (success),
102+
* %-EINVAL if given arguments are not valid,
103+
* %-ENODATA if the property does not have a value,
104+
* %-EPROTO if the property is not an array of numbers,
105+
* %-EOVERFLOW if the size of the property is not as expected.
106+
*/
107+
int device_property_read_u32_array(struct device *dev, const char *propname,
108+
u32 *val, size_t nval)
109+
{
110+
return DEV_PROP_READ_ARRAY(dev, propname, u32, DEV_PROP_U32, val, nval);
111+
}
112+
EXPORT_SYMBOL_GPL(device_property_read_u32_array);
113+
114+
/**
115+
* device_property_read_u64_array - return a u64 array property of a device
116+
* @dev: Device to get the property of
117+
* @propname: Name of the property
118+
* @val: The values are stored here
119+
* @nval: Size of the @val array
120+
*
121+
* Function reads an array of u64 properties with @propname from the device
122+
* firmware description and stores them to @val if found.
123+
*
124+
* Return: %0 if the property was found (success),
125+
* %-EINVAL if given arguments are not valid,
126+
* %-ENODATA if the property does not have a value,
127+
* %-EPROTO if the property is not an array of numbers,
128+
* %-EOVERFLOW if the size of the property is not as expected.
129+
*/
130+
int device_property_read_u64_array(struct device *dev, const char *propname,
131+
u64 *val, size_t nval)
132+
{
133+
return DEV_PROP_READ_ARRAY(dev, propname, u64, DEV_PROP_U64, val, nval);
134+
}
135+
EXPORT_SYMBOL_GPL(device_property_read_u64_array);
136+
137+
/**
138+
* device_property_read_string_array - return a string array property of device
139+
* @dev: Device to get the property of
140+
* @propname: Name of the property
141+
* @val: The values are stored here
142+
* @nval: Size of the @val array
143+
*
144+
* Function reads an array of string properties with @propname from the device
145+
* firmware description and stores them to @val if found.
146+
*
147+
* Return: %0 if the property was found (success),
148+
* %-EINVAL if given arguments are not valid,
149+
* %-ENODATA if the property does not have a value,
150+
* %-EPROTO or %-EILSEQ if the property is not an array of strings,
151+
* %-EOVERFLOW if the size of the property is not as expected.
152+
*/
153+
int device_property_read_string_array(struct device *dev, const char *propname,
154+
const char **val, size_t nval)
155+
{
156+
return IS_ENABLED(CONFIG_OF) && dev->of_node ?
157+
of_property_read_string_array(dev->of_node, propname, val, nval) :
158+
acpi_dev_prop_read(ACPI_COMPANION(dev), propname,
159+
DEV_PROP_STRING, val, nval);
160+
}
161+
EXPORT_SYMBOL_GPL(device_property_read_string_array);
162+
163+
/**
164+
* device_property_read_string - return a string property of a device
165+
* @dev: Device to get the property of
166+
* @propname: Name of the property
167+
* @val: The value is stored here
168+
*
169+
* Function reads property @propname from the device firmware description and
170+
* stores the value into @val if found. The value is checked to be a string.
171+
*
172+
* Return: %0 if the property was found (success),
173+
* %-EINVAL if given arguments are not valid,
174+
* %-ENODATA if the property does not have a value,
175+
* %-EPROTO or %-EILSEQ if the property type is not a string.
176+
*/
177+
int device_property_read_string(struct device *dev, const char *propname,
178+
const char **val)
179+
{
180+
return IS_ENABLED(CONFIG_OF) && dev->of_node ?
181+
of_property_read_string(dev->of_node, propname, val) :
182+
acpi_dev_prop_read(ACPI_COMPANION(dev), propname,
183+
DEV_PROP_STRING, val, 1);
184+
}
185+
EXPORT_SYMBOL_GPL(device_property_read_string);

0 commit comments

Comments
 (0)