initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / acpi / namespace / nsxfeval.c
blob8e916b5e274eff426fdf4f807825746da63f5297
1 /*******************************************************************************
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 * ACPI Object evaluation interfaces
6 ******************************************************************************/
8 /*
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
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.
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
68 * RETURN: Status
70 * DESCRIPTION: Find and evaluate the given object, passing the given
71 * parameters if necessary. One of "Handle" or "Pathname" must
72 * be valid (non-null)
74 ******************************************************************************/
76 acpi_status
77 acpi_evaluate_object_typed (
78 acpi_handle handle,
79 acpi_string pathname,
80 struct acpi_object_list *external_params,
81 struct acpi_buffer *return_buffer,
82 acpi_object_type return_type)
84 acpi_status status;
85 u8 must_free = FALSE;
88 ACPI_FUNCTION_TRACE ("acpi_evaluate_object_typed");
91 /* Return buffer must be valid */
93 if (!return_buffer) {
94 return_ACPI_STATUS (AE_BAD_PARAMETER);
97 if (return_buffer->length == ACPI_ALLOCATE_BUFFER) {
98 must_free = TRUE;
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)));
136 if (must_free) {
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.
160 * RETURN: Status
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 ******************************************************************************/
168 acpi_status
169 acpi_evaluate_object (
170 acpi_handle handle,
171 acpi_string pathname,
172 struct acpi_object_list *external_params,
173 struct acpi_buffer *return_buffer)
175 acpi_status status;
176 acpi_status status2;
177 struct acpi_parameter_info info;
178 acpi_size buffer_space_needed;
179 u32 i;
182 ACPI_FUNCTION_TRACE ("acpi_evaluate_object");
185 info.node = handle;
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) *
202 sizeof (void *));
203 if (!info.parameters) {
204 return_ACPI_STATUS (AE_NO_MEMORY);
208 * Convert each external object in the list to an
209 * internal object
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;
224 * Three major cases:
225 * 1) Fully qualified pathname
226 * 2) No handle, not fully qualified pathname (error)
227 * 3) Valid handle
229 if ((pathname) &&
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);
236 else if (!handle) {
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
242 if (!pathname) {
243 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
244 "Both Handle and Pathname are NULL\n"));
246 else {
247 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
248 "Handle is NULL and Pathname is relative\n"));
251 status = AE_BAD_PARAMETER;
253 else {
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
259 if (!pathname) {
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);
266 else {
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.
279 if (return_buffer) {
280 if (!info.return_object) {
281 return_buffer->length = 0;
283 else {
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.
293 status = AE_TYPE;
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)));
318 else {
320 * We have enough space for the object, build it
322 status = acpi_ut_copy_iobject_to_eobject (info.return_object,
323 return_buffer);
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
385 * function, etc.
387 ******************************************************************************/
389 acpi_status
390 acpi_walk_namespace (
391 acpi_object_type type,
392 acpi_handle start_object,
393 u32 max_depth,
394 acpi_walk_callback user_function,
395 void *context,
396 void **return_value)
398 acpi_status status;
401 ACPI_FUNCTION_TRACE ("acpi_walk_namespace");
404 /* Parameter validation */
406 if ((type > ACPI_TYPE_EXTERNAL_MAX) ||
407 (!max_depth) ||
408 (!user_function)) {
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
437 * RETURN: Status
439 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
440 * present devices, or if they specified a HID, it filters based
441 * on that.
443 ******************************************************************************/
445 static acpi_status
446 acpi_ns_get_device_callback (
447 acpi_handle obj_handle,
448 u32 nesting_level,
449 void *context,
450 void **return_value)
452 struct acpi_get_devices_info *info = context;
453 acpi_status status;
454 struct acpi_namespace_node *node;
455 u32 flags;
456 struct acpi_device_id hid;
457 struct acpi_compatible_id_list *cid;
458 acpi_native_uint i;
461 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
462 if (ACPI_FAILURE (status)) {
463 return (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)) {
469 return (status);
472 if (!node) {
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) {
494 return (AE_OK);
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) {
505 return (AE_OK);
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) {
516 ACPI_MEM_FREE (cid);
517 return (AE_OK);
520 ACPI_MEM_FREE (cid);
524 status = info->user_function (obj_handle, nesting_level, info->context, return_value);
525 return (status);
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 ******************************************************************************/
554 acpi_status
555 acpi_get_devices (
556 char *HID,
557 acpi_walk_callback user_function,
558 void *context,
559 void **return_value)
561 acpi_status status;
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;
580 info.hid = HID;
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,
595 ACPI_NS_WALK_UNLOCK,
596 acpi_ns_get_device_callback, &info,
597 return_value);
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
612 * RETURN: Status
614 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
616 ******************************************************************************/
618 acpi_status
619 acpi_attach_data (
620 acpi_handle obj_handle,
621 acpi_object_handler handler,
622 void *data)
624 struct acpi_namespace_node *node;
625 acpi_status status;
628 /* Parameter validation */
630 if (!obj_handle ||
631 !handler ||
632 !data) {
633 return (AE_BAD_PARAMETER);
636 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
637 if (ACPI_FAILURE (status)) {
638 return (status);
641 /* Convert and validate the handle */
643 node = acpi_ns_map_handle_to_node (obj_handle);
644 if (!node) {
645 status = AE_BAD_PARAMETER;
646 goto unlock_and_exit;
649 status = acpi_ns_attach_data (node, handler, data);
651 unlock_and_exit:
652 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
653 return (status);
657 /*******************************************************************************
659 * FUNCTION: acpi_detach_data
661 * PARAMETERS: obj_handle - Namespace node handle
662 * Handler - Handler used in call to acpi_attach_data
664 * RETURN: Status
666 * DESCRIPTION: Remove data that was previously attached to a node.
668 ******************************************************************************/
670 acpi_status
671 acpi_detach_data (
672 acpi_handle obj_handle,
673 acpi_object_handler handler)
675 struct acpi_namespace_node *node;
676 acpi_status status;
679 /* Parameter validation */
681 if (!obj_handle ||
682 !handler) {
683 return (AE_BAD_PARAMETER);
686 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
687 if (ACPI_FAILURE (status)) {
688 return (status);
691 /* Convert and validate the handle */
693 node = acpi_ns_map_handle_to_node (obj_handle);
694 if (!node) {
695 status = AE_BAD_PARAMETER;
696 goto unlock_and_exit;
699 status = acpi_ns_detach_data (node, handler);
701 unlock_and_exit:
702 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
703 return (status);
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
715 * RETURN: Status
717 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
719 ******************************************************************************/
721 acpi_status
722 acpi_get_data (
723 acpi_handle obj_handle,
724 acpi_object_handler handler,
725 void **data)
727 struct acpi_namespace_node *node;
728 acpi_status status;
731 /* Parameter validation */
733 if (!obj_handle ||
734 !handler ||
735 !data) {
736 return (AE_BAD_PARAMETER);
739 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
740 if (ACPI_FAILURE (status)) {
741 return (status);
744 /* Convert and validate the handle */
746 node = acpi_ns_map_handle_to_node (obj_handle);
747 if (!node) {
748 status = AE_BAD_PARAMETER;
749 goto unlock_and_exit;
752 status = acpi_ns_get_attached_data (node, handler, data);
754 unlock_and_exit:
755 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
756 return (status);