1/******************************************************************************
2 *
3 * Module Name: utpredef - support functions for predefined names
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2014, 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#include <acpi/acpi.h>
45#include "accommon.h"
46#include "acpredef.h"
47
48#define _COMPONENT          ACPI_UTILITIES
49ACPI_MODULE_NAME("utpredef")
50
51/*
52 * Names for the types that can be returned by the predefined objects.
53 * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
54 */
55static const char *ut_rtype_names[] = {
56	"/Integer",
57	"/String",
58	"/Buffer",
59	"/Package",
60	"/Reference",
61};
62
63/*******************************************************************************
64 *
65 * FUNCTION:    acpi_ut_get_next_predefined_method
66 *
67 * PARAMETERS:  this_name           - Entry in the predefined method/name table
68 *
69 * RETURN:      Pointer to next entry in predefined table.
70 *
71 * DESCRIPTION: Get the next entry in the predefine method table. Handles the
72 *              cases where a package info entry follows a method name that
73 *              returns a package.
74 *
75 ******************************************************************************/
76
77const union acpi_predefined_info *acpi_ut_get_next_predefined_method(const union
78								     acpi_predefined_info
79								     *this_name)
80{
81
82	/*
83	 * Skip next entry in the table if this name returns a Package
84	 * (next entry contains the package info)
85	 */
86	if ((this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) &&
87	    (this_name->info.expected_btypes != ACPI_RTYPE_ALL)) {
88		this_name++;
89	}
90
91	this_name++;
92	return (this_name);
93}
94
95/*******************************************************************************
96 *
97 * FUNCTION:    acpi_ut_match_predefined_method
98 *
99 * PARAMETERS:  name                - Name to find
100 *
101 * RETURN:      Pointer to entry in predefined table. NULL indicates not found.
102 *
103 * DESCRIPTION: Check an object name against the predefined object list.
104 *
105 ******************************************************************************/
106
107const union acpi_predefined_info *acpi_ut_match_predefined_method(char *name)
108{
109	const union acpi_predefined_info *this_name;
110
111	/* Quick check for a predefined name, first character must be underscore */
112
113	if (name[0] != '_') {
114		return (NULL);
115	}
116
117	/* Search info table for a predefined method/object name */
118
119	this_name = acpi_gbl_predefined_methods;
120	while (this_name->info.name[0]) {
121		if (ACPI_COMPARE_NAME(name, this_name->info.name)) {
122			return (this_name);
123		}
124
125		this_name = acpi_ut_get_next_predefined_method(this_name);
126	}
127
128	return (NULL);		/* Not found */
129}
130
131/*******************************************************************************
132 *
133 * FUNCTION:    acpi_ut_get_expected_return_types
134 *
135 * PARAMETERS:  buffer              - Where the formatted string is returned
136 *              expected_Btypes     - Bitfield of expected data types
137 *
138 * RETURN:      Formatted string in Buffer.
139 *
140 * DESCRIPTION: Format the expected object types into a printable string.
141 *
142 ******************************************************************************/
143
144void acpi_ut_get_expected_return_types(char *buffer, u32 expected_btypes)
145{
146	u32 this_rtype;
147	u32 i;
148	u32 j;
149
150	if (!expected_btypes) {
151		ACPI_STRCPY(buffer, "NONE");
152		return;
153	}
154
155	j = 1;
156	buffer[0] = 0;
157	this_rtype = ACPI_RTYPE_INTEGER;
158
159	for (i = 0; i < ACPI_NUM_RTYPES; i++) {
160
161		/* If one of the expected types, concatenate the name of this type */
162
163		if (expected_btypes & this_rtype) {
164			ACPI_STRCAT(buffer, &ut_rtype_names[i][j]);
165			j = 0;	/* Use name separator from now on */
166		}
167
168		this_rtype <<= 1;	/* Next Rtype */
169	}
170}
171
172/*******************************************************************************
173 *
174 * The remaining functions are used by iASL and acpi_help only
175 *
176 ******************************************************************************/
177
178#if (defined ACPI_ASL_COMPILER || defined ACPI_HELP_APP)
179#include <stdio.h>
180#include <string.h>
181
182/* Local prototypes */
183
184static u32 acpi_ut_get_argument_types(char *buffer, u16 argument_types);
185
186/* Types that can be returned externally by a predefined name */
187
188static const char *ut_external_type_names[] =	/* Indexed by ACPI_TYPE_* */
189{
190	", UNSUPPORTED-TYPE",
191	", Integer",
192	", String",
193	", Buffer",
194	", Package"
195};
196
197/* Bit widths for resource descriptor predefined names */
198
199static const char *ut_resource_type_names[] = {
200	"/1",
201	"/2",
202	"/3",
203	"/8",
204	"/16",
205	"/32",
206	"/64",
207	"/variable",
208};
209
210/*******************************************************************************
211 *
212 * FUNCTION:    acpi_ut_match_resource_name
213 *
214 * PARAMETERS:  name                - Name to find
215 *
216 * RETURN:      Pointer to entry in the resource table. NULL indicates not
217 *              found.
218 *
219 * DESCRIPTION: Check an object name against the predefined resource
220 *              descriptor object list.
221 *
222 ******************************************************************************/
223
224const union acpi_predefined_info *acpi_ut_match_resource_name(char *name)
225{
226	const union acpi_predefined_info *this_name;
227
228	/* Quick check for a predefined name, first character must be underscore */
229
230	if (name[0] != '_') {
231		return (NULL);
232	}
233
234	/* Search info table for a predefined method/object name */
235
236	this_name = acpi_gbl_resource_names;
237	while (this_name->info.name[0]) {
238		if (ACPI_COMPARE_NAME(name, this_name->info.name)) {
239			return (this_name);
240		}
241
242		this_name++;
243	}
244
245	return (NULL);		/* Not found */
246}
247
248/*******************************************************************************
249 *
250 * FUNCTION:    acpi_ut_display_predefined_method
251 *
252 * PARAMETERS:  buffer              - Scratch buffer for this function
253 *              this_name           - Entry in the predefined method/name table
254 *              multi_line          - TRUE if output should be on >1 line
255 *
256 * RETURN:      None
257 *
258 * DESCRIPTION: Display information about a predefined method. Number and
259 *              type of the input arguments, and expected type(s) for the
260 *              return value, if any.
261 *
262 ******************************************************************************/
263
264void
265acpi_ut_display_predefined_method(char *buffer,
266				  const union acpi_predefined_info *this_name,
267				  u8 multi_line)
268{
269	u32 arg_count;
270
271	/*
272	 * Get the argument count and the string buffer
273	 * containing all argument types
274	 */
275	arg_count = acpi_ut_get_argument_types(buffer,
276					       this_name->info.argument_list);
277
278	if (multi_line) {
279		printf("      ");
280	}
281
282	printf("%4.4s    Requires %s%u argument%s",
283	       this_name->info.name,
284	       (this_name->info.argument_list & ARG_COUNT_IS_MINIMUM) ?
285	       "(at least) " : "", arg_count, arg_count != 1 ? "s" : "");
286
287	/* Display the types for any arguments */
288
289	if (arg_count > 0) {
290		printf(" (%s)", buffer);
291	}
292
293	if (multi_line) {
294		printf("\n    ");
295	}
296
297	/* Get the return value type(s) allowed */
298
299	if (this_name->info.expected_btypes) {
300		acpi_ut_get_expected_return_types(buffer,
301						  this_name->info.
302						  expected_btypes);
303		printf("  Return value types: %s\n", buffer);
304	} else {
305		printf("  No return value\n");
306	}
307}
308
309/*******************************************************************************
310 *
311 * FUNCTION:    acpi_ut_get_argument_types
312 *
313 * PARAMETERS:  buffer              - Where to return the formatted types
314 *              argument_types      - Types field for this method
315 *
316 * RETURN:      count - the number of arguments required for this method
317 *
318 * DESCRIPTION: Format the required data types for this method (Integer,
319 *              String, Buffer, or Package) and return the required argument
320 *              count.
321 *
322 ******************************************************************************/
323
324static u32 acpi_ut_get_argument_types(char *buffer, u16 argument_types)
325{
326	u16 this_argument_type;
327	u16 sub_index;
328	u16 arg_count;
329	u32 i;
330
331	*buffer = 0;
332	sub_index = 2;
333
334	/* First field in the types list is the count of args to follow */
335
336	arg_count = METHOD_GET_ARG_COUNT(argument_types);
337	if (arg_count > METHOD_PREDEF_ARGS_MAX) {
338		printf("**** Invalid argument count (%u) "
339		       "in predefined info structure\n", arg_count);
340		return (arg_count);
341	}
342
343	/* Get each argument from the list, convert to ascii, store to buffer */
344
345	for (i = 0; i < arg_count; i++) {
346		this_argument_type = METHOD_GET_NEXT_TYPE(argument_types);
347
348		if (!this_argument_type
349		    || (this_argument_type > METHOD_MAX_ARG_TYPE)) {
350			printf("**** Invalid argument type (%u) "
351			       "in predefined info structure\n",
352			       this_argument_type);
353			return (arg_count);
354		}
355
356		strcat(buffer,
357		       ut_external_type_names[this_argument_type] + sub_index);
358		sub_index = 0;
359	}
360
361	return (arg_count);
362}
363
364/*******************************************************************************
365 *
366 * FUNCTION:    acpi_ut_get_resource_bit_width
367 *
368 * PARAMETERS:  buffer              - Where the formatted string is returned
369 *              types               - Bitfield of expected data types
370 *
371 * RETURN:      Count of return types. Formatted string in Buffer.
372 *
373 * DESCRIPTION: Format the resource bit widths into a printable string.
374 *
375 ******************************************************************************/
376
377u32 acpi_ut_get_resource_bit_width(char *buffer, u16 types)
378{
379	u32 i;
380	u16 sub_index;
381	u32 found;
382
383	*buffer = 0;
384	sub_index = 1;
385	found = 0;
386
387	for (i = 0; i < NUM_RESOURCE_WIDTHS; i++) {
388		if (types & 1) {
389			strcat(buffer, &(ut_resource_type_names[i][sub_index]));
390			sub_index = 0;
391			found++;
392		}
393
394		types >>= 1;
395	}
396
397	return (found);
398}
399#endif
400