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