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