utpredef.c revision 306536
1139823Simp/******************************************************************************
275374Sbp *
375374Sbp * Module Name: utpredef - support functions for predefined names
475374Sbp *
575374Sbp *****************************************************************************/
675374Sbp
775374Sbp/*
875374Sbp * Copyright (C) 2000 - 2016, Intel Corp.
975374Sbp * All rights reserved.
1075374Sbp *
1175374Sbp * Redistribution and use in source and binary forms, with or without
1275374Sbp * modification, are permitted provided that the following conditions
1375374Sbp * are met:
1475374Sbp * 1. Redistributions of source code must retain the above copyright
1575374Sbp *    notice, this list of conditions, and the following disclaimer,
1675374Sbp *    without modification.
1775374Sbp * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1875374Sbp *    substantially similar to the "NO WARRANTY" disclaimer below
1975374Sbp *    ("Disclaimer") and any redistribution must be conditioned upon
2075374Sbp *    including a substantially similar Disclaimer requirement for further
2175374Sbp *    binary redistribution.
2275374Sbp * 3. Neither the names of the above-listed copyright holders nor the names
2375374Sbp *    of any contributors may be used to endorse or promote products derived
2475374Sbp *    from this software without specific prior written permission.
2575374Sbp *
26116189Sobrien * Alternatively, this software may be distributed under the terms of the
27116189Sobrien * GNU General Public License ("GPL") version 2 as published by the Free
28116189Sobrien * Software Foundation.
29116189Sobrien *
3075374Sbp * NO WARRANTY
3175374Sbp * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3295533Smike * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3375374Sbp * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3475374Sbp * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3575374Sbp * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3675374Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3775374Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3875374Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3975374Sbp * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
4075374Sbp * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4175374Sbp * POSSIBILITY OF SUCH DAMAGES.
4275374Sbp */
4375374Sbp
4475374Sbp#include <contrib/dev/acpica/include/acpi.h>
4575374Sbp#include <contrib/dev/acpica/include/accommon.h>
4675374Sbp#include <contrib/dev/acpica/include/acpredef.h>
4775374Sbp
4875374Sbp
49249132Smav#define _COMPONENT          ACPI_UTILITIES
50249132Smav        ACPI_MODULE_NAME    ("utpredef")
5175374Sbp
5275374Sbp
5375374Sbp/*
5475374Sbp * Names for the types that can be returned by the predefined objects.
5575374Sbp * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
5687192Sbp */
5775374Sbpstatic const char   *UtRtypeNames[] =
5887192Sbp{
5987192Sbp    "/Integer",
6091406Sjhb    "/String",
6175374Sbp    "/Buffer",
6287192Sbp    "/Package",
6375374Sbp    "/Reference",
6475374Sbp};
6575374Sbp
6675374Sbp
6775374Sbp/*******************************************************************************
68112888Sjeff *
6975374Sbp * FUNCTION:    AcpiUtGetNextPredefinedMethod
70112888Sjeff *
7175374Sbp * PARAMETERS:  ThisName            - Entry in the predefined method/name table
7275374Sbp *
73112888Sjeff * RETURN:      Pointer to next entry in predefined table.
7475374Sbp *
75112888Sjeff * DESCRIPTION: Get the next entry in the predefine method table. Handles the
76112888Sjeff *              cases where a package info entry follows a method name that
77110849Stjr *              returns a package.
78104306Sjmallett *
79112888Sjeff ******************************************************************************/
80112888Sjeff
81114983Sjhbconst ACPI_PREDEFINED_INFO *
82114983SjhbAcpiUtGetNextPredefinedMethod (
83114983Sjhb    const ACPI_PREDEFINED_INFO  *ThisName)
84112888Sjeff{
85110849Stjr
8675374Sbp    /*
87110849Stjr     * Skip next entry in the table if this name returns a Package
88110849Stjr     * (next entry contains the package info)
8975374Sbp     */
9075374Sbp    if ((ThisName->Info.ExpectedBtypes & ACPI_RTYPE_PACKAGE) &&
9175374Sbp        (ThisName->Info.ExpectedBtypes != ACPI_RTYPE_ALL))
9275374Sbp    {
9375374Sbp        ThisName++;
9475374Sbp    }
9575374Sbp
96217174Scsjp    ThisName++;
9775374Sbp    return (ThisName);
9875374Sbp}
99111119Simp
10075374Sbp
10175374Sbp/*******************************************************************************
10275374Sbp *
10375374Sbp * FUNCTION:    AcpiUtMatchPredefinedMethod
10475374Sbp *
10575374Sbp * PARAMETERS:  Name                - Name to find
10675374Sbp *
10775374Sbp * RETURN:      Pointer to entry in predefined table. NULL indicates not found.
10875374Sbp *
10975374Sbp * DESCRIPTION: Check an object name against the predefined object list.
11075374Sbp *
111217174Scsjp ******************************************************************************/
11275374Sbp
11375374Sbpconst ACPI_PREDEFINED_INFO *
114217174ScsjpAcpiUtMatchPredefinedMethod (
115217174Scsjp    char                        *Name)
11675374Sbp{
117217174Scsjp    const ACPI_PREDEFINED_INFO  *ThisName;
11875374Sbp
11975374Sbp
12075374Sbp    /* Quick check for a predefined name, first character must be underscore */
12175374Sbp
12275374Sbp    if (Name[0] != '_')
12375374Sbp    {
12475374Sbp        return (NULL);
12575374Sbp    }
12675374Sbp
127111119Simp    /* Search info table for a predefined method/object name */
128154434Scsjp
129154434Scsjp    ThisName = AcpiGbl_PredefinedMethods;
130154434Scsjp    while (ThisName->Info.Name[0])
131154434Scsjp    {
132154434Scsjp        if (ACPI_COMPARE_NAME (Name, ThisName->Info.Name))
13375374Sbp        {
13475374Sbp            return (ThisName);
13575374Sbp        }
13675374Sbp
13775374Sbp        ThisName = AcpiUtGetNextPredefinedMethod (ThisName);
13875374Sbp    }
13975374Sbp
140217174Scsjp    return (NULL); /* Not found */
14175374Sbp}
14275374Sbp
14375374Sbp
14475374Sbp/*******************************************************************************
14575374Sbp *
146111119Simp * FUNCTION:    AcpiUtGetExpectedReturnTypes
14775374Sbp *
14875374Sbp * PARAMETERS:  Buffer              - Where the formatted string is returned
14975374Sbp *              ExpectedBTypes      - Bitfield of expected data types
15075374Sbp *
15175374Sbp * RETURN:      Formatted string in Buffer.
15275374Sbp *
15375374Sbp * DESCRIPTION: Format the expected object types into a printable string.
15475374Sbp *
15575374Sbp ******************************************************************************/
15675374Sbp
15775374Sbpvoid
15875374SbpAcpiUtGetExpectedReturnTypes (
15975374Sbp    char                    *Buffer,
16075374Sbp    UINT32                  ExpectedBtypes)
16175374Sbp{
16275374Sbp    UINT32                  ThisRtype;
163111119Simp    UINT32                  i;
16475374Sbp    UINT32                  j;
16575374Sbp
16675374Sbp
16775374Sbp    if (!ExpectedBtypes)
16875374Sbp    {
16975374Sbp        strcpy (Buffer, "NONE");
17075374Sbp        return;
17175374Sbp    }
17275374Sbp
17375374Sbp    j = 1;
17475374Sbp    Buffer[0] = 0;
17575374Sbp    ThisRtype = ACPI_RTYPE_INTEGER;
17675374Sbp
17775374Sbp    for (i = 0; i < ACPI_NUM_RTYPES; i++)
17875374Sbp    {
17975374Sbp        /* If one of the expected types, concatenate the name of this type */
18075374Sbp
18175374Sbp        if (ExpectedBtypes & ThisRtype)
18275374Sbp        {
183217174Scsjp            strcat (Buffer, &UtRtypeNames[i][j]);
18475374Sbp            j = 0;              /* Use name separator from now on */
18575374Sbp        }
18675374Sbp
18775374Sbp        ThisRtype <<= 1;    /* Next Rtype */
18875374Sbp    }
18975374Sbp}
19075374Sbp
19175374Sbp
19275374Sbp/*******************************************************************************
193107940Srobert *
19475374Sbp * The remaining functions are used by iASL and AcpiHelp only
19575374Sbp *
19675374Sbp ******************************************************************************/
19775374Sbp
19875374Sbp#if (defined ACPI_ASL_COMPILER || defined ACPI_HELP_APP)
19975374Sbp#include <stdio.h>
20075374Sbp#include <string.h>
20175374Sbp
202217174Scsjp/* Local prototypes */
20375374Sbp
20475374Sbpstatic UINT32
20575374SbpAcpiUtGetArgumentTypes (
20675374Sbp    char                    *Buffer,
207217174Scsjp    UINT16                  ArgumentTypes);
20875374Sbp
20975374Sbp
21075374Sbp/* Types that can be returned externally by a predefined name */
21175374Sbp
21275374Sbpstatic const char   *UtExternalTypeNames[] = /* Indexed by ACPI_TYPE_* */
21375374Sbp{
21475374Sbp    ", UNSUPPORTED-TYPE",
21575374Sbp    ", Integer",
21675374Sbp    ", String",
21775374Sbp    ", Buffer",
21875374Sbp    ", Package"
21975374Sbp};
22075374Sbp
22175374Sbp/* Bit widths for resource descriptor predefined names */
22275374Sbp
22375374Sbpstatic const char   *UtResourceTypeNames[] =
22475374Sbp{
22575374Sbp    "/1",
22675374Sbp    "/2",
22775374Sbp    "/3",
22875374Sbp    "/8",
22975374Sbp    "/16",
23075374Sbp    "/32",
23175374Sbp    "/64",
23275374Sbp    "/variable",
23375374Sbp};
23475374Sbp
23582046Sbp
23675374Sbp/*******************************************************************************
23775374Sbp *
23875374Sbp * FUNCTION:    AcpiUtMatchResourceName
23975374Sbp *
24075374Sbp * PARAMETERS:  Name                - Name to find
24175374Sbp *
24275374Sbp * RETURN:      Pointer to entry in the resource table. NULL indicates not
24375374Sbp *              found.
24475374Sbp *
24575374Sbp * DESCRIPTION: Check an object name against the predefined resource
24675374Sbp *              descriptor object list.
24775374Sbp *
24875374Sbp ******************************************************************************/
24975374Sbp
25075374Sbpconst ACPI_PREDEFINED_INFO *
25175374SbpAcpiUtMatchResourceName (
25275374Sbp    char                        *Name)
25375374Sbp{
25475374Sbp    const ACPI_PREDEFINED_INFO  *ThisName;
25575374Sbp
25675374Sbp
25775374Sbp    /*
25875374Sbp     * Quick check for a predefined name, first character must
25975374Sbp     * be underscore
26075374Sbp     */
26175374Sbp    if (Name[0] != '_')
26275374Sbp    {
26375374Sbp        return (NULL);
26475374Sbp    }
26575374Sbp
26675374Sbp    /* Search info table for a predefined method/object name */
26775374Sbp
268163992Sbp    ThisName = AcpiGbl_ResourceNames;
269163992Sbp    while (ThisName->Info.Name[0])
27075374Sbp    {
27175374Sbp        if (ACPI_COMPARE_NAME (Name, ThisName->Info.Name))
27294914Sbp        {
27394914Sbp            return (ThisName);
27475374Sbp        }
27575374Sbp
27675374Sbp        ThisName++;
27775374Sbp    }
27875374Sbp
27975374Sbp    return (NULL); /* Not found */
28075374Sbp}
28194914Sbp
28275374Sbp
28375374Sbp/*******************************************************************************
28475374Sbp *
28575374Sbp * FUNCTION:    AcpiUtDisplayPredefinedMethod
28675374Sbp *
28775374Sbp * PARAMETERS:  Buffer              - Scratch buffer for this function
28875374Sbp *              ThisName            - Entry in the predefined method/name table
28975374Sbp *              MultiLine           - TRUE if output should be on >1 line
29075374Sbp *
29175374Sbp * RETURN:      None
29294914Sbp *
29394914Sbp * DESCRIPTION: Display information about a predefined method. Number and
29494914Sbp *              type of the input arguments, and expected type(s) for the
29575374Sbp *              return value, if any.
29694914Sbp *
29794914Sbp ******************************************************************************/
29875374Sbp
29975374Sbpvoid
30075374SbpAcpiUtDisplayPredefinedMethod (
30175374Sbp    char                        *Buffer,
30275374Sbp    const ACPI_PREDEFINED_INFO  *ThisName,
30375374Sbp    BOOLEAN                     MultiLine)
30475374Sbp{
30575374Sbp    UINT32                      ArgCount;
30675374Sbp
30775374Sbp    /*
30875374Sbp     * Get the argument count and the string buffer
30975374Sbp     * containing all argument types
31075374Sbp     */
31175374Sbp    ArgCount = AcpiUtGetArgumentTypes (Buffer,
31275374Sbp        ThisName->Info.ArgumentList);
31375374Sbp
31475374Sbp    if (MultiLine)
31575374Sbp    {
31675374Sbp        printf ("      ");
31775374Sbp    }
31875374Sbp
31975374Sbp    printf ("%4.4s    Requires %s%u argument%s",
32075374Sbp        ThisName->Info.Name,
32175374Sbp        (ThisName->Info.ArgumentList & ARG_COUNT_IS_MINIMUM) ?
32275374Sbp            "(at least) " : "",
32375374Sbp        ArgCount, ArgCount != 1 ? "s" : "");
324148517Simura
325148517Simura    /* Display the types for any arguments */
32675374Sbp
327148517Simura    if (ArgCount > 0)
328148517Simura    {
32975374Sbp        printf (" (%s)", Buffer);
330148517Simura    }
331148517Simura
332148517Simura    if (MultiLine)
333148517Simura    {
334148517Simura        printf ("\n    ");
335148517Simura    }
336148517Simura
337148517Simura    /* Get the return value type(s) allowed */
33875374Sbp
33975374Sbp    if (ThisName->Info.ExpectedBtypes)
34075374Sbp    {
34175374Sbp        AcpiUtGetExpectedReturnTypes (Buffer, ThisName->Info.ExpectedBtypes);
342217174Scsjp        printf ("  Return value types: %s\n", Buffer);
34375374Sbp    }
34475374Sbp    else
34575374Sbp    {
34675374Sbp        printf ("  No return value\n");
34775374Sbp    }
34875374Sbp}
34975374Sbp
35075374Sbp
35175374Sbp/*******************************************************************************
35275374Sbp *
353230196Skevlo * FUNCTION:    AcpiUtGetArgumentTypes
354230196Skevlo *
35575374Sbp * PARAMETERS:  Buffer              - Where to return the formatted types
35675374Sbp *              ArgumentTypes       - Types field for this method
35775374Sbp *
35875374Sbp * RETURN:      Count - the number of arguments required for this method
35975374Sbp *
36075374Sbp * DESCRIPTION: Format the required data types for this method (Integer,
36175374Sbp *              String, Buffer, or Package) and return the required argument
36275374Sbp *              count.
36375374Sbp *
36475374Sbp ******************************************************************************/
36575374Sbp
36675374Sbpstatic UINT32
367230196SkevloAcpiUtGetArgumentTypes (
368230196Skevlo    char                    *Buffer,
36975374Sbp    UINT16                  ArgumentTypes)
37075374Sbp{
37175374Sbp    UINT16                  ThisArgumentType;
37275374Sbp    UINT16                  SubIndex;
37375374Sbp    UINT16                  ArgCount;
37475374Sbp    UINT32                  i;
37575374Sbp
37675374Sbp
37775374Sbp    *Buffer = 0;
37875374Sbp    SubIndex = 2;
37975374Sbp
38075374Sbp    /* First field in the types list is the count of args to follow */
38175374Sbp
38275374Sbp    ArgCount = METHOD_GET_ARG_COUNT (ArgumentTypes);
38375374Sbp    if (ArgCount > METHOD_PREDEF_ARGS_MAX)
38475374Sbp    {
38575374Sbp        printf ("**** Invalid argument count (%u) "
38675374Sbp            "in predefined info structure\n", ArgCount);
38775374Sbp        return (ArgCount);
388    }
389
390    /* Get each argument from the list, convert to ascii, store to buffer */
391
392    for (i = 0; i < ArgCount; i++)
393    {
394        ThisArgumentType = METHOD_GET_NEXT_TYPE (ArgumentTypes);
395
396        if (!ThisArgumentType || (ThisArgumentType > METHOD_MAX_ARG_TYPE))
397        {
398            printf ("**** Invalid argument type (%u) "
399                "in predefined info structure\n", ThisArgumentType);
400            return (ArgCount);
401        }
402
403        strcat (Buffer, UtExternalTypeNames[ThisArgumentType] + SubIndex);
404        SubIndex = 0;
405    }
406
407    return (ArgCount);
408}
409
410
411/*******************************************************************************
412 *
413 * FUNCTION:    AcpiUtGetResourceBitWidth
414 *
415 * PARAMETERS:  Buffer              - Where the formatted string is returned
416 *              Types               - Bitfield of expected data types
417 *
418 * RETURN:      Count of return types. Formatted string in Buffer.
419 *
420 * DESCRIPTION: Format the resource bit widths into a printable string.
421 *
422 ******************************************************************************/
423
424UINT32
425AcpiUtGetResourceBitWidth (
426    char                    *Buffer,
427    UINT16                  Types)
428{
429    UINT32                  i;
430    UINT16                  SubIndex;
431    UINT32                  Found;
432
433
434    *Buffer = 0;
435    SubIndex = 1;
436    Found = 0;
437
438    for (i = 0; i < NUM_RESOURCE_WIDTHS; i++)
439    {
440        if (Types & 1)
441        {
442            strcat (Buffer, &(UtResourceTypeNames[i][SubIndex]));
443            SubIndex = 0;
444            Found++;
445        }
446
447        Types >>= 1;
448    }
449
450    return (Found);
451}
452#endif
453