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