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