Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /******************************************************************************* |
2 | * | |
3 | * Module Name: nsxfeval - Public interfaces to the ACPI subsystem | |
4 | * ACPI Object evaluation interfaces | |
5 | * | |
6 | ******************************************************************************/ | |
7 | ||
8 | /* | |
25f044e6 | 9 | * Copyright (C) 2000 - 2013, Intel Corp. |
1da177e4 LT |
10 | * All rights reserved. |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions | |
14 | * are met: | |
15 | * 1. Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions, and the following disclaimer, | |
17 | * without modification. | |
18 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer | |
19 | * substantially similar to the "NO WARRANTY" disclaimer below | |
20 | * ("Disclaimer") and any redistribution must be conditioned upon | |
21 | * including a substantially similar Disclaimer requirement for further | |
22 | * binary redistribution. | |
23 | * 3. Neither the names of the above-listed copyright holders nor the names | |
24 | * of any contributors may be used to endorse or promote products derived | |
25 | * from this software without specific prior written permission. | |
26 | * | |
27 | * Alternatively, this software may be distributed under the terms of the | |
28 | * GNU General Public License ("GPL") version 2 as published by the Free | |
29 | * Software Foundation. | |
30 | * | |
31 | * NO WARRANTY | |
32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
33 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
34 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR | |
35 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
36 | * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
37 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
38 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
39 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
40 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
41 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
42 | * POSSIBILITY OF SUCH DAMAGES. | |
43 | */ | |
44 | ||
839e928f LZ |
45 | #define EXPORT_ACPI_INTERFACES |
46 | ||
1da177e4 | 47 | #include <acpi/acpi.h> |
e2f7a777 LB |
48 | #include "accommon.h" |
49 | #include "acnamesp.h" | |
50 | #include "acinterp.h" | |
1da177e4 | 51 | |
1da177e4 | 52 | #define _COMPONENT ACPI_NAMESPACE |
4be44fcd | 53 | ACPI_MODULE_NAME("nsxfeval") |
bbc24134 LM |
54 | |
55 | /* Local prototypes */ | |
56 | static void acpi_ns_resolve_references(struct acpi_evaluate_info *info); | |
57 | ||
1da177e4 LT |
58 | /******************************************************************************* |
59 | * | |
60 | * FUNCTION: acpi_evaluate_object_typed | |
61 | * | |
ba494bee BM |
62 | * PARAMETERS: handle - Object handle (optional) |
63 | * pathname - Object pathname (optional) | |
44f6c012 | 64 | * external_params - List of parameters to pass to method, |
73a3090a | 65 | * terminated by NULL. May be NULL |
1da177e4 | 66 | * if no parameters are being passed. |
44f6c012 | 67 | * return_buffer - Where to put method's return value (if |
73a3090a | 68 | * any). If NULL, no value is returned. |
1da177e4 LT |
69 | * return_type - Expected type of return object |
70 | * | |
71 | * RETURN: Status | |
72 | * | |
73 | * DESCRIPTION: Find and evaluate the given object, passing the given | |
73a3090a | 74 | * parameters if necessary. One of "Handle" or "Pathname" must |
1da177e4 LT |
75 | * be valid (non-null) |
76 | * | |
77 | ******************************************************************************/ | |
bbc24134 | 78 | |
1da177e4 | 79 | acpi_status |
4be44fcd LB |
80 | acpi_evaluate_object_typed(acpi_handle handle, |
81 | acpi_string pathname, | |
fd350943 LB |
82 | struct acpi_object_list *external_params, |
83 | struct acpi_buffer *return_buffer, | |
4be44fcd | 84 | acpi_object_type return_type) |
1da177e4 | 85 | { |
4be44fcd LB |
86 | acpi_status status; |
87 | u8 must_free = FALSE; | |
1da177e4 | 88 | |
b229cf92 | 89 | ACPI_FUNCTION_TRACE(acpi_evaluate_object_typed); |
1da177e4 LT |
90 | |
91 | /* Return buffer must be valid */ | |
92 | ||
93 | if (!return_buffer) { | |
4be44fcd | 94 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
1da177e4 LT |
95 | } |
96 | ||
97 | if (return_buffer->length == ACPI_ALLOCATE_BUFFER) { | |
98 | must_free = TRUE; | |
99 | } | |
100 | ||
101 | /* Evaluate the object */ | |
102 | ||
4be44fcd LB |
103 | status = |
104 | acpi_evaluate_object(handle, pathname, external_params, | |
105 | return_buffer); | |
106 | if (ACPI_FAILURE(status)) { | |
107 | return_ACPI_STATUS(status); | |
1da177e4 LT |
108 | } |
109 | ||
110 | /* Type ANY means "don't care" */ | |
111 | ||
112 | if (return_type == ACPI_TYPE_ANY) { | |
4be44fcd | 113 | return_ACPI_STATUS(AE_OK); |
1da177e4 LT |
114 | } |
115 | ||
116 | if (return_buffer->length == 0) { | |
52fc0b02 | 117 | |
1da177e4 LT |
118 | /* Error because caller specifically asked for a return value */ |
119 | ||
b8e4d893 | 120 | ACPI_ERROR((AE_INFO, "No return value")); |
4be44fcd | 121 | return_ACPI_STATUS(AE_NULL_OBJECT); |
1da177e4 LT |
122 | } |
123 | ||
124 | /* Examine the object type returned from evaluate_object */ | |
125 | ||
4be44fcd LB |
126 | if (((union acpi_object *)return_buffer->pointer)->type == return_type) { |
127 | return_ACPI_STATUS(AE_OK); | |
1da177e4 LT |
128 | } |
129 | ||
130 | /* Return object type does not match requested type */ | |
131 | ||
b8e4d893 BM |
132 | ACPI_ERROR((AE_INFO, |
133 | "Incorrect return type [%s] requested [%s]", | |
134 | acpi_ut_get_type_name(((union acpi_object *)return_buffer-> | |
135 | pointer)->type), | |
136 | acpi_ut_get_type_name(return_type))); | |
1da177e4 LT |
137 | |
138 | if (must_free) { | |
52fc0b02 | 139 | |
1da177e4 LT |
140 | /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */ |
141 | ||
bb1cab3d | 142 | ACPI_FREE_BUFFER(*return_buffer); |
1da177e4 LT |
143 | return_buffer->pointer = NULL; |
144 | } | |
145 | ||
146 | return_buffer->length = 0; | |
4be44fcd | 147 | return_ACPI_STATUS(AE_TYPE); |
1da177e4 | 148 | } |
8313524a BM |
149 | |
150 | ACPI_EXPORT_SYMBOL(acpi_evaluate_object_typed) | |
2c03d07a | 151 | |
1da177e4 LT |
152 | /******************************************************************************* |
153 | * | |
154 | * FUNCTION: acpi_evaluate_object | |
155 | * | |
ba494bee BM |
156 | * PARAMETERS: handle - Object handle (optional) |
157 | * pathname - Object pathname (optional) | |
1da177e4 | 158 | * external_params - List of parameters to pass to method, |
73a3090a | 159 | * terminated by NULL. May be NULL |
1da177e4 LT |
160 | * if no parameters are being passed. |
161 | * return_buffer - Where to put method's return value (if | |
73a3090a | 162 | * any). If NULL, no value is returned. |
1da177e4 LT |
163 | * |
164 | * RETURN: Status | |
165 | * | |
166 | * DESCRIPTION: Find and evaluate the given object, passing the given | |
73a3090a | 167 | * parameters if necessary. One of "Handle" or "Pathname" must |
1da177e4 LT |
168 | * be valid (non-null) |
169 | * | |
170 | ******************************************************************************/ | |
1da177e4 | 171 | acpi_status |
4be44fcd LB |
172 | acpi_evaluate_object(acpi_handle handle, |
173 | acpi_string pathname, | |
174 | struct acpi_object_list *external_params, | |
175 | struct acpi_buffer *return_buffer) | |
1da177e4 | 176 | { |
4be44fcd | 177 | acpi_status status; |
4119532c | 178 | struct acpi_evaluate_info *info; |
4be44fcd LB |
179 | acpi_size buffer_space_needed; |
180 | u32 i; | |
1da177e4 | 181 | |
b229cf92 | 182 | ACPI_FUNCTION_TRACE(acpi_evaluate_object); |
1da177e4 | 183 | |
4119532c BM |
184 | /* Allocate and initialize the evaluation information block */ |
185 | ||
186 | info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info)); | |
187 | if (!info) { | |
188 | return_ACPI_STATUS(AE_NO_MEMORY); | |
189 | } | |
190 | ||
4119532c BM |
191 | /* Convert and validate the device handle */ |
192 | ||
f24b664d | 193 | info->prefix_node = acpi_ns_validate_handle(handle); |
4119532c BM |
194 | if (!info->prefix_node) { |
195 | status = AE_BAD_PARAMETER; | |
196 | goto cleanup; | |
197 | } | |
1da177e4 LT |
198 | |
199 | /* | |
29a241cc BM |
200 | * Get the actual namespace node for the target object. |
201 | * Handles these cases: | |
202 | * | |
203 | * 1) Null node, valid pathname from root (absolute path) | |
204 | * 2) Node and valid pathname (path relative to Node) | |
205 | * 3) Node, Null pathname | |
206 | */ | |
207 | if ((pathname) && (ACPI_IS_ROOT_PREFIX(pathname[0]))) { | |
208 | ||
209 | /* The path is fully qualified, just evaluate by name */ | |
210 | ||
211 | info->prefix_node = NULL; | |
212 | } else if (!handle) { | |
213 | /* | |
214 | * A handle is optional iff a fully qualified pathname is specified. | |
215 | * Since we've already handled fully qualified names above, this is | |
216 | * an error. | |
217 | */ | |
218 | if (!pathname) { | |
219 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
220 | "Both Handle and Pathname are NULL")); | |
221 | } else { | |
222 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
223 | "Null Handle with relative pathname [%s]", | |
224 | pathname)); | |
225 | } | |
226 | ||
227 | status = AE_BAD_PARAMETER; | |
228 | goto cleanup; | |
229 | } | |
230 | ||
231 | info->relative_pathname = pathname; | |
232 | ||
233 | /* | |
234 | * Convert all external objects passed as arguments to the | |
235 | * internal version(s). | |
1da177e4 LT |
236 | */ |
237 | if (external_params && external_params->count) { | |
29a241cc BM |
238 | info->param_count = (u16)external_params->count; |
239 | ||
240 | /* Warn on impossible argument count */ | |
241 | ||
242 | if (info->param_count > ACPI_METHOD_NUM_ARGS) { | |
243 | ACPI_WARN_PREDEFINED((AE_INFO, pathname, | |
244 | ACPI_WARN_ALWAYS, | |
245 | "Excess arguments (%u) - using only %u", | |
246 | info->param_count, | |
247 | ACPI_METHOD_NUM_ARGS)); | |
248 | ||
249 | info->param_count = ACPI_METHOD_NUM_ARGS; | |
250 | } | |
251 | ||
1da177e4 LT |
252 | /* |
253 | * Allocate a new parameter block for the internal objects | |
254 | * Add 1 to count to allow for null terminated internal list | |
255 | */ | |
29a241cc BM |
256 | info->parameters = ACPI_ALLOCATE_ZEROED(((acpi_size) info-> |
257 | param_count + | |
4119532c BM |
258 | 1) * sizeof(void *)); |
259 | if (!info->parameters) { | |
260 | status = AE_NO_MEMORY; | |
261 | goto cleanup; | |
1da177e4 LT |
262 | } |
263 | ||
4119532c BM |
264 | /* Convert each external object in the list to an internal object */ |
265 | ||
29a241cc | 266 | for (i = 0; i < info->param_count; i++) { |
4be44fcd LB |
267 | status = |
268 | acpi_ut_copy_eobject_to_iobject(&external_params-> | |
269 | pointer[i], | |
4119532c | 270 | &info-> |
4be44fcd LB |
271 | parameters[i]); |
272 | if (ACPI_FAILURE(status)) { | |
4119532c | 273 | goto cleanup; |
1da177e4 LT |
274 | } |
275 | } | |
29a241cc BM |
276 | |
277 | info->parameters[info->param_count] = NULL; | |
1da177e4 LT |
278 | } |
279 | ||
29a241cc BM |
280 | #if 0 |
281 | ||
1da177e4 | 282 | /* |
29a241cc BM |
283 | * Begin incoming argument count analysis. Check for too few args |
284 | * and too many args. | |
1da177e4 | 285 | */ |
52fc0b02 | 286 | |
29a241cc BM |
287 | switch (acpi_ns_get_type(info->node)) { |
288 | case ACPI_TYPE_METHOD: | |
289 | ||
290 | /* Check incoming argument count against the method definition */ | |
291 | ||
292 | if (info->obj_desc->method.param_count > info->param_count) { | |
293 | ACPI_ERROR((AE_INFO, | |
294 | "Insufficient arguments (%u) - %u are required", | |
295 | info->param_count, | |
296 | info->obj_desc->method.param_count)); | |
297 | ||
298 | status = AE_MISSING_ARGUMENTS; | |
299 | goto cleanup; | |
300 | } | |
301 | ||
302 | else if (info->obj_desc->method.param_count < info->param_count) { | |
303 | ACPI_WARNING((AE_INFO, | |
304 | "Excess arguments (%u) - only %u are required", | |
305 | info->param_count, | |
306 | info->obj_desc->method.param_count)); | |
307 | ||
308 | /* Just pass the required number of arguments */ | |
309 | ||
310 | info->param_count = info->obj_desc->method.param_count; | |
311 | } | |
52fc0b02 | 312 | |
1da177e4 | 313 | /* |
29a241cc BM |
314 | * Any incoming external objects to be passed as arguments to the |
315 | * method must be converted to internal objects | |
1da177e4 | 316 | */ |
29a241cc BM |
317 | if (info->param_count) { |
318 | /* | |
319 | * Allocate a new parameter block for the internal objects | |
320 | * Add 1 to count to allow for null terminated internal list | |
321 | */ | |
322 | info->parameters = ACPI_ALLOCATE_ZEROED(((acpi_size) | |
323 | info-> | |
324 | param_count + | |
325 | 1) * | |
326 | sizeof(void *)); | |
327 | if (!info->parameters) { | |
328 | status = AE_NO_MEMORY; | |
329 | goto cleanup; | |
330 | } | |
331 | ||
332 | /* Convert each external object in the list to an internal object */ | |
333 | ||
334 | for (i = 0; i < info->param_count; i++) { | |
335 | status = | |
336 | acpi_ut_copy_eobject_to_iobject | |
337 | (&external_params->pointer[i], | |
338 | &info->parameters[i]); | |
339 | if (ACPI_FAILURE(status)) { | |
340 | goto cleanup; | |
341 | } | |
342 | } | |
343 | ||
344 | info->parameters[info->param_count] = NULL; | |
1da177e4 | 345 | } |
29a241cc | 346 | break; |
1da177e4 | 347 | |
29a241cc BM |
348 | default: |
349 | ||
350 | /* Warn if arguments passed to an object that is not a method */ | |
52fc0b02 | 351 | |
29a241cc BM |
352 | if (info->param_count) { |
353 | ACPI_WARNING((AE_INFO, | |
354 | "%u arguments were passed to a non-method ACPI object", | |
355 | info->param_count)); | |
356 | } | |
357 | break; | |
1da177e4 LT |
358 | } |
359 | ||
29a241cc BM |
360 | #endif |
361 | ||
362 | /* Now we can evaluate the object */ | |
363 | ||
364 | status = acpi_ns_evaluate(info); | |
365 | ||
1da177e4 LT |
366 | /* |
367 | * If we are expecting a return value, and all went well above, | |
368 | * copy the return value to an external object. | |
369 | */ | |
370 | if (return_buffer) { | |
4119532c | 371 | if (!info->return_object) { |
1da177e4 | 372 | return_buffer->length = 0; |
4be44fcd | 373 | } else { |
4119532c | 374 | if (ACPI_GET_DESCRIPTOR_TYPE(info->return_object) == |
4be44fcd | 375 | ACPI_DESC_TYPE_NAMED) { |
1da177e4 LT |
376 | /* |
377 | * If we received a NS Node as a return object, this means that | |
378 | * the object we are evaluating has nothing interesting to | |
379 | * return (such as a mutex, etc.) We return an error because | |
380 | * these types are essentially unsupported by this interface. | |
381 | * We don't check up front because this makes it easier to add | |
382 | * support for various types at a later date if necessary. | |
383 | */ | |
384 | status = AE_TYPE; | |
4119532c | 385 | info->return_object = NULL; /* No need to delete a NS Node */ |
1da177e4 LT |
386 | return_buffer->length = 0; |
387 | } | |
388 | ||
4be44fcd | 389 | if (ACPI_SUCCESS(status)) { |
4119532c | 390 | |
bbc24134 LM |
391 | /* Dereference Index and ref_of references */ |
392 | ||
393 | acpi_ns_resolve_references(info); | |
394 | ||
4119532c BM |
395 | /* Get the size of the returned object */ |
396 | ||
4be44fcd | 397 | status = |
4119532c | 398 | acpi_ut_get_object_size(info->return_object, |
4be44fcd LB |
399 | &buffer_space_needed); |
400 | if (ACPI_SUCCESS(status)) { | |
52fc0b02 | 401 | |
1da177e4 LT |
402 | /* Validate/Allocate/Clear caller buffer */ |
403 | ||
4be44fcd LB |
404 | status = |
405 | acpi_ut_initialize_buffer | |
406 | (return_buffer, | |
407 | buffer_space_needed); | |
408 | if (ACPI_FAILURE(status)) { | |
1da177e4 | 409 | /* |
52fc0b02 BM |
410 | * Caller's buffer is too small or a new one can't |
411 | * be allocated | |
1da177e4 | 412 | */ |
4be44fcd LB |
413 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
414 | "Needed buffer size %X, %s\n", | |
415 | (u32) | |
416 | buffer_space_needed, | |
417 | acpi_format_exception | |
418 | (status))); | |
419 | } else { | |
52fc0b02 BM |
420 | /* We have enough space for the object, build it */ |
421 | ||
4be44fcd LB |
422 | status = |
423 | acpi_ut_copy_iobject_to_eobject | |
4119532c | 424 | (info->return_object, |
4be44fcd | 425 | return_buffer); |
1da177e4 LT |
426 | } |
427 | } | |
428 | } | |
429 | } | |
430 | } | |
431 | ||
4119532c | 432 | if (info->return_object) { |
1da177e4 | 433 | /* |
4119532c BM |
434 | * Delete the internal return object. NOTE: Interpreter must be |
435 | * locked to avoid race condition. | |
1da177e4 | 436 | */ |
4d2acd9e | 437 | acpi_ex_enter_interpreter(); |
4119532c | 438 | |
4d2acd9e | 439 | /* Remove one reference on the return object (should delete it) */ |
4119532c | 440 | |
4d2acd9e LB |
441 | acpi_ut_remove_reference(info->return_object); |
442 | acpi_ex_exit_interpreter(); | |
1da177e4 LT |
443 | } |
444 | ||
4119532c BM |
445 | cleanup: |
446 | ||
52fc0b02 BM |
447 | /* Free the input parameter list (if we created one) */ |
448 | ||
4119532c | 449 | if (info->parameters) { |
52fc0b02 | 450 | |
1da177e4 LT |
451 | /* Free the allocated parameter block */ |
452 | ||
4119532c | 453 | acpi_ut_delete_internal_object_list(info->parameters); |
1da177e4 LT |
454 | } |
455 | ||
4119532c | 456 | ACPI_FREE(info); |
4be44fcd | 457 | return_ACPI_STATUS(status); |
1da177e4 | 458 | } |
1da177e4 | 459 | |
8313524a | 460 | ACPI_EXPORT_SYMBOL(acpi_evaluate_object) |
1da177e4 | 461 | |
bbc24134 LM |
462 | /******************************************************************************* |
463 | * | |
464 | * FUNCTION: acpi_ns_resolve_references | |
465 | * | |
ba494bee | 466 | * PARAMETERS: info - Evaluation info block |
bbc24134 LM |
467 | * |
468 | * RETURN: Info->return_object is replaced with the dereferenced object | |
469 | * | |
470 | * DESCRIPTION: Dereference certain reference objects. Called before an | |
471 | * internal return object is converted to an external union acpi_object. | |
472 | * | |
473 | * Performs an automatic dereference of Index and ref_of reference objects. | |
474 | * These reference objects are not supported by the union acpi_object, so this is a | |
475 | * last resort effort to return something useful. Also, provides compatibility | |
476 | * with other ACPI implementations. | |
477 | * | |
478 | * NOTE: does not handle references within returned package objects or nested | |
479 | * references, but this support could be added later if found to be necessary. | |
480 | * | |
481 | ******************************************************************************/ | |
482 | static void acpi_ns_resolve_references(struct acpi_evaluate_info *info) | |
483 | { | |
484 | union acpi_operand_object *obj_desc = NULL; | |
485 | struct acpi_namespace_node *node; | |
486 | ||
487 | /* We are interested in reference objects only */ | |
488 | ||
3371c19c | 489 | if ((info->return_object)->common.type != ACPI_TYPE_LOCAL_REFERENCE) { |
bbc24134 LM |
490 | return; |
491 | } | |
492 | ||
493 | /* | |
494 | * Two types of references are supported - those created by Index and | |
495 | * ref_of operators. A name reference (AML_NAMEPATH_OP) can be converted | |
496 | * to an union acpi_object, so it is not dereferenced here. A ddb_handle | |
497 | * (AML_LOAD_OP) cannot be dereferenced, nor can it be converted to | |
498 | * an union acpi_object. | |
499 | */ | |
1044f1f6 BM |
500 | switch (info->return_object->reference.class) { |
501 | case ACPI_REFCLASS_INDEX: | |
bbc24134 LM |
502 | |
503 | obj_desc = *(info->return_object->reference.where); | |
504 | break; | |
505 | ||
1044f1f6 | 506 | case ACPI_REFCLASS_REFOF: |
bbc24134 LM |
507 | |
508 | node = info->return_object->reference.object; | |
509 | if (node) { | |
510 | obj_desc = node->object; | |
511 | } | |
512 | break; | |
513 | ||
514 | default: | |
1d1ea1b7 | 515 | |
bbc24134 LM |
516 | return; |
517 | } | |
518 | ||
519 | /* Replace the existing reference object */ | |
520 | ||
521 | if (obj_desc) { | |
522 | acpi_ut_add_reference(obj_desc); | |
523 | acpi_ut_remove_reference(info->return_object); | |
524 | info->return_object = obj_desc; | |
525 | } | |
526 | ||
527 | return; | |
528 | } | |
529 | ||
1da177e4 LT |
530 | /******************************************************************************* |
531 | * | |
532 | * FUNCTION: acpi_walk_namespace | |
533 | * | |
ba494bee | 534 | * PARAMETERS: type - acpi_object_type to search for |
1da177e4 LT |
535 | * start_object - Handle in namespace where search begins |
536 | * max_depth - Depth to which search is to reach | |
4ef17507 | 537 | * descending_callback - Called during tree descent |
2263576c | 538 | * when an object of "Type" is found |
4ef17507 | 539 | * ascending_callback - Called during tree ascent |
2263576c | 540 | * when an object of "Type" is found |
ba494bee | 541 | * context - Passed to user function(s) above |
1da177e4 LT |
542 | * return_value - Location where return value of |
543 | * user_function is put if terminated early | |
544 | * | |
545 | * RETURNS Return value from the user_function if terminated early. | |
546 | * Otherwise, returns NULL. | |
547 | * | |
548 | * DESCRIPTION: Performs a modified depth-first walk of the namespace tree, | |
549 | * starting (and ending) at the object specified by start_handle. | |
2263576c LM |
550 | * The callback function is called whenever an object that matches |
551 | * the type parameter is found. If the callback function returns | |
1da177e4 LT |
552 | * a non-zero value, the search is terminated immediately and this |
553 | * value is returned to the caller. | |
554 | * | |
555 | * The point of this procedure is to provide a generic namespace | |
556 | * walk routine that can be called from multiple places to | |
2263576c LM |
557 | * provide multiple services; the callback function(s) can be |
558 | * tailored to each task, whether it is a print function, | |
559 | * a compare function, etc. | |
1da177e4 LT |
560 | * |
561 | ******************************************************************************/ | |
bbc24134 | 562 | |
1da177e4 | 563 | acpi_status |
4be44fcd LB |
564 | acpi_walk_namespace(acpi_object_type type, |
565 | acpi_handle start_object, | |
566 | u32 max_depth, | |
4ef17507 BM |
567 | acpi_walk_callback descending_callback, |
568 | acpi_walk_callback ascending_callback, | |
4be44fcd | 569 | void *context, void **return_value) |
1da177e4 | 570 | { |
4be44fcd | 571 | acpi_status status; |
1da177e4 | 572 | |
b229cf92 | 573 | ACPI_FUNCTION_TRACE(acpi_walk_namespace); |
1da177e4 LT |
574 | |
575 | /* Parameter validation */ | |
576 | ||
2263576c | 577 | if ((type > ACPI_TYPE_LOCAL_MAX) || |
4ef17507 | 578 | (!max_depth) || (!descending_callback && !ascending_callback)) { |
4be44fcd | 579 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
1da177e4 LT |
580 | } |
581 | ||
582 | /* | |
8a335a23 BM |
583 | * Need to acquire the namespace reader lock to prevent interference |
584 | * with any concurrent table unloads (which causes the deletion of | |
585 | * namespace objects). We cannot allow the deletion of a namespace node | |
586 | * while the user function is using it. The exception to this are the | |
587 | * nodes created and deleted during control method execution -- these | |
588 | * nodes are marked as temporary nodes and are ignored by the namespace | |
589 | * walk. Thus, control methods can be executed while holding the | |
590 | * namespace deletion lock (and the user function can execute control | |
591 | * methods.) | |
592 | */ | |
593 | status = acpi_ut_acquire_read_lock(&acpi_gbl_namespace_rw_lock); | |
594 | if (ACPI_FAILURE(status)) { | |
7b6afb67 | 595 | return_ACPI_STATUS(status); |
8a335a23 BM |
596 | } |
597 | ||
598 | /* | |
599 | * Lock the namespace around the walk. The namespace will be | |
600 | * unlocked/locked around each call to the user function - since the user | |
601 | * function must be allowed to make ACPICA calls itself (for example, it | |
602 | * will typically execute control methods during device enumeration.) | |
1da177e4 | 603 | */ |
4be44fcd LB |
604 | status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); |
605 | if (ACPI_FAILURE(status)) { | |
8a335a23 | 606 | goto unlock_and_exit; |
1da177e4 LT |
607 | } |
608 | ||
d53d8207 BM |
609 | /* Now we can validate the starting node */ |
610 | ||
611 | if (!acpi_ns_validate_handle(start_object)) { | |
612 | status = AE_BAD_PARAMETER; | |
613 | goto unlock_and_exit2; | |
614 | } | |
615 | ||
4be44fcd | 616 | status = acpi_ns_walk_namespace(type, start_object, max_depth, |
4ef17507 BM |
617 | ACPI_NS_WALK_UNLOCK, |
618 | descending_callback, ascending_callback, | |
619 | context, return_value); | |
1da177e4 | 620 | |
d53d8207 | 621 | unlock_and_exit2: |
4be44fcd | 622 | (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); |
8a335a23 BM |
623 | |
624 | unlock_and_exit: | |
625 | (void)acpi_ut_release_read_lock(&acpi_gbl_namespace_rw_lock); | |
4be44fcd | 626 | return_ACPI_STATUS(status); |
1da177e4 | 627 | } |
1da177e4 | 628 | |
8313524a | 629 | ACPI_EXPORT_SYMBOL(acpi_walk_namespace) |
1da177e4 LT |
630 | |
631 | /******************************************************************************* | |
632 | * | |
633 | * FUNCTION: acpi_ns_get_device_callback | |
634 | * | |
635 | * PARAMETERS: Callback from acpi_get_device | |
636 | * | |
637 | * RETURN: Status | |
638 | * | |
639 | * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non- | |
640 | * present devices, or if they specified a HID, it filters based | |
641 | * on that. | |
642 | * | |
643 | ******************************************************************************/ | |
1da177e4 | 644 | static acpi_status |
4be44fcd LB |
645 | acpi_ns_get_device_callback(acpi_handle obj_handle, |
646 | u32 nesting_level, | |
647 | void *context, void **return_value) | |
1da177e4 | 648 | { |
4be44fcd LB |
649 | struct acpi_get_devices_info *info = context; |
650 | acpi_status status; | |
651 | struct acpi_namespace_node *node; | |
652 | u32 flags; | |
78e25fef LZ |
653 | struct acpi_pnp_device_id *hid; |
654 | struct acpi_pnp_device_id_list *cid; | |
67a119f9 | 655 | u32 i; |
15b8dd53 BM |
656 | u8 found; |
657 | int no_match; | |
1da177e4 | 658 | |
4be44fcd LB |
659 | status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); |
660 | if (ACPI_FAILURE(status)) { | |
7b6afb67 | 661 | return (status); |
1da177e4 LT |
662 | } |
663 | ||
f24b664d | 664 | node = acpi_ns_validate_handle(obj_handle); |
4be44fcd LB |
665 | status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); |
666 | if (ACPI_FAILURE(status)) { | |
1da177e4 LT |
667 | return (status); |
668 | } | |
669 | ||
670 | if (!node) { | |
671 | return (AE_BAD_PARAMETER); | |
672 | } | |
673 | ||
5f8902ac LM |
674 | /* |
675 | * First, filter based on the device HID and CID. | |
676 | * | |
677 | * 01/2010: For this case where a specific HID is requested, we don't | |
678 | * want to run _STA until we have an actual HID match. Thus, we will | |
679 | * not unnecessarily execute _STA on devices for which the caller | |
680 | * doesn't care about. Previously, _STA was executed unconditionally | |
681 | * on all devices found here. | |
682 | * | |
683 | * A side-effect of this change is that now we will continue to search | |
684 | * for a matching HID even under device trees where the parent device | |
685 | * would have returned a _STA that indicates it is not present or | |
686 | * not functioning (thus aborting the search on that branch). | |
687 | */ | |
1da177e4 | 688 | if (info->hid != NULL) { |
4be44fcd | 689 | status = acpi_ut_execute_HID(node, &hid); |
1da177e4 LT |
690 | if (status == AE_NOT_FOUND) { |
691 | return (AE_OK); | |
4be44fcd | 692 | } else if (ACPI_FAILURE(status)) { |
1da177e4 LT |
693 | return (AE_CTRL_DEPTH); |
694 | } | |
695 | ||
15b8dd53 BM |
696 | no_match = ACPI_STRCMP(hid->string, info->hid); |
697 | ACPI_FREE(hid); | |
1da177e4 | 698 | |
15b8dd53 BM |
699 | if (no_match) { |
700 | /* | |
701 | * HID does not match, attempt match within the | |
702 | * list of Compatible IDs (CIDs) | |
703 | */ | |
4be44fcd | 704 | status = acpi_ut_execute_CID(node, &cid); |
1da177e4 LT |
705 | if (status == AE_NOT_FOUND) { |
706 | return (AE_OK); | |
4be44fcd | 707 | } else if (ACPI_FAILURE(status)) { |
1da177e4 LT |
708 | return (AE_CTRL_DEPTH); |
709 | } | |
710 | ||
711 | /* Walk the CID list */ | |
712 | ||
739dcbb9 | 713 | found = FALSE; |
1da177e4 | 714 | for (i = 0; i < cid->count; i++) { |
15b8dd53 BM |
715 | if (ACPI_STRCMP(cid->ids[i].string, info->hid) |
716 | == 0) { | |
3e8214e5 | 717 | |
75c8044f | 718 | /* Found a matching CID */ |
3e8214e5 | 719 | |
739dcbb9 | 720 | found = TRUE; |
02f8a858 | 721 | break; |
1da177e4 LT |
722 | } |
723 | } | |
739dcbb9 | 724 | |
8313524a | 725 | ACPI_FREE(cid); |
739dcbb9 | 726 | if (!found) { |
02f8a858 | 727 | return (AE_OK); |
739dcbb9 | 728 | } |
1da177e4 LT |
729 | } |
730 | } | |
731 | ||
5f8902ac LM |
732 | /* Run _STA to determine if device is present */ |
733 | ||
734 | status = acpi_ut_execute_STA(node, &flags); | |
735 | if (ACPI_FAILURE(status)) { | |
736 | return (AE_CTRL_DEPTH); | |
737 | } | |
738 | ||
739 | if (!(flags & ACPI_STA_DEVICE_PRESENT) && | |
740 | !(flags & ACPI_STA_DEVICE_FUNCTIONING)) { | |
741 | /* | |
742 | * Don't examine the children of the device only when the | |
743 | * device is neither present nor functional. See ACPI spec, | |
744 | * description of _STA for more information. | |
745 | */ | |
746 | return (AE_CTRL_DEPTH); | |
747 | } | |
748 | ||
749 | /* We have a valid device, invoke the user function */ | |
750 | ||
4be44fcd LB |
751 | status = info->user_function(obj_handle, nesting_level, info->context, |
752 | return_value); | |
1da177e4 LT |
753 | return (status); |
754 | } | |
755 | ||
1da177e4 LT |
756 | /******************************************************************************* |
757 | * | |
758 | * FUNCTION: acpi_get_devices | |
759 | * | |
760 | * PARAMETERS: HID - HID to search for. Can be NULL. | |
761 | * user_function - Called when a matching object is found | |
ba494bee | 762 | * context - Passed to user function |
1da177e4 LT |
763 | * return_value - Location where return value of |
764 | * user_function is put if terminated early | |
765 | * | |
766 | * RETURNS Return value from the user_function if terminated early. | |
767 | * Otherwise, returns NULL. | |
768 | * | |
769 | * DESCRIPTION: Performs a modified depth-first walk of the namespace tree, | |
770 | * starting (and ending) at the object specified by start_handle. | |
771 | * The user_function is called whenever an object of type | |
73a3090a | 772 | * Device is found. If the user function returns |
1da177e4 LT |
773 | * a non-zero value, the search is terminated immediately and this |
774 | * value is returned to the caller. | |
775 | * | |
776 | * This is a wrapper for walk_namespace, but the callback performs | |
549f4604 | 777 | * additional filtering. Please see acpi_ns_get_device_callback. |
1da177e4 LT |
778 | * |
779 | ******************************************************************************/ | |
780 | ||
781 | acpi_status | |
70b30fb1 | 782 | acpi_get_devices(const char *HID, |
4be44fcd LB |
783 | acpi_walk_callback user_function, |
784 | void *context, void **return_value) | |
1da177e4 | 785 | { |
4be44fcd LB |
786 | acpi_status status; |
787 | struct acpi_get_devices_info info; | |
1da177e4 | 788 | |
b229cf92 | 789 | ACPI_FUNCTION_TRACE(acpi_get_devices); |
1da177e4 LT |
790 | |
791 | /* Parameter validation */ | |
792 | ||
793 | if (!user_function) { | |
4be44fcd | 794 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
1da177e4 LT |
795 | } |
796 | ||
797 | /* | |
798 | * We're going to call their callback from OUR callback, so we need | |
799 | * to know what it is, and their context parameter. | |
800 | */ | |
52fc0b02 | 801 | info.hid = HID; |
4be44fcd | 802 | info.context = context; |
1da177e4 | 803 | info.user_function = user_function; |
1da177e4 LT |
804 | |
805 | /* | |
806 | * Lock the namespace around the walk. | |
807 | * The namespace will be unlocked/locked around each call | |
808 | * to the user function - since this function | |
809 | * must be allowed to make Acpi calls itself. | |
810 | */ | |
4be44fcd LB |
811 | status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); |
812 | if (ACPI_FAILURE(status)) { | |
813 | return_ACPI_STATUS(status); | |
1da177e4 LT |
814 | } |
815 | ||
52fc0b02 BM |
816 | status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
817 | ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK, | |
2263576c LM |
818 | acpi_ns_get_device_callback, NULL, |
819 | &info, return_value); | |
1da177e4 | 820 | |
4be44fcd LB |
821 | (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); |
822 | return_ACPI_STATUS(status); | |
1da177e4 | 823 | } |
1da177e4 | 824 | |
8313524a | 825 | ACPI_EXPORT_SYMBOL(acpi_get_devices) |
1da177e4 LT |
826 | |
827 | /******************************************************************************* | |
828 | * | |
829 | * FUNCTION: acpi_attach_data | |
830 | * | |
831 | * PARAMETERS: obj_handle - Namespace node | |
ba494bee BM |
832 | * handler - Handler for this attachment |
833 | * data - Pointer to data to be attached | |
1da177e4 LT |
834 | * |
835 | * RETURN: Status | |
836 | * | |
837 | * DESCRIPTION: Attach arbitrary data and handler to a namespace node. | |
838 | * | |
839 | ******************************************************************************/ | |
1da177e4 | 840 | acpi_status |
4be44fcd LB |
841 | acpi_attach_data(acpi_handle obj_handle, |
842 | acpi_object_handler handler, void *data) | |
1da177e4 | 843 | { |
4be44fcd LB |
844 | struct acpi_namespace_node *node; |
845 | acpi_status status; | |
1da177e4 LT |
846 | |
847 | /* Parameter validation */ | |
848 | ||
4be44fcd | 849 | if (!obj_handle || !handler || !data) { |
1da177e4 LT |
850 | return (AE_BAD_PARAMETER); |
851 | } | |
852 | ||
4be44fcd LB |
853 | status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); |
854 | if (ACPI_FAILURE(status)) { | |
1da177e4 LT |
855 | return (status); |
856 | } | |
857 | ||
858 | /* Convert and validate the handle */ | |
859 | ||
f24b664d | 860 | node = acpi_ns_validate_handle(obj_handle); |
1da177e4 LT |
861 | if (!node) { |
862 | status = AE_BAD_PARAMETER; | |
863 | goto unlock_and_exit; | |
864 | } | |
865 | ||
4be44fcd | 866 | status = acpi_ns_attach_data(node, handler, data); |
1da177e4 | 867 | |
4be44fcd LB |
868 | unlock_and_exit: |
869 | (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); | |
1da177e4 LT |
870 | return (status); |
871 | } | |
872 | ||
8313524a BM |
873 | ACPI_EXPORT_SYMBOL(acpi_attach_data) |
874 | ||
1da177e4 LT |
875 | /******************************************************************************* |
876 | * | |
877 | * FUNCTION: acpi_detach_data | |
878 | * | |
879 | * PARAMETERS: obj_handle - Namespace node handle | |
ba494bee | 880 | * handler - Handler used in call to acpi_attach_data |
1da177e4 LT |
881 | * |
882 | * RETURN: Status | |
883 | * | |
884 | * DESCRIPTION: Remove data that was previously attached to a node. | |
885 | * | |
886 | ******************************************************************************/ | |
1da177e4 | 887 | acpi_status |
4be44fcd | 888 | acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler) |
1da177e4 | 889 | { |
4be44fcd LB |
890 | struct acpi_namespace_node *node; |
891 | acpi_status status; | |
1da177e4 LT |
892 | |
893 | /* Parameter validation */ | |
894 | ||
4be44fcd | 895 | if (!obj_handle || !handler) { |
1da177e4 LT |
896 | return (AE_BAD_PARAMETER); |
897 | } | |
898 | ||
4be44fcd LB |
899 | status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); |
900 | if (ACPI_FAILURE(status)) { | |
1da177e4 LT |
901 | return (status); |
902 | } | |
903 | ||
904 | /* Convert and validate the handle */ | |
905 | ||
f24b664d | 906 | node = acpi_ns_validate_handle(obj_handle); |
1da177e4 LT |
907 | if (!node) { |
908 | status = AE_BAD_PARAMETER; | |
909 | goto unlock_and_exit; | |
910 | } | |
911 | ||
4be44fcd | 912 | status = acpi_ns_detach_data(node, handler); |
1da177e4 | 913 | |
4be44fcd LB |
914 | unlock_and_exit: |
915 | (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); | |
1da177e4 LT |
916 | return (status); |
917 | } | |
918 | ||
8313524a BM |
919 | ACPI_EXPORT_SYMBOL(acpi_detach_data) |
920 | ||
1da177e4 LT |
921 | /******************************************************************************* |
922 | * | |
923 | * FUNCTION: acpi_get_data | |
924 | * | |
925 | * PARAMETERS: obj_handle - Namespace node | |
ba494bee BM |
926 | * handler - Handler used in call to attach_data |
927 | * data - Where the data is returned | |
1da177e4 LT |
928 | * |
929 | * RETURN: Status | |
930 | * | |
931 | * DESCRIPTION: Retrieve data that was previously attached to a namespace node. | |
932 | * | |
933 | ******************************************************************************/ | |
1da177e4 | 934 | acpi_status |
4be44fcd | 935 | acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data) |
1da177e4 | 936 | { |
4be44fcd LB |
937 | struct acpi_namespace_node *node; |
938 | acpi_status status; | |
1da177e4 LT |
939 | |
940 | /* Parameter validation */ | |
941 | ||
4be44fcd | 942 | if (!obj_handle || !handler || !data) { |
1da177e4 LT |
943 | return (AE_BAD_PARAMETER); |
944 | } | |
945 | ||
4be44fcd LB |
946 | status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); |
947 | if (ACPI_FAILURE(status)) { | |
1da177e4 LT |
948 | return (status); |
949 | } | |
950 | ||
951 | /* Convert and validate the handle */ | |
952 | ||
f24b664d | 953 | node = acpi_ns_validate_handle(obj_handle); |
1da177e4 LT |
954 | if (!node) { |
955 | status = AE_BAD_PARAMETER; | |
956 | goto unlock_and_exit; | |
957 | } | |
958 | ||
4be44fcd | 959 | status = acpi_ns_get_attached_data(node, handler, data); |
1da177e4 | 960 | |
4be44fcd LB |
961 | unlock_and_exit: |
962 | (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); | |
1da177e4 LT |
963 | return (status); |
964 | } | |
8313524a BM |
965 | |
966 | ACPI_EXPORT_SYMBOL(acpi_get_data) |