blob: a65c09cc223f4294bb4f4d000ac074d79f8f7262 [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
Rafael J. Wysockibd8191c2015-08-27 04:35:14 +0200341 /*
342 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
343 * Device Tree compatible properties for this device.
344 */
345 list_for_each_entry(hwid, &adev->pnp.ids, list) {
346 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
347 acpi_of = true;
348 break;
349 }
350 }
351
352 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
353 ACPI_TYPE_PACKAGE);
354 if (ACPI_FAILURE(status))
355 goto out;
356
357 if (acpi_extract_properties(buf.pointer, &adev->data)) {
358 adev->data.pointer = buf.pointer;
359 if (acpi_of)
360 acpi_init_of_compatible(adev);
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200361 }
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300362 if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
363 &adev->data, acpi_fwnode_handle(adev)))
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200364 adev->data.pointer = buf.pointer;
365
366 if (!adev->data.pointer) {
Rafael J. Wysockibd8191c2015-08-27 04:35:14 +0200367 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
368 ACPI_FREE(buf.pointer);
369 }
Rafael J. Wysocki5c53b262015-05-05 15:43:07 +0200370
371 out:
372 if (acpi_of && !adev->flags.of_compatible_ok)
373 acpi_handle_info(adev->handle,
Rafael J. Wysockiee892092015-05-22 04:24:34 +0200374 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
Mika Westerbergffdcd952014-10-21 13:33:55 +0200375}
376
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200377static void acpi_destroy_nondev_subnodes(struct list_head *list)
378{
379 struct acpi_data_node *dn, *next;
380
381 if (list_empty(list))
382 return;
383
384 list_for_each_entry_safe_reverse(dn, next, list, sibling) {
385 acpi_destroy_nondev_subnodes(&dn->data.subnodes);
Rafael J. Wysocki263b4c12015-08-27 04:37:19 +0200386 wait_for_completion(&dn->kobj_done);
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200387 list_del(&dn->sibling);
388 ACPI_FREE((void *)dn->data.pointer);
389 kfree(dn);
390 }
391}
392
Mika Westerbergffdcd952014-10-21 13:33:55 +0200393void acpi_free_properties(struct acpi_device *adev)
394{
Rafael J. Wysocki445b0eb2015-08-27 04:36:14 +0200395 acpi_destroy_nondev_subnodes(&adev->data.subnodes);
Mika Westerbergffdcd952014-10-21 13:33:55 +0200396 ACPI_FREE((void *)adev->data.pointer);
Mika Westerberg733e6252014-10-21 13:33:56 +0200397 adev->data.of_compatible = NULL;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200398 adev->data.pointer = NULL;
399 adev->data.properties = NULL;
400}
401
402/**
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200403 * acpi_data_get_property - return an ACPI property with given name
404 * @data: ACPI device deta object to get the property from
Mika Westerbergffdcd952014-10-21 13:33:55 +0200405 * @name: Name of the property
406 * @type: Expected property type
407 * @obj: Location to store the property value (if not %NULL)
408 *
409 * Look up a property with @name and store a pointer to the resulting ACPI
410 * object at the location pointed to by @obj if found.
411 *
412 * Callers must not attempt to free the returned objects. These objects will be
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200413 * freed by the ACPI core automatically during the removal of @data.
Mika Westerbergffdcd952014-10-21 13:33:55 +0200414 *
415 * Return: %0 if property with @name has been found (success),
416 * %-EINVAL if the arguments are invalid,
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200417 * %-EINVAL if the property doesn't exist,
Mika Westerbergffdcd952014-10-21 13:33:55 +0200418 * %-EPROTO if the property value type doesn't match @type.
419 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300420static int acpi_data_get_property(const struct acpi_device_data *data,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200421 const char *name, acpi_object_type type,
422 const union acpi_object **obj)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200423{
424 const union acpi_object *properties;
425 int i;
426
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200427 if (!data || !name)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200428 return -EINVAL;
429
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200430 if (!data->pointer || !data->properties)
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200431 return -EINVAL;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200432
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200433 properties = data->properties;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200434 for (i = 0; i < properties->package.count; i++) {
435 const union acpi_object *propname, *propvalue;
436 const union acpi_object *property;
437
438 property = &properties->package.elements[i];
439
440 propname = &property->package.elements[0];
441 propvalue = &property->package.elements[1];
442
443 if (!strcmp(name, propname->string.pointer)) {
444 if (type != ACPI_TYPE_ANY && propvalue->type != type)
445 return -EPROTO;
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200446 if (obj)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200447 *obj = propvalue;
448
449 return 0;
450 }
451 }
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200452 return -EINVAL;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200453}
Mika Westerbergffdcd952014-10-21 13:33:55 +0200454
455/**
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200456 * acpi_dev_get_property - return an ACPI property with given name.
457 * @adev: ACPI device to get the property from.
458 * @name: Name of the property.
459 * @type: Expected property type.
460 * @obj: Location to store the property value (if not %NULL).
461 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300462int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200463 acpi_object_type type, const union acpi_object **obj)
464{
465 return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
466}
467EXPORT_SYMBOL_GPL(acpi_dev_get_property);
468
Sakari Ailus99a85462017-07-21 14:39:34 +0300469static const struct acpi_device_data *
470acpi_device_data_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200471{
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300472 if (is_acpi_device_node(fwnode)) {
Sakari Ailus99a85462017-07-21 14:39:34 +0300473 const struct acpi_device *adev = to_acpi_device_node(fwnode);
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200474 return &adev->data;
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300475 } else if (is_acpi_data_node(fwnode)) {
Sakari Ailus99a85462017-07-21 14:39:34 +0300476 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200477 return &dn->data;
478 }
479 return NULL;
480}
481
482/**
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 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300488int acpi_node_prop_get(const struct fwnode_handle *fwnode,
489 const char *propname, void **valptr)
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200490{
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
Mika Westerbergffdcd952014-10-21 13:33:55 +0200499 * @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
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200507 * freed by the ACPI core automatically during the removal of @data.
Mika Westerbergffdcd952014-10-21 13:33:55 +0200508 *
509 * Return: %0 if array property (package) with @name has been found (success),
510 * %-EINVAL if the arguments are invalid,
Andy Shevchenko3c60f112015-11-30 17:11:35 +0200511 * %-EINVAL if the property doesn't exist,
Mika Westerbergffdcd952014-10-21 13:33:55 +0200512 * %-EPROTO if the property is not a package or the type of its elements
513 * doesn't match @type.
514 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300515static int acpi_data_get_property_array(const struct acpi_device_data *data,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200516 const char *name,
517 acpi_object_type type,
518 const union acpi_object **obj)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200519{
520 const union acpi_object *prop;
521 int ret, i;
522
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200523 ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
Mika Westerbergffdcd952014-10-21 13:33:55 +0200524 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}
Mika Westerbergffdcd952014-10-21 13:33:55 +0200538
539/**
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300540 * __acpi_node_get_property_reference - returns handle to the referenced object
541 * @fwnode: Firmware node to get the property from
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200542 * @propname: Name of the property
Mika Westerbergffdcd952014-10-21 13:33:55 +0200543 * @index: Index of the reference to return
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300544 * @num_args: Maximum number of arguments after each reference
Mika Westerbergffdcd952014-10-21 13:33:55 +0200545 * @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
Rafael J. Wysocki60ba0322014-11-05 00:29:07 +0100549 * target object in @args->adev. If the reference includes arguments, store
550 * them in the @args->args[] array.
Mika Westerbergffdcd952014-10-21 13:33:55 +0200551 *
Rafael J. Wysocki60ba0322014-11-05 00:29:07 +0100552 * If there's more than one reference in the property value package, @index is
553 * used to select the one to return.
Mika Westerbergffdcd952014-10-21 13:33:55 +0200554 *
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300555 * 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 *
Mika Westerbergffdcd952014-10-21 13:33:55 +0200573 * Return: %0 on success, negative error code on failure.
574 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300575int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300576 const char *propname, size_t index, size_t num_args,
577 struct acpi_reference_args *args)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200578{
579 const union acpi_object *element, *end;
580 const union acpi_object *obj;
Sakari Ailus99a85462017-07-21 14:39:34 +0300581 const struct acpi_device_data *data;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200582 struct acpi_device *device;
583 int ret, idx = 0;
584
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300585 data = acpi_device_data_of_node(fwnode);
586 if (!data)
587 return -EINVAL;
588
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200589 ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
Mika Westerbergffdcd952014-10-21 13:33:55 +0200590 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) {
Rafael J. Wysocki60ba0322014-11-05 00:29:07 +0100598 if (index)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200599 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
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300628 if (element->type == ACPI_TYPE_LOCAL_REFERENCE) {
629 ret = acpi_bus_get_device(element->reference.handle,
630 &device);
631 if (ret)
632 return -ENODEV;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200633
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300634 nargs = 0;
635 element++;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200636
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300637 /* assume following integer elements are all args */
638 for (i = 0; element + i < end && i < num_args; i++) {
639 int type = element[i].type;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200640
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300641 if (type == ACPI_TYPE_INTEGER)
642 nargs++;
643 else if (type == ACPI_TYPE_LOCAL_REFERENCE)
644 break;
645 else
646 return -EPROTO;
647 }
Mika Westerbergffdcd952014-10-21 13:33:55 +0200648
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300649 if (nargs > MAX_ACPI_REFERENCE_ARGS)
Mika Westerbergffdcd952014-10-21 13:33:55 +0200650 return -EPROTO;
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300651
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;
657
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;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200668 }
669
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300670 idx++;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200671 }
672
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300673 return -ENODATA;
Mika Westerbergffdcd952014-10-21 13:33:55 +0200674}
Mika Westerbergb60e4ea2016-09-29 16:39:41 +0300675EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100676
Sakari Ailus99a85462017-07-21 14:39:34 +0300677static int acpi_data_prop_read_single(const struct acpi_device_data *data,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200678 const char *propname,
679 enum dev_prop_type proptype, void *val)
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100680{
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) {
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200688 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100689 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) {
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200713 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100714 if (ret)
715 return ret;
716
717 *(char **)val = obj->string.pointer;
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300718
719 return 1;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100720 } else {
721 ret = -EINVAL;
722 }
723 return ret;
724}
725
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200726int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
727 enum dev_prop_type proptype, void *val)
728{
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300729 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;
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200738}
739
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100740static 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 }
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300813 return nval;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100814}
815
Sakari Ailus99a85462017-07-21 14:39:34 +0300816static int acpi_data_prop_read(const struct acpi_device_data *data,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200817 const char *propname,
818 enum dev_prop_type proptype,
819 void *val, size_t nval)
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100820{
821 const union acpi_object *obj;
822 const union acpi_object *items;
823 int ret;
824
825 if (val && nval == 1) {
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200826 ret = acpi_data_prop_read_single(data, propname, proptype, val);
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300827 if (ret >= 0)
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100828 return ret;
829 }
830
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200831 ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100832 if (ret)
833 return ret;
834
835 if (!val)
836 return obj->package.count;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100837
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300838 if (proptype != DEV_PROP_STRING && nval > obj->package.count)
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100839 return -EOVERFLOW;
Andy Shevchenko7dc59dc2015-08-10 19:56:48 +0300840 else if (nval <= 0)
841 return -EINVAL;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100842
843 items = obj->package.elements;
Andy Shevchenko7dc59dc2015-08-10 19:56:48 +0300844
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100845 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:
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300859 ret = acpi_copy_property_array_string(
860 items, (char **)val,
861 min_t(u32, nval, obj->package.count));
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100862 break;
863 default:
864 ret = -EINVAL;
865 break;
866 }
867 return ret;
868}
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200869
Sakari Ailus99a85462017-07-21 14:39:34 +0300870int acpi_dev_prop_read(const struct acpi_device *adev, const char *propname,
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200871 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 */
Sakari Ailus99a85462017-07-21 14:39:34 +0300888int acpi_node_prop_read(const struct fwnode_handle *fwnode,
889 const char *propname, enum dev_prop_type proptype,
890 void *val, size_t nval)
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200891{
892 return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
893 propname, proptype, val, nval);
894}
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200895
896/**
Mika Westerberg34055192017-03-28 10:52:18 +0300897 * acpi_get_next_subnode - Return the next child node handle for a fwnode
898 * @fwnode: Firmware node to find the next child node for.
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200899 * @child: Handle to one of the device's child nodes or a null handle.
900 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300901struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200902 struct fwnode_handle *child)
903{
Sakari Ailus01c1da22017-07-21 14:39:32 +0300904 const struct acpi_device *adev = to_acpi_device_node(fwnode);
905 struct acpi_device *child_adev = NULL;
906 const struct list_head *head;
907 struct list_head *next;
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200908
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300909 if (!child || is_acpi_device_node(child)) {
Mika Westerberg34055192017-03-28 10:52:18 +0300910 if (adev)
911 head = &adev->children;
912 else
913 goto nondev;
914
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200915 if (list_empty(head))
916 goto nondev;
917
918 if (child) {
Sakari Ailus01c1da22017-07-21 14:39:32 +0300919 child_adev = to_acpi_device_node(child);
920 next = child_adev->node.next;
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200921 if (next == head) {
922 child = NULL;
923 goto nondev;
924 }
Sakari Ailus01c1da22017-07-21 14:39:32 +0300925 child_adev = list_entry(next, struct acpi_device, node);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200926 } else {
Sakari Ailus01c1da22017-07-21 14:39:32 +0300927 child_adev = list_first_entry(head, struct acpi_device,
928 node);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200929 }
Sakari Ailus01c1da22017-07-21 14:39:32 +0300930 return acpi_fwnode_handle(child_adev);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200931 }
932
933 nondev:
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300934 if (!child || is_acpi_data_node(child)) {
Sakari Ailus01c1da22017-07-21 14:39:32 +0300935 const struct acpi_data_node *data = to_acpi_data_node(fwnode);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200936 struct acpi_data_node *dn;
937
Sakari Ailus01c1da22017-07-21 14:39:32 +0300938 if (child_adev)
939 head = &child_adev->data.subnodes;
Mika Westerberg34055192017-03-28 10:52:18 +0300940 else if (data)
941 head = &data->data.subnodes;
942 else
943 return NULL;
944
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200945 if (list_empty(head))
946 return NULL;
947
948 if (child) {
949 dn = to_acpi_data_node(child);
950 next = dn->sibling.next;
951 if (next == head)
952 return NULL;
953
954 dn = list_entry(next, struct acpi_data_node, sibling);
955 } else {
956 dn = list_first_entry(head, struct acpi_data_node, sibling);
957 }
958 return &dn->fwnode;
959 }
960 return NULL;
961}
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300962
963/**
964 * acpi_node_get_parent - Return parent fwnode of this fwnode
965 * @fwnode: Firmware node whose parent to get
966 *
967 * Returns parent node of an ACPI device or data firmware node or %NULL if
968 * not available.
969 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300970struct fwnode_handle *acpi_node_get_parent(const struct fwnode_handle *fwnode)
Mika Westerbergdfa672f2017-03-28 10:52:16 +0300971{
972 if (is_acpi_data_node(fwnode)) {
973 /* All data nodes have parent pointer so just return that */
974 return to_acpi_data_node(fwnode)->parent;
975 } else if (is_acpi_device_node(fwnode)) {
976 acpi_handle handle, parent_handle;
977
978 handle = to_acpi_device_node(fwnode)->handle;
979 if (ACPI_SUCCESS(acpi_get_parent(handle, &parent_handle))) {
980 struct acpi_device *adev;
981
982 if (!acpi_bus_get_device(parent_handle, &adev))
983 return acpi_fwnode_handle(adev);
984 }
985 }
986
987 return NULL;
988}
Mika Westerberg79389a82017-03-28 10:52:20 +0300989
990/**
991 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
992 * @fwnode: Pointer to the parent firmware node
993 * @prev: Previous endpoint node or %NULL to get the first
994 *
995 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
996 * %NULL if there is no next endpoint, ERR_PTR() in case of error. In case
997 * of success the next endpoint is returned.
998 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300999struct fwnode_handle *acpi_graph_get_next_endpoint(
1000 const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
Mika Westerberg79389a82017-03-28 10:52:20 +03001001{
1002 struct fwnode_handle *port = NULL;
1003 struct fwnode_handle *endpoint;
1004
1005 if (!prev) {
1006 do {
1007 port = fwnode_get_next_child_node(fwnode, port);
1008 /* Ports must have port property */
1009 if (fwnode_property_present(port, "port"))
1010 break;
1011 } while (port);
1012 } else {
1013 port = fwnode_get_parent(prev);
1014 }
1015
1016 if (!port)
1017 return NULL;
1018
1019 endpoint = fwnode_get_next_child_node(port, prev);
1020 while (!endpoint) {
1021 port = fwnode_get_next_child_node(fwnode, port);
1022 if (!port)
1023 break;
1024 if (fwnode_property_present(port, "port"))
1025 endpoint = fwnode_get_next_child_node(port, NULL);
1026 }
1027
1028 if (endpoint) {
1029 /* Endpoints must have "endpoint" property */
1030 if (!fwnode_property_present(endpoint, "endpoint"))
1031 return ERR_PTR(-EPROTO);
1032 }
1033
1034 return endpoint;
1035}
1036
1037/**
1038 * acpi_graph_get_child_prop_value - Return a child with a given property value
1039 * @fwnode: device fwnode
1040 * @prop_name: The name of the property to look for
1041 * @val: the desired property value
1042 *
1043 * Return the port node corresponding to a given port number. Returns
1044 * the child node on success, NULL otherwise.
1045 */
1046static struct fwnode_handle *acpi_graph_get_child_prop_value(
Sakari Ailus37ba9832017-07-21 14:39:36 +03001047 const struct fwnode_handle *fwnode, const char *prop_name,
1048 unsigned int val)
Mika Westerberg79389a82017-03-28 10:52:20 +03001049{
1050 struct fwnode_handle *child;
1051
1052 fwnode_for_each_child_node(fwnode, child) {
1053 u32 nr;
1054
1055 if (!fwnode_property_read_u32(fwnode, prop_name, &nr))
1056 continue;
1057
1058 if (val == nr)
1059 return child;
1060 }
1061
1062 return NULL;
1063}
1064
1065
1066/**
1067 * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
1068 * @fwnode: Endpoint firmware node pointing to a remote device
1069 * @parent: Firmware node of remote port parent is filled here if not %NULL
1070 * @port: Firmware node of remote port is filled here if not %NULL
1071 * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
1072 *
1073 * Function parses remote end of ACPI firmware remote endpoint and fills in
1074 * fields requested by the caller. Returns %0 in case of success and
1075 * negative errno otherwise.
1076 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001077int acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode,
Mika Westerberg79389a82017-03-28 10:52:20 +03001078 struct fwnode_handle **parent,
1079 struct fwnode_handle **port,
1080 struct fwnode_handle **endpoint)
1081{
Sakari Ailus37ba9832017-07-21 14:39:36 +03001082 struct fwnode_handle *fwnode;
Mika Westerberg79389a82017-03-28 10:52:20 +03001083 unsigned int port_nr, endpoint_nr;
1084 struct acpi_reference_args args;
1085 int ret;
1086
1087 memset(&args, 0, sizeof(args));
Sakari Ailus37ba9832017-07-21 14:39:36 +03001088 ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
Mika Westerberg79389a82017-03-28 10:52:20 +03001089 &args);
1090 if (ret)
1091 return ret;
1092
1093 /*
1094 * Always require two arguments with the reference: port and
1095 * endpoint indices.
1096 */
1097 if (args.nargs != 2)
1098 return -EPROTO;
1099
1100 fwnode = acpi_fwnode_handle(args.adev);
1101 port_nr = args.args[0];
1102 endpoint_nr = args.args[1];
1103
1104 if (parent)
1105 *parent = fwnode;
1106
1107 if (!port && !endpoint)
1108 return 0;
1109
1110 fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
1111 if (!fwnode)
1112 return -EPROTO;
1113
1114 if (port)
1115 *port = fwnode;
1116
1117 if (!endpoint)
1118 return 0;
1119
1120 fwnode = acpi_graph_get_child_prop_value(fwnode, "endpoint",
1121 endpoint_nr);
1122 if (!fwnode)
1123 return -EPROTO;
1124
1125 *endpoint = fwnode;
1126
1127 return 0;
1128}
Sakari Ailus37081842017-06-06 12:37:37 +03001129
Sakari Ailus37ba9832017-07-21 14:39:36 +03001130static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001131{
1132 if (!is_acpi_device_node(fwnode))
1133 return false;
1134
1135 return acpi_device_is_present(to_acpi_device_node(fwnode));
1136}
1137
Sakari Ailus37ba9832017-07-21 14:39:36 +03001138static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +03001139 const char *propname)
1140{
1141 return !acpi_node_prop_get(fwnode, propname, NULL);
1142}
1143
Sakari Ailus37ba9832017-07-21 14:39:36 +03001144static int
1145acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1146 const char *propname,
1147 unsigned int elem_size, void *val,
1148 size_t nval)
Sakari Ailus37081842017-06-06 12:37:37 +03001149{
1150 enum dev_prop_type type;
1151
1152 switch (elem_size) {
1153 case sizeof(u8):
1154 type = DEV_PROP_U8;
1155 break;
1156 case sizeof(u16):
1157 type = DEV_PROP_U16;
1158 break;
1159 case sizeof(u32):
1160 type = DEV_PROP_U32;
1161 break;
1162 case sizeof(u64):
1163 type = DEV_PROP_U64;
1164 break;
1165 default:
1166 return -ENXIO;
1167 }
1168
1169 return acpi_node_prop_read(fwnode, propname, type, val, nval);
1170}
1171
Sakari Ailus37ba9832017-07-21 14:39:36 +03001172static int
1173acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1174 const char *propname, const char **val,
1175 size_t nval)
Sakari Ailus37081842017-06-06 12:37:37 +03001176{
1177 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
1178 val, nval);
1179}
1180
1181static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001182acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +03001183 const char *childname)
1184{
1185 struct fwnode_handle *child;
1186
1187 /*
1188 * Find first matching named child node of this fwnode.
1189 * For ACPI this will be a data only sub-node.
1190 */
1191 fwnode_for_each_child_node(fwnode, child)
1192 if (acpi_data_node_match(child, childname))
1193 return child;
1194
1195 return NULL;
1196}
1197
Sakari Ailus3e3119d2017-07-21 15:11:49 +03001198static int
1199acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1200 const char *prop, const char *nargs_prop,
1201 unsigned int args_count, unsigned int index,
1202 struct fwnode_reference_args *args)
1203{
1204 struct acpi_reference_args acpi_args;
1205 unsigned int i;
1206 int ret;
1207
1208 ret = __acpi_node_get_property_reference(fwnode, prop, index,
1209 args_count, &acpi_args);
1210 if (ret < 0)
1211 return ret;
1212 if (!args)
1213 return 0;
1214
1215 args->nargs = acpi_args.nargs;
1216 args->fwnode = acpi_fwnode_handle(acpi_args.adev);
1217
1218 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
1219 args->args[i] = i < acpi_args.nargs ? acpi_args.args[i] : 0;
1220
1221 return 0;
1222}
1223
Sakari Ailus3b27d002017-06-06 12:37:38 +03001224static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001225acpi_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
Sakari Ailus3b27d002017-06-06 12:37:38 +03001226 struct fwnode_handle *prev)
1227{
1228 struct fwnode_handle *endpoint;
1229
1230 endpoint = acpi_graph_get_next_endpoint(fwnode, prev);
1231 if (IS_ERR(endpoint))
1232 return NULL;
1233
1234 return endpoint;
1235}
1236
1237static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001238acpi_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
Sakari Ailus3b27d002017-06-06 12:37:38 +03001239{
1240 struct fwnode_handle *endpoint = NULL;
1241
1242 acpi_graph_get_remote_endpoint(fwnode, NULL, NULL, &endpoint);
1243
1244 return endpoint;
1245}
1246
Sakari Ailus37ba9832017-07-21 14:39:36 +03001247static struct fwnode_handle *
1248acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
1249{
1250 return acpi_node_get_parent(fwnode);
1251}
1252
1253static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
Sakari Ailus3b27d002017-06-06 12:37:38 +03001254 struct fwnode_endpoint *endpoint)
1255{
1256 struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1257
1258 endpoint->local_fwnode = fwnode;
1259
1260 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1261 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1262
1263 return 0;
1264}
1265
Sakari Ailusdb3e50f2017-07-21 14:39:31 +03001266#define DECLARE_ACPI_FWNODE_OPS(ops) \
1267 const struct fwnode_operations ops = { \
1268 .device_is_available = acpi_fwnode_device_is_available, \
1269 .property_present = acpi_fwnode_property_present, \
1270 .property_read_int_array = \
1271 acpi_fwnode_property_read_int_array, \
1272 .property_read_string_array = \
1273 acpi_fwnode_property_read_string_array, \
1274 .get_parent = acpi_node_get_parent, \
1275 .get_next_child_node = acpi_get_next_subnode, \
1276 .get_named_child_node = acpi_fwnode_get_named_child_node, \
Sakari Ailus3e3119d2017-07-21 15:11:49 +03001277 .get_reference_args = acpi_fwnode_get_reference_args, \
Sakari Ailusdb3e50f2017-07-21 14:39:31 +03001278 .graph_get_next_endpoint = \
1279 acpi_fwnode_graph_get_next_endpoint, \
1280 .graph_get_remote_endpoint = \
1281 acpi_fwnode_graph_get_remote_endpoint, \
Sakari Ailus37ba9832017-07-21 14:39:36 +03001282 .graph_get_port_parent = acpi_fwnode_get_parent, \
Sakari Ailusdb3e50f2017-07-21 14:39:31 +03001283 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1284 }; \
1285 EXPORT_SYMBOL_GPL(ops)
1286
1287DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
1288DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
1289const struct fwnode_operations acpi_static_fwnode_ops;