[ACPI] ACPICA 20051021
[linux-3.10.git] / drivers / acpi / resources / rslist.c
1 /*******************************************************************************
2  *
3  * Module Name: rslist - Linked list utilities
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
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 <acpi/acresrc.h>
46
47 #define _COMPONENT          ACPI_RESOURCES
48 ACPI_MODULE_NAME("rslist")
49
50 /* Local prototypes */
51 static struct acpi_rsconvert_info *acpi_rs_get_conversion_info(u8
52                                                                resource_type);
53
54 static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml);
55
56 /*******************************************************************************
57  *
58  * FUNCTION:    acpi_rs_validate_resource_length
59  *
60  * PARAMETERS:  Aml                 - Pointer to the AML resource descriptor
61  *
62  * RETURN:      Status - AE_OK if the resource length appears valid
63  *
64  * DESCRIPTION: Validate the resource_length. Fixed-length descriptors must
65  *              have the exact length; variable-length descriptors must be
66  *              at least as long as the minimum. Certain Small descriptors
67  *              can vary in size by at most one byte.
68  *
69  ******************************************************************************/
70
71 static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml)
72 {
73         struct acpi_resource_info *resource_info;
74         u16 minimum_aml_resource_length;
75         u16 resource_length;
76
77         ACPI_FUNCTION_ENTRY();
78
79         /* Get the size and type info about this resource descriptor */
80
81         resource_info =
82             acpi_rs_get_resource_info(aml->small_header.descriptor_type);
83         if (!resource_info) {
84                 return (AE_AML_INVALID_RESOURCE_TYPE);
85         }
86
87         resource_length = acpi_ut_get_resource_length(aml);
88         minimum_aml_resource_length =
89             resource_info->minimum_aml_resource_length;
90
91         /* Validate based upon the type of resource, fixed length or variable */
92
93         if (resource_info->length_type == ACPI_FIXED_LENGTH) {
94                 /* Fixed length resource, length must match exactly */
95
96                 if (resource_length != minimum_aml_resource_length) {
97                         return (AE_AML_BAD_RESOURCE_LENGTH);
98                 }
99         } else if (resource_info->length_type == ACPI_VARIABLE_LENGTH) {
100                 /* Variable length resource, must be at least the minimum */
101
102                 if (resource_length < minimum_aml_resource_length) {
103                         return (AE_AML_BAD_RESOURCE_LENGTH);
104                 }
105         } else {
106                 /* Small variable length resource, allowed to be (Min) or (Min-1) */
107
108                 if ((resource_length > minimum_aml_resource_length) ||
109                     (resource_length < (minimum_aml_resource_length - 1))) {
110                         return (AE_AML_BAD_RESOURCE_LENGTH);
111                 }
112         }
113
114         return (AE_OK);
115 }
116
117 /*******************************************************************************
118  *
119  * FUNCTION:    acpi_rs_get_conversion_info
120  *
121  * PARAMETERS:  resource_type       - Byte 0 of a resource descriptor
122  *
123  * RETURN:      Pointer to the resource conversion info table
124  *
125  * DESCRIPTION: Get the conversion table associated with this resource type
126  *
127  ******************************************************************************/
128
129 static struct acpi_rsconvert_info *acpi_rs_get_conversion_info(u8 resource_type)
130 {
131         ACPI_FUNCTION_ENTRY();
132
133         /* Determine if this is a small or large resource */
134
135         if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
136                 /* Large Resource Type -- bits 6:0 contain the name */
137
138                 if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
139                         return (NULL);
140                 }
141
142                 return (acpi_gbl_lg_get_resource_dispatch[(resource_type &
143                                                            ACPI_RESOURCE_NAME_LARGE_MASK)]);
144         } else {
145                 /* Small Resource Type -- bits 6:3 contain the name */
146
147                 return (acpi_gbl_sm_get_resource_dispatch[((resource_type &
148                                                             ACPI_RESOURCE_NAME_SMALL_MASK)
149                                                            >> 3)]);
150         }
151 }
152
153 /*******************************************************************************
154  *
155  * FUNCTION:    acpi_rs_convert_aml_to_resources
156  *
157  * PARAMETERS:  aml_buffer          - Pointer to the resource byte stream
158  *              aml_buffer_length   - Length of aml_buffer
159  *              output_buffer       - Pointer to the buffer that will
160  *                                    contain the output structures
161  *
162  * RETURN:      Status
163  *
164  * DESCRIPTION: Takes the resource byte stream and parses it, creating a
165  *              linked list of resources in the caller's output buffer
166  *
167  ******************************************************************************/
168
169 acpi_status
170 acpi_rs_convert_aml_to_resources(u8 * aml_buffer,
171                                  u32 aml_buffer_length, u8 * output_buffer)
172 {
173         u8 *buffer = output_buffer;
174         acpi_status status;
175         acpi_size bytes_parsed = 0;
176         struct acpi_resource *resource;
177         acpi_rsdesc_size descriptor_length;
178         struct acpi_rsconvert_info *info;
179
180         ACPI_FUNCTION_TRACE("rs_convert_aml_to_resources");
181
182         /* Loop until end-of-buffer or an end_tag is found */
183
184         while (bytes_parsed < aml_buffer_length) {
185                 /* Get the conversion table associated with this Descriptor Type */
186
187                 info = acpi_rs_get_conversion_info(*aml_buffer);
188                 if (!info) {
189                         /* No table indicates an invalid resource type */
190
191                         return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
192                 }
193
194                 descriptor_length = acpi_ut_get_descriptor_length(aml_buffer);
195
196                 /*
197                  * Perform limited validation of the resource length, based upon
198                  * what we know about the resource type
199                  */
200                 status =
201                     acpi_rs_validate_resource_length(ACPI_CAST_PTR
202                                                      (union aml_resource,
203                                                       aml_buffer));
204                 if (ACPI_FAILURE(status)) {
205                         return_ACPI_STATUS(status);
206                 }
207
208                 /* Convert the AML byte stream resource to a local resource struct */
209
210                 status =
211                     acpi_rs_convert_aml_to_resource(ACPI_CAST_PTR
212                                                     (struct acpi_resource,
213                                                      buffer),
214                                                     ACPI_CAST_PTR(union
215                                                                   aml_resource,
216                                                                   aml_buffer),
217                                                     info);
218                 if (ACPI_FAILURE(status)) {
219                         ACPI_REPORT_ERROR(("Could not convert AML resource (type %X) to resource, %s\n", *aml_buffer, acpi_format_exception(status)));
220                         return_ACPI_STATUS(status);
221                 }
222
223                 /* Set the aligned length of the new resource descriptor */
224
225                 resource = ACPI_CAST_PTR(struct acpi_resource, buffer);
226                 resource->length =
227                     (u32) ACPI_ALIGN_RESOURCE_SIZE(resource->length);
228
229                 /* Normal exit on completion of an end_tag resource descriptor */
230
231                 if (acpi_ut_get_resource_type(aml_buffer) ==
232                     ACPI_RESOURCE_NAME_END_TAG) {
233                         return_ACPI_STATUS(AE_OK);
234                 }
235
236                 /* Update counter and point to the next input resource */
237
238                 bytes_parsed += descriptor_length;
239                 aml_buffer += descriptor_length;
240
241                 /* Point to the next structure in the output buffer */
242
243                 buffer += resource->length;
244         }
245
246         /* Completed buffer, but did not find an end_tag resource descriptor */
247
248         return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
249 }
250
251 /*******************************************************************************
252  *
253  * FUNCTION:    acpi_rs_convert_resources_to_aml
254  *
255  * PARAMETERS:  Resource            - Pointer to the resource linked list
256  *              aml_size_needed     - Calculated size of the byte stream
257  *                                    needed from calling acpi_rs_get_aml_length()
258  *                                    The size of the output_buffer is
259  *                                    guaranteed to be >= aml_size_needed
260  *              output_buffer       - Pointer to the buffer that will
261  *                                    contain the byte stream
262  *
263  * RETURN:      Status
264  *
265  * DESCRIPTION: Takes the resource linked list and parses it, creating a
266  *              byte stream of resources in the caller's output buffer
267  *
268  ******************************************************************************/
269
270 acpi_status
271 acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
272                                  acpi_size aml_size_needed, u8 * output_buffer)
273 {
274         u8 *aml_buffer = output_buffer;
275         u8 *end_aml_buffer = output_buffer + aml_size_needed;
276         acpi_status status;
277
278         ACPI_FUNCTION_TRACE("rs_convert_resources_to_aml");
279
280         /* Walk the resource descriptor list, convert each descriptor */
281
282         while (aml_buffer < end_aml_buffer) {
283                 /* Validate the Resource Type */
284
285                 if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
286                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
287                                           "Invalid descriptor type (%X) in resource list\n",
288                                           resource->type));
289                         return_ACPI_STATUS(AE_BAD_DATA);
290                 }
291
292                 /* Perform the conversion */
293
294                 status = acpi_rs_convert_resource_to_aml(resource,
295                                                          ACPI_CAST_PTR(union
296                                                                        aml_resource,
297                                                                        aml_buffer),
298                                                          acpi_gbl_set_resource_dispatch
299                                                          [resource->type]);
300                 if (ACPI_FAILURE(status)) {
301                         ACPI_REPORT_ERROR(("Could not convert resource (type %X) to AML, %s\n", resource->type, acpi_format_exception(status)));
302                         return_ACPI_STATUS(status);
303                 }
304
305                 /* Perform final sanity check on the new AML resource descriptor */
306
307                 status =
308                     acpi_rs_validate_resource_length(ACPI_CAST_PTR
309                                                      (union aml_resource,
310                                                       aml_buffer));
311                 if (ACPI_FAILURE(status)) {
312                         return_ACPI_STATUS(status);
313                 }
314
315                 /* Check for end-of-list, normal exit */
316
317                 if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
318                         /* An End Tag indicates the end of the input Resource Template */
319
320                         return_ACPI_STATUS(AE_OK);
321                 }
322
323                 /*
324                  * Extract the total length of the new descriptor and set the
325                  * aml_buffer to point to the next (output) resource descriptor
326                  */
327                 aml_buffer += acpi_ut_get_descriptor_length(aml_buffer);
328
329                 /* Point to the next input resource descriptor */
330
331                 resource =
332                     ACPI_PTR_ADD(struct acpi_resource, resource,
333                                  resource->length);
334
335                 /* Check for end-of-list, normal exit */
336
337         }
338
339         /* Completed buffer, but did not find an end_tag resource descriptor */
340
341         return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
342 }