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