ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / acpi / executer / exmisc.c
1
2 /******************************************************************************
3  *
4  * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2004, R. Byron Moore
10  * All rights reserved.
11  *
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.
26  *
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.
30  *
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.
43  */
44
45
46 #include <acpi/acpi.h>
47 #include <acpi/acinterp.h>
48 #include <acpi/amlcode.h>
49
50
51 #define _COMPONENT          ACPI_EXECUTER
52          ACPI_MODULE_NAME    ("exmisc")
53
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    acpi_ex_get_object_reference
58  *
59  * PARAMETERS:  obj_desc            - Create a reference to this object
60  *              return_desc         - Where to store the reference
61  *              walk_state          - Current state
62  *
63  * RETURN:      Status
64  *
65  * DESCRIPTION: Obtain and return a "reference" to the target object
66  *              Common code for the ref_of_op and the cond_ref_of_op.
67  *
68  ******************************************************************************/
69
70 acpi_status
71 acpi_ex_get_object_reference (
72         union acpi_operand_object       *obj_desc,
73         union acpi_operand_object       **return_desc,
74         struct acpi_walk_state          *walk_state)
75 {
76         union acpi_operand_object       *reference_obj;
77         union acpi_operand_object       *referenced_obj;
78
79
80         ACPI_FUNCTION_TRACE_PTR ("ex_get_object_reference", obj_desc);
81
82
83         *return_desc = NULL;
84
85         switch (ACPI_GET_DESCRIPTOR_TYPE (obj_desc)) {
86         case ACPI_DESC_TYPE_OPERAND:
87
88                 if (ACPI_GET_OBJECT_TYPE (obj_desc) != ACPI_TYPE_LOCAL_REFERENCE) {
89                         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
90                 }
91
92                 /*
93                  * Must be a reference to a Local or Arg
94                  */
95                 switch (obj_desc->reference.opcode) {
96                 case AML_LOCAL_OP:
97                 case AML_ARG_OP:
98
99                         /* The referenced object is the pseudo-node for the local/arg */
100
101                         referenced_obj = obj_desc->reference.object;
102                         break;
103
104                 default:
105
106                         ACPI_REPORT_ERROR (("Unknown Reference subtype in get ref %X\n",
107                                 obj_desc->reference.opcode));
108                         return_ACPI_STATUS (AE_AML_INTERNAL);
109                 }
110                 break;
111
112
113         case ACPI_DESC_TYPE_NAMED:
114
115                 /*
116                  * A named reference that has already been resolved to a Node
117                  */
118                 referenced_obj = obj_desc;
119                 break;
120
121
122         default:
123
124                 ACPI_REPORT_ERROR (("Invalid descriptor type in get ref: %X\n",
125                                 ACPI_GET_DESCRIPTOR_TYPE (obj_desc)));
126                 return_ACPI_STATUS (AE_TYPE);
127         }
128
129
130         /* Create a new reference object */
131
132         reference_obj = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_REFERENCE);
133         if (!reference_obj) {
134                 return_ACPI_STATUS (AE_NO_MEMORY);
135         }
136
137         reference_obj->reference.opcode = AML_REF_OF_OP;
138         reference_obj->reference.object = referenced_obj;
139         *return_desc = reference_obj;
140
141         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p Type [%s], returning Reference %p\n",
142                         obj_desc, acpi_ut_get_object_type_name (obj_desc), *return_desc));
143
144         return_ACPI_STATUS (AE_OK);
145 }
146
147
148 /*******************************************************************************
149  *
150  * FUNCTION:    acpi_ex_concat_template
151  *
152  * PARAMETERS:  *obj_desc           - Object to be converted.  Must be an
153  *                                    Integer, Buffer, or String
154  *              walk_state          - Current walk state
155  *
156  * RETURN:      Status
157  *
158  * DESCRIPTION: Concatenate two resource templates
159  *
160  ******************************************************************************/
161
162 acpi_status
163 acpi_ex_concat_template (
164         union acpi_operand_object       *obj_desc1,
165         union acpi_operand_object       *obj_desc2,
166         union acpi_operand_object       **actual_return_desc,
167         struct acpi_walk_state          *walk_state)
168 {
169         union acpi_operand_object       *return_desc;
170         u8                              *new_buf;
171         u8                              *end_tag1;
172         u8                              *end_tag2;
173         acpi_size                       length1;
174         acpi_size                       length2;
175
176
177         ACPI_FUNCTION_TRACE ("ex_concat_template");
178
179
180         /* Find the end_tags in each resource template */
181
182         end_tag1 = acpi_ut_get_resource_end_tag (obj_desc1);
183         end_tag2 = acpi_ut_get_resource_end_tag (obj_desc2);
184         if (!end_tag1 || !end_tag2) {
185                 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
186         }
187
188         /* Compute the length of each part */
189
190         length1 = ACPI_PTR_DIFF (end_tag1, obj_desc1->buffer.pointer);
191         length2 = ACPI_PTR_DIFF (end_tag2, obj_desc2->buffer.pointer) +
192                           2; /* Size of END_TAG */
193
194         /* Create a new buffer object for the result */
195
196         return_desc = acpi_ut_create_buffer_object (length1 + length2);
197         if (!return_desc) {
198                 return_ACPI_STATUS (AE_NO_MEMORY);
199         }
200
201         /* Copy the templates to the new descriptor */
202
203         new_buf = return_desc->buffer.pointer;
204         ACPI_MEMCPY (new_buf, obj_desc1->buffer.pointer, length1);
205         ACPI_MEMCPY (new_buf + length1, obj_desc2->buffer.pointer, length2);
206
207         /* Compute the new checksum */
208
209         new_buf[return_desc->buffer.length - 1] =
210                         acpi_ut_generate_checksum (return_desc->buffer.pointer,
211                                            (return_desc->buffer.length - 1));
212
213         /* Return the completed template descriptor */
214
215         *actual_return_desc = return_desc;
216         return_ACPI_STATUS (AE_OK);
217 }
218
219
220 /*******************************************************************************
221  *
222  * FUNCTION:    acpi_ex_do_concatenate
223  *
224  * PARAMETERS:  obj_desc1           - First source object
225  *              obj_desc2           - Second source object
226  *              actual_return_desc  - Where to place the return object
227  *              walk_state          - Current walk state
228  *
229  * RETURN:      Status
230  *
231  * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
232  *
233  ******************************************************************************/
234
235 acpi_status
236 acpi_ex_do_concatenate (
237         union acpi_operand_object       *obj_desc1,
238         union acpi_operand_object       *obj_desc2,
239         union acpi_operand_object       **actual_return_desc,
240         struct acpi_walk_state          *walk_state)
241 {
242         acpi_status                     status;
243         u32                             i;
244         acpi_integer                    this_integer;
245         union acpi_operand_object       *return_desc;
246         char                            *new_buf;
247
248
249         ACPI_FUNCTION_ENTRY ();
250
251
252         /*
253          * There are three cases to handle:
254          *
255          * 1) Two Integers concatenated to produce a new Buffer
256          * 2) Two Strings concatenated to produce a new String
257          * 3) Two Buffers concatenated to produce a new Buffer
258          */
259         switch (ACPI_GET_OBJECT_TYPE (obj_desc1)) {
260         case ACPI_TYPE_INTEGER:
261
262                 /* Result of two Integers is a Buffer */
263                 /* Need enough buffer space for two integers */
264
265                 return_desc = acpi_ut_create_buffer_object (acpi_gbl_integer_byte_width * 2);
266                 if (!return_desc) {
267                         return (AE_NO_MEMORY);
268                 }
269
270                 new_buf = (char *) return_desc->buffer.pointer;
271
272                 /* Convert the first integer */
273
274                 this_integer = obj_desc1->integer.value;
275                 for (i = 0; i < acpi_gbl_integer_byte_width; i++) {
276                         new_buf[i] = (char) this_integer;
277                         this_integer >>= 8;
278                 }
279
280                 /* Convert the second integer */
281
282                 this_integer = obj_desc2->integer.value;
283                 for (; i < (ACPI_MUL_2 (acpi_gbl_integer_byte_width)); i++) {
284                         new_buf[i] = (char) this_integer;
285                         this_integer >>= 8;
286                 }
287
288                 break;
289
290
291         case ACPI_TYPE_STRING:
292
293                 /* Result of two Strings is a String */
294
295                 return_desc = acpi_ut_create_internal_object (ACPI_TYPE_STRING);
296                 if (!return_desc) {
297                         return (AE_NO_MEMORY);
298                 }
299
300                 /* Operand0 is string  */
301
302                 new_buf = ACPI_MEM_CALLOCATE ((acpi_size) obj_desc1->string.length +
303                                    (acpi_size) obj_desc2->string.length + 1);
304                 if (!new_buf) {
305                         ACPI_REPORT_ERROR
306                                 (("ex_do_concatenate: String allocation failure\n"));
307                         status = AE_NO_MEMORY;
308                         goto cleanup;
309                 }
310
311                 /* Concatenate the strings */
312
313                 ACPI_STRCPY (new_buf, obj_desc1->string.pointer);
314                 ACPI_STRCPY (new_buf + obj_desc1->string.length,
315                                   obj_desc2->string.pointer);
316
317                 /* Complete the String object initialization */
318
319                 return_desc->string.pointer = new_buf;
320                 return_desc->string.length = obj_desc1->string.length +
321                                    obj_desc2->string.length;
322                 break;
323
324
325         case ACPI_TYPE_BUFFER:
326
327                 /* Result of two Buffers is a Buffer */
328
329                 return_desc = acpi_ut_create_buffer_object (
330                                    (acpi_size) obj_desc1->buffer.length +
331                                    (acpi_size) obj_desc2->buffer.length);
332                 if (!return_desc) {
333                         return (AE_NO_MEMORY);
334                 }
335
336                 new_buf = (char *) return_desc->buffer.pointer;
337
338                 /* Concatenate the buffers */
339
340                 ACPI_MEMCPY (new_buf, obj_desc1->buffer.pointer,
341                                   obj_desc1->buffer.length);
342                 ACPI_MEMCPY (new_buf + obj_desc1->buffer.length, obj_desc2->buffer.pointer,
343                                    obj_desc2->buffer.length);
344
345                 break;
346
347
348         default:
349
350                 /* Invalid object type, should not happen here */
351
352                 ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
353                                 ACPI_GET_OBJECT_TYPE (obj_desc1)));
354                 status = AE_AML_INTERNAL;
355                 return_desc = NULL;
356         }
357
358         *actual_return_desc = return_desc;
359         return (AE_OK);
360
361
362 cleanup:
363
364         acpi_ut_remove_reference (return_desc);
365         return (status);
366 }
367
368
369 /*******************************************************************************
370  *
371  * FUNCTION:    acpi_ex_do_math_op
372  *
373  * PARAMETERS:  Opcode              - AML opcode
374  *              Operand0            - Integer operand #0
375  *              Operand1            - Integer operand #1
376  *
377  * RETURN:      Integer result of the operation
378  *
379  * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
380  *              math functions here is to prevent a lot of pointer dereferencing
381  *              to obtain the operands.
382  *
383  ******************************************************************************/
384
385 acpi_integer
386 acpi_ex_do_math_op (
387         u16                             opcode,
388         acpi_integer                    operand0,
389         acpi_integer                    operand1)
390 {
391
392
393         switch (opcode) {
394         case AML_ADD_OP:                /* Add (Operand0, Operand1, Result) */
395
396                 return (operand0 + operand1);
397
398
399         case AML_BIT_AND_OP:            /* And (Operand0, Operand1, Result) */
400
401                 return (operand0 & operand1);
402
403
404         case AML_BIT_NAND_OP:           /* NAnd (Operand0, Operand1, Result) */
405
406                 return (~(operand0 & operand1));
407
408
409         case AML_BIT_OR_OP:             /* Or (Operand0, Operand1, Result) */
410
411                 return (operand0 | operand1);
412
413
414         case AML_BIT_NOR_OP:            /* NOr (Operand0, Operand1, Result) */
415
416                 return (~(operand0 | operand1));
417
418
419         case AML_BIT_XOR_OP:            /* XOr (Operand0, Operand1, Result) */
420
421                 return (operand0 ^ operand1);
422
423
424         case AML_MULTIPLY_OP:           /* Multiply (Operand0, Operand1, Result) */
425
426                 return (operand0 * operand1);
427
428
429         case AML_SHIFT_LEFT_OP:         /* shift_left (Operand, shift_count, Result) */
430
431                 return (operand0 << operand1);
432
433
434         case AML_SHIFT_RIGHT_OP:        /* shift_right (Operand, shift_count, Result) */
435
436                 return (operand0 >> operand1);
437
438
439         case AML_SUBTRACT_OP:           /* Subtract (Operand0, Operand1, Result) */
440
441                 return (operand0 - operand1);
442
443         default:
444
445                 return (0);
446         }
447 }
448
449
450 /*******************************************************************************
451  *
452  * FUNCTION:    acpi_ex_do_logical_op
453  *
454  * PARAMETERS:  Opcode              - AML opcode
455  *              Operand0            - Integer operand #0
456  *              Operand1            - Integer operand #1
457  *
458  * RETURN:      TRUE/FALSE result of the operation
459  *
460  * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
461  *              functions here is to prevent a lot of pointer dereferencing
462  *              to obtain the operands and to simplify the generation of the
463  *              logical value.
464  *
465  *              Note: cleanest machine code seems to be produced by the code
466  *              below, rather than using statements of the form:
467  *                  Result = (Operand0 == Operand1);
468  *
469  ******************************************************************************/
470
471 u8
472 acpi_ex_do_logical_op (
473         u16                             opcode,
474         acpi_integer                    operand0,
475         acpi_integer                    operand1)
476 {
477
478
479         switch (opcode) {
480
481         case AML_LAND_OP:               /* LAnd (Operand0, Operand1) */
482
483                 if (operand0 && operand1) {
484                         return (TRUE);
485                 }
486                 break;
487
488
489         case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
490
491                 if (operand0 == operand1) {
492                         return (TRUE);
493                 }
494                 break;
495
496
497         case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
498
499                 if (operand0 > operand1) {
500                         return (TRUE);
501                 }
502                 break;
503
504
505         case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
506
507                 if (operand0 < operand1) {
508                         return (TRUE);
509                 }
510                 break;
511
512
513         case AML_LOR_OP:                 /* LOr (Operand0, Operand1) */
514
515                 if (operand0 || operand1) {
516                         return (TRUE);
517                 }
518                 break;
519
520         default:
521                 break;
522         }
523
524         return (FALSE);
525 }
526
527