Merge branch 'master'
[deliverable/linux.git] / drivers / acpi / namespace / nsxfeval.c
1 /*******************************************************************************
2 *
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 * ACPI Object evaluation interfaces
5 *
6 ******************************************************************************/
7
8 /*
9 * Copyright (C) 2000 - 2006, R. Byron Moore
10 * All rights reserved.
11 *
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.
26 *
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.
30 *
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.
43 */
44
45 #include <linux/module.h>
46
47 #include <acpi/acpi.h>
48 #include <acpi/acnamesp.h>
49 #include <acpi/acinterp.h>
50
51 #define _COMPONENT ACPI_NAMESPACE
52 ACPI_MODULE_NAME("nsxfeval")
53
54 /*******************************************************************************
55 *
56 * FUNCTION: acpi_evaluate_object_typed
57 *
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
66 *
67 * RETURN: Status
68 *
69 * DESCRIPTION: Find and evaluate the given object, passing the given
70 * parameters if necessary. One of "Handle" or "Pathname" must
71 * be valid (non-null)
72 *
73 ******************************************************************************/
74 #ifdef ACPI_FUTURE_USAGE
75 acpi_status
76 acpi_evaluate_object_typed(acpi_handle handle,
77 acpi_string pathname,
78 struct acpi_object_list *external_params,
79 struct acpi_buffer *return_buffer,
80 acpi_object_type return_type)
81 {
82 acpi_status status;
83 u8 must_free = FALSE;
84
85 ACPI_FUNCTION_TRACE("acpi_evaluate_object_typed");
86
87 /* Return buffer must be valid */
88
89 if (!return_buffer) {
90 return_ACPI_STATUS(AE_BAD_PARAMETER);
91 }
92
93 if (return_buffer->length == ACPI_ALLOCATE_BUFFER) {
94 must_free = TRUE;
95 }
96
97 /* Evaluate the object */
98
99 status =
100 acpi_evaluate_object(handle, pathname, external_params,
101 return_buffer);
102 if (ACPI_FAILURE(status)) {
103 return_ACPI_STATUS(status);
104 }
105
106 /* Type ANY means "don't care" */
107
108 if (return_type == ACPI_TYPE_ANY) {
109 return_ACPI_STATUS(AE_OK);
110 }
111
112 if (return_buffer->length == 0) {
113 /* Error because caller specifically asked for a return value */
114
115 ACPI_ERROR((AE_INFO, "No return value"));
116 return_ACPI_STATUS(AE_NULL_OBJECT);
117 }
118
119 /* Examine the object type returned from evaluate_object */
120
121 if (((union acpi_object *)return_buffer->pointer)->type == return_type) {
122 return_ACPI_STATUS(AE_OK);
123 }
124
125 /* Return object type does not match requested type */
126
127 ACPI_ERROR((AE_INFO,
128 "Incorrect return type [%s] requested [%s]",
129 acpi_ut_get_type_name(((union acpi_object *)return_buffer->
130 pointer)->type),
131 acpi_ut_get_type_name(return_type)));
132
133 if (must_free) {
134 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
135
136 acpi_os_free(return_buffer->pointer);
137 return_buffer->pointer = NULL;
138 }
139
140 return_buffer->length = 0;
141 return_ACPI_STATUS(AE_TYPE);
142 }
143 #endif /* ACPI_FUTURE_USAGE */
144
145 /*******************************************************************************
146 *
147 * FUNCTION: acpi_evaluate_object
148 *
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.
156 *
157 * RETURN: Status
158 *
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)
162 *
163 ******************************************************************************/
164
165 acpi_status
166 acpi_evaluate_object(acpi_handle handle,
167 acpi_string pathname,
168 struct acpi_object_list *external_params,
169 struct acpi_buffer *return_buffer)
170 {
171 acpi_status status;
172 acpi_status status2;
173 struct acpi_parameter_info info;
174 acpi_size buffer_space_needed;
175 u32 i;
176
177 ACPI_FUNCTION_TRACE("acpi_evaluate_object");
178
179 info.node = handle;
180 info.parameters = NULL;
181 info.return_object = NULL;
182 info.parameter_type = ACPI_PARAM_ARGS;
183
184 /*
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
188 */
189 if (external_params && external_params->count) {
190 /*
191 * Allocate a new parameter block for the internal objects
192 * Add 1 to count to allow for null terminated internal list
193 */
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);
199 }
200
201 /*
202 * Convert each external object in the list to an
203 * internal object
204 */
205 for (i = 0; i < external_params->count; i++) {
206 status =
207 acpi_ut_copy_eobject_to_iobject(&external_params->
208 pointer[i],
209 &info.
210 parameters[i]);
211 if (ACPI_FAILURE(status)) {
212 acpi_ut_delete_internal_object_list(info.
213 parameters);
214 return_ACPI_STATUS(status);
215 }
216 }
217 info.parameters[external_params->count] = NULL;
218 }
219
220 /*
221 * Three major cases:
222 * 1) Fully qualified pathname
223 * 2) No handle, not fully qualified pathname (error)
224 * 3) Valid handle
225 */
226 if ((pathname) && (acpi_ns_valid_root_prefix(pathname[0]))) {
227 /*
228 * The path is fully qualified, just evaluate by name
229 */
230 status = acpi_ns_evaluate_by_name(pathname, &info);
231 } else if (!handle) {
232 /*
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
236 */
237 if (!pathname) {
238 ACPI_ERROR((AE_INFO,
239 "Both Handle and Pathname are NULL"));
240 } else {
241 ACPI_ERROR((AE_INFO,
242 "Handle is NULL and Pathname is relative"));
243 }
244
245 status = AE_BAD_PARAMETER;
246 } else {
247 /*
248 * We get here if we have a handle -- and if we have a
249 * pathname it is relative. The handle will be validated
250 * in the lower procedures
251 */
252 if (!pathname) {
253 /*
254 * The null pathname case means the handle is for
255 * the actual object to be evaluated
256 */
257 status = acpi_ns_evaluate_by_handle(&info);
258 } else {
259 /*
260 * Both a Handle and a relative Pathname
261 */
262 status = acpi_ns_evaluate_relative(pathname, &info);
263 }
264 }
265
266 /*
267 * If we are expecting a return value, and all went well above,
268 * copy the return value to an external object.
269 */
270 if (return_buffer) {
271 if (!info.return_object) {
272 return_buffer->length = 0;
273 } else {
274 if (ACPI_GET_DESCRIPTOR_TYPE(info.return_object) ==
275 ACPI_DESC_TYPE_NAMED) {
276 /*
277 * If we received a NS Node as a return object, this means that
278 * the object we are evaluating has nothing interesting to
279 * return (such as a mutex, etc.) We return an error because
280 * these types are essentially unsupported by this interface.
281 * We don't check up front because this makes it easier to add
282 * support for various types at a later date if necessary.
283 */
284 status = AE_TYPE;
285 info.return_object = NULL; /* No need to delete a NS Node */
286 return_buffer->length = 0;
287 }
288
289 if (ACPI_SUCCESS(status)) {
290 /*
291 * Find out how large a buffer is needed
292 * to contain the returned object
293 */
294 status =
295 acpi_ut_get_object_size(info.return_object,
296 &buffer_space_needed);
297 if (ACPI_SUCCESS(status)) {
298 /* Validate/Allocate/Clear caller buffer */
299
300 status =
301 acpi_ut_initialize_buffer
302 (return_buffer,
303 buffer_space_needed);
304 if (ACPI_FAILURE(status)) {
305 /*
306 * Caller's buffer is too small or a new one can't be allocated
307 */
308 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
309 "Needed buffer size %X, %s\n",
310 (u32)
311 buffer_space_needed,
312 acpi_format_exception
313 (status)));
314 } else {
315 /*
316 * We have enough space for the object, build it
317 */
318 status =
319 acpi_ut_copy_iobject_to_eobject
320 (info.return_object,
321 return_buffer);
322 }
323 }
324 }
325 }
326 }
327
328 if (info.return_object) {
329 /*
330 * Delete the internal return object. NOTE: Interpreter
331 * must be locked to avoid race condition.
332 */
333 status2 = acpi_ex_enter_interpreter();
334 if (ACPI_SUCCESS(status2)) {
335 /*
336 * Delete the internal return object. (Or at least
337 * decrement the reference count by one)
338 */
339 acpi_ut_remove_reference(info.return_object);
340 acpi_ex_exit_interpreter();
341 }
342 }
343
344 /*
345 * Free the input parameter list (if we created one),
346 */
347 if (info.parameters) {
348 /* Free the allocated parameter block */
349
350 acpi_ut_delete_internal_object_list(info.parameters);
351 }
352
353 return_ACPI_STATUS(status);
354 }
355
356 EXPORT_SYMBOL(acpi_evaluate_object);
357
358 /*******************************************************************************
359 *
360 * FUNCTION: acpi_walk_namespace
361 *
362 * PARAMETERS: Type - acpi_object_type to search for
363 * start_object - Handle in namespace where search begins
364 * max_depth - Depth to which search is to reach
365 * user_function - Called when an object of "Type" is found
366 * Context - Passed to user function
367 * return_value - Location where return value of
368 * user_function is put if terminated early
369 *
370 * RETURNS Return value from the user_function if terminated early.
371 * Otherwise, returns NULL.
372 *
373 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
374 * starting (and ending) at the object specified by start_handle.
375 * The user_function is called whenever an object that matches
376 * the type parameter is found. If the user function returns
377 * a non-zero value, the search is terminated immediately and this
378 * value is returned to the caller.
379 *
380 * The point of this procedure is to provide a generic namespace
381 * walk routine that can be called from multiple places to
382 * provide multiple services; the User Function can be tailored
383 * to each task, whether it is a print function, a compare
384 * function, etc.
385 *
386 ******************************************************************************/
387
388 acpi_status
389 acpi_walk_namespace(acpi_object_type type,
390 acpi_handle start_object,
391 u32 max_depth,
392 acpi_walk_callback user_function,
393 void *context, void **return_value)
394 {
395 acpi_status status;
396
397 ACPI_FUNCTION_TRACE("acpi_walk_namespace");
398
399 /* Parameter validation */
400
401 if ((type > ACPI_TYPE_LOCAL_MAX) || (!max_depth) || (!user_function)) {
402 return_ACPI_STATUS(AE_BAD_PARAMETER);
403 }
404
405 /*
406 * Lock the namespace around the walk.
407 * The namespace will be unlocked/locked around each call
408 * to the user function - since this function
409 * must be allowed to make Acpi calls itself.
410 */
411 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
412 if (ACPI_FAILURE(status)) {
413 return_ACPI_STATUS(status);
414 }
415
416 status = acpi_ns_walk_namespace(type, start_object, max_depth,
417 ACPI_NS_WALK_UNLOCK,
418 user_function, context, return_value);
419
420 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
421 return_ACPI_STATUS(status);
422 }
423
424 EXPORT_SYMBOL(acpi_walk_namespace);
425
426 /*******************************************************************************
427 *
428 * FUNCTION: acpi_ns_get_device_callback
429 *
430 * PARAMETERS: Callback from acpi_get_device
431 *
432 * RETURN: Status
433 *
434 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
435 * present devices, or if they specified a HID, it filters based
436 * on that.
437 *
438 ******************************************************************************/
439
440 static acpi_status
441 acpi_ns_get_device_callback(acpi_handle obj_handle,
442 u32 nesting_level,
443 void *context, void **return_value)
444 {
445 struct acpi_get_devices_info *info = context;
446 acpi_status status;
447 struct acpi_namespace_node *node;
448 u32 flags;
449 struct acpi_device_id hid;
450 struct acpi_compatible_id_list *cid;
451 acpi_native_uint i;
452
453 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
454 if (ACPI_FAILURE(status)) {
455 return (status);
456 }
457
458 node = acpi_ns_map_handle_to_node(obj_handle);
459 status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
460 if (ACPI_FAILURE(status)) {
461 return (status);
462 }
463
464 if (!node) {
465 return (AE_BAD_PARAMETER);
466 }
467
468 /* Run _STA to determine if device is present */
469
470 status = acpi_ut_execute_STA(node, &flags);
471 if (ACPI_FAILURE(status)) {
472 return (AE_CTRL_DEPTH);
473 }
474
475 if (!(flags & ACPI_STA_DEVICE_PRESENT)) {
476 /* Don't examine children of the device if not present */
477
478 return (AE_CTRL_DEPTH);
479 }
480
481 /* Filter based on device HID & CID */
482
483 if (info->hid != NULL) {
484 status = acpi_ut_execute_HID(node, &hid);
485 if (status == AE_NOT_FOUND) {
486 return (AE_OK);
487 } else if (ACPI_FAILURE(status)) {
488 return (AE_CTRL_DEPTH);
489 }
490
491 if (ACPI_STRNCMP(hid.value, info->hid, sizeof(hid.value)) != 0) {
492 /* Get the list of Compatible IDs */
493
494 status = acpi_ut_execute_CID(node, &cid);
495 if (status == AE_NOT_FOUND) {
496 return (AE_OK);
497 } else if (ACPI_FAILURE(status)) {
498 return (AE_CTRL_DEPTH);
499 }
500
501 /* Walk the CID list */
502
503 for (i = 0; i < cid->count; i++) {
504 if (ACPI_STRNCMP(cid->id[i].value, info->hid,
505 sizeof(struct
506 acpi_compatible_id)) !=
507 0) {
508 ACPI_MEM_FREE(cid);
509 return (AE_OK);
510 }
511 }
512 ACPI_MEM_FREE(cid);
513 }
514 }
515
516 status = info->user_function(obj_handle, nesting_level, info->context,
517 return_value);
518 return (status);
519 }
520
521 /*******************************************************************************
522 *
523 * FUNCTION: acpi_get_devices
524 *
525 * PARAMETERS: HID - HID to search for. Can be NULL.
526 * user_function - Called when a matching object is found
527 * Context - Passed to user function
528 * return_value - Location where return value of
529 * user_function is put if terminated early
530 *
531 * RETURNS Return value from the user_function if terminated early.
532 * Otherwise, returns NULL.
533 *
534 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
535 * starting (and ending) at the object specified by start_handle.
536 * The user_function is called whenever an object of type
537 * Device is found. If the user function returns
538 * a non-zero value, the search is terminated immediately and this
539 * value is returned to the caller.
540 *
541 * This is a wrapper for walk_namespace, but the callback performs
542 * additional filtering. Please see acpi_get_device_callback.
543 *
544 ******************************************************************************/
545
546 acpi_status
547 acpi_get_devices(char *HID,
548 acpi_walk_callback user_function,
549 void *context, void **return_value)
550 {
551 acpi_status status;
552 struct acpi_get_devices_info info;
553
554 ACPI_FUNCTION_TRACE("acpi_get_devices");
555
556 /* Parameter validation */
557
558 if (!user_function) {
559 return_ACPI_STATUS(AE_BAD_PARAMETER);
560 }
561
562 /*
563 * We're going to call their callback from OUR callback, so we need
564 * to know what it is, and their context parameter.
565 */
566 info.context = context;
567 info.user_function = user_function;
568 info.hid = HID;
569
570 /*
571 * Lock the namespace around the walk.
572 * The namespace will be unlocked/locked around each call
573 * to the user function - since this function
574 * must be allowed to make Acpi calls itself.
575 */
576 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
577 if (ACPI_FAILURE(status)) {
578 return_ACPI_STATUS(status);
579 }
580
581 status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE,
582 ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
583 ACPI_NS_WALK_UNLOCK,
584 acpi_ns_get_device_callback, &info,
585 return_value);
586
587 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
588 return_ACPI_STATUS(status);
589 }
590
591 EXPORT_SYMBOL(acpi_get_devices);
592
593 /*******************************************************************************
594 *
595 * FUNCTION: acpi_attach_data
596 *
597 * PARAMETERS: obj_handle - Namespace node
598 * Handler - Handler for this attachment
599 * Data - Pointer to data to be attached
600 *
601 * RETURN: Status
602 *
603 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
604 *
605 ******************************************************************************/
606
607 acpi_status
608 acpi_attach_data(acpi_handle obj_handle,
609 acpi_object_handler handler, void *data)
610 {
611 struct acpi_namespace_node *node;
612 acpi_status status;
613
614 /* Parameter validation */
615
616 if (!obj_handle || !handler || !data) {
617 return (AE_BAD_PARAMETER);
618 }
619
620 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
621 if (ACPI_FAILURE(status)) {
622 return (status);
623 }
624
625 /* Convert and validate the handle */
626
627 node = acpi_ns_map_handle_to_node(obj_handle);
628 if (!node) {
629 status = AE_BAD_PARAMETER;
630 goto unlock_and_exit;
631 }
632
633 status = acpi_ns_attach_data(node, handler, data);
634
635 unlock_and_exit:
636 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
637 return (status);
638 }
639
640 /*******************************************************************************
641 *
642 * FUNCTION: acpi_detach_data
643 *
644 * PARAMETERS: obj_handle - Namespace node handle
645 * Handler - Handler used in call to acpi_attach_data
646 *
647 * RETURN: Status
648 *
649 * DESCRIPTION: Remove data that was previously attached to a node.
650 *
651 ******************************************************************************/
652
653 acpi_status
654 acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler)
655 {
656 struct acpi_namespace_node *node;
657 acpi_status status;
658
659 /* Parameter validation */
660
661 if (!obj_handle || !handler) {
662 return (AE_BAD_PARAMETER);
663 }
664
665 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
666 if (ACPI_FAILURE(status)) {
667 return (status);
668 }
669
670 /* Convert and validate the handle */
671
672 node = acpi_ns_map_handle_to_node(obj_handle);
673 if (!node) {
674 status = AE_BAD_PARAMETER;
675 goto unlock_and_exit;
676 }
677
678 status = acpi_ns_detach_data(node, handler);
679
680 unlock_and_exit:
681 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
682 return (status);
683 }
684
685 /*******************************************************************************
686 *
687 * FUNCTION: acpi_get_data
688 *
689 * PARAMETERS: obj_handle - Namespace node
690 * Handler - Handler used in call to attach_data
691 * Data - Where the data is returned
692 *
693 * RETURN: Status
694 *
695 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
696 *
697 ******************************************************************************/
698
699 acpi_status
700 acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data)
701 {
702 struct acpi_namespace_node *node;
703 acpi_status status;
704
705 /* Parameter validation */
706
707 if (!obj_handle || !handler || !data) {
708 return (AE_BAD_PARAMETER);
709 }
710
711 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
712 if (ACPI_FAILURE(status)) {
713 return (status);
714 }
715
716 /* Convert and validate the handle */
717
718 node = acpi_ns_map_handle_to_node(obj_handle);
719 if (!node) {
720 status = AE_BAD_PARAMETER;
721 goto unlock_and_exit;
722 }
723
724 status = acpi_ns_get_attached_data(node, handler, data);
725
726 unlock_and_exit:
727 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
728 return (status);
729 }
This page took 0.06036 seconds and 6 git commands to generate.