1 /*******************************************************************************
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 * ACPI Object evaluation interfaces
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2006, R. Byron Moore
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
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.
45 #include <acpi/acpi.h>
46 #include <acpi/acnamesp.h>
47 #include <acpi/acinterp.h>
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsxfeval")
52 #ifdef ACPI_FUTURE_USAGE
53 /*******************************************************************************
55 * FUNCTION: acpi_evaluate_object_typed
57 * PARAMETERS: Handle - Object handle (optional)
58 * Pathname - Object pathname (optional)
59 * external_params - List of parameters to pass to method,
60 * terminated by NULL. May be NULL
61 * if no parameters are being passed.
62 * return_buffer - Where to put method's return value (if
63 * any). If NULL, no value is returned.
64 * return_type - Expected type of return object
68 * DESCRIPTION: Find and evaluate the given object, passing the given
69 * parameters if necessary. One of "Handle" or "Pathname" must
72 ******************************************************************************/
74 acpi_evaluate_object_typed(acpi_handle handle
,
76 struct acpi_object_list
* external_params
,
77 struct acpi_buffer
* return_buffer
,
78 acpi_object_type return_type
)
83 ACPI_FUNCTION_TRACE(acpi_evaluate_object_typed
);
85 /* Return buffer must be valid */
88 return_ACPI_STATUS(AE_BAD_PARAMETER
);
91 if (return_buffer
->length
== ACPI_ALLOCATE_BUFFER
) {
95 /* Evaluate the object */
98 acpi_evaluate_object(handle
, pathname
, external_params
,
100 if (ACPI_FAILURE(status
)) {
101 return_ACPI_STATUS(status
);
104 /* Type ANY means "don't care" */
106 if (return_type
== ACPI_TYPE_ANY
) {
107 return_ACPI_STATUS(AE_OK
);
110 if (return_buffer
->length
== 0) {
112 /* Error because caller specifically asked for a return value */
114 ACPI_ERROR((AE_INFO
, "No return value"));
115 return_ACPI_STATUS(AE_NULL_OBJECT
);
118 /* Examine the object type returned from evaluate_object */
120 if (((union acpi_object
*)return_buffer
->pointer
)->type
== return_type
) {
121 return_ACPI_STATUS(AE_OK
);
124 /* Return object type does not match requested type */
127 "Incorrect return type [%s] requested [%s]",
128 acpi_ut_get_type_name(((union acpi_object
*)return_buffer
->
130 acpi_ut_get_type_name(return_type
)));
134 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
136 acpi_os_free(return_buffer
->pointer
);
137 return_buffer
->pointer
= NULL
;
140 return_buffer
->length
= 0;
141 return_ACPI_STATUS(AE_TYPE
);
144 ACPI_EXPORT_SYMBOL(acpi_evaluate_object_typed
)
145 #endif /* ACPI_FUTURE_USAGE */
147 /*******************************************************************************
149 * FUNCTION: acpi_evaluate_object
151 * PARAMETERS: Handle - Object handle (optional)
152 * Pathname - Object pathname (optional)
153 * external_params - List of parameters to pass to method,
154 * terminated by NULL. May be NULL
155 * if no parameters are being passed.
156 * return_buffer - Where to put method's return value (if
157 * any). If NULL, no value is returned.
161 * DESCRIPTION: Find and evaluate the given object, passing the given
162 * parameters if necessary. One of "Handle" or "Pathname" must
163 * be valid (non-null)
165 ******************************************************************************/
167 acpi_evaluate_object(acpi_handle handle
,
168 acpi_string pathname
,
169 struct acpi_object_list
*external_params
,
170 struct acpi_buffer
*return_buffer
)
174 struct acpi_parameter_info info
;
175 acpi_size buffer_space_needed
;
178 ACPI_FUNCTION_TRACE(acpi_evaluate_object
);
181 info
.parameters
= NULL
;
182 info
.return_object
= NULL
;
183 info
.parameter_type
= ACPI_PARAM_ARGS
;
186 * If there are parameters to be passed to the object
187 * (which must be a control method), the external objects
188 * must be converted to internal objects
190 if (external_params
&& external_params
->count
) {
192 * Allocate a new parameter block for the internal objects
193 * Add 1 to count to allow for null terminated internal list
195 info
.parameters
= ACPI_ALLOCATE_ZEROED(((acpi_size
)
196 external_params
->count
+
197 1) * sizeof(void *));
198 if (!info
.parameters
) {
199 return_ACPI_STATUS(AE_NO_MEMORY
);
203 * Convert each external object in the list to an
206 for (i
= 0; i
< external_params
->count
; i
++) {
208 acpi_ut_copy_eobject_to_iobject(&external_params
->
212 if (ACPI_FAILURE(status
)) {
213 acpi_ut_delete_internal_object_list(info
.
215 return_ACPI_STATUS(status
);
218 info
.parameters
[external_params
->count
] = NULL
;
223 * 1) Fully qualified pathname
224 * 2) No handle, not fully qualified pathname (error)
227 if ((pathname
) && (acpi_ns_valid_root_prefix(pathname
[0]))) {
229 /* The path is fully qualified, just evaluate by name */
231 status
= acpi_ns_evaluate_by_name(pathname
, &info
);
232 } else if (!handle
) {
234 * A handle is optional iff a fully qualified pathname
235 * is specified. Since we've already handled fully
236 * qualified names above, this is an error
239 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
240 "Both Handle and Pathname are NULL"));
242 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
243 "Null Handle with relative pathname [%s]",
247 status
= AE_BAD_PARAMETER
;
250 * We get here if we have a handle -- and if we have a
251 * pathname it is relative. The handle will be validated
252 * in the lower procedures
256 * The null pathname case means the handle is for
257 * the actual object to be evaluated
259 status
= acpi_ns_evaluate_by_handle(&info
);
261 /* Both a Handle and a relative Pathname */
263 status
= acpi_ns_evaluate_relative(pathname
, &info
);
268 * If we are expecting a return value, and all went well above,
269 * copy the return value to an external object.
272 if (!info
.return_object
) {
273 return_buffer
->length
= 0;
275 if (ACPI_GET_DESCRIPTOR_TYPE(info
.return_object
) ==
276 ACPI_DESC_TYPE_NAMED
) {
278 * If we received a NS Node as a return object, this means that
279 * the object we are evaluating has nothing interesting to
280 * return (such as a mutex, etc.) We return an error because
281 * these types are essentially unsupported by this interface.
282 * We don't check up front because this makes it easier to add
283 * support for various types at a later date if necessary.
286 info
.return_object
= NULL
; /* No need to delete a NS Node */
287 return_buffer
->length
= 0;
290 if (ACPI_SUCCESS(status
)) {
292 * Find out how large a buffer is needed
293 * to contain the returned object
296 acpi_ut_get_object_size(info
.return_object
,
297 &buffer_space_needed
);
298 if (ACPI_SUCCESS(status
)) {
300 /* Validate/Allocate/Clear caller buffer */
303 acpi_ut_initialize_buffer
305 buffer_space_needed
);
306 if (ACPI_FAILURE(status
)) {
308 * Caller's buffer is too small or a new one can't
311 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
312 "Needed buffer size %X, %s\n",
315 acpi_format_exception
318 /* We have enough space for the object, build it */
321 acpi_ut_copy_iobject_to_eobject
330 if (info
.return_object
) {
332 * Delete the internal return object. NOTE: Interpreter
333 * must be locked to avoid race condition.
335 status2
= acpi_ex_enter_interpreter();
336 if (ACPI_SUCCESS(status2
)) {
338 * Delete the internal return object. (Or at least
339 * decrement the reference count by one)
341 acpi_ut_remove_reference(info
.return_object
);
342 acpi_ex_exit_interpreter();
346 /* Free the input parameter list (if we created one) */
348 if (info
.parameters
) {
350 /* Free the allocated parameter block */
352 acpi_ut_delete_internal_object_list(info
.parameters
);
355 return_ACPI_STATUS(status
);
358 ACPI_EXPORT_SYMBOL(acpi_evaluate_object
)
360 /*******************************************************************************
362 * FUNCTION: acpi_walk_namespace
364 * PARAMETERS: Type - acpi_object_type to search for
365 * start_object - Handle in namespace where search begins
366 * max_depth - Depth to which search is to reach
367 * user_function - Called when an object of "Type" is found
368 * Context - Passed to user function
369 * return_value - Location where return value of
370 * user_function is put if terminated early
372 * RETURNS Return value from the user_function if terminated early.
373 * Otherwise, returns NULL.
375 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
376 * starting (and ending) at the object specified by start_handle.
377 * The user_function is called whenever an object that matches
378 * the type parameter is found. If the user function returns
379 * a non-zero value, the search is terminated immediately and this
380 * value is returned to the caller.
382 * The point of this procedure is to provide a generic namespace
383 * walk routine that can be called from multiple places to
384 * provide multiple services; the User Function can be tailored
385 * to each task, whether it is a print function, a compare
388 ******************************************************************************/
390 acpi_walk_namespace(acpi_object_type type
,
391 acpi_handle start_object
,
393 acpi_walk_callback user_function
,
394 void *context
, void **return_value
)
398 ACPI_FUNCTION_TRACE(acpi_walk_namespace
);
400 /* Parameter validation */
402 if ((type
> ACPI_TYPE_LOCAL_MAX
) || (!max_depth
) || (!user_function
)) {
403 return_ACPI_STATUS(AE_BAD_PARAMETER
);
407 * Lock the namespace around the walk.
408 * The namespace will be unlocked/locked around each call
409 * to the user function - since this function
410 * must be allowed to make Acpi calls itself.
412 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
413 if (ACPI_FAILURE(status
)) {
414 return_ACPI_STATUS(status
);
417 status
= acpi_ns_walk_namespace(type
, start_object
, max_depth
,
419 user_function
, context
, return_value
);
421 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
422 return_ACPI_STATUS(status
);
425 ACPI_EXPORT_SYMBOL(acpi_walk_namespace
)
427 /*******************************************************************************
429 * FUNCTION: acpi_ns_get_device_callback
431 * PARAMETERS: Callback from acpi_get_device
435 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
436 * present devices, or if they specified a HID, it filters based
439 ******************************************************************************/
441 acpi_ns_get_device_callback(acpi_handle obj_handle
,
443 void *context
, void **return_value
)
445 struct acpi_get_devices_info
*info
= context
;
447 struct acpi_namespace_node
*node
;
449 struct acpi_device_id hid
;
450 struct acpi_compatible_id_list
*cid
;
453 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
454 if (ACPI_FAILURE(status
)) {
458 node
= acpi_ns_map_handle_to_node(obj_handle
);
459 status
= acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
460 if (ACPI_FAILURE(status
)) {
465 return (AE_BAD_PARAMETER
);
468 /* Run _STA to determine if device is present */
470 status
= acpi_ut_execute_STA(node
, &flags
);
471 if (ACPI_FAILURE(status
)) {
472 return (AE_CTRL_DEPTH
);
475 if (!(flags
& ACPI_STA_DEVICE_PRESENT
)) {
477 /* Don't examine children of the device if not present */
479 return (AE_CTRL_DEPTH
);
482 /* Filter based on device HID & CID */
484 if (info
->hid
!= NULL
) {
485 status
= acpi_ut_execute_HID(node
, &hid
);
486 if (status
== AE_NOT_FOUND
) {
488 } else if (ACPI_FAILURE(status
)) {
489 return (AE_CTRL_DEPTH
);
492 if (ACPI_STRNCMP(hid
.value
, info
->hid
, sizeof(hid
.value
)) != 0) {
494 /* Get the list of Compatible IDs */
496 status
= acpi_ut_execute_CID(node
, &cid
);
497 if (status
== AE_NOT_FOUND
) {
499 } else if (ACPI_FAILURE(status
)) {
500 return (AE_CTRL_DEPTH
);
503 /* Walk the CID list */
505 for (i
= 0; i
< cid
->count
; i
++) {
506 if (ACPI_STRNCMP(cid
->id
[i
].value
, info
->hid
,
508 acpi_compatible_id
)) !=
518 status
= info
->user_function(obj_handle
, nesting_level
, info
->context
,
523 /*******************************************************************************
525 * FUNCTION: acpi_get_devices
527 * PARAMETERS: HID - HID to search for. Can be NULL.
528 * user_function - Called when a matching object is found
529 * Context - Passed to user function
530 * return_value - Location where return value of
531 * user_function is put if terminated early
533 * RETURNS Return value from the user_function if terminated early.
534 * Otherwise, returns NULL.
536 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
537 * starting (and ending) at the object specified by start_handle.
538 * The user_function is called whenever an object of type
539 * Device is found. If the user function returns
540 * a non-zero value, the search is terminated immediately and this
541 * value is returned to the caller.
543 * This is a wrapper for walk_namespace, but the callback performs
544 * additional filtering. Please see acpi_get_device_callback.
546 ******************************************************************************/
549 acpi_get_devices(char *HID
,
550 acpi_walk_callback user_function
,
551 void *context
, void **return_value
)
554 struct acpi_get_devices_info info
;
556 ACPI_FUNCTION_TRACE(acpi_get_devices
);
558 /* Parameter validation */
560 if (!user_function
) {
561 return_ACPI_STATUS(AE_BAD_PARAMETER
);
565 * We're going to call their callback from OUR callback, so we need
566 * to know what it is, and their context parameter.
569 info
.context
= context
;
570 info
.user_function
= user_function
;
573 * Lock the namespace around the walk.
574 * The namespace will be unlocked/locked around each call
575 * to the user function - since this function
576 * must be allowed to make Acpi calls itself.
578 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
579 if (ACPI_FAILURE(status
)) {
580 return_ACPI_STATUS(status
);
583 status
= acpi_ns_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
584 ACPI_UINT32_MAX
, ACPI_NS_WALK_UNLOCK
,
585 acpi_ns_get_device_callback
, &info
,
588 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
589 return_ACPI_STATUS(status
);
592 ACPI_EXPORT_SYMBOL(acpi_get_devices
)
594 /*******************************************************************************
596 * FUNCTION: acpi_attach_data
598 * PARAMETERS: obj_handle - Namespace node
599 * Handler - Handler for this attachment
600 * Data - Pointer to data to be attached
604 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
606 ******************************************************************************/
608 acpi_attach_data(acpi_handle obj_handle
,
609 acpi_object_handler handler
, void *data
)
611 struct acpi_namespace_node
*node
;
614 /* Parameter validation */
616 if (!obj_handle
|| !handler
|| !data
) {
617 return (AE_BAD_PARAMETER
);
620 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
621 if (ACPI_FAILURE(status
)) {
625 /* Convert and validate the handle */
627 node
= acpi_ns_map_handle_to_node(obj_handle
);
629 status
= AE_BAD_PARAMETER
;
630 goto unlock_and_exit
;
633 status
= acpi_ns_attach_data(node
, handler
, data
);
636 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
640 ACPI_EXPORT_SYMBOL(acpi_attach_data
)
642 /*******************************************************************************
644 * FUNCTION: acpi_detach_data
646 * PARAMETERS: obj_handle - Namespace node handle
647 * Handler - Handler used in call to acpi_attach_data
651 * DESCRIPTION: Remove data that was previously attached to a node.
653 ******************************************************************************/
655 acpi_detach_data(acpi_handle obj_handle
, acpi_object_handler handler
)
657 struct acpi_namespace_node
*node
;
660 /* Parameter validation */
662 if (!obj_handle
|| !handler
) {
663 return (AE_BAD_PARAMETER
);
666 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
667 if (ACPI_FAILURE(status
)) {
671 /* Convert and validate the handle */
673 node
= acpi_ns_map_handle_to_node(obj_handle
);
675 status
= AE_BAD_PARAMETER
;
676 goto unlock_and_exit
;
679 status
= acpi_ns_detach_data(node
, handler
);
682 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
686 ACPI_EXPORT_SYMBOL(acpi_detach_data
)
688 /*******************************************************************************
690 * FUNCTION: acpi_get_data
692 * PARAMETERS: obj_handle - Namespace node
693 * Handler - Handler used in call to attach_data
694 * Data - Where the data is returned
698 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
700 ******************************************************************************/
702 acpi_get_data(acpi_handle obj_handle
, acpi_object_handler handler
, void **data
)
704 struct acpi_namespace_node
*node
;
707 /* Parameter validation */
709 if (!obj_handle
|| !handler
|| !data
) {
710 return (AE_BAD_PARAMETER
);
713 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
714 if (ACPI_FAILURE(status
)) {
718 /* Convert and validate the handle */
720 node
= acpi_ns_map_handle_to_node(obj_handle
);
722 status
= AE_BAD_PARAMETER
;
723 goto unlock_and_exit
;
726 status
= acpi_ns_get_attached_data(node
, handler
, data
);
729 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
733 ACPI_EXPORT_SYMBOL(acpi_get_data
)