ACPICA: ACPI 4.0 : Add new return package type, restructure module.
[safe/jmp/linux-2.6] / drivers / acpi / acpica / nspredef.c
1 /******************************************************************************
2  *
3  * Module Name: nspredef - Validation of ACPI predefined methods and objects
4  *              $Revision: 1.1 $
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2008, Intel Corp.
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 #include <acpi/acpi.h>
46 #include "accommon.h"
47 #include "acnamesp.h"
48 #include "acpredef.h"
49
50 #define _COMPONENT          ACPI_NAMESPACE
51 ACPI_MODULE_NAME("nspredef")
52
53 /*******************************************************************************
54  *
55  * This module validates predefined ACPI objects that appear in the namespace,
56  * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
57  * validation is to detect problems with BIOS-exposed predefined ACPI objects
58  * before the results are returned to the ACPI-related drivers.
59  *
60  * There are several areas that are validated:
61  *
62  *  1) The number of input arguments as defined by the method/object in the
63  *      ASL is validated against the ACPI specification.
64  *  2) The type of the return object (if any) is validated against the ACPI
65  *      specification.
66  *  3) For returned package objects, the count of package elements is
67  *      validated, as well as the type of each package element. Nested
68  *      packages are supported.
69  *
70  * For any problems found, a warning message is issued.
71  *
72  ******************************************************************************/
73 /* Local prototypes */
74 static acpi_status
75 acpi_ns_check_package(struct acpi_predefined_data *data,
76                       union acpi_operand_object **return_object_ptr);
77
78 static acpi_status
79 acpi_ns_check_package_list(struct acpi_predefined_data *data,
80                            const union acpi_predefined_info *package,
81                            union acpi_operand_object **elements, u32 count);
82
83 static acpi_status
84 acpi_ns_check_package_elements(struct acpi_predefined_data *data,
85                                union acpi_operand_object **elements,
86                                u8 type1,
87                                u32 count1,
88                                u8 type2, u32 count2, u32 start_index);
89
90 static acpi_status
91 acpi_ns_check_object_type(struct acpi_predefined_data *data,
92                           union acpi_operand_object **return_object_ptr,
93                           u32 expected_btypes, u32 package_index);
94
95 static acpi_status
96 acpi_ns_check_reference(struct acpi_predefined_data *data,
97                         union acpi_operand_object *return_object);
98
99 static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes);
100
101 /*
102  * Names for the types that can be returned by the predefined objects.
103  * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
104  */
105 static const char *acpi_rtype_names[] = {
106         "/Integer",
107         "/String",
108         "/Buffer",
109         "/Package",
110         "/Reference",
111 };
112
113 /*******************************************************************************
114  *
115  * FUNCTION:    acpi_ns_check_predefined_names
116  *
117  * PARAMETERS:  Node            - Namespace node for the method/object
118  *              user_param_count - Number of parameters actually passed
119  *              return_status   - Status from the object evaluation
120  *              return_object_ptr - Pointer to the object returned from the
121  *                                evaluation of a method or object
122  *
123  * RETURN:      Status
124  *
125  * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
126  *
127  ******************************************************************************/
128
129 acpi_status
130 acpi_ns_check_predefined_names(struct acpi_namespace_node *node,
131                                u32 user_param_count,
132                                acpi_status return_status,
133                                union acpi_operand_object **return_object_ptr)
134 {
135         union acpi_operand_object *return_object = *return_object_ptr;
136         acpi_status status = AE_OK;
137         const union acpi_predefined_info *predefined;
138         char *pathname;
139         struct acpi_predefined_data *data;
140
141         /* Match the name for this method/object against the predefined list */
142
143         predefined = acpi_ns_check_for_predefined_name(node);
144
145         /* Get the full pathname to the object, for use in warning messages */
146
147         pathname = acpi_ns_get_external_pathname(node);
148         if (!pathname) {
149                 return AE_OK;   /* Could not get pathname, ignore */
150         }
151
152         /*
153          * Check that the parameter count for this method matches the ASL
154          * definition. For predefined names, ensure that both the caller and
155          * the method itself are in accordance with the ACPI specification.
156          */
157         acpi_ns_check_parameter_count(pathname, node, user_param_count,
158                                       predefined);
159
160         /* If not a predefined name, we cannot validate the return object */
161
162         if (!predefined) {
163                 goto cleanup;
164         }
165
166         /*
167          * If the method failed or did not actually return an object, we cannot
168          * validate the return object
169          */
170         if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
171                 goto cleanup;
172         }
173
174         /*
175          * If there is no return value, check if we require a return value for
176          * this predefined name. Either one return value is expected, or none,
177          * for both methods and other objects.
178          *
179          * Exit now if there is no return object. Warning if one was expected.
180          */
181         if (!return_object) {
182                 if ((predefined->info.expected_btypes) &&
183                     (!(predefined->info.expected_btypes & ACPI_RTYPE_NONE))) {
184                         ACPI_WARN_PREDEFINED((AE_INFO, pathname,
185                                               ACPI_WARN_ALWAYS,
186                                               "Missing expected return value"));
187
188                         status = AE_AML_NO_RETURN_VALUE;
189                 }
190                 goto cleanup;
191         }
192
193         /*
194          * We have a return value, but if one wasn't expected, just exit, this is
195          * not a problem. For example, if the "Implicit Return" feature is
196          * enabled, methods will always return a value.
197          */
198         if (!predefined->info.expected_btypes) {
199                 goto cleanup;
200         }
201
202         /* Create the parameter data block for object validation */
203
204         data = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_predefined_data));
205         if (!data) {
206                 goto cleanup;
207         }
208         data->predefined = predefined;
209         data->node_flags = node->flags;
210         data->pathname = pathname;
211
212         /*
213          * Check that the type of the return object is what is expected for
214          * this predefined name
215          */
216         status = acpi_ns_check_object_type(data, return_object_ptr,
217                                            predefined->info.expected_btypes,
218                                            ACPI_NOT_PACKAGE_ELEMENT);
219         if (ACPI_FAILURE(status)) {
220                 goto check_validation_status;
221         }
222
223         /* For returned Package objects, check the type of all sub-objects */
224
225         if (return_object->common.type == ACPI_TYPE_PACKAGE) {
226                 status = acpi_ns_check_package(data, return_object_ptr);
227         }
228
229 check_validation_status:
230         /*
231          * If the object validation failed or if we successfully repaired one
232          * or more objects, mark the parent node to suppress further warning
233          * messages during the next evaluation of the same method/object.
234          */
235         if (ACPI_FAILURE(status) || (data->flags & ACPI_OBJECT_REPAIRED)) {
236                 node->flags |= ANOBJ_EVALUATED;
237         }
238         ACPI_FREE(data);
239
240 cleanup:
241         ACPI_FREE(pathname);
242         return (status);
243 }
244
245 /*******************************************************************************
246  *
247  * FUNCTION:    acpi_ns_check_parameter_count
248  *
249  * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
250  *              Node            - Namespace node for the method/object
251  *              user_param_count - Number of args passed in by the caller
252  *              Predefined      - Pointer to entry in predefined name table
253  *
254  * RETURN:      None
255  *
256  * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
257  *              predefined name is what is expected (i.e., what is defined in
258  *              the ACPI specification for this predefined name.)
259  *
260  ******************************************************************************/
261
262 void
263 acpi_ns_check_parameter_count(char *pathname,
264                               struct acpi_namespace_node *node,
265                               u32 user_param_count,
266                               const union acpi_predefined_info *predefined)
267 {
268         u32 param_count;
269         u32 required_params_current;
270         u32 required_params_old;
271
272         /* Methods have 0-7 parameters. All other types have zero. */
273
274         param_count = 0;
275         if (node->type == ACPI_TYPE_METHOD) {
276                 param_count = node->object->method.param_count;
277         }
278
279         if (!predefined) {
280                 /*
281                  * Check the parameter count for non-predefined methods/objects.
282                  *
283                  * Warning if too few or too many arguments have been passed by the
284                  * caller. An incorrect number of arguments may not cause the method
285                  * to fail. However, the method will fail if there are too few
286                  * arguments and the method attempts to use one of the missing ones.
287                  */
288                 if (user_param_count < param_count) {
289                         ACPI_WARN_PREDEFINED((AE_INFO, pathname,
290                                               ACPI_WARN_ALWAYS,
291                                               "Insufficient arguments - needs %u, found %u",
292                                               param_count, user_param_count));
293                 } else if (user_param_count > param_count) {
294                         ACPI_WARN_PREDEFINED((AE_INFO, pathname,
295                                               ACPI_WARN_ALWAYS,
296                                               "Excess arguments - needs %u, found %u",
297                                               param_count, user_param_count));
298                 }
299                 return;
300         }
301
302         /*
303          * Validate the user-supplied parameter count.
304          * Allow two different legal argument counts (_SCP, etc.)
305          */
306         required_params_current = predefined->info.param_count & 0x0F;
307         required_params_old = predefined->info.param_count >> 4;
308
309         if (user_param_count != ACPI_UINT32_MAX) {
310                 if ((user_param_count != required_params_current) &&
311                     (user_param_count != required_params_old)) {
312                         ACPI_WARN_PREDEFINED((AE_INFO, pathname,
313                                               ACPI_WARN_ALWAYS,
314                                               "Parameter count mismatch - "
315                                               "caller passed %u, ACPI requires %u",
316                                               user_param_count,
317                                               required_params_current));
318                 }
319         }
320
321         /*
322          * Check that the ASL-defined parameter count is what is expected for
323          * this predefined name (parameter count as defined by the ACPI
324          * specification)
325          */
326         if ((param_count != required_params_current) &&
327             (param_count != required_params_old)) {
328                 ACPI_WARN_PREDEFINED((AE_INFO, pathname, node->flags,
329                                       "Parameter count mismatch - ASL declared %u, ACPI requires %u",
330                                       param_count, required_params_current));
331         }
332 }
333
334 /*******************************************************************************
335  *
336  * FUNCTION:    acpi_ns_check_for_predefined_name
337  *
338  * PARAMETERS:  Node            - Namespace node for the method/object
339  *
340  * RETURN:      Pointer to entry in predefined table. NULL indicates not found.
341  *
342  * DESCRIPTION: Check an object name against the predefined object list.
343  *
344  ******************************************************************************/
345
346 const union acpi_predefined_info *acpi_ns_check_for_predefined_name(struct
347                                                                     acpi_namespace_node
348                                                                     *node)
349 {
350         const union acpi_predefined_info *this_name;
351
352         /* Quick check for a predefined name, first character must be underscore */
353
354         if (node->name.ascii[0] != '_') {
355                 return (NULL);
356         }
357
358         /* Search info table for a predefined method/object name */
359
360         this_name = predefined_names;
361         while (this_name->info.name[0]) {
362                 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->info.name)) {
363                         return (this_name);
364                 }
365
366                 /*
367                  * Skip next entry in the table if this name returns a Package
368                  * (next entry contains the package info)
369                  */
370                 if (this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) {
371                         this_name++;
372                 }
373
374                 this_name++;
375         }
376
377         return (NULL);          /* Not found */
378 }
379
380 /*******************************************************************************
381  *
382  * FUNCTION:    acpi_ns_check_package
383  *
384  * PARAMETERS:  Data            - Pointer to validation data structure
385  *              return_object_ptr - Pointer to the object returned from the
386  *                                evaluation of a method or object
387  *
388  * RETURN:      Status
389  *
390  * DESCRIPTION: Check a returned package object for the correct count and
391  *              correct type of all sub-objects.
392  *
393  ******************************************************************************/
394
395 static acpi_status
396 acpi_ns_check_package(struct acpi_predefined_data *data,
397                       union acpi_operand_object **return_object_ptr)
398 {
399         union acpi_operand_object *return_object = *return_object_ptr;
400         const union acpi_predefined_info *package;
401         union acpi_operand_object **elements;
402         acpi_status status = AE_OK;
403         u32 expected_count;
404         u32 count;
405         u32 i;
406
407         ACPI_FUNCTION_NAME(ns_check_package);
408
409         /* The package info for this name is in the next table entry */
410
411         package = data->predefined + 1;
412
413         ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
414                           "%s Validating return Package of Type %X, Count %X\n",
415                           data->pathname, package->ret_info.type,
416                           return_object->package.count));
417
418         /* Extract package count and elements array */
419
420         elements = return_object->package.elements;
421         count = return_object->package.count;
422
423         /* The package must have at least one element, else invalid */
424
425         if (!count) {
426                 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
427                                       "Return Package has no elements (empty)"));
428
429                 return (AE_AML_OPERAND_VALUE);
430         }
431
432         /*
433          * Decode the type of the expected package contents
434          *
435          * PTYPE1 packages contain no subpackages
436          * PTYPE2 packages contain sub-packages
437          */
438         switch (package->ret_info.type) {
439         case ACPI_PTYPE1_FIXED:
440
441                 /*
442                  * The package count is fixed and there are no sub-packages
443                  *
444                  * If package is too small, exit.
445                  * If package is larger than expected, issue warning but continue
446                  */
447                 expected_count =
448                     package->ret_info.count1 + package->ret_info.count2;
449                 if (count < expected_count) {
450                         goto package_too_small;
451                 } else if (count > expected_count) {
452                         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
453                                               data->node_flags,
454                                               "Return Package is larger than needed - "
455                                               "found %u, expected %u", count,
456                                               expected_count));
457                 }
458
459                 /* Validate all elements of the returned package */
460
461                 status = acpi_ns_check_package_elements(data, elements,
462                                                         package->ret_info.
463                                                         object_type1,
464                                                         package->ret_info.
465                                                         count1,
466                                                         package->ret_info.
467                                                         object_type2,
468                                                         package->ret_info.
469                                                         count2, 0);
470                 break;
471
472         case ACPI_PTYPE1_VAR:
473
474                 /*
475                  * The package count is variable, there are no sub-packages, and all
476                  * elements must be of the same type
477                  */
478                 for (i = 0; i < count; i++) {
479                         status = acpi_ns_check_object_type(data, elements,
480                                                            package->ret_info.
481                                                            object_type1, i);
482                         if (ACPI_FAILURE(status)) {
483                                 return (status);
484                         }
485                         elements++;
486                 }
487                 break;
488
489         case ACPI_PTYPE1_OPTION:
490
491                 /*
492                  * The package count is variable, there are no sub-packages. There are
493                  * a fixed number of required elements, and a variable number of
494                  * optional elements.
495                  *
496                  * Check if package is at least as large as the minimum required
497                  */
498                 expected_count = package->ret_info3.count;
499                 if (count < expected_count) {
500                         goto package_too_small;
501                 }
502
503                 /* Variable number of sub-objects */
504
505                 for (i = 0; i < count; i++) {
506                         if (i < package->ret_info3.count) {
507
508                                 /* These are the required package elements (0, 1, or 2) */
509
510                                 status =
511                                     acpi_ns_check_object_type(data, elements,
512                                                               package->
513                                                               ret_info3.
514                                                               object_type[i],
515                                                               i);
516                                 if (ACPI_FAILURE(status)) {
517                                         return (status);
518                                 }
519                         } else {
520                                 /* These are the optional package elements */
521
522                                 status =
523                                     acpi_ns_check_object_type(data, elements,
524                                                               package->
525                                                               ret_info3.
526                                                               tail_object_type,
527                                                               i);
528                                 if (ACPI_FAILURE(status)) {
529                                         return (status);
530                                 }
531                         }
532                         elements++;
533                 }
534                 break;
535
536         case ACPI_PTYPE2_REV_FIXED:
537
538                 /* First element is the (Integer) revision */
539
540                 status = acpi_ns_check_object_type(data, elements,
541                                                    ACPI_RTYPE_INTEGER, 0);
542                 if (ACPI_FAILURE(status)) {
543                         return (status);
544                 }
545
546                 elements++;
547                 count--;
548
549                 /* Examine the sub-packages */
550
551                 status =
552                     acpi_ns_check_package_list(data, package, elements, count);
553                 break;
554
555         case ACPI_PTYPE2_PKG_COUNT:
556
557                 /* First element is the (Integer) count of sub-packages to follow */
558
559                 status = acpi_ns_check_object_type(data, elements,
560                                                    ACPI_RTYPE_INTEGER, 0);
561                 if (ACPI_FAILURE(status)) {
562                         return (status);
563                 }
564
565                 /*
566                  * Count cannot be larger than the parent package length, but allow it
567                  * to be smaller. The >= accounts for the Integer above.
568                  */
569                 expected_count = (u32) (*elements)->integer.value;
570                 if (expected_count >= count) {
571                         goto package_too_small;
572                 }
573
574                 count = expected_count;
575                 elements++;
576
577                 /* Examine the sub-packages */
578
579                 status =
580                     acpi_ns_check_package_list(data, package, elements, count);
581                 break;
582
583         case ACPI_PTYPE2:
584         case ACPI_PTYPE2_FIXED:
585         case ACPI_PTYPE2_MIN:
586         case ACPI_PTYPE2_COUNT:
587
588                 /*
589                  * These types all return a single Package that consists of a
590                  * variable number of sub-Packages.
591                  *
592                  * First, ensure that the first element is a sub-Package. If not,
593                  * the BIOS may have incorrectly returned the object as a single
594                  * package instead of a Package of Packages (a common error if
595                  * there is only one entry). We may be able to repair this by
596                  * wrapping the returned Package with a new outer Package.
597                  */
598                 if ((*elements)->common.type != ACPI_TYPE_PACKAGE) {
599
600                         /* Create the new outer package and populate it */
601
602                         status =
603                             acpi_ns_repair_package_list(data,
604                                                         return_object_ptr);
605                         if (ACPI_FAILURE(status)) {
606                                 return (status);
607                         }
608
609                         /* Update locals to point to the new package (of 1 element) */
610
611                         return_object = *return_object_ptr;
612                         elements = return_object->package.elements;
613                         count = 1;
614                 }
615
616                 /* Examine the sub-packages */
617
618                 status =
619                     acpi_ns_check_package_list(data, package, elements, count);
620                 break;
621
622         default:
623
624                 /* Should not get here if predefined info table is correct */
625
626                 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
627                                       "Invalid internal return type in table entry: %X",
628                                       package->ret_info.type));
629
630                 return (AE_AML_INTERNAL);
631         }
632
633         return (status);
634
635 package_too_small:
636
637         /* Error exit for the case with an incorrect package count */
638
639         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
640                               "Return Package is too small - found %u elements, expected %u",
641                               count, expected_count));
642
643         return (AE_AML_OPERAND_VALUE);
644 }
645
646 /*******************************************************************************
647  *
648  * FUNCTION:    acpi_ns_check_package_list
649  *
650  * PARAMETERS:  Data            - Pointer to validation data structure
651  *              Package         - Pointer to package-specific info for method
652  *              Elements        - Element list of parent package. All elements
653  *                                of this list should be of type Package.
654  *              Count           - Count of subpackages
655  *
656  * RETURN:      Status
657  *
658  * DESCRIPTION: Examine a list of subpackages
659  *
660  ******************************************************************************/
661
662 static acpi_status
663 acpi_ns_check_package_list(struct acpi_predefined_data *data,
664                            const union acpi_predefined_info *package,
665                            union acpi_operand_object **elements, u32 count)
666 {
667         union acpi_operand_object *sub_package;
668         union acpi_operand_object **sub_elements;
669         acpi_status status;
670         u32 expected_count;
671         u32 i;
672         u32 j;
673
674         /* Validate each sub-Package in the parent Package */
675
676         for (i = 0; i < count; i++) {
677                 sub_package = *elements;
678                 sub_elements = sub_package->package.elements;
679
680                 /* Each sub-object must be of type Package */
681
682                 status = acpi_ns_check_object_type(data, &sub_package,
683                                                    ACPI_RTYPE_PACKAGE, i);
684                 if (ACPI_FAILURE(status)) {
685                         return (status);
686                 }
687
688                 /* Examine the different types of expected sub-packages */
689
690                 switch (package->ret_info.type) {
691                 case ACPI_PTYPE2:
692                 case ACPI_PTYPE2_PKG_COUNT:
693                 case ACPI_PTYPE2_REV_FIXED:
694
695                         /* Each subpackage has a fixed number of elements */
696
697                         expected_count =
698                             package->ret_info.count1 + package->ret_info.count2;
699                         if (sub_package->package.count < expected_count) {
700                                 goto package_too_small;
701                         }
702
703                         status =
704                             acpi_ns_check_package_elements(data, sub_elements,
705                                                            package->ret_info.
706                                                            object_type1,
707                                                            package->ret_info.
708                                                            count1,
709                                                            package->ret_info.
710                                                            object_type2,
711                                                            package->ret_info.
712                                                            count2, 0);
713                         if (ACPI_FAILURE(status)) {
714                                 return (status);
715                         }
716                         break;
717
718                 case ACPI_PTYPE2_FIXED:
719
720                         /* Each sub-package has a fixed length */
721
722                         expected_count = package->ret_info2.count;
723                         if (sub_package->package.count < expected_count) {
724                                 goto package_too_small;
725                         }
726
727                         /* Check the type of each sub-package element */
728
729                         for (j = 0; j < expected_count; j++) {
730                                 status =
731                                     acpi_ns_check_object_type(data,
732                                                               &sub_elements[j],
733                                                               package->
734                                                               ret_info2.
735                                                               object_type[j],
736                                                               j);
737                                 if (ACPI_FAILURE(status)) {
738                                         return (status);
739                                 }
740                         }
741                         break;
742
743                 case ACPI_PTYPE2_MIN:
744
745                         /* Each sub-package has a variable but minimum length */
746
747                         expected_count = package->ret_info.count1;
748                         if (sub_package->package.count < expected_count) {
749                                 goto package_too_small;
750                         }
751
752                         /* Check the type of each sub-package element */
753
754                         status =
755                             acpi_ns_check_package_elements(data, sub_elements,
756                                                            package->ret_info.
757                                                            object_type1,
758                                                            sub_package->package.
759                                                            count, 0, 0, 0);
760                         if (ACPI_FAILURE(status)) {
761                                 return (status);
762                         }
763                         break;
764
765                 case ACPI_PTYPE2_COUNT:
766
767                         /*
768                          * First element is the (Integer) count of elements, including
769                          * the count field.
770                          */
771                         status = acpi_ns_check_object_type(data, sub_elements,
772                                                            ACPI_RTYPE_INTEGER,
773                                                            0);
774                         if (ACPI_FAILURE(status)) {
775                                 return (status);
776                         }
777
778                         /*
779                          * Make sure package is large enough for the Count and is
780                          * is as large as the minimum size
781                          */
782                         expected_count = (u32)(*sub_elements)->integer.value;
783                         if (sub_package->package.count < expected_count) {
784                                 goto package_too_small;
785                         }
786                         if (sub_package->package.count <
787                             package->ret_info.count1) {
788                                 expected_count = package->ret_info.count1;
789                                 goto package_too_small;
790                         }
791
792                         /* Check the type of each sub-package element */
793
794                         status =
795                             acpi_ns_check_package_elements(data,
796                                                            (sub_elements + 1),
797                                                            package->ret_info.
798                                                            object_type1,
799                                                            (expected_count - 1),
800                                                            0, 0, 1);
801                         if (ACPI_FAILURE(status)) {
802                                 return (status);
803                         }
804                         break;
805
806                 default:        /* Should not get here, type was validated by caller */
807
808                         return (AE_AML_INTERNAL);
809                 }
810
811                 elements++;
812         }
813
814         return (AE_OK);
815
816 package_too_small:
817
818         /* The sub-package count was smaller than required */
819
820         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
821                               "Return Sub-Package[%u] is too small - found %u elements, expected %u",
822                               i, sub_package->package.count, expected_count));
823
824         return (AE_AML_OPERAND_VALUE);
825 }
826
827 /*******************************************************************************
828  *
829  * FUNCTION:    acpi_ns_check_package_elements
830  *
831  * PARAMETERS:  Data            - Pointer to validation data structure
832  *              Elements        - Pointer to the package elements array
833  *              Type1           - Object type for first group
834  *              Count1          - Count for first group
835  *              Type2           - Object type for second group
836  *              Count2          - Count for second group
837  *              start_index     - Start of the first group of elements
838  *
839  * RETURN:      Status
840  *
841  * DESCRIPTION: Check that all elements of a package are of the correct object
842  *              type. Supports up to two groups of different object types.
843  *
844  ******************************************************************************/
845
846 static acpi_status
847 acpi_ns_check_package_elements(struct acpi_predefined_data *data,
848                                union acpi_operand_object **elements,
849                                u8 type1,
850                                u32 count1,
851                                u8 type2, u32 count2, u32 start_index)
852 {
853         union acpi_operand_object **this_element = elements;
854         acpi_status status;
855         u32 i;
856
857         /*
858          * Up to two groups of package elements are supported by the data
859          * structure. All elements in each group must be of the same type.
860          * The second group can have a count of zero.
861          */
862         for (i = 0; i < count1; i++) {
863                 status = acpi_ns_check_object_type(data, this_element,
864                                                    type1, i + start_index);
865                 if (ACPI_FAILURE(status)) {
866                         return (status);
867                 }
868                 this_element++;
869         }
870
871         for (i = 0; i < count2; i++) {
872                 status = acpi_ns_check_object_type(data, this_element,
873                                                    type2,
874                                                    (i + count1 + start_index));
875                 if (ACPI_FAILURE(status)) {
876                         return (status);
877                 }
878                 this_element++;
879         }
880
881         return (AE_OK);
882 }
883
884 /*******************************************************************************
885  *
886  * FUNCTION:    acpi_ns_check_object_type
887  *
888  * PARAMETERS:  Data            - Pointer to validation data structure
889  *              return_object_ptr - Pointer to the object returned from the
890  *                                evaluation of a method or object
891  *              expected_btypes - Bitmap of expected return type(s)
892  *              package_index   - Index of object within parent package (if
893  *                                applicable - ACPI_NOT_PACKAGE_ELEMENT
894  *                                otherwise)
895  *
896  * RETURN:      Status
897  *
898  * DESCRIPTION: Check the type of the return object against the expected object
899  *              type(s). Use of Btype allows multiple expected object types.
900  *
901  ******************************************************************************/
902
903 static acpi_status
904 acpi_ns_check_object_type(struct acpi_predefined_data *data,
905                           union acpi_operand_object **return_object_ptr,
906                           u32 expected_btypes, u32 package_index)
907 {
908         union acpi_operand_object *return_object = *return_object_ptr;
909         acpi_status status = AE_OK;
910         u32 return_btype;
911         char type_buffer[48];   /* Room for 5 types */
912
913         /*
914          * If we get a NULL return_object here, it is a NULL package element,
915          * and this is always an error.
916          */
917         if (!return_object) {
918                 goto type_error_exit;
919         }
920
921         /* A Namespace node should not get here, but make sure */
922
923         if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
924                 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
925                                       "Invalid return type - Found a Namespace node [%4.4s] type %s",
926                                       return_object->node.name.ascii,
927                                       acpi_ut_get_type_name(return_object->node.
928                                                             type)));
929                 return (AE_AML_OPERAND_TYPE);
930         }
931
932         /*
933          * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
934          * The bitmapped type allows multiple possible return types.
935          *
936          * Note, the cases below must handle all of the possible types returned
937          * from all of the predefined names (including elements of returned
938          * packages)
939          */
940         switch (return_object->common.type) {
941         case ACPI_TYPE_INTEGER:
942                 return_btype = ACPI_RTYPE_INTEGER;
943                 break;
944
945         case ACPI_TYPE_BUFFER:
946                 return_btype = ACPI_RTYPE_BUFFER;
947                 break;
948
949         case ACPI_TYPE_STRING:
950                 return_btype = ACPI_RTYPE_STRING;
951                 break;
952
953         case ACPI_TYPE_PACKAGE:
954                 return_btype = ACPI_RTYPE_PACKAGE;
955                 break;
956
957         case ACPI_TYPE_LOCAL_REFERENCE:
958                 return_btype = ACPI_RTYPE_REFERENCE;
959                 break;
960
961         default:
962                 /* Not one of the supported objects, must be incorrect */
963
964                 goto type_error_exit;
965         }
966
967         /* Is the object one of the expected types? */
968
969         if (!(return_btype & expected_btypes)) {
970
971                 /* Type mismatch -- attempt repair of the returned object */
972
973                 status = acpi_ns_repair_object(data, expected_btypes,
974                                                package_index,
975                                                return_object_ptr);
976                 if (ACPI_SUCCESS(status)) {
977                         return (AE_OK); /* Repair was successful */
978                 }
979                 goto type_error_exit;
980         }
981
982         /* For reference objects, check that the reference type is correct */
983
984         if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
985                 status = acpi_ns_check_reference(data, return_object);
986         }
987
988         return (status);
989
990       type_error_exit:
991
992         /* Create a string with all expected types for this predefined object */
993
994         acpi_ns_get_expected_types(type_buffer, expected_btypes);
995
996         if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
997                 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
998                                       "Return type mismatch - found %s, expected %s",
999                                       acpi_ut_get_object_type_name
1000                                       (return_object), type_buffer));
1001         } else {
1002                 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
1003                                       "Return Package type mismatch at index %u - "
1004                                       "found %s, expected %s", package_index,
1005                                       acpi_ut_get_object_type_name
1006                                       (return_object), type_buffer));
1007         }
1008
1009         return (AE_AML_OPERAND_TYPE);
1010 }
1011
1012 /*******************************************************************************
1013  *
1014  * FUNCTION:    acpi_ns_check_reference
1015  *
1016  * PARAMETERS:  Data            - Pointer to validation data structure
1017  *              return_object   - Object returned from the evaluation of a
1018  *                                method or object
1019  *
1020  * RETURN:      Status
1021  *
1022  * DESCRIPTION: Check a returned reference object for the correct reference
1023  *              type. The only reference type that can be returned from a
1024  *              predefined method is a named reference. All others are invalid.
1025  *
1026  ******************************************************************************/
1027
1028 static acpi_status
1029 acpi_ns_check_reference(struct acpi_predefined_data *data,
1030                         union acpi_operand_object *return_object)
1031 {
1032
1033         /*
1034          * Check the reference object for the correct reference type (opcode).
1035          * The only type of reference that can be converted to an union acpi_object is
1036          * a reference to a named object (reference class: NAME)
1037          */
1038         if (return_object->reference.class == ACPI_REFCLASS_NAME) {
1039                 return (AE_OK);
1040         }
1041
1042         ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
1043                               "Return type mismatch - unexpected reference object type [%s] %2.2X",
1044                               acpi_ut_get_reference_name(return_object),
1045                               return_object->reference.class));
1046
1047         return (AE_AML_OPERAND_TYPE);
1048 }
1049
1050 /*******************************************************************************
1051  *
1052  * FUNCTION:    acpi_ns_get_expected_types
1053  *
1054  * PARAMETERS:  Buffer          - Pointer to where the string is returned
1055  *              expected_btypes - Bitmap of expected return type(s)
1056  *
1057  * RETURN:      Buffer is populated with type names.
1058  *
1059  * DESCRIPTION: Translate the expected types bitmap into a string of ascii
1060  *              names of expected types, for use in warning messages.
1061  *
1062  ******************************************************************************/
1063
1064 static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes)
1065 {
1066         u32 this_rtype;
1067         u32 i;
1068         u32 j;
1069
1070         j = 1;
1071         buffer[0] = 0;
1072         this_rtype = ACPI_RTYPE_INTEGER;
1073
1074         for (i = 0; i < ACPI_NUM_RTYPES; i++) {
1075
1076                 /* If one of the expected types, concatenate the name of this type */
1077
1078                 if (expected_btypes & this_rtype) {
1079                         ACPI_STRCAT(buffer, &acpi_rtype_names[i][j]);
1080                         j = 0;  /* Use name separator from now on */
1081                 }
1082                 this_rtype <<= 1;       /* Next Rtype */
1083         }
1084 }