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