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