dfa31c5ba6c3424b8a97e0dd0b47c4c99d0bb559
[linux-2.6-microblaze.git] / drivers / acpi / acpica / nsrepair.c
1 /******************************************************************************
2  *
3  * Module Name: nsrepair - Repair for objects returned by predefined methods
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2009, Intel Corp.
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 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "acpredef.h"
48
49 #define _COMPONENT          ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsrepair")
51
52 /*******************************************************************************
53  *
54  * FUNCTION:    acpi_ns_repair_object
55  *
56  * PARAMETERS:  Data                - Pointer to validation data structure
57  *              expected_btypes     - Object types expected
58  *              package_index       - Index of object within parent package (if
59  *                                    applicable - ACPI_NOT_PACKAGE_ELEMENT
60  *                                    otherwise)
61  *              return_object_ptr   - Pointer to the object returned from the
62  *                                    evaluation of a method or object
63  *
64  * RETURN:      Status. AE_OK if repair was successful.
65  *
66  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
67  *              not expected.
68  *
69  ******************************************************************************/
70 acpi_status
71 acpi_ns_repair_object(struct acpi_predefined_data *data,
72                       u32 expected_btypes,
73                       u32 package_index,
74                       union acpi_operand_object **return_object_ptr)
75 {
76         union acpi_operand_object *return_object = *return_object_ptr;
77         union acpi_operand_object *new_object;
78         acpi_size length;
79
80         /*
81          * At this point, we know that the type of the returned object was not
82          * one of the expected types for this predefined name. Attempt to
83          * repair the object. Only a limited number of repairs are possible.
84          */
85         switch (return_object->common.type) {
86         case ACPI_TYPE_BUFFER:
87
88                 /* Does the method/object legally return a string? */
89
90                 if (!(expected_btypes & ACPI_RTYPE_STRING)) {
91                         return (AE_AML_OPERAND_TYPE);
92                 }
93
94                 /*
95                  * Have a Buffer, expected a String, convert. Use a to_string
96                  * conversion, no transform performed on the buffer data. The best
97                  * example of this is the _BIF method, where the string data from
98                  * the battery is often (incorrectly) returned as buffer object(s).
99                  */
100                 length = 0;
101                 while ((length < return_object->buffer.length) &&
102                        (return_object->buffer.pointer[length])) {
103                         length++;
104                 }
105
106                 /* Allocate a new string object */
107
108                 new_object = acpi_ut_create_string_object(length);
109                 if (!new_object) {
110                         return (AE_NO_MEMORY);
111                 }
112
113                 /*
114                  * Copy the raw buffer data with no transform. String is already NULL
115                  * terminated at Length+1.
116                  */
117                 ACPI_MEMCPY(new_object->string.pointer,
118                             return_object->buffer.pointer, length);
119                 break;
120
121         case ACPI_TYPE_INTEGER:
122
123                 /* Does the method/object legally return a string? */
124
125                 if (expected_btypes & ACPI_RTYPE_STRING) {
126                         /*
127                          * The only supported Integer-to-String conversion is to convert
128                          * an integer of value 0 to a NULL string. The last element of
129                          * _BIF and _BIX packages occasionally need this fix.
130                          */
131                         if (return_object->integer.value != 0) {
132                                 return (AE_AML_OPERAND_TYPE);
133                         }
134
135                         /* Allocate a new NULL string object */
136
137                         new_object = acpi_ut_create_string_object(0);
138                         if (!new_object) {
139                                 return (AE_NO_MEMORY);
140                         }
141                 } else {
142                         return (AE_AML_OPERAND_TYPE);
143                 }
144                 break;
145
146         default:
147
148                 /* We cannot repair this object */
149
150                 return (AE_AML_OPERAND_TYPE);
151         }
152
153         /* Object was successfully repaired */
154
155         /*
156          * If the original object is a package element, we need to:
157          * 1. Set the reference count of the new object to match the
158          *    reference count of the old object.
159          * 2. Decrement the reference count of the original object.
160          */
161         if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
162                 new_object->common.reference_count =
163                     return_object->common.reference_count;
164
165                 if (return_object->common.reference_count > 1) {
166                         return_object->common.reference_count--;
167                 }
168
169                 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
170                                       "Converted %s to expected %s at index %u",
171                                       acpi_ut_get_object_type_name
172                                       (return_object),
173                                       acpi_ut_get_object_type_name(new_object),
174                                       package_index));
175         } else {
176                 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
177                                       "Converted %s to expected %s",
178                                       acpi_ut_get_object_type_name
179                                       (return_object),
180                                       acpi_ut_get_object_type_name
181                                       (new_object)));
182         }
183
184         /* Delete old object, install the new return object */
185
186         acpi_ut_remove_reference(return_object);
187         *return_object_ptr = new_object;
188         data->flags |= ACPI_OBJECT_REPAIRED;
189         return (AE_OK);
190 }
191
192 /*******************************************************************************
193  *
194  * FUNCTION:    acpi_ns_repair_package_list
195  *
196  * PARAMETERS:  Data                - Pointer to validation data structure
197  *              obj_desc_ptr        - Pointer to the object to repair. The new
198  *                                    package object is returned here,
199  *                                    overwriting the old object.
200  *
201  * RETURN:      Status, new object in *obj_desc_ptr
202  *
203  * DESCRIPTION: Repair a common problem with objects that are defined to return
204  *              a variable-length Package of Packages. If the variable-length
205  *              is one, some BIOS code mistakenly simply declares a single
206  *              Package instead of a Package with one sub-Package. This
207  *              function attempts to repair this error by wrapping a Package
208  *              object around the original Package, creating the correct
209  *              Package with one sub-Package.
210  *
211  *              Names that can be repaired in this manner include:
212  *              _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
213  *
214  ******************************************************************************/
215
216 acpi_status
217 acpi_ns_repair_package_list(struct acpi_predefined_data *data,
218                             union acpi_operand_object **obj_desc_ptr)
219 {
220         union acpi_operand_object *pkg_obj_desc;
221
222         /*
223          * Create the new outer package and populate it. The new package will
224          * have a single element, the lone subpackage.
225          */
226         pkg_obj_desc = acpi_ut_create_package_object(1);
227         if (!pkg_obj_desc) {
228                 return (AE_NO_MEMORY);
229         }
230
231         pkg_obj_desc->package.elements[0] = *obj_desc_ptr;
232
233         /* Return the new object in the object pointer */
234
235         *obj_desc_ptr = pkg_obj_desc;
236         data->flags |= ACPI_OBJECT_REPAIRED;
237
238         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
239                               "Incorrectly formed Package, attempting repair"));
240
241         return (AE_OK);
242 }