Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / acpi / acpica / nsconvert.c
blobc803bda7915cdd4c9c399097a6e6b6d16e8a1e90
1 /******************************************************************************
3 * Module Name: nsconvert - Object conversions for objects returned by
4 * predefined methods
6 *****************************************************************************/
8 /*
9 * Copyright (C) 2000 - 2016, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47 #include "acnamesp.h"
48 #include "acinterp.h"
49 #include "acpredef.h"
50 #include "amlresrc.h"
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME("nsconvert")
55 /*******************************************************************************
57 * FUNCTION: acpi_ns_convert_to_integer
59 * PARAMETERS: original_object - Object to be converted
60 * return_object - Where the new converted object is returned
62 * RETURN: Status. AE_OK if conversion was successful.
64 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
66 ******************************************************************************/
67 acpi_status
68 acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
69 union acpi_operand_object **return_object)
71 union acpi_operand_object *new_object;
72 acpi_status status;
73 u64 value = 0;
74 u32 i;
76 switch (original_object->common.type) {
77 case ACPI_TYPE_STRING:
79 /* String-to-Integer conversion */
81 status = acpi_ut_strtoul64(original_object->string.pointer,
82 ACPI_ANY_BASE,
83 acpi_gbl_integer_byte_width, &value);
84 if (ACPI_FAILURE(status)) {
85 return (status);
87 break;
89 case ACPI_TYPE_BUFFER:
91 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
93 if (original_object->buffer.length > 8) {
94 return (AE_AML_OPERAND_TYPE);
97 /* Extract each buffer byte to create the integer */
99 for (i = 0; i < original_object->buffer.length; i++) {
100 value |= ((u64)
101 original_object->buffer.pointer[i] << (i *
102 8));
104 break;
106 default:
108 return (AE_AML_OPERAND_TYPE);
111 new_object = acpi_ut_create_integer_object(value);
112 if (!new_object) {
113 return (AE_NO_MEMORY);
116 *return_object = new_object;
117 return (AE_OK);
120 /*******************************************************************************
122 * FUNCTION: acpi_ns_convert_to_string
124 * PARAMETERS: original_object - Object to be converted
125 * return_object - Where the new converted object is returned
127 * RETURN: Status. AE_OK if conversion was successful.
129 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
131 ******************************************************************************/
133 acpi_status
134 acpi_ns_convert_to_string(union acpi_operand_object *original_object,
135 union acpi_operand_object **return_object)
137 union acpi_operand_object *new_object;
138 acpi_size length;
139 acpi_status status;
141 switch (original_object->common.type) {
142 case ACPI_TYPE_INTEGER:
144 * Integer-to-String conversion. Commonly, convert
145 * an integer of value 0 to a NULL string. The last element of
146 * _BIF and _BIX packages occasionally need this fix.
148 if (original_object->integer.value == 0) {
150 /* Allocate a new NULL string object */
152 new_object = acpi_ut_create_string_object(0);
153 if (!new_object) {
154 return (AE_NO_MEMORY);
156 } else {
157 status = acpi_ex_convert_to_string(original_object,
158 &new_object,
159 ACPI_IMPLICIT_CONVERT_HEX);
160 if (ACPI_FAILURE(status)) {
161 return (status);
164 break;
166 case ACPI_TYPE_BUFFER:
168 * Buffer-to-String conversion. Use a to_string
169 * conversion, no transform performed on the buffer data. The best
170 * example of this is the _BIF method, where the string data from
171 * the battery is often (incorrectly) returned as buffer object(s).
173 length = 0;
174 while ((length < original_object->buffer.length) &&
175 (original_object->buffer.pointer[length])) {
176 length++;
179 /* Allocate a new string object */
181 new_object = acpi_ut_create_string_object(length);
182 if (!new_object) {
183 return (AE_NO_MEMORY);
187 * Copy the raw buffer data with no transform. String is already NULL
188 * terminated at Length+1.
190 memcpy(new_object->string.pointer,
191 original_object->buffer.pointer, length);
192 break;
194 default:
196 return (AE_AML_OPERAND_TYPE);
199 *return_object = new_object;
200 return (AE_OK);
203 /*******************************************************************************
205 * FUNCTION: acpi_ns_convert_to_buffer
207 * PARAMETERS: original_object - Object to be converted
208 * return_object - Where the new converted object is returned
210 * RETURN: Status. AE_OK if conversion was successful.
212 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
214 ******************************************************************************/
216 acpi_status
217 acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
218 union acpi_operand_object **return_object)
220 union acpi_operand_object *new_object;
221 acpi_status status;
222 union acpi_operand_object **elements;
223 u32 *dword_buffer;
224 u32 count;
225 u32 i;
227 switch (original_object->common.type) {
228 case ACPI_TYPE_INTEGER:
230 * Integer-to-Buffer conversion.
231 * Convert the Integer to a packed-byte buffer. _MAT and other
232 * objects need this sometimes, if a read has been performed on a
233 * Field object that is less than or equal to the global integer
234 * size (32 or 64 bits).
236 status =
237 acpi_ex_convert_to_buffer(original_object, &new_object);
238 if (ACPI_FAILURE(status)) {
239 return (status);
241 break;
243 case ACPI_TYPE_STRING:
245 /* String-to-Buffer conversion. Simple data copy */
247 new_object = acpi_ut_create_buffer_object
248 (original_object->string.length);
249 if (!new_object) {
250 return (AE_NO_MEMORY);
253 memcpy(new_object->buffer.pointer,
254 original_object->string.pointer,
255 original_object->string.length);
256 break;
258 case ACPI_TYPE_PACKAGE:
260 * This case is often seen for predefined names that must return a
261 * Buffer object with multiple DWORD integers within. For example,
262 * _FDE and _GTM. The Package can be converted to a Buffer.
265 /* All elements of the Package must be integers */
267 elements = original_object->package.elements;
268 count = original_object->package.count;
270 for (i = 0; i < count; i++) {
271 if ((!*elements) ||
272 ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
273 return (AE_AML_OPERAND_TYPE);
275 elements++;
278 /* Create the new buffer object to replace the Package */
280 new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
281 if (!new_object) {
282 return (AE_NO_MEMORY);
285 /* Copy the package elements (integers) to the buffer as DWORDs */
287 elements = original_object->package.elements;
288 dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
290 for (i = 0; i < count; i++) {
291 *dword_buffer = (u32)(*elements)->integer.value;
292 dword_buffer++;
293 elements++;
295 break;
297 default:
299 return (AE_AML_OPERAND_TYPE);
302 *return_object = new_object;
303 return (AE_OK);
306 /*******************************************************************************
308 * FUNCTION: acpi_ns_convert_to_unicode
310 * PARAMETERS: scope - Namespace node for the method/object
311 * original_object - ASCII String Object to be converted
312 * return_object - Where the new converted object is returned
314 * RETURN: Status. AE_OK if conversion was successful.
316 * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.
318 ******************************************************************************/
320 acpi_status
321 acpi_ns_convert_to_unicode(struct acpi_namespace_node *scope,
322 union acpi_operand_object *original_object,
323 union acpi_operand_object **return_object)
325 union acpi_operand_object *new_object;
326 char *ascii_string;
327 u16 *unicode_buffer;
328 u32 unicode_length;
329 u32 i;
331 if (!original_object) {
332 return (AE_OK);
335 /* If a Buffer was returned, it must be at least two bytes long */
337 if (original_object->common.type == ACPI_TYPE_BUFFER) {
338 if (original_object->buffer.length < 2) {
339 return (AE_AML_OPERAND_VALUE);
342 *return_object = NULL;
343 return (AE_OK);
347 * The original object is an ASCII string. Convert this string to
348 * a unicode buffer.
350 ascii_string = original_object->string.pointer;
351 unicode_length = (original_object->string.length * 2) + 2;
353 /* Create a new buffer object for the Unicode data */
355 new_object = acpi_ut_create_buffer_object(unicode_length);
356 if (!new_object) {
357 return (AE_NO_MEMORY);
360 unicode_buffer = ACPI_CAST_PTR(u16, new_object->buffer.pointer);
362 /* Convert ASCII to Unicode */
364 for (i = 0; i < original_object->string.length; i++) {
365 unicode_buffer[i] = (u16)ascii_string[i];
368 *return_object = new_object;
369 return (AE_OK);
372 /*******************************************************************************
374 * FUNCTION: acpi_ns_convert_to_resource
376 * PARAMETERS: scope - Namespace node for the method/object
377 * original_object - Object to be converted
378 * return_object - Where the new converted object is returned
380 * RETURN: Status. AE_OK if conversion was successful
382 * DESCRIPTION: Attempt to convert a Integer object to a resource_template
383 * Buffer.
385 ******************************************************************************/
387 acpi_status
388 acpi_ns_convert_to_resource(struct acpi_namespace_node *scope,
389 union acpi_operand_object *original_object,
390 union acpi_operand_object **return_object)
392 union acpi_operand_object *new_object;
393 u8 *buffer;
396 * We can fix the following cases for an expected resource template:
397 * 1. No return value (interpreter slack mode is disabled)
398 * 2. A "Return (Zero)" statement
399 * 3. A "Return empty buffer" statement
401 * We will return a buffer containing a single end_tag
402 * resource descriptor.
404 if (original_object) {
405 switch (original_object->common.type) {
406 case ACPI_TYPE_INTEGER:
408 /* We can only repair an Integer==0 */
410 if (original_object->integer.value) {
411 return (AE_AML_OPERAND_TYPE);
413 break;
415 case ACPI_TYPE_BUFFER:
417 if (original_object->buffer.length) {
419 /* Additional checks can be added in the future */
421 *return_object = NULL;
422 return (AE_OK);
424 break;
426 case ACPI_TYPE_STRING:
427 default:
429 return (AE_AML_OPERAND_TYPE);
433 /* Create the new buffer object for the resource descriptor */
435 new_object = acpi_ut_create_buffer_object(2);
436 if (!new_object) {
437 return (AE_NO_MEMORY);
440 buffer = ACPI_CAST_PTR(u8, new_object->buffer.pointer);
442 /* Initialize the Buffer with a single end_tag descriptor */
444 buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);
445 buffer[1] = 0x00;
447 *return_object = new_object;
448 return (AE_OK);
451 /*******************************************************************************
453 * FUNCTION: acpi_ns_convert_to_reference
455 * PARAMETERS: scope - Namespace node for the method/object
456 * original_object - Object to be converted
457 * return_object - Where the new converted object is returned
459 * RETURN: Status. AE_OK if conversion was successful
461 * DESCRIPTION: Attempt to convert a Integer object to a object_reference.
462 * Buffer.
464 ******************************************************************************/
466 acpi_status
467 acpi_ns_convert_to_reference(struct acpi_namespace_node *scope,
468 union acpi_operand_object *original_object,
469 union acpi_operand_object **return_object)
471 union acpi_operand_object *new_object = NULL;
472 acpi_status status;
473 struct acpi_namespace_node *node;
474 union acpi_generic_state scope_info;
475 char *name;
477 ACPI_FUNCTION_NAME(ns_convert_to_reference);
479 /* Convert path into internal presentation */
481 status =
482 acpi_ns_internalize_name(original_object->string.pointer, &name);
483 if (ACPI_FAILURE(status)) {
484 return_ACPI_STATUS(status);
487 /* Find the namespace node */
489 scope_info.scope.node =
490 ACPI_CAST_PTR(struct acpi_namespace_node, scope);
491 status =
492 acpi_ns_lookup(&scope_info, name, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
493 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
494 NULL, &node);
495 if (ACPI_FAILURE(status)) {
497 /* Check if we are resolving a named reference within a package */
499 ACPI_ERROR_NAMESPACE(original_object->string.pointer, status);
500 goto error_exit;
503 /* Create and init a new internal ACPI object */
505 new_object = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
506 if (!new_object) {
507 status = AE_NO_MEMORY;
508 goto error_exit;
510 new_object->reference.node = node;
511 new_object->reference.object = node->object;
512 new_object->reference.class = ACPI_REFCLASS_NAME;
515 * Increase reference of the object if needed (the object is likely a
516 * null for device nodes).
518 acpi_ut_add_reference(node->object);
520 error_exit:
521 ACPI_FREE(name);
522 *return_object = new_object;
523 return (AE_OK);