]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/acpi/property.c
ACPI / property: Don't limit named child node matching to data nodes
[linux.git] / drivers / acpi / property.c
1 /*
2  * ACPI device specific properties support.
3  *
4  * Copyright (C) 2014, Intel Corporation
5  * All rights reserved.
6  *
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>
10  *
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.
14  */
15
16 #include <linux/acpi.h>
17 #include <linux/device.h>
18 #include <linux/export.h>
19
20 #include "internal.h"
21
22 static int acpi_data_get_property_array(const struct acpi_device_data *data,
23                                         const char *name,
24                                         acpi_object_type type,
25                                         const union acpi_object **obj);
26
27 /*
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.
34  */
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),
45 };
46
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);
51
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);
58
59 static bool acpi_nondev_subnode_extract(const union acpi_object *desc,
60                                         acpi_handle handle,
61                                         const union acpi_object *link,
62                                         struct list_head *list,
63                                         struct fwnode_handle *parent)
64 {
65         struct acpi_data_node *dn;
66         bool result;
67
68         dn = kzalloc(sizeof(*dn), GFP_KERNEL);
69         if (!dn)
70                 return false;
71
72         dn->name = link->package.elements[0].string.pointer;
73         dn->fwnode.ops = &acpi_data_fwnode_ops;
74         dn->parent = parent;
75         INIT_LIST_HEAD(&dn->data.properties);
76         INIT_LIST_HEAD(&dn->data.subnodes);
77
78         result = acpi_extract_properties(desc, &dn->data);
79
80         if (handle) {
81                 acpi_handle scope;
82                 acpi_status status;
83
84                 /*
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
88                  * object's parent.
89                  */
90                 status = acpi_get_parent(handle, &scope);
91                 if (ACPI_SUCCESS(status)
92                     && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
93                                                       &dn->fwnode))
94                         result = true;
95         } else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
96                                                   &dn->fwnode)) {
97                 result = true;
98         }
99
100         if (result) {
101                 dn->handle = handle;
102                 dn->data.pointer = desc;
103                 list_add_tail(&dn->sibling, list);
104                 return true;
105         }
106
107         kfree(dn);
108         acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
109         return false;
110 }
111
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)
116 {
117         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
118         acpi_status status;
119
120         status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
121                                             ACPI_TYPE_PACKAGE);
122         if (ACPI_FAILURE(status))
123                 return false;
124
125         if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
126                                         parent))
127                 return true;
128
129         ACPI_FREE(buf.pointer);
130         return false;
131 }
132
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)
137 {
138         acpi_handle handle;
139         acpi_status status;
140
141         if (!scope)
142                 return false;
143
144         status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
145                                  &handle);
146         if (ACPI_FAILURE(status))
147                 return false;
148
149         return acpi_nondev_subnode_data_ok(handle, link, list, parent);
150 }
151
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)
156 {
157         bool ret = false;
158         int i;
159
160         for (i = 0; i < links->package.count; i++) {
161                 const union acpi_object *link, *desc;
162                 acpi_handle handle;
163                 bool result;
164
165                 link = &links->package.elements[i];
166                 /* Only two elements allowed. */
167                 if (link->package.count != 2)
168                         continue;
169
170                 /* The first one must be a string. */
171                 if (link->package.elements[0].type != ACPI_TYPE_STRING)
172                         continue;
173
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,
178                                                          parent);
179                         break;
180                 case ACPI_TYPE_LOCAL_REFERENCE:
181                         handle = link->package.elements[1].reference.handle;
182                         result = acpi_nondev_subnode_data_ok(handle, link, list,
183                                                              parent);
184                         break;
185                 case ACPI_TYPE_PACKAGE:
186                         desc = &link->package.elements[1];
187                         result = acpi_nondev_subnode_extract(desc, NULL, link,
188                                                              list, parent);
189                         break;
190                 default:
191                         result = false;
192                         break;
193                 }
194                 ret = ret || result;
195         }
196
197         return ret;
198 }
199
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)
204 {
205         int i;
206
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;
210
211                 guid = &desc->package.elements[i];
212                 links = &desc->package.elements[i + 1];
213
214                 /*
215                  * The first element must be a GUID and the second one must be
216                  * a package.
217                  */
218                 if (guid->type != ACPI_TYPE_BUFFER ||
219                     guid->buffer.length != 16 ||
220                     links->type != ACPI_TYPE_PACKAGE)
221                         break;
222
223                 if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid))
224                         continue;
225
226                 return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
227                                                 parent);
228         }
229
230         return false;
231 }
232
233 static bool acpi_property_value_ok(const union acpi_object *value)
234 {
235         int j;
236
237         /*
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.
240          */
241         switch (value->type) {
242         case ACPI_TYPE_INTEGER:
243         case ACPI_TYPE_STRING:
244         case ACPI_TYPE_LOCAL_REFERENCE:
245                 return true;
246
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:
253                                 continue;
254
255                         default:
256                                 return false;
257                         }
258
259                 return true;
260         }
261         return false;
262 }
263
264 static bool acpi_properties_format_valid(const union acpi_object *properties)
265 {
266         int i;
267
268         for (i = 0; i < properties->package.count; i++) {
269                 const union acpi_object *property;
270
271                 property = &properties->package.elements[i];
272                 /*
273                  * Only two elements allowed, the first one must be a string and
274                  * the second one has to satisfy certain conditions.
275                  */
276                 if (property->package.count != 2
277                     || property->package.elements[0].type != ACPI_TYPE_STRING
278                     || !acpi_property_value_ok(&property->package.elements[1]))
279                         return false;
280         }
281         return true;
282 }
283
284 static void acpi_init_of_compatible(struct acpi_device *adev)
285 {
286         const union acpi_object *of_compatible;
287         int ret;
288
289         ret = acpi_data_get_property_array(&adev->data, "compatible",
290                                            ACPI_TYPE_STRING, &of_compatible);
291         if (ret) {
292                 ret = acpi_dev_get_property(adev, "compatible",
293                                             ACPI_TYPE_STRING, &of_compatible);
294                 if (ret) {
295                         if (adev->parent
296                             && adev->parent->flags.of_compatible_ok)
297                                 goto out;
298
299                         return;
300                 }
301         }
302         adev->data.of_compatible = of_compatible;
303
304  out:
305         adev->flags.of_compatible_ok = 1;
306 }
307
308 static bool acpi_is_property_guid(const guid_t *guid)
309 {
310         int i;
311
312         for (i = 0; i < ARRAY_SIZE(prp_guids); i++) {
313                 if (guid_equal(guid, &prp_guids[i]))
314                         return true;
315         }
316
317         return false;
318 }
319
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)
323 {
324         struct acpi_device_properties *props;
325
326         props = kzalloc(sizeof(*props), GFP_KERNEL);
327         if (props) {
328                 INIT_LIST_HEAD(&props->list);
329                 props->guid = guid;
330                 props->properties = properties;
331                 list_add_tail(&props->list, &data->properties);
332         }
333
334         return props;
335 }
336
337 static bool acpi_extract_properties(const union acpi_object *desc,
338                                     struct acpi_device_data *data)
339 {
340         int i;
341
342         if (desc->package.count % 2)
343                 return false;
344
345         /* Look for the device properties GUID. */
346         for (i = 0; i < desc->package.count; i += 2) {
347                 const union acpi_object *guid, *properties;
348
349                 guid = &desc->package.elements[i];
350                 properties = &desc->package.elements[i + 1];
351
352                 /*
353                  * The first element must be a GUID and the second one must be
354                  * a package.
355                  */
356                 if (guid->type != ACPI_TYPE_BUFFER ||
357                     guid->buffer.length != 16 ||
358                     properties->type != ACPI_TYPE_PACKAGE)
359                         break;
360
361                 if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer))
362                         continue;
363
364                 /*
365                  * We found the matching GUID. Now validate the format of the
366                  * package immediately following it.
367                  */
368                 if (!acpi_properties_format_valid(properties))
369                         continue;
370
371                 acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer,
372                                     properties);
373         }
374
375         return !list_empty(&data->properties);
376 }
377
378 void acpi_init_properties(struct acpi_device *adev)
379 {
380         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
381         struct acpi_hardware_id *hwid;
382         acpi_status status;
383         bool acpi_of = false;
384
385         INIT_LIST_HEAD(&adev->data.properties);
386         INIT_LIST_HEAD(&adev->data.subnodes);
387
388         if (!adev->handle)
389                 return;
390
391         /*
392          * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
393          * Device Tree compatible properties for this device.
394          */
395         list_for_each_entry(hwid, &adev->pnp.ids, list) {
396                 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
397                         acpi_of = true;
398                         break;
399                 }
400         }
401
402         status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
403                                             ACPI_TYPE_PACKAGE);
404         if (ACPI_FAILURE(status))
405                 goto out;
406
407         if (acpi_extract_properties(buf.pointer, &adev->data)) {
408                 adev->data.pointer = buf.pointer;
409                 if (acpi_of)
410                         acpi_init_of_compatible(adev);
411         }
412         if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
413                                         &adev->data, acpi_fwnode_handle(adev)))
414                 adev->data.pointer = buf.pointer;
415
416         if (!adev->data.pointer) {
417                 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
418                 ACPI_FREE(buf.pointer);
419         }
420
421  out:
422         if (acpi_of && !adev->flags.of_compatible_ok)
423                 acpi_handle_info(adev->handle,
424                          ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
425
426         if (!adev->data.pointer)
427                 acpi_extract_apple_properties(adev);
428 }
429
430 static void acpi_destroy_nondev_subnodes(struct list_head *list)
431 {
432         struct acpi_data_node *dn, *next;
433
434         if (list_empty(list))
435                 return;
436
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);
442                 kfree(dn);
443         }
444 }
445
446 void acpi_free_properties(struct acpi_device *adev)
447 {
448         struct acpi_device_properties *props, *tmp;
449
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);
456                 kfree(props);
457         }
458 }
459
460 /**
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)
466  *
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.
469  *
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.
472  *
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.
477  */
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)
481 {
482         const struct acpi_device_properties *props;
483
484         if (!data || !name)
485                 return -EINVAL;
486
487         if (!data->pointer || list_empty(&data->properties))
488                 return -EINVAL;
489
490         list_for_each_entry(props, &data->properties, list) {
491                 const union acpi_object *properties;
492                 unsigned int i;
493
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;
498
499                         property = &properties->package.elements[i];
500
501                         propname = &property->package.elements[0];
502                         propvalue = &property->package.elements[1];
503
504                         if (!strcmp(name, propname->string.pointer)) {
505                                 if (type != ACPI_TYPE_ANY &&
506                                     propvalue->type != type)
507                                         return -EPROTO;
508                                 if (obj)
509                                         *obj = propvalue;
510
511                                 return 0;
512                         }
513                 }
514         }
515         return -EINVAL;
516 }
517
518 /**
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).
524  */
525 int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
526                           acpi_object_type type, const union acpi_object **obj)
527 {
528         return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
529 }
530 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
531
532 static const struct acpi_device_data *
533 acpi_device_data_of_node(const struct fwnode_handle *fwnode)
534 {
535         if (is_acpi_device_node(fwnode)) {
536                 const struct acpi_device *adev = to_acpi_device_node(fwnode);
537                 return &adev->data;
538         } else if (is_acpi_data_node(fwnode)) {
539                 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
540                 return &dn->data;
541         }
542         return NULL;
543 }
544
545 /**
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).
550  */
551 int acpi_node_prop_get(const struct fwnode_handle *fwnode,
552                        const char *propname, void **valptr)
553 {
554         return acpi_data_get_property(acpi_device_data_of_node(fwnode),
555                                       propname, ACPI_TYPE_ANY,
556                                       (const union acpi_object **)valptr);
557 }
558
559 /**
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)
565  *
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.
568  *
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.
571  *
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.
577  */
578 static int acpi_data_get_property_array(const struct acpi_device_data *data,
579                                         const char *name,
580                                         acpi_object_type type,
581                                         const union acpi_object **obj)
582 {
583         const union acpi_object *prop;
584         int ret, i;
585
586         ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
587         if (ret)
588                 return ret;
589
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)
594                                 return -EPROTO;
595         }
596         if (obj)
597                 *obj = prop;
598
599         return 0;
600 }
601
602 static struct fwnode_handle *
603 acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
604                                  const char *childname)
605 {
606         char name[ACPI_PATH_SEGMENT_LENGTH];
607         struct fwnode_handle *child;
608         struct acpi_buffer path;
609         acpi_status status;
610
611         path.length = sizeof(name);
612         path.pointer = name;
613
614         fwnode_for_each_child_node(fwnode, child) {
615                 if (is_acpi_data_node(child)) {
616                         if (acpi_data_node_match(child, childname))
617                                 return child;
618                         continue;
619                 }
620
621                 status = acpi_get_name(ACPI_HANDLE_FWNODE(child),
622                                        ACPI_SINGLE_NAME, &path);
623                 if (ACPI_FAILURE(status))
624                         break;
625
626                 if (!strncmp(name, childname, ACPI_NAMESEG_SIZE))
627                         return child;
628         }
629
630         return NULL;
631 }
632
633 /**
634  * __acpi_node_get_property_reference - returns handle to the referenced object
635  * @fwnode: Firmware node to get the property from
636  * @propname: Name of the property
637  * @index: Index of the reference to return
638  * @num_args: Maximum number of arguments after each reference
639  * @args: Location to store the returned reference with optional arguments
640  *
641  * Find property with @name, verifify that it is a package containing at least
642  * one object reference and if so, store the ACPI device object pointer to the
643  * target object in @args->adev.  If the reference includes arguments, store
644  * them in the @args->args[] array.
645  *
646  * If there's more than one reference in the property value package, @index is
647  * used to select the one to return.
648  *
649  * It is possible to leave holes in the property value set like in the
650  * example below:
651  *
652  * Package () {
653  *     "cs-gpios",
654  *     Package () {
655  *        ^GPIO, 19, 0, 0,
656  *        ^GPIO, 20, 0, 0,
657  *        0,
658  *        ^GPIO, 21, 0, 0,
659  *     }
660  * }
661  *
662  * Calling this function with index %2 or index %3 return %-ENOENT. If the
663  * property does not contain any more values %-ENOENT is returned. The NULL
664  * entry must be single integer and preferably contain value %0.
665  *
666  * Return: %0 on success, negative error code on failure.
667  */
668 int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
669         const char *propname, size_t index, size_t num_args,
670         struct fwnode_reference_args *args)
671 {
672         const union acpi_object *element, *end;
673         const union acpi_object *obj;
674         const struct acpi_device_data *data;
675         struct acpi_device *device;
676         int ret, idx = 0;
677
678         data = acpi_device_data_of_node(fwnode);
679         if (!data)
680                 return -ENOENT;
681
682         ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
683         if (ret)
684                 return ret == -EINVAL ? -ENOENT : -EINVAL;
685
686         /*
687          * The simplest case is when the value is a single reference.  Just
688          * return that reference then.
689          */
690         if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
691                 if (index)
692                         return -EINVAL;
693
694                 ret = acpi_bus_get_device(obj->reference.handle, &device);
695                 if (ret)
696                         return ret == -ENODEV ? -EINVAL : ret;
697
698                 args->fwnode = acpi_fwnode_handle(device);
699                 args->nargs = 0;
700                 return 0;
701         }
702
703         /*
704          * If it is not a single reference, then it is a package of
705          * references followed by number of ints as follows:
706          *
707          *  Package () { REF, INT, REF, INT, INT }
708          *
709          * The index argument is then used to determine which reference
710          * the caller wants (along with the arguments).
711          */
712         if (obj->type != ACPI_TYPE_PACKAGE)
713                 return -EINVAL;
714         if (index >= obj->package.count)
715                 return -ENOENT;
716
717         element = obj->package.elements;
718         end = element + obj->package.count;
719
720         while (element < end) {
721                 u32 nargs, i;
722
723                 if (element->type == ACPI_TYPE_LOCAL_REFERENCE) {
724                         struct fwnode_handle *ref_fwnode;
725
726                         ret = acpi_bus_get_device(element->reference.handle,
727                                                   &device);
728                         if (ret)
729                                 return -EINVAL;
730
731                         nargs = 0;
732                         element++;
733
734                         /*
735                          * Find the referred data extension node under the
736                          * referred device node.
737                          */
738                         for (ref_fwnode = acpi_fwnode_handle(device);
739                              element < end && element->type == ACPI_TYPE_STRING;
740                              element++) {
741                                 ref_fwnode = acpi_fwnode_get_named_child_node(
742                                         ref_fwnode, element->string.pointer);
743                                 if (!ref_fwnode)
744                                         return -EINVAL;
745                         }
746
747                         /* assume following integer elements are all args */
748                         for (i = 0; element + i < end && i < num_args; i++) {
749                                 int type = element[i].type;
750
751                                 if (type == ACPI_TYPE_INTEGER)
752                                         nargs++;
753                                 else if (type == ACPI_TYPE_LOCAL_REFERENCE)
754                                         break;
755                                 else
756                                         return -EINVAL;
757                         }
758
759                         if (nargs > NR_FWNODE_REFERENCE_ARGS)
760                                 return -EINVAL;
761
762                         if (idx == index) {
763                                 args->fwnode = ref_fwnode;
764                                 args->nargs = nargs;
765                                 for (i = 0; i < nargs; i++)
766                                         args->args[i] = element[i].integer.value;
767
768                                 return 0;
769                         }
770
771                         element += nargs;
772                 } else if (element->type == ACPI_TYPE_INTEGER) {
773                         if (idx == index)
774                                 return -ENOENT;
775                         element++;
776                 } else {
777                         return -EINVAL;
778                 }
779
780                 idx++;
781         }
782
783         return -ENOENT;
784 }
785 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
786
787 static int acpi_data_prop_read_single(const struct acpi_device_data *data,
788                                       const char *propname,
789                                       enum dev_prop_type proptype, void *val)
790 {
791         const union acpi_object *obj;
792         int ret;
793
794         if (!val)
795                 return -EINVAL;
796
797         if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
798                 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
799                 if (ret)
800                         return ret;
801
802                 switch (proptype) {
803                 case DEV_PROP_U8:
804                         if (obj->integer.value > U8_MAX)
805                                 return -EOVERFLOW;
806                         *(u8 *)val = obj->integer.value;
807                         break;
808                 case DEV_PROP_U16:
809                         if (obj->integer.value > U16_MAX)
810                                 return -EOVERFLOW;
811                         *(u16 *)val = obj->integer.value;
812                         break;
813                 case DEV_PROP_U32:
814                         if (obj->integer.value > U32_MAX)
815                                 return -EOVERFLOW;
816                         *(u32 *)val = obj->integer.value;
817                         break;
818                 default:
819                         *(u64 *)val = obj->integer.value;
820                         break;
821                 }
822         } else if (proptype == DEV_PROP_STRING) {
823                 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
824                 if (ret)
825                         return ret;
826
827                 *(char **)val = obj->string.pointer;
828
829                 return 1;
830         } else {
831                 ret = -EINVAL;
832         }
833         return ret;
834 }
835
836 int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
837                               enum dev_prop_type proptype, void *val)
838 {
839         int ret;
840
841         if (!adev)
842                 return -EINVAL;
843
844         ret = acpi_data_prop_read_single(&adev->data, propname, proptype, val);
845         if (ret < 0 || proptype != ACPI_TYPE_STRING)
846                 return ret;
847         return 0;
848 }
849
850 static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
851                                        size_t nval)
852 {
853         int i;
854
855         for (i = 0; i < nval; i++) {
856                 if (items[i].type != ACPI_TYPE_INTEGER)
857                         return -EPROTO;
858                 if (items[i].integer.value > U8_MAX)
859                         return -EOVERFLOW;
860
861                 val[i] = items[i].integer.value;
862         }
863         return 0;
864 }
865
866 static int acpi_copy_property_array_u16(const union acpi_object *items,
867                                         u16 *val, size_t nval)
868 {
869         int i;
870
871         for (i = 0; i < nval; i++) {
872                 if (items[i].type != ACPI_TYPE_INTEGER)
873                         return -EPROTO;
874                 if (items[i].integer.value > U16_MAX)
875                         return -EOVERFLOW;
876
877                 val[i] = items[i].integer.value;
878         }
879         return 0;
880 }
881
882 static int acpi_copy_property_array_u32(const union acpi_object *items,
883                                         u32 *val, size_t nval)
884 {
885         int i;
886
887         for (i = 0; i < nval; i++) {
888                 if (items[i].type != ACPI_TYPE_INTEGER)
889                         return -EPROTO;
890                 if (items[i].integer.value > U32_MAX)
891                         return -EOVERFLOW;
892
893                 val[i] = items[i].integer.value;
894         }
895         return 0;
896 }
897
898 static int acpi_copy_property_array_u64(const union acpi_object *items,
899                                         u64 *val, size_t nval)
900 {
901         int i;
902
903         for (i = 0; i < nval; i++) {
904                 if (items[i].type != ACPI_TYPE_INTEGER)
905                         return -EPROTO;
906
907                 val[i] = items[i].integer.value;
908         }
909         return 0;
910 }
911
912 static int acpi_copy_property_array_string(const union acpi_object *items,
913                                            char **val, size_t nval)
914 {
915         int i;
916
917         for (i = 0; i < nval; i++) {
918                 if (items[i].type != ACPI_TYPE_STRING)
919                         return -EPROTO;
920
921                 val[i] = items[i].string.pointer;
922         }
923         return nval;
924 }
925
926 static int acpi_data_prop_read(const struct acpi_device_data *data,
927                                const char *propname,
928                                enum dev_prop_type proptype,
929                                void *val, size_t nval)
930 {
931         const union acpi_object *obj;
932         const union acpi_object *items;
933         int ret;
934
935         if (val && nval == 1) {
936                 ret = acpi_data_prop_read_single(data, propname, proptype, val);
937                 if (ret >= 0)
938                         return ret;
939         }
940
941         ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
942         if (ret)
943                 return ret;
944
945         if (!val)
946                 return obj->package.count;
947
948         if (proptype != DEV_PROP_STRING && nval > obj->package.count)
949                 return -EOVERFLOW;
950         else if (nval <= 0)
951                 return -EINVAL;
952
953         items = obj->package.elements;
954
955         switch (proptype) {
956         case DEV_PROP_U8:
957                 ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
958                 break;
959         case DEV_PROP_U16:
960                 ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
961                 break;
962         case DEV_PROP_U32:
963                 ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
964                 break;
965         case DEV_PROP_U64:
966                 ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
967                 break;
968         case DEV_PROP_STRING:
969                 ret = acpi_copy_property_array_string(
970                         items, (char **)val,
971                         min_t(u32, nval, obj->package.count));
972                 break;
973         default:
974                 ret = -EINVAL;
975                 break;
976         }
977         return ret;
978 }
979
980 int acpi_dev_prop_read(const struct acpi_device *adev, const char *propname,
981                        enum dev_prop_type proptype, void *val, size_t nval)
982 {
983         return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL;
984 }
985
986 /**
987  * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
988  * @fwnode: Firmware node to get the property from.
989  * @propname: Name of the property.
990  * @proptype: Expected property type.
991  * @val: Location to store the property value (if not %NULL).
992  * @nval: Size of the array pointed to by @val.
993  *
994  * If @val is %NULL, return the number of array elements comprising the value
995  * of the property.  Otherwise, read at most @nval values to the array at the
996  * location pointed to by @val.
997  */
998 int acpi_node_prop_read(const struct fwnode_handle *fwnode,
999                         const char *propname, enum dev_prop_type proptype,
1000                         void *val, size_t nval)
1001 {
1002         return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
1003                                    propname, proptype, val, nval);
1004 }
1005
1006 /**
1007  * acpi_get_next_subnode - Return the next child node handle for a fwnode
1008  * @fwnode: Firmware node to find the next child node for.
1009  * @child: Handle to one of the device's child nodes or a null handle.
1010  */
1011 struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
1012                                             struct fwnode_handle *child)
1013 {
1014         const struct acpi_device *adev = to_acpi_device_node(fwnode);
1015         const struct list_head *head;
1016         struct list_head *next;
1017
1018         if (!child || is_acpi_device_node(child)) {
1019                 struct acpi_device *child_adev;
1020
1021                 if (adev)
1022                         head = &adev->children;
1023                 else
1024                         goto nondev;
1025
1026                 if (list_empty(head))
1027                         goto nondev;
1028
1029                 if (child) {
1030                         adev = to_acpi_device_node(child);
1031                         next = adev->node.next;
1032                         if (next == head) {
1033                                 child = NULL;
1034                                 goto nondev;
1035                         }
1036                         child_adev = list_entry(next, struct acpi_device, node);
1037                 } else {
1038                         child_adev = list_first_entry(head, struct acpi_device,
1039                                                       node);
1040                 }
1041                 return acpi_fwnode_handle(child_adev);
1042         }
1043
1044  nondev:
1045         if (!child || is_acpi_data_node(child)) {
1046                 const struct acpi_data_node *data = to_acpi_data_node(fwnode);
1047                 struct acpi_data_node *dn;
1048
1049                 /*
1050                  * We can have a combination of device and data nodes, e.g. with
1051                  * hierarchical _DSD properties. Make sure the adev pointer is
1052                  * restored before going through data nodes, otherwise we will
1053                  * be looking for data_nodes below the last device found instead
1054                  * of the common fwnode shared by device_nodes and data_nodes.
1055                  */
1056                 adev = to_acpi_device_node(fwnode);
1057                 if (adev)
1058                         head = &adev->data.subnodes;
1059                 else if (data)
1060                         head = &data->data.subnodes;
1061                 else
1062                         return NULL;
1063
1064                 if (list_empty(head))
1065                         return NULL;
1066
1067                 if (child) {
1068                         dn = to_acpi_data_node(child);
1069                         next = dn->sibling.next;
1070                         if (next == head)
1071                                 return NULL;
1072
1073                         dn = list_entry(next, struct acpi_data_node, sibling);
1074                 } else {
1075                         dn = list_first_entry(head, struct acpi_data_node, sibling);
1076                 }
1077                 return &dn->fwnode;
1078         }
1079         return NULL;
1080 }
1081
1082 /**
1083  * acpi_node_get_parent - Return parent fwnode of this fwnode
1084  * @fwnode: Firmware node whose parent to get
1085  *
1086  * Returns parent node of an ACPI device or data firmware node or %NULL if
1087  * not available.
1088  */
1089 struct fwnode_handle *acpi_node_get_parent(const struct fwnode_handle *fwnode)
1090 {
1091         if (is_acpi_data_node(fwnode)) {
1092                 /* All data nodes have parent pointer so just return that */
1093                 return to_acpi_data_node(fwnode)->parent;
1094         } else if (is_acpi_device_node(fwnode)) {
1095                 acpi_handle handle, parent_handle;
1096
1097                 handle = to_acpi_device_node(fwnode)->handle;
1098                 if (ACPI_SUCCESS(acpi_get_parent(handle, &parent_handle))) {
1099                         struct acpi_device *adev;
1100
1101                         if (!acpi_bus_get_device(parent_handle, &adev))
1102                                 return acpi_fwnode_handle(adev);
1103                 }
1104         }
1105
1106         return NULL;
1107 }
1108
1109 /*
1110  * Return true if the node is an ACPI graph node. Called on either ports
1111  * or endpoints.
1112  */
1113 static bool is_acpi_graph_node(struct fwnode_handle *fwnode,
1114                                const char *str)
1115 {
1116         unsigned int len = strlen(str);
1117         const char *name;
1118
1119         if (!len || !is_acpi_data_node(fwnode))
1120                 return false;
1121
1122         name = to_acpi_data_node(fwnode)->name;
1123
1124         return (fwnode_property_present(fwnode, "reg") &&
1125                 !strncmp(name, str, len) && name[len] == '@') ||
1126                 fwnode_property_present(fwnode, str);
1127 }
1128
1129 /**
1130  * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
1131  * @fwnode: Pointer to the parent firmware node
1132  * @prev: Previous endpoint node or %NULL to get the first
1133  *
1134  * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
1135  * %NULL if there is no next endpoint or in case of error. In case of success
1136  * the next endpoint is returned.
1137  */
1138 static struct fwnode_handle *acpi_graph_get_next_endpoint(
1139         const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
1140 {
1141         struct fwnode_handle *port = NULL;
1142         struct fwnode_handle *endpoint;
1143
1144         if (!prev) {
1145                 do {
1146                         port = fwnode_get_next_child_node(fwnode, port);
1147                         /*
1148                          * The names of the port nodes begin with "port@"
1149                          * followed by the number of the port node and they also
1150                          * have a "reg" property that also has the number of the
1151                          * port node. For compatibility reasons a node is also
1152                          * recognised as a port node from the "port" property.
1153                          */
1154                         if (is_acpi_graph_node(port, "port"))
1155                                 break;
1156                 } while (port);
1157         } else {
1158                 port = fwnode_get_parent(prev);
1159         }
1160
1161         if (!port)
1162                 return NULL;
1163
1164         endpoint = fwnode_get_next_child_node(port, prev);
1165         while (!endpoint) {
1166                 port = fwnode_get_next_child_node(fwnode, port);
1167                 if (!port)
1168                         break;
1169                 if (is_acpi_graph_node(port, "port"))
1170                         endpoint = fwnode_get_next_child_node(port, NULL);
1171         }
1172
1173         /*
1174          * The names of the endpoint nodes begin with "endpoint@" followed by
1175          * the number of the endpoint node and they also have a "reg" property
1176          * that also has the number of the endpoint node. For compatibility
1177          * reasons a node is also recognised as an endpoint node from the
1178          * "endpoint" property.
1179          */
1180         if (!is_acpi_graph_node(endpoint, "endpoint"))
1181                 return NULL;
1182
1183         return endpoint;
1184 }
1185
1186 /**
1187  * acpi_graph_get_child_prop_value - Return a child with a given property value
1188  * @fwnode: device fwnode
1189  * @prop_name: The name of the property to look for
1190  * @val: the desired property value
1191  *
1192  * Return the port node corresponding to a given port number. Returns
1193  * the child node on success, NULL otherwise.
1194  */
1195 static struct fwnode_handle *acpi_graph_get_child_prop_value(
1196         const struct fwnode_handle *fwnode, const char *prop_name,
1197         unsigned int val)
1198 {
1199         struct fwnode_handle *child;
1200
1201         fwnode_for_each_child_node(fwnode, child) {
1202                 u32 nr;
1203
1204                 if (fwnode_property_read_u32(child, prop_name, &nr))
1205                         continue;
1206
1207                 if (val == nr)
1208                         return child;
1209         }
1210
1211         return NULL;
1212 }
1213
1214
1215 /**
1216  * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
1217  * @fwnode: Endpoint firmware node pointing to a remote device
1218  * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
1219  *
1220  * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
1221  */
1222 static struct fwnode_handle *
1223 acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode)
1224 {
1225         struct fwnode_handle *fwnode;
1226         unsigned int port_nr, endpoint_nr;
1227         struct fwnode_reference_args args;
1228         int ret;
1229
1230         memset(&args, 0, sizeof(args));
1231         ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
1232                                                &args);
1233         if (ret)
1234                 return NULL;
1235
1236         /* Direct endpoint reference? */
1237         if (!is_acpi_device_node(args.fwnode))
1238                 return args.nargs ? NULL : args.fwnode;
1239
1240         /*
1241          * Always require two arguments with the reference: port and
1242          * endpoint indices.
1243          */
1244         if (args.nargs != 2)
1245                 return NULL;
1246
1247         fwnode = args.fwnode;
1248         port_nr = args.args[0];
1249         endpoint_nr = args.args[1];
1250
1251         fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
1252
1253         return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr);
1254 }
1255
1256 static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
1257 {
1258         if (!is_acpi_device_node(fwnode))
1259                 return false;
1260
1261         return acpi_device_is_present(to_acpi_device_node(fwnode));
1262 }
1263
1264 static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
1265                                          const char *propname)
1266 {
1267         return !acpi_node_prop_get(fwnode, propname, NULL);
1268 }
1269
1270 static int
1271 acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1272                                     const char *propname,
1273                                     unsigned int elem_size, void *val,
1274                                     size_t nval)
1275 {
1276         enum dev_prop_type type;
1277
1278         switch (elem_size) {
1279         case sizeof(u8):
1280                 type = DEV_PROP_U8;
1281                 break;
1282         case sizeof(u16):
1283                 type = DEV_PROP_U16;
1284                 break;
1285         case sizeof(u32):
1286                 type = DEV_PROP_U32;
1287                 break;
1288         case sizeof(u64):
1289                 type = DEV_PROP_U64;
1290                 break;
1291         default:
1292                 return -ENXIO;
1293         }
1294
1295         return acpi_node_prop_read(fwnode, propname, type, val, nval);
1296 }
1297
1298 static int
1299 acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1300                                        const char *propname, const char **val,
1301                                        size_t nval)
1302 {
1303         return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
1304                                    val, nval);
1305 }
1306
1307 static int
1308 acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1309                                const char *prop, const char *nargs_prop,
1310                                unsigned int args_count, unsigned int index,
1311                                struct fwnode_reference_args *args)
1312 {
1313         return __acpi_node_get_property_reference(fwnode, prop, index,
1314                                                   args_count, args);
1315 }
1316
1317 static struct fwnode_handle *
1318 acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
1319 {
1320         return acpi_node_get_parent(fwnode);
1321 }
1322
1323 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1324                                             struct fwnode_endpoint *endpoint)
1325 {
1326         struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1327
1328         endpoint->local_fwnode = fwnode;
1329
1330         if (fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port))
1331                 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1332         if (fwnode_property_read_u32(fwnode, "reg", &endpoint->id))
1333                 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1334
1335         return 0;
1336 }
1337
1338 static const void *
1339 acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1340                                   const struct device *dev)
1341 {
1342         return acpi_device_get_match_data(dev);
1343 }
1344
1345 #define DECLARE_ACPI_FWNODE_OPS(ops) \
1346         const struct fwnode_operations ops = {                          \
1347                 .device_is_available = acpi_fwnode_device_is_available, \
1348                 .device_get_match_data = acpi_fwnode_device_get_match_data, \
1349                 .property_present = acpi_fwnode_property_present,       \
1350                 .property_read_int_array =                              \
1351                         acpi_fwnode_property_read_int_array,            \
1352                 .property_read_string_array =                           \
1353                         acpi_fwnode_property_read_string_array,         \
1354                 .get_parent = acpi_node_get_parent,                     \
1355                 .get_next_child_node = acpi_get_next_subnode,           \
1356                 .get_named_child_node = acpi_fwnode_get_named_child_node, \
1357                 .get_reference_args = acpi_fwnode_get_reference_args,   \
1358                 .graph_get_next_endpoint =                              \
1359                         acpi_graph_get_next_endpoint,                   \
1360                 .graph_get_remote_endpoint =                            \
1361                         acpi_graph_get_remote_endpoint,                 \
1362                 .graph_get_port_parent = acpi_fwnode_get_parent,        \
1363                 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1364         };                                                              \
1365         EXPORT_SYMBOL_GPL(ops)
1366
1367 DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
1368 DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
1369 const struct fwnode_operations acpi_static_fwnode_ops;
1370
1371 bool is_acpi_device_node(const struct fwnode_handle *fwnode)
1372 {
1373         return !IS_ERR_OR_NULL(fwnode) &&
1374                 fwnode->ops == &acpi_device_fwnode_ops;
1375 }
1376 EXPORT_SYMBOL(is_acpi_device_node);
1377
1378 bool is_acpi_data_node(const struct fwnode_handle *fwnode)
1379 {
1380         return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops;
1381 }
1382 EXPORT_SYMBOL(is_acpi_data_node);