blob: c1c216163de3f0ab5b044c52527ed10e3090a9e5 [file] [log] [blame]
Mika Westerbergffdcd952014-10-21 13:33:55 +02001/*
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
Sakari Ailus99a85462017-07-21 14:39:34 +030022static int acpi_data_get_property_array(const struct acpi_device_data *data,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +020023 const char *name,
24 acpi_object_type type,
25 const union acpi_object **obj);
26
Andy Shevchenko3689d3d2017-07-19 21:28:57 +030027/* ACPI _DSD device properties GUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
28static 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 */
32static const guid_t ads_guid =
33 GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6,
34 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b);
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020035
36static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
37 const union acpi_object *desc,
Mika Westerbergdfa672f2017-03-28 10:52:16 +030038 struct acpi_device_data *data,
39 struct fwnode_handle *parent);
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020040static bool acpi_extract_properties(const union acpi_object *desc,
41 struct acpi_device_data *data);
42
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010043static bool acpi_nondev_subnode_extract(const union acpi_object *desc,
44 acpi_handle handle,
45 const union acpi_object *link,
Mika Westerbergdfa672f2017-03-28 10:52:16 +030046 struct list_head *list,
47 struct fwnode_handle *parent)
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020048{
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020049 struct acpi_data_node *dn;
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010050 bool result;
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020051
52 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
53 if (!dn)
54 return false;
55
56 dn->name = link->package.elements[0].string.pointer;
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030057 dn->fwnode.ops = &acpi_data_fwnode_ops;
Mika Westerbergdfa672f2017-03-28 10:52:16 +030058 dn->parent = parent;
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020059 INIT_LIST_HEAD(&dn->data.subnodes);
60
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010061 result = acpi_extract_properties(desc, &dn->data);
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020062
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010063 if (handle) {
64 acpi_handle scope;
65 acpi_status status;
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020066
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010067 /*
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)
Mika Westerbergdfa672f2017-03-28 10:52:16 +030075 && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
76 &dn->fwnode))
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010077 result = true;
Mika Westerbergdfa672f2017-03-28 10:52:16 +030078 } else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
79 &dn->fwnode)) {
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010080 result = true;
81 }
82
83 if (result) {
Rafael J. Wysocki263b4c12015-08-27 04:37:19 +020084 dn->handle = handle;
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010085 dn->data.pointer = desc;
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020086 list_add_tail(&dn->sibling, list);
87 return true;
88 }
89
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020090 kfree(dn);
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010091 acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +020092 return false;
93}
94
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010095static bool acpi_nondev_subnode_data_ok(acpi_handle handle,
96 const union acpi_object *link,
Mika Westerbergdfa672f2017-03-28 10:52:16 +030097 struct list_head *list,
98 struct fwnode_handle *parent)
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +010099{
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
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300108 if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
109 parent))
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +0100110 return true;
111
112 ACPI_FREE(buf.pointer);
113 return false;
114}
115
116static bool acpi_nondev_subnode_ok(acpi_handle scope,
117 const union acpi_object *link,
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300118 struct list_head *list,
119 struct fwnode_handle *parent)
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +0100120{
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
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300132 return acpi_nondev_subnode_data_ok(handle, link, list, parent);
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +0100133}
134
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200135static int acpi_add_nondev_subnodes(acpi_handle scope,
136 const union acpi_object *links,
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300137 struct list_head *list,
138 struct fwnode_handle *parent)
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200139{
140 bool ret = false;
141 int i;
142
143 for (i = 0; i < links->package.count; i++) {
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +0100144 const union acpi_object *link, *desc;
145 acpi_handle handle;
146 bool result;
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200147
148 link = &links->package.elements[i];
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +0100149 /* 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:
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300160 result = acpi_nondev_subnode_ok(scope, link, list,
161 parent);
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +0100162 break;
163 case ACPI_TYPE_LOCAL_REFERENCE:
164 handle = link->package.elements[1].reference.handle;
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300165 result = acpi_nondev_subnode_data_ok(handle, link, list,
166 parent);
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +0100167 break;
168 case ACPI_TYPE_PACKAGE:
169 desc = &link->package.elements[1];
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300170 result = acpi_nondev_subnode_extract(desc, NULL, link,
171 list, parent);
Rafael J. Wysocki99db5ff2016-11-22 00:02:19 +0100172 break;
173 default:
174 result = false;
175 break;
176 }
177 ret = ret || result;
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200178 }
179
180 return ret;
181}
182
183static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
184 const union acpi_object *desc,
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300185 struct acpi_device_data *data,
186 struct fwnode_handle *parent)
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200187{
188 int i;
189
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300190 /* Look for the ACPI data subnodes GUID. */
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200191 for (i = 0; i < desc->package.count; i += 2) {
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300192 const union acpi_object *guid, *links;
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200193
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300194 guid = &desc->package.elements[i];
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200195 links = &desc->package.elements[i + 1];
196
197 /*
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300198 * The first element must be a GUID and the second one must be
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200199 * a package.
200 */
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300201 if (guid->type != ACPI_TYPE_BUFFER ||
202 guid->buffer.length != 16 ||
203 links->type != ACPI_TYPE_PACKAGE)
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200204 break;
205
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300206 if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid))
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200207 continue;
208
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300209 return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
210 parent);
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200211 }
212
213 return false;
214}
Mika Westerbergffdcd952014-10-21 13:33:55 +0200215
216static 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
247static 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
Mika Westerberg733e6252014-10-21 13:33:56 +0200267static void acpi_init_of_compatible(struct acpi_device *adev)
268{
269 const union acpi_object *of_compatible;
Mika Westerberg733e6252014-10-21 13:33:56 +0200270 int ret;
271
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200272 ret = acpi_data_get_property_array(&adev->data, "compatible",
273 ACPI_TYPE_STRING, &of_compatible);
Rafael J. Wysocki5c53b262015-05-05 15:43:07 +0200274 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
Rafael J. Wysockibd8191c2015-08-27 04:35:14 +0200291static bool acpi_extract_properties(const union acpi_object *desc,
292 struct acpi_device_data *data)
Rafael J. Wysocki5c53b262015-05-05 15:43:07 +0200293{
Rafael J. Wysocki5c53b262015-05-05 15:43:07 +0200294 int i;
295
Mika Westerbergffdcd952014-10-21 13:33:55 +0200296 if (desc->package.count % 2)
Rafael J. Wysockibd8191c2015-08-27 04:35:14 +0200297 return false;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200298
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300299 /* Look for the device properties GUID. */
Mika Westerbergffdcd952014-10-21 13:33:55 +0200300 for (i = 0; i < desc->package.count; i += 2) {
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300301 const union acpi_object *guid, *properties;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200302
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300303 guid = &desc->package.elements[i];
Mika Westerbergffdcd952014-10-21 13:33:55 +0200304 properties = &desc->package.elements[i + 1];
305
306 /*
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300307 * The first element must be a GUID and the second one must be
Mika Westerbergffdcd952014-10-21 13:33:55 +0200308 * a package.
309 */
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300310 if (guid->type != ACPI_TYPE_BUFFER ||
311 guid->buffer.length != 16 ||
312 properties->type != ACPI_TYPE_PACKAGE)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200313 break;
314
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300315 if (!guid_equal((guid_t *)guid->buffer.pointer, &prp_guid))
Mika Westerbergffdcd952014-10-21 13:33:55 +0200316 continue;
317
318 /*
Andy Shevchenko3689d3d2017-07-19 21:28:57 +0300319 * We found the matching GUID. Now validate the format of the
Mika Westerbergffdcd952014-10-21 13:33:55 +0200320 * package immediately following it.
321 */
322 if (!acpi_properties_format_valid(properties))
323 break;
324
Rafael J. Wysockibd8191c2015-08-27 04:35:14 +0200325 data->properties = properties;
326 return true;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200327 }
328
Rafael J. Wysockibd8191c2015-08-27 04:35:14 +0200329 return false;
330}
331
332void 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
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200339 INIT_LIST_HEAD(&adev->data.subnodes);
340
Lukas Wunner75fc70e2017-08-01 14:10:41 +0200341 if (!adev->handle)
342 return;
343
Rafael J. Wysockibd8191c2015-08-27 04:35:14 +0200344 /*
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);
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200364 }
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300365 if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
366 &adev->data, acpi_fwnode_handle(adev)))
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200367 adev->data.pointer = buf.pointer;
368
369 if (!adev->data.pointer) {
Rafael J. Wysockibd8191c2015-08-27 04:35:14 +0200370 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
371 ACPI_FREE(buf.pointer);
372 }
Rafael J. Wysocki5c53b262015-05-05 15:43:07 +0200373
374 out:
375 if (acpi_of && !adev->flags.of_compatible_ok)
376 acpi_handle_info(adev->handle,
Rafael J. Wysockiee892092015-05-22 04:24:34 +0200377 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
Lukas Wunner899596e2017-08-01 14:10:41 +0200378
379 if (!adev->data.pointer)
380 acpi_extract_apple_properties(adev);
Mika Westerbergffdcd952014-10-21 13:33:55 +0200381}
382
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200383static 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);
Rafael J. Wysocki263b4c12015-08-27 04:37:19 +0200392 wait_for_completion(&dn->kobj_done);
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200393 list_del(&dn->sibling);
394 ACPI_FREE((void *)dn->data.pointer);
395 kfree(dn);
396 }
397}
398
Mika Westerbergffdcd952014-10-21 13:33:55 +0200399void acpi_free_properties(struct acpi_device *adev)
400{
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200401 acpi_destroy_nondev_subnodes(&adev->data.subnodes);
Mika Westerbergffdcd952014-10-21 13:33:55 +0200402 ACPI_FREE((void *)adev->data.pointer);
Mika Westerberg733e6252014-10-21 13:33:56 +0200403 adev->data.of_compatible = NULL;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200404 adev->data.pointer = NULL;
405 adev->data.properties = NULL;
406}
407
408/**
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200409 * acpi_data_get_property - return an ACPI property with given name
410 * @data: ACPI device deta object to get the property from
Mika Westerbergffdcd952014-10-21 13:33:55 +0200411 * @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
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200419 * freed by the ACPI core automatically during the removal of @data.
Mika Westerbergffdcd952014-10-21 13:33:55 +0200420 *
421 * Return: %0 if property with @name has been found (success),
422 * %-EINVAL if the arguments are invalid,
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200423 * %-EINVAL if the property doesn't exist,
Mika Westerbergffdcd952014-10-21 13:33:55 +0200424 * %-EPROTO if the property value type doesn't match @type.
425 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300426static int acpi_data_get_property(const struct acpi_device_data *data,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200427 const char *name, acpi_object_type type,
428 const union acpi_object **obj)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200429{
430 const union acpi_object *properties;
431 int i;
432
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200433 if (!data || !name)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200434 return -EINVAL;
435
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200436 if (!data->pointer || !data->properties)
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200437 return -EINVAL;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200438
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200439 properties = data->properties;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200440 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;
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200452 if (obj)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200453 *obj = propvalue;
454
455 return 0;
456 }
457 }
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200458 return -EINVAL;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200459}
Mika Westerbergffdcd952014-10-21 13:33:55 +0200460
461/**
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200462 * 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 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300468int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200469 acpi_object_type type, const union acpi_object **obj)
470{
471 return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
472}
473EXPORT_SYMBOL_GPL(acpi_dev_get_property);
474
Sakari Ailus99a85462017-07-21 14:39:34 +0300475static const struct acpi_device_data *
476acpi_device_data_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200477{
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300478 if (is_acpi_device_node(fwnode)) {
Sakari Ailus99a85462017-07-21 14:39:34 +0300479 const struct acpi_device *adev = to_acpi_device_node(fwnode);
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200480 return &adev->data;
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300481 } else if (is_acpi_data_node(fwnode)) {
Sakari Ailus99a85462017-07-21 14:39:34 +0300482 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200483 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 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300494int acpi_node_prop_get(const struct fwnode_handle *fwnode,
495 const char *propname, void **valptr)
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200496{
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
Mika Westerbergffdcd952014-10-21 13:33:55 +0200505 * @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
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200513 * freed by the ACPI core automatically during the removal of @data.
Mika Westerbergffdcd952014-10-21 13:33:55 +0200514 *
515 * Return: %0 if array property (package) with @name has been found (success),
516 * %-EINVAL if the arguments are invalid,
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200517 * %-EINVAL if the property doesn't exist,
Mika Westerbergffdcd952014-10-21 13:33:55 +0200518 * %-EPROTO if the property is not a package or the type of its elements
519 * doesn't match @type.
520 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300521static int acpi_data_get_property_array(const struct acpi_device_data *data,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200522 const char *name,
523 acpi_object_type type,
524 const union acpi_object **obj)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200525{
526 const union acpi_object *prop;
527 int ret, i;
528
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200529 ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
Mika Westerbergffdcd952014-10-21 13:33:55 +0200530 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}
Mika Westerbergffdcd952014-10-21 13:33:55 +0200544
545/**
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300546 * __acpi_node_get_property_reference - returns handle to the referenced object
547 * @fwnode: Firmware node to get the property from
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200548 * @propname: Name of the property
Mika Westerbergffdcd952014-10-21 13:33:55 +0200549 * @index: Index of the reference to return
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300550 * @num_args: Maximum number of arguments after each reference
Mika Westerbergffdcd952014-10-21 13:33:55 +0200551 * @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
Rafael J. Wysocki60ba0322014-11-05 00:29:07 +0100555 * target object in @args->adev. If the reference includes arguments, store
556 * them in the @args->args[] array.
Mika Westerbergffdcd952014-10-21 13:33:55 +0200557 *
Rafael J. Wysocki60ba0322014-11-05 00:29:07 +0100558 * If there's more than one reference in the property value package, @index is
559 * used to select the one to return.
Mika Westerbergffdcd952014-10-21 13:33:55 +0200560 *
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300561 * 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 return %-ENOENT and with index %3
575 * returns the last entry. If the property does not contain any more values
576 * %-ENODATA is returned. The NULL entry must be single integer and
577 * preferably contain value %0.
578 *
Mika Westerbergffdcd952014-10-21 13:33:55 +0200579 * Return: %0 on success, negative error code on failure.
580 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300581int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300582 const char *propname, size_t index, size_t num_args,
583 struct acpi_reference_args *args)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200584{
585 const union acpi_object *element, *end;
586 const union acpi_object *obj;
Sakari Ailus99a85462017-07-21 14:39:34 +0300587 const struct acpi_device_data *data;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200588 struct acpi_device *device;
589 int ret, idx = 0;
590
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300591 data = acpi_device_data_of_node(fwnode);
592 if (!data)
593 return -EINVAL;
594
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200595 ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
Mika Westerbergffdcd952014-10-21 13:33:55 +0200596 if (ret)
597 return ret;
598
599 /*
600 * The simplest case is when the value is a single reference. Just
601 * return that reference then.
602 */
603 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
Rafael J. Wysocki60ba0322014-11-05 00:29:07 +0100604 if (index)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200605 return -EINVAL;
606
607 ret = acpi_bus_get_device(obj->reference.handle, &device);
608 if (ret)
609 return ret;
610
611 args->adev = device;
612 args->nargs = 0;
613 return 0;
614 }
615
616 /*
617 * If it is not a single reference, then it is a package of
618 * references followed by number of ints as follows:
619 *
620 * Package () { REF, INT, REF, INT, INT }
621 *
622 * The index argument is then used to determine which reference
623 * the caller wants (along with the arguments).
624 */
625 if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
626 return -EPROTO;
627
628 element = obj->package.elements;
629 end = element + obj->package.count;
630
631 while (element < end) {
632 u32 nargs, i;
633
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300634 if (element->type == ACPI_TYPE_LOCAL_REFERENCE) {
635 ret = acpi_bus_get_device(element->reference.handle,
636 &device);
637 if (ret)
638 return -ENODEV;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200639
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300640 nargs = 0;
641 element++;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200642
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300643 /* assume following integer elements are all args */
644 for (i = 0; element + i < end && i < num_args; i++) {
645 int type = element[i].type;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200646
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300647 if (type == ACPI_TYPE_INTEGER)
648 nargs++;
649 else if (type == ACPI_TYPE_LOCAL_REFERENCE)
650 break;
651 else
652 return -EPROTO;
653 }
Mika Westerbergffdcd952014-10-21 13:33:55 +0200654
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300655 if (nargs > MAX_ACPI_REFERENCE_ARGS)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200656 return -EPROTO;
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300657
658 if (idx == index) {
659 args->adev = device;
660 args->nargs = nargs;
661 for (i = 0; i < nargs; i++)
662 args->args[i] = element[i].integer.value;
663
664 return 0;
665 }
666
667 element += nargs;
668 } else if (element->type == ACPI_TYPE_INTEGER) {
669 if (idx == index)
670 return -ENOENT;
671 element++;
672 } else {
673 return -EPROTO;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200674 }
675
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300676 idx++;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200677 }
678
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300679 return -ENODATA;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200680}
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300681EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100682
Sakari Ailus99a85462017-07-21 14:39:34 +0300683static int acpi_data_prop_read_single(const struct acpi_device_data *data,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200684 const char *propname,
685 enum dev_prop_type proptype, void *val)
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100686{
687 const union acpi_object *obj;
688 int ret;
689
690 if (!val)
691 return -EINVAL;
692
693 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200694 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100695 if (ret)
696 return ret;
697
698 switch (proptype) {
699 case DEV_PROP_U8:
700 if (obj->integer.value > U8_MAX)
701 return -EOVERFLOW;
702 *(u8 *)val = obj->integer.value;
703 break;
704 case DEV_PROP_U16:
705 if (obj->integer.value > U16_MAX)
706 return -EOVERFLOW;
707 *(u16 *)val = obj->integer.value;
708 break;
709 case DEV_PROP_U32:
710 if (obj->integer.value > U32_MAX)
711 return -EOVERFLOW;
712 *(u32 *)val = obj->integer.value;
713 break;
714 default:
715 *(u64 *)val = obj->integer.value;
716 break;
717 }
718 } else if (proptype == DEV_PROP_STRING) {
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200719 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100720 if (ret)
721 return ret;
722
723 *(char **)val = obj->string.pointer;
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300724
725 return 1;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100726 } else {
727 ret = -EINVAL;
728 }
729 return ret;
730}
731
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200732int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
733 enum dev_prop_type proptype, void *val)
734{
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300735 int ret;
736
737 if (!adev)
738 return -EINVAL;
739
740 ret = acpi_data_prop_read_single(&adev->data, propname, proptype, val);
741 if (ret < 0 || proptype != ACPI_TYPE_STRING)
742 return ret;
743 return 0;
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200744}
745
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100746static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
747 size_t nval)
748{
749 int i;
750
751 for (i = 0; i < nval; i++) {
752 if (items[i].type != ACPI_TYPE_INTEGER)
753 return -EPROTO;
754 if (items[i].integer.value > U8_MAX)
755 return -EOVERFLOW;
756
757 val[i] = items[i].integer.value;
758 }
759 return 0;
760}
761
762static int acpi_copy_property_array_u16(const union acpi_object *items,
763 u16 *val, size_t nval)
764{
765 int i;
766
767 for (i = 0; i < nval; i++) {
768 if (items[i].type != ACPI_TYPE_INTEGER)
769 return -EPROTO;
770 if (items[i].integer.value > U16_MAX)
771 return -EOVERFLOW;
772
773 val[i] = items[i].integer.value;
774 }
775 return 0;
776}
777
778static int acpi_copy_property_array_u32(const union acpi_object *items,
779 u32 *val, size_t nval)
780{
781 int i;
782
783 for (i = 0; i < nval; i++) {
784 if (items[i].type != ACPI_TYPE_INTEGER)
785 return -EPROTO;
786 if (items[i].integer.value > U32_MAX)
787 return -EOVERFLOW;
788
789 val[i] = items[i].integer.value;
790 }
791 return 0;
792}
793
794static int acpi_copy_property_array_u64(const union acpi_object *items,
795 u64 *val, size_t nval)
796{
797 int i;
798
799 for (i = 0; i < nval; i++) {
800 if (items[i].type != ACPI_TYPE_INTEGER)
801 return -EPROTO;
802
803 val[i] = items[i].integer.value;
804 }
805 return 0;
806}
807
808static int acpi_copy_property_array_string(const union acpi_object *items,
809 char **val, size_t nval)
810{
811 int i;
812
813 for (i = 0; i < nval; i++) {
814 if (items[i].type != ACPI_TYPE_STRING)
815 return -EPROTO;
816
817 val[i] = items[i].string.pointer;
818 }
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300819 return nval;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100820}
821
Sakari Ailus99a85462017-07-21 14:39:34 +0300822static int acpi_data_prop_read(const struct acpi_device_data *data,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200823 const char *propname,
824 enum dev_prop_type proptype,
825 void *val, size_t nval)
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100826{
827 const union acpi_object *obj;
828 const union acpi_object *items;
829 int ret;
830
831 if (val && nval == 1) {
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200832 ret = acpi_data_prop_read_single(data, propname, proptype, val);
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300833 if (ret >= 0)
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100834 return ret;
835 }
836
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200837 ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100838 if (ret)
839 return ret;
840
841 if (!val)
842 return obj->package.count;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100843
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300844 if (proptype != DEV_PROP_STRING && nval > obj->package.count)
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100845 return -EOVERFLOW;
Andy Shevchenko7dc59dc2015-08-10 19:56:48 +0300846 else if (nval <= 0)
847 return -EINVAL;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100848
849 items = obj->package.elements;
Andy Shevchenko7dc59dc2015-08-10 19:56:48 +0300850
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100851 switch (proptype) {
852 case DEV_PROP_U8:
853 ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
854 break;
855 case DEV_PROP_U16:
856 ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
857 break;
858 case DEV_PROP_U32:
859 ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
860 break;
861 case DEV_PROP_U64:
862 ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
863 break;
864 case DEV_PROP_STRING:
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300865 ret = acpi_copy_property_array_string(
866 items, (char **)val,
867 min_t(u32, nval, obj->package.count));
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100868 break;
869 default:
870 ret = -EINVAL;
871 break;
872 }
873 return ret;
874}
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200875
Sakari Ailus99a85462017-07-21 14:39:34 +0300876int acpi_dev_prop_read(const struct acpi_device *adev, const char *propname,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200877 enum dev_prop_type proptype, void *val, size_t nval)
878{
879 return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL;
880}
881
882/**
883 * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
884 * @fwnode: Firmware node to get the property from.
885 * @propname: Name of the property.
886 * @proptype: Expected property type.
887 * @val: Location to store the property value (if not %NULL).
888 * @nval: Size of the array pointed to by @val.
889 *
890 * If @val is %NULL, return the number of array elements comprising the value
891 * of the property. Otherwise, read at most @nval values to the array at the
892 * location pointed to by @val.
893 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300894int acpi_node_prop_read(const struct fwnode_handle *fwnode,
895 const char *propname, enum dev_prop_type proptype,
896 void *val, size_t nval)
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200897{
898 return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
899 propname, proptype, val, nval);
900}
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200901
902/**
Mika Westerberg34055192017-03-28 10:52:18 +0300903 * acpi_get_next_subnode - Return the next child node handle for a fwnode
904 * @fwnode: Firmware node to find the next child node for.
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200905 * @child: Handle to one of the device's child nodes or a null handle.
906 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300907struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200908 struct fwnode_handle *child)
909{
Sakari Ailus01c1da22017-07-21 14:39:32 +0300910 const struct acpi_device *adev = to_acpi_device_node(fwnode);
911 struct acpi_device *child_adev = NULL;
912 const struct list_head *head;
913 struct list_head *next;
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200914
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300915 if (!child || is_acpi_device_node(child)) {
Mika Westerberg34055192017-03-28 10:52:18 +0300916 if (adev)
917 head = &adev->children;
918 else
919 goto nondev;
920
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200921 if (list_empty(head))
922 goto nondev;
923
924 if (child) {
Sakari Ailus01c1da22017-07-21 14:39:32 +0300925 child_adev = to_acpi_device_node(child);
926 next = child_adev->node.next;
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200927 if (next == head) {
928 child = NULL;
929 goto nondev;
930 }
Sakari Ailus01c1da22017-07-21 14:39:32 +0300931 child_adev = list_entry(next, struct acpi_device, node);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200932 } else {
Sakari Ailus01c1da22017-07-21 14:39:32 +0300933 child_adev = list_first_entry(head, struct acpi_device,
934 node);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200935 }
Sakari Ailus01c1da22017-07-21 14:39:32 +0300936 return acpi_fwnode_handle(child_adev);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200937 }
938
939 nondev:
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300940 if (!child || is_acpi_data_node(child)) {
Sakari Ailus01c1da22017-07-21 14:39:32 +0300941 const struct acpi_data_node *data = to_acpi_data_node(fwnode);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200942 struct acpi_data_node *dn;
943
Sakari Ailus01c1da22017-07-21 14:39:32 +0300944 if (child_adev)
945 head = &child_adev->data.subnodes;
Mika Westerberg34055192017-03-28 10:52:18 +0300946 else if (data)
947 head = &data->data.subnodes;
948 else
949 return NULL;
950
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200951 if (list_empty(head))
952 return NULL;
953
954 if (child) {
955 dn = to_acpi_data_node(child);
956 next = dn->sibling.next;
957 if (next == head)
958 return NULL;
959
960 dn = list_entry(next, struct acpi_data_node, sibling);
961 } else {
962 dn = list_first_entry(head, struct acpi_data_node, sibling);
963 }
964 return &dn->fwnode;
965 }
966 return NULL;
967}
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300968
969/**
970 * acpi_node_get_parent - Return parent fwnode of this fwnode
971 * @fwnode: Firmware node whose parent to get
972 *
973 * Returns parent node of an ACPI device or data firmware node or %NULL if
974 * not available.
975 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300976struct fwnode_handle *acpi_node_get_parent(const struct fwnode_handle *fwnode)
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300977{
978 if (is_acpi_data_node(fwnode)) {
979 /* All data nodes have parent pointer so just return that */
980 return to_acpi_data_node(fwnode)->parent;
981 } else if (is_acpi_device_node(fwnode)) {
982 acpi_handle handle, parent_handle;
983
984 handle = to_acpi_device_node(fwnode)->handle;
985 if (ACPI_SUCCESS(acpi_get_parent(handle, &parent_handle))) {
986 struct acpi_device *adev;
987
988 if (!acpi_bus_get_device(parent_handle, &adev))
989 return acpi_fwnode_handle(adev);
990 }
991 }
992
993 return NULL;
994}
Mika Westerberg79389a82017-03-28 10:52:20 +0300995
996/**
997 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
998 * @fwnode: Pointer to the parent firmware node
999 * @prev: Previous endpoint node or %NULL to get the first
1000 *
1001 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
1002 * %NULL if there is no next endpoint, ERR_PTR() in case of error. In case
1003 * of success the next endpoint is returned.
1004 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001005struct fwnode_handle *acpi_graph_get_next_endpoint(
1006 const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
Mika Westerberg79389a82017-03-28 10:52:20 +03001007{
1008 struct fwnode_handle *port = NULL;
1009 struct fwnode_handle *endpoint;
1010
1011 if (!prev) {
1012 do {
1013 port = fwnode_get_next_child_node(fwnode, port);
1014 /* Ports must have port property */
1015 if (fwnode_property_present(port, "port"))
1016 break;
1017 } while (port);
1018 } else {
1019 port = fwnode_get_parent(prev);
1020 }
1021
1022 if (!port)
1023 return NULL;
1024
1025 endpoint = fwnode_get_next_child_node(port, prev);
1026 while (!endpoint) {
1027 port = fwnode_get_next_child_node(fwnode, port);
1028 if (!port)
1029 break;
1030 if (fwnode_property_present(port, "port"))
1031 endpoint = fwnode_get_next_child_node(port, NULL);
1032 }
1033
1034 if (endpoint) {
1035 /* Endpoints must have "endpoint" property */
1036 if (!fwnode_property_present(endpoint, "endpoint"))
1037 return ERR_PTR(-EPROTO);
1038 }
1039
1040 return endpoint;
1041}
1042
1043/**
1044 * acpi_graph_get_child_prop_value - Return a child with a given property value
1045 * @fwnode: device fwnode
1046 * @prop_name: The name of the property to look for
1047 * @val: the desired property value
1048 *
1049 * Return the port node corresponding to a given port number. Returns
1050 * the child node on success, NULL otherwise.
1051 */
1052static struct fwnode_handle *acpi_graph_get_child_prop_value(
Sakari Ailus37ba9832017-07-21 14:39:36 +03001053 const struct fwnode_handle *fwnode, const char *prop_name,
1054 unsigned int val)
Mika Westerberg79389a82017-03-28 10:52:20 +03001055{
1056 struct fwnode_handle *child;
1057
1058 fwnode_for_each_child_node(fwnode, child) {
1059 u32 nr;
1060
Sakari Ailusb5212f52017-08-22 23:39:58 +03001061 if (fwnode_property_read_u32(child, prop_name, &nr))
Mika Westerberg79389a82017-03-28 10:52:20 +03001062 continue;
1063
1064 if (val == nr)
1065 return child;
1066 }
1067
1068 return NULL;
1069}
1070
1071
1072/**
1073 * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
1074 * @fwnode: Endpoint firmware node pointing to a remote device
1075 * @parent: Firmware node of remote port parent is filled here if not %NULL
1076 * @port: Firmware node of remote port is filled here if not %NULL
1077 * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
1078 *
1079 * Function parses remote end of ACPI firmware remote endpoint and fills in
1080 * fields requested by the caller. Returns %0 in case of success and
1081 * negative errno otherwise.
1082 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001083int acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode,
Mika Westerberg79389a82017-03-28 10:52:20 +03001084 struct fwnode_handle **parent,
1085 struct fwnode_handle **port,
1086 struct fwnode_handle **endpoint)
1087{
Sakari Ailus37ba9832017-07-21 14:39:36 +03001088 struct fwnode_handle *fwnode;
Mika Westerberg79389a82017-03-28 10:52:20 +03001089 unsigned int port_nr, endpoint_nr;
1090 struct acpi_reference_args args;
1091 int ret;
1092
1093 memset(&args, 0, sizeof(args));
Sakari Ailus37ba9832017-07-21 14:39:36 +03001094 ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
Mika Westerberg79389a82017-03-28 10:52:20 +03001095 &args);
1096 if (ret)
1097 return ret;
1098
1099 /*
1100 * Always require two arguments with the reference: port and
1101 * endpoint indices.
1102 */
1103 if (args.nargs != 2)
1104 return -EPROTO;
1105
1106 fwnode = acpi_fwnode_handle(args.adev);
1107 port_nr = args.args[0];
1108 endpoint_nr = args.args[1];
1109
1110 if (parent)
1111 *parent = fwnode;
1112
1113 if (!port && !endpoint)
1114 return 0;
1115
1116 fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
1117 if (!fwnode)
1118 return -EPROTO;
1119
1120 if (port)
1121 *port = fwnode;
1122
1123 if (!endpoint)
1124 return 0;
1125
1126 fwnode = acpi_graph_get_child_prop_value(fwnode, "endpoint",
1127 endpoint_nr);
1128 if (!fwnode)
1129 return -EPROTO;
1130
1131 *endpoint = fwnode;
1132
1133 return 0;
1134}
Sakari Ailus37081842017-06-06 12:37:37 +03001135
Sakari Ailus37ba9832017-07-21 14:39:36 +03001136static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001137{
1138 if (!is_acpi_device_node(fwnode))
1139 return false;
1140
1141 return acpi_device_is_present(to_acpi_device_node(fwnode));
1142}
1143
Sakari Ailus37ba9832017-07-21 14:39:36 +03001144static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +03001145 const char *propname)
1146{
1147 return !acpi_node_prop_get(fwnode, propname, NULL);
1148}
1149
Sakari Ailus37ba9832017-07-21 14:39:36 +03001150static int
1151acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1152 const char *propname,
1153 unsigned int elem_size, void *val,
1154 size_t nval)
Sakari Ailus37081842017-06-06 12:37:37 +03001155{
1156 enum dev_prop_type type;
1157
1158 switch (elem_size) {
1159 case sizeof(u8):
1160 type = DEV_PROP_U8;
1161 break;
1162 case sizeof(u16):
1163 type = DEV_PROP_U16;
1164 break;
1165 case sizeof(u32):
1166 type = DEV_PROP_U32;
1167 break;
1168 case sizeof(u64):
1169 type = DEV_PROP_U64;
1170 break;
1171 default:
1172 return -ENXIO;
1173 }
1174
1175 return acpi_node_prop_read(fwnode, propname, type, val, nval);
1176}
1177
Sakari Ailus37ba9832017-07-21 14:39:36 +03001178static int
1179acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1180 const char *propname, const char **val,
1181 size_t nval)
Sakari Ailus37081842017-06-06 12:37:37 +03001182{
1183 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
1184 val, nval);
1185}
1186
1187static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001188acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +03001189 const char *childname)
1190{
1191 struct fwnode_handle *child;
1192
1193 /*
1194 * Find first matching named child node of this fwnode.
1195 * For ACPI this will be a data only sub-node.
1196 */
1197 fwnode_for_each_child_node(fwnode, child)
1198 if (acpi_data_node_match(child, childname))
1199 return child;
1200
1201 return NULL;
1202}
1203
Sakari Ailus3e3119d2017-07-21 15:11:49 +03001204static int
1205acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1206 const char *prop, const char *nargs_prop,
1207 unsigned int args_count, unsigned int index,
1208 struct fwnode_reference_args *args)
1209{
1210 struct acpi_reference_args acpi_args;
1211 unsigned int i;
1212 int ret;
1213
1214 ret = __acpi_node_get_property_reference(fwnode, prop, index,
1215 args_count, &acpi_args);
1216 if (ret < 0)
1217 return ret;
1218 if (!args)
1219 return 0;
1220
1221 args->nargs = acpi_args.nargs;
1222 args->fwnode = acpi_fwnode_handle(acpi_args.adev);
1223
1224 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
1225 args->args[i] = i < acpi_args.nargs ? acpi_args.args[i] : 0;
1226
1227 return 0;
1228}
1229
Sakari Ailus3b27d002017-06-06 12:37:38 +03001230static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001231acpi_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
Sakari Ailus3b27d002017-06-06 12:37:38 +03001232 struct fwnode_handle *prev)
1233{
1234 struct fwnode_handle *endpoint;
1235
1236 endpoint = acpi_graph_get_next_endpoint(fwnode, prev);
1237 if (IS_ERR(endpoint))
1238 return NULL;
1239
1240 return endpoint;
1241}
1242
1243static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001244acpi_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
Sakari Ailus3b27d002017-06-06 12:37:38 +03001245{
1246 struct fwnode_handle *endpoint = NULL;
1247
1248 acpi_graph_get_remote_endpoint(fwnode, NULL, NULL, &endpoint);
1249
1250 return endpoint;
1251}
1252
Sakari Ailus37ba9832017-07-21 14:39:36 +03001253static struct fwnode_handle *
1254acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
1255{
1256 return acpi_node_get_parent(fwnode);
1257}
1258
1259static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
Sakari Ailus3b27d002017-06-06 12:37:38 +03001260 struct fwnode_endpoint *endpoint)
1261{
1262 struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1263
1264 endpoint->local_fwnode = fwnode;
1265
1266 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1267 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1268
1269 return 0;
1270}
1271
Sakari Ailusdb3e50f2017-07-21 14:39:31 +03001272#define DECLARE_ACPI_FWNODE_OPS(ops) \
1273 const struct fwnode_operations ops = { \
1274 .device_is_available = acpi_fwnode_device_is_available, \
1275 .property_present = acpi_fwnode_property_present, \
1276 .property_read_int_array = \
1277 acpi_fwnode_property_read_int_array, \
1278 .property_read_string_array = \
1279 acpi_fwnode_property_read_string_array, \
1280 .get_parent = acpi_node_get_parent, \
1281 .get_next_child_node = acpi_get_next_subnode, \
1282 .get_named_child_node = acpi_fwnode_get_named_child_node, \
Sakari Ailus3e3119d2017-07-21 15:11:49 +03001283 .get_reference_args = acpi_fwnode_get_reference_args, \
Sakari Ailusdb3e50f2017-07-21 14:39:31 +03001284 .graph_get_next_endpoint = \
1285 acpi_fwnode_graph_get_next_endpoint, \
1286 .graph_get_remote_endpoint = \
1287 acpi_fwnode_graph_get_remote_endpoint, \
Sakari Ailus37ba9832017-07-21 14:39:36 +03001288 .graph_get_port_parent = acpi_fwnode_get_parent, \
Sakari Ailusdb3e50f2017-07-21 14:39:31 +03001289 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1290 }; \
1291 EXPORT_SYMBOL_GPL(ops)
1292
1293DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
1294DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
1295const struct fwnode_operations acpi_static_fwnode_ops;