ACPICA 20050408 from Bob Moore
[linux-2.6.git] / drivers / acpi / namespace / nsxfeval.c
blob12ea202257fa257dd0f6277baf31d9ab964ade1f
1 /*******************************************************************************
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 * ACPI Object evaluation interfaces
6 ******************************************************************************/
8 /*
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
14 * are met:
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.
31 * NO WARRANTY
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
69 * RETURN: Status
71 * DESCRIPTION: Find and evaluate the given object, passing the given
72 * parameters if necessary. One of "Handle" or "Pathname" must
73 * be valid (non-null)
75 ******************************************************************************/
77 #ifdef ACPI_FUTURE_USAGE
78 acpi_status
79 acpi_evaluate_object_typed (
80 acpi_handle handle,
81 acpi_string pathname,
82 struct acpi_object_list *external_params,
83 struct acpi_buffer *return_buffer,
84 acpi_object_type return_type)
86 acpi_status status;
87 u8 must_free = FALSE;
90 ACPI_FUNCTION_TRACE ("acpi_evaluate_object_typed");
93 /* Return buffer must be valid */
95 if (!return_buffer) {
96 return_ACPI_STATUS (AE_BAD_PARAMETER);
99 if (return_buffer->length == ACPI_ALLOCATE_BUFFER) {
100 must_free = TRUE;
103 /* Evaluate the object */
105 status = acpi_evaluate_object (handle, pathname, external_params, return_buffer);
106 if (ACPI_FAILURE (status)) {
107 return_ACPI_STATUS (status);
110 /* Type ANY means "don't care" */
112 if (return_type == ACPI_TYPE_ANY) {
113 return_ACPI_STATUS (AE_OK);
116 if (return_buffer->length == 0) {
117 /* Error because caller specifically asked for a return value */
119 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
120 "No return value\n"));
122 return_ACPI_STATUS (AE_NULL_OBJECT);
125 /* Examine the object type returned from evaluate_object */
127 if (((union acpi_object *) return_buffer->pointer)->type == return_type) {
128 return_ACPI_STATUS (AE_OK);
131 /* Return object type does not match requested type */
133 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
134 "Incorrect return type [%s] requested [%s]\n",
135 acpi_ut_get_type_name (((union acpi_object *) return_buffer->pointer)->type),
136 acpi_ut_get_type_name (return_type)));
138 if (must_free) {
139 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
141 acpi_os_free (return_buffer->pointer);
142 return_buffer->pointer = NULL;
145 return_buffer->length = 0;
146 return_ACPI_STATUS (AE_TYPE);
148 #endif /* ACPI_FUTURE_USAGE */
151 /*******************************************************************************
153 * FUNCTION: acpi_evaluate_object
155 * PARAMETERS: Handle - Object handle (optional)
156 * Pathname - Object pathname (optional)
157 * external_params - List of parameters to pass to method,
158 * terminated by NULL. May be NULL
159 * if no parameters are being passed.
160 * return_buffer - Where to put method's return value (if
161 * any). If NULL, no value is returned.
163 * RETURN: Status
165 * DESCRIPTION: Find and evaluate the given object, passing the given
166 * parameters if necessary. One of "Handle" or "Pathname" must
167 * be valid (non-null)
169 ******************************************************************************/
171 acpi_status
172 acpi_evaluate_object (
173 acpi_handle handle,
174 acpi_string pathname,
175 struct acpi_object_list *external_params,
176 struct acpi_buffer *return_buffer)
178 acpi_status status;
179 acpi_status status2;
180 struct acpi_parameter_info info;
181 acpi_size buffer_space_needed;
182 u32 i;
185 ACPI_FUNCTION_TRACE ("acpi_evaluate_object");
188 info.node = handle;
189 info.parameters = NULL;
190 info.return_object = NULL;
191 info.parameter_type = ACPI_PARAM_ARGS;
194 * If there are parameters to be passed to the object
195 * (which must be a control method), the external objects
196 * must be converted to internal objects
198 if (external_params && external_params->count) {
200 * Allocate a new parameter block for the internal objects
201 * Add 1 to count to allow for null terminated internal list
203 info.parameters = ACPI_MEM_CALLOCATE (
204 ((acpi_size) external_params->count + 1) *
205 sizeof (void *));
206 if (!info.parameters) {
207 return_ACPI_STATUS (AE_NO_MEMORY);
211 * Convert each external object in the list to an
212 * internal object
214 for (i = 0; i < external_params->count; i++) {
215 status = acpi_ut_copy_eobject_to_iobject (&external_params->pointer[i],
216 &info.parameters[i]);
217 if (ACPI_FAILURE (status)) {
218 acpi_ut_delete_internal_object_list (info.parameters);
219 return_ACPI_STATUS (status);
222 info.parameters[external_params->count] = NULL;
227 * Three major cases:
228 * 1) Fully qualified pathname
229 * 2) No handle, not fully qualified pathname (error)
230 * 3) Valid handle
232 if ((pathname) &&
233 (acpi_ns_valid_root_prefix (pathname[0]))) {
235 * The path is fully qualified, just evaluate by name
237 status = acpi_ns_evaluate_by_name (pathname, &info);
239 else if (!handle) {
241 * A handle is optional iff a fully qualified pathname
242 * is specified. Since we've already handled fully
243 * qualified names above, this is an error
245 if (!pathname) {
246 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
247 "Both Handle and Pathname are NULL\n"));
249 else {
250 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
251 "Handle is NULL and Pathname is relative\n"));
254 status = AE_BAD_PARAMETER;
256 else {
258 * We get here if we have a handle -- and if we have a
259 * pathname it is relative. The handle will be validated
260 * in the lower procedures
262 if (!pathname) {
264 * The null pathname case means the handle is for
265 * the actual object to be evaluated
267 status = acpi_ns_evaluate_by_handle (&info);
269 else {
271 * Both a Handle and a relative Pathname
273 status = acpi_ns_evaluate_relative (pathname, &info);
279 * If we are expecting a return value, and all went well above,
280 * copy the return value to an external object.
282 if (return_buffer) {
283 if (!info.return_object) {
284 return_buffer->length = 0;
286 else {
287 if (ACPI_GET_DESCRIPTOR_TYPE (info.return_object) == ACPI_DESC_TYPE_NAMED) {
289 * If we received a NS Node as a return object, this means that
290 * the object we are evaluating has nothing interesting to
291 * return (such as a mutex, etc.) We return an error because
292 * these types are essentially unsupported by this interface.
293 * We don't check up front because this makes it easier to add
294 * support for various types at a later date if necessary.
296 status = AE_TYPE;
297 info.return_object = NULL; /* No need to delete a NS Node */
298 return_buffer->length = 0;
301 if (ACPI_SUCCESS (status)) {
303 * Find out how large a buffer is needed
304 * to contain the returned object
306 status = acpi_ut_get_object_size (info.return_object,
307 &buffer_space_needed);
308 if (ACPI_SUCCESS (status)) {
309 /* Validate/Allocate/Clear caller buffer */
311 status = acpi_ut_initialize_buffer (return_buffer,
312 buffer_space_needed);
313 if (ACPI_FAILURE (status)) {
315 * Caller's buffer is too small or a new one can't be allocated
317 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
318 "Needed buffer size %X, %s\n",
319 (u32) buffer_space_needed,
320 acpi_format_exception (status)));
322 else {
324 * We have enough space for the object, build it
326 status = acpi_ut_copy_iobject_to_eobject (info.return_object,
327 return_buffer);
334 if (info.return_object) {
336 * Delete the internal return object. NOTE: Interpreter
337 * must be locked to avoid race condition.
339 status2 = acpi_ex_enter_interpreter ();
340 if (ACPI_SUCCESS (status2)) {
342 * Delete the internal return object. (Or at least
343 * decrement the reference count by one)
345 acpi_ut_remove_reference (info.return_object);
346 acpi_ex_exit_interpreter ();
351 * Free the input parameter list (if we created one),
353 if (info.parameters) {
354 /* Free the allocated parameter block */
356 acpi_ut_delete_internal_object_list (info.parameters);
359 return_ACPI_STATUS (status);
361 EXPORT_SYMBOL(acpi_evaluate_object);
364 /*******************************************************************************
366 * FUNCTION: acpi_walk_namespace
368 * PARAMETERS: Type - acpi_object_type to search for
369 * start_object - Handle in namespace where search begins
370 * max_depth - Depth to which search is to reach
371 * user_function - Called when an object of "Type" is found
372 * Context - Passed to user function
373 * return_value - Location where return value of
374 * user_function is put if terminated early
376 * RETURNS Return value from the user_function if terminated early.
377 * Otherwise, returns NULL.
379 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
380 * starting (and ending) at the object specified by start_handle.
381 * The user_function is called whenever an object that matches
382 * the type parameter is found. If the user function returns
383 * a non-zero value, the search is terminated immediately and this
384 * value is returned to the caller.
386 * The point of this procedure is to provide a generic namespace
387 * walk routine that can be called from multiple places to
388 * provide multiple services; the User Function can be tailored
389 * to each task, whether it is a print function, a compare
390 * function, etc.
392 ******************************************************************************/
394 acpi_status
395 acpi_walk_namespace (
396 acpi_object_type type,
397 acpi_handle start_object,
398 u32 max_depth,
399 acpi_walk_callback user_function,
400 void *context,
401 void **return_value)
403 acpi_status status;
406 ACPI_FUNCTION_TRACE ("acpi_walk_namespace");
409 /* Parameter validation */
411 if ((type > ACPI_TYPE_EXTERNAL_MAX) ||
412 (!max_depth) ||
413 (!user_function)) {
414 return_ACPI_STATUS (AE_BAD_PARAMETER);
418 * Lock the namespace around the walk.
419 * The namespace will be unlocked/locked around each call
420 * to the user function - since this function
421 * must be allowed to make Acpi calls itself.
423 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
424 if (ACPI_FAILURE (status)) {
425 return_ACPI_STATUS (status);
428 status = acpi_ns_walk_namespace (type, start_object, max_depth,
429 ACPI_NS_WALK_UNLOCK,
430 user_function, context, return_value);
432 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
433 return_ACPI_STATUS (status);
435 EXPORT_SYMBOL(acpi_walk_namespace);
438 /*******************************************************************************
440 * FUNCTION: acpi_ns_get_device_callback
442 * PARAMETERS: Callback from acpi_get_device
444 * RETURN: Status
446 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
447 * present devices, or if they specified a HID, it filters based
448 * on that.
450 ******************************************************************************/
452 static acpi_status
453 acpi_ns_get_device_callback (
454 acpi_handle obj_handle,
455 u32 nesting_level,
456 void *context,
457 void **return_value)
459 struct acpi_get_devices_info *info = context;
460 acpi_status status;
461 struct acpi_namespace_node *node;
462 u32 flags;
463 struct acpi_device_id hid;
464 struct acpi_compatible_id_list *cid;
465 acpi_native_uint i;
468 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
469 if (ACPI_FAILURE (status)) {
470 return (status);
473 node = acpi_ns_map_handle_to_node (obj_handle);
474 status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
475 if (ACPI_FAILURE (status)) {
476 return (status);
479 if (!node) {
480 return (AE_BAD_PARAMETER);
483 /* Run _STA to determine if device is present */
485 status = acpi_ut_execute_STA (node, &flags);
486 if (ACPI_FAILURE (status)) {
487 return (AE_CTRL_DEPTH);
490 if (!(flags & 0x01)) {
491 /* Don't return at the device or children of the device if not there */
493 return (AE_CTRL_DEPTH);
496 /* Filter based on device HID & CID */
498 if (info->hid != NULL) {
499 status = acpi_ut_execute_HID (node, &hid);
500 if (status == AE_NOT_FOUND) {
501 return (AE_OK);
503 else if (ACPI_FAILURE (status)) {
504 return (AE_CTRL_DEPTH);
507 if (ACPI_STRNCMP (hid.value, info->hid, sizeof (hid.value)) != 0) {
508 /* Get the list of Compatible IDs */
510 status = acpi_ut_execute_CID (node, &cid);
511 if (status == AE_NOT_FOUND) {
512 return (AE_OK);
514 else if (ACPI_FAILURE (status)) {
515 return (AE_CTRL_DEPTH);
518 /* Walk the CID list */
520 for (i = 0; i < cid->count; i++) {
521 if (ACPI_STRNCMP (cid->id[i].value, info->hid,
522 sizeof (struct acpi_compatible_id)) != 0) {
523 ACPI_MEM_FREE (cid);
524 return (AE_OK);
527 ACPI_MEM_FREE (cid);
531 status = info->user_function (obj_handle, nesting_level, info->context,
532 return_value);
533 return (status);
537 /*******************************************************************************
539 * FUNCTION: acpi_get_devices
541 * PARAMETERS: HID - HID to search for. Can be NULL.
542 * user_function - Called when a matching object is found
543 * Context - Passed to user function
544 * return_value - Location where return value of
545 * user_function is put if terminated early
547 * RETURNS Return value from the user_function if terminated early.
548 * Otherwise, returns NULL.
550 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
551 * starting (and ending) at the object specified by start_handle.
552 * The user_function is called whenever an object of type
553 * Device is found. If the user function returns
554 * a non-zero value, the search is terminated immediately and this
555 * value is returned to the caller.
557 * This is a wrapper for walk_namespace, but the callback performs
558 * additional filtering. Please see acpi_get_device_callback.
560 ******************************************************************************/
562 acpi_status
563 acpi_get_devices (
564 char *HID,
565 acpi_walk_callback user_function,
566 void *context,
567 void **return_value)
569 acpi_status status;
570 struct acpi_get_devices_info info;
573 ACPI_FUNCTION_TRACE ("acpi_get_devices");
576 /* Parameter validation */
578 if (!user_function) {
579 return_ACPI_STATUS (AE_BAD_PARAMETER);
583 * We're going to call their callback from OUR callback, so we need
584 * to know what it is, and their context parameter.
586 info.context = context;
587 info.user_function = user_function;
588 info.hid = HID;
591 * Lock the namespace around the walk.
592 * The namespace will be unlocked/locked around each call
593 * to the user function - since this function
594 * must be allowed to make Acpi calls itself.
596 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
597 if (ACPI_FAILURE (status)) {
598 return_ACPI_STATUS (status);
601 status = acpi_ns_walk_namespace (ACPI_TYPE_DEVICE,
602 ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
603 ACPI_NS_WALK_UNLOCK,
604 acpi_ns_get_device_callback, &info,
605 return_value);
607 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
608 return_ACPI_STATUS (status);
610 EXPORT_SYMBOL(acpi_get_devices);
613 /*******************************************************************************
615 * FUNCTION: acpi_attach_data
617 * PARAMETERS: obj_handle - Namespace node
618 * Handler - Handler for this attachment
619 * Data - Pointer to data to be attached
621 * RETURN: Status
623 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
625 ******************************************************************************/
627 acpi_status
628 acpi_attach_data (
629 acpi_handle obj_handle,
630 acpi_object_handler handler,
631 void *data)
633 struct acpi_namespace_node *node;
634 acpi_status status;
637 /* Parameter validation */
639 if (!obj_handle ||
640 !handler ||
641 !data) {
642 return (AE_BAD_PARAMETER);
645 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
646 if (ACPI_FAILURE (status)) {
647 return (status);
650 /* Convert and validate the handle */
652 node = acpi_ns_map_handle_to_node (obj_handle);
653 if (!node) {
654 status = AE_BAD_PARAMETER;
655 goto unlock_and_exit;
658 status = acpi_ns_attach_data (node, handler, data);
660 unlock_and_exit:
661 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
662 return (status);
666 /*******************************************************************************
668 * FUNCTION: acpi_detach_data
670 * PARAMETERS: obj_handle - Namespace node handle
671 * Handler - Handler used in call to acpi_attach_data
673 * RETURN: Status
675 * DESCRIPTION: Remove data that was previously attached to a node.
677 ******************************************************************************/
679 acpi_status
680 acpi_detach_data (
681 acpi_handle obj_handle,
682 acpi_object_handler handler)
684 struct acpi_namespace_node *node;
685 acpi_status status;
688 /* Parameter validation */
690 if (!obj_handle ||
691 !handler) {
692 return (AE_BAD_PARAMETER);
695 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
696 if (ACPI_FAILURE (status)) {
697 return (status);
700 /* Convert and validate the handle */
702 node = acpi_ns_map_handle_to_node (obj_handle);
703 if (!node) {
704 status = AE_BAD_PARAMETER;
705 goto unlock_and_exit;
708 status = acpi_ns_detach_data (node, handler);
710 unlock_and_exit:
711 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
712 return (status);
716 /*******************************************************************************
718 * FUNCTION: acpi_get_data
720 * PARAMETERS: obj_handle - Namespace node
721 * Handler - Handler used in call to attach_data
722 * Data - Where the data is returned
724 * RETURN: Status
726 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
728 ******************************************************************************/
730 acpi_status
731 acpi_get_data (
732 acpi_handle obj_handle,
733 acpi_object_handler handler,
734 void **data)
736 struct acpi_namespace_node *node;
737 acpi_status status;
740 /* Parameter validation */
742 if (!obj_handle ||
743 !handler ||
744 !data) {
745 return (AE_BAD_PARAMETER);
748 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
749 if (ACPI_FAILURE (status)) {
750 return (status);
753 /* Convert and validate the handle */
755 node = acpi_ns_map_handle_to_node (obj_handle);
756 if (!node) {
757 status = AE_BAD_PARAMETER;
758 goto unlock_and_exit;
761 status = acpi_ns_get_attached_data (node, handler, data);
763 unlock_and_exit:
764 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
765 return (status);