[ACPI] ACPICA 20050930
[linux-2.6.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 ACPI_GET_RESOURCE_HANDLER acpi_rs_get_resource_handler(u8 resource_type);
52
53 static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml);
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    acpi_rs_validate_resource_length
58  *
59  * PARAMETERS:  Aml                 - Pointer to the AML resource descriptor
60  *
61  * RETURN:      Status - AE_OK if the resource length appears valid
62  *
63  * DESCRIPTION: Validate the resource_length. Fixed-length descriptors must
64  *              have the exact length; variable-length descriptors must be
65  *              at least as long as the minimum. Certain Small descriptors
66  *              can vary in size by at most one byte.
67  *
68  ******************************************************************************/
69
70 static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml)
71 {
72         struct acpi_resource_info *resource_info;
73         u16 minimum_aml_resource_length;
74         u16 resource_length;
75
76         ACPI_FUNCTION_ENTRY();
77
78         /* Get the size and type info about this resource descriptor */
79
80         resource_info =
81             acpi_rs_get_resource_info(aml->small_header.descriptor_type);
82         if (!resource_info) {
83                 return (AE_AML_INVALID_RESOURCE_TYPE);
84         }
85
86         resource_length = acpi_rs_get_resource_length(aml);
87         minimum_aml_resource_length =
88             resource_info->minimum_aml_resource_length;
89
90         /* Validate based upon the type of resource, fixed length or variable */
91
92         if (resource_info->length_type == ACPI_FIXED_LENGTH) {
93                 /* Fixed length resource, length must match exactly */
94
95                 if (resource_length != minimum_aml_resource_length) {
96                         return (AE_AML_BAD_RESOURCE_LENGTH);
97                 }
98         } else if (resource_info->length_type == ACPI_VARIABLE_LENGTH) {
99                 /* Variable length resource, must be at least the minimum */
100
101                 if (resource_length < minimum_aml_resource_length) {
102                         return (AE_AML_BAD_RESOURCE_LENGTH);
103                 }
104         } else {
105                 /* Small variable length resource, allowed to be (Min) or (Min-1) */
106
107                 if ((resource_length > minimum_aml_resource_length) ||
108                     (resource_length < (minimum_aml_resource_length - 1))) {
109                         return (AE_AML_BAD_RESOURCE_LENGTH);
110                 }
111         }
112
113         return (AE_OK);
114 }
115
116 /*******************************************************************************
117  *
118  * FUNCTION:    acpi_rs_get_resource_handler
119  *
120  * PARAMETERS:  resource_type       - Byte 0 of a resource descriptor
121  *
122  * RETURN:      Pointer to the resource conversion handler
123  *
124  * DESCRIPTION: Extract the Resource Type/Name from the first byte of
125  *              a resource descriptor.
126  *
127  ******************************************************************************/
128
129 static ACPI_GET_RESOURCE_HANDLER acpi_rs_get_resource_handler(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         u16 resource_length;
178         u32 descriptor_length;
179         ACPI_GET_RESOURCE_HANDLER handler;
180
181         ACPI_FUNCTION_TRACE("rs_convert_aml_to_resources");
182
183         /* Loop until end-of-buffer or an end_tag is found */
184
185         while (bytes_parsed < aml_buffer_length) {
186                 /* Get the handler associated with this Descriptor Type */
187
188                 handler = acpi_rs_get_resource_handler(*aml_buffer);
189                 if (!handler) {
190                         /* No handler indicates invalid resource type */
191
192                         return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
193                 }
194
195                 resource_length =
196                     acpi_rs_get_resource_length(ACPI_CAST_PTR
197                                                 (union aml_resource,
198                                                  aml_buffer));
199
200                 descriptor_length =
201                     acpi_rs_get_descriptor_length(ACPI_CAST_PTR
202                                                   (union aml_resource,
203                                                    aml_buffer));
204
205                 /*
206                  * Perform limited validation of the resource length, based upon
207                  * what we know about the resource type
208                  */
209                 status =
210                     acpi_rs_validate_resource_length(ACPI_CAST_PTR
211                                                      (union aml_resource,
212                                                       aml_buffer));
213                 if (ACPI_FAILURE(status)) {
214                         return_ACPI_STATUS(status);
215                 }
216
217                 /* Convert a byte stream resource to local resource struct */
218
219                 status = handler(ACPI_CAST_PTR(union aml_resource, aml_buffer),
220                                  resource_length,
221                                  ACPI_CAST_PTR(struct acpi_resource, buffer));
222                 if (ACPI_FAILURE(status)) {
223                         ACPI_REPORT_ERROR(("Could not convert AML resource (type %X) to resource, %s\n", *aml_buffer, acpi_format_exception(status)));
224                         return_ACPI_STATUS(status);
225                 }
226
227                 /* Set the aligned length of the new resource descriptor */
228
229                 resource = ACPI_CAST_PTR(struct acpi_resource, buffer);
230                 resource->length =
231                     (u32) ACPI_ALIGN_RESOURCE_SIZE(resource->length);
232
233                 /* Normal exit on completion of an end_tag resource descriptor */
234
235                 if (acpi_rs_get_resource_type(*aml_buffer) ==
236                     ACPI_RESOURCE_NAME_END_TAG) {
237                         return_ACPI_STATUS(AE_OK);
238                 }
239
240                 /* Update counter and point to the next input resource */
241
242                 bytes_parsed += descriptor_length;
243                 aml_buffer += descriptor_length;
244
245                 /* Point to the next structure in the output buffer */
246
247                 buffer += resource->length;
248         }
249
250         /* Completed buffer, but did not find an end_tag resource descriptor */
251
252         return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
253 }
254
255 /*******************************************************************************
256  *
257  * FUNCTION:    acpi_rs_convert_resources_to_aml
258  *
259  * PARAMETERS:  Resource            - Pointer to the resource linked list
260  *              aml_size_needed     - Calculated size of the byte stream
261  *                                    needed from calling acpi_rs_get_aml_length()
262  *                                    The size of the output_buffer is
263  *                                    guaranteed to be >= aml_size_needed
264  *              output_buffer       - Pointer to the buffer that will
265  *                                    contain the byte stream
266  *
267  * RETURN:      Status
268  *
269  * DESCRIPTION: Takes the resource linked list and parses it, creating a
270  *              byte stream of resources in the caller's output buffer
271  *
272  ******************************************************************************/
273
274 acpi_status
275 acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
276                                  acpi_size aml_size_needed, u8 * output_buffer)
277 {
278         u8 *aml_buffer = output_buffer;
279         acpi_status status;
280
281         ACPI_FUNCTION_TRACE("rs_convert_resources_to_aml");
282
283         /* Convert each resource descriptor in the list */
284
285         while (1) {
286                 /* Validate Resource Descriptor Type before dispatch */
287
288                 if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
289                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
290                                           "Invalid descriptor type (%X) in resource list\n",
291                                           resource->type));
292                         return_ACPI_STATUS(AE_BAD_DATA);
293                 }
294
295                 /* Perform the conversion per resource type */
296
297                 status =
298                     acpi_gbl_set_resource_dispatch[resource->type] (resource,
299                                                                     ACPI_CAST_PTR
300                                                                     (union
301                                                                      aml_resource,
302                                                                      aml_buffer));
303                 if (ACPI_FAILURE(status)) {
304                         ACPI_REPORT_ERROR(("Could not convert resource (type %X) to AML, %s\n", resource->type, acpi_format_exception(status)));
305                         return_ACPI_STATUS(status);
306                 }
307
308                 /* Perform final sanity check on the new AML resource descriptor */
309
310                 status =
311                     acpi_rs_validate_resource_length(ACPI_CAST_PTR
312                                                      (union aml_resource,
313                                                       aml_buffer));
314                 if (ACPI_FAILURE(status)) {
315                         return_ACPI_STATUS(status);
316                 }
317
318                 /* Check for end-of-list, normal exit */
319
320                 if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
321                         /* An End Tag indicates the end of the input Resource Template */
322
323                         return_ACPI_STATUS(AE_OK);
324                 }
325
326                 /* Extract the total length of the new descriptor */
327                 /* Set the aml_buffer to point to the next (output) resource descriptor */
328
329                 aml_buffer +=
330                     acpi_rs_get_descriptor_length(ACPI_CAST_PTR
331                                                   (union aml_resource,
332                                                    aml_buffer));
333
334                 /* Point to the next input resource descriptor */
335
336                 resource =
337                     ACPI_PTR_ADD(struct acpi_resource, resource,
338                                  resource->length);
339         }
340 }