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