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
);
28 * The GUIDs here are made equivalent to each other in order to avoid extra
29 * complexity in the properties handling code, with the caveat that the
30 * kernel will accept certain combinations of GUID and properties that are
31 * not defined without a warning. For instance if any of the properties
32 * from different GUID appear in a property list of another, it will be
33 * accepted by the kernel. Firmware validation tools should catch these.
35 static const guid_t prp_guids
[] = {
36 /* ACPI _DSD device properties GUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
37 GUID_INIT(0xdaffd814, 0x6eba, 0x4d8c,
38 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01),
39 /* Hotplug in D3 GUID: 6211e2c0-58a3-4af3-90e1-927a4e0c55a4 */
40 GUID_INIT(0x6211e2c0, 0x58a3, 0x4af3,
41 0x90, 0xe1, 0x92, 0x7a, 0x4e, 0x0c, 0x55, 0xa4),
42 /* External facing port GUID: efcc06cc-73ac-4bc3-bff0-76143807c389 */
43 GUID_INIT(0xefcc06cc, 0x73ac, 0x4bc3,
44 0xbf, 0xf0, 0x76, 0x14, 0x38, 0x07, 0xc3, 0x89),
47 /* ACPI _DSD data subnodes GUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
48 static const guid_t ads_guid
=
49 GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6,
50 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b);
52 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope
,
53 const union acpi_object
*desc
,
54 struct acpi_device_data
*data
,
55 struct fwnode_handle
*parent
);
56 static bool acpi_extract_properties(const union acpi_object
*desc
,
57 struct acpi_device_data
*data
);
59 static bool acpi_nondev_subnode_extract(const union acpi_object
*desc
,
61 const union acpi_object
*link
,
62 struct list_head
*list
,
63 struct fwnode_handle
*parent
)
65 struct acpi_data_node
*dn
;
68 dn
= kzalloc(sizeof(*dn
), GFP_KERNEL
);
72 dn
->name
= link
->package
.elements
[0].string
.pointer
;
73 dn
->fwnode
.ops
= &acpi_data_fwnode_ops
;
75 INIT_LIST_HEAD(&dn
->data
.properties
);
76 INIT_LIST_HEAD(&dn
->data
.subnodes
);
78 result
= acpi_extract_properties(desc
, &dn
->data
);
85 * The scope for the subnode object lookup is the one of the
86 * namespace node (device) containing the object that has
87 * returned the package. That is, it's the scope of that
90 status
= acpi_get_parent(handle
, &scope
);
91 if (ACPI_SUCCESS(status
)
92 && acpi_enumerate_nondev_subnodes(scope
, desc
, &dn
->data
,
95 } else if (acpi_enumerate_nondev_subnodes(NULL
, desc
, &dn
->data
,
102 dn
->data
.pointer
= desc
;
103 list_add_tail(&dn
->sibling
, list
);
108 acpi_handle_debug(handle
, "Invalid properties/subnodes data, skipping\n");
112 static bool acpi_nondev_subnode_data_ok(acpi_handle handle
,
113 const union acpi_object
*link
,
114 struct list_head
*list
,
115 struct fwnode_handle
*parent
)
117 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
120 status
= acpi_evaluate_object_typed(handle
, NULL
, NULL
, &buf
,
122 if (ACPI_FAILURE(status
))
125 if (acpi_nondev_subnode_extract(buf
.pointer
, handle
, link
, list
,
129 ACPI_FREE(buf
.pointer
);
133 static bool acpi_nondev_subnode_ok(acpi_handle scope
,
134 const union acpi_object
*link
,
135 struct list_head
*list
,
136 struct fwnode_handle
*parent
)
144 status
= acpi_get_handle(scope
, link
->package
.elements
[1].string
.pointer
,
146 if (ACPI_FAILURE(status
))
149 return acpi_nondev_subnode_data_ok(handle
, link
, list
, parent
);
152 static int acpi_add_nondev_subnodes(acpi_handle scope
,
153 const union acpi_object
*links
,
154 struct list_head
*list
,
155 struct fwnode_handle
*parent
)
160 for (i
= 0; i
< links
->package
.count
; i
++) {
161 const union acpi_object
*link
, *desc
;
165 link
= &links
->package
.elements
[i
];
166 /* Only two elements allowed. */
167 if (link
->package
.count
!= 2)
170 /* The first one must be a string. */
171 if (link
->package
.elements
[0].type
!= ACPI_TYPE_STRING
)
174 /* The second one may be a string, a reference or a package. */
175 switch (link
->package
.elements
[1].type
) {
176 case ACPI_TYPE_STRING
:
177 result
= acpi_nondev_subnode_ok(scope
, link
, list
,
180 case ACPI_TYPE_LOCAL_REFERENCE
:
181 handle
= link
->package
.elements
[1].reference
.handle
;
182 result
= acpi_nondev_subnode_data_ok(handle
, link
, list
,
185 case ACPI_TYPE_PACKAGE
:
186 desc
= &link
->package
.elements
[1];
187 result
= acpi_nondev_subnode_extract(desc
, NULL
, link
,
200 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope
,
201 const union acpi_object
*desc
,
202 struct acpi_device_data
*data
,
203 struct fwnode_handle
*parent
)
207 /* Look for the ACPI data subnodes GUID. */
208 for (i
= 0; i
< desc
->package
.count
; i
+= 2) {
209 const union acpi_object
*guid
, *links
;
211 guid
= &desc
->package
.elements
[i
];
212 links
= &desc
->package
.elements
[i
+ 1];
215 * The first element must be a GUID and the second one must be
218 if (guid
->type
!= ACPI_TYPE_BUFFER
||
219 guid
->buffer
.length
!= 16 ||
220 links
->type
!= ACPI_TYPE_PACKAGE
)
223 if (!guid_equal((guid_t
*)guid
->buffer
.pointer
, &ads_guid
))
226 return acpi_add_nondev_subnodes(scope
, links
, &data
->subnodes
,
233 static bool acpi_property_value_ok(const union acpi_object
*value
)
238 * The value must be an integer, a string, a reference, or a package
239 * whose every element must be an integer, a string, or a reference.
241 switch (value
->type
) {
242 case ACPI_TYPE_INTEGER
:
243 case ACPI_TYPE_STRING
:
244 case ACPI_TYPE_LOCAL_REFERENCE
:
247 case ACPI_TYPE_PACKAGE
:
248 for (j
= 0; j
< value
->package
.count
; j
++)
249 switch (value
->package
.elements
[j
].type
) {
250 case ACPI_TYPE_INTEGER
:
251 case ACPI_TYPE_STRING
:
252 case ACPI_TYPE_LOCAL_REFERENCE
:
264 static bool acpi_properties_format_valid(const union acpi_object
*properties
)
268 for (i
= 0; i
< properties
->package
.count
; i
++) {
269 const union acpi_object
*property
;
271 property
= &properties
->package
.elements
[i
];
273 * Only two elements allowed, the first one must be a string and
274 * the second one has to satisfy certain conditions.
276 if (property
->package
.count
!= 2
277 || property
->package
.elements
[0].type
!= ACPI_TYPE_STRING
278 || !acpi_property_value_ok(&property
->package
.elements
[1]))
284 static void acpi_init_of_compatible(struct acpi_device
*adev
)
286 const union acpi_object
*of_compatible
;
289 ret
= acpi_data_get_property_array(&adev
->data
, "compatible",
290 ACPI_TYPE_STRING
, &of_compatible
);
292 ret
= acpi_dev_get_property(adev
, "compatible",
293 ACPI_TYPE_STRING
, &of_compatible
);
296 && adev
->parent
->flags
.of_compatible_ok
)
302 adev
->data
.of_compatible
= of_compatible
;
305 adev
->flags
.of_compatible_ok
= 1;
308 static bool acpi_is_property_guid(const guid_t
*guid
)
312 for (i
= 0; i
< ARRAY_SIZE(prp_guids
); i
++) {
313 if (guid_equal(guid
, &prp_guids
[i
]))
320 struct acpi_device_properties
*
321 acpi_data_add_props(struct acpi_device_data
*data
, const guid_t
*guid
,
322 const union acpi_object
*properties
)
324 struct acpi_device_properties
*props
;
326 props
= kzalloc(sizeof(*props
), GFP_KERNEL
);
328 INIT_LIST_HEAD(&props
->list
);
330 props
->properties
= properties
;
331 list_add_tail(&props
->list
, &data
->properties
);
337 static bool acpi_extract_properties(const union acpi_object
*desc
,
338 struct acpi_device_data
*data
)
342 if (desc
->package
.count
% 2)
345 /* Look for the device properties GUID. */
346 for (i
= 0; i
< desc
->package
.count
; i
+= 2) {
347 const union acpi_object
*guid
, *properties
;
349 guid
= &desc
->package
.elements
[i
];
350 properties
= &desc
->package
.elements
[i
+ 1];
353 * The first element must be a GUID and the second one must be
356 if (guid
->type
!= ACPI_TYPE_BUFFER
||
357 guid
->buffer
.length
!= 16 ||
358 properties
->type
!= ACPI_TYPE_PACKAGE
)
361 if (!acpi_is_property_guid((guid_t
*)guid
->buffer
.pointer
))
365 * We found the matching GUID. Now validate the format of the
366 * package immediately following it.
368 if (!acpi_properties_format_valid(properties
))
371 acpi_data_add_props(data
, (const guid_t
*)guid
->buffer
.pointer
,
375 return !list_empty(&data
->properties
);
378 void acpi_init_properties(struct acpi_device
*adev
)
380 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
381 struct acpi_hardware_id
*hwid
;
383 bool acpi_of
= false;
385 INIT_LIST_HEAD(&adev
->data
.properties
);
386 INIT_LIST_HEAD(&adev
->data
.subnodes
);
392 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
393 * Device Tree compatible properties for this device.
395 list_for_each_entry(hwid
, &adev
->pnp
.ids
, list
) {
396 if (!strcmp(hwid
->id
, ACPI_DT_NAMESPACE_HID
)) {
402 status
= acpi_evaluate_object_typed(adev
->handle
, "_DSD", NULL
, &buf
,
404 if (ACPI_FAILURE(status
))
407 if (acpi_extract_properties(buf
.pointer
, &adev
->data
)) {
408 adev
->data
.pointer
= buf
.pointer
;
410 acpi_init_of_compatible(adev
);
412 if (acpi_enumerate_nondev_subnodes(adev
->handle
, buf
.pointer
,
413 &adev
->data
, acpi_fwnode_handle(adev
)))
414 adev
->data
.pointer
= buf
.pointer
;
416 if (!adev
->data
.pointer
) {
417 acpi_handle_debug(adev
->handle
, "Invalid _DSD data, skipping\n");
418 ACPI_FREE(buf
.pointer
);
422 if (acpi_of
&& !adev
->flags
.of_compatible_ok
)
423 acpi_handle_info(adev
->handle
,
424 ACPI_DT_NAMESPACE_HID
" requires 'compatible' property\n");
426 if (!adev
->data
.pointer
)
427 acpi_extract_apple_properties(adev
);
430 static void acpi_destroy_nondev_subnodes(struct list_head
*list
)
432 struct acpi_data_node
*dn
, *next
;
434 if (list_empty(list
))
437 list_for_each_entry_safe_reverse(dn
, next
, list
, sibling
) {
438 acpi_destroy_nondev_subnodes(&dn
->data
.subnodes
);
439 wait_for_completion(&dn
->kobj_done
);
440 list_del(&dn
->sibling
);
441 ACPI_FREE((void *)dn
->data
.pointer
);
446 void acpi_free_properties(struct acpi_device
*adev
)
448 struct acpi_device_properties
*props
, *tmp
;
450 acpi_destroy_nondev_subnodes(&adev
->data
.subnodes
);
451 ACPI_FREE((void *)adev
->data
.pointer
);
452 adev
->data
.of_compatible
= NULL
;
453 adev
->data
.pointer
= NULL
;
454 list_for_each_entry_safe(props
, tmp
, &adev
->data
.properties
, list
) {
455 list_del(&props
->list
);
461 * acpi_data_get_property - return an ACPI property with given name
462 * @data: ACPI device deta object to get the property from
463 * @name: Name of the property
464 * @type: Expected property type
465 * @obj: Location to store the property value (if not %NULL)
467 * Look up a property with @name and store a pointer to the resulting ACPI
468 * object at the location pointed to by @obj if found.
470 * Callers must not attempt to free the returned objects. These objects will be
471 * freed by the ACPI core automatically during the removal of @data.
473 * Return: %0 if property with @name has been found (success),
474 * %-EINVAL if the arguments are invalid,
475 * %-EINVAL if the property doesn't exist,
476 * %-EPROTO if the property value type doesn't match @type.
478 static int acpi_data_get_property(const struct acpi_device_data
*data
,
479 const char *name
, acpi_object_type type
,
480 const union acpi_object
**obj
)
482 const struct acpi_device_properties
*props
;
487 if (!data
->pointer
|| list_empty(&data
->properties
))
490 list_for_each_entry(props
, &data
->properties
, list
) {
491 const union acpi_object
*properties
;
494 properties
= props
->properties
;
495 for (i
= 0; i
< properties
->package
.count
; i
++) {
496 const union acpi_object
*propname
, *propvalue
;
497 const union acpi_object
*property
;
499 property
= &properties
->package
.elements
[i
];
501 propname
= &property
->package
.elements
[0];
502 propvalue
= &property
->package
.elements
[1];
504 if (!strcmp(name
, propname
->string
.pointer
)) {
505 if (type
!= ACPI_TYPE_ANY
&&
506 propvalue
->type
!= type
)
519 * acpi_dev_get_property - return an ACPI property with given name.
520 * @adev: ACPI device to get the property from.
521 * @name: Name of the property.
522 * @type: Expected property type.
523 * @obj: Location to store the property value (if not %NULL).
525 int acpi_dev_get_property(const struct acpi_device
*adev
, const char *name
,
526 acpi_object_type type
, const union acpi_object
**obj
)
528 return adev
? acpi_data_get_property(&adev
->data
, name
, type
, obj
) : -EINVAL
;
530 EXPORT_SYMBOL_GPL(acpi_dev_get_property
);
532 static const struct acpi_device_data
*
533 acpi_device_data_of_node(const struct fwnode_handle
*fwnode
)
535 if (is_acpi_device_node(fwnode
)) {
536 const struct acpi_device
*adev
= to_acpi_device_node(fwnode
);
538 } else if (is_acpi_data_node(fwnode
)) {
539 const struct acpi_data_node
*dn
= to_acpi_data_node(fwnode
);
546 * acpi_node_prop_get - return an ACPI property with given name.
547 * @fwnode: Firmware node to get the property from.
548 * @propname: Name of the property.
549 * @valptr: Location to store a pointer to the property value (if not %NULL).
551 int acpi_node_prop_get(const struct fwnode_handle
*fwnode
,
552 const char *propname
, void **valptr
)
554 return acpi_data_get_property(acpi_device_data_of_node(fwnode
),
555 propname
, ACPI_TYPE_ANY
,
556 (const union acpi_object
**)valptr
);
560 * acpi_data_get_property_array - return an ACPI array property with given name
561 * @adev: ACPI data object to get the property from
562 * @name: Name of the property
563 * @type: Expected type of array elements
564 * @obj: Location to store a pointer to the property value (if not NULL)
566 * Look up an array property with @name and store a pointer to the resulting
567 * ACPI object at the location pointed to by @obj if found.
569 * Callers must not attempt to free the returned objects. Those objects will be
570 * freed by the ACPI core automatically during the removal of @data.
572 * Return: %0 if array property (package) with @name has been found (success),
573 * %-EINVAL if the arguments are invalid,
574 * %-EINVAL if the property doesn't exist,
575 * %-EPROTO if the property is not a package or the type of its elements
576 * doesn't match @type.
578 static int acpi_data_get_property_array(const struct acpi_device_data
*data
,
580 acpi_object_type type
,
581 const union acpi_object
**obj
)
583 const union acpi_object
*prop
;
586 ret
= acpi_data_get_property(data
, name
, ACPI_TYPE_PACKAGE
, &prop
);
590 if (type
!= ACPI_TYPE_ANY
) {
591 /* Check that all elements are of correct type. */
592 for (i
= 0; i
< prop
->package
.count
; i
++)
593 if (prop
->package
.elements
[i
].type
!= type
)
602 static struct fwnode_handle
*
603 acpi_fwnode_get_named_child_node(const struct fwnode_handle
*fwnode
,
604 const char *childname
)
606 struct fwnode_handle
*child
;
609 * Find first matching named child node of this fwnode.
610 * For ACPI this will be a data only sub-node.
612 fwnode_for_each_child_node(fwnode
, child
)
613 if (acpi_data_node_match(child
, childname
))
620 * __acpi_node_get_property_reference - returns handle to the referenced object
621 * @fwnode: Firmware node to get the property from
622 * @propname: Name of the property
623 * @index: Index of the reference to return
624 * @num_args: Maximum number of arguments after each reference
625 * @args: Location to store the returned reference with optional arguments
627 * Find property with @name, verifify that it is a package containing at least
628 * one object reference and if so, store the ACPI device object pointer to the
629 * target object in @args->adev. If the reference includes arguments, store
630 * them in the @args->args[] array.
632 * If there's more than one reference in the property value package, @index is
633 * used to select the one to return.
635 * It is possible to leave holes in the property value set like in the
648 * Calling this function with index %2 or index %3 return %-ENOENT. If the
649 * property does not contain any more values %-ENOENT is returned. The NULL
650 * entry must be single integer and preferably contain value %0.
652 * Return: %0 on success, negative error code on failure.
654 int __acpi_node_get_property_reference(const struct fwnode_handle
*fwnode
,
655 const char *propname
, size_t index
, size_t num_args
,
656 struct fwnode_reference_args
*args
)
658 const union acpi_object
*element
, *end
;
659 const union acpi_object
*obj
;
660 const struct acpi_device_data
*data
;
661 struct acpi_device
*device
;
664 data
= acpi_device_data_of_node(fwnode
);
668 ret
= acpi_data_get_property(data
, propname
, ACPI_TYPE_ANY
, &obj
);
670 return ret
== -EINVAL
? -ENOENT
: -EINVAL
;
673 * The simplest case is when the value is a single reference. Just
674 * return that reference then.
676 if (obj
->type
== ACPI_TYPE_LOCAL_REFERENCE
) {
680 ret
= acpi_bus_get_device(obj
->reference
.handle
, &device
);
682 return ret
== -ENODEV
? -EINVAL
: ret
;
684 args
->fwnode
= acpi_fwnode_handle(device
);
690 * If it is not a single reference, then it is a package of
691 * references followed by number of ints as follows:
693 * Package () { REF, INT, REF, INT, INT }
695 * The index argument is then used to determine which reference
696 * the caller wants (along with the arguments).
698 if (obj
->type
!= ACPI_TYPE_PACKAGE
)
700 if (index
>= obj
->package
.count
)
703 element
= obj
->package
.elements
;
704 end
= element
+ obj
->package
.count
;
706 while (element
< end
) {
709 if (element
->type
== ACPI_TYPE_LOCAL_REFERENCE
) {
710 struct fwnode_handle
*ref_fwnode
;
712 ret
= acpi_bus_get_device(element
->reference
.handle
,
721 * Find the referred data extension node under the
722 * referred device node.
724 for (ref_fwnode
= acpi_fwnode_handle(device
);
725 element
< end
&& element
->type
== ACPI_TYPE_STRING
;
727 ref_fwnode
= acpi_fwnode_get_named_child_node(
728 ref_fwnode
, element
->string
.pointer
);
733 /* assume following integer elements are all args */
734 for (i
= 0; element
+ i
< end
&& i
< num_args
; i
++) {
735 int type
= element
[i
].type
;
737 if (type
== ACPI_TYPE_INTEGER
)
739 else if (type
== ACPI_TYPE_LOCAL_REFERENCE
)
745 if (nargs
> NR_FWNODE_REFERENCE_ARGS
)
749 args
->fwnode
= ref_fwnode
;
751 for (i
= 0; i
< nargs
; i
++)
752 args
->args
[i
] = element
[i
].integer
.value
;
758 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
771 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference
);
773 static int acpi_data_prop_read_single(const struct acpi_device_data
*data
,
774 const char *propname
,
775 enum dev_prop_type proptype
, void *val
)
777 const union acpi_object
*obj
;
783 if (proptype
>= DEV_PROP_U8
&& proptype
<= DEV_PROP_U64
) {
784 ret
= acpi_data_get_property(data
, propname
, ACPI_TYPE_INTEGER
, &obj
);
790 if (obj
->integer
.value
> U8_MAX
)
792 *(u8
*)val
= obj
->integer
.value
;
795 if (obj
->integer
.value
> U16_MAX
)
797 *(u16
*)val
= obj
->integer
.value
;
800 if (obj
->integer
.value
> U32_MAX
)
802 *(u32
*)val
= obj
->integer
.value
;
805 *(u64
*)val
= obj
->integer
.value
;
808 } else if (proptype
== DEV_PROP_STRING
) {
809 ret
= acpi_data_get_property(data
, propname
, ACPI_TYPE_STRING
, &obj
);
813 *(char **)val
= obj
->string
.pointer
;
822 int acpi_dev_prop_read_single(struct acpi_device
*adev
, const char *propname
,
823 enum dev_prop_type proptype
, void *val
)
830 ret
= acpi_data_prop_read_single(&adev
->data
, propname
, proptype
, val
);
831 if (ret
< 0 || proptype
!= ACPI_TYPE_STRING
)
836 static int acpi_copy_property_array_u8(const union acpi_object
*items
, u8
*val
,
841 for (i
= 0; i
< nval
; i
++) {
842 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
844 if (items
[i
].integer
.value
> U8_MAX
)
847 val
[i
] = items
[i
].integer
.value
;
852 static int acpi_copy_property_array_u16(const union acpi_object
*items
,
853 u16
*val
, size_t nval
)
857 for (i
= 0; i
< nval
; i
++) {
858 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
860 if (items
[i
].integer
.value
> U16_MAX
)
863 val
[i
] = items
[i
].integer
.value
;
868 static int acpi_copy_property_array_u32(const union acpi_object
*items
,
869 u32
*val
, size_t nval
)
873 for (i
= 0; i
< nval
; i
++) {
874 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
876 if (items
[i
].integer
.value
> U32_MAX
)
879 val
[i
] = items
[i
].integer
.value
;
884 static int acpi_copy_property_array_u64(const union acpi_object
*items
,
885 u64
*val
, size_t nval
)
889 for (i
= 0; i
< nval
; i
++) {
890 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
893 val
[i
] = items
[i
].integer
.value
;
898 static int acpi_copy_property_array_string(const union acpi_object
*items
,
899 char **val
, size_t nval
)
903 for (i
= 0; i
< nval
; i
++) {
904 if (items
[i
].type
!= ACPI_TYPE_STRING
)
907 val
[i
] = items
[i
].string
.pointer
;
912 static int acpi_data_prop_read(const struct acpi_device_data
*data
,
913 const char *propname
,
914 enum dev_prop_type proptype
,
915 void *val
, size_t nval
)
917 const union acpi_object
*obj
;
918 const union acpi_object
*items
;
921 if (val
&& nval
== 1) {
922 ret
= acpi_data_prop_read_single(data
, propname
, proptype
, val
);
927 ret
= acpi_data_get_property_array(data
, propname
, ACPI_TYPE_ANY
, &obj
);
932 return obj
->package
.count
;
934 if (proptype
!= DEV_PROP_STRING
&& nval
> obj
->package
.count
)
939 items
= obj
->package
.elements
;
943 ret
= acpi_copy_property_array_u8(items
, (u8
*)val
, nval
);
946 ret
= acpi_copy_property_array_u16(items
, (u16
*)val
, nval
);
949 ret
= acpi_copy_property_array_u32(items
, (u32
*)val
, nval
);
952 ret
= acpi_copy_property_array_u64(items
, (u64
*)val
, nval
);
954 case DEV_PROP_STRING
:
955 ret
= acpi_copy_property_array_string(
957 min_t(u32
, nval
, obj
->package
.count
));
966 int acpi_dev_prop_read(const struct acpi_device
*adev
, const char *propname
,
967 enum dev_prop_type proptype
, void *val
, size_t nval
)
969 return adev
? acpi_data_prop_read(&adev
->data
, propname
, proptype
, val
, nval
) : -EINVAL
;
973 * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
974 * @fwnode: Firmware node to get the property from.
975 * @propname: Name of the property.
976 * @proptype: Expected property type.
977 * @val: Location to store the property value (if not %NULL).
978 * @nval: Size of the array pointed to by @val.
980 * If @val is %NULL, return the number of array elements comprising the value
981 * of the property. Otherwise, read at most @nval values to the array at the
982 * location pointed to by @val.
984 int acpi_node_prop_read(const struct fwnode_handle
*fwnode
,
985 const char *propname
, enum dev_prop_type proptype
,
986 void *val
, size_t nval
)
988 return acpi_data_prop_read(acpi_device_data_of_node(fwnode
),
989 propname
, proptype
, val
, nval
);
993 * acpi_get_next_subnode - Return the next child node handle for a fwnode
994 * @fwnode: Firmware node to find the next child node for.
995 * @child: Handle to one of the device's child nodes or a null handle.
997 struct fwnode_handle
*acpi_get_next_subnode(const struct fwnode_handle
*fwnode
,
998 struct fwnode_handle
*child
)
1000 const struct acpi_device
*adev
= to_acpi_device_node(fwnode
);
1001 const struct list_head
*head
;
1002 struct list_head
*next
;
1004 if (!child
|| is_acpi_device_node(child
)) {
1005 struct acpi_device
*child_adev
;
1008 head
= &adev
->children
;
1012 if (list_empty(head
))
1016 adev
= to_acpi_device_node(child
);
1017 next
= adev
->node
.next
;
1022 child_adev
= list_entry(next
, struct acpi_device
, node
);
1024 child_adev
= list_first_entry(head
, struct acpi_device
,
1027 return acpi_fwnode_handle(child_adev
);
1031 if (!child
|| is_acpi_data_node(child
)) {
1032 const struct acpi_data_node
*data
= to_acpi_data_node(fwnode
);
1033 struct acpi_data_node
*dn
;
1036 * We can have a combination of device and data nodes, e.g. with
1037 * hierarchical _DSD properties. Make sure the adev pointer is
1038 * restored before going through data nodes, otherwise we will
1039 * be looking for data_nodes below the last device found instead
1040 * of the common fwnode shared by device_nodes and data_nodes.
1042 adev
= to_acpi_device_node(fwnode
);
1044 head
= &adev
->data
.subnodes
;
1046 head
= &data
->data
.subnodes
;
1050 if (list_empty(head
))
1054 dn
= to_acpi_data_node(child
);
1055 next
= dn
->sibling
.next
;
1059 dn
= list_entry(next
, struct acpi_data_node
, sibling
);
1061 dn
= list_first_entry(head
, struct acpi_data_node
, sibling
);
1069 * acpi_node_get_parent - Return parent fwnode of this fwnode
1070 * @fwnode: Firmware node whose parent to get
1072 * Returns parent node of an ACPI device or data firmware node or %NULL if
1075 struct fwnode_handle
*acpi_node_get_parent(const struct fwnode_handle
*fwnode
)
1077 if (is_acpi_data_node(fwnode
)) {
1078 /* All data nodes have parent pointer so just return that */
1079 return to_acpi_data_node(fwnode
)->parent
;
1080 } else if (is_acpi_device_node(fwnode
)) {
1081 acpi_handle handle
, parent_handle
;
1083 handle
= to_acpi_device_node(fwnode
)->handle
;
1084 if (ACPI_SUCCESS(acpi_get_parent(handle
, &parent_handle
))) {
1085 struct acpi_device
*adev
;
1087 if (!acpi_bus_get_device(parent_handle
, &adev
))
1088 return acpi_fwnode_handle(adev
);
1096 * Return true if the node is an ACPI graph node. Called on either ports
1099 static bool is_acpi_graph_node(struct fwnode_handle
*fwnode
,
1102 unsigned int len
= strlen(str
);
1105 if (!len
|| !is_acpi_data_node(fwnode
))
1108 name
= to_acpi_data_node(fwnode
)->name
;
1110 return (fwnode_property_present(fwnode
, "reg") &&
1111 !strncmp(name
, str
, len
) && name
[len
] == '@') ||
1112 fwnode_property_present(fwnode
, str
);
1116 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
1117 * @fwnode: Pointer to the parent firmware node
1118 * @prev: Previous endpoint node or %NULL to get the first
1120 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
1121 * %NULL if there is no next endpoint or in case of error. In case of success
1122 * the next endpoint is returned.
1124 static struct fwnode_handle
*acpi_graph_get_next_endpoint(
1125 const struct fwnode_handle
*fwnode
, struct fwnode_handle
*prev
)
1127 struct fwnode_handle
*port
= NULL
;
1128 struct fwnode_handle
*endpoint
;
1132 port
= fwnode_get_next_child_node(fwnode
, port
);
1134 * The names of the port nodes begin with "port@"
1135 * followed by the number of the port node and they also
1136 * have a "reg" property that also has the number of the
1137 * port node. For compatibility reasons a node is also
1138 * recognised as a port node from the "port" property.
1140 if (is_acpi_graph_node(port
, "port"))
1144 port
= fwnode_get_parent(prev
);
1150 endpoint
= fwnode_get_next_child_node(port
, prev
);
1152 port
= fwnode_get_next_child_node(fwnode
, port
);
1155 if (is_acpi_graph_node(port
, "port"))
1156 endpoint
= fwnode_get_next_child_node(port
, NULL
);
1160 * The names of the endpoint nodes begin with "endpoint@" followed by
1161 * the number of the endpoint node and they also have a "reg" property
1162 * that also has the number of the endpoint node. For compatibility
1163 * reasons a node is also recognised as an endpoint node from the
1164 * "endpoint" property.
1166 if (!is_acpi_graph_node(endpoint
, "endpoint"))
1173 * acpi_graph_get_child_prop_value - Return a child with a given property value
1174 * @fwnode: device fwnode
1175 * @prop_name: The name of the property to look for
1176 * @val: the desired property value
1178 * Return the port node corresponding to a given port number. Returns
1179 * the child node on success, NULL otherwise.
1181 static struct fwnode_handle
*acpi_graph_get_child_prop_value(
1182 const struct fwnode_handle
*fwnode
, const char *prop_name
,
1185 struct fwnode_handle
*child
;
1187 fwnode_for_each_child_node(fwnode
, child
) {
1190 if (fwnode_property_read_u32(child
, prop_name
, &nr
))
1202 * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
1203 * @fwnode: Endpoint firmware node pointing to a remote device
1204 * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
1206 * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
1208 static struct fwnode_handle
*
1209 acpi_graph_get_remote_endpoint(const struct fwnode_handle
*__fwnode
)
1211 struct fwnode_handle
*fwnode
;
1212 unsigned int port_nr
, endpoint_nr
;
1213 struct fwnode_reference_args args
;
1216 memset(&args
, 0, sizeof(args
));
1217 ret
= acpi_node_get_property_reference(__fwnode
, "remote-endpoint", 0,
1222 /* Direct endpoint reference? */
1223 if (!is_acpi_device_node(args
.fwnode
))
1224 return args
.nargs
? NULL
: args
.fwnode
;
1227 * Always require two arguments with the reference: port and
1230 if (args
.nargs
!= 2)
1233 fwnode
= args
.fwnode
;
1234 port_nr
= args
.args
[0];
1235 endpoint_nr
= args
.args
[1];
1237 fwnode
= acpi_graph_get_child_prop_value(fwnode
, "port", port_nr
);
1239 return acpi_graph_get_child_prop_value(fwnode
, "endpoint", endpoint_nr
);
1242 static bool acpi_fwnode_device_is_available(const struct fwnode_handle
*fwnode
)
1244 if (!is_acpi_device_node(fwnode
))
1247 return acpi_device_is_present(to_acpi_device_node(fwnode
));
1250 static bool acpi_fwnode_property_present(const struct fwnode_handle
*fwnode
,
1251 const char *propname
)
1253 return !acpi_node_prop_get(fwnode
, propname
, NULL
);
1257 acpi_fwnode_property_read_int_array(const struct fwnode_handle
*fwnode
,
1258 const char *propname
,
1259 unsigned int elem_size
, void *val
,
1262 enum dev_prop_type type
;
1264 switch (elem_size
) {
1269 type
= DEV_PROP_U16
;
1272 type
= DEV_PROP_U32
;
1275 type
= DEV_PROP_U64
;
1281 return acpi_node_prop_read(fwnode
, propname
, type
, val
, nval
);
1285 acpi_fwnode_property_read_string_array(const struct fwnode_handle
*fwnode
,
1286 const char *propname
, const char **val
,
1289 return acpi_node_prop_read(fwnode
, propname
, DEV_PROP_STRING
,
1294 acpi_fwnode_get_reference_args(const struct fwnode_handle
*fwnode
,
1295 const char *prop
, const char *nargs_prop
,
1296 unsigned int args_count
, unsigned int index
,
1297 struct fwnode_reference_args
*args
)
1299 return __acpi_node_get_property_reference(fwnode
, prop
, index
,
1303 static struct fwnode_handle
*
1304 acpi_fwnode_get_parent(struct fwnode_handle
*fwnode
)
1306 return acpi_node_get_parent(fwnode
);
1309 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle
*fwnode
,
1310 struct fwnode_endpoint
*endpoint
)
1312 struct fwnode_handle
*port_fwnode
= fwnode_get_parent(fwnode
);
1314 endpoint
->local_fwnode
= fwnode
;
1316 if (fwnode_property_read_u32(port_fwnode
, "reg", &endpoint
->port
))
1317 fwnode_property_read_u32(port_fwnode
, "port", &endpoint
->port
);
1318 if (fwnode_property_read_u32(fwnode
, "reg", &endpoint
->id
))
1319 fwnode_property_read_u32(fwnode
, "endpoint", &endpoint
->id
);
1325 acpi_fwnode_device_get_match_data(const struct fwnode_handle
*fwnode
,
1326 const struct device
*dev
)
1328 return acpi_device_get_match_data(dev
);
1331 #define DECLARE_ACPI_FWNODE_OPS(ops) \
1332 const struct fwnode_operations ops = { \
1333 .device_is_available = acpi_fwnode_device_is_available, \
1334 .device_get_match_data = acpi_fwnode_device_get_match_data, \
1335 .property_present = acpi_fwnode_property_present, \
1336 .property_read_int_array = \
1337 acpi_fwnode_property_read_int_array, \
1338 .property_read_string_array = \
1339 acpi_fwnode_property_read_string_array, \
1340 .get_parent = acpi_node_get_parent, \
1341 .get_next_child_node = acpi_get_next_subnode, \
1342 .get_named_child_node = acpi_fwnode_get_named_child_node, \
1343 .get_reference_args = acpi_fwnode_get_reference_args, \
1344 .graph_get_next_endpoint = \
1345 acpi_graph_get_next_endpoint, \
1346 .graph_get_remote_endpoint = \
1347 acpi_graph_get_remote_endpoint, \
1348 .graph_get_port_parent = acpi_fwnode_get_parent, \
1349 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1351 EXPORT_SYMBOL_GPL(ops)
1353 DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops
);
1354 DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops
);
1355 const struct fwnode_operations acpi_static_fwnode_ops
;
1357 bool is_acpi_device_node(const struct fwnode_handle
*fwnode
)
1359 return !IS_ERR_OR_NULL(fwnode
) &&
1360 fwnode
->ops
== &acpi_device_fwnode_ops
;
1362 EXPORT_SYMBOL(is_acpi_device_node
);
1364 bool is_acpi_data_node(const struct fwnode_handle
*fwnode
)
1366 return !IS_ERR_OR_NULL(fwnode
) && fwnode
->ops
== &acpi_data_fwnode_ops
;
1368 EXPORT_SYMBOL(is_acpi_data_node
);