:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / drivers / bus / acpi / resource / rscreate.c
1 /*******************************************************************************
2  *
3  * Module Name: rscreate - Acpi_rs_create_resource_list
4  *                         Acpi_rs_create_pci_routing_table
5  *                         Acpi_rs_create_byte_stream
6  *              $Revision$
7  *
8  ******************************************************************************/
9
10 /*
11  *  Copyright (C) 2000, 2001 R. Byron Moore
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 2 of the License, or
16  *  (at your option) any later version.
17  *
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software
25  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28
29 #include "acpi.h"
30 #include "acresrc.h"
31 #include "amlcode.h"
32 #include "acnamesp.h"
33
34 #define _COMPONENT          ACPI_RESOURCES
35          MODULE_NAME         ("rscreate")
36
37
38 /*******************************************************************************
39  *
40  * FUNCTION:    Acpi_rs_create_resource_list
41  *
42  * PARAMETERS:
43  *              Byte_stream_buffer      - Pointer to the resource byte stream
44  *              Output_buffer           - Pointer to the user's buffer
45  *              Output_buffer_length    - Pointer to the size of Output_buffer
46  *
47  * RETURN:      Status  - AE_OK if okay, else a valid ACPI_STATUS code
48  *              If Output_buffer is not large enough, Output_buffer_length
49  *              indicates how large Output_buffer should be, else it
50  *              indicates how may u8 elements of Output_buffer are valid.
51  *
52  * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
53  *              execution and parses the stream to create a linked list
54  *              of device resources.
55  *
56  ******************************************************************************/
57
58 ACPI_STATUS
59 acpi_rs_create_resource_list (
60         ACPI_OPERAND_OBJECT     *byte_stream_buffer,
61         u8                      *output_buffer,
62         u32                     *output_buffer_length)
63 {
64
65         ACPI_STATUS             status;
66         u8                      *byte_stream_start = NULL;
67         u32                     list_size_needed = 0;
68         u32                     byte_stream_buffer_length = 0;
69
70
71         /*
72          * Params already validated, so we don't re-validate here
73          */
74
75         byte_stream_buffer_length = byte_stream_buffer->buffer.length;
76         byte_stream_start = byte_stream_buffer->buffer.pointer;
77
78         /*
79          * Pass the Byte_stream_buffer into a module that can calculate
80          * the buffer size needed for the linked list
81          */
82         status = acpi_rs_calculate_list_length (byte_stream_start,
83                          byte_stream_buffer_length,
84                          &list_size_needed);
85
86         /*
87          * Exit with the error passed back
88          */
89         if (ACPI_FAILURE (status)) {
90                 return (status);
91         }
92
93         /*
94          * If the linked list will fit into the available buffer
95          * call to fill in the list
96          */
97
98         if (list_size_needed <= *output_buffer_length) {
99                 /*
100                  * Zero out the return buffer before proceeding
101                  */
102                 MEMSET (output_buffer, 0x00, *output_buffer_length);
103
104                 status = acpi_rs_byte_stream_to_list (byte_stream_start,
105                                  byte_stream_buffer_length,
106                                  &output_buffer);
107
108                 /*
109                  * Exit with the error passed back
110                  */
111                 if (ACPI_FAILURE (status)) {
112                         return (status);
113                 }
114
115         }
116
117         else {
118                 *output_buffer_length = list_size_needed;
119                 return (AE_BUFFER_OVERFLOW);
120         }
121
122         *output_buffer_length = list_size_needed;
123         return (AE_OK);
124
125 }
126
127
128 /*******************************************************************************
129  *
130  * FUNCTION:    Acpi_rs_create_pci_routing_table
131  *
132  * PARAMETERS:
133  *              Package_object          - Pointer to an ACPI_OPERAND_OBJECT
134  *                                          package
135  *              Output_buffer           - Pointer to the user's buffer
136  *              Output_buffer_length    - Size of Output_buffer
137  *
138  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
139  *              If the Output_buffer is too small, the error will be
140  *              AE_BUFFER_OVERFLOW and Output_buffer_length will point
141  *              to the size buffer needed.
142  *
143  * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT  package and creates a
144  *              linked list of PCI interrupt descriptions
145  *
146  ******************************************************************************/
147
148 ACPI_STATUS
149 acpi_rs_create_pci_routing_table (
150         ACPI_OPERAND_OBJECT     *package_object,
151         u8                      *output_buffer,
152         u32                     *output_buffer_length)
153 {
154         u8                      *buffer = output_buffer;
155         ACPI_OPERAND_OBJECT     **top_object_list = NULL;
156         ACPI_OPERAND_OBJECT     **sub_object_list = NULL;
157         ACPI_OPERAND_OBJECT     *package_element = NULL;
158         u32                     buffer_size_needed = 0;
159         u32                     number_of_elements = 0;
160         u32                     index = 0;
161         PCI_ROUTING_TABLE       *user_prt = NULL;
162         ACPI_NAMESPACE_NODE     *node;
163         ACPI_STATUS             status;
164
165
166         /*
167          * Params already validated, so we don't re-validate here
168          */
169
170         status = acpi_rs_calculate_pci_routing_table_length(package_object,
171                           &buffer_size_needed);
172
173         /*
174          * If the data will fit into the available buffer
175          * call to fill in the list
176          */
177         if (buffer_size_needed <= *output_buffer_length) {
178                 /*
179                  * Zero out the return buffer before proceeding
180                  */
181                 MEMSET (output_buffer, 0x00, *output_buffer_length);
182
183                 /*
184                  * Loop through the ACPI_INTERNAL_OBJECTS - Each object should
185                  * contain a u32 Address, a u8 Pin, a Name and a u8
186                  * Source_index.
187                  */
188                 top_object_list     = package_object->package.elements;
189                 number_of_elements  = package_object->package.count;
190                 user_prt            = (PCI_ROUTING_TABLE *) buffer;
191
192
193                 buffer = ROUND_PTR_UP_TO_8 (buffer, u8);
194
195                 for (index = 0; index < number_of_elements; index++) {
196                         /*
197                          * Point User_prt past this current structure
198                          *
199                          * NOTE: On the first iteration, User_prt->Length will
200                          * be zero because we cleared the return buffer earlier
201                          */
202                         buffer += user_prt->length;
203                         user_prt = (PCI_ROUTING_TABLE *) buffer;
204
205
206                         /*
207                          * Fill in the Length field with the information we
208                          * have at this point.
209                          * The minus four is to subtract the size of the
210                          * u8 Source[4] member because it is added below.
211                          */
212                         user_prt->length = (sizeof (PCI_ROUTING_TABLE) -4);
213
214                         /*
215                          * Dereference the sub-package
216                          */
217                         package_element = *top_object_list;
218
219                         /*
220                          * The Sub_object_list will now point to an array of
221                          * the four IRQ elements: Address, Pin, Source and
222                          * Source_index
223                          */
224                         sub_object_list = package_element->package.elements;
225
226                         /*
227                          * 1) First subobject:  Dereference the Address
228                          */
229                         if (ACPI_TYPE_INTEGER == (*sub_object_list)->common.type) {
230                                 user_prt->address = (*sub_object_list)->integer.value;
231                         }
232
233                         else {
234                                 return (AE_BAD_DATA);
235                         }
236
237                         /*
238                          * 2) Second subobject: Dereference the Pin
239                          */
240                         sub_object_list++;
241
242                         if (ACPI_TYPE_INTEGER == (*sub_object_list)->common.type) {
243                                 user_prt->pin =
244                                                 (u32) (*sub_object_list)->integer.value;
245                         }
246
247                         else {
248                                 return (AE_BAD_DATA);
249                         }
250
251                         /*
252                          * 3) Third subobject: Dereference the Source Name
253                          */
254                         sub_object_list++;
255
256                         switch ((*sub_object_list)->common.type) {
257                         case INTERNAL_TYPE_REFERENCE:
258                                 if ((*sub_object_list)->reference.opcode != AML_NAMEPATH_OP) {
259                                         return (AE_BAD_DATA);
260                                 }
261
262                                 node = (*sub_object_list)->reference.node;
263
264                                 /* TBD: use *remaining* length of the buffer! */
265
266                                 status = acpi_ns_handle_to_pathname ((ACPI_HANDLE *) node,
267                                                  output_buffer_length, user_prt->source);
268
269                                 user_prt->length += STRLEN (user_prt->source) + 1; /* include null terminator */
270                                 break;
271
272
273                         case ACPI_TYPE_STRING:
274
275                                 STRCPY (user_prt->source,
276                                           (*sub_object_list)->string.pointer);
277
278                                 /*
279                                  * Add to the Length field the length of the string
280                                  */
281                                 user_prt->length += (*sub_object_list)->string.length;
282                                 break;
283
284
285                         case ACPI_TYPE_INTEGER:
286                                 /*
287                                  * If this is a number, then the Source Name
288                                  * is NULL, since the entire buffer was zeroed
289                                  * out, we can leave this alone.
290                                  */
291                                 /*
292                                  * Add to the Length field the length of
293                                  * the u32 NULL
294                                  */
295                                 user_prt->length += sizeof (u32);
296                                 break;
297
298
299                         default:
300                            return (AE_BAD_DATA);
301                            break;
302                         }
303
304                         /* Now align the current length */
305
306                         user_prt->length = ROUND_UP_TO_64_bITS (user_prt->length);
307
308                         /*
309                          * 4) Fourth subobject: Dereference the Source Index
310                          */
311                         sub_object_list++;
312
313                         if (ACPI_TYPE_INTEGER == (*sub_object_list)->common.type) {
314                                 user_prt->source_index =
315                                                 (u32) (*sub_object_list)->integer.value;
316                         }
317
318                         else {
319                                 return (AE_BAD_DATA);
320                         }
321
322                         /*
323                          * Point to the next ACPI_OPERAND_OBJECT
324                          */
325                         top_object_list++;
326                 }
327
328         }
329
330         else {
331                 *output_buffer_length = buffer_size_needed;
332
333                 return (AE_BUFFER_OVERFLOW);
334         }
335
336         /*
337          * Report the amount of buffer used
338          */
339         *output_buffer_length = buffer_size_needed;
340
341         return (AE_OK);
342 }
343
344
345 /*******************************************************************************
346  *
347  * FUNCTION:    Acpi_rs_create_byte_stream
348  *
349  * PARAMETERS:
350  *              Linked_list_buffer      - Pointer to the resource linked list
351  *              Output_buffer           - Pointer to the user's buffer
352  *              Output_buffer_length    - Size of Output_buffer
353  *
354  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
355  *              If the Output_buffer is too small, the error will be
356  *              AE_BUFFER_OVERFLOW and Output_buffer_length will point
357  *              to the size buffer needed.
358  *
359  * DESCRIPTION: Takes the linked list of device resources and
360  *              creates a bytestream to be used as input for the
361  *              _SRS control method.
362  *
363  ******************************************************************************/
364
365 ACPI_STATUS
366 acpi_rs_create_byte_stream (
367         RESOURCE                *linked_list_buffer,
368         u8                      *output_buffer,
369         u32                     *output_buffer_length)
370 {
371         ACPI_STATUS             status;
372         u32                     byte_stream_size_needed = 0;
373
374
375         /*
376          * Params already validated, so we don't re-validate here
377          *
378          * Pass the Linked_list_buffer into a module that can calculate
379          * the buffer size needed for the byte stream.
380          */
381         status = acpi_rs_calculate_byte_stream_length (linked_list_buffer,
382                          &byte_stream_size_needed);
383
384         /*
385          * Exit with the error passed back
386          */
387         if (ACPI_FAILURE (status)) {
388                 return (status);
389         }
390
391         /*
392          * If the linked list will fit into the available buffer
393          * call to fill in the list
394          */
395
396         if (byte_stream_size_needed <= *output_buffer_length) {
397                 /*
398                  * Zero out the return buffer before proceeding
399                  */
400                 MEMSET (output_buffer, 0x00, *output_buffer_length);
401
402                 status = acpi_rs_list_to_byte_stream (linked_list_buffer,
403                                  byte_stream_size_needed,
404                                  &output_buffer);
405
406                 /*
407                  * Exit with the error passed back
408                  */
409                 if (ACPI_FAILURE (status)) {
410                         return (status);
411                 }
412
413         }
414         else {
415                 *output_buffer_length = byte_stream_size_needed;
416                 return (AE_BUFFER_OVERFLOW);
417         }
418
419         return (AE_OK);
420 }
421