rslist.c 7.04 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7
/*******************************************************************************
 *
 * Module Name: rslist - Linked list utilities
 *
 ******************************************************************************/

/*
Bob Moore's avatar
Bob Moore committed
8
 * Copyright (C) 2000 - 2006, R. Byron Moore
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
#include <acpi/acresrc.h>

#define _COMPONENT          ACPI_RESOURCES
Len Brown's avatar
Len Brown committed
48
ACPI_MODULE_NAME("rslist")
Linus Torvalds's avatar
Linus Torvalds committed
49 50 51

/*******************************************************************************
 *
Bob Moore's avatar
Bob Moore committed
52
 * FUNCTION:    acpi_rs_convert_aml_to_resources
Linus Torvalds's avatar
Linus Torvalds committed
53
 *
Bob Moore's avatar
Bob Moore committed
54 55
 * PARAMETERS:  Aml                 - Pointer to the resource byte stream
 *              aml_length          - Length of Aml
Bob Moore's avatar
Bob Moore committed
56 57
 *              output_buffer       - Pointer to the buffer that will
 *                                    contain the output structures
Linus Torvalds's avatar
Linus Torvalds committed
58 59 60 61 62 63 64 65
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Takes the resource byte stream and parses it, creating a
 *              linked list of resources in the caller's output buffer
 *
 ******************************************************************************/
acpi_status
Bob Moore's avatar
Bob Moore committed
66
acpi_rs_convert_aml_to_resources(u8 * aml, u32 aml_length, u8 * output_buffer)
Linus Torvalds's avatar
Linus Torvalds committed
67
{
Bob Moore's avatar
Bob Moore committed
68
	struct acpi_resource *resource = (void *)output_buffer;
Len Brown's avatar
Len Brown committed
69
	acpi_status status;
Bob Moore's avatar
Bob Moore committed
70 71
	u8 resource_index;
	u8 *end_aml;
Len Brown's avatar
Len Brown committed
72

Bob Moore's avatar
Bob Moore committed
73
	ACPI_FUNCTION_TRACE("rs_convert_aml_to_resources");
Len Brown's avatar
Len Brown committed
74

Bob Moore's avatar
Bob Moore committed
75
	end_aml = aml + aml_length;
Robert Moore's avatar
Robert Moore committed
76

Bob Moore's avatar
Bob Moore committed
77
	/* Loop until end-of-buffer or an end_tag is found */
Robert Moore's avatar
Robert Moore committed
78

Bob Moore's avatar
Bob Moore committed
79 80
	while (aml < end_aml) {
		/* Validate the Resource Type and Resource Length */
Bob Moore's avatar
Bob Moore committed
81

Bob Moore's avatar
Bob Moore committed
82
		status = acpi_ut_validate_resource(aml, &resource_index);
Bob Moore's avatar
Bob Moore committed
83 84
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
85 86
		}

Bob Moore's avatar
Bob Moore committed
87
		/* Convert the AML byte stream resource to a local resource struct */
Bob Moore's avatar
Bob Moore committed
88

Bob Moore's avatar
Bob Moore committed
89
		status =
Bob Moore's avatar
Bob Moore committed
90
		    acpi_rs_convert_aml_to_resource(resource,
Bob Moore's avatar
Bob Moore committed
91 92
						    ACPI_CAST_PTR(union
								  aml_resource,
Bob Moore's avatar
Bob Moore committed
93 94 95
								  aml),
						    acpi_gbl_get_resource_dispatch
						    [resource_index]);
Len Brown's avatar
Len Brown committed
96
		if (ACPI_FAILURE(status)) {
Bob Moore's avatar
Bob Moore committed
97 98 99
			ACPI_EXCEPTION((AE_INFO, status,
					"Could not convert AML resource (Type %X)",
					*aml));
Len Brown's avatar
Len Brown committed
100
			return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
101 102
		}

Robert Moore's avatar
Robert Moore committed
103
		/* Normal exit on completion of an end_tag resource descriptor */
104

Bob Moore's avatar
Bob Moore committed
105
		if (acpi_ut_get_resource_type(aml) ==
Bob Moore's avatar
Bob Moore committed
106
		    ACPI_RESOURCE_NAME_END_TAG) {
Robert Moore's avatar
Robert Moore committed
107 108 109
			return_ACPI_STATUS(AE_OK);
		}

Bob Moore's avatar
Bob Moore committed
110
		/* Point to the next input AML resource */
Robert Moore's avatar
Robert Moore committed
111

Bob Moore's avatar
Bob Moore committed
112
		aml += acpi_ut_get_descriptor_length(aml);
Linus Torvalds's avatar
Linus Torvalds committed
113

Robert Moore's avatar
Robert Moore committed
114
		/* Point to the next structure in the output buffer */
115

Bob Moore's avatar
Bob Moore committed
116
		resource =
Bob Moore's avatar
Bob Moore committed
117
		    ACPI_ADD_PTR(struct acpi_resource, resource,
Bob Moore's avatar
Bob Moore committed
118
				 resource->length);
119
	}
Linus Torvalds's avatar
Linus Torvalds committed
120

Bob Moore's avatar
Bob Moore committed
121
	/* Did not find an end_tag resource descriptor */
Linus Torvalds's avatar
Linus Torvalds committed
122

Robert Moore's avatar
Robert Moore committed
123
	return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
