1 /*******************************************************************************
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 * ACPI Object evaluation interfaces
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2005, 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 <linux/module.h>
47 #include <acpi/acpi.h>
48 #include <acpi/acnamesp.h>
49 #include <acpi/acinterp.h>
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME ("nsxfeval")
56 /*******************************************************************************
58 * FUNCTION: acpi_evaluate_object_typed
60 * PARAMETERS: Handle - Object handle (optional)
61 * *Pathname - Object pathname (optional)
62 * **external_params - List of parameters to pass to method,
63 * terminated by NULL. May be NULL
64 * if no parameters are being passed.
65 * *return_buffer - Where to put method's return value (if
66 * any). If NULL, no value is returned.
67 * return_type - Expected type of return object
71 * DESCRIPTION: Find and evaluate the given object, passing the given
72 * parameters if necessary. One of "Handle" or "Pathname" must
75 ******************************************************************************/
76 #ifdef ACPI_FUTURE_USAGE
78 acpi_evaluate_object_typed (
81 struct acpi_object_list
*external_params
,
82 struct acpi_buffer
*return_buffer
,
83 acpi_object_type return_type
)
89 ACPI_FUNCTION_TRACE ("acpi_evaluate_object_typed");
92 /* Return buffer must be valid */
95 return_ACPI_STATUS (AE_BAD_PARAMETER
);
98 if (return_buffer
->length
== ACPI_ALLOCATE_BUFFER
) {
102 /* Evaluate the object */
104 status
= acpi_evaluate_object (handle
, pathname
, external_params
, return_buffer
);
105 if (ACPI_FAILURE (status
)) {
106 return_ACPI_STATUS (status
);
109 /* Type ANY means "don't care" */
111 if (return_type
== ACPI_TYPE_ANY
) {
112 return_ACPI_STATUS (AE_OK
);
115 if (return_buffer
->length
== 0) {
116 /* Error because caller specifically asked for a return value */
118 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
119 "No return value\n"));
121 return_ACPI_STATUS (AE_NULL_OBJECT
);
124 /* Examine the object type returned from evaluate_object */
126 if (((union acpi_object
*) return_buffer
->pointer
)->type
== return_type
) {
127 return_ACPI_STATUS (AE_OK
);
130 /* Return object type does not match requested type */
132 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
133 "Incorrect return type [%s] requested [%s]\n",
134 acpi_ut_get_type_name (((union acpi_object
*) return_buffer
->pointer
)->type
),
135 acpi_ut_get_type_name (return_type
)));
138 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
140 acpi_os_free (return_buffer
->pointer
);
141 return_buffer
->pointer
= NULL
;
144 return_buffer
->length
= 0;
145 return_ACPI_STATUS (AE_TYPE
);
147 #endif /* ACPI_FUTURE_USAGE */
150 /*******************************************************************************
152 * FUNCTION: acpi_evaluate_object
154 * PARAMETERS: Handle - Object handle (optional)
155 * Pathname - Object pathname (optional)
156 * external_params - List of parameters to pass to method,
157 * terminated by NULL. May be NULL
158 * if no parameters are being passed.
159 * return_buffer - Where to put method's return value (if
160 * any). If NULL, no value is returned.
164 * DESCRIPTION: Find and evaluate the given object, passing the given
165 * parameters if necessary. One of "Handle" or "Pathname" must
166 * be valid (non-null)
168 ******************************************************************************/
171 acpi_evaluate_object (
173 acpi_string pathname
,
174 struct acpi_object_list
*external_params
,
175 struct acpi_buffer
*return_buffer
)
179 struct acpi_parameter_info info
;
180 acpi_size buffer_space_needed
;
184 ACPI_FUNCTION_TRACE ("acpi_evaluate_object");
188 info
.parameters
= NULL
;
189 info
.return_object
= NULL
;
190 info
.parameter_type
= ACPI_PARAM_ARGS
;
193 * If there are parameters to be passed to the object
194 * (which must be a control method), the external objects
195 * must be converted to internal objects
197 if (external_params
&& external_params
->count
) {
199 * Allocate a new parameter block for the internal objects
200 * Add 1 to count to allow for null terminated internal list
202 info
.parameters
= ACPI_MEM_CALLOCATE (
203 ((acpi_size
) external_params
->count
+ 1) *
205 if (!info
.parameters
) {
206 return_ACPI_STATUS (AE_NO_MEMORY
);
210 * Convert each external object in the list to an
213 for (i
= 0; i
< external_params
->count
; i
++) {
214 status
= acpi_ut_copy_eobject_to_iobject (&external_params
->pointer
[i
],
215 &info
.parameters
[i
]);
216 if (ACPI_FAILURE (status
)) {
217 acpi_ut_delete_internal_object_list (info
.parameters
);
218 return_ACPI_STATUS (status
);
221 info
.parameters
[external_params
->count
] = NULL
;
227 * 1) Fully qualified pathname
228 * 2) No handle, not fully qualified pathname (error)
232 (acpi_ns_valid_root_prefix (pathname
[0]))) {
234 * The path is fully qualified, just evaluate by name
236 status
= acpi_ns_evaluate_by_name (pathname
, &info
);
240 * A handle is optional iff a fully qualified pathname
241 * is specified. Since we've already handled fully
242 * qualified names above, this is an error
245 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
246 "Both Handle and Pathname are NULL\n"));
249 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
250 "Handle is NULL and Pathname is relative\n"));
253 status
= AE_BAD_PARAMETER
;
257 * We get here if we have a handle -- and if we have a
258 * pathname it is relative. The handle will be validated
259 * in the lower procedures
263 * The null pathname case means the handle is for
264 * the actual object to be evaluated
266 status
= acpi_ns_evaluate_by_handle (&info
);
270 * Both a Handle and a relative Pathname
272 status
= acpi_ns_evaluate_relative (pathname
, &info
);
278 * If we are expecting a return value, and all went well above,
279 * copy the return value to an external object.
282 if (!info
.return_object
) {
283 return_buffer
->length
= 0;
286 if (ACPI_GET_DESCRIPTOR_TYPE (info
.return_object
) == ACPI_DESC_TYPE_NAMED
) {
288 * If we received a NS Node as a return object, this means that
289 * the object we are evaluating has nothing interesting to
290 * return (such as a mutex, etc.) We return an error because
291 * these types are essentially unsupported by this interface.
292 * We don't check up front because this makes it easier to add
293 * support for various types at a later date if necessary.
296 info
.return_object
= NULL
; /* No need to delete a NS Node */
297 return_buffer
->length
= 0;
300 if (ACPI_SUCCESS (status
)) {
302 * Find out how large a buffer is needed
303 * to contain the returned object
305 status
= acpi_ut_get_object_size (info
.return_object
,
306 &buffer_space_needed
);
307 if (ACPI_SUCCESS (status
)) {
308 /* Validate/Allocate/Clear caller buffer */
310 status
= acpi_ut_initialize_buffer (return_buffer
, buffer_space_needed
);
311 if (ACPI_FAILURE (status
)) {
313 * Caller's buffer is too small or a new one can't be allocated
315 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
316 "Needed buffer size %X, %s\n",
317 (u32
) buffer_space_needed
,
318 acpi_format_exception (status
)));
322 * We have enough space for the object, build it
324 status
= acpi_ut_copy_iobject_to_eobject (info
.return_object
,
332 if (info
.return_object
) {
334 * Delete the internal return object. NOTE: Interpreter
335 * must be locked to avoid race condition.
337 status2
= acpi_ex_enter_interpreter ();
338 if (ACPI_SUCCESS (status2
)) {
340 * Delete the internal return object. (Or at least
341 * decrement the reference count by one)
343 acpi_ut_remove_reference (info
.return_object
);
344 acpi_ex_exit_interpreter ();
349 * Free the input parameter list (if we created one),
351 if (info
.parameters
) {
352 /* Free the allocated parameter block */
354 acpi_ut_delete_internal_object_list (info
.parameters
);
357 return_ACPI_STATUS (status
);
359 EXPORT_SYMBOL(acpi_evaluate_object
);
362 /*******************************************************************************
364 * FUNCTION: acpi_walk_namespace
366 * PARAMETERS: Type - acpi_object_type to search for
367 * start_object - Handle in namespace where search begins
368 * max_depth - Depth to which search is to reach
369 * user_function - Called when an object of "Type" is found
370 * Context - Passed to user function
371 * return_value - Location where return value of
372 * user_function is put if terminated early
374 * RETURNS Return value from the user_function if terminated early.
375 * Otherwise, returns NULL.
377 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
378 * starting (and ending) at the object specified by start_handle.
379 * The user_function is called whenever an object that matches
380 * the type parameter is found. If the user function returns
381 * a non-zero value, the search is terminated immediately and this
382 * value is returned to the caller.
384 * The point of this procedure is to provide a generic namespace
385 * walk routine that can be called from multiple places to
386 * provide multiple services; the User Function can be tailored
387 * to each task, whether it is a print function, a compare
390 ******************************************************************************/
393 acpi_walk_namespace (
394 acpi_object_type type
,
395 acpi_handle start_object
,
397 acpi_walk_callback user_function
,
404 ACPI_FUNCTION_TRACE ("acpi_walk_namespace");
407 /* Parameter validation */
409 if ((type
> ACPI_TYPE_EXTERNAL_MAX
) ||
412 return_ACPI_STATUS (AE_BAD_PARAMETER
);
416 * Lock the namespace around the walk.
417 * The namespace will be unlocked/locked around each call
418 * to the user function - since this function
419 * must be allowed to make Acpi calls itself.
421 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
422 if (ACPI_FAILURE (status
)) {
423 return_ACPI_STATUS (status
);
426 status
= acpi_ns_walk_namespace (type
, start_object
, max_depth
, ACPI_NS_WALK_UNLOCK
,
427 user_function
, context
, return_value
);
429 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
430 return_ACPI_STATUS (status
);
432 EXPORT_SYMBOL(acpi_walk_namespace
);
435 /*******************************************************************************
437 * FUNCTION: acpi_ns_get_device_callback
439 * PARAMETERS: Callback from acpi_get_device
443 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
444 * present devices, or if they specified a HID, it filters based
447 ******************************************************************************/
450 acpi_ns_get_device_callback (
451 acpi_handle obj_handle
,
456 struct acpi_get_devices_info
*info
= context
;
458 struct acpi_namespace_node
*node
;
460 struct acpi_device_id hid
;
461 struct acpi_compatible_id_list
*cid
;
465 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
466 if (ACPI_FAILURE (status
)) {
470 node
= acpi_ns_map_handle_to_node (obj_handle
);
471 status
= acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
472 if (ACPI_FAILURE (status
)) {
477 return (AE_BAD_PARAMETER
);
480 /* Run _STA to determine if device is present */
482 status
= acpi_ut_execute_STA (node
, &flags
);
483 if (ACPI_FAILURE (status
)) {
484 return (AE_CTRL_DEPTH
);
487 if (!(flags
& 0x01)) {
488 /* Don't return at the device or children of the device if not there */
490 return (AE_CTRL_DEPTH
);
493 /* Filter based on device HID & CID */
495 if (info
->hid
!= NULL
) {
496 status
= acpi_ut_execute_HID (node
, &hid
);
497 if (status
== AE_NOT_FOUND
) {
500 else if (ACPI_FAILURE (status
)) {
501 return (AE_CTRL_DEPTH
);
504 if (ACPI_STRNCMP (hid
.value
, info
->hid
, sizeof (hid
.value
)) != 0) {
505 /* Get the list of Compatible IDs */
507 status
= acpi_ut_execute_CID (node
, &cid
);
508 if (status
== AE_NOT_FOUND
) {
511 else if (ACPI_FAILURE (status
)) {
512 return (AE_CTRL_DEPTH
);
515 /* Walk the CID list */
517 for (i
= 0; i
< cid
->count
; i
++) {
518 if (ACPI_STRNCMP (cid
->id
[i
].value
, info
->hid
,
519 sizeof (struct acpi_compatible_id
)) != 0) {
528 status
= info
->user_function (obj_handle
, nesting_level
, info
->context
, return_value
);
533 /*******************************************************************************
535 * FUNCTION: acpi_get_devices
537 * PARAMETERS: HID - HID to search for. Can be NULL.
538 * user_function - Called when a matching object is found
539 * Context - Passed to user function
540 * return_value - Location where return value of
541 * user_function is put if terminated early
543 * RETURNS Return value from the user_function if terminated early.
544 * Otherwise, returns NULL.
546 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
547 * starting (and ending) at the object specified by start_handle.
548 * The user_function is called whenever an object of type
549 * Device is found. If the user function returns
550 * a non-zero value, the search is terminated immediately and this
551 * value is returned to the caller.
553 * This is a wrapper for walk_namespace, but the callback performs
554 * additional filtering. Please see acpi_get_device_callback.
556 ******************************************************************************/
561 acpi_walk_callback user_function
,
566 struct acpi_get_devices_info info
;
569 ACPI_FUNCTION_TRACE ("acpi_get_devices");
572 /* Parameter validation */
574 if (!user_function
) {
575 return_ACPI_STATUS (AE_BAD_PARAMETER
);
579 * We're going to call their callback from OUR callback, so we need
580 * to know what it is, and their context parameter.
582 info
.context
= context
;
583 info
.user_function
= user_function
;
587 * Lock the namespace around the walk.
588 * The namespace will be unlocked/locked around each call
589 * to the user function - since this function
590 * must be allowed to make Acpi calls itself.
592 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
593 if (ACPI_FAILURE (status
)) {
594 return_ACPI_STATUS (status
);
597 status
= acpi_ns_walk_namespace (ACPI_TYPE_DEVICE
,
598 ACPI_ROOT_OBJECT
, ACPI_UINT32_MAX
,
600 acpi_ns_get_device_callback
, &info
,
603 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
604 return_ACPI_STATUS (status
);
606 EXPORT_SYMBOL(acpi_get_devices
);
609 /*******************************************************************************
611 * FUNCTION: acpi_attach_data
613 * PARAMETERS: obj_handle - Namespace node
614 * Handler - Handler for this attachment
615 * Data - Pointer to data to be attached
619 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
621 ******************************************************************************/
625 acpi_handle obj_handle
,
626 acpi_object_handler handler
,
629 struct acpi_namespace_node
*node
;
633 /* Parameter validation */
638 return (AE_BAD_PARAMETER
);
641 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
642 if (ACPI_FAILURE (status
)) {
646 /* Convert and validate the handle */
648 node
= acpi_ns_map_handle_to_node (obj_handle
);
650 status
= AE_BAD_PARAMETER
;
651 goto unlock_and_exit
;
654 status
= acpi_ns_attach_data (node
, handler
, data
);
657 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
662 /*******************************************************************************
664 * FUNCTION: acpi_detach_data
666 * PARAMETERS: obj_handle - Namespace node handle
667 * Handler - Handler used in call to acpi_attach_data
671 * DESCRIPTION: Remove data that was previously attached to a node.
673 ******************************************************************************/
677 acpi_handle obj_handle
,
678 acpi_object_handler handler
)
680 struct acpi_namespace_node
*node
;
684 /* Parameter validation */
688 return (AE_BAD_PARAMETER
);
691 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
692 if (ACPI_FAILURE (status
)) {
696 /* Convert and validate the handle */
698 node
= acpi_ns_map_handle_to_node (obj_handle
);
700 status
= AE_BAD_PARAMETER
;
701 goto unlock_and_exit
;
704 status
= acpi_ns_detach_data (node
, handler
);
707 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);
712 /*******************************************************************************
714 * FUNCTION: acpi_get_data
716 * PARAMETERS: obj_handle - Namespace node
717 * Handler - Handler used in call to attach_data
718 * Data - Where the data is returned
722 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
724 ******************************************************************************/
728 acpi_handle obj_handle
,
729 acpi_object_handler handler
,
732 struct acpi_namespace_node
*node
;
736 /* Parameter validation */
741 return (AE_BAD_PARAMETER
);
744 status
= acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE
);
745 if (ACPI_FAILURE (status
)) {
749 /* Convert and validate the handle */
751 node
= acpi_ns_map_handle_to_node (obj_handle
);
753 status
= AE_BAD_PARAMETER
;
754 goto unlock_and_exit
;
757 status
= acpi_ns_get_attached_data (node
, handler
, data
);
760 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE
);