[ACPI] ACPICA 20050930
[deliverable/linux.git] / drivers / acpi / resources / rslist.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
44#include <acpi/acpi.h>
45#include <acpi/acresrc.h>
46
47#define _COMPONENT ACPI_RESOURCES
4be44fcd 48ACPI_MODULE_NAME("rslist")
1da177e4 49
bda663d3 50/* Local prototypes */
50eca3eb 51static ACPI_GET_RESOURCE_HANDLER acpi_rs_get_resource_handler(u8 resource_type);
bda663d3 52
50eca3eb 53static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml);
bda663d3 54
1da177e4
LT
55/*******************************************************************************
56 *
50eca3eb 57 * FUNCTION: acpi_rs_validate_resource_length
1da177e4 58 *
50eca3eb 59 * PARAMETERS: Aml - Pointer to the AML resource descriptor
1da177e4 60 *
50eca3eb 61 * RETURN: Status - AE_OK if the resource length appears valid
1da177e4 62 *
50eca3eb
BM
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.
1da177e4
LT
67 *
68 ******************************************************************************/
1da177e4 69
50eca3eb 70static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml)
bda663d3 71{
50eca3eb
BM
72 struct acpi_resource_info *resource_info;
73 u16 minimum_aml_resource_length;
74 u16 resource_length;
75
4be44fcd 76 ACPI_FUNCTION_ENTRY();
1da177e4 77
50eca3eb 78 /* Get the size and type info about this resource descriptor */
44f6c012 79
50eca3eb
BM
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 */
bda663d3 94
50eca3eb
BM
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 }
bda663d3 104 } else {
50eca3eb 105 /* Small variable length resource, allowed to be (Min) or (Min-1) */
bda663d3 106
50eca3eb
BM
107 if ((resource_length > minimum_aml_resource_length) ||
108 (resource_length < (minimum_aml_resource_length - 1))) {
109 return (AE_AML_BAD_RESOURCE_LENGTH);
110 }
bda663d3 111 }
50eca3eb
BM
112
113 return (AE_OK);
bda663d3
RM
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 ******************************************************************************/
1da177e4 128
50eca3eb 129static ACPI_GET_RESOURCE_HANDLER acpi_rs_get_resource_handler(u8 resource_type)
bda663d3
RM
130{
131 ACPI_FUNCTION_ENTRY();
44f6c012 132
bda663d3 133 /* Determine if this is a small or large resource */
1da177e4 134
50eca3eb 135 if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
bda663d3 136 /* Large Resource Type -- bits 6:0 contain the name */
1da177e4 137
50eca3eb 138 if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
bda663d3
RM
139 return (NULL);
140 }
44f6c012 141
50eca3eb
BM
142 return (acpi_gbl_lg_get_resource_dispatch[(resource_type &
143 ACPI_RESOURCE_NAME_LARGE_MASK)]);
bda663d3
RM
144 } else {
145 /* Small Resource Type -- bits 6:3 contain the name */
1da177e4 146
50eca3eb
BM
147 return (acpi_gbl_sm_get_resource_dispatch[((resource_type &
148 ACPI_RESOURCE_NAME_SMALL_MASK)
149 >> 3)]);
1da177e4 150 }
1da177e4
LT
151}
152
1da177e4
LT
153/*******************************************************************************
154 *
50eca3eb 155 * FUNCTION: acpi_rs_convert_aml_to_resources
1da177e4 156 *
50eca3eb
BM
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
1da177e4
LT
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
169acpi_status
50eca3eb
BM
170acpi_rs_convert_aml_to_resources(u8 * aml_buffer,
171 u32 aml_buffer_length, u8 * output_buffer)
1da177e4 172{
bda663d3 173 u8 *buffer = output_buffer;
4be44fcd
LB
174 acpi_status status;
175 acpi_size bytes_parsed = 0;
4be44fcd 176 struct acpi_resource *resource;
50eca3eb
BM
177 u16 resource_length;
178 u32 descriptor_length;
179 ACPI_GET_RESOURCE_HANDLER handler;
4be44fcd 180
50eca3eb 181 ACPI_FUNCTION_TRACE("rs_convert_aml_to_resources");
4be44fcd 182
bda663d3
RM
183 /* Loop until end-of-buffer or an end_tag is found */
184
50eca3eb 185 while (bytes_parsed < aml_buffer_length) {
bda663d3
RM
186 /* Get the handler associated with this Descriptor Type */
187
50eca3eb
BM
188 handler = acpi_rs_get_resource_handler(*aml_buffer);
189 if (!handler) {
190 /* No handler indicates invalid resource type */
bda663d3 191
50eca3eb
BM
192 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
193 }
bda663d3 194
50eca3eb
BM
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);
1da177e4
LT
215 }
216
50eca3eb
BM
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));
4be44fcd 222 if (ACPI_FAILURE(status)) {
50eca3eb 223 ACPI_REPORT_ERROR(("Could not convert AML resource (type %X) to resource, %s\n", *aml_buffer, acpi_format_exception(status)));
4be44fcd 224 return_ACPI_STATUS(status);
1da177e4
LT
225 }
226
bda663d3 227 /* Set the aligned length of the new resource descriptor */
44f6c012 228
bda663d3
RM
229 resource = ACPI_CAST_PTR(struct acpi_resource, buffer);
230 resource->length =
231 (u32) ACPI_ALIGN_RESOURCE_SIZE(resource->length);
1da177e4 232
bda663d3 233 /* Normal exit on completion of an end_tag resource descriptor */
44f6c012 234
50eca3eb
BM
235 if (acpi_rs_get_resource_type(*aml_buffer) ==
236 ACPI_RESOURCE_NAME_END_TAG) {
bda663d3
RM
237 return_ACPI_STATUS(AE_OK);
238 }
239
240 /* Update counter and point to the next input resource */
241
50eca3eb
BM
242 bytes_parsed += descriptor_length;
243 aml_buffer += descriptor_length;
1da177e4 244
bda663d3 245 /* Point to the next structure in the output buffer */
44f6c012 246
50eca3eb 247 buffer += resource->length;
44f6c012 248 }
1da177e4 249
bda663d3 250 /* Completed buffer, but did not find an end_tag resource descriptor */
1da177e4 251
bda663d3 252 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
1da177e4
LT
253}
254
1da177e4
LT
255/*******************************************************************************
256 *
50eca3eb 257 * FUNCTION: acpi_rs_convert_resources_to_aml
1da177e4 258 *
50eca3eb
BM
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
1da177e4
LT
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
274acpi_status
50eca3eb
BM
275acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
276 acpi_size aml_size_needed, u8 * output_buffer)
1da177e4 277{
50eca3eb 278 u8 *aml_buffer = output_buffer;
bda663d3 279 acpi_status status;
1da177e4 280
50eca3eb 281 ACPI_FUNCTION_TRACE("rs_convert_resources_to_aml");
1da177e4 282
bda663d3 283 /* Convert each resource descriptor in the list */
1da177e4 284
bda663d3 285 while (1) {
50eca3eb 286 /* Validate Resource Descriptor Type before dispatch */
44f6c012 287
50eca3eb 288 if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
4be44fcd
LB
289 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
290 "Invalid descriptor type (%X) in resource list\n",
bda663d3
RM
291 resource->type));
292 return_ACPI_STATUS(AE_BAD_DATA);
44f6c012 293 }
1da177e4 294
50eca3eb
BM
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 */
bda663d3 309
50eca3eb
BM
310 status =
311 acpi_rs_validate_resource_length(ACPI_CAST_PTR
312 (union aml_resource,
313 aml_buffer));
4be44fcd
LB
314 if (ACPI_FAILURE(status)) {
315 return_ACPI_STATUS(status);
1da177e4
LT
316 }
317
50eca3eb 318 /* Check for end-of-list, normal exit */
bda663d3 319
50eca3eb
BM
320 if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
321 /* An End Tag indicates the end of the input Resource Template */
bda663d3
RM
322
323 return_ACPI_STATUS(AE_OK);
324 }
325
50eca3eb
BM
326 /* Extract the total length of the new descriptor */
327 /* Set the aml_buffer to point to the next (output) resource descriptor */
44f6c012 328
50eca3eb
BM
329 aml_buffer +=
330 acpi_rs_get_descriptor_length(ACPI_CAST_PTR
331 (union aml_resource,
332 aml_buffer));
1da177e4 333
50eca3eb 334 /* Point to the next input resource descriptor */
44f6c012 335
50eca3eb
BM
336 resource =
337 ACPI_PTR_ADD(struct acpi_resource, resource,
338 resource->length);
1da177e4 339 }
1da177e4 340}
This page took 0.066102 seconds and 5 git commands to generate.