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(const struct acpi_device_data *data,
24 acpi_object_type type,
25 const union acpi_object **obj);
27 static const guid_t prp_guids[] = {
28 /* ACPI _DSD device properties GUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
29 GUID_INIT(0xdaffd814, 0x6eba, 0x4d8c,
30 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01),
31 /* Hotplug in D3 GUID: 6211e2c0-58a3-4af3-90e1-927a4e0c55a4 */
32 GUID_INIT(0x6211e2c0, 0x58a3, 0x4af3,
33 0x90, 0xe1, 0x92, 0x7a, 0x4e, 0x0c, 0x55, 0xa4),
36 static const guid_t ads_guid =
37 GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6,
38 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b);
40 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
41 const union acpi_object *desc,
42 struct acpi_device_data *data,
43 struct fwnode_handle *parent);
44 static bool acpi_extract_properties(const union acpi_object *desc,
45 struct acpi_device_data *data);
47 static bool acpi_nondev_subnode_extract(const union acpi_object *desc,
49 const union acpi_object *link,
50 struct list_head *list,
51 struct fwnode_handle *parent)
53 struct acpi_data_node *dn;
56 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
60 dn->name = link->package.elements[0].string.pointer;
61 dn->fwnode.ops = &acpi_data_fwnode_ops;
63 INIT_LIST_HEAD(&dn->data.properties);
64 INIT_LIST_HEAD(&dn->data.subnodes);
66 result = acpi_extract_properties(desc, &dn->data);
73 * The scope for the subnode object lookup is the one of the
74 * namespace node (device) containing the object that has
75 * returned the package. That is, it's the scope of that
78 status = acpi_get_parent(handle, &scope);
79 if (ACPI_SUCCESS(status)
80 && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
83 } else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
90 dn->data.pointer = desc;
91 list_add_tail(&dn->sibling, list);
96 acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
100 static bool acpi_nondev_subnode_data_ok(acpi_handle handle,
101 const union acpi_object *link,
102 struct list_head *list,
103 struct fwnode_handle *parent)
105 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
108 status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
110 if (ACPI_FAILURE(status))
113 if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
117 ACPI_FREE(buf.pointer);
121 static bool acpi_nondev_subnode_ok(acpi_handle scope,
122 const union acpi_object *link,
123 struct list_head *list,
124 struct fwnode_handle *parent)
132 status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
134 if (ACPI_FAILURE(status))
137 return acpi_nondev_subnode_data_ok(handle, link, list, parent);
140 static int acpi_add_nondev_subnodes(acpi_handle scope,
141 const union acpi_object *links,
142 struct list_head *list,
143 struct fwnode_handle *parent)
148 for (i = 0; i < links->package.count; i++) {
149 const union acpi_object *link, *desc;
153 link = &links->package.elements[i];
154 /* Only two elements allowed. */
155 if (link->package.count != 2)
158 /* The first one must be a string. */
159 if (link->package.elements[0].type != ACPI_TYPE_STRING)
162 /* The second one may be a string, a reference or a package. */
163 switch (link->package.elements[1].type) {
164 case ACPI_TYPE_STRING:
165 result = acpi_nondev_subnode_ok(scope, link, list,
168 case ACPI_TYPE_LOCAL_REFERENCE:
169 handle = link->package.elements[1].reference.handle;
170 result = acpi_nondev_subnode_data_ok(handle, link, list,
173 case ACPI_TYPE_PACKAGE:
174 desc = &link->package.elements[1];
175 result = acpi_nondev_subnode_extract(desc, NULL, link,
188 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
189 const union acpi_object *desc,
190 struct acpi_device_data *data,
191 struct fwnode_handle *parent)
195 /* Look for the ACPI data subnodes GUID. */
196 for (i = 0; i < desc->package.count; i += 2) {
197 const union acpi_object *guid, *links;
199 guid = &desc->package.elements[i];
200 links = &desc->package.elements[i + 1];
203 * The first element must be a GUID and the second one must be
206 if (guid->type != ACPI_TYPE_BUFFER ||
207 guid->buffer.length != 16 ||
208 links->type != ACPI_TYPE_PACKAGE)
211 if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid))
214 return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
221 static bool acpi_property_value_ok(const union acpi_object *value)
226 * The value must be an integer, a string, a reference, or a package
227 * whose every element must be an integer, a string, or a reference.
229 switch (value->type) {
230 case ACPI_TYPE_INTEGER:
231 case ACPI_TYPE_STRING:
232 case ACPI_TYPE_LOCAL_REFERENCE:
235 case ACPI_TYPE_PACKAGE:
236 for (j = 0; j < value->package.count; j++)
237 switch (value->package.elements[j].type) {
238 case ACPI_TYPE_INTEGER:
239 case ACPI_TYPE_STRING:
240 case ACPI_TYPE_LOCAL_REFERENCE:
252 static bool acpi_properties_format_valid(const union acpi_object *properties)
256 for (i = 0; i < properties->package.count; i++) {
257 const union acpi_object *property;
259 property = &properties->package.elements[i];
261 * Only two elements allowed, the first one must be a string and
262 * the second one has to satisfy certain conditions.
264 if (property->package.count != 2
265 || property->package.elements[0].type != ACPI_TYPE_STRING
266 || !acpi_property_value_ok(&property->package.elements[1]))
272 static void acpi_init_of_compatible(struct acpi_device *adev)
274 const union acpi_object *of_compatible;
277 ret = acpi_data_get_property_array(&adev->data, "compatible",
278 ACPI_TYPE_STRING, &of_compatible);
280 ret = acpi_dev_get_property(adev, "compatible",
281 ACPI_TYPE_STRING, &of_compatible);
284 && adev->parent->flags.of_compatible_ok)
290 adev->data.of_compatible = of_compatible;
293 adev->flags.of_compatible_ok = 1;
296 static bool acpi_is_property_guid(const guid_t *guid)
300 for (i = 0; i < ARRAY_SIZE(prp_guids); i++) {
301 if (guid_equal(guid, &prp_guids[i]))
308 struct acpi_device_properties *
309 acpi_data_add_props(struct acpi_device_data *data, const guid_t *guid,
310 const union acpi_object *properties)
312 struct acpi_device_properties *props;
314 props = kzalloc(sizeof(*props), GFP_KERNEL);
316 INIT_LIST_HEAD(&props->list);
318 props->properties = properties;
319 list_add_tail(&props->list, &data->properties);
325 static bool acpi_extract_properties(const union acpi_object *desc,
326 struct acpi_device_data *data)
330 if (desc->package.count % 2)
333 /* Look for the device properties GUID. */
334 for (i = 0; i < desc->package.count; i += 2) {
335 const union acpi_object *guid, *properties;
337 guid = &desc->package.elements[i];
338 properties = &desc->package.elements[i + 1];
341 * The first element must be a GUID and the second one must be
344 if (guid->type != ACPI_TYPE_BUFFER ||
345 guid->buffer.length != 16 ||
346 properties->type != ACPI_TYPE_PACKAGE)
349 if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer))
353 * We found the matching GUID. Now validate the format of the
354 * package immediately following it.
356 if (!acpi_properties_format_valid(properties))
359 acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer,
363 return !list_empty(&data->properties);
366 void acpi_init_properties(struct acpi_device *adev)
368 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
369 struct acpi_hardware_id *hwid;
371 bool acpi_of = false;
373 INIT_LIST_HEAD(&adev->data.properties);
374 INIT_LIST_HEAD(&adev->data.subnodes);
380 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
381 * Device Tree compatible properties for this device.
383 list_for_each_entry(hwid, &adev->pnp.ids, list) {
384 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
390 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
392 if (ACPI_FAILURE(status))
395 if (acpi_extract_properties(buf.pointer, &adev->data)) {
396 adev->data.pointer = buf.pointer;
398 acpi_init_of_compatible(adev);
400 if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
401 &adev->data, acpi_fwnode_handle(adev)))
402 adev->data.pointer = buf.pointer;
404 if (!adev->data.pointer) {
405 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
406 ACPI_FREE(buf.pointer);
410 if (acpi_of && !adev->flags.of_compatible_ok)
411 acpi_handle_info(adev->handle,
412 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
414 if (!adev->data.pointer)
415 acpi_extract_apple_properties(adev);
418 static void acpi_destroy_nondev_subnodes(struct list_head *list)
420 struct acpi_data_node *dn, *next;
422 if (list_empty(list))
425 list_for_each_entry_safe_reverse(dn, next, list, sibling) {
426 acpi_destroy_nondev_subnodes(&dn->data.subnodes);
427 wait_for_completion(&dn->kobj_done);
428 list_del(&dn->sibling);
429 ACPI_FREE((void *)dn->data.pointer);
434 void acpi_free_properties(struct acpi_device *adev)
436 struct acpi_device_properties *props, *tmp;
438 acpi_destroy_nondev_subnodes(&adev->data.subnodes);
439 ACPI_FREE((void *)adev->data.pointer);
440 adev->data.of_compatible = NULL;
441 adev->data.pointer = NULL;
442 list_for_each_entry_safe(props, tmp, &adev->data.properties, list) {
443 list_del(&props->list);
449 * acpi_data_get_property - return an ACPI property with given name
450 * @data: ACPI device deta object to get the property from
451 * @name: Name of the property
452 * @type: Expected property type
453 * @obj: Location to store the property value (if not %NULL)
455 * Look up a property with @name and store a pointer to the resulting ACPI
456 * object at the location pointed to by @obj if found.
458 * Callers must not attempt to free the returned objects. These objects will be
459 * freed by the ACPI core automatically during the removal of @data.
461 * Return: %0 if property with @name has been found (success),
462 * %-EINVAL if the arguments are invalid,
463 * %-EINVAL if the property doesn't exist,
464 * %-EPROTO if the property value type doesn't match @type.
466 static int acpi_data_get_property(const struct acpi_device_data *data,
467 const char *name, acpi_object_type type,
468 const union acpi_object **obj)
470 const struct acpi_device_properties *props;
475 if (!data->pointer || list_empty(&data->properties))
478 list_for_each_entry(props, &data->properties, list) {
479 const union acpi_object *properties;
482 properties = props->properties;
483 for (i = 0; i < properties->package.count; i++) {
484 const union acpi_object *propname, *propvalue;
485 const union acpi_object *property;
487 property = &properties->package.elements[i];
489 propname = &property->package.elements[0];
490 propvalue = &property->package.elements[1];
492 if (!strcmp(name, propname->string.pointer)) {
493 if (type != ACPI_TYPE_ANY &&
494 propvalue->type != type)
507 * acpi_dev_get_property - return an ACPI property with given name.
508 * @adev: ACPI device to get the property from.
509 * @name: Name of the property.
510 * @type: Expected property type.
511 * @obj: Location to store the property value (if not %NULL).
513 int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
514 acpi_object_type type, const union acpi_object **obj)
516 return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
518 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
520 static const struct acpi_device_data *
521 acpi_device_data_of_node(const struct fwnode_handle *fwnode)
523 if (is_acpi_device_node(fwnode)) {
524 const struct acpi_device *adev = to_acpi_device_node(fwnode);
526 } else if (is_acpi_data_node(fwnode)) {
527 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
534 * acpi_node_prop_get - return an ACPI property with given name.
535 * @fwnode: Firmware node to get the property from.
536 * @propname: Name of the property.
537 * @valptr: Location to store a pointer to the property value (if not %NULL).
539 int acpi_node_prop_get(const struct fwnode_handle *fwnode,
540 const char *propname, void **valptr)
542 return acpi_data_get_property(acpi_device_data_of_node(fwnode),
543 propname, ACPI_TYPE_ANY,
544 (const union acpi_object **)valptr);
548 * acpi_data_get_property_array - return an ACPI array property with given name
549 * @adev: ACPI data object to get the property from
550 * @name: Name of the property
551 * @type: Expected type of array elements
552 * @obj: Location to store a pointer to the property value (if not NULL)
554 * Look up an array property with @name and store a pointer to the resulting
555 * ACPI object at the location pointed to by @obj if found.
557 * Callers must not attempt to free the returned objects. Those objects will be
558 * freed by the ACPI core automatically during the removal of @data.
560 * Return: %0 if array property (package) with @name has been found (success),
561 * %-EINVAL if the arguments are invalid,
562 * %-EINVAL if the property doesn't exist,
563 * %-EPROTO if the property is not a package or the type of its elements
564 * doesn't match @type.
566 static int acpi_data_get_property_array(const struct acpi_device_data *data,
568 acpi_object_type type,
569 const union acpi_object **obj)
571 const union acpi_object *prop;
574 ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
578 if (type != ACPI_TYPE_ANY) {
579 /* Check that all elements are of correct type. */
580 for (i = 0; i < prop->package.count; i++)
581 if (prop->package.elements[i].type != type)
590 static struct fwnode_handle *
591 acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
592 const char *childname)
594 struct fwnode_handle *child;
597 * Find first matching named child node of this fwnode.
598 * For ACPI this will be a data only sub-node.
600 fwnode_for_each_child_node(fwnode, child)
601 if (acpi_data_node_match(child, childname))
608 * __acpi_node_get_property_reference - returns handle to the referenced object
609 * @fwnode: Firmware node to get the property from
610 * @propname: Name of the property
611 * @index: Index of the reference to return
612 * @num_args: Maximum number of arguments after each reference
613 * @args: Location to store the returned reference with optional arguments
615 * Find property with @name, verifify that it is a package containing at least
616 * one object reference and if so, store the ACPI device object pointer to the
617 * target object in @args->adev. If the reference includes arguments, store
618 * them in the @args->args[] array.
620 * If there's more than one reference in the property value package, @index is
621 * used to select the one to return.
623 * It is possible to leave holes in the property value set like in the
636 * Calling this function with index %2 or index %3 return %-ENOENT. If the
637 * property does not contain any more values %-ENOENT is returned. The NULL
638 * entry must be single integer and preferably contain value %0.
640 * Return: %0 on success, negative error code on failure.
642 int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
643 const char *propname, size_t index, size_t num_args,
644 struct fwnode_reference_args *args)
646 const union acpi_object *element, *end;
647 const union acpi_object *obj;
648 const struct acpi_device_data *data;
649 struct acpi_device *device;
652 data = acpi_device_data_of_node(fwnode);
656 ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
658 return ret == -EINVAL ? -ENOENT : -EINVAL;
661 * The simplest case is when the value is a single reference. Just
662 * return that reference then.
664 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
668 ret = acpi_bus_get_device(obj->reference.handle, &device);
670 return ret == -ENODEV ? -EINVAL : ret;
672 args->fwnode = acpi_fwnode_handle(device);
678 * If it is not a single reference, then it is a package of
679 * references followed by number of ints as follows:
681 * Package () { REF, INT, REF, INT, INT }
683 * The index argument is then used to determine which reference
684 * the caller wants (along with the arguments).
686 if (obj->type != ACPI_TYPE_PACKAGE)
688 if (index >= obj->package.count)
691 element = obj->package.elements;
692 end = element + obj->package.count;
694 while (element < end) {
697 if (element->type == ACPI_TYPE_LOCAL_REFERENCE) {
698 struct fwnode_handle *ref_fwnode;
700 ret = acpi_bus_get_device(element->reference.handle,
709 * Find the referred data extension node under the
710 * referred device node.
712 for (ref_fwnode = acpi_fwnode_handle(device);
713 element < end && element->type == ACPI_TYPE_STRING;
715 ref_fwnode = acpi_fwnode_get_named_child_node(
716 ref_fwnode, element->string.pointer);
721 /* assume following integer elements are all args */
722 for (i = 0; element + i < end && i < num_args; i++) {
723 int type = element[i].type;
725 if (type == ACPI_TYPE_INTEGER)
727 else if (type == ACPI_TYPE_LOCAL_REFERENCE)
733 if (nargs > NR_FWNODE_REFERENCE_ARGS)
737 args->fwnode = ref_fwnode;
739 for (i = 0; i < nargs; i++)
740 args->args[i] = element[i].integer.value;
746 } else if (element->type == ACPI_TYPE_INTEGER) {
759 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
761 static int acpi_data_prop_read_single(const struct acpi_device_data *data,
762 const char *propname,
763 enum dev_prop_type proptype, void *val)
765 const union acpi_object *obj;
771 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
772 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
778 if (obj->integer.value > U8_MAX)
780 *(u8 *)val = obj->integer.value;
783 if (obj->integer.value > U16_MAX)
785 *(u16 *)val = obj->integer.value;
788 if (obj->integer.value > U32_MAX)
790 *(u32 *)val = obj->integer.value;
793 *(u64 *)val = obj->integer.value;
796 } else if (proptype == DEV_PROP_STRING) {
797 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
801 *(char **)val = obj->string.pointer;
810 int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
811 enum dev_prop_type proptype, void *val)
818 ret = acpi_data_prop_read_single(&adev->data, propname, proptype, val);
819 if (ret < 0 || proptype != ACPI_TYPE_STRING)
824 static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
829 for (i = 0; i < nval; i++) {
830 if (items[i].type != ACPI_TYPE_INTEGER)
832 if (items[i].integer.value > U8_MAX)
835 val[i] = items[i].integer.value;
840 static int acpi_copy_property_array_u16(const union acpi_object *items,
841 u16 *val, size_t nval)
845 for (i = 0; i < nval; i++) {
846 if (items[i].type != ACPI_TYPE_INTEGER)
848 if (items[i].integer.value > U16_MAX)
851 val[i] = items[i].integer.value;
856 static int acpi_copy_property_array_u32(const union acpi_object *items,
857 u32 *val, size_t nval)
861 for (i = 0; i < nval; i++) {
862 if (items[i].type != ACPI_TYPE_INTEGER)
864 if (items[i].integer.value > U32_MAX)
867 val[i] = items[i].integer.value;
872 static int acpi_copy_property_array_u64(const union acpi_object *items,
873 u64 *val, size_t nval)
877 for (i = 0; i < nval; i++) {
878 if (items[i].type != ACPI_TYPE_INTEGER)
881 val[i] = items[i].integer.value;
886 static int acpi_copy_property_array_string(const union acpi_object *items,
887 char **val, size_t nval)
891 for (i = 0; i < nval; i++) {
892 if (items[i].type != ACPI_TYPE_STRING)
895 val[i] = items[i].string.pointer;
900 static int acpi_data_prop_read(const struct acpi_device_data *data,
901 const char *propname,
902 enum dev_prop_type proptype,
903 void *val, size_t nval)
905 const union acpi_object *obj;
906 const union acpi_object *items;
909 if (val && nval == 1) {
910 ret = acpi_data_prop_read_single(data, propname, proptype, val);
915 ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
920 return obj->package.count;
922 if (proptype != DEV_PROP_STRING && nval > obj->package.count)
927 items = obj->package.elements;
931 ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
934 ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
937 ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
940 ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
942 case DEV_PROP_STRING:
943 ret = acpi_copy_property_array_string(
945 min_t(u32, nval, obj->package.count));
954 int acpi_dev_prop_read(const struct acpi_device *adev, const char *propname,
955 enum dev_prop_type proptype, void *val, size_t nval)
957 return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL;
961 * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
962 * @fwnode: Firmware node to get the property from.
963 * @propname: Name of the property.
964 * @proptype: Expected property type.
965 * @val: Location to store the property value (if not %NULL).
966 * @nval: Size of the array pointed to by @val.
968 * If @val is %NULL, return the number of array elements comprising the value
969 * of the property. Otherwise, read at most @nval values to the array at the
970 * location pointed to by @val.
972 int acpi_node_prop_read(const struct fwnode_handle *fwnode,
973 const char *propname, enum dev_prop_type proptype,
974 void *val, size_t nval)
976 return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
977 propname, proptype, val, nval);
981 * acpi_get_next_subnode - Return the next child node handle for a fwnode
982 * @fwnode: Firmware node to find the next child node for.
983 * @child: Handle to one of the device's child nodes or a null handle.
985 struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
986 struct fwnode_handle *child)
988 const struct acpi_device *adev = to_acpi_device_node(fwnode);
989 const struct list_head *head;
990 struct list_head *next;
992 if (!child || is_acpi_device_node(child)) {
993 struct acpi_device *child_adev;
996 head = &adev->children;
1000 if (list_empty(head))
1004 adev = to_acpi_device_node(child);
1005 next = adev->node.next;
1010 child_adev = list_entry(next, struct acpi_device, node);
1012 child_adev = list_first_entry(head, struct acpi_device,
1015 return acpi_fwnode_handle(child_adev);
1019 if (!child || is_acpi_data_node(child)) {
1020 const struct acpi_data_node *data = to_acpi_data_node(fwnode);
1021 struct acpi_data_node *dn;
1024 head = &adev->data.subnodes;
1026 head = &data->data.subnodes;
1030 if (list_empty(head))
1034 dn = to_acpi_data_node(child);
1035 next = dn->sibling.next;
1039 dn = list_entry(next, struct acpi_data_node, sibling);
1041 dn = list_first_entry(head, struct acpi_data_node, sibling);
1049 * acpi_node_get_parent - Return parent fwnode of this fwnode
1050 * @fwnode: Firmware node whose parent to get
1052 * Returns parent node of an ACPI device or data firmware node or %NULL if
1055 struct fwnode_handle *acpi_node_get_parent(const struct fwnode_handle *fwnode)
1057 if (is_acpi_data_node(fwnode)) {
1058 /* All data nodes have parent pointer so just return that */
1059 return to_acpi_data_node(fwnode)->parent;
1060 } else if (is_acpi_device_node(fwnode)) {
1061 acpi_handle handle, parent_handle;
1063 handle = to_acpi_device_node(fwnode)->handle;
1064 if (ACPI_SUCCESS(acpi_get_parent(handle, &parent_handle))) {
1065 struct acpi_device *adev;
1067 if (!acpi_bus_get_device(parent_handle, &adev))
1068 return acpi_fwnode_handle(adev);
1076 * Return true if the node is an ACPI graph node. Called on either ports
1079 static bool is_acpi_graph_node(struct fwnode_handle *fwnode,
1082 unsigned int len = strlen(str);
1085 if (!len || !is_acpi_data_node(fwnode))
1088 name = to_acpi_data_node(fwnode)->name;
1090 return (fwnode_property_present(fwnode, "reg") &&
1091 !strncmp(name, str, len) && name[len] == '@') ||
1092 fwnode_property_present(fwnode, str);
1096 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
1097 * @fwnode: Pointer to the parent firmware node
1098 * @prev: Previous endpoint node or %NULL to get the first
1100 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
1101 * %NULL if there is no next endpoint or in case of error. In case of success
1102 * the next endpoint is returned.
1104 static struct fwnode_handle *acpi_graph_get_next_endpoint(
1105 const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
1107 struct fwnode_handle *port = NULL;
1108 struct fwnode_handle *endpoint;
1112 port = fwnode_get_next_child_node(fwnode, port);
1114 * The names of the port nodes begin with "port@"
1115 * followed by the number of the port node and they also
1116 * have a "reg" property that also has the number of the
1117 * port node. For compatibility reasons a node is also
1118 * recognised as a port node from the "port" property.
1120 if (is_acpi_graph_node(port, "port"))
1124 port = fwnode_get_parent(prev);
1130 endpoint = fwnode_get_next_child_node(port, prev);
1132 port = fwnode_get_next_child_node(fwnode, port);
1135 if (is_acpi_graph_node(port, "port"))
1136 endpoint = fwnode_get_next_child_node(port, NULL);
1140 * The names of the endpoint nodes begin with "endpoint@" followed by
1141 * the number of the endpoint node and they also have a "reg" property
1142 * that also has the number of the endpoint node. For compatibility
1143 * reasons a node is also recognised as an endpoint node from the
1144 * "endpoint" property.
1146 if (!is_acpi_graph_node(endpoint, "endpoint"))
1153 * acpi_graph_get_child_prop_value - Return a child with a given property value
1154 * @fwnode: device fwnode
1155 * @prop_name: The name of the property to look for
1156 * @val: the desired property value
1158 * Return the port node corresponding to a given port number. Returns
1159 * the child node on success, NULL otherwise.
1161 static struct fwnode_handle *acpi_graph_get_child_prop_value(
1162 const struct fwnode_handle *fwnode, const char *prop_name,
1165 struct fwnode_handle *child;
1167 fwnode_for_each_child_node(fwnode, child) {
1170 if (fwnode_property_read_u32(child, prop_name, &nr))
1182 * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
1183 * @fwnode: Endpoint firmware node pointing to a remote device
1184 * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
1186 * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
1188 static struct fwnode_handle *
1189 acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode)
1191 struct fwnode_handle *fwnode;
1192 unsigned int port_nr, endpoint_nr;
1193 struct fwnode_reference_args args;
1196 memset(&args, 0, sizeof(args));
1197 ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
1202 /* Direct endpoint reference? */
1203 if (!is_acpi_device_node(args.fwnode))
1204 return args.nargs ? NULL : args.fwnode;
1207 * Always require two arguments with the reference: port and
1210 if (args.nargs != 2)
1213 fwnode = args.fwnode;
1214 port_nr = args.args[0];
1215 endpoint_nr = args.args[1];
1217 fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
1219 return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr);
1222 static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
1224 if (!is_acpi_device_node(fwnode))
1227 return acpi_device_is_present(to_acpi_device_node(fwnode));
1230 static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
1231 const char *propname)
1233 return !acpi_node_prop_get(fwnode, propname, NULL);
1237 acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1238 const char *propname,
1239 unsigned int elem_size, void *val,
1242 enum dev_prop_type type;
1244 switch (elem_size) {
1249 type = DEV_PROP_U16;
1252 type = DEV_PROP_U32;
1255 type = DEV_PROP_U64;
1261 return acpi_node_prop_read(fwnode, propname, type, val, nval);
1265 acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1266 const char *propname, const char **val,
1269 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
1274 acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1275 const char *prop, const char *nargs_prop,
1276 unsigned int args_count, unsigned int index,
1277 struct fwnode_reference_args *args)
1279 return __acpi_node_get_property_reference(fwnode, prop, index,
1283 static struct fwnode_handle *
1284 acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
1286 return acpi_node_get_parent(fwnode);
1289 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1290 struct fwnode_endpoint *endpoint)
1292 struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1294 endpoint->local_fwnode = fwnode;
1296 if (fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port))
1297 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1298 if (fwnode_property_read_u32(fwnode, "reg", &endpoint->id))
1299 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1305 acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1306 const struct device *dev)
1308 return acpi_device_get_match_data(dev);
1311 #define DECLARE_ACPI_FWNODE_OPS(ops) \
1312 const struct fwnode_operations ops = { \
1313 .device_is_available = acpi_fwnode_device_is_available, \
1314 .device_get_match_data = acpi_fwnode_device_get_match_data, \
1315 .property_present = acpi_fwnode_property_present, \
1316 .property_read_int_array = \
1317 acpi_fwnode_property_read_int_array, \
1318 .property_read_string_array = \
1319 acpi_fwnode_property_read_string_array, \
1320 .get_parent = acpi_node_get_parent, \
1321 .get_next_child_node = acpi_get_next_subnode, \
1322 .get_named_child_node = acpi_fwnode_get_named_child_node, \
1323 .get_reference_args = acpi_fwnode_get_reference_args, \
1324 .graph_get_next_endpoint = \
1325 acpi_graph_get_next_endpoint, \
1326 .graph_get_remote_endpoint = \
1327 acpi_graph_get_remote_endpoint, \
1328 .graph_get_port_parent = acpi_fwnode_get_parent, \
1329 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1331 EXPORT_SYMBOL_GPL(ops)
1333 DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
1334 DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
1335 const struct fwnode_operations acpi_static_fwnode_ops;
1337 bool is_acpi_device_node(const struct fwnode_handle *fwnode)
1339 return !IS_ERR_OR_NULL(fwnode) &&
1340 fwnode->ops == &acpi_device_fwnode_ops;
1342 EXPORT_SYMBOL(is_acpi_device_node);
1344 bool is_acpi_data_node(const struct fwnode_handle *fwnode)
1346 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops;
1348 EXPORT_SYMBOL(is_acpi_data_node);