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) {
113 /* Error because caller specifically asked for a return value */
115 ACPI_REPORT_ERROR(("No return value\n"));
116 return_ACPI_STATUS(AE_NULL_OBJECT);
119 /* Examine the object type returned from evaluate_object */
121 if (((union acpi_object *)return_buffer->pointer)->type == return_type) {
122 return_ACPI_STATUS(AE_OK);
125 /* Return object type does not match requested type */
127 ACPI_REPORT_ERROR(("Incorrect return type [%s] requested [%s]\n",
128 acpi_ut_get_type_name(((union acpi_object *)
129 return_buffer->pointer)->
131 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);
143 #endif /* ACPI_FUTURE_USAGE */
145 /*******************************************************************************
147 * FUNCTION: acpi_evaluate_object
149 * PARAMETERS: Handle - Object handle (optional)
150 * Pathname - Object pathname (optional)
151 * external_params - List of parameters to pass to method,
152 * terminated by NULL. May be NULL
153 * if no parameters are being passed.
154 * return_buffer - Where to put method's return value (if
155 * any). If NULL, no value is returned.
159 * DESCRIPTION: Find and evaluate the given object, passing the given
160 * parameters if necessary. One of "Handle" or "Pathname" must
161 * be valid (non-null)
163 ******************************************************************************/
166 acpi_evaluate_object(acpi_handle handle,
167 acpi_string pathname,
168 struct acpi_object_list *external_params,
169 struct acpi_buffer *return_buffer)
173 struct acpi_parameter_info info;
174 acpi_size buffer_space_needed;
177 ACPI_FUNCTION_TRACE("acpi_evaluate_object");
180 info.parameters = NULL;
181 info.return_object = NULL;
182 info.parameter_type = ACPI_PARAM_ARGS;
185 * If there are parameters to be passed to the object
186 * (which must be a control method), the external objects
187 * must be converted to internal objects
189 if (external_params && external_params->count) {
191 * Allocate a new parameter block for the internal objects
192 * Add 1 to count to allow for null terminated internal list
194 info.parameters = ACPI_MEM_CALLOCATE(((acpi_size)
195 external_params->count +
196 1) * sizeof(void *));
197 if (!info.parameters) {
198 return_ACPI_STATUS(AE_NO_MEMORY);
202 * Convert each external object in the list to an
205 for (i = 0; i < external_params->count; i++) {
207 acpi_ut_copy_eobject_to_iobject(&external_params->
211 if (ACPI_FAILURE(status)) {
212 acpi_ut_delete_internal_object_list(info.
214 return_ACPI_STATUS(status);
217 info.parameters[external_params->count] = NULL;
222 * 1) Fully qualified pathname
223 * 2) No handle, not fully qualified pathname (error)
226 if ((pathname) && (acpi_ns_valid_root_prefix(pathname[0]))) {
228 * The path is fully qualified, just evaluate by name
230 status = acpi_ns_evaluate_by_name(pathname, &info);
231 } else if (!handle) {
233 * A handle is optional iff a fully qualified pathname
234 * is specified. Since we've already handled fully
235 * qualified names above, this is an error
238 ACPI_REPORT_ERROR(("Both Handle and Pathname are NULL\n"));
240 ACPI_REPORT_ERROR(("Handle is NULL and Pathname is relative\n"));
243 status = AE_BAD_PARAMETER;
246 * We get here if we have a handle -- and if we have a
247 * pathname it is relative. The handle will be validated
248 * in the lower procedures
252 * The null pathname case means the handle is for
253 * the actual object to be evaluated
255 status = acpi_ns_evaluate_by_handle(&info);
258 * Both a Handle and a relative Pathname
260 status = acpi_ns_evaluate_relative(pathname, &info);
265 * If we are expecting a return value, and all went well above,
266 * copy the return value to an external object.
269 if (!info.return_object) {
270 return_buffer->length = 0;
272 if (ACPI_GET_DESCRIPTOR_TYPE(info.return_object) ==
273 ACPI_DESC_TYPE_NAMED) {
275 * If we received a NS Node as a return object, this means that
276 * the object we are evaluating has nothing interesting to
277 * return (such as a mutex, etc.) We return an error because
278 * these types are essentially unsupported by this interface.
279 * We don't check up front because this makes it easier to add
280 * support for various types at a later date if necessary.
283 info.return_object = NULL; /* No need to delete a NS Node */
284 return_buffer->length = 0;
287 if (ACPI_SUCCESS(status)) {
289 * Find out how large a buffer is needed
290 * to contain the returned object
293 acpi_ut_get_object_size(info.return_object,
294 &buffer_space_needed);
295 if (ACPI_SUCCESS(status)) {
296 /* Validate/Allocate/Clear caller buffer */
299 acpi_ut_initialize_buffer
301 buffer_space_needed);
302 if (ACPI_FAILURE(status)) {
304 * Caller's buffer is too small or a new one can't be allocated
306 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
307 "Needed buffer size %X, %s\n",
310 acpi_format_exception
314 * We have enough space for the object, build it
317 acpi_ut_copy_iobject_to_eobject
326 if (info.return_object) {
328 * Delete the internal return object. NOTE: Interpreter
329 * must be locked to avoid race condition.
331 status2 = acpi_ex_enter_interpreter();
332 if (ACPI_SUCCESS(status2)) {
334 * Delete the internal return object. (Or at least
335 * decrement the reference count by one)
337 acpi_ut_remove_reference(info.return_object);
338 acpi_ex_exit_interpreter();
343 * Free the input parameter list (if we created one),
345 if (info.parameters) {
346 /* Free the allocated parameter block */
348 acpi_ut_delete_internal_object_list(info.parameters);
351 return_ACPI_STATUS(status);
354 EXPORT_SYMBOL(acpi_evaluate_object);
356 /*******************************************************************************
358 * FUNCTION: acpi_walk_namespace
360 * PARAMETERS: Type - acpi_object_type to search for
361 * start_object - Handle in namespace where search begins
362 * max_depth - Depth to which search is to reach
363 * user_function - Called when an object of "Type" is found
364 * Context - Passed to user function
365 * return_value - Location where return value of
366 * user_function is put if terminated early
368 * RETURNS Return value from the user_function if terminated early.
369 * Otherwise, returns NULL.
371 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
372 * starting (and ending) at the object specified by start_handle.
373 * The user_function is called whenever an object that matches
374 * the type parameter is found. If the user function returns
375 * a non-zero value, the search is terminated immediately and this
376 * value is returned to the caller.
378 * The point of this procedure is to provide a generic namespace
379 * walk routine that can be called from multiple places to
380 * provide multiple services; the User Function can be tailored
381 * to each task, whether it is a print function, a compare
384 ******************************************************************************/
387 acpi_walk_namespace(acpi_object_type type,
388 acpi_handle start_object,
390 acpi_walk_callback user_function,
391 void *context, void **return_value)
395 ACPI_FUNCTION_TRACE("acpi_walk_namespace");
397 /* Parameter validation */
399 if ((type > ACPI_TYPE_LOCAL_MAX) || (!max_depth) || (!user_function)) {
400 return_ACPI_STATUS(AE_BAD_PARAMETER);
404 * Lock the namespace around the walk.
405 * The namespace will be unlocked/locked around each call
406 * to the user function - since this function
407 * must be allowed to make Acpi calls itself.
409 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
410 if (ACPI_FAILURE(status)) {
411 return_ACPI_STATUS(status);
414 status = acpi_ns_walk_namespace(type, start_object, max_depth,
416 user_function, context, return_value);
418 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
419 return_ACPI_STATUS(status);
422 EXPORT_SYMBOL(acpi_walk_namespace);
424 /*******************************************************************************
426 * FUNCTION: acpi_ns_get_device_callback
428 * PARAMETERS: Callback from acpi_get_device
432 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
433 * present devices, or if they specified a HID, it filters based
436 ******************************************************************************/
439 acpi_ns_get_device_callback(acpi_handle obj_handle,
441 void *context, void **return_value)
443 struct acpi_get_devices_info *info = context;
445 struct acpi_namespace_node *node;
447 struct acpi_device_id hid;
448 struct acpi_compatible_id_list *cid;
451 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
452 if (ACPI_FAILURE(status)) {
456 node = acpi_ns_map_handle_to_node(obj_handle);
457 status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
458 if (ACPI_FAILURE(status)) {
463 return (AE_BAD_PARAMETER);
466 /* Run _STA to determine if device is present */
468 status = acpi_ut_execute_STA(node, &flags);
469 if (ACPI_FAILURE(status)) {
470 return (AE_CTRL_DEPTH);
473 if (!(flags & ACPI_STA_DEVICE_PRESENT)) {
474 /* Don't examine children of the device if not present */
476 return (AE_CTRL_DEPTH);
479 /* Filter based on device HID & CID */
481 if (info->hid != NULL) {
482 status = acpi_ut_execute_HID(node, &hid);
483 if (status == AE_NOT_FOUND) {
485 } else if (ACPI_FAILURE(status)) {
486 return (AE_CTRL_DEPTH);
489 if (ACPI_STRNCMP(hid.value, info->hid, sizeof(hid.value)) != 0) {
490 /* Get the list of Compatible IDs */
492 status = acpi_ut_execute_CID(node, &cid);
493 if (status == AE_NOT_FOUND) {
495 } else if (ACPI_FAILURE(status)) {
496 return (AE_CTRL_DEPTH);
499 /* Walk the CID list */
501 for (i = 0; i < cid->count; i++) {
502 if (ACPI_STRNCMP(cid->id[i].value, info->hid,
504 acpi_compatible_id)) !=
514 status = info->user_function(obj_handle, nesting_level, info->context,
519 /*******************************************************************************
521 * FUNCTION: acpi_get_devices
523 * PARAMETERS: HID - HID to search for. Can be NULL.
524 * user_function - Called when a matching object is found
525 * Context - Passed to user function
526 * return_value - Location where return value of
527 * user_function is put if terminated early
529 * RETURNS Return value from the user_function if terminated early.
530 * Otherwise, returns NULL.
532 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
533 * starting (and ending) at the object specified by start_handle.
534 * The user_function is called whenever an object of type
535 * Device is found. If the user function returns
536 * a non-zero value, the search is terminated immediately and this
537 * value is returned to the caller.
539 * This is a wrapper for walk_namespace, but the callback performs
540 * additional filtering. Please see acpi_get_device_callback.
542 ******************************************************************************/
545 acpi_get_devices(char *HID,
546 acpi_walk_callback user_function,
547 void *context, void **return_value)
550 struct acpi_get_devices_info info;
552 ACPI_FUNCTION_TRACE("acpi_get_devices");
554 /* Parameter validation */
556 if (!user_function) {
557 return_ACPI_STATUS(AE_BAD_PARAMETER);
561 * We're going to call their callback from OUR callback, so we need
562 * to know what it is, and their context parameter.
564 info.context = context;
565 info.user_function = user_function;
569 * Lock the namespace around the walk.
570 * The namespace will be unlocked/locked around each call
571 * to the user function - since this function
572 * must be allowed to make Acpi calls itself.
574 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
575 if (ACPI_FAILURE(status)) {
576 return_ACPI_STATUS(status);
579 status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE,
580 ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
582 acpi_ns_get_device_callback, &info,
585 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
586 return_ACPI_STATUS(status);
589 EXPORT_SYMBOL(acpi_get_devices);
591 /*******************************************************************************
593 * FUNCTION: acpi_attach_data
595 * PARAMETERS: obj_handle - Namespace node
596 * Handler - Handler for this attachment
597 * Data - Pointer to data to be attached
601 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
603 ******************************************************************************/
606 acpi_attach_data(acpi_handle obj_handle,
607 acpi_object_handler handler, void *data)
609 struct acpi_namespace_node *node;
612 /* Parameter validation */
614 if (!obj_handle || !handler || !data) {
615 return (AE_BAD_PARAMETER);
618 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
619 if (ACPI_FAILURE(status)) {
623 /* Convert and validate the handle */
625 node = acpi_ns_map_handle_to_node(obj_handle);
627 status = AE_BAD_PARAMETER;
628 goto unlock_and_exit;
631 status = acpi_ns_attach_data(node, handler, data);
634 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
638 /*******************************************************************************
640 * FUNCTION: acpi_detach_data
642 * PARAMETERS: obj_handle - Namespace node handle
643 * Handler - Handler used in call to acpi_attach_data
647 * DESCRIPTION: Remove data that was previously attached to a node.
649 ******************************************************************************/
652 acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler)
654 struct acpi_namespace_node *node;
657 /* Parameter validation */
659 if (!obj_handle || !handler) {
660 return (AE_BAD_PARAMETER);
663 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
664 if (ACPI_FAILURE(status)) {
668 /* Convert and validate the handle */
670 node = acpi_ns_map_handle_to_node(obj_handle);
672 status = AE_BAD_PARAMETER;
673 goto unlock_and_exit;
676 status = acpi_ns_detach_data(node, handler);
679 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
683 /*******************************************************************************
685 * FUNCTION: acpi_get_data
687 * PARAMETERS: obj_handle - Namespace node
688 * Handler - Handler used in call to attach_data
689 * Data - Where the data is returned
693 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
695 ******************************************************************************/
698 acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data)
700 struct acpi_namespace_node *node;
703 /* Parameter validation */
705 if (!obj_handle || !handler || !data) {
706 return (AE_BAD_PARAMETER);
709 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
710 if (ACPI_FAILURE(status)) {
714 /* Convert and validate the handle */
716 node = acpi_ns_map_handle_to_node(obj_handle);
718 status = AE_BAD_PARAMETER;
719 goto unlock_and_exit;
722 status = acpi_ns_get_attached_data(node, handler, data);
725 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);