ACPICA: Return object repair: Add string-to-unicode conversion
[firefly-linux-kernel-4.4.55.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 - 2013, 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 #include "acpredef.h"
49 #include "amlresrc.h"
50
51 #define _COMPONENT          ACPI_NAMESPACE
52 ACPI_MODULE_NAME("nsrepair")
53
54 /*******************************************************************************
55  *
56  * This module attempts to repair or convert objects returned by the
57  * predefined methods to an object type that is expected, as per the ACPI
58  * specification. The need for this code is dictated by the many machines that
59  * return incorrect types for the standard predefined methods. Performing these
60  * conversions here, in one place, eliminates the need for individual ACPI
61  * device drivers to do the same. Note: Most of these conversions are different
62  * than the internal object conversion routines used for implicit object
63  * conversion.
64  *
65  * The following conversions can be performed as necessary:
66  *
67  * Integer -> String
68  * Integer -> Buffer
69  * String  -> Integer
70  * String  -> Buffer
71  * Buffer  -> Integer
72  * Buffer  -> String
73  * Buffer  -> Package of Integers
74  * Package -> Package of one Package
75  *
76  * Additional conversions that are available:
77  *  Convert a null return or zero return value to an end_tag descriptor
78  *  Convert an ASCII string to a Unicode buffer
79  *
80  * An incorrect standalone object is wrapped with required outer package
81  *
82  * Additional possible repairs:
83  * Required package elements that are NULL replaced by Integer/String/Buffer
84  *
85  ******************************************************************************/
86 /* Local prototypes */
87 static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
88                                                                          acpi_namespace_node
89                                                                          *node,
90                                                                          u32
91                                                                          return_btype,
92                                                                          u32
93                                                                          package_index);
94
95 /*
96  * Special but simple repairs for some names.
97  *
98  * 2nd argument: Unexpected types that can be repaired
99  */
100 static const struct acpi_simple_repair_info acpi_object_repair_info[] = {
101         /* Unicode conversions */
102
103         {"_MLS", ACPI_RTYPE_STRING, 1,
104          acpi_ns_convert_to_unicode},
105         {"_STR", ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER,
106          ACPI_NOT_PACKAGE_ELEMENT,
107          acpi_ns_convert_to_unicode},
108         {{0, 0, 0, 0}, 0, 0, NULL}      /* Table terminator */
109 };
110
111 /*******************************************************************************
112  *
113  * FUNCTION:    acpi_ns_simple_repair
114  *
115  * PARAMETERS:  data                - Pointer to validation data structure
116  *              expected_btypes     - Object types expected
117  *              package_index       - Index of object within parent package (if
118  *                                    applicable - ACPI_NOT_PACKAGE_ELEMENT
119  *                                    otherwise)
120  *              return_object_ptr   - Pointer to the object returned from the
121  *                                    evaluation of a method or object
122  *
123  * RETURN:      Status. AE_OK if repair was successful.
124  *
125  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
126  *              not expected.
127  *
128  ******************************************************************************/
129
130 acpi_status
131 acpi_ns_simple_repair(struct acpi_predefined_data *data,
132                       u32 expected_btypes,
133                       u32 package_index,
134                       union acpi_operand_object **return_object_ptr)
135 {
136         union acpi_operand_object *return_object = *return_object_ptr;
137         union acpi_operand_object *new_object = NULL;
138         acpi_status status;
139         const struct acpi_simple_repair_info *predefined;
140
141         ACPI_FUNCTION_NAME(ns_simple_repair);
142
143         /*
144          * Special repairs for certain names that are in the repair table.
145          * Check if this name is in the list of repairable names.
146          */
147         predefined = acpi_ns_match_simple_repair(data->node,
148                                                  data->return_btype,
149                                                  package_index);
150         if (predefined) {
151                 if (!return_object) {
152                         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
153                                               ACPI_WARN_ALWAYS,
154                                               "Missing expected return value"));
155                 }
156
157                 status =
158                     predefined->object_converter(return_object, &new_object);
159                 if (ACPI_FAILURE(status)) {
160
161                         /* A fatal error occurred during a conversion */
162
163                         ACPI_EXCEPTION((AE_INFO, status,
164                                         "During return object analysis"));
165                         return (status);
166                 }
167                 if (new_object) {
168                         goto object_repaired;
169                 }
170         }
171
172         /*
173          * Do not perform simple object repair unless the return type is not
174          * expected.
175          */
176         if (data->return_btype & expected_btypes) {
177                 return (AE_OK);
178         }
179
180         /*
181          * At this point, we know that the type of the returned object was not
182          * one of the expected types for this predefined name. Attempt to
183          * repair the object by converting it to one of the expected object
184          * types for this predefined name.
185          */
186
187         /*
188          * If there is no return value, check if we require a return value for
189          * this predefined name. Either one return value is expected, or none,
190          * for both methods and other objects.
191          *
192          * Exit now if there is no return object. Warning if one was expected.
193          */
194         if (!return_object) {
195                 if (expected_btypes && (!(expected_btypes & ACPI_RTYPE_NONE))) {
196                         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
197                                               ACPI_WARN_ALWAYS,
198                                               "Missing expected return value"));
199
200                         return (AE_AML_NO_RETURN_VALUE);
201                 }
202         }
203
204         if (expected_btypes & ACPI_RTYPE_INTEGER) {
205                 status = acpi_ns_convert_to_integer(return_object, &new_object);
206                 if (ACPI_SUCCESS(status)) {
207                         goto object_repaired;
208                 }
209         }
210         if (expected_btypes & ACPI_RTYPE_STRING) {
211                 status = acpi_ns_convert_to_string(return_object, &new_object);
212                 if (ACPI_SUCCESS(status)) {
213                         goto object_repaired;
214                 }
215         }
216         if (expected_btypes & ACPI_RTYPE_BUFFER) {
217                 status = acpi_ns_convert_to_buffer(return_object, &new_object);
218                 if (ACPI_SUCCESS(status)) {
219                         goto object_repaired;
220                 }
221         }
222         if (expected_btypes & ACPI_RTYPE_PACKAGE) {
223                 /*
224                  * A package is expected. We will wrap the existing object with a
225                  * new package object. It is often the case that if a variable-length
226                  * package is required, but there is only a single object needed, the
227                  * BIOS will return that object instead of wrapping it with a Package
228                  * object. Note: after the wrapping, the package will be validated
229                  * for correct contents (expected object type or types).
230                  */
231                 status =
232                     acpi_ns_wrap_with_package(data, return_object, &new_object);
233                 if (ACPI_SUCCESS(status)) {
234                         /*
235                          * The original object just had its reference count
236                          * incremented for being inserted into the new package.
237                          */
238                         *return_object_ptr = new_object;        /* New Package object */
239                         data->flags |= ACPI_OBJECT_REPAIRED;
240                         return (AE_OK);
241                 }
242         }
243
244         /* We cannot repair this object */
245
246         return (AE_AML_OPERAND_TYPE);
247
248       object_repaired:
249
250         /* Object was successfully repaired */
251
252         if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
253                 /*
254                  * The original object is a package element. We need to
255                  * decrement the reference count of the original object,
256                  * for removing it from the package.
257                  *
258                  * However, if the original object was just wrapped with a
259                  * package object as part of the repair, we don't need to
260                  * change the reference count.
261                  */
262                 if (!(data->flags & ACPI_OBJECT_WRAPPED)) {
263                         new_object->common.reference_count =
264                             return_object->common.reference_count;
265
266                         if (return_object->common.reference_count > 1) {
267                                 return_object->common.reference_count--;
268                         }
269                 }
270
271                 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
272                                   "%s: Converted %s to expected %s at Package index %u\n",
273                                   data->pathname,
274                                   acpi_ut_get_object_type_name(return_object),
275                                   acpi_ut_get_object_type_name(new_object),
276                                   package_index));
277         } else {
278                 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
279                                   "%s: Converted %s to expected %s\n",
280                                   data->pathname,
281                                   acpi_ut_get_object_type_name(return_object),
282                                   acpi_ut_get_object_type_name(new_object)));
283         }
284
285         /* Delete old object, install the new return object */
286
287         acpi_ut_remove_reference(return_object);
288         *return_object_ptr = new_object;
289         data->flags |= ACPI_OBJECT_REPAIRED;
290         return (AE_OK);
291 }
292
293 /******************************************************************************
294  *
295  * FUNCTION:    acpi_ns_match_simple_repair
296  *
297  * PARAMETERS:  node                - Namespace node for the method/object
298  *              return_btype        - Object type that was returned
299  *              package_index       - Index of object within parent package (if
300  *                                    applicable - ACPI_NOT_PACKAGE_ELEMENT
301  *                                    otherwise)
302  *
303  * RETURN:      Pointer to entry in repair table. NULL indicates not found.
304  *
305  * DESCRIPTION: Check an object name against the repairable object list.
306  *
307  *****************************************************************************/
308
309 static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
310                                                                          acpi_namespace_node
311                                                                          *node,
312                                                                          u32
313                                                                          return_btype,
314                                                                          u32
315                                                                          package_index)
316 {
317         const struct acpi_simple_repair_info *this_name;
318
319         /* Search info table for a repairable predefined method/object name */
320
321         this_name = acpi_object_repair_info;
322         while (this_name->object_converter) {
323                 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
324
325                         /* Check if we can actually repair this name/type combination */
326
327                         if ((return_btype & this_name->unexpected_btypes) &&
328                             (package_index == this_name->package_index)) {
329                                 return (this_name);
330                         }
331
332                         return (NULL);
333                 }
334                 this_name++;
335         }
336
337         return (NULL);          /* Name was not found in the repair table */
338 }
339
340 /*******************************************************************************
341  *
342  * FUNCTION:    acpi_ns_repair_null_element
343  *
344  * PARAMETERS:  data                - Pointer to validation data structure
345  *              expected_btypes     - Object types expected
346  *              package_index       - Index of object within parent package (if
347  *                                    applicable - ACPI_NOT_PACKAGE_ELEMENT
348  *                                    otherwise)
349  *              return_object_ptr   - Pointer to the object returned from the
350  *                                    evaluation of a method or object
351  *
352  * RETURN:      Status. AE_OK if repair was successful.
353  *
354  * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
355  *
356  ******************************************************************************/
357
358 acpi_status
359 acpi_ns_repair_null_element(struct acpi_predefined_data *data,
360                             u32 expected_btypes,
361                             u32 package_index,
362                             union acpi_operand_object **return_object_ptr)
363 {
364         union acpi_operand_object *return_object = *return_object_ptr;
365         union acpi_operand_object *new_object;
366
367         ACPI_FUNCTION_NAME(ns_repair_null_element);
368
369         /* No repair needed if return object is non-NULL */
370
371         if (return_object) {
372                 return (AE_OK);
373         }
374
375         /*
376          * Attempt to repair a NULL element of a Package object. This applies to
377          * predefined names that return a fixed-length package and each element
378          * is required. It does not apply to variable-length packages where NULL
379          * elements are allowed, especially at the end of the package.
380          */
381         if (expected_btypes & ACPI_RTYPE_INTEGER) {
382
383                 /* Need an integer - create a zero-value integer */
384
385                 new_object = acpi_ut_create_integer_object((u64)0);
386         } else if (expected_btypes & ACPI_RTYPE_STRING) {
387
388                 /* Need a string - create a NULL string */
389
390                 new_object = acpi_ut_create_string_object(0);
391         } else if (expected_btypes & ACPI_RTYPE_BUFFER) {
392
393                 /* Need a buffer - create a zero-length buffer */
394
395                 new_object = acpi_ut_create_buffer_object(0);
396         } else {
397                 /* Error for all other expected types */
398
399                 return (AE_AML_OPERAND_TYPE);
400         }
401
402         if (!new_object) {
403                 return (AE_NO_MEMORY);
404         }
405
406         /* Set the reference count according to the parent Package object */
407
408         new_object->common.reference_count =
409             data->parent_package->common.reference_count;
410
411         ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
412                           "%s: Converted NULL package element to expected %s at index %u\n",
413                           data->pathname,
414                           acpi_ut_get_object_type_name(new_object),
415                           package_index));
416
417         *return_object_ptr = new_object;
418         data->flags |= ACPI_OBJECT_REPAIRED;
419         return (AE_OK);
420 }
421
422 /******************************************************************************
423  *
424  * FUNCTION:    acpi_ns_remove_null_elements
425  *
426  * PARAMETERS:  data                - Pointer to validation data structure
427  *              package_type        - An acpi_return_package_types value
428  *              obj_desc            - A Package object
429  *
430  * RETURN:      None.
431  *
432  * DESCRIPTION: Remove all NULL package elements from packages that contain
433  *              a variable number of sub-packages. For these types of
434  *              packages, NULL elements can be safely removed.
435  *
436  *****************************************************************************/
437
438 void
439 acpi_ns_remove_null_elements(struct acpi_predefined_data *data,
440                              u8 package_type,
441                              union acpi_operand_object *obj_desc)
442 {
443         union acpi_operand_object **source;
444         union acpi_operand_object **dest;
445         u32 count;
446         u32 new_count;
447         u32 i;
448
449         ACPI_FUNCTION_NAME(ns_remove_null_elements);
450
451         /*
452          * We can safely remove all NULL elements from these package types:
453          * PTYPE1_VAR packages contain a variable number of simple data types.
454          * PTYPE2 packages contain a variable number of sub-packages.
455          */
456         switch (package_type) {
457         case ACPI_PTYPE1_VAR:
458         case ACPI_PTYPE2:
459         case ACPI_PTYPE2_COUNT:
460         case ACPI_PTYPE2_PKG_COUNT:
461         case ACPI_PTYPE2_FIXED:
462         case ACPI_PTYPE2_MIN:
463         case ACPI_PTYPE2_REV_FIXED:
464         case ACPI_PTYPE2_FIX_VAR:
465                 break;
466
467         default:
468         case ACPI_PTYPE1_FIXED:
469         case ACPI_PTYPE1_OPTION:
470                 return;
471         }
472
473         count = obj_desc->package.count;
474         new_count = count;
475
476         source = obj_desc->package.elements;
477         dest = source;
478
479         /* Examine all elements of the package object, remove nulls */
480
481         for (i = 0; i < count; i++) {
482                 if (!*source) {
483                         new_count--;
484                 } else {
485                         *dest = *source;
486                         dest++;
487                 }
488                 source++;
489         }
490
491         /* Update parent package if any null elements were removed */
492
493         if (new_count < count) {
494                 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
495                                   "%s: Found and removed %u NULL elements\n",
496                                   data->pathname, (count - new_count)));
497
498                 /* NULL terminate list and update the package count */
499
500                 *dest = NULL;
501                 obj_desc->package.count = new_count;
502         }
503 }
504
505 /*******************************************************************************
506  *
507  * FUNCTION:    acpi_ns_wrap_with_package
508  *
509  * PARAMETERS:  data                - Pointer to validation data structure
510  *              original_object     - Pointer to the object to repair.
511  *              obj_desc_ptr        - The new package object is returned here
512  *
513  * RETURN:      Status, new object in *obj_desc_ptr
514  *
515  * DESCRIPTION: Repair a common problem with objects that are defined to
516  *              return a variable-length Package of sub-objects. If there is
517  *              only one sub-object, some BIOS code mistakenly simply declares
518  *              the single object instead of a Package with one sub-object.
519  *              This function attempts to repair this error by wrapping a
520  *              Package object around the original object, creating the
521  *              correct and expected Package with one sub-object.
522  *
523  *              Names that can be repaired in this manner include:
524  *              _ALR, _CSD, _HPX, _MLS, _PLD, _PRT, _PSS, _TRT, _TSS,
525  *              _BCL, _DOD, _FIX, _Sx
526  *
527  ******************************************************************************/
528
529 acpi_status
530 acpi_ns_wrap_with_package(struct acpi_predefined_data *data,
531                           union acpi_operand_object *original_object,
532                           union acpi_operand_object **obj_desc_ptr)
533 {
534         union acpi_operand_object *pkg_obj_desc;
535
536         ACPI_FUNCTION_NAME(ns_wrap_with_package);
537
538         /*
539          * Create the new outer package and populate it. The new package will
540          * have a single element, the lone sub-object.
541          */
542         pkg_obj_desc = acpi_ut_create_package_object(1);
543         if (!pkg_obj_desc) {
544                 return (AE_NO_MEMORY);
545         }
546
547         pkg_obj_desc->package.elements[0] = original_object;
548
549         ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
550                           "%s: Wrapped %s with expected Package object\n",
551                           data->pathname,
552                           acpi_ut_get_object_type_name(original_object)));
553
554         /* Return the new object in the object pointer */
555
556         *obj_desc_ptr = pkg_obj_desc;
557         data->flags |= ACPI_OBJECT_REPAIRED | ACPI_OBJECT_WRAPPED;
558         return (AE_OK);
559 }