[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / drivers / acpi / namespace / nsxfeval.c
blob1dc995586cbef4615a18cdd5c1b12e9f8012c029
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 ******************************************************************************/
76 #ifdef ACPI_FUTURE_USAGE
77 acpi_status
78 acpi_evaluate_object_typed (
79 acpi_handle handle,
80 acpi_string pathname,
81 struct acpi_object_list *external_params,
82 struct acpi_buffer *return_buffer,
83 acpi_object_type return_type)
85 acpi_status status;
86 u8 must_free = FALSE;
89 ACPI_FUNCTION_TRACE ("acpi_evaluate_object_typed");
92 /* Return buffer must be valid */
94 if (!return_buffer) {
95 return_ACPI_STATUS (AE_BAD_PARAMETER);
98 if (return_buffer->length == ACPI_ALLOCATE_BUFFER) {
99 must_free = TRUE;
102 /* Evaluate the object */
104 status = acpi_evaluate_object (handle, pathname, external_params, return_buffer);
105 if (ACPI_FAILURE (status)) {
106 return_ACPI_STATUS (status);
109 /* Type ANY means "don't care" */
111 if (return_type == ACPI_TYPE_ANY) {
112 return_ACPI_STATUS (AE_OK);
115 if (return_buffer->length == 0) {
116 /* Error because caller specifically asked for a return value */
118 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
119 "No return value\n"));
121 return_ACPI_STATUS (AE_NULL_OBJECT);
124 /* Examine the object type returned from evaluate_object */
126 if (((union acpi_object *) return_buffer->pointer)->type == return_type) {
127 return_ACPI_STATUS (AE_OK);
130 /* Return object type does not match requested type */
132 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
133 "Incorrect return type [%s] requested [%s]\n",
134 acpi_ut_get_type_name (((union acpi_object *) return_buffer->pointer)->type),
135 acpi_ut_get_type_name (return_type)));
137 if (must_free) {
138 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
140 acpi_os_free (return_buffer->pointer);
141 return_buffer->pointer = NULL;
144 return_buffer->length = 0;
145 return_ACPI_STATUS (AE_TYPE);
147 #endif /* ACPI_FUTURE_USAGE */
150 /*******************************************************************************
152 * FUNCTION: acpi_evaluate_object
154 * PARAMETERS: Handle - Object handle (optional)
155 * Pathname - Object pathname (optional)
156 * external_params - List of parameters to pass to method,
157 * terminated by NULL. May be NULL
158 * if no parameters are being passed.
159 * return_buffer - Where to put method's return value (if
160 * any). If NULL, no value is returned.
162 * RETURN: Status
164 * DESCRIPTION: Find and evaluate the given object, passing the given
165 * parameters if necessary. One of "Handle" or "Pathname" must
166 * be valid (non-null)
168 ******************************************************************************/
170 acpi_status
171 acpi_evaluate_object (
172 acpi_handle handle,
173 acpi_string pathname,
174 struct acpi_object_list *external_params,
175 struct acpi_buffer *return_buffer)
177 acpi_status status;
178 acpi_status status2;
179 struct acpi_parameter_info info;
180 acpi_size buffer_space_needed;
181 u32 i;
184 ACPI_FUNCTION_TRACE ("acpi_evaluate_object");
187 info.node = handle;
188 info.parameters = NULL;
189 info.return_object = NULL;
190 info.parameter_type = ACPI_PARAM_ARGS;
193 * If there are parameters to be passed to the object
194 * (which must be a control method), the external objects
195 * must be converted to internal objects
197 if (external_params && external_params->count) {
199 * Allocate a new parameter block for the internal objects
200 * Add 1 to count to allow for null terminated internal list
202 info.parameters = ACPI_MEM_CALLOCATE (
203 ((acpi_size) external_params->count + 1) *
204 sizeof (void *));
205 if (!info.parameters) {
206 return_ACPI_STATUS (AE_NO_MEMORY);
210 * Convert each external object in the list to an
211 * internal object
213 for (i = 0; i < external_params->count; i++) {
214 status = acpi_ut_copy_eobject_to_iobject (&external_params->pointer[i],
215 &info.parameters[i]);
216 if (ACPI_FAILURE (status)) {
217 acpi_ut_delete_internal_object_list (info.parameters);
218 return_ACPI_STATUS (status);
221 info.parameters[external_params->count] = NULL;
226 * Three major cases:
227 * 1) Fully qualified pathname
228 * 2) No handle, not fully qualified pathname (error)
229 * 3) Valid handle
231 if ((pathname) &&
232 (acpi_ns_valid_root_prefix (pathname[0]))) {
234 * The path is fully qualified, just evaluate by name
236 status = acpi_ns_evaluate_by_name (pathname, &info);
238 else if (!handle) {
240 * A handle is optional iff a fully qualified pathname
241 * is specified. Since we've already handled fully
242 * qualified names above, this is an error
244 if (!pathname) {
245 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
246 "Both Handle and Pathname are NULL\n"));
248 else {
249 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
250 "Handle is NULL and Pathname is relative\n"));
253 status = AE_BAD_PARAMETER;
255 else {
257 * We get here if we have a handle -- and if we have a
258 * pathname it is relative. The handle will be validated
259 * in the lower procedures
261 if (!pathname) {
263 * The null pathname case means the handle is for
264 * the actual object to be evaluated
266 status = acpi_ns_evaluate_by_handle (&info);
268 else {
270 * Both a Handle and a relative Pathname
272 status = acpi_ns_evaluate_relative (pathname, &info);
278 * If we are expecting a return value, and all went well above,
279 * copy the return value to an external object.
281 if (return_buffer) {
282 if (!info.return_object) {
283 return_buffer->length = 0;
285 else {
286 if (ACPI_GET_DESCRIPTOR_TYPE (info.return_object) == ACPI_DESC_TYPE_NAMED) {
288 * If we received a NS Node as a return object, this means that
289 * the object we are evaluating has nothing interesting to
290 * return (such as a mutex, etc.) We return an error because
291 * these types are essentially unsupported by this interface.
292 * We don't check up front because this makes it easier to add
293 * support for various types at a later date if necessary.
295 status = AE_TYPE;
296 info.return_object = NULL; /* No need to delete a NS Node */
297 return_buffer->length = 0;
300 if (ACPI_SUCCESS (status)) {
302 * Find out how large a buffer is needed
303 * to contain the returned object
305 status = acpi_ut_get_object_size (info.return_object,
306 &buffer_space_needed);
307 if (ACPI_SUCCESS (status)) {
308 /* Validate/Allocate/Clear caller buffer */
310 status = acpi_ut_initialize_buffer (return_buffer, buffer_space_needed);
311 if (ACPI_FAILURE (status)) {
313 * Caller's buffer is too small or a new one can't be allocated
315 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
316 "Needed buffer size %X, %s\n",
317 (u32) buffer_space_needed,
318 acpi_format_exception (status)));
320 else {
322 * We have enough space for the object, build it
324 status = acpi_ut_copy_iobject_to_eobject (info.return_object,
325 return_buffer);
332 if (info.return_object) {
334 * Delete the internal return object. NOTE: Interpreter
335 * must be locked to avoid race condition.
337 status2 = acpi_ex_enter_interpreter ();
338 if (ACPI_SUCCESS (status2)) {
340 * Delete the internal return object. (Or at least
341 * decrement the reference count by one)
343 acpi_ut_remove_reference (info.return_object);
344 acpi_ex_exit_interpreter ();
349 * Free the input parameter list (if we created one),
351 if (info.parameters) {
352 /* Free the allocated parameter block */
354 acpi_ut_delete_internal_object_list (info.parameters);
357 return_ACPI_STATUS (status);
359 EXPORT_SYMBOL(acpi_evaluate_object);
362 /*******************************************************************************
364 * FUNCTION: acpi_walk_namespace
366 * PARAMETERS: Type - acpi_object_type to search for
367 * start_object - Handle in namespace where search begins
368 * max_depth - Depth to which search is to reach
369 * user_function - Called when an object of "Type" is found
370 * Context - Passed to user function
371 * return_value - Location where return value of
372 * user_function is put if terminated early
374 * RETURNS Return value from the user_function if terminated early.
375 * Otherwise, returns NULL.
377 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
378 * starting (and ending) at the object specified by start_handle.
379 * The user_function is called whenever an object that matches
380 * the type parameter is found. If the user function returns
381 * a non-zero value, the search is terminated immediately and this
382 * value is returned to the caller.
384 * The point of this procedure is to provide a generic namespace
385 * walk routine that can be called from multiple places to
386 * provide multiple services; the User Function can be tailored
387 * to each task, whether it is a print function, a compare
388 * function, etc.
390 ******************************************************************************/
392 acpi_status
393 acpi_walk_namespace (
394 acpi_object_type type,
395 acpi_handle start_object,
396 u32 max_depth,
397 acpi_walk_callback user_function,
398 void *context,
399 void **return_value)
401 acpi_status status;
404 ACPI_FUNCTION_TRACE ("acpi_walk_namespace");
407 /* Parameter validation */
409 if ((type > ACPI_TYPE_EXTERNAL_MAX) ||
410 (!max_depth) ||
411 (!user_function)) {
412 return_ACPI_STATUS (AE_BAD_PARAMETER);
416 * Lock the namespace around the walk.
417 * The namespace will be unlocked/locked around each call
418 * to the user function - since this function
419 * must be allowed to make Acpi calls itself.
421 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
422 if (ACPI_FAILURE (status)) {
423 return_ACPI_STATUS (status);
426 status = acpi_ns_walk_namespace (type, start_object, max_depth, ACPI_NS_WALK_UNLOCK,
427 user_function, context, return_value);
429 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
430 return_ACPI_STATUS (status);
432 EXPORT_SYMBOL(acpi_walk_namespace);
435 /*******************************************************************************
437 * FUNCTION: acpi_ns_get_device_callback
439 * PARAMETERS: Callback from acpi_get_device
441 * RETURN: Status
443 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
444 * present devices, or if they specified a HID, it filters based
445 * on that.
447 ******************************************************************************/
449 static acpi_status
450 acpi_ns_get_device_callback (
451 acpi_handle obj_handle,
452 u32 nesting_level,
453 void *context,
454 void **return_value)
456 struct acpi_get_devices_info *info = context;
457 acpi_status status;
458 struct acpi_namespace_node *node;
459 u32 flags;
460 struct acpi_device_id hid;
461 struct acpi_compatible_id_list *cid;
462 acpi_native_uint i;
465 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
466 if (ACPI_FAILURE (status)) {
467 return (status);
470 node = acpi_ns_map_handle_to_node (obj_handle);
471 status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
472 if (ACPI_FAILURE (status)) {
473 return (status);
476 if (!node) {
477 return (AE_BAD_PARAMETER);
480 /* Run _STA to determine if device is present */
482 status = acpi_ut_execute_STA (node, &flags);
483 if (ACPI_FAILURE (status)) {
484 return (AE_CTRL_DEPTH);
487 if (!(flags & 0x01)) {
488 /* Don't return at the device or children of the device if not there */
490 return (AE_CTRL_DEPTH);
493 /* Filter based on device HID & CID */
495 if (info->hid != NULL) {
496 status = acpi_ut_execute_HID (node, &hid);
497 if (status == AE_NOT_FOUND) {
498 return (AE_OK);
500 else if (ACPI_FAILURE (status)) {
501 return (AE_CTRL_DEPTH);
504 if (ACPI_STRNCMP (hid.value, info->hid, sizeof (hid.value)) != 0) {
505 /* Get the list of Compatible IDs */
507 status = acpi_ut_execute_CID (node, &cid);
508 if (status == AE_NOT_FOUND) {
509 return (AE_OK);
511 else if (ACPI_FAILURE (status)) {
512 return (AE_CTRL_DEPTH);
515 /* Walk the CID list */
517 for (i = 0; i < cid->count; i++) {
518 if (ACPI_STRNCMP (cid->id[i].value, info->hid,
519 sizeof (struct acpi_compatible_id)) != 0) {
520 ACPI_MEM_FREE (cid);
521 return (AE_OK);
524 ACPI_MEM_FREE (cid);
528 status = info->user_function (obj_handle, nesting_level, info->context, return_value);
529 return (status);
533 /*******************************************************************************
535 * FUNCTION: acpi_get_devices
537 * PARAMETERS: HID - HID to search for. Can be NULL.
538 * user_function - Called when a matching object is found
539 * Context - Passed to user function
540 * return_value - Location where return value of
541 * user_function is put if terminated early
543 * RETURNS Return value from the user_function if terminated early.
544 * Otherwise, returns NULL.
546 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
547 * starting (and ending) at the object specified by start_handle.
548 * The user_function is called whenever an object of type
549 * Device is found. If the user function returns
550 * a non-zero value, the search is terminated immediately and this
551 * value is returned to the caller.
553 * This is a wrapper for walk_namespace, but the callback performs
554 * additional filtering. Please see acpi_get_device_callback.
556 ******************************************************************************/
558 acpi_status
559 acpi_get_devices (
560 char *HID,
561 acpi_walk_callback user_function,
562 void *context,
563 void **return_value)
565 acpi_status status;
566 struct acpi_get_devices_info info;
569 ACPI_FUNCTION_TRACE ("acpi_get_devices");
572 /* Parameter validation */
574 if (!user_function) {
575 return_ACPI_STATUS (AE_BAD_PARAMETER);
579 * We're going to call their callback from OUR callback, so we need
580 * to know what it is, and their context parameter.
582 info.context = context;
583 info.user_function = user_function;
584 info.hid = HID;
587 * Lock the namespace around the walk.
588 * The namespace will be unlocked/locked around each call
589 * to the user function - since this function
590 * must be allowed to make Acpi calls itself.
592 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
593 if (ACPI_FAILURE (status)) {
594 return_ACPI_STATUS (status);
597 status = acpi_ns_walk_namespace (ACPI_TYPE_DEVICE,
598 ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
599 ACPI_NS_WALK_UNLOCK,
600 acpi_ns_get_device_callback, &info,
601 return_value);
603 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
604 return_ACPI_STATUS (status);
606 EXPORT_SYMBOL(acpi_get_devices);
609 /*******************************************************************************
611 * FUNCTION: acpi_attach_data
613 * PARAMETERS: obj_handle - Namespace node
614 * Handler - Handler for this attachment
615 * Data - Pointer to data to be attached
617 * RETURN: Status
619 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
621 ******************************************************************************/
623 acpi_status
624 acpi_attach_data (
625 acpi_handle obj_handle,
626 acpi_object_handler handler,
627 void *data)
629 struct acpi_namespace_node *node;
630 acpi_status status;
633 /* Parameter validation */
635 if (!obj_handle ||
636 !handler ||
637 !data) {
638 return (AE_BAD_PARAMETER);
641 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
642 if (ACPI_FAILURE (status)) {
643 return (status);
646 /* Convert and validate the handle */
648 node = acpi_ns_map_handle_to_node (obj_handle);
649 if (!node) {
650 status = AE_BAD_PARAMETER;
651 goto unlock_and_exit;
654 status = acpi_ns_attach_data (node, handler, data);
656 unlock_and_exit:
657 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
658 return (status);
662 /*******************************************************************************
664 * FUNCTION: acpi_detach_data
666 * PARAMETERS: obj_handle - Namespace node handle
667 * Handler - Handler used in call to acpi_attach_data
669 * RETURN: Status
671 * DESCRIPTION: Remove data that was previously attached to a node.
673 ******************************************************************************/
675 acpi_status
676 acpi_detach_data (
677 acpi_handle obj_handle,
678 acpi_object_handler handler)
680 struct acpi_namespace_node *node;
681 acpi_status status;
684 /* Parameter validation */
686 if (!obj_handle ||
687 !handler) {
688 return (AE_BAD_PARAMETER);
691 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
692 if (ACPI_FAILURE (status)) {
693 return (status);
696 /* Convert and validate the handle */
698 node = acpi_ns_map_handle_to_node (obj_handle);
699 if (!node) {
700 status = AE_BAD_PARAMETER;
701 goto unlock_and_exit;
704 status = acpi_ns_detach_data (node, handler);
706 unlock_and_exit:
707 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
708 return (status);
712 /*******************************************************************************
714 * FUNCTION: acpi_get_data
716 * PARAMETERS: obj_handle - Namespace node
717 * Handler - Handler used in call to attach_data
718 * Data - Where the data is returned
720 * RETURN: Status
722 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
724 ******************************************************************************/
726 acpi_status
727 acpi_get_data (
728 acpi_handle obj_handle,
729 acpi_object_handler handler,
730 void **data)
732 struct acpi_namespace_node *node;
733 acpi_status status;
736 /* Parameter validation */
738 if (!obj_handle ||
739 !handler ||
740 !data) {
741 return (AE_BAD_PARAMETER);
744 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
745 if (ACPI_FAILURE (status)) {
746 return (status);
749 /* Convert and validate the handle */
751 node = acpi_ns_map_handle_to_node (obj_handle);
752 if (!node) {
753 status = AE_BAD_PARAMETER;
754 goto unlock_and_exit;
757 status = acpi_ns_get_attached_data (node, handler, data);
759 unlock_and_exit:
760 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
761 return (status);