vserver 1.9.3
[linux-2.6.git] / drivers / acpi / utilities / uteval.c
1 /******************************************************************************
2  *
3  * Module Name: uteval - Object evaluation
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2004, R. Byron Moore
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44
45 #include <acpi/acpi.h>
46 #include <acpi/acnamesp.h>
47 #include <acpi/acinterp.h>
48
49
50 #define _COMPONENT          ACPI_UTILITIES
51          ACPI_MODULE_NAME    ("uteval")
52
53
54 /*******************************************************************************
55  *
56  * FUNCTION:    acpi_ut_osi_implementation
57  *
58  * PARAMETERS:  walk_state          - Current walk state
59  *
60  * RETURN:      Status
61  *
62  * DESCRIPTION: Implementation of _OSI predefined control method
63  *              Supported = _OSI (String)
64  *
65  ******************************************************************************/
66
67 acpi_status
68 acpi_ut_osi_implementation (
69         struct acpi_walk_state          *walk_state)
70 {
71         union acpi_operand_object       *string_desc;
72         union acpi_operand_object       *return_desc;
73         acpi_native_uint                i;
74
75
76         ACPI_FUNCTION_TRACE ("ut_osi_implementation");
77
78
79         /* Validate the string input argument */
80
81         string_desc = walk_state->arguments[0].object;
82         if (!string_desc || (string_desc->common.type != ACPI_TYPE_STRING)) {
83                 return_ACPI_STATUS (AE_TYPE);
84         }
85
86         /* Create a return object (Default value = 0) */
87
88         return_desc = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
89         if (!return_desc) {
90                 return_ACPI_STATUS (AE_NO_MEMORY);
91         }
92
93         /* Compare input string to table of supported strings */
94
95         for (i = 0; i < ACPI_NUM_OSI_STRINGS; i++) {
96                 if (!ACPI_STRCMP (string_desc->string.pointer,
97                                    (char *) acpi_gbl_valid_osi_strings[i])) {
98                         /* This string is supported */
99
100                         return_desc->integer.value = 0xFFFFFFFF;
101                         break;
102                 }
103         }
104
105         walk_state->return_desc = return_desc;
106         return_ACPI_STATUS (AE_CTRL_TERMINATE);
107 }
108
109
110 /*******************************************************************************
111  *
112  * FUNCTION:    acpi_ut_evaluate_object
113  *
114  * PARAMETERS:  prefix_node         - Starting node
115  *              Path                - Path to object from starting node
116  *              expected_return_types - Bitmap of allowed return types
117  *              return_desc         - Where a return value is stored
118  *
119  * RETURN:      Status
120  *
121  * DESCRIPTION: Evaluates a namespace object and verifies the type of the
122  *              return object.  Common code that simplifies accessing objects
123  *              that have required return objects of fixed types.
124  *
125  *              NOTE: Internal function, no parameter validation
126  *
127  ******************************************************************************/
128
129 acpi_status
130 acpi_ut_evaluate_object (
131         struct acpi_namespace_node      *prefix_node,
132         char                            *path,
133         u32                             expected_return_btypes,
134         union acpi_operand_object       **return_desc)
135 {
136         struct acpi_parameter_info      info;
137         acpi_status                     status;
138         u32                             return_btype;
139
140
141         ACPI_FUNCTION_TRACE ("ut_evaluate_object");
142
143
144         info.node = prefix_node;
145         info.parameters = NULL;
146         info.parameter_type = ACPI_PARAM_ARGS;
147
148         /* Evaluate the object/method */
149
150         status = acpi_ns_evaluate_relative (path, &info);
151         if (ACPI_FAILURE (status)) {
152                 if (status == AE_NOT_FOUND) {
153                         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "[%4.4s.%s] was not found\n",
154                                 acpi_ut_get_node_name (prefix_node), path));
155                 }
156                 else {
157                         ACPI_REPORT_METHOD_ERROR ("Method execution failed",
158                                 prefix_node, path, status);
159                 }
160
161                 return_ACPI_STATUS (status);
162         }
163
164         /* Did we get a return object? */
165
166         if (!info.return_object) {
167                 if (expected_return_btypes) {
168                         ACPI_REPORT_METHOD_ERROR ("No object was returned from",
169                                 prefix_node, path, AE_NOT_EXIST);
170
171                         return_ACPI_STATUS (AE_NOT_EXIST);
172                 }
173
174                 return_ACPI_STATUS (AE_OK);
175         }
176
177         /* Map the return object type to the bitmapped type */
178
179         switch (ACPI_GET_OBJECT_TYPE (info.return_object)) {
180         case ACPI_TYPE_INTEGER:
181                 return_btype = ACPI_BTYPE_INTEGER;
182                 break;
183
184         case ACPI_TYPE_BUFFER:
185                 return_btype = ACPI_BTYPE_BUFFER;
186                 break;
187
188         case ACPI_TYPE_STRING:
189                 return_btype = ACPI_BTYPE_STRING;
190                 break;
191
192         case ACPI_TYPE_PACKAGE:
193                 return_btype = ACPI_BTYPE_PACKAGE;
194                 break;
195
196         default:
197                 return_btype = 0;
198                 break;
199         }
200
201         /* Is the return object one of the expected types? */
202
203         if (!(expected_return_btypes & return_btype)) {
204                 ACPI_REPORT_METHOD_ERROR ("Return object type is incorrect",
205                         prefix_node, path, AE_TYPE);
206
207                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
208                         "Type returned from %s was incorrect: %X\n",
209                         path, ACPI_GET_OBJECT_TYPE (info.return_object)));
210
211                 /* On error exit, we must delete the return object */
212
213                 acpi_ut_remove_reference (info.return_object);
214                 return_ACPI_STATUS (AE_TYPE);
215         }
216
217         /* Object type is OK, return it */
218
219         *return_desc = info.return_object;
220         return_ACPI_STATUS (AE_OK);
221 }
222
223
224 /*******************************************************************************
225  *
226  * FUNCTION:    acpi_ut_evaluate_numeric_object
227  *
228  * PARAMETERS:  *object_name        - Object name to be evaluated
229  *              device_node         - Node for the device
230  *              *Address            - Where the value is returned
231  *
232  * RETURN:      Status
233  *
234  * DESCRIPTION: Evaluates a numeric namespace object for a selected device
235  *              and stores result in *Address.
236  *
237  *              NOTE: Internal function, no parameter validation
238  *
239  ******************************************************************************/
240
241 acpi_status
242 acpi_ut_evaluate_numeric_object (
243         char                            *object_name,
244         struct acpi_namespace_node      *device_node,
245         acpi_integer                    *address)
246 {
247         union acpi_operand_object       *obj_desc;
248         acpi_status                     status;
249
250
251         ACPI_FUNCTION_TRACE ("ut_evaluate_numeric_object");
252
253
254         status = acpi_ut_evaluate_object (device_node, object_name,
255                          ACPI_BTYPE_INTEGER, &obj_desc);
256         if (ACPI_FAILURE (status)) {
257                 return_ACPI_STATUS (status);
258         }
259
260         /* Get the returned Integer */
261
262         *address = obj_desc->integer.value;
263
264         /* On exit, we must delete the return object */
265
266         acpi_ut_remove_reference (obj_desc);
267         return_ACPI_STATUS (status);
268 }
269
270
271 /*******************************************************************************
272  *
273  * FUNCTION:    acpi_ut_copy_id_string
274  *
275  * PARAMETERS:  Destination         - Where to copy the string
276  *              Source              - Source string
277  *              max_length          - Length of the destination buffer
278  *
279  * RETURN:      None
280  *
281  * DESCRIPTION: Copies an ID string for the _HID, _CID, and _UID methods.
282  *              Performs removal of a leading asterisk if present -- workaround
283  *              for a known issue on a bunch of machines.
284  *
285  ******************************************************************************/
286
287 static void
288 acpi_ut_copy_id_string (
289         char                            *destination,
290         char                            *source,
291         acpi_size                       max_length)
292 {
293
294
295         /*
296          * Workaround for ID strings that have a leading asterisk. This construct
297          * is not allowed by the ACPI specification  (ID strings must be
298          * alphanumeric), but enough existing machines have this embedded in their
299          * ID strings that the following code is useful.
300          */
301         if (*source == '*') {
302                 source++;
303         }
304
305         /* Do the actual copy */
306
307         ACPI_STRNCPY (destination, source, max_length);
308 }
309
310
311 /*******************************************************************************
312  *
313  * FUNCTION:    acpi_ut_execute_HID
314  *
315  * PARAMETERS:  device_node         - Node for the device
316  *              *Hid                - Where the HID is returned
317  *
318  * RETURN:      Status
319  *
320  * DESCRIPTION: Executes the _HID control method that returns the hardware
321  *              ID of the device.
322  *
323  *              NOTE: Internal function, no parameter validation
324  *
325  ******************************************************************************/
326
327 acpi_status
328 acpi_ut_execute_HID (
329         struct acpi_namespace_node      *device_node,
330         struct acpi_device_id           *hid)
331 {
332         union acpi_operand_object       *obj_desc;
333         acpi_status                     status;
334
335
336         ACPI_FUNCTION_TRACE ("ut_execute_HID");
337
338
339         status = acpi_ut_evaluate_object (device_node, METHOD_NAME__HID,
340                          ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING, &obj_desc);
341         if (ACPI_FAILURE (status)) {
342                 return_ACPI_STATUS (status);
343         }
344
345         if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_INTEGER) {
346                 /* Convert the Numeric HID to string */
347
348                 acpi_ex_eisa_id_to_string ((u32) obj_desc->integer.value, hid->value);
349         }
350         else {
351                 /* Copy the String HID from the returned object */
352
353                 acpi_ut_copy_id_string (hid->value, obj_desc->string.pointer,
354                                 sizeof (hid->value));
355         }
356
357         /* On exit, we must delete the return object */
358
359         acpi_ut_remove_reference (obj_desc);
360         return_ACPI_STATUS (status);
361 }
362
363
364 /*******************************************************************************
365  *
366  * FUNCTION:    acpi_ut_translate_one_cid
367  *
368  * PARAMETERS:  obj_desc            - _CID object, must be integer or string
369  *              one_cid             - Where the CID string is returned
370  *
371  * RETURN:      Status
372  *
373  * DESCRIPTION: Return a numeric or string _CID value as a string.
374  *              (Compatible ID)
375  *
376  *              NOTE:  Assumes a maximum _CID string length of
377  *                     ACPI_MAX_CID_LENGTH.
378  *
379  ******************************************************************************/
380
381 static acpi_status
382 acpi_ut_translate_one_cid (
383         union acpi_operand_object       *obj_desc,
384         struct acpi_compatible_id       *one_cid)
385 {
386
387
388         switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
389         case ACPI_TYPE_INTEGER:
390
391                 /* Convert the Numeric CID to string */
392
393                 acpi_ex_eisa_id_to_string ((u32) obj_desc->integer.value, one_cid->value);
394                 return (AE_OK);
395
396         case ACPI_TYPE_STRING:
397
398                 if (obj_desc->string.length > ACPI_MAX_CID_LENGTH) {
399                         return (AE_AML_STRING_LIMIT);
400                 }
401
402                 /* Copy the String CID from the returned object */
403
404                 acpi_ut_copy_id_string (one_cid->value, obj_desc->string.pointer,
405                                 ACPI_MAX_CID_LENGTH);
406                 return (AE_OK);
407
408         default:
409
410                 return (AE_TYPE);
411         }
412 }
413
414
415 /*******************************************************************************
416  *
417  * FUNCTION:    acpi_ut_execute_CID
418  *
419  * PARAMETERS:  device_node         - Node for the device
420  *              *Cid                - Where the CID is returned
421  *
422  * RETURN:      Status
423  *
424  * DESCRIPTION: Executes the _CID control method that returns one or more
425  *              compatible hardware IDs for the device.
426  *
427  *              NOTE: Internal function, no parameter validation
428  *
429  ******************************************************************************/
430
431 acpi_status
432 acpi_ut_execute_CID (
433         struct acpi_namespace_node      *device_node,
434         struct acpi_compatible_id_list **return_cid_list)
435 {
436         union acpi_operand_object       *obj_desc;
437         acpi_status                     status;
438         u32                             count;
439         u32                             size;
440         struct acpi_compatible_id_list *cid_list;
441         acpi_native_uint                i;
442
443
444         ACPI_FUNCTION_TRACE ("ut_execute_CID");
445
446
447         /* Evaluate the _CID method for this device */
448
449         status = acpi_ut_evaluate_object (device_node, METHOD_NAME__CID,
450                          ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING | ACPI_BTYPE_PACKAGE,
451                          &obj_desc);
452         if (ACPI_FAILURE (status)) {
453                 return_ACPI_STATUS (status);
454         }
455
456         /* Get the number of _CIDs returned */
457
458         count = 1;
459         if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_PACKAGE) {
460                 count = obj_desc->package.count;
461         }
462
463         /* Allocate a worst-case buffer for the _CIDs */
464
465         size = (((count - 1) * sizeof (struct acpi_compatible_id)) +
466                            sizeof (struct acpi_compatible_id_list));
467
468         cid_list = ACPI_MEM_CALLOCATE ((acpi_size) size);
469         if (!cid_list) {
470                 return_ACPI_STATUS (AE_NO_MEMORY);
471         }
472
473         /* Init CID list */
474
475         cid_list->count = count;
476         cid_list->size = size;
477
478         /*
479          *  A _CID can return either a single compatible ID or a package of compatible
480          *  IDs.  Each compatible ID can be one of the following:
481          *  -- Number (32 bit compressed EISA ID) or
482          *  -- String (PCI ID format, e.g. "PCI\VEN_vvvv&DEV_dddd&SUBSYS_ssssssss").
483          */
484
485         /* The _CID object can be either a single CID or a package (list) of CIDs */
486
487         if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_PACKAGE) {
488                 /* Translate each package element */
489
490                 for (i = 0; i < count; i++) {
491                         status = acpi_ut_translate_one_cid (obj_desc->package.elements[i],
492                                           &cid_list->id[i]);
493                         if (ACPI_FAILURE (status)) {
494                                 break;
495                         }
496                 }
497         }
498         else {
499                 /* Only one CID, translate to a string */
500
501                 status = acpi_ut_translate_one_cid (obj_desc, cid_list->id);
502         }
503
504         /* Cleanup on error */
505
506         if (ACPI_FAILURE (status)) {
507                 ACPI_MEM_FREE (cid_list);
508         }
509         else {
510                 *return_cid_list = cid_list;
511         }
512
513         /* On exit, we must delete the _CID return object */
514
515         acpi_ut_remove_reference (obj_desc);
516         return_ACPI_STATUS (status);
517 }
518
519
520 /*******************************************************************************
521  *
522  * FUNCTION:    acpi_ut_execute_UID
523  *
524  * PARAMETERS:  device_node         - Node for the device
525  *              *Uid                - Where the UID is returned
526  *
527  * RETURN:      Status
528  *
529  * DESCRIPTION: Executes the _UID control method that returns the hardware
530  *              ID of the device.
531  *
532  *              NOTE: Internal function, no parameter validation
533  *
534  ******************************************************************************/
535
536 acpi_status
537 acpi_ut_execute_UID (
538         struct acpi_namespace_node      *device_node,
539         struct acpi_device_id           *uid)
540 {
541         union acpi_operand_object       *obj_desc;
542         acpi_status                     status;
543
544
545         ACPI_FUNCTION_TRACE ("ut_execute_UID");
546
547
548         status = acpi_ut_evaluate_object (device_node, METHOD_NAME__UID,
549                          ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING, &obj_desc);
550         if (ACPI_FAILURE (status)) {
551                 return_ACPI_STATUS (status);
552         }
553
554         if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_INTEGER) {
555                 /* Convert the Numeric UID to string */
556
557                 acpi_ex_unsigned_integer_to_string (obj_desc->integer.value, uid->value);
558         }
559         else {
560                 /* Copy the String UID from the returned object */
561
562                 acpi_ut_copy_id_string (uid->value, obj_desc->string.pointer,
563                                 sizeof (uid->value));
564         }
565
566         /* On exit, we must delete the return object */
567
568         acpi_ut_remove_reference (obj_desc);
569         return_ACPI_STATUS (status);
570 }
571
572
573 /*******************************************************************************
574  *
575  * FUNCTION:    acpi_ut_execute_STA
576  *
577  * PARAMETERS:  device_node         - Node for the device
578  *              *Flags              - Where the status flags are returned
579  *
580  * RETURN:      Status
581  *
582  * DESCRIPTION: Executes _STA for selected device and stores results in
583  *              *Flags.
584  *
585  *              NOTE: Internal function, no parameter validation
586  *
587  ******************************************************************************/
588
589 acpi_status
590 acpi_ut_execute_STA (
591         struct acpi_namespace_node      *device_node,
592         u32                             *flags)
593 {
594         union acpi_operand_object       *obj_desc;
595         acpi_status                     status;
596
597
598         ACPI_FUNCTION_TRACE ("ut_execute_STA");
599
600
601         status = acpi_ut_evaluate_object (device_node, METHOD_NAME__STA,
602                          ACPI_BTYPE_INTEGER, &obj_desc);
603         if (ACPI_FAILURE (status)) {
604                 if (AE_NOT_FOUND == status) {
605                         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
606                                 "_STA on %4.4s was not found, assuming device is present\n",
607                                 acpi_ut_get_node_name (device_node)));
608
609                         *flags = 0x0F;
610                         status = AE_OK;
611                 }
612
613                 return_ACPI_STATUS (status);
614         }
615
616         /* Extract the status flags */
617
618         *flags = (u32) obj_desc->integer.value;
619
620         /* On exit, we must delete the return object */
621
622         acpi_ut_remove_reference (obj_desc);
623         return_ACPI_STATUS (status);
624 }
625
626
627 /*******************************************************************************
628  *
629  * FUNCTION:    acpi_ut_execute_Sxds
630  *
631  * PARAMETERS:  device_node         - Node for the device
632  *              *Flags              - Where the status flags are returned
633  *
634  * RETURN:      Status
635  *
636  * DESCRIPTION: Executes _STA for selected device and stores results in
637  *              *Flags.
638  *
639  *              NOTE: Internal function, no parameter validation
640  *
641  ******************************************************************************/
642
643 acpi_status
644 acpi_ut_execute_sxds (
645         struct acpi_namespace_node      *device_node,
646         u8                              *highest)
647 {
648         union acpi_operand_object       *obj_desc;
649         acpi_status                     status;
650         u32                             i;
651
652
653         ACPI_FUNCTION_TRACE ("ut_execute_Sxds");
654
655
656         for (i = 0; i < 4; i++) {
657                 highest[i] = 0xFF;
658                 status = acpi_ut_evaluate_object (device_node,
659                                  (char *) acpi_gbl_highest_dstate_names[i],
660                                  ACPI_BTYPE_INTEGER, &obj_desc);
661                 if (ACPI_FAILURE (status)) {
662                         if (status != AE_NOT_FOUND) {
663                                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
664                                         "%s on Device %4.4s, %s\n",
665                                         (char *) acpi_gbl_highest_dstate_names[i],
666                                         acpi_ut_get_node_name (device_node),
667                                         acpi_format_exception (status)));
668
669                                 return_ACPI_STATUS (status);
670                         }
671                 }
672                 else {
673                         /* Extract the Dstate value */
674
675                         highest[i] = (u8) obj_desc->integer.value;
676
677                         /* Delete the return object */
678
679                         acpi_ut_remove_reference (obj_desc);
680                 }
681         }
682
683         return_ACPI_STATUS (AE_OK);
684 }