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 static const guid_t ads_guid
=
48 GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6,
49 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b);
51 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope
,
52 const union acpi_object
*desc
,
53 struct acpi_device_data
*data
,
54 struct fwnode_handle
*parent
);
55 static bool acpi_extract_properties(const union acpi_object
*desc
,
56 struct acpi_device_data
*data
);
58 static bool acpi_nondev_subnode_extract(const union acpi_object
*desc
,
60 const union acpi_object
*link
,
61 struct list_head
*list
,
62 struct fwnode_handle
*parent
)
64 struct acpi_data_node
*dn
;
67 dn
= kzalloc(sizeof(*dn
), GFP_KERNEL
);
71 dn
->name
= link
->package
.elements
[0].string
.pointer
;
72 dn
->fwnode
.ops
= &acpi_data_fwnode_ops
;
74 INIT_LIST_HEAD(&dn
->data
.properties
);
75 INIT_LIST_HEAD(&dn
->data
.subnodes
);
77 result
= acpi_extract_properties(desc
, &dn
->data
);
84 * The scope for the subnode object lookup is the one of the
85 * namespace node (device) containing the object that has
86 * returned the package. That is, it's the scope of that
89 status
= acpi_get_parent(handle
, &scope
);
90 if (ACPI_SUCCESS(status
)
91 && acpi_enumerate_nondev_subnodes(scope
, desc
, &dn
->data
,
94 } else if (acpi_enumerate_nondev_subnodes(NULL
, desc
, &dn
->data
,
101 dn
->data
.pointer
= desc
;
102 list_add_tail(&dn
->sibling
, list
);
107 acpi_handle_debug(handle
, "Invalid properties/subnodes data, skipping\n");
111 static bool acpi_nondev_subnode_data_ok(acpi_handle handle
,
112 const union acpi_object
*link
,
113 struct list_head
*list
,
114 struct fwnode_handle
*parent
)
116 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
119 status
= acpi_evaluate_object_typed(handle
, NULL
, NULL
, &buf
,
121 if (ACPI_FAILURE(status
))
124 if (acpi_nondev_subnode_extract(buf
.pointer
, handle
, link
, list
,
128 ACPI_FREE(buf
.pointer
);
132 static bool acpi_nondev_subnode_ok(acpi_handle scope
,
133 const union acpi_object
*link
,
134 struct list_head
*list
,
135 struct fwnode_handle
*parent
)
143 status
= acpi_get_handle(scope
, link
->package
.elements
[1].string
.pointer
,
145 if (ACPI_FAILURE(status
))
148 return acpi_nondev_subnode_data_ok(handle
, link
, list
, parent
);
151 static int acpi_add_nondev_subnodes(acpi_handle scope
,
152 const union acpi_object
*links
,
153 struct list_head
*list
,
154 struct fwnode_handle
*parent
)
159 for (i
= 0; i
< links
->package
.count
; i
++) {
160 const union acpi_object
*link
, *desc
;
164 link
= &links
->package
.elements
[i
];
165 /* Only two elements allowed. */
166 if (link
->package
.count
!= 2)
169 /* The first one must be a string. */
170 if (link
->package
.elements
[0].type
!= ACPI_TYPE_STRING
)
173 /* The second one may be a string, a reference or a package. */
174 switch (link
->package
.elements
[1].type
) {
175 case ACPI_TYPE_STRING
:
176 result
= acpi_nondev_subnode_ok(scope
, link
, list
,
179 case ACPI_TYPE_LOCAL_REFERENCE
:
180 handle
= link
->package
.elements
[1].reference
.handle
;
181 result
= acpi_nondev_subnode_data_ok(handle
, link
, list
,
184 case ACPI_TYPE_PACKAGE
:
185 desc
= &link
->package
.elements
[1];
186 result
= acpi_nondev_subnode_extract(desc
, NULL
, link
,
199 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope
,
200 const union acpi_object
*desc
,
201 struct acpi_device_data
*data
,
202 struct fwnode_handle
*parent
)
206 /* Look for the ACPI data subnodes GUID. */
207 for (i
= 0; i
< desc
->package
.count
; i
+= 2) {
208 const union acpi_object
*guid
, *links
;
210 guid
= &desc
->package
.elements
[i
];
211 links
= &desc
->package
.elements
[i
+ 1];
214 * The first element must be a GUID and the second one must be
217 if (guid
->type
!= ACPI_TYPE_BUFFER
||
218 guid
->buffer
.length
!= 16 ||
219 links
->type
!= ACPI_TYPE_PACKAGE
)
222 if (!guid_equal((guid_t
*)guid
->buffer
.pointer
, &ads_guid
))
225 return acpi_add_nondev_subnodes(scope
, links
, &data
->subnodes
,
232 static bool acpi_property_value_ok(const union acpi_object
*value
)
237 * The value must be an integer, a string, a reference, or a package
238 * whose every element must be an integer, a string, or a reference.
240 switch (value
->type
) {
241 case ACPI_TYPE_INTEGER
:
242 case ACPI_TYPE_STRING
:
243 case ACPI_TYPE_LOCAL_REFERENCE
:
246 case ACPI_TYPE_PACKAGE
:
247 for (j
= 0; j
< value
->package
.count
; j
++)
248 switch (value
->package
.elements
[j
].type
) {
249 case ACPI_TYPE_INTEGER
:
250 case ACPI_TYPE_STRING
:
251 case ACPI_TYPE_LOCAL_REFERENCE
:
263 static bool acpi_properties_format_valid(const union acpi_object
*properties
)
267 for (i
= 0; i
< properties
->package
.count
; i
++) {
268 const union acpi_object
*property
;
270 property
= &properties
->package
.elements
[i
];
272 * Only two elements allowed, the first one must be a string and
273 * the second one has to satisfy certain conditions.
275 if (property
->package
.count
!= 2
276 || property
->package
.elements
[0].type
!= ACPI_TYPE_STRING
277 || !acpi_property_value_ok(&property
->package
.elements
[1]))
283 static void acpi_init_of_compatible(struct acpi_device
*adev
)
285 const union acpi_object
*of_compatible
;
288 ret
= acpi_data_get_property_array(&adev
->data
, "compatible",
289 ACPI_TYPE_STRING
, &of_compatible
);
291 ret
= acpi_dev_get_property(adev
, "compatible",
292 ACPI_TYPE_STRING
, &of_compatible
);
295 && adev
->parent
->flags
.of_compatible_ok
)
301 adev
->data
.of_compatible
= of_compatible
;
304 adev
->flags
.of_compatible_ok
= 1;
307 static bool acpi_is_property_guid(const guid_t
*guid
)
311 for (i
= 0; i
< ARRAY_SIZE(prp_guids
); i
++) {
312 if (guid_equal(guid
, &prp_guids
[i
]))
319 struct acpi_device_properties
*
320 acpi_data_add_props(struct acpi_device_data
*data
, const guid_t
*guid
,
321 const union acpi_object
*properties
)
323 struct acpi_device_properties
*props
;
325 props
= kzalloc(sizeof(*props
), GFP_KERNEL
);
327 INIT_LIST_HEAD(&props
->list
);
329 props
->properties
= properties
;
330 list_add_tail(&props
->list
, &data
->properties
);
336 static bool acpi_extract_properties(const union acpi_object
*desc
,
337 struct acpi_device_data
*data
)
341 if (desc
->package
.count
% 2)
344 /* Look for the device properties GUID. */
345 for (i
= 0; i
< desc
->package
.count
; i
+= 2) {
346 const union acpi_object
*guid
, *properties
;
348 guid
= &desc
->package
.elements
[i
];
349 properties
= &desc
->package
.elements
[i
+ 1];
352 * The first element must be a GUID and the second one must be
355 if (guid
->type
!= ACPI_TYPE_BUFFER
||
356 guid
->buffer
.length
!= 16 ||
357 properties
->type
!= ACPI_TYPE_PACKAGE
)
360 if (!acpi_is_property_guid((guid_t
*)guid
->buffer
.pointer
))
364 * We found the matching GUID. Now validate the format of the
365 * package immediately following it.
367 if (!acpi_properties_format_valid(properties
))
370 acpi_data_add_props(data
, (const guid_t
*)guid
->buffer
.pointer
,
374 return !list_empty(&data
->properties
);
377 void acpi_init_properties(struct acpi_device
*adev
)
379 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
380 struct acpi_hardware_id
*hwid
;
382 bool acpi_of
= false;
384 INIT_LIST_HEAD(&adev
->data
.properties
);
385 INIT_LIST_HEAD(&adev
->data
.subnodes
);
391 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
392 * Device Tree compatible properties for this device.
394 list_for_each_entry(hwid
, &adev
->pnp
.ids
, list
) {
395 if (!strcmp(hwid
->id
, ACPI_DT_NAMESPACE_HID
)) {
401 status
= acpi_evaluate_object_typed(adev
->handle
, "_DSD", NULL
, &buf
,
403 if (ACPI_FAILURE(status
))
406 if (acpi_extract_properties(buf
.pointer
, &adev
->data
)) {
407 adev
->data
.pointer
= buf
.pointer
;
409 acpi_init_of_compatible(adev
);
411 if (acpi_enumerate_nondev_subnodes(adev
->handle
, buf
.pointer
,
412 &adev
->data
, acpi_fwnode_handle(adev
)))
413 adev
->data
.pointer
= buf
.pointer
;
415 if (!adev
->data
.pointer
) {
416 acpi_handle_debug(adev
->handle
, "Invalid _DSD data, skipping\n");
417 ACPI_FREE(buf
.pointer
);
421 if (acpi_of
&& !adev
->flags
.of_compatible_ok
)
422 acpi_handle_info(adev
->handle
,
423 ACPI_DT_NAMESPACE_HID
" requires 'compatible' property\n");
425 if (!adev
->data
.pointer
)
426 acpi_extract_apple_properties(adev
);
429 static void acpi_destroy_nondev_subnodes(struct list_head
*list
)
431 struct acpi_data_node
*dn
, *next
;
433 if (list_empty(list
))
436 list_for_each_entry_safe_reverse(dn
, next
, list
, sibling
) {
437 acpi_destroy_nondev_subnodes(&dn
->data
.subnodes
);
438 wait_for_completion(&dn
->kobj_done
);
439 list_del(&dn
->sibling
);
440 ACPI_FREE((void *)dn
->data
.pointer
);
445 void acpi_free_properties(struct acpi_device
*adev
)
447 struct acpi_device_properties
*props
, *tmp
;
449 acpi_destroy_nondev_subnodes(&adev
->data
.subnodes
);
450 ACPI_FREE((void *)adev
->data
.pointer
);
451 adev
->data
.of_compatible
= NULL
;
452 adev
->data
.pointer
= NULL
;
453 list_for_each_entry_safe(props
, tmp
, &adev
->data
.properties
, list
) {
454 list_del(&props
->list
);
460 * acpi_data_get_property - return an ACPI property with given name
461 * @data: ACPI device deta object to get the property from
462 * @name: Name of the property
463 * @type: Expected property type
464 * @obj: Location to store the property value (if not %NULL)
466 * Look up a property with @name and store a pointer to the resulting ACPI
467 * object at the location pointed to by @obj if found.
469 * Callers must not attempt to free the returned objects. These objects will be
470 * freed by the ACPI core automatically during the removal of @data.
472 * Return: %0 if property with @name has been found (success),
473 * %-EINVAL if the arguments are invalid,
474 * %-EINVAL if the property doesn't exist,
475 * %-EPROTO if the property value type doesn't match @type.
477 static int acpi_data_get_property(const struct acpi_device_data
*data
,
478 const char *name
, acpi_object_type type
,
479 const union acpi_object
**obj
)
481 const struct acpi_device_properties
*props
;
486 if (!data
->pointer
|| list_empty(&data
->properties
))
489 list_for_each_entry(props
, &data
->properties
, list
) {
490 const union acpi_object
*properties
;
493 properties
= props
->properties
;
494 for (i
= 0; i
< properties
->package
.count
; i
++) {
495 const union acpi_object
*propname
, *propvalue
;
496 const union acpi_object
*property
;
498 property
= &properties
->package
.elements
[i
];
500 propname
= &property
->package
.elements
[0];
501 propvalue
= &property
->package
.elements
[1];
503 if (!strcmp(name
, propname
->string
.pointer
)) {
504 if (type
!= ACPI_TYPE_ANY
&&
505 propvalue
->type
!= type
)
518 * acpi_dev_get_property - return an ACPI property with given name.
519 * @adev: ACPI device to get the property from.
520 * @name: Name of the property.
521 * @type: Expected property type.
522 * @obj: Location to store the property value (if not %NULL).
524 int acpi_dev_get_property(const struct acpi_device
*adev
, const char *name
,
525 acpi_object_type type
, const union acpi_object
**obj
)
527 return adev
? acpi_data_get_property(&adev
->data
, name
, type
, obj
) : -EINVAL
;
529 EXPORT_SYMBOL_GPL(acpi_dev_get_property
);
531 static const struct acpi_device_data
*
532 acpi_device_data_of_node(const struct fwnode_handle
*fwnode
)
534 if (is_acpi_device_node(fwnode
)) {
535 const struct acpi_device
*adev
= to_acpi_device_node(fwnode
);
537 } else if (is_acpi_data_node(fwnode
)) {
538 const struct acpi_data_node
*dn
= to_acpi_data_node(fwnode
);
545 * acpi_node_prop_get - return an ACPI property with given name.
546 * @fwnode: Firmware node to get the property from.
547 * @propname: Name of the property.
548 * @valptr: Location to store a pointer to the property value (if not %NULL).
550 int acpi_node_prop_get(const struct fwnode_handle
*fwnode
,
551 const char *propname
, void **valptr
)
553 return acpi_data_get_property(acpi_device_data_of_node(fwnode
),
554 propname
, ACPI_TYPE_ANY
,
555 (const union acpi_object
**)valptr
);
559 * acpi_data_get_property_array - return an ACPI array property with given name
560 * @adev: ACPI data object to get the property from
561 * @name: Name of the property
562 * @type: Expected type of array elements
563 * @obj: Location to store a pointer to the property value (if not NULL)
565 * Look up an array property with @name and store a pointer to the resulting
566 * ACPI object at the location pointed to by @obj if found.
568 * Callers must not attempt to free the returned objects. Those objects will be
569 * freed by the ACPI core automatically during the removal of @data.
571 * Return: %0 if array property (package) with @name has been found (success),
572 * %-EINVAL if the arguments are invalid,
573 * %-EINVAL if the property doesn't exist,
574 * %-EPROTO if the property is not a package or the type of its elements
575 * doesn't match @type.
577 static int acpi_data_get_property_array(const struct acpi_device_data
*data
,
579 acpi_object_type type
,
580 const union acpi_object
**obj
)
582 const union acpi_object
*prop
;
585 ret
= acpi_data_get_property(data
, name
, ACPI_TYPE_PACKAGE
, &prop
);
589 if (type
!= ACPI_TYPE_ANY
) {
590 /* Check that all elements are of correct type. */
591 for (i
= 0; i
< prop
->package
.count
; i
++)
592 if (prop
->package
.elements
[i
].type
!= type
)
601 static struct fwnode_handle
*
602 acpi_fwnode_get_named_child_node(const struct fwnode_handle
*fwnode
,
603 const char *childname
)
605 struct fwnode_handle
*child
;
608 * Find first matching named child node of this fwnode.
609 * For ACPI this will be a data only sub-node.
611 fwnode_for_each_child_node(fwnode
, child
)
612 if (acpi_data_node_match(child
, childname
))
619 * __acpi_node_get_property_reference - returns handle to the referenced object
620 * @fwnode: Firmware node to get the property from
621 * @propname: Name of the property
622 * @index: Index of the reference to return
623 * @num_args: Maximum number of arguments after each reference
624 * @args: Location to store the returned reference with optional arguments
626 * Find property with @name, verifify that it is a package containing at least
627 * one object reference and if so, store the ACPI device object pointer to the
628 * target object in @args->adev. If the reference includes arguments, store
629 * them in the @args->args[] array.
631 * If there's more than one reference in the property value package, @index is
632 * used to select the one to return.
634 * It is possible to leave holes in the property value set like in the
647 * Calling this function with index %2 or index %3 return %-ENOENT. If the
648 * property does not contain any more values %-ENOENT is returned. The NULL
649 * entry must be single integer and preferably contain value %0.
651 * Return: %0 on success, negative error code on failure.
653 int __acpi_node_get_property_reference(const struct fwnode_handle
*fwnode
,
654 const char *propname
, size_t index
, size_t num_args
,
655 struct fwnode_reference_args
*args
)
657 const union acpi_object
*element
, *end
;
658 const union acpi_object
*obj
;
659 const struct acpi_device_data
*data
;
660 struct acpi_device
*device
;
663 data
= acpi_device_data_of_node(fwnode
);
667 ret
= acpi_data_get_property(data
, propname
, ACPI_TYPE_ANY
, &obj
);
669 return ret
== -EINVAL
? -ENOENT
: -EINVAL
;
672 * The simplest case is when the value is a single reference. Just
673 * return that reference then.
675 if (obj
->type
== ACPI_TYPE_LOCAL_REFERENCE
) {
679 ret
= acpi_bus_get_device(obj
->reference
.handle
, &device
);
681 return ret
== -ENODEV
? -EINVAL
: ret
;
683 args
->fwnode
= acpi_fwnode_handle(device
);
689 * If it is not a single reference, then it is a package of
690 * references followed by number of ints as follows:
692 * Package () { REF, INT, REF, INT, INT }
694 * The index argument is then used to determine which reference
695 * the caller wants (along with the arguments).
697 if (obj
->type
!= ACPI_TYPE_PACKAGE
)
699 if (index
>= obj
->package
.count
)
702 element
= obj
->package
.elements
;
703 end
= element
+ obj
->package
.count
;
705 while (element
< end
) {
708 if (element
->type
== ACPI_TYPE_LOCAL_REFERENCE
) {
709 struct fwnode_handle
*ref_fwnode
;
711 ret
= acpi_bus_get_device(element
->reference
.handle
,
720 * Find the referred data extension node under the
721 * referred device node.
723 for (ref_fwnode
= acpi_fwnode_handle(device
);
724 element
< end
&& element
->type
== ACPI_TYPE_STRING
;
726 ref_fwnode
= acpi_fwnode_get_named_child_node(
727 ref_fwnode
, element
->string
.pointer
);
732 /* assume following integer elements are all args */
733 for (i
= 0; element
+ i
< end
&& i
< num_args
; i
++) {
734 int type
= element
[i
].type
;
736 if (type
== ACPI_TYPE_INTEGER
)
738 else if (type
== ACPI_TYPE_LOCAL_REFERENCE
)
744 if (nargs
> NR_FWNODE_REFERENCE_ARGS
)
748 args
->fwnode
= ref_fwnode
;
750 for (i
= 0; i
< nargs
; i
++)
751 args
->args
[i
] = element
[i
].integer
.value
;
757 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
770 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference
);
772 static int acpi_data_prop_read_single(const struct acpi_device_data
*data
,
773 const char *propname
,
774 enum dev_prop_type proptype
, void *val
)
776 const union acpi_object
*obj
;
782 if (proptype
>= DEV_PROP_U8
&& proptype
<= DEV_PROP_U64
) {
783 ret
= acpi_data_get_property(data
, propname
, ACPI_TYPE_INTEGER
, &obj
);
789 if (obj
->integer
.value
> U8_MAX
)
791 *(u8
*)val
= obj
->integer
.value
;
794 if (obj
->integer
.value
> U16_MAX
)
796 *(u16
*)val
= obj
->integer
.value
;
799 if (obj
->integer
.value
> U32_MAX
)
801 *(u32
*)val
= obj
->integer
.value
;
804 *(u64
*)val
= obj
->integer
.value
;
807 } else if (proptype
== DEV_PROP_STRING
) {
808 ret
= acpi_data_get_property(data
, propname
, ACPI_TYPE_STRING
, &obj
);
812 *(char **)val
= obj
->string
.pointer
;
821 int acpi_dev_prop_read_single(struct acpi_device
*adev
, const char *propname
,
822 enum dev_prop_type proptype
, void *val
)
829 ret
= acpi_data_prop_read_single(&adev
->data
, propname
, proptype
, val
);
830 if (ret
< 0 || proptype
!= ACPI_TYPE_STRING
)
835 static int acpi_copy_property_array_u8(const union acpi_object
*items
, u8
*val
,
840 for (i
= 0; i
< nval
; i
++) {
841 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
843 if (items
[i
].integer
.value
> U8_MAX
)
846 val
[i
] = items
[i
].integer
.value
;
851 static int acpi_copy_property_array_u16(const union acpi_object
*items
,
852 u16
*val
, size_t nval
)
856 for (i
= 0; i
< nval
; i
++) {
857 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
859 if (items
[i
].integer
.value
> U16_MAX
)
862 val
[i
] = items
[i
].integer
.value
;
867 static int acpi_copy_property_array_u32(const union acpi_object
*items
,
868 u32
*val
, size_t nval
)
872 for (i
= 0; i
< nval
; i
++) {
873 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
875 if (items
[i
].integer
.value
> U32_MAX
)
878 val
[i
] = items
[i
].integer
.value
;
883 static int acpi_copy_property_array_u64(const union acpi_object
*items
,
884 u64
*val
, size_t nval
)
888 for (i
= 0; i
< nval
; i
++) {
889 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
892 val
[i
] = items
[i
].integer
.value
;
897 static int acpi_copy_property_array_string(const union acpi_object
*items
,
898 char **val
, size_t nval
)
902 for (i
= 0; i
< nval
; i
++) {
903 if (items
[i
].type
!= ACPI_TYPE_STRING
)
906 val
[i
] = items
[i
].string
.pointer
;
911 static int acpi_data_prop_read(const struct acpi_device_data
*data
,
912 const char *propname
,
913 enum dev_prop_type proptype
,
914 void *val
, size_t nval
)
916 const union acpi_object
*obj
;
917 const union acpi_object
*items
;
920 if (val
&& nval
== 1) {
921 ret
= acpi_data_prop_read_single(data
, propname
, proptype
, val
);
926 ret
= acpi_data_get_property_array(data
, propname
, ACPI_TYPE_ANY
, &obj
);
931 return obj
->package
.count
;
933 if (proptype
!= DEV_PROP_STRING
&& nval
> obj
->package
.count
)
938 items
= obj
->package
.elements
;
942 ret
= acpi_copy_property_array_u8(items
, (u8
*)val
, nval
);
945 ret
= acpi_copy_property_array_u16(items
, (u16
*)val
, nval
);
948 ret
= acpi_copy_property_array_u32(items
, (u32
*)val
, nval
);
951 ret
= acpi_copy_property_array_u64(items
, (u64
*)val
, nval
);
953 case DEV_PROP_STRING
:
954 ret
= acpi_copy_property_array_string(
956 min_t(u32
, nval
, obj
->package
.count
));
965 int acpi_dev_prop_read(const struct acpi_device
*adev
, const char *propname
,
966 enum dev_prop_type proptype
, void *val
, size_t nval
)
968 return adev
? acpi_data_prop_read(&adev
->data
, propname
, proptype
, val
, nval
) : -EINVAL
;
972 * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
973 * @fwnode: Firmware node to get the property from.
974 * @propname: Name of the property.
975 * @proptype: Expected property type.
976 * @val: Location to store the property value (if not %NULL).
977 * @nval: Size of the array pointed to by @val.
979 * If @val is %NULL, return the number of array elements comprising the value
980 * of the property. Otherwise, read at most @nval values to the array at the
981 * location pointed to by @val.
983 int acpi_node_prop_read(const struct fwnode_handle
*fwnode
,
984 const char *propname
, enum dev_prop_type proptype
,
985 void *val
, size_t nval
)
987 return acpi_data_prop_read(acpi_device_data_of_node(fwnode
),
988 propname
, proptype
, val
, nval
);
992 * acpi_get_next_subnode - Return the next child node handle for a fwnode
993 * @fwnode: Firmware node to find the next child node for.
994 * @child: Handle to one of the device's child nodes or a null handle.
996 struct fwnode_handle
*acpi_get_next_subnode(const struct fwnode_handle
*fwnode
,
997 struct fwnode_handle
*child
)
999 const struct acpi_device
*adev
= to_acpi_device_node(fwnode
);
1000 const struct list_head
*head
;
1001 struct list_head
*next
;
1003 if (!child
|| is_acpi_device_node(child
)) {
1004 struct acpi_device
*child_adev
;
1007 head
= &adev
->children
;
1011 if (list_empty(head
))
1015 adev
= to_acpi_device_node(child
);
1016 next
= adev
->node
.next
;
1021 child_adev
= list_entry(next
, struct acpi_device
, node
);
1023 child_adev
= list_first_entry(head
, struct acpi_device
,
1026 return acpi_fwnode_handle(child_adev
);
1030 if (!child
|| is_acpi_data_node(child
)) {
1031 const struct acpi_data_node
*data
= to_acpi_data_node(fwnode
);
1032 struct acpi_data_node
*dn
;
1035 head
= &adev
->data
.subnodes
;
1037 head
= &data
->data
.subnodes
;
1041 if (list_empty(head
))
1045 dn
= to_acpi_data_node(child
);
1046 next
= dn
->sibling
.next
;
1050 dn
= list_entry(next
, struct acpi_data_node
, sibling
);
1052 dn
= list_first_entry(head
, struct acpi_data_node
, sibling
);
1060 * acpi_node_get_parent - Return parent fwnode of this fwnode
1061 * @fwnode: Firmware node whose parent to get
1063 * Returns parent node of an ACPI device or data firmware node or %NULL if
1066 struct fwnode_handle
*acpi_node_get_parent(const struct fwnode_handle
*fwnode
)
1068 if (is_acpi_data_node(fwnode
)) {
1069 /* All data nodes have parent pointer so just return that */
1070 return to_acpi_data_node(fwnode
)->parent
;
1071 } else if (is_acpi_device_node(fwnode
)) {
1072 acpi_handle handle
, parent_handle
;
1074 handle
= to_acpi_device_node(fwnode
)->handle
;
1075 if (ACPI_SUCCESS(acpi_get_parent(handle
, &parent_handle
))) {
1076 struct acpi_device
*adev
;
1078 if (!acpi_bus_get_device(parent_handle
, &adev
))
1079 return acpi_fwnode_handle(adev
);
1087 * Return true if the node is an ACPI graph node. Called on either ports
1090 static bool is_acpi_graph_node(struct fwnode_handle
*fwnode
,
1093 unsigned int len
= strlen(str
);
1096 if (!len
|| !is_acpi_data_node(fwnode
))
1099 name
= to_acpi_data_node(fwnode
)->name
;
1101 return (fwnode_property_present(fwnode
, "reg") &&
1102 !strncmp(name
, str
, len
) && name
[len
] == '@') ||
1103 fwnode_property_present(fwnode
, str
);
1107 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
1108 * @fwnode: Pointer to the parent firmware node
1109 * @prev: Previous endpoint node or %NULL to get the first
1111 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
1112 * %NULL if there is no next endpoint or in case of error. In case of success
1113 * the next endpoint is returned.
1115 static struct fwnode_handle
*acpi_graph_get_next_endpoint(
1116 const struct fwnode_handle
*fwnode
, struct fwnode_handle
*prev
)
1118 struct fwnode_handle
*port
= NULL
;
1119 struct fwnode_handle
*endpoint
;
1123 port
= fwnode_get_next_child_node(fwnode
, port
);
1125 * The names of the port nodes begin with "port@"
1126 * followed by the number of the port node and they also
1127 * have a "reg" property that also has the number of the
1128 * port node. For compatibility reasons a node is also
1129 * recognised as a port node from the "port" property.
1131 if (is_acpi_graph_node(port
, "port"))
1135 port
= fwnode_get_parent(prev
);
1141 endpoint
= fwnode_get_next_child_node(port
, prev
);
1143 port
= fwnode_get_next_child_node(fwnode
, port
);
1146 if (is_acpi_graph_node(port
, "port"))
1147 endpoint
= fwnode_get_next_child_node(port
, NULL
);
1151 * The names of the endpoint nodes begin with "endpoint@" followed by
1152 * the number of the endpoint node and they also have a "reg" property
1153 * that also has the number of the endpoint node. For compatibility
1154 * reasons a node is also recognised as an endpoint node from the
1155 * "endpoint" property.
1157 if (!is_acpi_graph_node(endpoint
, "endpoint"))
1164 * acpi_graph_get_child_prop_value - Return a child with a given property value
1165 * @fwnode: device fwnode
1166 * @prop_name: The name of the property to look for
1167 * @val: the desired property value
1169 * Return the port node corresponding to a given port number. Returns
1170 * the child node on success, NULL otherwise.
1172 static struct fwnode_handle
*acpi_graph_get_child_prop_value(
1173 const struct fwnode_handle
*fwnode
, const char *prop_name
,
1176 struct fwnode_handle
*child
;
1178 fwnode_for_each_child_node(fwnode
, child
) {
1181 if (fwnode_property_read_u32(child
, prop_name
, &nr
))
1193 * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
1194 * @fwnode: Endpoint firmware node pointing to a remote device
1195 * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
1197 * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
1199 static struct fwnode_handle
*
1200 acpi_graph_get_remote_endpoint(const struct fwnode_handle
*__fwnode
)
1202 struct fwnode_handle
*fwnode
;
1203 unsigned int port_nr
, endpoint_nr
;
1204 struct fwnode_reference_args args
;
1207 memset(&args
, 0, sizeof(args
));
1208 ret
= acpi_node_get_property_reference(__fwnode
, "remote-endpoint", 0,
1213 /* Direct endpoint reference? */
1214 if (!is_acpi_device_node(args
.fwnode
))
1215 return args
.nargs
? NULL
: args
.fwnode
;
1218 * Always require two arguments with the reference: port and
1221 if (args
.nargs
!= 2)
1224 fwnode
= args
.fwnode
;
1225 port_nr
= args
.args
[0];
1226 endpoint_nr
= args
.args
[1];
1228 fwnode
= acpi_graph_get_child_prop_value(fwnode
, "port", port_nr
);
1230 return acpi_graph_get_child_prop_value(fwnode
, "endpoint", endpoint_nr
);
1233 static bool acpi_fwnode_device_is_available(const struct fwnode_handle
*fwnode
)
1235 if (!is_acpi_device_node(fwnode
))
1238 return acpi_device_is_present(to_acpi_device_node(fwnode
));
1241 static bool acpi_fwnode_property_present(const struct fwnode_handle
*fwnode
,
1242 const char *propname
)
1244 return !acpi_node_prop_get(fwnode
, propname
, NULL
);
1248 acpi_fwnode_property_read_int_array(const struct fwnode_handle
*fwnode
,
1249 const char *propname
,
1250 unsigned int elem_size
, void *val
,
1253 enum dev_prop_type type
;
1255 switch (elem_size
) {
1260 type
= DEV_PROP_U16
;
1263 type
= DEV_PROP_U32
;
1266 type
= DEV_PROP_U64
;
1272 return acpi_node_prop_read(fwnode
, propname
, type
, val
, nval
);
1276 acpi_fwnode_property_read_string_array(const struct fwnode_handle
*fwnode
,
1277 const char *propname
, const char **val
,
1280 return acpi_node_prop_read(fwnode
, propname
, DEV_PROP_STRING
,
1285 acpi_fwnode_get_reference_args(const struct fwnode_handle
*fwnode
,
1286 const char *prop
, const char *nargs_prop
,
1287 unsigned int args_count
, unsigned int index
,
1288 struct fwnode_reference_args
*args
)
1290 return __acpi_node_get_property_reference(fwnode
, prop
, index
,
1294 static struct fwnode_handle
*
1295 acpi_fwnode_get_parent(struct fwnode_handle
*fwnode
)
1297 return acpi_node_get_parent(fwnode
);
1300 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle
*fwnode
,
1301 struct fwnode_endpoint
*endpoint
)
1303 struct fwnode_handle
*port_fwnode
= fwnode_get_parent(fwnode
);
1305 endpoint
->local_fwnode
= fwnode
;
1307 if (fwnode_property_read_u32(port_fwnode
, "reg", &endpoint
->port
))
1308 fwnode_property_read_u32(port_fwnode
, "port", &endpoint
->port
);
1309 if (fwnode_property_read_u32(fwnode
, "reg", &endpoint
->id
))
1310 fwnode_property_read_u32(fwnode
, "endpoint", &endpoint
->id
);
1316 acpi_fwnode_device_get_match_data(const struct fwnode_handle
*fwnode
,
1317 const struct device
*dev
)
1319 return acpi_device_get_match_data(dev
);
1322 #define DECLARE_ACPI_FWNODE_OPS(ops) \
1323 const struct fwnode_operations ops = { \
1324 .device_is_available = acpi_fwnode_device_is_available, \
1325 .device_get_match_data = acpi_fwnode_device_get_match_data, \
1326 .property_present = acpi_fwnode_property_present, \
1327 .property_read_int_array = \
1328 acpi_fwnode_property_read_int_array, \
1329 .property_read_string_array = \
1330 acpi_fwnode_property_read_string_array, \
1331 .get_parent = acpi_node_get_parent, \
1332 .get_next_child_node = acpi_get_next_subnode, \
1333 .get_named_child_node = acpi_fwnode_get_named_child_node, \
1334 .get_reference_args = acpi_fwnode_get_reference_args, \
1335 .graph_get_next_endpoint = \
1336 acpi_graph_get_next_endpoint, \
1337 .graph_get_remote_endpoint = \
1338 acpi_graph_get_remote_endpoint, \
1339 .graph_get_port_parent = acpi_fwnode_get_parent, \
1340 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1342 EXPORT_SYMBOL_GPL(ops)
1344 DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops
);
1345 DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops
);
1346 const struct fwnode_operations acpi_static_fwnode_ops
;
1348 bool is_acpi_device_node(const struct fwnode_handle
*fwnode
)
1350 return !IS_ERR_OR_NULL(fwnode
) &&
1351 fwnode
->ops
== &acpi_device_fwnode_ops
;
1353 EXPORT_SYMBOL(is_acpi_device_node
);
1355 bool is_acpi_data_node(const struct fwnode_handle
*fwnode
)
1357 return !IS_ERR_OR_NULL(fwnode
) && fwnode
->ops
== &acpi_data_fwnode_ops
;
1359 EXPORT_SYMBOL(is_acpi_data_node
);