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
27 /* ACPI _DSD data subnodes UUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
28 static const u8 ads_uuid[16] = {
29 0xe6, 0xe3, 0xb8, 0xdb, 0x86, 0x58, 0xa6, 0x4b,
30 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b
33 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
34 const union acpi_object *desc,
35 struct acpi_device_data *data);
36 static bool acpi_extract_properties(const union acpi_object *desc,
37 struct acpi_device_data *data);
39 static bool acpi_nondev_subnode_ok(acpi_handle scope,
40 const union acpi_object *link,
41 struct list_head *list)
43 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
44 struct acpi_data_node *dn;
48 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
52 dn->name = link->package.elements[0].string.pointer;
53 dn->fwnode.type = FWNODE_ACPI_DATA;
54 INIT_LIST_HEAD(&dn->data.subnodes);
56 status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
58 if (ACPI_FAILURE(status))
61 status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
63 if (ACPI_FAILURE(status))
66 if (acpi_extract_properties(buf.pointer, &dn->data))
69 if (acpi_enumerate_nondev_subnodes(scope, buf.pointer, &dn->data))
73 dn->data.pointer = buf.pointer;
74 list_add_tail(&dn->sibling, list);
78 acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
81 ACPI_FREE(buf.pointer);
86 static int acpi_add_nondev_subnodes(acpi_handle scope,
87 const union acpi_object *links,
88 struct list_head *list)
93 for (i = 0; i < links->package.count; i++) {
94 const union acpi_object *link;
96 link = &links->package.elements[i];
97 /* Only two elements allowed, both must be strings. */
98 if (link->package.count == 2
99 && link->package.elements[0].type == ACPI_TYPE_STRING
100 && link->package.elements[1].type == ACPI_TYPE_STRING
101 && acpi_nondev_subnode_ok(scope, link, list))
108 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
109 const union acpi_object *desc,
110 struct acpi_device_data *data)
114 /* Look for the ACPI data subnodes UUID. */
115 for (i = 0; i < desc->package.count; i += 2) {
116 const union acpi_object *uuid, *links;
118 uuid = &desc->package.elements[i];
119 links = &desc->package.elements[i + 1];
122 * The first element must be a UUID and the second one must be
125 if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
126 || links->type != ACPI_TYPE_PACKAGE)
129 if (memcmp(uuid->buffer.pointer, ads_uuid, sizeof(ads_uuid)))
132 return acpi_add_nondev_subnodes(scope, links, &data->subnodes);
138 static bool acpi_property_value_ok(const union acpi_object *value)
143 * The value must be an integer, a string, a reference, or a package
144 * whose every element must be an integer, a string, or a reference.
146 switch (value->type) {
147 case ACPI_TYPE_INTEGER:
148 case ACPI_TYPE_STRING:
149 case ACPI_TYPE_LOCAL_REFERENCE:
152 case ACPI_TYPE_PACKAGE:
153 for (j = 0; j < value->package.count; j++)
154 switch (value->package.elements[j].type) {
155 case ACPI_TYPE_INTEGER:
156 case ACPI_TYPE_STRING:
157 case ACPI_TYPE_LOCAL_REFERENCE:
169 static bool acpi_properties_format_valid(const union acpi_object *properties)
173 for (i = 0; i < properties->package.count; i++) {
174 const union acpi_object *property;
176 property = &properties->package.elements[i];
178 * Only two elements allowed, the first one must be a string and
179 * the second one has to satisfy certain conditions.
181 if (property->package.count != 2
182 || property->package.elements[0].type != ACPI_TYPE_STRING
183 || !acpi_property_value_ok(&property->package.elements[1]))
189 static void acpi_init_of_compatible(struct acpi_device *adev)
191 const union acpi_object *of_compatible;
194 ret = acpi_dev_get_property_array(adev, "compatible", ACPI_TYPE_STRING,
197 ret = acpi_dev_get_property(adev, "compatible",
198 ACPI_TYPE_STRING, &of_compatible);
201 && adev->parent->flags.of_compatible_ok)
207 adev->data.of_compatible = of_compatible;
210 adev->flags.of_compatible_ok = 1;
213 static bool acpi_extract_properties(const union acpi_object *desc,
214 struct acpi_device_data *data)
218 if (desc->package.count % 2)
221 /* Look for the device properties UUID. */
222 for (i = 0; i < desc->package.count; i += 2) {
223 const union acpi_object *uuid, *properties;
225 uuid = &desc->package.elements[i];
226 properties = &desc->package.elements[i + 1];
229 * The first element must be a UUID and the second one must be
232 if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
233 || properties->type != ACPI_TYPE_PACKAGE)
236 if (memcmp(uuid->buffer.pointer, prp_uuid, sizeof(prp_uuid)))
240 * We found the matching UUID. Now validate the format of the
241 * package immediately following it.
243 if (!acpi_properties_format_valid(properties))
246 data->properties = properties;
253 void acpi_init_properties(struct acpi_device *adev)
255 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
256 struct acpi_hardware_id *hwid;
258 bool acpi_of = false;
260 INIT_LIST_HEAD(&adev->data.subnodes);
263 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
264 * Device Tree compatible properties for this device.
266 list_for_each_entry(hwid, &adev->pnp.ids, list) {
267 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
273 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
275 if (ACPI_FAILURE(status))
278 if (acpi_extract_properties(buf.pointer, &adev->data)) {
279 adev->data.pointer = buf.pointer;
281 acpi_init_of_compatible(adev);
283 if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer, &adev->data))
284 adev->data.pointer = buf.pointer;
286 if (!adev->data.pointer) {
287 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
288 ACPI_FREE(buf.pointer);
292 if (acpi_of && !adev->flags.of_compatible_ok)
293 acpi_handle_info(adev->handle,
294 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
297 static void acpi_destroy_nondev_subnodes(struct list_head *list)
299 struct acpi_data_node *dn, *next;
301 if (list_empty(list))
304 list_for_each_entry_safe_reverse(dn, next, list, sibling) {
305 acpi_destroy_nondev_subnodes(&dn->data.subnodes);
306 wait_for_completion(&dn->kobj_done);
307 list_del(&dn->sibling);
308 ACPI_FREE((void *)dn->data.pointer);
313 void acpi_free_properties(struct acpi_device *adev)
315 acpi_destroy_nondev_subnodes(&adev->data.subnodes);
316 ACPI_FREE((void *)adev->data.pointer);
317 adev->data.of_compatible = NULL;
318 adev->data.pointer = NULL;
319 adev->data.properties = NULL;
323 * acpi_dev_get_property - return an ACPI property with given name
324 * @adev: ACPI device to get property
325 * @name: Name of the property
326 * @type: Expected property type
327 * @obj: Location to store the property value (if not %NULL)
329 * Look up a property with @name and store a pointer to the resulting ACPI
330 * object at the location pointed to by @obj if found.
332 * Callers must not attempt to free the returned objects. These objects will be
333 * freed by the ACPI core automatically during the removal of @adev.
335 * Return: %0 if property with @name has been found (success),
336 * %-EINVAL if the arguments are invalid,
337 * %-ENODATA if the property doesn't exist,
338 * %-EPROTO if the property value type doesn't match @type.
340 int acpi_dev_get_property(struct acpi_device *adev, const char *name,
341 acpi_object_type type, const union acpi_object **obj)
343 const union acpi_object *properties;
349 if (!adev->data.pointer || !adev->data.properties)
352 properties = adev->data.properties;
353 for (i = 0; i < properties->package.count; i++) {
354 const union acpi_object *propname, *propvalue;
355 const union acpi_object *property;
357 property = &properties->package.elements[i];
359 propname = &property->package.elements[0];
360 propvalue = &property->package.elements[1];
362 if (!strcmp(name, propname->string.pointer)) {
363 if (type != ACPI_TYPE_ANY && propvalue->type != type)
373 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
376 * acpi_dev_get_property_array - return an ACPI array property with given name
377 * @adev: ACPI device to get property
378 * @name: Name of the property
379 * @type: Expected type of array elements
380 * @obj: Location to store a pointer to the property value (if not NULL)
382 * Look up an array property with @name and store a pointer to the resulting
383 * ACPI object at the location pointed to by @obj if found.
385 * Callers must not attempt to free the returned objects. Those objects will be
386 * freed by the ACPI core automatically during the removal of @adev.
388 * Return: %0 if array property (package) with @name has been found (success),
389 * %-EINVAL if the arguments are invalid,
390 * %-ENODATA if the property doesn't exist,
391 * %-EPROTO if the property is not a package or the type of its elements
392 * doesn't match @type.
394 int acpi_dev_get_property_array(struct acpi_device *adev, const char *name,
395 acpi_object_type type,
396 const union acpi_object **obj)
398 const union acpi_object *prop;
401 ret = acpi_dev_get_property(adev, name, ACPI_TYPE_PACKAGE, &prop);
405 if (type != ACPI_TYPE_ANY) {
406 /* Check that all elements are of correct type. */
407 for (i = 0; i < prop->package.count; i++)
408 if (prop->package.elements[i].type != type)
416 EXPORT_SYMBOL_GPL(acpi_dev_get_property_array);
419 * acpi_dev_get_property_reference - returns handle to the referenced object
420 * @adev: ACPI device to get property
421 * @name: Name of the property
422 * @index: Index of the reference to return
423 * @args: Location to store the returned reference with optional arguments
425 * Find property with @name, verifify that it is a package containing at least
426 * one object reference and if so, store the ACPI device object pointer to the
427 * target object in @args->adev. If the reference includes arguments, store
428 * them in the @args->args[] array.
430 * If there's more than one reference in the property value package, @index is
431 * used to select the one to return.
433 * Return: %0 on success, negative error code on failure.
435 int acpi_dev_get_property_reference(struct acpi_device *adev,
436 const char *name, size_t index,
437 struct acpi_reference_args *args)
439 const union acpi_object *element, *end;
440 const union acpi_object *obj;
441 struct acpi_device *device;
444 ret = acpi_dev_get_property(adev, name, ACPI_TYPE_ANY, &obj);
449 * The simplest case is when the value is a single reference. Just
450 * return that reference then.
452 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
456 ret = acpi_bus_get_device(obj->reference.handle, &device);
466 * If it is not a single reference, then it is a package of
467 * references followed by number of ints as follows:
469 * Package () { REF, INT, REF, INT, INT }
471 * The index argument is then used to determine which reference
472 * the caller wants (along with the arguments).
474 if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
477 element = obj->package.elements;
478 end = element + obj->package.count;
480 while (element < end) {
483 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
486 ret = acpi_bus_get_device(element->reference.handle, &device);
493 /* assume following integer elements are all args */
494 for (i = 0; element + i < end; i++) {
495 int type = element[i].type;
497 if (type == ACPI_TYPE_INTEGER)
499 else if (type == ACPI_TYPE_LOCAL_REFERENCE)
505 if (idx++ == index) {
508 for (i = 0; i < nargs; i++)
509 args->args[i] = element[i].integer.value;
519 EXPORT_SYMBOL_GPL(acpi_dev_get_property_reference);
521 int acpi_dev_prop_get(struct acpi_device *adev, const char *propname,
524 return acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
525 (const union acpi_object **)valptr);
528 int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
529 enum dev_prop_type proptype, void *val)
531 const union acpi_object *obj;
537 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
538 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_INTEGER, &obj);
544 if (obj->integer.value > U8_MAX)
546 *(u8 *)val = obj->integer.value;
549 if (obj->integer.value > U16_MAX)
551 *(u16 *)val = obj->integer.value;
554 if (obj->integer.value > U32_MAX)
556 *(u32 *)val = obj->integer.value;
559 *(u64 *)val = obj->integer.value;
562 } else if (proptype == DEV_PROP_STRING) {
563 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_STRING, &obj);
567 *(char **)val = obj->string.pointer;
574 static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
579 for (i = 0; i < nval; i++) {
580 if (items[i].type != ACPI_TYPE_INTEGER)
582 if (items[i].integer.value > U8_MAX)
585 val[i] = items[i].integer.value;
590 static int acpi_copy_property_array_u16(const union acpi_object *items,
591 u16 *val, size_t nval)
595 for (i = 0; i < nval; i++) {
596 if (items[i].type != ACPI_TYPE_INTEGER)
598 if (items[i].integer.value > U16_MAX)
601 val[i] = items[i].integer.value;
606 static int acpi_copy_property_array_u32(const union acpi_object *items,
607 u32 *val, size_t nval)
611 for (i = 0; i < nval; i++) {
612 if (items[i].type != ACPI_TYPE_INTEGER)
614 if (items[i].integer.value > U32_MAX)
617 val[i] = items[i].integer.value;
622 static int acpi_copy_property_array_u64(const union acpi_object *items,
623 u64 *val, size_t nval)
627 for (i = 0; i < nval; i++) {
628 if (items[i].type != ACPI_TYPE_INTEGER)
631 val[i] = items[i].integer.value;
636 static int acpi_copy_property_array_string(const union acpi_object *items,
637 char **val, size_t nval)
641 for (i = 0; i < nval; i++) {
642 if (items[i].type != ACPI_TYPE_STRING)
645 val[i] = items[i].string.pointer;
650 int acpi_dev_prop_read(struct acpi_device *adev, const char *propname,
651 enum dev_prop_type proptype, void *val, size_t nval)
653 const union acpi_object *obj;
654 const union acpi_object *items;
657 if (val && nval == 1) {
658 ret = acpi_dev_prop_read_single(adev, propname, proptype, val);
663 ret = acpi_dev_get_property_array(adev, propname, ACPI_TYPE_ANY, &obj);
668 return obj->package.count;
670 if (nval > obj->package.count)
675 items = obj->package.elements;
679 ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
682 ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
685 ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
688 ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
690 case DEV_PROP_STRING:
691 ret = acpi_copy_property_array_string(items, (char **)val, nval);