Linus Torvalds's avatar
Linus Torvalds committed
124 125 126 127
}

/*******************************************************************************
 *
Bob Moore's avatar
Bob Moore committed
128
 * FUNCTION:    acpi_rs_convert_resources_to_aml
Linus Torvalds's avatar
Linus Torvalds committed
129
 *
Bob Moore's avatar
Bob Moore committed
130 131 132 133 134 135 136
 * PARAMETERS:  Resource            - Pointer to the resource linked list
 *              aml_size_needed     - Calculated size of the byte stream
 *                                    needed from calling acpi_rs_get_aml_length()
 *                                    The size of the output_buffer is
 *                                    guaranteed to be >= aml_size_needed
 *              output_buffer       - Pointer to the buffer that will
 *                                    contain the byte stream
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139 140 141 142 143 144 145
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Takes the resource linked list and parses it, creating a
 *              byte stream of resources in the caller's output buffer
 *
 ******************************************************************************/

acpi_status
Bob Moore's avatar
Bob Moore committed
146 147
acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
				 acpi_size aml_size_needed, u8 * output_buffer)
Linus Torvalds's avatar
Linus Torvalds committed
148
{
Bob Moore's avatar
Bob Moore committed
149 150
	u8 *aml = output_buffer;
	u8 *end_aml = output_buffer + aml_size_needed;
Robert Moore's avatar
Robert Moore committed
151
	acpi_status status;
Linus Torvalds's avatar
Linus Torvalds committed
152

Bob Moore's avatar
Bob Moore committed
153
	ACPI_FUNCTION_TRACE("rs_convert_resources_to_aml");
Linus Torvalds's avatar
Linus Torvalds committed
154

Bob Moore's avatar
Bob Moore committed
155
	/* Walk the resource descriptor list, convert each descriptor */
Linus Torvalds's avatar
Linus Torvalds committed
156

Bob Moore's avatar
Bob Moore committed
157 158
	while (aml < end_aml) {
		/* Validate the (internal) Resource Type */
159

Bob Moore's avatar
Bob Moore committed
160
		if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
Bob Moore's avatar
Bob Moore committed
161 162 163
			ACPI_ERROR((AE_INFO,
				    "Invalid descriptor type (%X) in resource list",
				    resource->type));
Robert Moore's avatar
Robert Moore committed
164
			return_ACPI_STATUS(AE_BAD_DATA);
165
		}
Linus Torvalds's avatar
Linus Torvalds committed
166

Bob Moore's avatar
Bob Moore committed
167
		/* Perform the conversion */
Bob Moore's avatar
Bob Moore committed
168

Bob Moore's avatar
Bob Moore committed
169 170 171
		status = acpi_rs_convert_resource_to_aml(resource,
							 ACPI_CAST_PTR(union
								       aml_resource,
Bob Moore's avatar
Bob Moore committed
172
								       aml),
Bob Moore's avatar
Bob Moore committed
173 174
							 acpi_gbl_set_resource_dispatch
							 [resource->type]);
Bob Moore's avatar
Bob Moore committed
175
		if (ACPI_FAILURE(status)) {
Bob Moore's avatar
Bob Moore committed
176 177 178
			ACPI_EXCEPTION((AE_INFO, status,
					"Could not convert resource (type %X) to AML",
					resource->type));
Bob Moore's avatar
Bob Moore committed
179 180 181 182
			return_ACPI_STATUS(status);
		}

		/* Perform final sanity check on the new AML resource descriptor */
Robert Moore's avatar
Robert Moore committed
183

Bob Moore's avatar
Bob Moore committed
184
		status =
Bob Moore's avatar
Bob Moore committed
185 186
		    acpi_ut_validate_resource(ACPI_CAST_PTR
					      (union aml_resource, aml), NULL);
Len Brown's avatar
Len Brown committed
187 188
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
189 190
		}

Bob Moore's avatar
Bob Moore committed
191
		/* Check for end-of-list, normal exit */
Robert Moore's avatar
Robert Moore committed
192

Bob Moore's avatar
Bob Moore committed
193 194
		if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
			/* An End Tag indicates the end of the input Resource Template */
Robert Moore's avatar
Robert Moore committed
195 196 197 198

			return_ACPI_STATUS(AE_OK);
		}

Bob Moore's avatar
Bob Moore committed
199 200
		/*
		 * Extract the total length of the new descriptor and set the
Bob Moore's avatar
Bob Moore committed
201
		 * Aml to point to the next (output) resource descriptor
Bob Moore's avatar
Bob Moore committed
202
		 */
Bob Moore's avatar
Bob Moore committed
203
		aml += acpi_ut_get_descriptor_length(aml);
Linus Torvalds's avatar
Linus Torvalds committed
204

Bob Moore's avatar
Bob Moore committed
205
		/* Point to the next input resource descriptor */
206

Bob Moore's avatar
Bob Moore committed
207
		resource =
Bob Moore's avatar
Bob Moore committed
208
		    ACPI_ADD_PTR(struct acpi_resource, resource,
Bob Moore's avatar
Bob Moore committed
209
				 resource->length);
Linus Torvalds's avatar
Linus Torvalds committed
210
	}
Bob Moore's avatar
Bob Moore committed
211 212 213 214

	/* Completed buffer, but did not find an end_tag resource descriptor */

	return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
Linus Torvalds's avatar
Linus Torvalds committed
215
}