:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / drivers / bus / acpi / utils / cmcopy.c
1 /******************************************************************************
2  *
3  * Module Name: cmcopy - Internal to external object translation utilities
4  *              $Revision$
5  *
6  *****************************************************************************/
7
8 /*
9  *  Copyright (C) 2000, 2001 R. Byron Moore
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26
27 #include "acpi.h"
28 #include "acinterp.h"
29 #include "acnamesp.h"
30 #include "amlcode.h"
31
32
33 #define _COMPONENT          ACPI_UTILITIES
34          MODULE_NAME         ("cmcopy")
35
36
37 /*******************************************************************************
38  *
39  * FUNCTION:    Acpi_cm_copy_isimple_to_esimple
40  *
41  * PARAMETERS:  *Internal_object    - Pointer to the object we are examining
42  *              *Buffer             - Where the object is returned
43  *              *Space_used         - Where the data length is returned
44  *
45  * RETURN:      Status
46  *
47  * DESCRIPTION: This function is called to place a simple object in a user
48  *              buffer.
49  *
50  *              The buffer is assumed to have sufficient space for the object.
51  *
52  ******************************************************************************/
53
54 static ACPI_STATUS
55 acpi_cm_copy_isimple_to_esimple (
56         ACPI_OPERAND_OBJECT     *internal_object,
57         ACPI_OBJECT             *external_object,
58         u8                      *data_space,
59         u32                     *buffer_space_used)
60 {
61         u32                     length = 0;
62         ACPI_STATUS             status = AE_OK;
63
64
65         /*
66          * Check for NULL object case (could be an uninitialized
67          * package element
68          */
69
70         if (!internal_object) {
71                 *buffer_space_used = 0;
72                 return (AE_OK);
73         }
74
75         /* Always clear the external object */
76
77         MEMSET (external_object, 0, sizeof (ACPI_OBJECT));
78
79         /*
80          * In general, the external object will be the same type as
81          * the internal object
82          */
83
84         external_object->type = internal_object->common.type;
85
86         /* However, only a limited number of external types are supported */
87
88         switch (internal_object->common.type) {
89
90         case ACPI_TYPE_STRING:
91
92                 length = internal_object->string.length + 1;
93                 external_object->string.length = internal_object->string.length;
94                 external_object->string.pointer = (NATIVE_CHAR *) data_space;
95                 MEMCPY ((void *) data_space, (void *) internal_object->string.pointer, length);
96                 break;
97
98
99         case ACPI_TYPE_BUFFER:
100
101                 length = internal_object->buffer.length;
102                 external_object->buffer.length = internal_object->buffer.length;
103                 external_object->buffer.pointer = data_space;
104                 MEMCPY ((void *) data_space, (void *) internal_object->buffer.pointer, length);
105                 break;
106
107
108         case ACPI_TYPE_INTEGER:
109
110                 external_object->integer.value= internal_object->integer.value;
111                 break;
112
113
114         case INTERNAL_TYPE_REFERENCE:
115
116                 /*
117                  * This is an object reference.  Attempt to dereference it.
118                  */
119
120                 switch (internal_object->reference.opcode) {
121                 case AML_ZERO_OP:
122                         external_object->type = ACPI_TYPE_INTEGER;
123                         external_object->integer.value = 0;
124                         break;
125
126                 case AML_ONE_OP:
127                         external_object->type = ACPI_TYPE_INTEGER;
128                         external_object->integer.value = 1;
129                         break;
130
131                 case AML_ONES_OP:
132                         external_object->type = ACPI_TYPE_INTEGER;
133                         external_object->integer.value = ACPI_INTEGER_MAX;
134                         break;
135
136                 case AML_NAMEPATH_OP:
137                         /*
138                          * This is a named reference, get the string.  We already know that
139                          * we have room for it, use max length
140                          */
141                         length = MAX_STRING_LENGTH;
142                         external_object->type = ACPI_TYPE_STRING;
143                         external_object->string.pointer = (NATIVE_CHAR *) data_space;
144                         status = acpi_ns_handle_to_pathname ((ACPI_HANDLE *) internal_object->reference.node,
145                                          &length, (char *) data_space);
146
147                         /* Converted (external) string length is returned from above */
148
149                         external_object->string.length = length;
150                         break;
151
152                 default:
153                         /*
154                          * Use the object type of "Any" to indicate a reference
155                          * to object containing a handle to an ACPI named object.
156                          */
157                         external_object->type = ACPI_TYPE_ANY;
158                         external_object->reference.handle = internal_object->reference.node;
159                         break;
160                 }
161                 break;
162
163
164         case ACPI_TYPE_PROCESSOR:
165
166                 external_object->processor.proc_id = internal_object->processor.proc_id;
167                 external_object->processor.pblk_address = internal_object->processor.address;
168                 external_object->processor.pblk_length = internal_object->processor.length;
169                 break;
170
171
172         case ACPI_TYPE_POWER:
173
174                 external_object->power_resource.system_level =
175                                    internal_object->power_resource.system_level;
176
177                 external_object->power_resource.resource_order =
178                                    internal_object->power_resource.resource_order;
179                 break;
180
181
182         default:
183                 /*
184                  * There is no corresponding external object type
185                  */
186                 return (AE_SUPPORT);
187                 break;
188         }
189
190
191         *buffer_space_used = (u32) ROUND_UP_TO_NATIVE_WORD (length);
192
193         return (status);
194 }
195
196
197 /*******************************************************************************
198  *
199  * FUNCTION:    Acpi_cm_copy_ielement_to_eelement
200  *
201  * PARAMETERS:  ACPI_PKG_CALLBACK
202  *
203  * RETURN:      Status
204  *
205  * DESCRIPTION: Copy one package element to another package element
206  *
207  ******************************************************************************/
208
209 ACPI_STATUS
210 acpi_cm_copy_ielement_to_eelement (
211         u8                      object_type,
212         ACPI_OPERAND_OBJECT     *source_object,
213         ACPI_GENERIC_STATE      *state,
214         void                    *context)
215 {
216         ACPI_STATUS             status = AE_OK;
217         ACPI_PKG_INFO           *info = (ACPI_PKG_INFO *) context;
218         u32                     object_space;
219         u32                     this_index;
220         ACPI_OBJECT             *target_object;
221
222
223         this_index      = state->pkg.index;
224         target_object   = (ACPI_OBJECT *)
225                            &((ACPI_OBJECT *)(state->pkg.dest_object))->package.elements[this_index];
226
227
228         switch (object_type) {
229         case ACPI_COPY_TYPE_SIMPLE:
230
231                 /*
232                  * This is a simple or null object -- get the size
233                  */
234
235                 status = acpi_cm_copy_isimple_to_esimple (source_object,
236                                   target_object, info->free_space, &object_space);
237                 if (ACPI_FAILURE (status)) {
238                         return (status);
239                 }
240
241                 break;
242
243         case ACPI_COPY_TYPE_PACKAGE:
244
245                 /*
246                  * Build the package object
247                  */
248                 target_object->type             = ACPI_TYPE_PACKAGE;
249                 target_object->package.count    = source_object->package.count;
250                 target_object->package.elements = (ACPI_OBJECT *) info->free_space;
251
252                 /*
253                  * Pass the new package object back to the package walk routine
254                  */
255                 state->pkg.this_target_obj = target_object;
256
257                 /*
258                  * Save space for the array of objects (Package elements)
259                  * update the buffer length counter
260                  */
261                 object_space = (u32) ROUND_UP_TO_NATIVE_WORD (
262                                    target_object->package.count * sizeof (ACPI_OBJECT));
263                 break;
264
265         default:
266                 return (AE_BAD_PARAMETER);
267         }
268
269
270         info->free_space  += object_space;
271         info->length      += object_space;
272
273         return (status);
274 }
275
276
277 /*******************************************************************************
278  *
279  * FUNCTION:    Acpi_cm_copy_ipackage_to_epackage
280  *
281  * PARAMETERS:  *Internal_object    - Pointer to the object we are returning
282  *              *Buffer             - Where the object is returned
283  *              *Space_used         - Where the object length is returned
284  *
285  * RETURN:      Status
286  *
287  * DESCRIPTION: This function is called to place a package object in a user
288  *              buffer.  A package object by definition contains other objects.
289  *
290  *              The buffer is assumed to have sufficient space for the object.
291  *              The caller must have verified the buffer length needed using the
292  *              Acpi_cm_get_object_size function before calling this function.
293  *
294  ******************************************************************************/
295
296 static ACPI_STATUS
297 acpi_cm_copy_ipackage_to_epackage (
298         ACPI_OPERAND_OBJECT     *internal_object,
299         u8                      *buffer,
300         u32                     *space_used)
301 {
302         ACPI_OBJECT             *external_object;
303         ACPI_STATUS             status;
304         ACPI_PKG_INFO           info;
305
306
307         /*
308          * First package at head of the buffer
309          */
310         external_object = (ACPI_OBJECT *) buffer;
311
312         /*
313          * Free space begins right after the first package
314          */
315         info.length      = 0;
316         info.object_space = 0;
317         info.num_packages = 1;
318         info.free_space  = buffer + ROUND_UP_TO_NATIVE_WORD (sizeof (ACPI_OBJECT));
319
320
321         external_object->type              = internal_object->common.type;
322         external_object->package.count     = internal_object->package.count;
323         external_object->package.elements  = (ACPI_OBJECT *) info.free_space;
324
325
326         /*
327          * Build an array of ACPI_OBJECTS in the buffer
328          * and move the free space past it
329          */
330
331         info.free_space += external_object->package.count *
332                           ROUND_UP_TO_NATIVE_WORD (sizeof (ACPI_OBJECT));
333
334
335         status = acpi_cm_walk_package_tree (internal_object, external_object,
336                          acpi_cm_copy_ielement_to_eelement, &info);
337
338         *space_used = info.length;
339
340         return (status);
341
342 }
343
344 /*******************************************************************************
345  *
346  * FUNCTION:    Acpi_cm_copy_iobject_to_eobject
347  *
348  * PARAMETERS:  *Internal_object    - The internal object to be converted
349  *              *Buffer_ptr         - Where the object is returned
350  *
351  * RETURN:      Status
352  *
353  * DESCRIPTION: This function is called to build an API object to be returned to
354  *              the caller.
355  *
356  ******************************************************************************/
357
358 ACPI_STATUS
359 acpi_cm_copy_iobject_to_eobject (
360         ACPI_OPERAND_OBJECT     *internal_object,
361         ACPI_BUFFER             *ret_buffer)
362 {
363         ACPI_STATUS             status;
364
365
366         if (IS_THIS_OBJECT_TYPE (internal_object, ACPI_TYPE_PACKAGE)) {
367                 /*
368                  * Package object:  Copy all subobjects (including
369                  * nested packages)
370                  */
371                 status = acpi_cm_copy_ipackage_to_epackage (internal_object,
372                                   ret_buffer->pointer, &ret_buffer->length);
373         }
374
375         else {
376                 /*
377                  * Build a simple object (no nested objects)
378                  */
379                 status = acpi_cm_copy_isimple_to_esimple (internal_object,
380                                   (ACPI_OBJECT *) ret_buffer->pointer,
381                                   ((u8 *) ret_buffer->pointer +
382                                   ROUND_UP_TO_NATIVE_WORD (sizeof (ACPI_OBJECT))),
383                                   &ret_buffer->length);
384                 /*
385                  * build simple does not include the object size in the length
386                  * so we add it in here
387                  */
388                 ret_buffer->length += sizeof (ACPI_OBJECT);
389         }
390
391         return (status);
392 }
393
394
395 /*******************************************************************************
396  *
397  * FUNCTION:    Acpi_cm_copy_esimple_to_isimple
398  *
399  * PARAMETERS:  *External_object   - The external object to be converted
400  *              *Internal_object   - Where the internal object is returned
401  *
402  * RETURN:      Status
403  *
404  * DESCRIPTION: This function copies an external object to an internal one.
405  *              NOTE: Pointers can be copied, we don't need to copy data.
406  *              (The pointers have to be valid in our address space no matter
407  *              what we do with them!)
408  *
409  ******************************************************************************/
410
411 ACPI_STATUS
412 acpi_cm_copy_esimple_to_isimple (
413         ACPI_OBJECT             *external_object,
414         ACPI_OPERAND_OBJECT     *internal_object)
415 {
416
417
418         internal_object->common.type = (u8) external_object->type;
419
420         switch (external_object->type) {
421
422         case ACPI_TYPE_STRING:
423
424                 internal_object->string.length = external_object->string.length;
425                 internal_object->string.pointer = external_object->string.pointer;
426                 break;
427
428
429         case ACPI_TYPE_BUFFER:
430
431                 internal_object->buffer.length = external_object->buffer.length;
432                 internal_object->buffer.pointer = external_object->buffer.pointer;
433                 break;
434
435
436         case ACPI_TYPE_INTEGER:
437                 /*
438                  * Number is included in the object itself
439                  */
440                 internal_object->integer.value  = external_object->integer.value;
441                 break;
442
443
444         default:
445                 return (AE_CTRL_RETURN_VALUE);
446                 break;
447         }
448
449
450         return (AE_OK);
451 }
452
453
454 #ifdef ACPI_FUTURE_IMPLEMENTATION
455
456 /* Code to convert packages that are parameters to control methods */
457
458 /*******************************************************************************
459  *
460  * FUNCTION:    Acpi_cm_copy_epackage_to_ipackage
461  *
462  * PARAMETERS:  *Internal_object   - Pointer to the object we are returning
463  *              *Buffer         - Where the object is returned
464  *              *Space_used     - Where the length of the object is returned
465  *
466  * RETURN:      Status          - the status of the call
467  *
468  * DESCRIPTION: This function is called to place a package object in a user
469  *              buffer.  A package object by definition contains other objects.
470  *
471  *              The buffer is assumed to have sufficient space for the object.
472  *              The caller must have verified the buffer length needed using the
473  *              Acpi_cm_get_object_size function before calling this function.
474  *
475  ******************************************************************************/
476
477 static ACPI_STATUS
478 acpi_cm_copy_epackage_to_ipackage (
479         ACPI_OPERAND_OBJECT     *internal_object,
480         u8                      *buffer,
481         u32                     *space_used)
482 {
483         u8                      *free_space;
484         ACPI_OBJECT             *external_object;
485         u32                     length = 0;
486         u32                     this_index;
487         u32                     object_space = 0;
488         ACPI_OPERAND_OBJECT     *this_internal_obj;
489         ACPI_OBJECT             *this_external_obj;
490
491
492         /*
493          * First package at head of the buffer
494          */
495         external_object = (ACPI_OBJECT *)buffer;
496
497         /*
498          * Free space begins right after the first package
499          */
500         free_space = buffer + sizeof(ACPI_OBJECT);
501
502
503         external_object->type              = internal_object->common.type;
504         external_object->package.count     = internal_object->package.count;
505         external_object->package.elements  = (ACPI_OBJECT *)free_space;
506
507
508         /*
509          * Build an array of ACPI_OBJECTS in the buffer
510          * and move the free space past it
511          */
512
513         free_space += external_object->package.count * sizeof(ACPI_OBJECT);
514
515
516         /* Call Walk_package */
517
518 }
519
520 #endif /* Future implementation */
521
522
523 /*******************************************************************************
524  *
525  * FUNCTION:    Acpi_cm_copy_eobject_to_iobject
526  *
527  * PARAMETERS:  *Internal_object   - The external object to be converted
528  *              *Buffer_ptr     - Where the internal object is returned
529  *
530  * RETURN:      Status          - the status of the call
531  *
532  * DESCRIPTION: Converts an external object to an internal object.
533  *
534  ******************************************************************************/
535
536 ACPI_STATUS
537 acpi_cm_copy_eobject_to_iobject (
538         ACPI_OBJECT             *external_object,
539         ACPI_OPERAND_OBJECT     *internal_object)
540 {
541         ACPI_STATUS             status;
542
543
544         if (external_object->type == ACPI_TYPE_PACKAGE) {
545                 /*
546                  * Package objects contain other objects (which can be objects)
547                  * buildpackage does it all
548                  *
549                  * TBD: Package conversion must be completed and tested
550                  * NOTE: this code converts packages as input parameters to
551                  * control methods only.  This is a very, very rare case.
552                  */
553 /*
554                 Status = Acpi_cm_copy_epackage_to_ipackage(Internal_object,
555                                  Ret_buffer->Pointer,
556                                  &Ret_buffer->Length);
557 */
558                 return (AE_NOT_IMPLEMENTED);
559         }
560
561         else {
562                 /*
563                  * Build a simple object (no nested objects)
564                  */
565                 status = acpi_cm_copy_esimple_to_isimple (external_object, internal_object);
566                 /*
567                  * build simple does not include the object size in the length
568                  * so we add it in here
569                  */
570         }
571
572         return (status);
573 }
574
575
576 /*******************************************************************************
577  *
578  * FUNCTION:    Acpi_cm_copy_ielement_to_ielement
579  *
580  * PARAMETERS:  ACPI_PKG_CALLBACK
581  *
582  * RETURN:      Status          - the status of the call
583  *
584  * DESCRIPTION: Copy one package element to another package element
585  *
586  ******************************************************************************/
587
588 ACPI_STATUS
589 acpi_cm_copy_ielement_to_ielement (
590         u8                      object_type,
591         ACPI_OPERAND_OBJECT     *source_object,
592         ACPI_GENERIC_STATE      *state,
593         void                    *context)
594 {
595         ACPI_STATUS             status = AE_OK;
596         u32                     this_index;
597         ACPI_OPERAND_OBJECT     **this_target_ptr;
598         ACPI_OPERAND_OBJECT     *target_object;
599
600
601         this_index    = state->pkg.index;
602         this_target_ptr = (ACPI_OPERAND_OBJECT **)
603                            &state->pkg.dest_object->package.elements[this_index];
604
605         switch (object_type) {
606         case 0:
607
608                 /*
609                  * This is a simple object, just copy it
610                  */
611                 target_object = acpi_cm_create_internal_object (source_object->common.type);
612                 if (!target_object) {
613                         return (AE_NO_MEMORY);
614                 }
615
616                 status = acpi_aml_store_object_to_object (source_object, target_object,
617                                   (ACPI_WALK_STATE *) context);
618                 if (ACPI_FAILURE (status)) {
619                         return (status);
620                 }
621
622                 *this_target_ptr = target_object;
623                 break;
624
625
626         case 1:
627                 /*
628                  * This object is a package - go down another nesting level
629                  * Create and build the package object
630                  */
631                 target_object = acpi_cm_create_internal_object (ACPI_TYPE_PACKAGE);
632                 if (!target_object) {
633                         /* TBD: must delete package created up to this point */
634
635                         return (AE_NO_MEMORY);
636                 }
637
638                 target_object->package.count = source_object->package.count;
639
640                 /*
641                  * Pass the new package object back to the package walk routine
642                  */
643                 state->pkg.this_target_obj = target_object;
644
645                 /*
646                  * Store the object pointer in the parent package object
647                  */
648                 *this_target_ptr = target_object;
649                 break;
650
651         default:
652                 return (AE_BAD_PARAMETER);
653         }
654
655
656         return (status);
657 }
658
659
660 /*******************************************************************************
661  *
662  * FUNCTION:    Acpi_cm_copy_ipackage_to_ipackage
663  *
664  * PARAMETERS:  *Source_obj     - Pointer to the source package object
665  *              *Dest_obj       - Where the internal object is returned
666  *
667  * RETURN:      Status          - the status of the call
668  *
669  * DESCRIPTION: This function is called to copy an internal package object
670  *              into another internal package object.
671  *
672  ******************************************************************************/
673
674 ACPI_STATUS
675 acpi_cm_copy_ipackage_to_ipackage (
676         ACPI_OPERAND_OBJECT     *source_obj,
677         ACPI_OPERAND_OBJECT     *dest_obj,
678         ACPI_WALK_STATE         *walk_state)
679 {
680         ACPI_STATUS             status = AE_OK;
681
682
683         dest_obj->common.type   = source_obj->common.type;
684         dest_obj->package.count = source_obj->package.count;
685
686
687         /*
688          * Create the object array and walk the source package tree
689          */
690
691         dest_obj->package.elements = acpi_cm_callocate ((source_obj->package.count + 1) *
692                          sizeof (void *));
693         dest_obj->package.next_element = dest_obj->package.elements;
694
695         if (!dest_obj->package.elements) {
696                 REPORT_ERROR (
697                         ("Aml_build_copy_internal_package_object: Package allocation failure\n"));
698                 return (AE_NO_MEMORY);
699         }
700
701
702         status = acpi_cm_walk_package_tree (source_obj, dest_obj,
703                          acpi_cm_copy_ielement_to_ielement, walk_state);
704
705         return (status);
706 }
707