062a016d45549402e6fd7e75720c28c3e8c9eac7
[linux-block.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 "acinterp.h"
48
49 #define _COMPONENT          ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsrepair")
51
52 /*******************************************************************************
53  *
54  * This module attempts to repair or convert objects returned by the
55  * predefined methods to an object type that is expected, as per the ACPI
56  * specification. The need for this code is dictated by the many machines that
57  * return incorrect types for the standard predefined methods. Performing these
58  * conversions here, in one place, eliminates the need for individual ACPI
59  * device drivers to do the same. Note: Most of these conversions are different
60  * than the internal object conversion routines used for implicit object
61  * conversion.
62  *
63  * The following conversions can be performed as necessary:
64  *
65  * Integer -> String
66  * Integer -> Buffer
67  * String  -> Integer
68  * String  -> Buffer
69  * Buffer  -> Integer
70  * Buffer  -> String
71  * Buffer  -> Package of Integers
72  * Package -> Package of one Package
73  *
74  ******************************************************************************/
75 /* Local prototypes */
76 static acpi_status
77 acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
78                            union acpi_operand_object **return_object);
79
80 static acpi_status
81 acpi_ns_convert_to_string(union acpi_operand_object *original_object,
82                           union acpi_operand_object **return_object);
83
84 static acpi_status
85 acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
86                           union acpi_operand_object **return_object);
87
88 static acpi_status
89 acpi_ns_convert_to_package(union acpi_operand_object *original_object,
90                            union acpi_operand_object **return_object);
91
92 /*******************************************************************************
93  *
94  * FUNCTION:    acpi_ns_repair_object
95  *
96  * PARAMETERS:  Data                - Pointer to validation data structure
97  *              expected_btypes     - Object types expected
98  *              package_index       - Index of object within parent package (if
99  *                                    applicable - ACPI_NOT_PACKAGE_ELEMENT
100  *                                    otherwise)
101  *              return_object_ptr   - Pointer to the object returned from the
102  *                                    evaluation of a method or object
103  *
104  * RETURN:      Status. AE_OK if repair was successful.
105  *
106  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
107  *              not expected.
108  *
109  ******************************************************************************/
110
111 acpi_status
112 acpi_ns_repair_object(struct acpi_predefined_data *data,
113                       u32 expected_btypes,
114                       u32 package_index,
115                       union acpi_operand_object **return_object_ptr)
116 {
117         union acpi_operand_object *return_object = *return_object_ptr;
118         union acpi_operand_object *new_object;
119         acpi_status status;
120
121         /*
122          * At this point, we know that the type of the returned object was not
123          * one of the expected types for this predefined name. Attempt to
124          * repair the object by converting it to one of the expected object
125          * types for this predefined name.
126          */
127         if (expected_btypes & ACPI_RTYPE_INTEGER) {
128                 status = acpi_ns_convert_to_integer(return_object, &new_object);
129                 if (ACPI_SUCCESS(status)) {
130                         goto object_repaired;
131                 }
132         }
133         if (expected_btypes & ACPI_RTYPE_STRING) {
134                 status = acpi_ns_convert_to_string(return_object, &new_object);
135                 if (ACPI_SUCCESS(status)) {
136                         goto object_repaired;
137                 }
138         }
139         if (expected_btypes & ACPI_RTYPE_BUFFER) {
140                 status = acpi_ns_convert_to_buffer(return_object, &new_object);
141                 if (ACPI_SUCCESS(status)) {
142                         goto object_repaired;
143                 }
144         }
145         if (expected_btypes & ACPI_RTYPE_PACKAGE) {
146                 status = acpi_ns_convert_to_package(return_object, &new_object);
147                 if (ACPI_SUCCESS(status)) {
148                         goto object_repaired;
149                 }
150         }
151
152         /* We cannot repair this object */
153
154         return (AE_AML_OPERAND_TYPE);
155
156       object_repaired:
157
158         /* Object was successfully repaired */
159
160         /*
161          * If the original object is a package element, we need to:
162          * 1. Set the reference count of the new object to match the
163          *    reference count of the old object.
164          * 2. Decrement the reference count of the original object.
165          */
166         if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
167                 new_object->common.reference_count =
168                     return_object->common.reference_count;
169
170                 if (return_object->common.reference_count > 1) {
171                         return_object->common.reference_count--;
172                 }
173
174                 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
175                                       "Converted %s to expected %s at index %u",
176                                       acpi_ut_get_object_type_name
177                                       (return_object),
178                                       acpi_ut_get_object_type_name(new_object),
179                                       package_index));
180         } else {
181                 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
182                                       "Converted %s to expected %s",
183                                       acpi_ut_get_object_type_name
184                                       (return_object),
185                                       acpi_ut_get_object_type_name
186                                       (new_object)));
187         }
188
189         /* Delete old object, install the new return object */
190
191         acpi_ut_remove_reference(return_object);
192         *return_object_ptr = new_object;
193         data->flags |= ACPI_OBJECT_REPAIRED;
194         return (AE_OK);
195 }
196
197 /*******************************************************************************
198  *
199  * FUNCTION:    acpi_ns_convert_to_integer
200  *
201  * PARAMETERS:  original_object     - Object to be converted
202  *              return_object       - Where the new converted object is returned
203  *
204  * RETURN:      Status. AE_OK if conversion was successful.
205  *
206  * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
207  *
208  ******************************************************************************/
209
210 static acpi_status
211 acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
212                            union acpi_operand_object **return_object)
213 {
214         union acpi_operand_object *new_object;
215         acpi_status status;
216         u64 value = 0;
217         u32 i;
218
219         switch (original_object->common.type) {
220         case ACPI_TYPE_STRING:
221
222                 /* String-to-Integer conversion */
223
224                 status = acpi_ut_strtoul64(original_object->string.pointer,
225                                            ACPI_ANY_BASE, &value);
226                 if (ACPI_FAILURE(status)) {
227                         return (status);
228                 }
229                 break;
230
231         case ACPI_TYPE_BUFFER:
232
233                 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
234
235                 if (original_object->buffer.length > 8) {
236                         return (AE_AML_OPERAND_TYPE);
237                 }
238
239                 /* Extract each buffer byte to create the integer */
240
241                 for (i = 0; i < original_object->buffer.length; i++) {
242                         value |=
243                             ((u64) original_object->buffer.
244                              pointer[i] << (i * 8));
245                 }
246                 break;
247
248         default:
249                 return (AE_AML_OPERAND_TYPE);
250         }
251
252         new_object = acpi_ut_create_integer_object(value);
253         if (!new_object) {
254                 return (AE_NO_MEMORY);
255         }
256
257         *return_object = new_object;
258         return (AE_OK);
259 }
260
261 /*******************************************************************************
262  *
263  * FUNCTION:    acpi_ns_convert_to_string
264  *
265  * PARAMETERS:  original_object     - Object to be converted
266  *              return_object       - Where the new converted object is returned
267  *
268  * RETURN:      Status. AE_OK if conversion was successful.
269  *
270  * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
271  *
272  ******************************************************************************/
273
274 static acpi_status
275 acpi_ns_convert_to_string(union acpi_operand_object *original_object,
276                           union acpi_operand_object **return_object)
277 {
278         union acpi_operand_object *new_object;
279         acpi_size length;
280         acpi_status status;
281
282         switch (original_object->common.type) {
283         case ACPI_TYPE_INTEGER:
284                 /*
285                  * Integer-to-String conversion. Commonly, convert
286                  * an integer of value 0 to a NULL string. The last element of
287                  * _BIF and _BIX packages occasionally need this fix.
288                  */
289                 if (original_object->integer.value == 0) {
290
291                         /* Allocate a new NULL string object */
292
293                         new_object = acpi_ut_create_string_object(0);
294                         if (!new_object) {
295                                 return (AE_NO_MEMORY);
296                         }
297                 } else {
298                         status =
299                             acpi_ex_convert_to_string(original_object,
300                                                       &new_object,
301                                                       ACPI_IMPLICIT_CONVERT_HEX);
302                         if (ACPI_FAILURE(status)) {
303                                 return (status);
304                         }
305                 }
306                 break;
307
308         case ACPI_TYPE_BUFFER:
309                 /*
310                  * Buffer-to-String conversion. Use a to_string
311                  * conversion, no transform performed on the buffer data. The best
312                  * example of this is the _BIF method, where the string data from
313                  * the battery is often (incorrectly) returned as buffer object(s).
314                  */
315                 length = 0;
316                 while ((length < original_object->buffer.length) &&
317                        (original_object->buffer.pointer[length])) {
318                         length++;
319                 }
320
321                 /* Allocate a new string object */
322
323                 new_object = acpi_ut_create_string_object(length);
324                 if (!new_object) {
325                         return (AE_NO_MEMORY);
326                 }
327
328                 /*
329                  * Copy the raw buffer data with no transform. String is already NULL
330                  * terminated at Length+1.
331                  */
332                 ACPI_MEMCPY(new_object->string.pointer,
333                             original_object->buffer.pointer, length);
334                 break;
335
336         default:
337                 return (AE_AML_OPERAND_TYPE);
338         }
339
340         *return_object = new_object;
341         return (AE_OK);
342 }
343
344 /*******************************************************************************
345  *
346  * FUNCTION:    acpi_ns_convert_to_buffer
347  *
348  * PARAMETERS:  original_object     - Object to be converted
349  *              return_object       - Where the new converted object is returned
350  *
351  * RETURN:      Status. AE_OK if conversion was successful.
352  *
353  * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
354  *
355  ******************************************************************************/
356
357 static acpi_status
358 acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
359                           union acpi_operand_object **return_object)
360 {
361         union acpi_operand_object *new_object;
362         acpi_status status;
363         union acpi_operand_object **elements;
364         u32 *dword_buffer;
365         u32 count;
366         u32 i;
367
368         switch (original_object->common.type) {
369         case ACPI_TYPE_INTEGER:
370                 /*
371                  * Integer-to-Buffer conversion.
372                  * Convert the Integer to a packed-byte buffer. _MAT and other
373                  * objects need this sometimes, if a read has been performed on a
374                  * Field object that is less than or equal to the global integer
375                  * size (32 or 64 bits).
376                  */
377                 status =
378                     acpi_ex_convert_to_buffer(original_object, &new_object);
379                 if (ACPI_FAILURE(status)) {
380                         return (status);
381                 }
382                 break;
383
384         case ACPI_TYPE_STRING:
385
386                 /* String-to-Buffer conversion. Simple data copy */
387
388                 new_object =
389                     acpi_ut_create_buffer_object(original_object->string.
390                                                  length);
391                 if (!new_object) {
392                         return (AE_NO_MEMORY);
393                 }
394
395                 ACPI_MEMCPY(new_object->buffer.pointer,
396                             original_object->string.pointer,
397                             original_object->string.length);
398                 break;
399
400         case ACPI_TYPE_PACKAGE:
401
402                 /* All elements of the Package must be integers */
403
404                 elements = original_object->package.elements;
405                 count = original_object->package.count;
406
407                 for (i = 0; i < count; i++) {
408                         if ((!*elements) ||
409                             ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
410                                 return (AE_AML_OPERAND_TYPE);
411                         }
412                         elements++;
413                 }
414
415                 /* Create the new buffer object to replace the Package */
416
417                 new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
418                 if (!new_object) {
419                         return (AE_NO_MEMORY);
420                 }
421
422                 /* Copy the package elements (integers) to the buffer as DWORDs */
423
424                 elements = original_object->package.elements;
425                 dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
426
427                 for (i = 0; i < count; i++) {
428                         *dword_buffer = (u32) (*elements)->integer.value;
429                         dword_buffer++;
430                         elements++;
431                 }
432                 break;
433
434         default:
435                 return (AE_AML_OPERAND_TYPE);
436         }
437
438         *return_object = new_object;
439         return (AE_OK);
440 }
441
442 /*******************************************************************************
443  *
444  * FUNCTION:    acpi_ns_convert_to_package
445  *
446  * PARAMETERS:  original_object     - Object to be converted
447  *              return_object       - Where the new converted object is returned
448  *
449  * RETURN:      Status. AE_OK if conversion was successful.
450  *
451  * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
452  *              the buffer is converted to a single integer package element.
453  *
454  ******************************************************************************/
455
456 static acpi_status
457 acpi_ns_convert_to_package(union acpi_operand_object *original_object,
458                            union acpi_operand_object **return_object)
459 {
460         union acpi_operand_object *new_object;
461         union acpi_operand_object **elements;
462         u32 length;
463         u8 *buffer;
464
465         switch (original_object->common.type) {
466         case ACPI_TYPE_BUFFER:
467
468                 /* Buffer-to-Package conversion */
469
470                 length = original_object->buffer.length;
471                 new_object = acpi_ut_create_package_object(length);
472                 if (!new_object) {
473                         return (AE_NO_MEMORY);
474                 }
475
476                 /* Convert each buffer byte to an integer package element */
477
478                 elements = new_object->package.elements;
479                 buffer = original_object->buffer.pointer;
480
481                 while (length--) {
482                         *elements =
483                             acpi_ut_create_integer_object((u64) *buffer);
484                         if (!*elements) {
485                                 acpi_ut_remove_reference(new_object);
486                                 return (AE_NO_MEMORY);
487                         }
488                         elements++;
489                         buffer++;
490                 }
491                 break;
492
493         default:
494                 return (AE_AML_OPERAND_TYPE);
495         }
496
497         *return_object = new_object;
498         return (AE_OK);
499 }
500
501 /*******************************************************************************
502  *
503  * FUNCTION:    acpi_ns_repair_package_list
504  *
505  * PARAMETERS:  Data                - Pointer to validation data structure
506  *              obj_desc_ptr        - Pointer to the object to repair. The new
507  *                                    package object is returned here,
508  *                                    overwriting the old object.
509  *
510  * RETURN:      Status, new object in *obj_desc_ptr
511  *
512  * DESCRIPTION: Repair a common problem with objects that are defined to return
513  *              a variable-length Package of Packages. If the variable-length
514  *              is one, some BIOS code mistakenly simply declares a single
515  *              Package instead of a Package with one sub-Package. This
516  *              function attempts to repair this error by wrapping a Package
517  *              object around the original Package, creating the correct
518  *              Package with one sub-Package.
519  *
520  *              Names that can be repaired in this manner include:
521  *              _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
522  *
523  ******************************************************************************/
524
525 acpi_status
526 acpi_ns_repair_package_list(struct acpi_predefined_data *data,
527                             union acpi_operand_object **obj_desc_ptr)
528 {
529         union acpi_operand_object *pkg_obj_desc;
530
531         /*
532          * Create the new outer package and populate it. The new package will
533          * have a single element, the lone subpackage.
534          */
535         pkg_obj_desc = acpi_ut_create_package_object(1);
536         if (!pkg_obj_desc) {
537                 return (AE_NO_MEMORY);
538         }
539
540         pkg_obj_desc->package.elements[0] = *obj_desc_ptr;
541
542         /* Return the new object in the object pointer */
543
544         *obj_desc_ptr = pkg_obj_desc;
545         data->flags |= ACPI_OBJECT_REPAIRED;
546
547         ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
548                               "Repaired Incorrectly formed Package"));
549
550         return (AE_OK);
551 }