2 * ACPI device specific properties support.
4 * Copyright (C) 2014, Intel Corporation
7 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
8 * Darren Hart <dvhart@linux.intel.com>
9 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/acpi.h>
17 #include <linux/device.h>
18 #include <linux/export.h>
22 /* ACPI _DSD device properties UUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
23 static const u8 prp_uuid[16] = {
24 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d,
25 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01
28 static bool acpi_property_value_ok(const union acpi_object *value)
33 * The value must be an integer, a string, a reference, or a package
34 * whose every element must be an integer, a string, or a reference.
36 switch (value->type) {
37 case ACPI_TYPE_INTEGER:
38 case ACPI_TYPE_STRING:
39 case ACPI_TYPE_LOCAL_REFERENCE:
42 case ACPI_TYPE_PACKAGE:
43 for (j = 0; j < value->package.count; j++)
44 switch (value->package.elements[j].type) {
45 case ACPI_TYPE_INTEGER:
46 case ACPI_TYPE_STRING:
47 case ACPI_TYPE_LOCAL_REFERENCE:
59 static bool acpi_properties_format_valid(const union acpi_object *properties)
63 for (i = 0; i < properties->package.count; i++) {
64 const union acpi_object *property;
66 property = &properties->package.elements[i];
68 * Only two elements allowed, the first one must be a string and
69 * the second one has to satisfy certain conditions.
71 if (property->package.count != 2
72 || property->package.elements[0].type != ACPI_TYPE_STRING
73 || !acpi_property_value_ok(&property->package.elements[1]))
79 static void acpi_init_of_compatible(struct acpi_device *adev)
81 const union acpi_object *of_compatible;
82 struct acpi_hardware_id *hwid;
87 * Check if the special PRP0001 ACPI ID is present and in that
88 * case we fill in Device Tree compatible properties for this
91 list_for_each_entry(hwid, &adev->pnp.ids, list) {
92 if (!strcmp(hwid->id, "PRP0001")) {
101 ret = acpi_dev_get_property_array(adev, "compatible", ACPI_TYPE_STRING,
104 ret = acpi_dev_get_property(adev, "compatible",
105 ACPI_TYPE_STRING, &of_compatible);
107 acpi_handle_warn(adev->handle,
108 "PRP0001 requires compatible property\n");
112 adev->data.of_compatible = of_compatible;
115 void acpi_init_properties(struct acpi_device *adev)
117 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
118 const union acpi_object *desc;
122 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
124 if (ACPI_FAILURE(status))
128 if (desc->package.count % 2)
131 /* Look for the device properties UUID. */
132 for (i = 0; i < desc->package.count; i += 2) {
133 const union acpi_object *uuid, *properties;
135 uuid = &desc->package.elements[i];
136 properties = &desc->package.elements[i + 1];
139 * The first element must be a UUID and the second one must be
142 if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
143 || properties->type != ACPI_TYPE_PACKAGE)
146 if (memcmp(uuid->buffer.pointer, prp_uuid, sizeof(prp_uuid)))
150 * We found the matching UUID. Now validate the format of the
151 * package immediately following it.
153 if (!acpi_properties_format_valid(properties))
156 adev->data.pointer = buf.pointer;
157 adev->data.properties = properties;
159 acpi_init_of_compatible(adev);
164 dev_warn(&adev->dev, "Returned _DSD data is not valid, skipping\n");
165 ACPI_FREE(buf.pointer);
168 void acpi_free_properties(struct acpi_device *adev)
170 ACPI_FREE((void *)adev->data.pointer);
171 adev->data.of_compatible = NULL;
172 adev->data.pointer = NULL;
173 adev->data.properties = NULL;
177 * acpi_dev_get_property - return an ACPI property with given name
178 * @adev: ACPI device to get property
179 * @name: Name of the property
180 * @type: Expected property type
181 * @obj: Location to store the property value (if not %NULL)
183 * Look up a property with @name and store a pointer to the resulting ACPI
184 * object at the location pointed to by @obj if found.
186 * Callers must not attempt to free the returned objects. These objects will be
187 * freed by the ACPI core automatically during the removal of @adev.
189 * Return: %0 if property with @name has been found (success),
190 * %-EINVAL if the arguments are invalid,
191 * %-ENODATA if the property doesn't exist,
192 * %-EPROTO if the property value type doesn't match @type.
194 int acpi_dev_get_property(struct acpi_device *adev, const char *name,
195 acpi_object_type type, const union acpi_object **obj)
197 const union acpi_object *properties;
203 if (!adev->data.pointer || !adev->data.properties)
206 properties = adev->data.properties;
207 for (i = 0; i < properties->package.count; i++) {
208 const union acpi_object *propname, *propvalue;
209 const union acpi_object *property;
211 property = &properties->package.elements[i];
213 propname = &property->package.elements[0];
214 propvalue = &property->package.elements[1];
216 if (!strcmp(name, propname->string.pointer)) {
217 if (type != ACPI_TYPE_ANY && propvalue->type != type)
227 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
230 * acpi_dev_get_property_array - return an ACPI array property with given name
231 * @adev: ACPI device to get property
232 * @name: Name of the property
233 * @type: Expected type of array elements
234 * @obj: Location to store a pointer to the property value (if not NULL)
236 * Look up an array property with @name and store a pointer to the resulting
237 * ACPI object at the location pointed to by @obj if found.
239 * Callers must not attempt to free the returned objects. Those objects will be
240 * freed by the ACPI core automatically during the removal of @adev.
242 * Return: %0 if array property (package) with @name has been found (success),
243 * %-EINVAL if the arguments are invalid,
244 * %-ENODATA if the property doesn't exist,
245 * %-EPROTO if the property is not a package or the type of its elements
246 * doesn't match @type.
248 int acpi_dev_get_property_array(struct acpi_device *adev, const char *name,
249 acpi_object_type type,
250 const union acpi_object **obj)
252 const union acpi_object *prop;
255 ret = acpi_dev_get_property(adev, name, ACPI_TYPE_PACKAGE, &prop);
259 if (type != ACPI_TYPE_ANY) {
260 /* Check that all elements are of correct type. */
261 for (i = 0; i < prop->package.count; i++)
262 if (prop->package.elements[i].type != type)
270 EXPORT_SYMBOL_GPL(acpi_dev_get_property_array);
273 * acpi_dev_get_property_reference - returns handle to the referenced object
274 * @adev: ACPI device to get property
275 * @name: Name of the property
276 * @index: Index of the reference to return
277 * @args: Location to store the returned reference with optional arguments
279 * Find property with @name, verifify that it is a package containing at least
280 * one object reference and if so, store the ACPI device object pointer to the
281 * target object in @args->adev. If the reference includes arguments, store
282 * them in the @args->args[] array.
284 * If there's more than one reference in the property value package, @index is
285 * used to select the one to return.
287 * Return: %0 on success, negative error code on failure.
289 int acpi_dev_get_property_reference(struct acpi_device *adev,
290 const char *name, size_t index,
291 struct acpi_reference_args *args)
293 const union acpi_object *element, *end;
294 const union acpi_object *obj;
295 struct acpi_device *device;
298 ret = acpi_dev_get_property(adev, name, ACPI_TYPE_ANY, &obj);
303 * The simplest case is when the value is a single reference. Just
304 * return that reference then.
306 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
310 ret = acpi_bus_get_device(obj->reference.handle, &device);
320 * If it is not a single reference, then it is a package of
321 * references followed by number of ints as follows:
323 * Package () { REF, INT, REF, INT, INT }
325 * The index argument is then used to determine which reference
326 * the caller wants (along with the arguments).
328 if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
331 element = obj->package.elements;
332 end = element + obj->package.count;
334 while (element < end) {
337 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
340 ret = acpi_bus_get_device(element->reference.handle, &device);
347 /* assume following integer elements are all args */
348 for (i = 0; element + i < end; i++) {
349 int type = element[i].type;
351 if (type == ACPI_TYPE_INTEGER)
353 else if (type == ACPI_TYPE_LOCAL_REFERENCE)
359 if (idx++ == index) {
362 for (i = 0; i < nargs; i++)
363 args->args[i] = element[i].integer.value;
373 EXPORT_SYMBOL_GPL(acpi_dev_get_property_reference);
375 int acpi_dev_prop_get(struct acpi_device *adev, const char *propname,
378 return acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
379 (const union acpi_object **)valptr);
382 int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
383 enum dev_prop_type proptype, void *val)
385 const union acpi_object *obj;
391 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
392 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_INTEGER, &obj);
398 if (obj->integer.value > U8_MAX)
400 *(u8 *)val = obj->integer.value;
403 if (obj->integer.value > U16_MAX)
405 *(u16 *)val = obj->integer.value;
408 if (obj->integer.value > U32_MAX)
410 *(u32 *)val = obj->integer.value;
413 *(u64 *)val = obj->integer.value;
416 } else if (proptype == DEV_PROP_STRING) {
417 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_STRING, &obj);
421 *(char **)val = obj->string.pointer;
428 static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
433 for (i = 0; i < nval; i++) {
434 if (items[i].type != ACPI_TYPE_INTEGER)
436 if (items[i].integer.value > U8_MAX)
439 val[i] = items[i].integer.value;
444 static int acpi_copy_property_array_u16(const union acpi_object *items,
445 u16 *val, size_t nval)
449 for (i = 0; i < nval; i++) {
450 if (items[i].type != ACPI_TYPE_INTEGER)
452 if (items[i].integer.value > U16_MAX)
455 val[i] = items[i].integer.value;
460 static int acpi_copy_property_array_u32(const union acpi_object *items,
461 u32 *val, size_t nval)
465 for (i = 0; i < nval; i++) {
466 if (items[i].type != ACPI_TYPE_INTEGER)
468 if (items[i].integer.value > U32_MAX)
471 val[i] = items[i].integer.value;
476 static int acpi_copy_property_array_u64(const union acpi_object *items,
477 u64 *val, size_t nval)
481 for (i = 0; i < nval; i++) {
482 if (items[i].type != ACPI_TYPE_INTEGER)
485 val[i] = items[i].integer.value;
490 static int acpi_copy_property_array_string(const union acpi_object *items,
491 char **val, size_t nval)
495 for (i = 0; i < nval; i++) {
496 if (items[i].type != ACPI_TYPE_STRING)
499 val[i] = items[i].string.pointer;
504 int acpi_dev_prop_read(struct acpi_device *adev, const char *propname,
505 enum dev_prop_type proptype, void *val, size_t nval)
507 const union acpi_object *obj;
508 const union acpi_object *items;
511 if (val && nval == 1) {
512 ret = acpi_dev_prop_read_single(adev, propname, proptype, val);
517 ret = acpi_dev_get_property_array(adev, propname, ACPI_TYPE_ANY, &obj);
522 return obj->package.count;
526 if (nval > obj->package.count)
529 items = obj->package.elements;
532 ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
535 ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
538 ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
541 ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
543 case DEV_PROP_STRING:
544 ret = acpi_copy_property_array_string(items, (char **)val, nval);