1 /*******************************************************************************
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 * ACPI Object evaluation interfaces
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2004, 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.
46 #include <acpi/acpi.h>
47 #include <acpi/acnamesp.h>
48 #include <acpi/acinterp.h>
51 #define _COMPONENT ACPI_NAMESPACE
52 ACPI_MODULE_NAME ("nsxfeval")
55 /*******************************************************************************
57 * FUNCTION: acpi_evaluate_object_typed
59 * PARAMETERS: Handle - Object handle (optional)
60 * *Pathname - Object pathname (optional)
61 * **external_params - List of parameters to pass to method,
62 * terminated by NULL. May be NULL
63 * if no parameters are being passed.
64 * *return_buffer - Where to put method's return value (if
65 * any). If NULL, no value is returned.
66 * return_type - Expected type of return object
70 * DESCRIPTION: Find and evaluate the given object, passing the given
71 * parameters if necessary. One of "Handle" or "Pathname" must
74 ******************************************************************************/
77 acpi_evaluate_object_typed (
80 struct acpi_object_list
*external_params
,
81 struct acpi_buffer
*return_buffer
,
82 acpi_object_type return_type
)
88 ACPI_FUNCTION_TRACE ("acpi_evaluate_object_typed");
91 /* Return buffer must be valid */
94 return_ACPI_STATUS (AE_BAD_PARAMETER
);
97 if (return_buffer
->length
== ACPI_ALLOCATE_BUFFER
) {
101 /* Evaluate the object */
103 status
= acpi_evaluate_object (handle
, pathname
, external_params
, return_buffer
);
104 if (ACPI_FAILURE (status
)) {
105 return_ACPI_STATUS (status
);
108 /* Type ANY means "don't care" */
110 if (return_type
== ACPI_TYPE_ANY
) {
111 return_ACPI_STATUS (AE_OK
);
114 if (return_buffer
->length
== 0) {
115 /* Error because caller specifically asked for a return value */
117 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
118 "No return value\n"));
120 return_ACPI_STATUS (AE_NULL_OBJECT
);
123 /* Examine the object type returned from evaluate_object */
125 if (((union acpi_object
*) return_buffer
->pointer
)->type
== return_type
) {
126 return_ACPI_STATUS (AE_OK
);
129 /* Return object type does not match requested type */
131 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
132 "Incorrect return type [%s] requested [%s]\n",
133 acpi_ut_get_type_name (((union acpi_object
*) return_buffer
->pointer
)->type
),
134 acpi_ut_get_type_name (return_type
)));
137 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
139 acpi_os_free (return_buffer
->pointer
);
140 return_buffer
->pointer
= NULL
;
143 return_buffer
->length
= 0;
144 return_ACPI_STATUS (AE_TYPE
);
148 /*******************************************************************************
150 * FUNCTION: acpi_evaluate_object
152 * PARAMETERS: Handle - Object handle (optional)
153 * Pathname - Object pathname (optional)
154 * external_params - List of parameters to pass to method,
155 * terminated by NULL. May be NULL
156 * if no parameters are being passed.
157 * return_buffer - Where to put method's return value (if
158 * any). If NULL, no value is returned.
162 * DESCRIPTION: Find and evaluate the given object, passing the given
163 * parameters if necessary. One of "Handle" or "Pathname" must
164 * be valid (non-null)
166 ******************************************************************************/
169 acpi_evaluate_object (
171 acpi_string pathname
,
172 struct acpi_object_list
*external_params
,
173 struct acpi_buffer
*return_buffer
)
177 struct acpi_parameter_info info
;
178 acpi_size buffer_space_needed
;
182 ACPI_FUNCTION_TRACE ("acpi_evaluate_object");
186 info
.parameters
= NULL
;
187 info
.return_object
= NULL
;
188 info
.parameter_type
= ACPI_PARAM_ARGS
;
191 * If there are parameters to be passed to the object
192 * (which must be a control method), the external objects
193 * must be converted to internal objects
195 if (external_params
&& external_params
->count
) {
197 * Allocate a new parameter block for the internal objects
198 * Add 1 to count to allow for null terminated internal list
200 info
.parameters
= ACPI_MEM_CALLOCATE (
201 ((acpi_size
) external_params
->count
+ 1) *
203 if (!info
.parameters
) {
204 return_ACPI_STATUS (AE_NO_MEMORY
);
208 * Convert each external object in the list to an
211 for (i
= 0; i
< external_params
->count
; i
++) {
212 status
= acpi_ut_copy_eobject_to_iobject (&external_params
->pointer
[i
],
213 &info
.parameters
[i
]);
214 if (ACPI_FAILURE (status
)) {
215 acpi_ut_delete_internal_object_list (info
.parameters
);
216 return_ACPI_STATUS (status
);
219 info
.parameters
[external_params
->count
] = NULL
;
225 * 1) Fully qualified pathname
226 * 2) No handle, not fully qualified pathname (error)
230 (acpi_ns_valid_root_prefix (pathname
[0]))) {
232 * The path is fully qualified, just evaluate by name
234 status
= acpi_ns_evaluate_by_name (pathname
, &info
);
238 * A handle is optional iff a fully qualified pathname
239 * is specified. Since we've already handled fully
240 * qualified names above, this is an error
243 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
244 "Both Handle and Pathname are NULL\n"));
247 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
248 "Handle is NULL and Pathname is relative\n"));
251 status
= AE_BAD_PARAMETER
;
255 * We get here if we have a handle -- and if we have a
256 * pathname it is relative. The handle will be validated
257 * in the lower procedures
261 * The null pathname case means the handle is for
262 * the actual object to be evaluated
264 status
= acpi_ns_evaluate_by_handle (&info
);
268 * Both a Handle and a relative Pathname
270 status
= acpi_ns_evaluate_relative (pathname
, &info
);
276 * If we are expecting a return value, and all went well above,
277 * copy the return value to an external object.
280 if (!info
.return_object
) {
281 return_buffer
->length
= 0;
284 if (ACPI_GET_DESCRIPTOR_TYPE (info
.return_object
) == ACPI_DESC_TYPE_NAMED
) {
286 * If we received a NS Node as a return object, this means that
287 * the object we are evaluating has nothing interesting to
288 * return (such as a mutex, etc.) We return an error because
289 * these types are essentially unsupported by this interface.
290 * We don't check up front because this makes it easier to add
291 * support for various types at a later date if necessary.
294 info
.return_object
= NULL
; /* No need to delete a NS Node */
295 return_buffer
->length
= 0;
298 if (ACPI_SUCCESS (status
)) {
300 * Find out how large a buffer is needed
301 * to contain the returned object
303 status
= acpi_ut_get_object_size (info
.return_object
,
304 &buffer_space_needed
);
305 if (ACPI_SUCCESS (status
)) {
306 /* Validate/Allocate/Clear caller buffer */
308 status
= acpi_ut_initialize_buffer (return_buffer
, buffer_space_needed
);
309 if (ACPI_FAILURE (status
)) {
311 * Caller's buffer is too small or a new one can't be allocated
313 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
314 "Needed buffer size %X, %s\n",
315 (u32
) buffer_space_needed
,
316 acpi_format_exception (status
)));
320 * We have enough space for the object, build it
322 status
= acpi_ut_copy_iobject_to_eobject (info
.return_object
,
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 ();
347 * Free the input parameter list (if we created one),
349 if (info
.parameters
) {
350 /* Free the allocated parameter block */
352 acpi_ut_delete_internal_object_list (info
.parameters
);
355 return_ACPI_STATUS (status
);
359 /*******************************************************************************
361 * FUNCTION: acpi_walk_namespace
363 * PARAMETERS: Type - acpi_object_type to search for
364 * start_object - Handle in namespace where search begins
365 * max_depth - Depth to which search is to reach
366 * user_function - Called when an object of "Type" is found
367 * Context - Passed to user function
368 * return_value - Location where return value of
369 * user_function is put if terminated early
371 * RETURNS Return value from the user_function if terminated early.
372 * Otherwise, returns NULL.
374 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
375 * starting (and ending) at the object specified by start_handle.
376 * The user_function is called whenever an object that matches
377 * the type parameter is found. If the user function returns
378 * a non-zero value, the search is terminated immediately and this
379 * value is returned to the caller.
381 * The point of this procedure is to provide a generic namespace
382 * walk routine that can be called from multiple places to
383 * provide multiple services; the User Function can be tailored
384 * to each task, whether it is a print function, a compare
387 ******************************************************************************/
390 acpi_walk_namespace (
391 acpi_object_type type
,
392 acpi_handle start_object
,
394 acpi_walk_callback user_function
,
401 ACPI_FUNCTION_TRACE ("acpi_walk_namespace");
404 /* Parameter validation */
406 if ((type
> ACPI_TYPE_EXTERNAL_MAX
) ||
409 return_ACPI_STATUS (AE_BAD_PARAMETER
);
413 * Lock the namespace around the walk.
414 * The namespace will be unlocked/locked around each call
415 * to the user function - since this function
416 * must be allowed to make Acpi calls itself.
418 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
419 if (ACPI_FAILURE (status
)) {
420 return_ACPI_STATUS (status
);
423 status
= acpi_ns_walk_namespace (type
, start_object
, max_depth
, ACPI_NS_WALK_UNLOCK
,
424 user_function
, context
, return_value
);
426 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
427 return_ACPI_STATUS (status
);
431 /*******************************************************************************
433 * FUNCTION: acpi_ns_get_device_callback
435 * PARAMETERS: Callback from acpi_get_device
439 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
440 * present devices, or if they specified a HID, it filters based
443 ******************************************************************************/
446 acpi_ns_get_device_callback (
447 acpi_handle obj_handle
,
452 struct acpi_get_devices_info
*info
= context
;
454 struct acpi_namespace_node
*node
;
456 struct acpi_device_id hid
;
457 struct acpi_compatible_id_list
*cid
;
461 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
462 if (ACPI_FAILURE (status
)) {
466 node
= acpi_ns_map_handle_to_node (obj_handle
);
467 status
= acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
468 if (ACPI_FAILURE (status
)) {
473 return (AE_BAD_PARAMETER
);
476 /* Run _STA to determine if device is present */
478 status
= acpi_ut_execute_STA (node
, &flags
);
479 if (ACPI_FAILURE (status
)) {
480 return (AE_CTRL_DEPTH
);
483 if (!(flags
& 0x01)) {
484 /* Don't return at the device or children of the device if not there */
486 return (AE_CTRL_DEPTH
);
489 /* Filter based on device HID & CID */
491 if (info
->hid
!= NULL
) {
492 status
= acpi_ut_execute_HID (node
, &hid
);
493 if (status
== AE_NOT_FOUND
) {
496 else if (ACPI_FAILURE (status
)) {
497 return (AE_CTRL_DEPTH
);
500 if (ACPI_STRNCMP (hid
.value
, info
->hid
, sizeof (hid
.value
)) != 0) {
501 /* Get the list of Compatible IDs */
503 status
= acpi_ut_execute_CID (node
, &cid
);
504 if (status
== AE_NOT_FOUND
) {
507 else if (ACPI_FAILURE (status
)) {
508 return (AE_CTRL_DEPTH
);
511 /* Walk the CID list */
513 for (i
= 0; i
< cid
->count
; i
++) {
514 if (ACPI_STRNCMP (cid
->id
[i
].value
, info
->hid
,
515 sizeof (struct acpi_compatible_id
)) != 0) {
524 status
= info
->user_function (obj_handle
, nesting_level
, info
->context
, return_value
);
529 /*******************************************************************************
531 * FUNCTION: acpi_get_devices
533 * PARAMETERS: HID - HID to search for. Can be NULL.
534 * user_function - Called when a matching object is found
535 * Context - Passed to user function
536 * return_value - Location where return value of
537 * user_function is put if terminated early
539 * RETURNS Return value from the user_function if terminated early.
540 * Otherwise, returns NULL.
542 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
543 * starting (and ending) at the object specified by start_handle.
544 * The user_function is called whenever an object of type
545 * Device is found. If the user function returns
546 * a non-zero value, the search is terminated immediately and this
547 * value is returned to the caller.
549 * This is a wrapper for walk_namespace, but the callback performs
550 * additional filtering. Please see acpi_get_device_callback.
552 ******************************************************************************/
557 acpi_walk_callback user_function
,
562 struct acpi_get_devices_info info
;
565 ACPI_FUNCTION_TRACE ("acpi_get_devices");
568 /* Parameter validation */
570 if (!user_function
) {
571 return_ACPI_STATUS (AE_BAD_PARAMETER
);
575 * We're going to call their callback from OUR callback, so we need
576 * to know what it is, and their context parameter.
578 info
.context
= context
;
579 info
.user_function
= user_function
;
583 * Lock the namespace around the walk.
584 * The namespace will be unlocked/locked around each call
585 * to the user function - since this function
586 * must be allowed to make Acpi calls itself.
588 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
589 if (ACPI_FAILURE (status
)) {
590 return_ACPI_STATUS (status
);
593 status
= acpi_ns_walk_namespace (ACPI_TYPE_DEVICE
,
594 ACPI_ROOT_OBJECT
, ACPI_UINT32_MAX
,
596 acpi_ns_get_device_callback
, &info
,
599 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
600 return_ACPI_STATUS (status
);
604 /*******************************************************************************
606 * FUNCTION: acpi_attach_data
608 * PARAMETERS: obj_handle - Namespace node
609 * Handler - Handler for this attachment
610 * Data - Pointer to data to be attached
614 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
616 ******************************************************************************/
620 acpi_handle obj_handle
,
621 acpi_object_handler handler
,
624 struct acpi_namespace_node
*node
;
628 /* Parameter validation */
633 return (AE_BAD_PARAMETER
);
636 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
637 if (ACPI_FAILURE (status
)) {
641 /* Convert and validate the handle */
643 node
= acpi_ns_map_handle_to_node (obj_handle
);
645 status
= AE_BAD_PARAMETER
;
646 goto unlock_and_exit
;
649 status
= acpi_ns_attach_data (node
, handler
, data
);
652 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
657 /*******************************************************************************
659 * FUNCTION: acpi_detach_data
661 * PARAMETERS: obj_handle - Namespace node handle
662 * Handler - Handler used in call to acpi_attach_data
666 * DESCRIPTION: Remove data that was previously attached to a node.
668 ******************************************************************************/
672 acpi_handle obj_handle
,
673 acpi_object_handler handler
)
675 struct acpi_namespace_node
*node
;
679 /* Parameter validation */
683 return (AE_BAD_PARAMETER
);
686 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
687 if (ACPI_FAILURE (status
)) {
691 /* Convert and validate the handle */
693 node
= acpi_ns_map_handle_to_node (obj_handle
);
695 status
= AE_BAD_PARAMETER
;
696 goto unlock_and_exit
;
699 status
= acpi_ns_detach_data (node
, handler
);
702 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
707 /*******************************************************************************
709 * FUNCTION: acpi_get_data
711 * PARAMETERS: obj_handle - Namespace node
712 * Handler - Handler used in call to attach_data
713 * Data - Where the data is returned
717 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
719 ******************************************************************************/
723 acpi_handle obj_handle
,
724 acpi_object_handler handler
,
727 struct acpi_namespace_node
*node
;
731 /* Parameter validation */
736 return (AE_BAD_PARAMETER
);
739 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
740 if (ACPI_FAILURE (status
)) {
744 /* Convert and validate the handle */
746 node
= acpi_ns_map_handle_to_node (obj_handle
);
748 status
= AE_BAD_PARAMETER
;
749 goto unlock_and_exit
;
752 status
= acpi_ns_get_attached_data (node
, handler
, data
);
755 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);