vserver 1.9.5.x5
[linux-2.6.git] / drivers / acpi / executer / exconvrt.c
1 /******************************************************************************
2  *
3  * Module Name: exconvrt - Object conversion routines
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
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
45 #include <acpi/acpi.h>
46 #include <acpi/acinterp.h>
47 #include <acpi/amlcode.h>
48
49
50 #define _COMPONENT          ACPI_EXECUTER
51          ACPI_MODULE_NAME    ("exconvrt")
52
53
54 /*******************************************************************************
55  *
56  * FUNCTION:    acpi_ex_convert_to_integer
57  *
58  * PARAMETERS:  obj_desc        - Object to be converted.  Must be an
59  *                                Integer, Buffer, or String
60  *              result_desc     - Where the new Integer object is returned
61  *              Flags           - Used for string conversion
62  *
63  * RETURN:      Status
64  *
65  * DESCRIPTION: Convert an ACPI Object to an integer.
66  *
67  ******************************************************************************/
68
69 acpi_status
70 acpi_ex_convert_to_integer (
71         union acpi_operand_object       *obj_desc,
72         union acpi_operand_object       **result_desc,
73         u32                             flags)
74 {
75         union acpi_operand_object       *return_desc;
76         u8                              *pointer;
77         acpi_integer                    result;
78         u32                             i;
79         u32                             count;
80         acpi_status                     status;
81
82
83         ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_integer", obj_desc);
84
85
86         switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
87         case ACPI_TYPE_INTEGER:
88
89                 /* No conversion necessary */
90
91                 *result_desc = obj_desc;
92                 return_ACPI_STATUS (AE_OK);
93
94         case ACPI_TYPE_BUFFER:
95         case ACPI_TYPE_STRING:
96
97                 /* Note: Takes advantage of common buffer/string fields */
98
99                 pointer = obj_desc->buffer.pointer;
100                 count   = obj_desc->buffer.length;
101                 break;
102
103         default:
104                 return_ACPI_STATUS (AE_TYPE);
105         }
106
107         /*
108          * Convert the buffer/string to an integer.  Note that both buffers and
109          * strings are treated as raw data - we don't convert ascii to hex for
110          * strings.
111          *
112          * There are two terminating conditions for the loop:
113          * 1) The size of an integer has been reached, or
114          * 2) The end of the buffer or string has been reached
115          */
116         result = 0;
117
118         /*
119          * String conversion is different than Buffer conversion
120          */
121         switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
122         case ACPI_TYPE_STRING:
123
124                 /*
125                  * Convert string to an integer - for most cases, the string must be
126                  * hexadecimal as per the ACPI specification.  The only exception (as
127                  * of ACPI 3.0) is that the to_integer() operator allows both decimal
128                  * and hexadecimal strings (hex prefixed with "0x").
129                  */
130                 status = acpi_ut_strtoul64 ((char *) pointer, flags, &result);
131                 if (ACPI_FAILURE (status)) {
132                         return_ACPI_STATUS (status);
133                 }
134                 break;
135
136
137         case ACPI_TYPE_BUFFER:
138
139                 /* Check for zero-length buffer */
140
141                 if (!count) {
142                         return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
143                 }
144
145                 /* Transfer no more than an integer's worth of data */
146
147                 if (count > acpi_gbl_integer_byte_width) {
148                         count = acpi_gbl_integer_byte_width;
149                 }
150
151                 /*
152                  * Convert buffer to an integer - we simply grab enough raw data
153                  * from the buffer to fill an integer
154                  */
155                 for (i = 0; i < count; i++) {
156                         /*
157                          * Get next byte and shift it into the Result.
158                          * Little endian is used, meaning that the first byte of the buffer
159                          * is the LSB of the integer
160                          */
161                         result |= (((acpi_integer) pointer[i]) << (i * 8));
162                 }
163                 break;
164
165
166         default:
167                 /* No other types can get here */
168                 break;
169         }
170
171         /*
172          * Create a new integer
173          */
174         return_desc = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
175         if (!return_desc) {
176                 return_ACPI_STATUS (AE_NO_MEMORY);
177         }
178
179         /* Save the Result */
180
181         return_desc->integer.value = result;
182         acpi_ex_truncate_for32bit_table (return_desc);
183         *result_desc = return_desc;
184         return_ACPI_STATUS (AE_OK);
185 }
186
187
188 /*******************************************************************************
189  *
190  * FUNCTION:    acpi_ex_convert_to_buffer
191  *
192  * PARAMETERS:  obj_desc        - Object to be converted.  Must be an
193  *                                Integer, Buffer, or String
194  *              result_desc     - Where the new buffer object is returned
195  *
196  * RETURN:      Status
197  *
198  * DESCRIPTION: Convert an ACPI Object to a Buffer
199  *
200  ******************************************************************************/
201
202 acpi_status
203 acpi_ex_convert_to_buffer (
204         union acpi_operand_object       *obj_desc,
205         union acpi_operand_object       **result_desc)
206 {
207         union acpi_operand_object       *return_desc;
208         u8                              *new_buf;
209
210
211         ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_buffer", obj_desc);
212
213
214         switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
215         case ACPI_TYPE_BUFFER:
216
217                 /* No conversion necessary */
218
219                 *result_desc = obj_desc;
220                 return_ACPI_STATUS (AE_OK);
221
222
223         case ACPI_TYPE_INTEGER:
224
225                 /*
226                  * Create a new Buffer object.
227                  * Need enough space for one integer
228                  */
229                 return_desc = acpi_ut_create_buffer_object (acpi_gbl_integer_byte_width);
230                 if (!return_desc) {
231                         return_ACPI_STATUS (AE_NO_MEMORY);
232                 }
233
234                 /* Copy the integer to the buffer, LSB first */
235
236                 new_buf = return_desc->buffer.pointer;
237                 ACPI_MEMCPY (new_buf,
238                                   &obj_desc->integer.value,
239                                   acpi_gbl_integer_byte_width);
240                 break;
241
242
243         case ACPI_TYPE_STRING:
244
245                 /*
246                  * Create a new Buffer object
247                  * Size will be the string length
248                  *
249                  * NOTE: Add one to the string length to include the null terminator.
250                  * The ACPI spec is unclear on this subject, but there is existing
251                  * ASL/AML code that depends on the null being transferred to the new
252                  * buffer.
253                  */
254                 return_desc = acpi_ut_create_buffer_object ((acpi_size) obj_desc->string.length + 1);
255                 if (!return_desc) {
256                         return_ACPI_STATUS (AE_NO_MEMORY);
257                 }
258
259                 /* Copy the string to the buffer */
260
261                 new_buf = return_desc->buffer.pointer;
262                 ACPI_STRNCPY ((char *) new_buf, (char *) obj_desc->string.pointer,
263                         obj_desc->string.length);
264                 break;
265
266
267         default:
268                 return_ACPI_STATUS (AE_TYPE);
269         }
270
271         /* Mark buffer initialized */
272
273         return_desc->common.flags |= AOPOBJ_DATA_VALID;
274         *result_desc = return_desc;
275         return_ACPI_STATUS (AE_OK);
276 }
277
278
279 /*******************************************************************************
280  *
281  * FUNCTION:    acpi_ex_convert_to_ascii
282  *
283  * PARAMETERS:  Integer         - Value to be converted
284  *              Base            - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
285  *              String          - Where the string is returned
286  *              data_width      - Size of data item to be converted, in bytes
287  *
288  * RETURN:      Actual string length
289  *
290  * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
291  *
292  ******************************************************************************/
293
294 u32
295 acpi_ex_convert_to_ascii (
296         acpi_integer                    integer,
297         u16                             base,
298         u8                              *string,
299         u8                              data_width)
300 {
301         acpi_integer                    digit;
302         acpi_native_uint                i;
303         acpi_native_uint                j;
304         acpi_native_uint                k = 0;
305         acpi_native_uint                hex_length;
306         acpi_native_uint                decimal_length;
307         u32                             remainder;
308         u8                              supress_zeros;
309
310
311         ACPI_FUNCTION_ENTRY ();
312
313
314         switch (base) {
315         case 10:
316
317                 /* Setup max length for the decimal number */
318
319                 switch (data_width) {
320                 case 1:
321                         decimal_length = ACPI_MAX8_DECIMAL_DIGITS;
322                         break;
323
324                 case 4:
325                         decimal_length = ACPI_MAX32_DECIMAL_DIGITS;
326                         break;
327
328                 case 8:
329                 default:
330                         decimal_length = ACPI_MAX64_DECIMAL_DIGITS;
331                         break;
332                 }
333
334                 supress_zeros = TRUE;    /* No leading zeros */
335                 remainder = 0;
336
337                 for (i = decimal_length; i > 0; i--) {
338                         /* Divide by nth factor of 10 */
339
340                         digit = integer;
341                         for (j = 0; j < i; j++) {
342                                 (void) acpi_ut_short_divide (digit, 10, &digit, &remainder);
343                         }
344
345                         /* Handle leading zeros */
346
347                         if (remainder != 0) {
348                                 supress_zeros = FALSE;
349                         }
350
351                         if (!supress_zeros) {
352                                 string[k] = (u8) (ACPI_ASCII_ZERO + remainder);
353                                 k++;
354                         }
355                 }
356                 break;
357
358         case 16:
359
360                 hex_length = ACPI_MUL_2 (data_width); /* 2 ascii hex chars per data byte */
361
362                 for (i = 0, j = (hex_length-1); i < hex_length; i++, j--) {
363                         /* Get one hex digit, most significant digits first */
364
365                         string[k] = (u8) acpi_ut_hex_to_ascii_char (integer, ACPI_MUL_4 (j));
366                         k++;
367                 }
368                 break;
369
370         default:
371                 return (0);
372         }
373
374         /*
375          * Since leading zeros are supressed, we must check for the case where
376          * the integer equals 0
377          *
378          * Finally, null terminate the string and return the length
379          */
380         if (!k) {
381                 string [0] = ACPI_ASCII_ZERO;
382                 k = 1;
383         }
384
385         string [k] = 0;
386         return ((u32) k);
387 }
388
389
390 /*******************************************************************************
391  *
392  * FUNCTION:    acpi_ex_convert_to_string
393  *
394  * PARAMETERS:  obj_desc        - Object to be converted.  Must be an
395  *                                Integer, Buffer, or String
396  *              result_desc     - Where the string object is returned
397  *              Type            - String flags (base and conversion type)
398  *
399  * RETURN:      Status
400  *
401  * DESCRIPTION: Convert an ACPI Object to a string
402  *
403  ******************************************************************************/
404
405 acpi_status
406 acpi_ex_convert_to_string (
407         union acpi_operand_object       *obj_desc,
408         union acpi_operand_object       **result_desc,
409         u32                             type)
410 {
411         union acpi_operand_object       *return_desc;
412         u8                              *new_buf;
413         u32                             i;
414         u32                             string_length = 0;
415         u16                             base = 16;
416         u8                              separator = ',';
417
418
419         ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_string", obj_desc);
420
421
422         switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
423         case ACPI_TYPE_STRING:
424
425                 /* No conversion necessary */
426
427                 *result_desc = obj_desc;
428                 return_ACPI_STATUS (AE_OK);
429
430
431         case ACPI_TYPE_INTEGER:
432
433                 switch (type) {
434                 case ACPI_EXPLICIT_CONVERT_DECIMAL:
435
436                         /* Make room for maximum decimal number */
437
438                         string_length = ACPI_MAX_DECIMAL_DIGITS;
439                         base = 10;
440                         break;
441
442                 default:
443
444                         /* Two hex string characters for each integer byte */
445
446                         string_length = ACPI_MUL_2 (acpi_gbl_integer_byte_width);
447                         break;
448                 }
449
450                 /*
451                  * Create a new String
452                  * Need enough space for one ASCII integer (plus null terminator)
453                  */
454                 return_desc = acpi_ut_create_string_object ((acpi_size) string_length);
455                 if (!return_desc) {
456                         return_ACPI_STATUS (AE_NO_MEMORY);
457                 }
458
459                 new_buf = return_desc->buffer.pointer;
460
461                 /* Convert integer to string */
462
463                 string_length = acpi_ex_convert_to_ascii (obj_desc->integer.value, base,
464                                    new_buf, acpi_gbl_integer_byte_width);
465
466                 /* Null terminate at the correct place */
467
468                 return_desc->string.length = string_length;
469                 new_buf [string_length] = 0;
470                 break;
471
472
473         case ACPI_TYPE_BUFFER:
474
475                 /* Setup string length, base, and separator */
476
477                 switch (type) {
478                 case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by to_decimal_string operator */
479                         /*
480                          * From ACPI: "If Data is a buffer, it is converted to a string of
481                          * decimal values separated by commas."
482                          */
483                         base = 10;
484
485                         /*
486                          * Calculate the final string length.  Individual string values
487                          * are variable length (include separator for each)
488                          */
489                         for (i = 0; i < obj_desc->buffer.length; i++) {
490                                 if (obj_desc->buffer.pointer[i] >= 100) {
491                                         string_length += 4;
492                                 }
493                                 else if (obj_desc->buffer.pointer[i] >= 10) {
494                                         string_length += 3;
495                                 }
496                                 else {
497                                         string_length += 2;
498                                 }
499                         }
500                         break;
501
502                 case ACPI_IMPLICIT_CONVERT_HEX:
503                         /*
504                          * From the ACPI spec:
505                          *"The entire contents of the buffer are converted to a string of
506                          * two-character hexadecimal numbers, each separated by a space."
507                          */
508                         separator = ' ';
509                         string_length = (obj_desc->buffer.length * 3);
510                         break;
511
512                 case ACPI_EXPLICIT_CONVERT_HEX:     /* Used by to_hex_string operator */
513                         /*
514                          * From ACPI: "If Data is a buffer, it is converted to a string of
515                          * hexadecimal values separated by commas."
516                          */
517                         string_length = (obj_desc->buffer.length * 3);
518                         break;
519
520                 default:
521                         return_ACPI_STATUS (AE_BAD_PARAMETER);
522                 }
523
524                 /*
525                  * Perform the conversion.
526                  * (-1 because of extra separator included in string_length from above)
527                  */
528                 string_length--;
529                 if (string_length > ACPI_MAX_STRING_CONVERSION) /* ACPI limit */ {
530                         return_ACPI_STATUS (AE_AML_STRING_LIMIT);
531                 }
532
533                 /*
534                  * Create a new string object and string buffer
535                  */
536                 return_desc = acpi_ut_create_string_object ((acpi_size) string_length);
537                 if (!return_desc) {
538                         return_ACPI_STATUS (AE_NO_MEMORY);
539                 }
540
541                 new_buf = return_desc->buffer.pointer;
542
543                 /*
544                  * Convert buffer bytes to hex or decimal values
545                  * (separated by commas or spaces)
546                  */
547                 for (i = 0; i < obj_desc->buffer.length; i++) {
548                         new_buf += acpi_ex_convert_to_ascii (
549                                          (acpi_integer) obj_desc->buffer.pointer[i], base,
550                                          new_buf, 1);
551                         *new_buf++ = separator; /* each separated by a comma or space */
552                 }
553
554                 /* Null terminate the string (overwrites final comma/space from above) */
555
556                 new_buf--;
557                 *new_buf = 0;
558                 break;
559
560         default:
561                 return_ACPI_STATUS (AE_TYPE);
562         }
563
564         *result_desc = return_desc;
565         return_ACPI_STATUS (AE_OK);
566 }
567
568
569 /*******************************************************************************
570  *
571  * FUNCTION:    acpi_ex_convert_to_target_type
572  *
573  * PARAMETERS:  destination_type    - Current type of the destination
574  *              source_desc         - Source object to be converted.
575  *              result_desc         - Where the converted object is returned
576  *              walk_state          - Current method state
577  *
578  * RETURN:      Status
579  *
580  * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
581  *
582  ******************************************************************************/
583
584 acpi_status
585 acpi_ex_convert_to_target_type (
586         acpi_object_type                destination_type,
587         union acpi_operand_object       *source_desc,
588         union acpi_operand_object       **result_desc,
589         struct acpi_walk_state          *walk_state)
590 {
591         acpi_status                     status = AE_OK;
592
593
594         ACPI_FUNCTION_TRACE ("ex_convert_to_target_type");
595
596
597         /* Default behavior */
598
599         *result_desc = source_desc;
600
601         /*
602          * If required by the target,
603          * perform implicit conversion on the source before we store it.
604          */
605         switch (GET_CURRENT_ARG_TYPE (walk_state->op_info->runtime_args)) {
606         case ARGI_SIMPLE_TARGET:
607         case ARGI_FIXED_TARGET:
608         case ARGI_INTEGER_REF:      /* Handles Increment, Decrement cases */
609
610                 switch (destination_type) {
611                 case ACPI_TYPE_LOCAL_REGION_FIELD:
612                         /*
613                          * Named field can always handle conversions
614                          */
615                         break;
616
617                 default:
618                         /* No conversion allowed for these types */
619
620                         if (destination_type != ACPI_GET_OBJECT_TYPE (source_desc)) {
621                                 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
622                                         "Explicit operator, will store (%s) over existing type (%s)\n",
623                                         acpi_ut_get_object_type_name (source_desc),
624                                         acpi_ut_get_type_name (destination_type)));
625                                 status = AE_TYPE;
626                         }
627                 }
628                 break;
629
630
631         case ARGI_TARGETREF:
632
633                 switch (destination_type) {
634                 case ACPI_TYPE_INTEGER:
635                 case ACPI_TYPE_BUFFER_FIELD:
636                 case ACPI_TYPE_LOCAL_BANK_FIELD:
637                 case ACPI_TYPE_LOCAL_INDEX_FIELD:
638                         /*
639                          * These types require an Integer operand.  We can convert
640                          * a Buffer or a String to an Integer if necessary.
641                          */
642                         status = acpi_ex_convert_to_integer (source_desc, result_desc,
643                                          16);
644                         break;
645
646
647                 case ACPI_TYPE_STRING:
648
649                         /*
650                          * The operand must be a String.  We can convert an
651                          * Integer or Buffer if necessary
652                          */
653                         status = acpi_ex_convert_to_string (source_desc, result_desc,
654                                          ACPI_IMPLICIT_CONVERT_HEX);
655                         break;
656
657
658                 case ACPI_TYPE_BUFFER:
659
660                         /*
661                          * The operand must be a Buffer.  We can convert an
662                          * Integer or String if necessary
663                          */
664                         status = acpi_ex_convert_to_buffer (source_desc, result_desc);
665                         break;
666
667
668                 default:
669                         ACPI_REPORT_ERROR (("Bad destination type during conversion: %X\n",
670                                 destination_type));
671                         status = AE_AML_INTERNAL;
672                         break;
673                 }
674                 break;
675
676
677         case ARGI_REFERENCE:
678                 /*
679                  * create_xxxx_field cases - we are storing the field object into the name
680                  */
681                 break;
682
683
684         default:
685                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
686                         "Unknown Target type ID 0x%X Op %s dest_type %s\n",
687                         GET_CURRENT_ARG_TYPE (walk_state->op_info->runtime_args),
688                         walk_state->op_info->name, acpi_ut_get_type_name (destination_type)));
689
690                 ACPI_REPORT_ERROR (("Bad Target Type (ARGI): %X\n",
691                         GET_CURRENT_ARG_TYPE (walk_state->op_info->runtime_args)))
692                 status = AE_AML_INTERNAL;
693         }
694
695         /*
696          * Source-to-Target conversion semantics:
697          *
698          * If conversion to the target type cannot be performed, then simply
699          * overwrite the target with the new object and type.
700          */
701         if (status == AE_TYPE) {
702                 status = AE_OK;
703         }
704
705         return_ACPI_STATUS (status);
706 }
707
708