xref: /freebsd-src/sys/contrib/dev/acpica/components/utilities/utpredef.c (revision 9c7c683c56f9d25aa7c25c40d530d7a153b19d18)
1 /******************************************************************************
2  *
3  * Module Name: utpredef - support functions for predefined names
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2013, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #define __UTPREDEF_C__
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acpredef.h>
49 
50 
51 #define _COMPONENT          ACPI_UTILITIES
52         ACPI_MODULE_NAME    ("utpredef")
53 
54 
55 /*
56  * Names for the types that can be returned by the predefined objects.
57  * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
58  */
59 static const char   *UtRtypeNames[] =
60 {
61     "/Integer",
62     "/String",
63     "/Buffer",
64     "/Package",
65     "/Reference",
66 };
67 
68 
69 /*******************************************************************************
70  *
71  * FUNCTION:    AcpiUtGetNextPredefinedMethod
72  *
73  * PARAMETERS:  ThisName            - Entry in the predefined method/name table
74  *
75  * RETURN:      Pointer to next entry in predefined table.
76  *
77  * DESCRIPTION: Get the next entry in the predefine method table. Handles the
78  *              cases where a package info entry follows a method name that
79  *              returns a package.
80  *
81  ******************************************************************************/
82 
83 const ACPI_PREDEFINED_INFO *
84 AcpiUtGetNextPredefinedMethod (
85     const ACPI_PREDEFINED_INFO  *ThisName)
86 {
87 
88     /*
89      * Skip next entry in the table if this name returns a Package
90      * (next entry contains the package info)
91      */
92     if ((ThisName->Info.ExpectedBtypes & ACPI_RTYPE_PACKAGE) &&
93         (ThisName->Info.ExpectedBtypes != ACPI_RTYPE_ALL))
94     {
95         ThisName++;
96     }
97 
98     ThisName++;
99     return (ThisName);
100 }
101 
102 
103 /*******************************************************************************
104  *
105  * FUNCTION:    AcpiUtMatchPredefinedMethod
106  *
107  * PARAMETERS:  Name                - Name to find
108  *
109  * RETURN:      Pointer to entry in predefined table. NULL indicates not found.
110  *
111  * DESCRIPTION: Check an object name against the predefined object list.
112  *
113  ******************************************************************************/
114 
115 const ACPI_PREDEFINED_INFO *
116 AcpiUtMatchPredefinedMethod (
117     char                        *Name)
118 {
119     const ACPI_PREDEFINED_INFO  *ThisName;
120 
121 
122     /* Quick check for a predefined name, first character must be underscore */
123 
124     if (Name[0] != '_')
125     {
126         return (NULL);
127     }
128 
129     /* Search info table for a predefined method/object name */
130 
131     ThisName = AcpiGbl_PredefinedMethods;
132     while (ThisName->Info.Name[0])
133     {
134         if (ACPI_COMPARE_NAME (Name, ThisName->Info.Name))
135         {
136             return (ThisName);
137         }
138 
139         ThisName = AcpiUtGetNextPredefinedMethod (ThisName);
140     }
141 
142     return (NULL); /* Not found */
143 }
144 
145 
146 /*******************************************************************************
147  *
148  * FUNCTION:    AcpiUtGetExpectedReturnTypes
149  *
150  * PARAMETERS:  Buffer              - Where the formatted string is returned
151  *              ExpectedBTypes      - Bitfield of expected data types
152  *
153  * RETURN:      Formatted string in Buffer.
154  *
155  * DESCRIPTION: Format the expected object types into a printable string.
156  *
157  ******************************************************************************/
158 
159 void
160 AcpiUtGetExpectedReturnTypes (
161     char                    *Buffer,
162     UINT32                  ExpectedBtypes)
163 {
164     UINT32                  ThisRtype;
165     UINT32                  i;
166     UINT32                  j;
167 
168 
169     j = 1;
170     Buffer[0] = 0;
171     ThisRtype = ACPI_RTYPE_INTEGER;
172 
173     for (i = 0; i < ACPI_NUM_RTYPES; i++)
174     {
175         /* If one of the expected types, concatenate the name of this type */
176 
177         if (ExpectedBtypes & ThisRtype)
178         {
179             ACPI_STRCAT (Buffer, &UtRtypeNames[i][j]);
180             j = 0;              /* Use name separator from now on */
181         }
182 
183         ThisRtype <<= 1;    /* Next Rtype */
184     }
185 }
186 
187 
188 /*******************************************************************************
189  *
190  * The remaining functions are used by iASL and AcpiHelp only
191  *
192  ******************************************************************************/
193 
194 #if (defined ACPI_ASL_COMPILER || defined ACPI_HELP_APP)
195 #include <stdio.h>
196 #include <string.h>
197 
198 /* Local prototypes */
199 
200 static UINT32
201 AcpiUtGetArgumentTypes (
202     char                    *Buffer,
203     UINT16                  ArgumentTypes);
204 
205 
206 /* Types that can be returned externally by a predefined name */
207 
208 static const char   *UtExternalTypeNames[] = /* Indexed by ACPI_TYPE_* */
209 {
210     ", UNSUPPORTED-TYPE",
211     ", Integer",
212     ", String",
213     ", Buffer",
214     ", Package"
215 };
216 
217 /* Bit widths for resource descriptor predefined names */
218 
219 static const char   *UtResourceTypeNames[] =
220 {
221     "/1",
222     "/2",
223     "/3",
224     "/8",
225     "/16",
226     "/32",
227     "/64",
228     "/variable",
229 };
230 
231 
232 /*******************************************************************************
233  *
234  * FUNCTION:    AcpiUtMatchResourceName
235  *
236  * PARAMETERS:  Name                - Name to find
237  *
238  * RETURN:      Pointer to entry in the resource table. NULL indicates not
239  *              found.
240  *
241  * DESCRIPTION: Check an object name against the predefined resource
242  *              descriptor object list.
243  *
244  ******************************************************************************/
245 
246 const ACPI_PREDEFINED_INFO *
247 AcpiUtMatchResourceName (
248     char                        *Name)
249 {
250     const ACPI_PREDEFINED_INFO  *ThisName;
251 
252 
253     /* Quick check for a predefined name, first character must be underscore */
254 
255     if (Name[0] != '_')
256     {
257         return (NULL);
258     }
259 
260     /* Search info table for a predefined method/object name */
261 
262     ThisName = AcpiGbl_ResourceNames;
263     while (ThisName->Info.Name[0])
264     {
265         if (ACPI_COMPARE_NAME (Name, ThisName->Info.Name))
266         {
267             return (ThisName);
268         }
269 
270         ThisName++;
271     }
272 
273     return (NULL); /* Not found */
274 }
275 
276 
277 /*******************************************************************************
278  *
279  * FUNCTION:    AcpiUtDisplayPredefinedMethod
280  *
281  * PARAMETERS:  Buffer              - Scratch buffer for this function
282  *              ThisName            - Entry in the predefined method/name table
283  *              MultiLine           - TRUE if output should be on >1 line
284  *
285  * RETURN:      None
286  *
287  * DESCRIPTION: Display information about a predefined method. Number and
288  *              type of the input arguments, and expected type(s) for the
289  *              return value, if any.
290  *
291  ******************************************************************************/
292 
293 void
294 AcpiUtDisplayPredefinedMethod (
295     char                        *Buffer,
296     const ACPI_PREDEFINED_INFO  *ThisName,
297     BOOLEAN                     MultiLine)
298 {
299     UINT32                      ArgCount;
300 
301     /*
302      * Get the argument count and the string buffer
303      * containing all argument types
304      */
305     ArgCount = AcpiUtGetArgumentTypes (Buffer,
306         ThisName->Info.ArgumentList);
307 
308     if (MultiLine)
309     {
310         printf ("      ");
311     }
312 
313     printf ("%4.4s    Requires %s%u argument%s",
314         ThisName->Info.Name,
315         (ThisName->Info.ArgumentList & ARG_COUNT_IS_MINIMUM) ?
316             "(at least) " : "",
317         ArgCount, ArgCount != 1 ? "s" : "");
318 
319     /* Display the types for any arguments */
320 
321     if (ArgCount > 0)
322     {
323         printf (" (%s)", Buffer);
324     }
325 
326     if (MultiLine)
327     {
328         printf ("\n    ");
329     }
330 
331     /* Get the return value type(s) allowed */
332 
333     if (ThisName->Info.ExpectedBtypes)
334     {
335         AcpiUtGetExpectedReturnTypes (Buffer, ThisName->Info.ExpectedBtypes);
336         printf ("  Return value types: %s\n", Buffer);
337     }
338     else
339     {
340         printf ("  No return value\n");
341     }
342 }
343 
344 
345 /*******************************************************************************
346  *
347  * FUNCTION:    AcpiUtGetArgumentTypes
348  *
349  * PARAMETERS:  Buffer              - Where to return the formatted types
350  *              ArgumentTypes       - Types field for this method
351  *
352  * RETURN:      Count - the number of arguments required for this method
353  *
354  * DESCRIPTION: Format the required data types for this method (Integer,
355  *              String, Buffer, or Package) and return the required argument
356  *              count.
357  *
358  ******************************************************************************/
359 
360 static UINT32
361 AcpiUtGetArgumentTypes (
362     char                    *Buffer,
363     UINT16                  ArgumentTypes)
364 {
365     UINT16                  ThisArgumentType;
366     UINT16                  SubIndex;
367     UINT16                  ArgCount;
368     UINT32                  i;
369 
370 
371     *Buffer = 0;
372     SubIndex = 2;
373 
374     /* First field in the types list is the count of args to follow */
375 
376     ArgCount = (ArgumentTypes & METHOD_ARG_MASK);
377     ArgumentTypes >>= METHOD_ARG_BIT_WIDTH;
378 
379     if (ArgCount > METHOD_PREDEF_ARGS_MAX)
380     {
381         printf ("**** Invalid argument count (%u) "
382             "in predefined info structure\n", ArgCount);
383         return (ArgCount);
384     }
385 
386     /* Get each argument from the list, convert to ascii, store to buffer */
387 
388     for (i = 0; i < ArgCount; i++)
389     {
390         ThisArgumentType = (ArgumentTypes & METHOD_ARG_MASK);
391         if (!ThisArgumentType || (ThisArgumentType > METHOD_MAX_ARG_TYPE))
392         {
393             printf ("**** Invalid argument type (%u) "
394                 "in predefined info structure\n", ThisArgumentType);
395             return (ArgCount);
396         }
397 
398         strcat (Buffer, UtExternalTypeNames[ThisArgumentType] + SubIndex);
399 
400         /* Shift to next argument type field */
401 
402         ArgumentTypes >>= METHOD_ARG_BIT_WIDTH;
403         SubIndex = 0;
404     }
405 
406     return (ArgCount);
407 }
408 
409 
410 /*******************************************************************************
411  *
412  * FUNCTION:    AcpiUtGetResourceBitWidth
413  *
414  * PARAMETERS:  Buffer              - Where the formatted string is returned
415  *              Types               - Bitfield of expected data types
416  *
417  * RETURN:      Count of return types. Formatted string in Buffer.
418  *
419  * DESCRIPTION: Format the resource bit widths into a printable string.
420  *
421  ******************************************************************************/
422 
423 UINT32
424 AcpiUtGetResourceBitWidth (
425     char                    *Buffer,
426     UINT16                  Types)
427 {
428     UINT32                  i;
429     UINT16                  SubIndex;
430     UINT32                  Found;
431 
432 
433     *Buffer = 0;
434     SubIndex = 1;
435     Found = 0;
436 
437     for (i = 0; i < NUM_RESOURCE_WIDTHS; i++)
438     {
439         if (Types & 1)
440         {
441             strcat (Buffer, &(UtResourceTypeNames[i][SubIndex]));
442             SubIndex = 0;
443             Found++;
444         }
445 
446         Types >>= 1;
447     }
448 
449     return (Found);
450 }
451 #endif
452