Merge with Linux 2.5.56.
[linux-2.6/linux-mips.git] / drivers / acpi / namespace / nsxfeval.c
blob8b0fa0f406f445d6cdbbefe369e25d0626694a9d
1 /*******************************************************************************
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 * ACPI Object evaluation interfaces
6 ******************************************************************************/
8 /*
9 * Copyright (C) 2000 - 2003, R. Byron Moore
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "acpi.h"
28 #include "acnamesp.h"
31 #define _COMPONENT ACPI_NAMESPACE
32 ACPI_MODULE_NAME ("nsxfeval")
35 /*******************************************************************************
37 * FUNCTION: acpi_evaluate_object_typed
39 * PARAMETERS: Handle - Object handle (optional)
40 * *Pathname - Object pathname (optional)
41 * **external_params - List of parameters to pass to method,
42 * terminated by NULL. May be NULL
43 * if no parameters are being passed.
44 * *return_buffer - Where to put method's return value (if
45 * any). If NULL, no value is returned.
46 * return_type - Expected type of return object
48 * RETURN: Status
50 * DESCRIPTION: Find and evaluate the given object, passing the given
51 * parameters if necessary. One of "Handle" or "Pathname" must
52 * be valid (non-null)
54 ******************************************************************************/
56 acpi_status
57 acpi_evaluate_object_typed (
58 acpi_handle handle,
59 acpi_string pathname,
60 struct acpi_object_list *external_params,
61 struct acpi_buffer *return_buffer,
62 acpi_object_type return_type)
64 acpi_status status;
65 u8 must_free = FALSE;
68 ACPI_FUNCTION_TRACE ("acpi_evaluate_object_typed");
71 /* Return buffer must be valid */
73 if (!return_buffer) {
74 return_ACPI_STATUS (AE_BAD_PARAMETER);
77 if (return_buffer->length == ACPI_ALLOCATE_BUFFER) {
78 must_free = TRUE;
81 /* Evaluate the object */
83 status = acpi_evaluate_object (handle, pathname, external_params, return_buffer);
84 if (ACPI_FAILURE (status)) {
85 return_ACPI_STATUS (status);
88 /* Type ANY means "don't care" */
90 if (return_type == ACPI_TYPE_ANY) {
91 return_ACPI_STATUS (AE_OK);
94 if (return_buffer->length == 0) {
95 /* Error because caller specifically asked for a return value */
97 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
98 "No return value\n"));
100 return_ACPI_STATUS (AE_NULL_OBJECT);
103 /* Examine the object type returned from evaluate_object */
105 if (((union acpi_object *) return_buffer->pointer)->type == return_type) {
106 return_ACPI_STATUS (AE_OK);
109 /* Return object type does not match requested type */
111 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
112 "Incorrect return type [%s] requested [%s]\n",
113 acpi_ut_get_type_name (((union acpi_object *) return_buffer->pointer)->type),
114 acpi_ut_get_type_name (return_type)));
116 if (must_free) {
117 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
119 acpi_os_free (return_buffer->pointer);
120 return_buffer->pointer = NULL;
123 return_buffer->length = 0;
124 return_ACPI_STATUS (AE_TYPE);
128 /*******************************************************************************
130 * FUNCTION: acpi_evaluate_object
132 * PARAMETERS: Handle - Object handle (optional)
133 * *Pathname - Object pathname (optional)
134 * **external_params - List of parameters to pass to method,
135 * terminated by NULL. May be NULL
136 * if no parameters are being passed.
137 * *return_buffer - Where to put method's return value (if
138 * any). If NULL, no value is returned.
140 * RETURN: Status
142 * DESCRIPTION: Find and evaluate the given object, passing the given
143 * parameters if necessary. One of "Handle" or "Pathname" must
144 * be valid (non-null)
146 ******************************************************************************/
148 acpi_status
149 acpi_evaluate_object (
150 acpi_handle handle,
151 acpi_string pathname,
152 struct acpi_object_list *external_params,
153 struct acpi_buffer *return_buffer)
155 acpi_status status;
156 union acpi_operand_object **internal_params = NULL;
157 union acpi_operand_object *internal_return_obj = NULL;
158 acpi_size buffer_space_needed;
159 u32 i;
162 ACPI_FUNCTION_TRACE ("acpi_evaluate_object");
166 * If there are parameters to be passed to the object
167 * (which must be a control method), the external objects
168 * must be converted to internal objects
170 if (external_params && external_params->count) {
172 * Allocate a new parameter block for the internal objects
173 * Add 1 to count to allow for null terminated internal list
175 internal_params = ACPI_MEM_CALLOCATE (((acpi_size) external_params->count + 1) *
176 sizeof (void *));
177 if (!internal_params) {
178 return_ACPI_STATUS (AE_NO_MEMORY);
182 * Convert each external object in the list to an
183 * internal object
185 for (i = 0; i < external_params->count; i++) {
186 status = acpi_ut_copy_eobject_to_iobject (&external_params->pointer[i],
187 &internal_params[i]);
188 if (ACPI_FAILURE (status)) {
189 acpi_ut_delete_internal_object_list (internal_params);
190 return_ACPI_STATUS (status);
193 internal_params[external_params->count] = NULL;
197 * Three major cases:
198 * 1) Fully qualified pathname
199 * 2) No handle, not fully qualified pathname (error)
200 * 3) Valid handle
202 if ((pathname) &&
203 (acpi_ns_valid_root_prefix (pathname[0]))) {
205 * The path is fully qualified, just evaluate by name
207 status = acpi_ns_evaluate_by_name (pathname, internal_params,
208 &internal_return_obj);
210 else if (!handle) {
212 * A handle is optional iff a fully qualified pathname
213 * is specified. Since we've already handled fully
214 * qualified names above, this is an error
216 if (!pathname) {
217 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
218 "Both Handle and Pathname are NULL\n"));
220 else {
221 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
222 "Handle is NULL and Pathname is relative\n"));
225 status = AE_BAD_PARAMETER;
227 else {
229 * We get here if we have a handle -- and if we have a
230 * pathname it is relative. The handle will be validated
231 * in the lower procedures
233 if (!pathname) {
235 * The null pathname case means the handle is for
236 * the actual object to be evaluated
238 status = acpi_ns_evaluate_by_handle (handle, internal_params,
239 &internal_return_obj);
241 else {
243 * Both a Handle and a relative Pathname
245 status = acpi_ns_evaluate_relative (handle, pathname, internal_params,
246 &internal_return_obj);
252 * If we are expecting a return value, and all went well above,
253 * copy the return value to an external object.
255 if (return_buffer) {
256 if (!internal_return_obj) {
257 return_buffer->length = 0;
259 else {
260 if (ACPI_GET_DESCRIPTOR_TYPE (internal_return_obj) == ACPI_DESC_TYPE_NAMED) {
262 * If we received a NS Node as a return object, this means that
263 * the object we are evaluating has nothing interesting to
264 * return (such as a mutex, etc.) We return an error because
265 * these types are essentially unsupported by this interface.
266 * We don't check up front because this makes it easier to add
267 * support for various types at a later date if necessary.
269 status = AE_TYPE;
270 internal_return_obj = NULL; /* No need to delete a NS Node */
271 return_buffer->length = 0;
274 if (ACPI_SUCCESS (status)) {
276 * Find out how large a buffer is needed
277 * to contain the returned object
279 status = acpi_ut_get_object_size (internal_return_obj,
280 &buffer_space_needed);
281 if (ACPI_SUCCESS (status)) {
282 /* Validate/Allocate/Clear caller buffer */
284 status = acpi_ut_initialize_buffer (return_buffer, buffer_space_needed);
285 if (ACPI_FAILURE (status)) {
287 * Caller's buffer is too small or a new one can't be allocated
289 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
290 "Needed buffer size %X, %s\n",
291 (u32) buffer_space_needed, acpi_format_exception (status)));
293 else {
295 * We have enough space for the object, build it
297 status = acpi_ut_copy_iobject_to_eobject (internal_return_obj,
298 return_buffer);
305 /* Delete the return and parameter objects */
307 if (internal_return_obj) {
309 * Delete the internal return object. (Or at least
310 * decrement the reference count by one)
312 acpi_ut_remove_reference (internal_return_obj);
316 * Free the input parameter list (if we created one),
318 if (internal_params) {
319 /* Free the allocated parameter block */
321 acpi_ut_delete_internal_object_list (internal_params);
324 return_ACPI_STATUS (status);
328 /*******************************************************************************
330 * FUNCTION: acpi_walk_namespace
332 * PARAMETERS: Type - acpi_object_type to search for
333 * start_object - Handle in namespace where search begins
334 * max_depth - Depth to which search is to reach
335 * user_function - Called when an object of "Type" is found
336 * Context - Passed to user function
337 * return_value - Location where return value of
338 * user_function is put if terminated early
340 * RETURNS Return value from the user_function if terminated early.
341 * Otherwise, returns NULL.
343 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
344 * starting (and ending) at the object specified by start_handle.
345 * The user_function is called whenever an object that matches
346 * the type parameter is found. If the user function returns
347 * a non-zero value, the search is terminated immediately and this
348 * value is returned to the caller.
350 * The point of this procedure is to provide a generic namespace
351 * walk routine that can be called from multiple places to
352 * provide multiple services; the User Function can be tailored
353 * to each task, whether it is a print function, a compare
354 * function, etc.
356 ******************************************************************************/
358 acpi_status
359 acpi_walk_namespace (
360 acpi_object_type type,
361 acpi_handle start_object,
362 u32 max_depth,
363 acpi_walk_callback user_function,
364 void *context,
365 void **return_value)
367 acpi_status status;
370 ACPI_FUNCTION_TRACE ("acpi_walk_namespace");
373 /* Parameter validation */
375 if ((type > ACPI_TYPE_EXTERNAL_MAX) ||
376 (!max_depth) ||
377 (!user_function)) {
378 return_ACPI_STATUS (AE_BAD_PARAMETER);
382 * Lock the namespace around the walk.
383 * The namespace will be unlocked/locked around each call
384 * to the user function - since this function
385 * must be allowed to make Acpi calls itself.
387 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
388 if (ACPI_FAILURE (status)) {
389 return_ACPI_STATUS (status);
392 status = acpi_ns_walk_namespace (type, start_object, max_depth, ACPI_NS_WALK_UNLOCK,
393 user_function, context, return_value);
395 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
396 return_ACPI_STATUS (status);
400 /*******************************************************************************
402 * FUNCTION: acpi_ns_get_device_callback
404 * PARAMETERS: Callback from acpi_get_device
406 * RETURN: Status
408 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
409 * present devices, or if they specified a HID, it filters based
410 * on that.
412 ******************************************************************************/
414 static acpi_status
415 acpi_ns_get_device_callback (
416 acpi_handle obj_handle,
417 u32 nesting_level,
418 void *context,
419 void **return_value)
421 acpi_status status;
422 struct acpi_namespace_node *node;
423 u32 flags;
424 struct acpi_device_id hid;
425 struct acpi_device_id cid;
426 struct acpi_get_devices_info *info;
429 info = context;
431 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
432 if (ACPI_FAILURE (status)) {
433 return (status);
436 node = acpi_ns_map_handle_to_node (obj_handle);
437 status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
438 if (ACPI_FAILURE (status)) {
439 return (status);
442 if (!node) {
443 return (AE_BAD_PARAMETER);
447 * Run _STA to determine if device is present
449 status = acpi_ut_execute_STA (node, &flags);
450 if (ACPI_FAILURE (status)) {
451 return (AE_CTRL_DEPTH);
454 if (!(flags & 0x01)) {
455 /* Don't return at the device or children of the device if not there */
456 return (AE_CTRL_DEPTH);
460 * Filter based on device HID & CID
462 if (info->hid != NULL) {
463 status = acpi_ut_execute_HID (node, &hid);
464 if (status == AE_NOT_FOUND) {
465 return (AE_OK);
467 else if (ACPI_FAILURE (status)) {
468 return (AE_CTRL_DEPTH);
471 if (ACPI_STRNCMP (hid.buffer, info->hid, sizeof (hid.buffer)) != 0) {
472 status = acpi_ut_execute_CID (node, &cid);
473 if (status == AE_NOT_FOUND) {
474 return (AE_OK);
476 else if (ACPI_FAILURE (status)) {
477 return (AE_CTRL_DEPTH);
480 /* TBD: Handle CID packages */
482 if (ACPI_STRNCMP (cid.buffer, info->hid, sizeof (cid.buffer)) != 0) {
483 return (AE_OK);
488 status = info->user_function (obj_handle, nesting_level, info->context, return_value);
489 return (status);
493 /*******************************************************************************
495 * FUNCTION: acpi_get_devices
497 * PARAMETERS: HID - HID to search for. Can be NULL.
498 * user_function - Called when a matching object is found
499 * Context - Passed to user function
500 * return_value - Location where return value of
501 * user_function is put if terminated early
503 * RETURNS Return value from the user_function if terminated early.
504 * Otherwise, returns NULL.
506 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
507 * starting (and ending) at the object specified by start_handle.
508 * The user_function is called whenever an object that matches
509 * the type parameter is found. If the user function returns
510 * a non-zero value, the search is terminated immediately and this
511 * value is returned to the caller.
513 * This is a wrapper for walk_namespace, but the callback performs
514 * additional filtering. Please see acpi_get_device_callback.
516 ******************************************************************************/
518 acpi_status
519 acpi_get_devices (
520 char *HID,
521 acpi_walk_callback user_function,
522 void *context,
523 void **return_value)
525 acpi_status status;
526 struct acpi_get_devices_info info;
529 ACPI_FUNCTION_TRACE ("acpi_get_devices");
532 /* Parameter validation */
534 if (!user_function) {
535 return_ACPI_STATUS (AE_BAD_PARAMETER);
539 * We're going to call their callback from OUR callback, so we need
540 * to know what it is, and their context parameter.
542 info.context = context;
543 info.user_function = user_function;
544 info.hid = HID;
547 * Lock the namespace around the walk.
548 * The namespace will be unlocked/locked around each call
549 * to the user function - since this function
550 * must be allowed to make Acpi calls itself.
552 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
553 if (ACPI_FAILURE (status)) {
554 return_ACPI_STATUS (status);
557 status = acpi_ns_walk_namespace (ACPI_TYPE_DEVICE,
558 ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
559 ACPI_NS_WALK_UNLOCK,
560 acpi_ns_get_device_callback, &info,
561 return_value);
563 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
564 return_ACPI_STATUS (status);
568 /*******************************************************************************
570 * FUNCTION: acpi_attach_data
572 * PARAMETERS: obj_handle - Namespace node
573 * Handler - Handler for this attachment
574 * Data - Pointer to data to be attached
576 * RETURN: Status
578 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
580 ******************************************************************************/
582 acpi_status
583 acpi_attach_data (
584 acpi_handle obj_handle,
585 acpi_object_handler handler,
586 void *data)
588 struct acpi_namespace_node *node;
589 acpi_status status;
592 /* Parameter validation */
594 if (!obj_handle ||
595 !handler ||
596 !data) {
597 return (AE_BAD_PARAMETER);
600 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
601 if (ACPI_FAILURE (status)) {
602 return (status);
605 /* Convert and validate the handle */
607 node = acpi_ns_map_handle_to_node (obj_handle);
608 if (!node) {
609 status = AE_BAD_PARAMETER;
610 goto unlock_and_exit;
613 status = acpi_ns_attach_data (node, handler, data);
615 unlock_and_exit:
616 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
617 return (status);
621 /*******************************************************************************
623 * FUNCTION: acpi_detach_data
625 * PARAMETERS: obj_handle - Namespace node handle
626 * Handler - Handler used in call to acpi_attach_data
628 * RETURN: Status
630 * DESCRIPTION: Remove data that was previously attached to a node.
632 ******************************************************************************/
634 acpi_status
635 acpi_detach_data (
636 acpi_handle obj_handle,
637 acpi_object_handler handler)
639 struct acpi_namespace_node *node;
640 acpi_status status;
643 /* Parameter validation */
645 if (!obj_handle ||
646 !handler) {
647 return (AE_BAD_PARAMETER);
650 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
651 if (ACPI_FAILURE (status)) {
652 return (status);
655 /* Convert and validate the handle */
657 node = acpi_ns_map_handle_to_node (obj_handle);
658 if (!node) {
659 status = AE_BAD_PARAMETER;
660 goto unlock_and_exit;
663 status = acpi_ns_detach_data (node, handler);
665 unlock_and_exit:
666 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
667 return (status);
671 /*******************************************************************************
673 * FUNCTION: acpi_get_data
675 * PARAMETERS: obj_handle - Namespace node
676 * Handler - Handler used in call to attach_data
677 * Data - Where the data is returned
679 * RETURN: Status
681 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
683 ******************************************************************************/
685 acpi_status
686 acpi_get_data (
687 acpi_handle obj_handle,
688 acpi_object_handler handler,
689 void **data)
691 struct acpi_namespace_node *node;
692 acpi_status status;
695 /* Parameter validation */
697 if (!obj_handle ||
698 !handler ||
699 !data) {
700 return (AE_BAD_PARAMETER);
703 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
704 if (ACPI_FAILURE (status)) {
705 return (status);
708 /* Convert and validate the handle */
710 node = acpi_ns_map_handle_to_node (obj_handle);
711 if (!node) {
712 status = AE_BAD_PARAMETER;
713 goto unlock_and_exit;
716 status = acpi_ns_get_attached_data (node, handler, data);
718 unlock_and_exit:
719 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
720 return (status);