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