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 static int acpi_data_get_property_array(struct acpi_device_data *data,
24 acpi_object_type type,
25 const union acpi_object **obj);
27 /* ACPI _DSD device properties UUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
28 static const u8 prp_uuid[16] = {
29 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d,
30 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01
32 /* ACPI _DSD data subnodes UUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
33 static const u8 ads_uuid[16] = {
34 0xe6, 0xe3, 0xb8, 0xdb, 0x86, 0x58, 0xa6, 0x4b,
35 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b
38 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
39 const union acpi_object *desc,
40 struct acpi_device_data *data);
41 static bool acpi_extract_properties(const union acpi_object *desc,
42 struct acpi_device_data *data);
44 static bool acpi_nondev_subnode_ok(acpi_handle scope,
45 const union acpi_object *link,
46 struct list_head *list)
48 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
49 struct acpi_data_node *dn;
53 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
57 dn->name = link->package.elements[0].string.pointer;
58 dn->fwnode.type = FWNODE_ACPI_DATA;
59 INIT_LIST_HEAD(&dn->data.subnodes);
61 status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
63 if (ACPI_FAILURE(status))
66 status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
68 if (ACPI_FAILURE(status))
71 if (acpi_extract_properties(buf.pointer, &dn->data))
74 if (acpi_enumerate_nondev_subnodes(scope, buf.pointer, &dn->data))
78 dn->data.pointer = buf.pointer;
79 list_add_tail(&dn->sibling, list);
83 acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
86 ACPI_FREE(buf.pointer);
91 static int acpi_add_nondev_subnodes(acpi_handle scope,
92 const union acpi_object *links,
93 struct list_head *list)
98 for (i = 0; i < links->package.count; i++) {
99 const union acpi_object *link;
101 link = &links->package.elements[i];
102 /* Only two elements allowed, both must be strings. */
103 if (link->package.count == 2
104 && link->package.elements[0].type == ACPI_TYPE_STRING
105 && link->package.elements[1].type == ACPI_TYPE_STRING
106 && acpi_nondev_subnode_ok(scope, link, list))
113 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
114 const union acpi_object *desc,
115 struct acpi_device_data *data)
119 /* Look for the ACPI data subnodes UUID. */
120 for (i = 0; i < desc->package.count; i += 2) {
121 const union acpi_object *uuid, *links;
123 uuid = &desc->package.elements[i];
124 links = &desc->package.elements[i + 1];
127 * The first element must be a UUID and the second one must be
130 if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
131 || links->type != ACPI_TYPE_PACKAGE)
134 if (memcmp(uuid->buffer.pointer, ads_uuid, sizeof(ads_uuid)))
137 return acpi_add_nondev_subnodes(scope, links, &data->subnodes);
143 static bool acpi_property_value_ok(const union acpi_object *value)
148 * The value must be an integer, a string, a reference, or a package
149 * whose every element must be an integer, a string, or a reference.
151 switch (value->type) {
152 case ACPI_TYPE_INTEGER:
153 case ACPI_TYPE_STRING:
154 case ACPI_TYPE_LOCAL_REFERENCE:
157 case ACPI_TYPE_PACKAGE:
158 for (j = 0; j < value->package.count; j++)
159 switch (value->package.elements[j].type) {
160 case ACPI_TYPE_INTEGER:
161 case ACPI_TYPE_STRING:
162 case ACPI_TYPE_LOCAL_REFERENCE:
174 static bool acpi_properties_format_valid(const union acpi_object *properties)
178 for (i = 0; i < properties->package.count; i++) {
179 const union acpi_object *property;
181 property = &properties->package.elements[i];
183 * Only two elements allowed, the first one must be a string and
184 * the second one has to satisfy certain conditions.
186 if (property->package.count != 2
187 || property->package.elements[0].type != ACPI_TYPE_STRING
188 || !acpi_property_value_ok(&property->package.elements[1]))
194 static void acpi_init_of_compatible(struct acpi_device *adev)
196 const union acpi_object *of_compatible;
199 ret = acpi_data_get_property_array(&adev->data, "compatible",
200 ACPI_TYPE_STRING, &of_compatible);
202 ret = acpi_dev_get_property(adev, "compatible",
203 ACPI_TYPE_STRING, &of_compatible);
206 && adev->parent->flags.of_compatible_ok)
212 adev->data.of_compatible = of_compatible;
215 adev->flags.of_compatible_ok = 1;
218 static bool acpi_extract_properties(const union acpi_object *desc,
219 struct acpi_device_data *data)
223 if (desc->package.count % 2)
226 /* Look for the device properties UUID. */
227 for (i = 0; i < desc->package.count; i += 2) {
228 const union acpi_object *uuid, *properties;
230 uuid = &desc->package.elements[i];
231 properties = &desc->package.elements[i + 1];
234 * The first element must be a UUID and the second one must be
237 if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
238 || properties->type != ACPI_TYPE_PACKAGE)
241 if (memcmp(uuid->buffer.pointer, prp_uuid, sizeof(prp_uuid)))
245 * We found the matching UUID. Now validate the format of the
246 * package immediately following it.
248 if (!acpi_properties_format_valid(properties))
251 data->properties = properties;
258 void acpi_init_properties(struct acpi_device *adev)
260 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
261 struct acpi_hardware_id *hwid;
263 bool acpi_of = false;
265 INIT_LIST_HEAD(&adev->data.subnodes);
268 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
269 * Device Tree compatible properties for this device.
271 list_for_each_entry(hwid, &adev->pnp.ids, list) {
272 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
278 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
280 if (ACPI_FAILURE(status))
283 if (acpi_extract_properties(buf.pointer, &adev->data)) {
284 adev->data.pointer = buf.pointer;
286 acpi_init_of_compatible(adev);
288 if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer, &adev->data))
289 adev->data.pointer = buf.pointer;
291 if (!adev->data.pointer) {
292 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
293 ACPI_FREE(buf.pointer);
297 if (acpi_of && !adev->flags.of_compatible_ok)
298 acpi_handle_info(adev->handle,
299 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
302 static void acpi_destroy_nondev_subnodes(struct list_head *list)
304 struct acpi_data_node *dn, *next;
306 if (list_empty(list))
309 list_for_each_entry_safe_reverse(dn, next, list, sibling) {
310 acpi_destroy_nondev_subnodes(&dn->data.subnodes);
311 wait_for_completion(&dn->kobj_done);
312 list_del(&dn->sibling);
313 ACPI_FREE((void *)dn->data.pointer);
318 void acpi_free_properties(struct acpi_device *adev)
320 acpi_destroy_nondev_subnodes(&adev->data.subnodes);
321 ACPI_FREE((void *)adev->data.pointer);
322 adev->data.of_compatible = NULL;
323 adev->data.pointer = NULL;
324 adev->data.properties = NULL;
328 * acpi_data_get_property - return an ACPI property with given name
329 * @data: ACPI device deta object to get the property from
330 * @name: Name of the property
331 * @type: Expected property type
332 * @obj: Location to store the property value (if not %NULL)
334 * Look up a property with @name and store a pointer to the resulting ACPI
335 * object at the location pointed to by @obj if found.
337 * Callers must not attempt to free the returned objects. These objects will be
338 * freed by the ACPI core automatically during the removal of @data.
340 * Return: %0 if property with @name has been found (success),
341 * %-EINVAL if the arguments are invalid,
342 * %-ENODATA if the property doesn't exist,
343 * %-EPROTO if the property value type doesn't match @type.
345 static int acpi_data_get_property(struct acpi_device_data *data,
346 const char *name, acpi_object_type type,
347 const union acpi_object **obj)
349 const union acpi_object *properties;
355 if (!data->pointer || !data->properties)
358 properties = data->properties;
359 for (i = 0; i < properties->package.count; i++) {
360 const union acpi_object *propname, *propvalue;
361 const union acpi_object *property;
363 property = &properties->package.elements[i];
365 propname = &property->package.elements[0];
366 propvalue = &property->package.elements[1];
368 if (!strcmp(name, propname->string.pointer)) {
369 if (type != ACPI_TYPE_ANY && propvalue->type != type)
381 * acpi_dev_get_property - return an ACPI property with given name.
382 * @adev: ACPI device to get the property from.
383 * @name: Name of the property.
384 * @type: Expected property type.
385 * @obj: Location to store the property value (if not %NULL).
387 int acpi_dev_get_property(struct acpi_device *adev, const char *name,
388 acpi_object_type type, const union acpi_object **obj)
390 return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
392 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
394 static struct acpi_device_data *acpi_device_data_of_node(struct fwnode_handle *fwnode)
396 if (fwnode->type == FWNODE_ACPI) {
397 struct acpi_device *adev = to_acpi_device_node(fwnode);
399 } else if (fwnode->type == FWNODE_ACPI_DATA) {
400 struct acpi_data_node *dn = to_acpi_data_node(fwnode);
407 * acpi_node_prop_get - return an ACPI property with given name.
408 * @fwnode: Firmware node to get the property from.
409 * @propname: Name of the property.
410 * @valptr: Location to store a pointer to the property value (if not %NULL).
412 int acpi_node_prop_get(struct fwnode_handle *fwnode, const char *propname,
415 return acpi_data_get_property(acpi_device_data_of_node(fwnode),
416 propname, ACPI_TYPE_ANY,
417 (const union acpi_object **)valptr);
421 * acpi_data_get_property_array - return an ACPI array property with given name
422 * @adev: ACPI data object to get the property from
423 * @name: Name of the property
424 * @type: Expected type of array elements
425 * @obj: Location to store a pointer to the property value (if not NULL)
427 * Look up an array property with @name and store a pointer to the resulting
428 * ACPI object at the location pointed to by @obj if found.
430 * Callers must not attempt to free the returned objects. Those objects will be
431 * freed by the ACPI core automatically during the removal of @data.
433 * Return: %0 if array property (package) with @name has been found (success),
434 * %-EINVAL if the arguments are invalid,
435 * %-ENODATA if the property doesn't exist,
436 * %-EPROTO if the property is not a package or the type of its elements
437 * doesn't match @type.
439 static int acpi_data_get_property_array(struct acpi_device_data *data,
441 acpi_object_type type,
442 const union acpi_object **obj)
444 const union acpi_object *prop;
447 ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
451 if (type != ACPI_TYPE_ANY) {
452 /* Check that all elements are of correct type. */
453 for (i = 0; i < prop->package.count; i++)
454 if (prop->package.elements[i].type != type)
464 * acpi_dev_get_property_reference - returns handle to the referenced object
465 * @adev: ACPI device to get property
466 * @name: Name of the property
467 * @index: Index of the reference to return
468 * @args: Location to store the returned reference with optional arguments
470 * Find property with @name, verifify that it is a package containing at least
471 * one object reference and if so, store the ACPI device object pointer to the
472 * target object in @args->adev. If the reference includes arguments, store
473 * them in the @args->args[] array.
475 * If there's more than one reference in the property value package, @index is
476 * used to select the one to return.
478 * Return: %0 on success, negative error code on failure.
480 int acpi_dev_get_property_reference(struct acpi_device *adev,
481 const char *name, size_t index,
482 struct acpi_reference_args *args)
484 const union acpi_object *element, *end;
485 const union acpi_object *obj;
486 struct acpi_device *device;
489 ret = acpi_dev_get_property(adev, name, ACPI_TYPE_ANY, &obj);
494 * The simplest case is when the value is a single reference. Just
495 * return that reference then.
497 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
501 ret = acpi_bus_get_device(obj->reference.handle, &device);
511 * If it is not a single reference, then it is a package of
512 * references followed by number of ints as follows:
514 * Package () { REF, INT, REF, INT, INT }
516 * The index argument is then used to determine which reference
517 * the caller wants (along with the arguments).
519 if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
522 element = obj->package.elements;
523 end = element + obj->package.count;
525 while (element < end) {
528 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
531 ret = acpi_bus_get_device(element->reference.handle, &device);
538 /* assume following integer elements are all args */
539 for (i = 0; element + i < end; i++) {
540 int type = element[i].type;
542 if (type == ACPI_TYPE_INTEGER)
544 else if (type == ACPI_TYPE_LOCAL_REFERENCE)
550 if (idx++ == index) {
553 for (i = 0; i < nargs; i++)
554 args->args[i] = element[i].integer.value;
564 EXPORT_SYMBOL_GPL(acpi_dev_get_property_reference);
566 static int acpi_data_prop_read_single(struct acpi_device_data *data,
567 const char *propname,
568 enum dev_prop_type proptype, void *val)
570 const union acpi_object *obj;
576 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
577 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
583 if (obj->integer.value > U8_MAX)
585 *(u8 *)val = obj->integer.value;
588 if (obj->integer.value > U16_MAX)
590 *(u16 *)val = obj->integer.value;
593 if (obj->integer.value > U32_MAX)
595 *(u32 *)val = obj->integer.value;
598 *(u64 *)val = obj->integer.value;
601 } else if (proptype == DEV_PROP_STRING) {
602 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
606 *(char **)val = obj->string.pointer;
613 int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
614 enum dev_prop_type proptype, void *val)
616 return adev ? acpi_data_prop_read_single(&adev->data, propname, proptype, val) : -EINVAL;
619 static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
624 for (i = 0; i < nval; i++) {
625 if (items[i].type != ACPI_TYPE_INTEGER)
627 if (items[i].integer.value > U8_MAX)
630 val[i] = items[i].integer.value;
635 static int acpi_copy_property_array_u16(const union acpi_object *items,
636 u16 *val, size_t nval)
640 for (i = 0; i < nval; i++) {
641 if (items[i].type != ACPI_TYPE_INTEGER)
643 if (items[i].integer.value > U16_MAX)
646 val[i] = items[i].integer.value;
651 static int acpi_copy_property_array_u32(const union acpi_object *items,
652 u32 *val, size_t nval)
656 for (i = 0; i < nval; i++) {
657 if (items[i].type != ACPI_TYPE_INTEGER)
659 if (items[i].integer.value > U32_MAX)
662 val[i] = items[i].integer.value;
667 static int acpi_copy_property_array_u64(const union acpi_object *items,
668 u64 *val, size_t nval)
672 for (i = 0; i < nval; i++) {
673 if (items[i].type != ACPI_TYPE_INTEGER)
676 val[i] = items[i].integer.value;
681 static int acpi_copy_property_array_string(const union acpi_object *items,
682 char **val, size_t nval)
686 for (i = 0; i < nval; i++) {
687 if (items[i].type != ACPI_TYPE_STRING)
690 val[i] = items[i].string.pointer;
695 static int acpi_data_prop_read(struct acpi_device_data *data,
696 const char *propname,
697 enum dev_prop_type proptype,
698 void *val, size_t nval)
700 const union acpi_object *obj;
701 const union acpi_object *items;
704 if (val && nval == 1) {
705 ret = acpi_data_prop_read_single(data, propname, proptype, val);
710 ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
715 return obj->package.count;
717 if (nval > obj->package.count)
722 items = obj->package.elements;
726 ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
729 ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
732 ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
735 ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
737 case DEV_PROP_STRING:
738 ret = acpi_copy_property_array_string(items, (char **)val, nval);
747 int acpi_dev_prop_read(struct acpi_device *adev, const char *propname,
748 enum dev_prop_type proptype, void *val, size_t nval)
750 return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL;
754 * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
755 * @fwnode: Firmware node to get the property from.
756 * @propname: Name of the property.
757 * @proptype: Expected property type.
758 * @val: Location to store the property value (if not %NULL).
759 * @nval: Size of the array pointed to by @val.
761 * If @val is %NULL, return the number of array elements comprising the value
762 * of the property. Otherwise, read at most @nval values to the array at the
763 * location pointed to by @val.
765 int acpi_node_prop_read(struct fwnode_handle *fwnode, const char *propname,
766 enum dev_prop_type proptype, void *val, size_t nval)
768 return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
769 propname, proptype, val, nval);