1249109Sjkim/******************************************************************************
2249109Sjkim *
3249109Sjkim * Module Name: utpredef - support functions for predefined names
4249109Sjkim *
5249109Sjkim *****************************************************************************/
6249109Sjkim
7249109Sjkim/*
8249109Sjkim * Copyright (C) 2000 - 2013, Intel Corp.
9249109Sjkim * All rights reserved.
10249109Sjkim *
11249109Sjkim * Redistribution and use in source and binary forms, with or without
12249109Sjkim * modification, are permitted provided that the following conditions
13249109Sjkim * are met:
14249109Sjkim * 1. Redistributions of source code must retain the above copyright
15249109Sjkim *    notice, this list of conditions, and the following disclaimer,
16249109Sjkim *    without modification.
17249109Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18249109Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19249109Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20249109Sjkim *    including a substantially similar Disclaimer requirement for further
21249109Sjkim *    binary redistribution.
22249109Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23249109Sjkim *    of any contributors may be used to endorse or promote products derived
24249109Sjkim *    from this software without specific prior written permission.
25249109Sjkim *
26249109Sjkim * Alternatively, this software may be distributed under the terms of the
27249109Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28249109Sjkim * Software Foundation.
29249109Sjkim *
30249109Sjkim * NO WARRANTY
31249109Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32249109Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33249109Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34249109Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35249109Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36249109Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37249109Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38249109Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39249109Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40249109Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41249109Sjkim * POSSIBILITY OF SUCH DAMAGES.
42249109Sjkim */
43249109Sjkim
44249109Sjkim#define __UTPREDEF_C__
45249109Sjkim
46249112Sjkim#include <contrib/dev/acpica/include/acpi.h>
47249112Sjkim#include <contrib/dev/acpica/include/accommon.h>
48249112Sjkim#include <contrib/dev/acpica/include/acpredef.h>
49249109Sjkim
50249109Sjkim
51249109Sjkim#define _COMPONENT          ACPI_UTILITIES
52249109Sjkim        ACPI_MODULE_NAME    ("utpredef")
53249109Sjkim
54249109Sjkim
55249109Sjkim/*
56249109Sjkim * Names for the types that can be returned by the predefined objects.
57249109Sjkim * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
58249109Sjkim */
59249109Sjkimstatic const char   *UtRtypeNames[] =
60249109Sjkim{
61249109Sjkim    "/Integer",
62249109Sjkim    "/String",
63249109Sjkim    "/Buffer",
64249109Sjkim    "/Package",
65249109Sjkim    "/Reference",
66249109Sjkim};
67249109Sjkim
68249109Sjkim
69249109Sjkim/*******************************************************************************
70249109Sjkim *
71249109Sjkim * FUNCTION:    AcpiUtGetNextPredefinedMethod
72249109Sjkim *
73249109Sjkim * PARAMETERS:  ThisName            - Entry in the predefined method/name table
74249109Sjkim *
75249109Sjkim * RETURN:      Pointer to next entry in predefined table.
76249109Sjkim *
77249109Sjkim * DESCRIPTION: Get the next entry in the predefine method table. Handles the
78249109Sjkim *              cases where a package info entry follows a method name that
79249109Sjkim *              returns a package.
80249109Sjkim *
81249109Sjkim ******************************************************************************/
82249109Sjkim
83249109Sjkimconst ACPI_PREDEFINED_INFO *
84249109SjkimAcpiUtGetNextPredefinedMethod (
85249109Sjkim    const ACPI_PREDEFINED_INFO  *ThisName)
86249109Sjkim{
87249109Sjkim
88249109Sjkim    /*
89249109Sjkim     * Skip next entry in the table if this name returns a Package
90249109Sjkim     * (next entry contains the package info)
91249109Sjkim     */
92249109Sjkim    if ((ThisName->Info.ExpectedBtypes & ACPI_RTYPE_PACKAGE) &&
93249109Sjkim        (ThisName->Info.ExpectedBtypes != ACPI_RTYPE_ALL))
94249109Sjkim    {
95249109Sjkim        ThisName++;
96249109Sjkim    }
97249109Sjkim
98249109Sjkim    ThisName++;
99249109Sjkim    return (ThisName);
100249109Sjkim}
101249109Sjkim
102249109Sjkim
103249109Sjkim/*******************************************************************************
104249109Sjkim *
105249109Sjkim * FUNCTION:    AcpiUtMatchPredefinedMethod
106249109Sjkim *
107249109Sjkim * PARAMETERS:  Name                - Name to find
108249109Sjkim *
109249109Sjkim * RETURN:      Pointer to entry in predefined table. NULL indicates not found.
110249109Sjkim *
111249109Sjkim * DESCRIPTION: Check an object name against the predefined object list.
112249109Sjkim *
113249109Sjkim ******************************************************************************/
114249109Sjkim
115249109Sjkimconst ACPI_PREDEFINED_INFO *
116249109SjkimAcpiUtMatchPredefinedMethod (
117249109Sjkim    char                        *Name)
118249109Sjkim{
119249109Sjkim    const ACPI_PREDEFINED_INFO  *ThisName;
120249109Sjkim
121249109Sjkim
122249109Sjkim    /* Quick check for a predefined name, first character must be underscore */
123249109Sjkim
124249109Sjkim    if (Name[0] != '_')
125249109Sjkim    {
126249109Sjkim        return (NULL);
127249109Sjkim    }
128249109Sjkim
129249109Sjkim    /* Search info table for a predefined method/object name */
130249109Sjkim
131249109Sjkim    ThisName = AcpiGbl_PredefinedMethods;
132249109Sjkim    while (ThisName->Info.Name[0])
133249109Sjkim    {
134249109Sjkim        if (ACPI_COMPARE_NAME (Name, ThisName->Info.Name))
135249109Sjkim        {
136249109Sjkim            return (ThisName);
137249109Sjkim        }
138249109Sjkim
139249109Sjkim        ThisName = AcpiUtGetNextPredefinedMethod (ThisName);
140249109Sjkim    }
141249109Sjkim
142249109Sjkim    return (NULL); /* Not found */
143249109Sjkim}
144249109Sjkim
145249109Sjkim
146249109Sjkim/*******************************************************************************
147249109Sjkim *
148249109Sjkim * FUNCTION:    AcpiUtGetExpectedReturnTypes
149249109Sjkim *
150249109Sjkim * PARAMETERS:  Buffer              - Where the formatted string is returned
151249109Sjkim *              ExpectedBTypes      - Bitfield of expected data types
152249109Sjkim *
153249109Sjkim * RETURN:      Formatted string in Buffer.
154249109Sjkim *
155249109Sjkim * DESCRIPTION: Format the expected object types into a printable string.
156249109Sjkim *
157249109Sjkim ******************************************************************************/
158249109Sjkim
159249109Sjkimvoid
160249109SjkimAcpiUtGetExpectedReturnTypes (
161249109Sjkim    char                    *Buffer,
162249109Sjkim    UINT32                  ExpectedBtypes)
163249109Sjkim{
164249109Sjkim    UINT32                  ThisRtype;
165249109Sjkim    UINT32                  i;
166249109Sjkim    UINT32                  j;
167249109Sjkim
168249109Sjkim
169249663Sjkim    if (!ExpectedBtypes)
170249663Sjkim    {
171249663Sjkim        ACPI_STRCPY (Buffer, "NONE");
172249663Sjkim        return;
173249663Sjkim    }
174249663Sjkim
175249109Sjkim    j = 1;
176249109Sjkim    Buffer[0] = 0;
177249109Sjkim    ThisRtype = ACPI_RTYPE_INTEGER;
178249109Sjkim
179249109Sjkim    for (i = 0; i < ACPI_NUM_RTYPES; i++)
180249109Sjkim    {
181249109Sjkim        /* If one of the expected types, concatenate the name of this type */
182249109Sjkim
183249109Sjkim        if (ExpectedBtypes & ThisRtype)
184249109Sjkim        {
185249109Sjkim            ACPI_STRCAT (Buffer, &UtRtypeNames[i][j]);
186249109Sjkim            j = 0;              /* Use name separator from now on */
187249109Sjkim        }
188249109Sjkim
189249109Sjkim        ThisRtype <<= 1;    /* Next Rtype */
190249109Sjkim    }
191249109Sjkim}
192249109Sjkim
193249109Sjkim
194249109Sjkim/*******************************************************************************
195249109Sjkim *
196249109Sjkim * The remaining functions are used by iASL and AcpiHelp only
197249109Sjkim *
198249109Sjkim ******************************************************************************/
199249109Sjkim
200249109Sjkim#if (defined ACPI_ASL_COMPILER || defined ACPI_HELP_APP)
201249109Sjkim#include <stdio.h>
202249109Sjkim#include <string.h>
203249109Sjkim
204249109Sjkim/* Local prototypes */
205249109Sjkim
206249109Sjkimstatic UINT32
207249109SjkimAcpiUtGetArgumentTypes (
208249109Sjkim    char                    *Buffer,
209249109Sjkim    UINT16                  ArgumentTypes);
210249109Sjkim
211249109Sjkim
212249109Sjkim/* Types that can be returned externally by a predefined name */
213249109Sjkim
214249109Sjkimstatic const char   *UtExternalTypeNames[] = /* Indexed by ACPI_TYPE_* */
215249109Sjkim{
216249109Sjkim    ", UNSUPPORTED-TYPE",
217249109Sjkim    ", Integer",
218249109Sjkim    ", String",
219249109Sjkim    ", Buffer",
220249109Sjkim    ", Package"
221249109Sjkim};
222249109Sjkim
223249109Sjkim/* Bit widths for resource descriptor predefined names */
224249109Sjkim
225249109Sjkimstatic const char   *UtResourceTypeNames[] =
226249109Sjkim{
227249109Sjkim    "/1",
228249109Sjkim    "/2",
229249109Sjkim    "/3",
230249109Sjkim    "/8",
231249109Sjkim    "/16",
232249109Sjkim    "/32",
233249109Sjkim    "/64",
234249109Sjkim    "/variable",
235249109Sjkim};
236249109Sjkim
237249109Sjkim
238249109Sjkim/*******************************************************************************
239249109Sjkim *
240249109Sjkim * FUNCTION:    AcpiUtMatchResourceName
241249109Sjkim *
242249109Sjkim * PARAMETERS:  Name                - Name to find
243249109Sjkim *
244249109Sjkim * RETURN:      Pointer to entry in the resource table. NULL indicates not
245249109Sjkim *              found.
246249109Sjkim *
247249109Sjkim * DESCRIPTION: Check an object name against the predefined resource
248249109Sjkim *              descriptor object list.
249249109Sjkim *
250249109Sjkim ******************************************************************************/
251249109Sjkim
252249109Sjkimconst ACPI_PREDEFINED_INFO *
253249109SjkimAcpiUtMatchResourceName (
254249109Sjkim    char                        *Name)
255249109Sjkim{
256249109Sjkim    const ACPI_PREDEFINED_INFO  *ThisName;
257249109Sjkim
258249109Sjkim
259249109Sjkim    /* Quick check for a predefined name, first character must be underscore */
260249109Sjkim
261249109Sjkim    if (Name[0] != '_')
262249109Sjkim    {
263249109Sjkim        return (NULL);
264249109Sjkim    }
265249109Sjkim
266249109Sjkim    /* Search info table for a predefined method/object name */
267249109Sjkim
268249109Sjkim    ThisName = AcpiGbl_ResourceNames;
269249109Sjkim    while (ThisName->Info.Name[0])
270249109Sjkim    {
271249109Sjkim        if (ACPI_COMPARE_NAME (Name, ThisName->Info.Name))
272249109Sjkim        {
273249109Sjkim            return (ThisName);
274249109Sjkim        }
275249109Sjkim
276249109Sjkim        ThisName++;
277249109Sjkim    }
278249109Sjkim
279249109Sjkim    return (NULL); /* Not found */
280249109Sjkim}
281249109Sjkim
282249109Sjkim
283249109Sjkim/*******************************************************************************
284249109Sjkim *
285249109Sjkim * FUNCTION:    AcpiUtDisplayPredefinedMethod
286249109Sjkim *
287249109Sjkim * PARAMETERS:  Buffer              - Scratch buffer for this function
288249109Sjkim *              ThisName            - Entry in the predefined method/name table
289249109Sjkim *              MultiLine           - TRUE if output should be on >1 line
290249109Sjkim *
291249109Sjkim * RETURN:      None
292249109Sjkim *
293249109Sjkim * DESCRIPTION: Display information about a predefined method. Number and
294249109Sjkim *              type of the input arguments, and expected type(s) for the
295249109Sjkim *              return value, if any.
296249109Sjkim *
297249109Sjkim ******************************************************************************/
298249109Sjkim
299249109Sjkimvoid
300249109SjkimAcpiUtDisplayPredefinedMethod (
301249109Sjkim    char                        *Buffer,
302249109Sjkim    const ACPI_PREDEFINED_INFO  *ThisName,
303249109Sjkim    BOOLEAN                     MultiLine)
304249109Sjkim{
305249109Sjkim    UINT32                      ArgCount;
306249109Sjkim
307249109Sjkim    /*
308249109Sjkim     * Get the argument count and the string buffer
309249109Sjkim     * containing all argument types
310249109Sjkim     */
311249109Sjkim    ArgCount = AcpiUtGetArgumentTypes (Buffer,
312249109Sjkim        ThisName->Info.ArgumentList);
313249109Sjkim
314249109Sjkim    if (MultiLine)
315249109Sjkim    {
316249109Sjkim        printf ("      ");
317249109Sjkim    }
318249109Sjkim
319249109Sjkim    printf ("%4.4s    Requires %s%u argument%s",
320249109Sjkim        ThisName->Info.Name,
321249109Sjkim        (ThisName->Info.ArgumentList & ARG_COUNT_IS_MINIMUM) ?
322249109Sjkim            "(at least) " : "",
323249109Sjkim        ArgCount, ArgCount != 1 ? "s" : "");
324249109Sjkim
325249109Sjkim    /* Display the types for any arguments */
326249109Sjkim
327249109Sjkim    if (ArgCount > 0)
328249109Sjkim    {
329249109Sjkim        printf (" (%s)", Buffer);
330249109Sjkim    }
331249109Sjkim
332249109Sjkim    if (MultiLine)
333249109Sjkim    {
334249109Sjkim        printf ("\n    ");
335249109Sjkim    }
336249109Sjkim
337249109Sjkim    /* Get the return value type(s) allowed */
338249109Sjkim
339249109Sjkim    if (ThisName->Info.ExpectedBtypes)
340249109Sjkim    {
341249109Sjkim        AcpiUtGetExpectedReturnTypes (Buffer, ThisName->Info.ExpectedBtypes);
342249109Sjkim        printf ("  Return value types: %s\n", Buffer);
343249109Sjkim    }
344249109Sjkim    else
345249109Sjkim    {
346249109Sjkim        printf ("  No return value\n");
347249109Sjkim    }
348249109Sjkim}
349249109Sjkim
350249109Sjkim
351249109Sjkim/*******************************************************************************
352249109Sjkim *
353249109Sjkim * FUNCTION:    AcpiUtGetArgumentTypes
354249109Sjkim *
355249109Sjkim * PARAMETERS:  Buffer              - Where to return the formatted types
356249109Sjkim *              ArgumentTypes       - Types field for this method
357249109Sjkim *
358249109Sjkim * RETURN:      Count - the number of arguments required for this method
359249109Sjkim *
360249109Sjkim * DESCRIPTION: Format the required data types for this method (Integer,
361249109Sjkim *              String, Buffer, or Package) and return the required argument
362249109Sjkim *              count.
363249109Sjkim *
364249109Sjkim ******************************************************************************/
365249109Sjkim
366249109Sjkimstatic UINT32
367249109SjkimAcpiUtGetArgumentTypes (
368249109Sjkim    char                    *Buffer,
369249109Sjkim    UINT16                  ArgumentTypes)
370249109Sjkim{
371249109Sjkim    UINT16                  ThisArgumentType;
372249109Sjkim    UINT16                  SubIndex;
373249109Sjkim    UINT16                  ArgCount;
374249109Sjkim    UINT32                  i;
375249109Sjkim
376249109Sjkim
377249109Sjkim    *Buffer = 0;
378249109Sjkim    SubIndex = 2;
379249109Sjkim
380249109Sjkim    /* First field in the types list is the count of args to follow */
381249109Sjkim
382249663Sjkim    ArgCount = METHOD_GET_ARG_COUNT (ArgumentTypes);
383249109Sjkim    if (ArgCount > METHOD_PREDEF_ARGS_MAX)
384249109Sjkim    {
385249109Sjkim        printf ("**** Invalid argument count (%u) "
386249109Sjkim            "in predefined info structure\n", ArgCount);
387249109Sjkim        return (ArgCount);
388249109Sjkim    }
389249109Sjkim
390249109Sjkim    /* Get each argument from the list, convert to ascii, store to buffer */
391249109Sjkim
392249109Sjkim    for (i = 0; i < ArgCount; i++)
393249109Sjkim    {
394249663Sjkim        ThisArgumentType = METHOD_GET_NEXT_TYPE (ArgumentTypes);
395249663Sjkim
396249109Sjkim        if (!ThisArgumentType || (ThisArgumentType > METHOD_MAX_ARG_TYPE))
397249109Sjkim        {
398249109Sjkim            printf ("**** Invalid argument type (%u) "
399249109Sjkim                "in predefined info structure\n", ThisArgumentType);
400249109Sjkim            return (ArgCount);
401249109Sjkim        }
402249109Sjkim
403249109Sjkim        strcat (Buffer, UtExternalTypeNames[ThisArgumentType] + SubIndex);
404249109Sjkim        SubIndex = 0;
405249109Sjkim    }
406249109Sjkim
407249109Sjkim    return (ArgCount);
408249109Sjkim}
409249109Sjkim
410249109Sjkim
411249109Sjkim/*******************************************************************************
412249109Sjkim *
413249109Sjkim * FUNCTION:    AcpiUtGetResourceBitWidth
414249109Sjkim *
415249109Sjkim * PARAMETERS:  Buffer              - Where the formatted string is returned
416249109Sjkim *              Types               - Bitfield of expected data types
417249109Sjkim *
418249109Sjkim * RETURN:      Count of return types. Formatted string in Buffer.
419249109Sjkim *
420249109Sjkim * DESCRIPTION: Format the resource bit widths into a printable string.
421249109Sjkim *
422249109Sjkim ******************************************************************************/
423249109Sjkim
424249109SjkimUINT32
425249109SjkimAcpiUtGetResourceBitWidth (
426249109Sjkim    char                    *Buffer,
427249109Sjkim    UINT16                  Types)
428249109Sjkim{
429249109Sjkim    UINT32                  i;
430249109Sjkim    UINT16                  SubIndex;
431249109Sjkim    UINT32                  Found;
432249109Sjkim
433249109Sjkim
434249109Sjkim    *Buffer = 0;
435249109Sjkim    SubIndex = 1;
436249109Sjkim    Found = 0;
437249109Sjkim
438249109Sjkim    for (i = 0; i < NUM_RESOURCE_WIDTHS; i++)
439249109Sjkim    {
440249109Sjkim        if (Types & 1)
441249109Sjkim        {
442249109Sjkim            strcat (Buffer, &(UtResourceTypeNames[i][SubIndex]));
443249109Sjkim            SubIndex = 0;
444249109Sjkim            Found++;
445249109Sjkim        }
446249109Sjkim
447249109Sjkim        Types >>= 1;
448249109Sjkim    }
449249109Sjkim
450249109Sjkim    return (Found);
451249109Sjkim}
452249109Sjkim#endif
453