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 <linux/module.h>
47 #include <acpi/acpi.h>
48 #include <acpi/acnamesp.h>
49 #include <acpi/acinterp.h>
51 #define _COMPONENT ACPI_NAMESPACE
52 ACPI_MODULE_NAME("nsxfeval")
54 /*******************************************************************************
56 * FUNCTION: acpi_evaluate_object_typed
58 * PARAMETERS: Handle - Object handle (optional)
59 * Pathname - Object pathname (optional)
60 * external_params - List of parameters to pass to method,
61 * terminated by NULL. May be NULL
62 * if no parameters are being passed.
63 * return_buffer - Where to put method's return value (if
64 * any). If NULL, no value is returned.
65 * return_type - Expected type of return object
69 * DESCRIPTION: Find and evaluate the given object, passing the given
70 * parameters if necessary. One of "Handle" or "Pathname" must
73 ******************************************************************************/
74 #ifdef ACPI_FUTURE_USAGE
76 acpi_evaluate_object_typed(acpi_handle handle
,
78 struct acpi_object_list
*external_params
,
79 struct acpi_buffer
*return_buffer
,
80 acpi_object_type return_type
)
85 ACPI_FUNCTION_TRACE("acpi_evaluate_object_typed");
87 /* Return buffer must be valid */
90 return_ACPI_STATUS(AE_BAD_PARAMETER
);
93 if (return_buffer
->length
== ACPI_ALLOCATE_BUFFER
) {
97 /* Evaluate the object */
100 acpi_evaluate_object(handle
, pathname
, external_params
,
102 if (ACPI_FAILURE(status
)) {
103 return_ACPI_STATUS(status
);
106 /* Type ANY means "don't care" */
108 if (return_type
== ACPI_TYPE_ANY
) {
109 return_ACPI_STATUS(AE_OK
);
112 if (return_buffer
->length
== 0) {
114 /* Error because caller specifically asked for a return value */
116 ACPI_ERROR((AE_INFO
, "No return value"));
117 return_ACPI_STATUS(AE_NULL_OBJECT
);
120 /* Examine the object type returned from evaluate_object */
122 if (((union acpi_object
*)return_buffer
->pointer
)->type
== return_type
) {
123 return_ACPI_STATUS(AE_OK
);
126 /* Return object type does not match requested type */
129 "Incorrect return type [%s] requested [%s]",
130 acpi_ut_get_type_name(((union acpi_object
*)return_buffer
->
132 acpi_ut_get_type_name(return_type
)));
136 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
138 acpi_os_free(return_buffer
->pointer
);
139 return_buffer
->pointer
= NULL
;
142 return_buffer
->length
= 0;
143 return_ACPI_STATUS(AE_TYPE
);
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 ******************************************************************************/
168 acpi_evaluate_object(acpi_handle handle
,
169 acpi_string pathname
,
170 struct acpi_object_list
*external_params
,
171 struct acpi_buffer
*return_buffer
)
175 struct acpi_parameter_info info
;
176 acpi_size buffer_space_needed
;
179 ACPI_FUNCTION_TRACE("acpi_evaluate_object");
182 info
.parameters
= NULL
;
183 info
.return_object
= NULL
;
184 info
.parameter_type
= ACPI_PARAM_ARGS
;
187 * If there are parameters to be passed to the object
188 * (which must be a control method), the external objects
189 * must be converted to internal objects
191 if (external_params
&& external_params
->count
) {
193 * Allocate a new parameter block for the internal objects
194 * Add 1 to count to allow for null terminated internal list
196 info
.parameters
= ACPI_MEM_CALLOCATE(((acpi_size
)
197 external_params
->count
+
198 1) * sizeof(void *));
199 if (!info
.parameters
) {
200 return_ACPI_STATUS(AE_NO_MEMORY
);
204 * Convert each external object in the list to an
207 for (i
= 0; i
< external_params
->count
; i
++) {
209 acpi_ut_copy_eobject_to_iobject(&external_params
->
213 if (ACPI_FAILURE(status
)) {
214 acpi_ut_delete_internal_object_list(info
.
216 return_ACPI_STATUS(status
);
219 info
.parameters
[external_params
->count
] = NULL
;
224 * 1) Fully qualified pathname
225 * 2) No handle, not fully qualified pathname (error)
228 if ((pathname
) && (acpi_ns_valid_root_prefix(pathname
[0]))) {
230 /* The path is fully qualified, just evaluate by name */
232 status
= acpi_ns_evaluate_by_name(pathname
, &info
);
233 } else if (!handle
) {
235 * A handle is optional iff a fully qualified pathname
236 * is specified. Since we've already handled fully
237 * qualified names above, this is an error
240 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
241 "Both Handle and Pathname are NULL"));
243 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
244 "Null Handle with relative pathname [%s]",
248 status
= AE_BAD_PARAMETER
;
251 * We get here if we have a handle -- and if we have a
252 * pathname it is relative. The handle will be validated
253 * in the lower procedures
257 * The null pathname case means the handle is for
258 * the actual object to be evaluated
260 status
= acpi_ns_evaluate_by_handle(&info
);
262 /* Both a Handle and a relative Pathname */
264 status
= acpi_ns_evaluate_relative(pathname
, &info
);
269 * If we are expecting a return value, and all went well above,
270 * copy the return value to an external object.
273 if (!info
.return_object
) {
274 return_buffer
->length
= 0;
276 if (ACPI_GET_DESCRIPTOR_TYPE(info
.return_object
) ==
277 ACPI_DESC_TYPE_NAMED
) {
279 * If we received a NS Node as a return object, this means that
280 * the object we are evaluating has nothing interesting to
281 * return (such as a mutex, etc.) We return an error because
282 * these types are essentially unsupported by this interface.
283 * We don't check up front because this makes it easier to add
284 * support for various types at a later date if necessary.
287 info
.return_object
= NULL
; /* No need to delete a NS Node */
288 return_buffer
->length
= 0;
291 if (ACPI_SUCCESS(status
)) {
293 * Find out how large a buffer is needed
294 * to contain the returned object
297 acpi_ut_get_object_size(info
.return_object
,
298 &buffer_space_needed
);
299 if (ACPI_SUCCESS(status
)) {
301 /* Validate/Allocate/Clear caller buffer */
304 acpi_ut_initialize_buffer
306 buffer_space_needed
);
307 if (ACPI_FAILURE(status
)) {
309 * Caller's buffer is too small or a new one can't
312 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
313 "Needed buffer size %X, %s\n",
316 acpi_format_exception
319 /* We have enough space for the object, build it */
322 acpi_ut_copy_iobject_to_eobject
331 if (info
.return_object
) {
333 * Delete the internal return object. NOTE: Interpreter
334 * must be locked to avoid race condition.
336 status2
= acpi_ex_enter_interpreter();
337 if (ACPI_SUCCESS(status2
)) {
339 * Delete the internal return object. (Or at least
340 * decrement the reference count by one)
342 acpi_ut_remove_reference(info
.return_object
);
343 acpi_ex_exit_interpreter();
347 /* Free the input parameter list (if we created one) */
349 if (info
.parameters
) {
351 /* Free the allocated parameter block */
353 acpi_ut_delete_internal_object_list(info
.parameters
);
356 return_ACPI_STATUS(status
);
359 EXPORT_SYMBOL(acpi_evaluate_object
);
361 /*******************************************************************************
363 * FUNCTION: acpi_walk_namespace
365 * PARAMETERS: Type - acpi_object_type to search for
366 * start_object - Handle in namespace where search begins
367 * max_depth - Depth to which search is to reach
368 * user_function - Called when an object of "Type" is found
369 * Context - Passed to user function
370 * return_value - Location where return value of
371 * user_function is put if terminated early
373 * RETURNS Return value from the user_function if terminated early.
374 * Otherwise, returns NULL.
376 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
377 * starting (and ending) at the object specified by start_handle.
378 * The user_function is called whenever an object that matches
379 * the type parameter is found. If the user function returns
380 * a non-zero value, the search is terminated immediately and this
381 * value is returned to the caller.
383 * The point of this procedure is to provide a generic namespace
384 * walk routine that can be called from multiple places to
385 * provide multiple services; the User Function can be tailored
386 * to each task, whether it is a print function, a compare
389 ******************************************************************************/
392 acpi_walk_namespace(acpi_object_type type
,
393 acpi_handle start_object
,
395 acpi_walk_callback user_function
,
396 void *context
, void **return_value
)
400 ACPI_FUNCTION_TRACE("acpi_walk_namespace");
402 /* Parameter validation */
404 if ((type
> ACPI_TYPE_LOCAL_MAX
) || (!max_depth
) || (!user_function
)) {
405 return_ACPI_STATUS(AE_BAD_PARAMETER
);
409 * Lock the namespace around the walk.
410 * The namespace will be unlocked/locked around each call
411 * to the user function - since this function
412 * must be allowed to make Acpi calls itself.
414 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
415 if (ACPI_FAILURE(status
)) {
416 return_ACPI_STATUS(status
);
419 status
= acpi_ns_walk_namespace(type
, start_object
, max_depth
,
421 user_function
, context
, return_value
);
423 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
424 return_ACPI_STATUS(status
);
427 EXPORT_SYMBOL(acpi_walk_namespace
);
429 /*******************************************************************************
431 * FUNCTION: acpi_ns_get_device_callback
433 * PARAMETERS: Callback from acpi_get_device
437 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
438 * present devices, or if they specified a HID, it filters based
441 ******************************************************************************/
444 acpi_ns_get_device_callback(acpi_handle obj_handle
,
446 void *context
, void **return_value
)
448 struct acpi_get_devices_info
*info
= context
;
450 struct acpi_namespace_node
*node
;
452 struct acpi_device_id hid
;
453 struct acpi_compatible_id_list
*cid
;
456 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
457 if (ACPI_FAILURE(status
)) {
461 node
= acpi_ns_map_handle_to_node(obj_handle
);
462 status
= acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
463 if (ACPI_FAILURE(status
)) {
468 return (AE_BAD_PARAMETER
);
471 /* Run _STA to determine if device is present */
473 status
= acpi_ut_execute_STA(node
, &flags
);
474 if (ACPI_FAILURE(status
)) {
475 return (AE_CTRL_DEPTH
);
478 if (!(flags
& ACPI_STA_DEVICE_PRESENT
)) {
480 /* Don't examine children of the device if not present */
482 return (AE_CTRL_DEPTH
);
485 /* Filter based on device HID & CID */
487 if (info
->hid
!= NULL
) {
488 status
= acpi_ut_execute_HID(node
, &hid
);
489 if (status
== AE_NOT_FOUND
) {
491 } else if (ACPI_FAILURE(status
)) {
492 return (AE_CTRL_DEPTH
);
495 if (ACPI_STRNCMP(hid
.value
, info
->hid
, sizeof(hid
.value
)) != 0) {
497 /* Get the list of Compatible IDs */
499 status
= acpi_ut_execute_CID(node
, &cid
);
500 if (status
== AE_NOT_FOUND
) {
502 } else if (ACPI_FAILURE(status
)) {
503 return (AE_CTRL_DEPTH
);
506 /* Walk the CID list */
508 for (i
= 0; i
< cid
->count
; i
++) {
509 if (ACPI_STRNCMP(cid
->id
[i
].value
, info
->hid
,
511 acpi_compatible_id
)) !=
521 status
= info
->user_function(obj_handle
, nesting_level
, info
->context
,
526 /*******************************************************************************
528 * FUNCTION: acpi_get_devices
530 * PARAMETERS: HID - HID to search for. Can be NULL.
531 * user_function - Called when a matching object is found
532 * Context - Passed to user function
533 * return_value - Location where return value of
534 * user_function is put if terminated early
536 * RETURNS Return value from the user_function if terminated early.
537 * Otherwise, returns NULL.
539 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
540 * starting (and ending) at the object specified by start_handle.
541 * The user_function is called whenever an object of type
542 * Device is found. If the user function returns
543 * a non-zero value, the search is terminated immediately and this
544 * value is returned to the caller.
546 * This is a wrapper for walk_namespace, but the callback performs
547 * additional filtering. Please see acpi_get_device_callback.
549 ******************************************************************************/
552 acpi_get_devices(char *HID
,
553 acpi_walk_callback user_function
,
554 void *context
, void **return_value
)
557 struct acpi_get_devices_info info
;
559 ACPI_FUNCTION_TRACE("acpi_get_devices");
561 /* Parameter validation */
563 if (!user_function
) {
564 return_ACPI_STATUS(AE_BAD_PARAMETER
);
568 * We're going to call their callback from OUR callback, so we need
569 * to know what it is, and their context parameter.
572 info
.context
= context
;
573 info
.user_function
= user_function
;
576 * Lock the namespace around the walk.
577 * The namespace will be unlocked/locked around each call
578 * to the user function - since this function
579 * must be allowed to make Acpi calls itself.
581 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
582 if (ACPI_FAILURE(status
)) {
583 return_ACPI_STATUS(status
);
586 status
= acpi_ns_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
587 ACPI_UINT32_MAX
, ACPI_NS_WALK_UNLOCK
,
588 acpi_ns_get_device_callback
, &info
,
591 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
592 return_ACPI_STATUS(status
);
595 EXPORT_SYMBOL(acpi_get_devices
);
597 /*******************************************************************************
599 * FUNCTION: acpi_attach_data
601 * PARAMETERS: obj_handle - Namespace node
602 * Handler - Handler for this attachment
603 * Data - Pointer to data to be attached
607 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
609 ******************************************************************************/
612 acpi_attach_data(acpi_handle obj_handle
,
613 acpi_object_handler handler
, void *data
)
615 struct acpi_namespace_node
*node
;
618 /* Parameter validation */
620 if (!obj_handle
|| !handler
|| !data
) {
621 return (AE_BAD_PARAMETER
);
624 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
625 if (ACPI_FAILURE(status
)) {
629 /* Convert and validate the handle */
631 node
= acpi_ns_map_handle_to_node(obj_handle
);
633 status
= AE_BAD_PARAMETER
;
634 goto unlock_and_exit
;
637 status
= acpi_ns_attach_data(node
, handler
, data
);
640 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
644 /*******************************************************************************
646 * FUNCTION: acpi_detach_data
648 * PARAMETERS: obj_handle - Namespace node handle
649 * Handler - Handler used in call to acpi_attach_data
653 * DESCRIPTION: Remove data that was previously attached to a node.
655 ******************************************************************************/
658 acpi_detach_data(acpi_handle obj_handle
, acpi_object_handler handler
)
660 struct acpi_namespace_node
*node
;
663 /* Parameter validation */
665 if (!obj_handle
|| !handler
) {
666 return (AE_BAD_PARAMETER
);
669 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
670 if (ACPI_FAILURE(status
)) {
674 /* Convert and validate the handle */
676 node
= acpi_ns_map_handle_to_node(obj_handle
);
678 status
= AE_BAD_PARAMETER
;
679 goto unlock_and_exit
;
682 status
= acpi_ns_detach_data(node
, handler
);
685 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
689 /*******************************************************************************
691 * FUNCTION: acpi_get_data
693 * PARAMETERS: obj_handle - Namespace node
694 * Handler - Handler used in call to attach_data
695 * Data - Where the data is returned
699 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
701 ******************************************************************************/
704 acpi_get_data(acpi_handle obj_handle
, acpi_object_handler handler
, void **data
)
706 struct acpi_namespace_node
*node
;
709 /* Parameter validation */
711 if (!obj_handle
|| !handler
|| !data
) {
712 return (AE_BAD_PARAMETER
);
715 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
716 if (ACPI_FAILURE(status
)) {
720 /* Convert and validate the handle */
722 node
= acpi_ns_map_handle_to_node(obj_handle
);
724 status
= AE_BAD_PARAMETER
;
725 goto unlock_and_exit
;
728 status
= acpi_ns_get_attached_data(node
, handler
, data
);
731 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);