2 * ACPI device specific properties support.
4 * Copyright (C) 2014, Intel Corporation
7 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
8 * Darren Hart <dvhart@linux.intel.com>
9 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/acpi.h>
17 #include <linux/device.h>
18 #include <linux/export.h>
22 static int acpi_data_get_property_array(struct acpi_device_data
*data
,
24 acpi_object_type type
,
25 const union acpi_object
**obj
);
27 /* ACPI _DSD device properties UUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
28 static const u8 prp_uuid
[16] = {
29 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d,
30 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01
32 /* ACPI _DSD data subnodes UUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
33 static const u8 ads_uuid
[16] = {
34 0xe6, 0xe3, 0xb8, 0xdb, 0x86, 0x58, 0xa6, 0x4b,
35 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b
38 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope
,
39 const union acpi_object
*desc
,
40 struct acpi_device_data
*data
,
41 struct fwnode_handle
*parent
);
42 static bool acpi_extract_properties(const union acpi_object
*desc
,
43 struct acpi_device_data
*data
);
45 static bool acpi_nondev_subnode_extract(const union acpi_object
*desc
,
47 const union acpi_object
*link
,
48 struct list_head
*list
,
49 struct fwnode_handle
*parent
)
51 struct acpi_data_node
*dn
;
54 dn
= kzalloc(sizeof(*dn
), GFP_KERNEL
);
58 dn
->name
= link
->package
.elements
[0].string
.pointer
;
59 dn
->fwnode
.type
= FWNODE_ACPI_DATA
;
60 dn
->fwnode
.ops
= &acpi_fwnode_ops
;
62 INIT_LIST_HEAD(&dn
->data
.subnodes
);
64 result
= acpi_extract_properties(desc
, &dn
->data
);
71 * The scope for the subnode object lookup is the one of the
72 * namespace node (device) containing the object that has
73 * returned the package. That is, it's the scope of that
76 status
= acpi_get_parent(handle
, &scope
);
77 if (ACPI_SUCCESS(status
)
78 && acpi_enumerate_nondev_subnodes(scope
, desc
, &dn
->data
,
81 } else if (acpi_enumerate_nondev_subnodes(NULL
, desc
, &dn
->data
,
88 dn
->data
.pointer
= desc
;
89 list_add_tail(&dn
->sibling
, list
);
94 acpi_handle_debug(handle
, "Invalid properties/subnodes data, skipping\n");
98 static bool acpi_nondev_subnode_data_ok(acpi_handle handle
,
99 const union acpi_object
*link
,
100 struct list_head
*list
,
101 struct fwnode_handle
*parent
)
103 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
106 status
= acpi_evaluate_object_typed(handle
, NULL
, NULL
, &buf
,
108 if (ACPI_FAILURE(status
))
111 if (acpi_nondev_subnode_extract(buf
.pointer
, handle
, link
, list
,
115 ACPI_FREE(buf
.pointer
);
119 static bool acpi_nondev_subnode_ok(acpi_handle scope
,
120 const union acpi_object
*link
,
121 struct list_head
*list
,
122 struct fwnode_handle
*parent
)
130 status
= acpi_get_handle(scope
, link
->package
.elements
[1].string
.pointer
,
132 if (ACPI_FAILURE(status
))
135 return acpi_nondev_subnode_data_ok(handle
, link
, list
, parent
);
138 static int acpi_add_nondev_subnodes(acpi_handle scope
,
139 const union acpi_object
*links
,
140 struct list_head
*list
,
141 struct fwnode_handle
*parent
)
146 for (i
= 0; i
< links
->package
.count
; i
++) {
147 const union acpi_object
*link
, *desc
;
151 link
= &links
->package
.elements
[i
];
152 /* Only two elements allowed. */
153 if (link
->package
.count
!= 2)
156 /* The first one must be a string. */
157 if (link
->package
.elements
[0].type
!= ACPI_TYPE_STRING
)
160 /* The second one may be a string, a reference or a package. */
161 switch (link
->package
.elements
[1].type
) {
162 case ACPI_TYPE_STRING
:
163 result
= acpi_nondev_subnode_ok(scope
, link
, list
,
166 case ACPI_TYPE_LOCAL_REFERENCE
:
167 handle
= link
->package
.elements
[1].reference
.handle
;
168 result
= acpi_nondev_subnode_data_ok(handle
, link
, list
,
171 case ACPI_TYPE_PACKAGE
:
172 desc
= &link
->package
.elements
[1];
173 result
= acpi_nondev_subnode_extract(desc
, NULL
, link
,
186 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope
,
187 const union acpi_object
*desc
,
188 struct acpi_device_data
*data
,
189 struct fwnode_handle
*parent
)
193 /* Look for the ACPI data subnodes UUID. */
194 for (i
= 0; i
< desc
->package
.count
; i
+= 2) {
195 const union acpi_object
*uuid
, *links
;
197 uuid
= &desc
->package
.elements
[i
];
198 links
= &desc
->package
.elements
[i
+ 1];
201 * The first element must be a UUID and the second one must be
204 if (uuid
->type
!= ACPI_TYPE_BUFFER
|| uuid
->buffer
.length
!= 16
205 || links
->type
!= ACPI_TYPE_PACKAGE
)
208 if (memcmp(uuid
->buffer
.pointer
, ads_uuid
, sizeof(ads_uuid
)))
211 return acpi_add_nondev_subnodes(scope
, links
, &data
->subnodes
,
218 static bool acpi_property_value_ok(const union acpi_object
*value
)
223 * The value must be an integer, a string, a reference, or a package
224 * whose every element must be an integer, a string, or a reference.
226 switch (value
->type
) {
227 case ACPI_TYPE_INTEGER
:
228 case ACPI_TYPE_STRING
:
229 case ACPI_TYPE_LOCAL_REFERENCE
:
232 case ACPI_TYPE_PACKAGE
:
233 for (j
= 0; j
< value
->package
.count
; j
++)
234 switch (value
->package
.elements
[j
].type
) {
235 case ACPI_TYPE_INTEGER
:
236 case ACPI_TYPE_STRING
:
237 case ACPI_TYPE_LOCAL_REFERENCE
:
249 static bool acpi_properties_format_valid(const union acpi_object
*properties
)
253 for (i
= 0; i
< properties
->package
.count
; i
++) {
254 const union acpi_object
*property
;
256 property
= &properties
->package
.elements
[i
];
258 * Only two elements allowed, the first one must be a string and
259 * the second one has to satisfy certain conditions.
261 if (property
->package
.count
!= 2
262 || property
->package
.elements
[0].type
!= ACPI_TYPE_STRING
263 || !acpi_property_value_ok(&property
->package
.elements
[1]))
269 static void acpi_init_of_compatible(struct acpi_device
*adev
)
271 const union acpi_object
*of_compatible
;
274 ret
= acpi_data_get_property_array(&adev
->data
, "compatible",
275 ACPI_TYPE_STRING
, &of_compatible
);
277 ret
= acpi_dev_get_property(adev
, "compatible",
278 ACPI_TYPE_STRING
, &of_compatible
);
281 && adev
->parent
->flags
.of_compatible_ok
)
287 adev
->data
.of_compatible
= of_compatible
;
290 adev
->flags
.of_compatible_ok
= 1;
293 static bool acpi_extract_properties(const union acpi_object
*desc
,
294 struct acpi_device_data
*data
)
298 if (desc
->package
.count
% 2)
301 /* Look for the device properties UUID. */
302 for (i
= 0; i
< desc
->package
.count
; i
+= 2) {
303 const union acpi_object
*uuid
, *properties
;
305 uuid
= &desc
->package
.elements
[i
];
306 properties
= &desc
->package
.elements
[i
+ 1];
309 * The first element must be a UUID and the second one must be
312 if (uuid
->type
!= ACPI_TYPE_BUFFER
|| uuid
->buffer
.length
!= 16
313 || properties
->type
!= ACPI_TYPE_PACKAGE
)
316 if (memcmp(uuid
->buffer
.pointer
, prp_uuid
, sizeof(prp_uuid
)))
320 * We found the matching UUID. Now validate the format of the
321 * package immediately following it.
323 if (!acpi_properties_format_valid(properties
))
326 data
->properties
= properties
;
333 void acpi_init_properties(struct acpi_device
*adev
)
335 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
336 struct acpi_hardware_id
*hwid
;
338 bool acpi_of
= false;
340 INIT_LIST_HEAD(&adev
->data
.subnodes
);
346 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
347 * Device Tree compatible properties for this device.
349 list_for_each_entry(hwid
, &adev
->pnp
.ids
, list
) {
350 if (!strcmp(hwid
->id
, ACPI_DT_NAMESPACE_HID
)) {
356 status
= acpi_evaluate_object_typed(adev
->handle
, "_DSD", NULL
, &buf
,
358 if (ACPI_FAILURE(status
))
361 if (acpi_extract_properties(buf
.pointer
, &adev
->data
)) {
362 adev
->data
.pointer
= buf
.pointer
;
364 acpi_init_of_compatible(adev
);
366 if (acpi_enumerate_nondev_subnodes(adev
->handle
, buf
.pointer
,
367 &adev
->data
, acpi_fwnode_handle(adev
)))
368 adev
->data
.pointer
= buf
.pointer
;
370 if (!adev
->data
.pointer
) {
371 acpi_handle_debug(adev
->handle
, "Invalid _DSD data, skipping\n");
372 ACPI_FREE(buf
.pointer
);
376 if (acpi_of
&& !adev
->flags
.of_compatible_ok
)
377 acpi_handle_info(adev
->handle
,
378 ACPI_DT_NAMESPACE_HID
" requires 'compatible' property\n");
381 static void acpi_destroy_nondev_subnodes(struct list_head
*list
)
383 struct acpi_data_node
*dn
, *next
;
385 if (list_empty(list
))
388 list_for_each_entry_safe_reverse(dn
, next
, list
, sibling
) {
389 acpi_destroy_nondev_subnodes(&dn
->data
.subnodes
);
390 wait_for_completion(&dn
->kobj_done
);
391 list_del(&dn
->sibling
);
392 ACPI_FREE((void *)dn
->data
.pointer
);
397 void acpi_free_properties(struct acpi_device
*adev
)
399 acpi_destroy_nondev_subnodes(&adev
->data
.subnodes
);
400 ACPI_FREE((void *)adev
->data
.pointer
);
401 adev
->data
.of_compatible
= NULL
;
402 adev
->data
.pointer
= NULL
;
403 adev
->data
.properties
= NULL
;
407 * acpi_data_get_property - return an ACPI property with given name
408 * @data: ACPI device deta object to get the property from
409 * @name: Name of the property
410 * @type: Expected property type
411 * @obj: Location to store the property value (if not %NULL)
413 * Look up a property with @name and store a pointer to the resulting ACPI
414 * object at the location pointed to by @obj if found.
416 * Callers must not attempt to free the returned objects. These objects will be
417 * freed by the ACPI core automatically during the removal of @data.
419 * Return: %0 if property with @name has been found (success),
420 * %-EINVAL if the arguments are invalid,
421 * %-EINVAL if the property doesn't exist,
422 * %-EPROTO if the property value type doesn't match @type.
424 static int acpi_data_get_property(struct acpi_device_data
*data
,
425 const char *name
, acpi_object_type type
,
426 const union acpi_object
**obj
)
428 const union acpi_object
*properties
;
434 if (!data
->pointer
|| !data
->properties
)
437 properties
= data
->properties
;
438 for (i
= 0; i
< properties
->package
.count
; i
++) {
439 const union acpi_object
*propname
, *propvalue
;
440 const union acpi_object
*property
;
442 property
= &properties
->package
.elements
[i
];
444 propname
= &property
->package
.elements
[0];
445 propvalue
= &property
->package
.elements
[1];
447 if (!strcmp(name
, propname
->string
.pointer
)) {
448 if (type
!= ACPI_TYPE_ANY
&& propvalue
->type
!= type
)
460 * acpi_dev_get_property - return an ACPI property with given name.
461 * @adev: ACPI device 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 int acpi_dev_get_property(struct acpi_device
*adev
, const char *name
,
467 acpi_object_type type
, const union acpi_object
**obj
)
469 return adev
? acpi_data_get_property(&adev
->data
, name
, type
, obj
) : -EINVAL
;
471 EXPORT_SYMBOL_GPL(acpi_dev_get_property
);
473 static struct acpi_device_data
*acpi_device_data_of_node(struct fwnode_handle
*fwnode
)
475 if (fwnode
->type
== FWNODE_ACPI
) {
476 struct acpi_device
*adev
= to_acpi_device_node(fwnode
);
478 } else if (fwnode
->type
== FWNODE_ACPI_DATA
) {
479 struct acpi_data_node
*dn
= to_acpi_data_node(fwnode
);
486 * acpi_node_prop_get - return an ACPI property with given name.
487 * @fwnode: Firmware node to get the property from.
488 * @propname: Name of the property.
489 * @valptr: Location to store a pointer to the property value (if not %NULL).
491 int acpi_node_prop_get(struct fwnode_handle
*fwnode
, const char *propname
,
494 return acpi_data_get_property(acpi_device_data_of_node(fwnode
),
495 propname
, ACPI_TYPE_ANY
,
496 (const union acpi_object
**)valptr
);
500 * acpi_data_get_property_array - return an ACPI array property with given name
501 * @adev: ACPI data object to get the property from
502 * @name: Name of the property
503 * @type: Expected type of array elements
504 * @obj: Location to store a pointer to the property value (if not NULL)
506 * Look up an array property with @name and store a pointer to the resulting
507 * ACPI object at the location pointed to by @obj if found.
509 * Callers must not attempt to free the returned objects. Those objects will be
510 * freed by the ACPI core automatically during the removal of @data.
512 * Return: %0 if array property (package) with @name has been found (success),
513 * %-EINVAL if the arguments are invalid,
514 * %-EINVAL if the property doesn't exist,
515 * %-EPROTO if the property is not a package or the type of its elements
516 * doesn't match @type.
518 static int acpi_data_get_property_array(struct acpi_device_data
*data
,
520 acpi_object_type type
,
521 const union acpi_object
**obj
)
523 const union acpi_object
*prop
;
526 ret
= acpi_data_get_property(data
, name
, ACPI_TYPE_PACKAGE
, &prop
);
530 if (type
!= ACPI_TYPE_ANY
) {
531 /* Check that all elements are of correct type. */
532 for (i
= 0; i
< prop
->package
.count
; i
++)
533 if (prop
->package
.elements
[i
].type
!= type
)
543 * __acpi_node_get_property_reference - returns handle to the referenced object
544 * @fwnode: Firmware node to get the property from
545 * @propname: Name of the property
546 * @index: Index of the reference to return
547 * @num_args: Maximum number of arguments after each reference
548 * @args: Location to store the returned reference with optional arguments
550 * Find property with @name, verifify that it is a package containing at least
551 * one object reference and if so, store the ACPI device object pointer to the
552 * target object in @args->adev. If the reference includes arguments, store
553 * them in the @args->args[] array.
555 * If there's more than one reference in the property value package, @index is
556 * used to select the one to return.
558 * It is possible to leave holes in the property value set like in the
571 * Calling this function with index %2 return %-ENOENT and with index %3
572 * returns the last entry. If the property does not contain any more values
573 * %-ENODATA is returned. The NULL entry must be single integer and
574 * preferably contain value %0.
576 * Return: %0 on success, negative error code on failure.
578 int __acpi_node_get_property_reference(struct fwnode_handle
*fwnode
,
579 const char *propname
, size_t index
, size_t num_args
,
580 struct acpi_reference_args
*args
)
582 const union acpi_object
*element
, *end
;
583 const union acpi_object
*obj
;
584 struct acpi_device_data
*data
;
585 struct acpi_device
*device
;
588 data
= acpi_device_data_of_node(fwnode
);
592 ret
= acpi_data_get_property(data
, propname
, ACPI_TYPE_ANY
, &obj
);
597 * The simplest case is when the value is a single reference. Just
598 * return that reference then.
600 if (obj
->type
== ACPI_TYPE_LOCAL_REFERENCE
) {
604 ret
= acpi_bus_get_device(obj
->reference
.handle
, &device
);
614 * If it is not a single reference, then it is a package of
615 * references followed by number of ints as follows:
617 * Package () { REF, INT, REF, INT, INT }
619 * The index argument is then used to determine which reference
620 * the caller wants (along with the arguments).
622 if (obj
->type
!= ACPI_TYPE_PACKAGE
|| index
>= obj
->package
.count
)
625 element
= obj
->package
.elements
;
626 end
= element
+ obj
->package
.count
;
628 while (element
< end
) {
631 if (element
->type
== ACPI_TYPE_LOCAL_REFERENCE
) {
632 ret
= acpi_bus_get_device(element
->reference
.handle
,
640 /* assume following integer elements are all args */
641 for (i
= 0; element
+ i
< end
&& i
< num_args
; i
++) {
642 int type
= element
[i
].type
;
644 if (type
== ACPI_TYPE_INTEGER
)
646 else if (type
== ACPI_TYPE_LOCAL_REFERENCE
)
652 if (nargs
> MAX_ACPI_REFERENCE_ARGS
)
658 for (i
= 0; i
< nargs
; i
++)
659 args
->args
[i
] = element
[i
].integer
.value
;
665 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
678 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference
);
680 static int acpi_data_prop_read_single(struct acpi_device_data
*data
,
681 const char *propname
,
682 enum dev_prop_type proptype
, void *val
)
684 const union acpi_object
*obj
;
690 if (proptype
>= DEV_PROP_U8
&& proptype
<= DEV_PROP_U64
) {
691 ret
= acpi_data_get_property(data
, propname
, ACPI_TYPE_INTEGER
, &obj
);
697 if (obj
->integer
.value
> U8_MAX
)
699 *(u8
*)val
= obj
->integer
.value
;
702 if (obj
->integer
.value
> U16_MAX
)
704 *(u16
*)val
= obj
->integer
.value
;
707 if (obj
->integer
.value
> U32_MAX
)
709 *(u32
*)val
= obj
->integer
.value
;
712 *(u64
*)val
= obj
->integer
.value
;
715 } else if (proptype
== DEV_PROP_STRING
) {
716 ret
= acpi_data_get_property(data
, propname
, ACPI_TYPE_STRING
, &obj
);
720 *(char **)val
= obj
->string
.pointer
;
729 int acpi_dev_prop_read_single(struct acpi_device
*adev
, const char *propname
,
730 enum dev_prop_type proptype
, void *val
)
737 ret
= acpi_data_prop_read_single(&adev
->data
, propname
, proptype
, val
);
738 if (ret
< 0 || proptype
!= ACPI_TYPE_STRING
)
743 static int acpi_copy_property_array_u8(const union acpi_object
*items
, u8
*val
,
748 for (i
= 0; i
< nval
; i
++) {
749 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
751 if (items
[i
].integer
.value
> U8_MAX
)
754 val
[i
] = items
[i
].integer
.value
;
759 static int acpi_copy_property_array_u16(const union acpi_object
*items
,
760 u16
*val
, size_t nval
)
764 for (i
= 0; i
< nval
; i
++) {
765 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
767 if (items
[i
].integer
.value
> U16_MAX
)
770 val
[i
] = items
[i
].integer
.value
;
775 static int acpi_copy_property_array_u32(const union acpi_object
*items
,
776 u32
*val
, size_t nval
)
780 for (i
= 0; i
< nval
; i
++) {
781 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
783 if (items
[i
].integer
.value
> U32_MAX
)
786 val
[i
] = items
[i
].integer
.value
;
791 static int acpi_copy_property_array_u64(const union acpi_object
*items
,
792 u64
*val
, size_t nval
)
796 for (i
= 0; i
< nval
; i
++) {
797 if (items
[i
].type
!= ACPI_TYPE_INTEGER
)
800 val
[i
] = items
[i
].integer
.value
;
805 static int acpi_copy_property_array_string(const union acpi_object
*items
,
806 char **val
, size_t nval
)
810 for (i
= 0; i
< nval
; i
++) {
811 if (items
[i
].type
!= ACPI_TYPE_STRING
)
814 val
[i
] = items
[i
].string
.pointer
;
819 static int acpi_data_prop_read(struct acpi_device_data
*data
,
820 const char *propname
,
821 enum dev_prop_type proptype
,
822 void *val
, size_t nval
)
824 const union acpi_object
*obj
;
825 const union acpi_object
*items
;
828 if (val
&& nval
== 1) {
829 ret
= acpi_data_prop_read_single(data
, propname
, proptype
, val
);
834 ret
= acpi_data_get_property_array(data
, propname
, ACPI_TYPE_ANY
, &obj
);
839 return obj
->package
.count
;
841 if (proptype
!= DEV_PROP_STRING
&& nval
> obj
->package
.count
)
846 items
= obj
->package
.elements
;
850 ret
= acpi_copy_property_array_u8(items
, (u8
*)val
, nval
);
853 ret
= acpi_copy_property_array_u16(items
, (u16
*)val
, nval
);
856 ret
= acpi_copy_property_array_u32(items
, (u32
*)val
, nval
);
859 ret
= acpi_copy_property_array_u64(items
, (u64
*)val
, nval
);
861 case DEV_PROP_STRING
:
862 ret
= acpi_copy_property_array_string(
864 min_t(u32
, nval
, obj
->package
.count
));
873 int acpi_dev_prop_read(struct acpi_device
*adev
, const char *propname
,
874 enum dev_prop_type proptype
, void *val
, size_t nval
)
876 return adev
? acpi_data_prop_read(&adev
->data
, propname
, proptype
, val
, nval
) : -EINVAL
;
880 * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
881 * @fwnode: Firmware node to get the property from.
882 * @propname: Name of the property.
883 * @proptype: Expected property type.
884 * @val: Location to store the property value (if not %NULL).
885 * @nval: Size of the array pointed to by @val.
887 * If @val is %NULL, return the number of array elements comprising the value
888 * of the property. Otherwise, read at most @nval values to the array at the
889 * location pointed to by @val.
891 int acpi_node_prop_read(struct fwnode_handle
*fwnode
, const char *propname
,
892 enum dev_prop_type proptype
, void *val
, size_t nval
)
894 return acpi_data_prop_read(acpi_device_data_of_node(fwnode
),
895 propname
, proptype
, val
, nval
);
899 * acpi_get_next_subnode - Return the next child node handle for a fwnode
900 * @fwnode: Firmware node to find the next child node for.
901 * @child: Handle to one of the device's child nodes or a null handle.
903 struct fwnode_handle
*acpi_get_next_subnode(struct fwnode_handle
*fwnode
,
904 struct fwnode_handle
*child
)
906 struct acpi_device
*adev
= to_acpi_device_node(fwnode
);
907 struct list_head
*head
, *next
;
909 if (!child
|| child
->type
== FWNODE_ACPI
) {
911 head
= &adev
->children
;
915 if (list_empty(head
))
919 adev
= to_acpi_device_node(child
);
920 next
= adev
->node
.next
;
925 adev
= list_entry(next
, struct acpi_device
, node
);
927 adev
= list_first_entry(head
, struct acpi_device
, node
);
929 return acpi_fwnode_handle(adev
);
933 if (!child
|| child
->type
== FWNODE_ACPI_DATA
) {
934 struct acpi_data_node
*data
= to_acpi_data_node(fwnode
);
935 struct acpi_data_node
*dn
;
938 head
= &adev
->data
.subnodes
;
940 head
= &data
->data
.subnodes
;
944 if (list_empty(head
))
948 dn
= to_acpi_data_node(child
);
949 next
= dn
->sibling
.next
;
953 dn
= list_entry(next
, struct acpi_data_node
, sibling
);
955 dn
= list_first_entry(head
, struct acpi_data_node
, sibling
);
963 * acpi_node_get_parent - Return parent fwnode of this fwnode
964 * @fwnode: Firmware node whose parent to get
966 * Returns parent node of an ACPI device or data firmware node or %NULL if
969 struct fwnode_handle
*acpi_node_get_parent(struct fwnode_handle
*fwnode
)
971 if (is_acpi_data_node(fwnode
)) {
972 /* All data nodes have parent pointer so just return that */
973 return to_acpi_data_node(fwnode
)->parent
;
974 } else if (is_acpi_device_node(fwnode
)) {
975 acpi_handle handle
, parent_handle
;
977 handle
= to_acpi_device_node(fwnode
)->handle
;
978 if (ACPI_SUCCESS(acpi_get_parent(handle
, &parent_handle
))) {
979 struct acpi_device
*adev
;
981 if (!acpi_bus_get_device(parent_handle
, &adev
))
982 return acpi_fwnode_handle(adev
);
990 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
991 * @fwnode: Pointer to the parent firmware node
992 * @prev: Previous endpoint node or %NULL to get the first
994 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
995 * %NULL if there is no next endpoint, ERR_PTR() in case of error. In case
996 * of success the next endpoint is returned.
998 struct fwnode_handle
*acpi_graph_get_next_endpoint(struct fwnode_handle
*fwnode
,
999 struct fwnode_handle
*prev
)
1001 struct fwnode_handle
*port
= NULL
;
1002 struct fwnode_handle
*endpoint
;
1006 port
= fwnode_get_next_child_node(fwnode
, port
);
1007 /* Ports must have port property */
1008 if (fwnode_property_present(port
, "port"))
1012 port
= fwnode_get_parent(prev
);
1018 endpoint
= fwnode_get_next_child_node(port
, prev
);
1020 port
= fwnode_get_next_child_node(fwnode
, port
);
1023 if (fwnode_property_present(port
, "port"))
1024 endpoint
= fwnode_get_next_child_node(port
, NULL
);
1028 /* Endpoints must have "endpoint" property */
1029 if (!fwnode_property_present(endpoint
, "endpoint"))
1030 return ERR_PTR(-EPROTO
);
1037 * acpi_graph_get_child_prop_value - Return a child with a given property value
1038 * @fwnode: device fwnode
1039 * @prop_name: The name of the property to look for
1040 * @val: the desired property value
1042 * Return the port node corresponding to a given port number. Returns
1043 * the child node on success, NULL otherwise.
1045 static struct fwnode_handle
*acpi_graph_get_child_prop_value(
1046 struct fwnode_handle
*fwnode
, const char *prop_name
, unsigned int val
)
1048 struct fwnode_handle
*child
;
1050 fwnode_for_each_child_node(fwnode
, child
) {
1053 if (!fwnode_property_read_u32(fwnode
, prop_name
, &nr
))
1065 * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
1066 * @fwnode: Endpoint firmware node pointing to a remote device
1067 * @parent: Firmware node of remote port parent is filled here if not %NULL
1068 * @port: Firmware node of remote port is filled here if not %NULL
1069 * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
1071 * Function parses remote end of ACPI firmware remote endpoint and fills in
1072 * fields requested by the caller. Returns %0 in case of success and
1073 * negative errno otherwise.
1075 int acpi_graph_get_remote_endpoint(struct fwnode_handle
*fwnode
,
1076 struct fwnode_handle
**parent
,
1077 struct fwnode_handle
**port
,
1078 struct fwnode_handle
**endpoint
)
1080 unsigned int port_nr
, endpoint_nr
;
1081 struct acpi_reference_args args
;
1084 memset(&args
, 0, sizeof(args
));
1085 ret
= acpi_node_get_property_reference(fwnode
, "remote-endpoint", 0,
1091 * Always require two arguments with the reference: port and
1094 if (args
.nargs
!= 2)
1097 fwnode
= acpi_fwnode_handle(args
.adev
);
1098 port_nr
= args
.args
[0];
1099 endpoint_nr
= args
.args
[1];
1104 if (!port
&& !endpoint
)
1107 fwnode
= acpi_graph_get_child_prop_value(fwnode
, "port", port_nr
);
1117 fwnode
= acpi_graph_get_child_prop_value(fwnode
, "endpoint",
1127 static bool acpi_fwnode_device_is_available(struct fwnode_handle
*fwnode
)
1129 if (!is_acpi_device_node(fwnode
))
1132 return acpi_device_is_present(to_acpi_device_node(fwnode
));
1135 static bool acpi_fwnode_property_present(struct fwnode_handle
*fwnode
,
1136 const char *propname
)
1138 return !acpi_node_prop_get(fwnode
, propname
, NULL
);
1141 static int acpi_fwnode_property_read_int_array(struct fwnode_handle
*fwnode
,
1142 const char *propname
,
1143 unsigned int elem_size
,
1144 void *val
, size_t nval
)
1146 enum dev_prop_type type
;
1148 switch (elem_size
) {
1153 type
= DEV_PROP_U16
;
1156 type
= DEV_PROP_U32
;
1159 type
= DEV_PROP_U64
;
1165 return acpi_node_prop_read(fwnode
, propname
, type
, val
, nval
);
1168 static int acpi_fwnode_property_read_string_array(struct fwnode_handle
*fwnode
,
1169 const char *propname
,
1170 const char **val
, size_t nval
)
1172 return acpi_node_prop_read(fwnode
, propname
, DEV_PROP_STRING
,
1176 static struct fwnode_handle
*
1177 acpi_fwnode_get_named_child_node(struct fwnode_handle
*fwnode
,
1178 const char *childname
)
1180 struct fwnode_handle
*child
;
1183 * Find first matching named child node of this fwnode.
1184 * For ACPI this will be a data only sub-node.
1186 fwnode_for_each_child_node(fwnode
, child
)
1187 if (acpi_data_node_match(child
, childname
))
1193 static struct fwnode_handle
*
1194 acpi_fwnode_graph_get_next_endpoint(struct fwnode_handle
*fwnode
,
1195 struct fwnode_handle
*prev
)
1197 struct fwnode_handle
*endpoint
;
1199 endpoint
= acpi_graph_get_next_endpoint(fwnode
, prev
);
1200 if (IS_ERR(endpoint
))
1206 static struct fwnode_handle
*
1207 acpi_fwnode_graph_get_remote_endpoint(struct fwnode_handle
*fwnode
)
1209 struct fwnode_handle
*endpoint
= NULL
;
1211 acpi_graph_get_remote_endpoint(fwnode
, NULL
, NULL
, &endpoint
);
1216 static int acpi_fwnode_graph_parse_endpoint(struct fwnode_handle
*fwnode
,
1217 struct fwnode_endpoint
*endpoint
)
1219 struct fwnode_handle
*port_fwnode
= fwnode_get_parent(fwnode
);
1221 endpoint
->local_fwnode
= fwnode
;
1223 fwnode_property_read_u32(port_fwnode
, "port", &endpoint
->port
);
1224 fwnode_property_read_u32(fwnode
, "endpoint", &endpoint
->id
);
1229 const struct fwnode_operations acpi_fwnode_ops
= {
1230 .device_is_available
= acpi_fwnode_device_is_available
,
1231 .property_present
= acpi_fwnode_property_present
,
1232 .property_read_int_array
= acpi_fwnode_property_read_int_array
,
1233 .property_read_string_array
= acpi_fwnode_property_read_string_array
,
1234 .get_parent
= acpi_node_get_parent
,
1235 .get_next_child_node
= acpi_get_next_subnode
,
1236 .get_named_child_node
= acpi_fwnode_get_named_child_node
,
1237 .graph_get_next_endpoint
= acpi_fwnode_graph_get_next_endpoint
,
1238 .graph_get_remote_endpoint
= acpi_fwnode_graph_get_remote_endpoint
,
1239 .graph_get_port_parent
= acpi_node_get_parent
,
1240 .graph_parse_endpoint
= acpi_fwnode_graph_parse_endpoint
,