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