:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / drivers / bus / acpi / resource / rsmisc.c
1 /*******************************************************************************
2  *
3  * Module Name: rsmisc - Acpi_rs_end_tag_resource
4  *                       Acpi_rs_end_tag_stream
5  *                       Acpi_rs_vendor_resource
6  *                       Acpi_rs_vendor_stream
7  *                       Acpi_rs_start_dependent_functions_resource
8  *                       Acpi_rs_end_dependent_functions_resource
9  *                       Acpi_rs_start_dependent_functions_stream
10  *                       Acpi_rs_end_dependent_functions_stream
11  *              $Revision$
12  *
13  ******************************************************************************/
14
15 /*
16  *  Copyright (C) 2000, 2001 R. Byron Moore
17  *
18  *  This program is free software; you can redistribute it and/or modify
19  *  it under the terms of the GNU General Public License as published by
20  *  the Free Software Foundation; either version 2 of the License, or
21  *  (at your option) any later version.
22  *
23  *  This program is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27  *
28  *  You should have received a copy of the GNU General Public License
29  *  along with this program; if not, write to the Free Software
30  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  */
32
33
34 #include "acpi.h"
35 #include "acresrc.h"
36
37 #define _COMPONENT          ACPI_RESOURCES
38          MODULE_NAME         ("rsmisc")
39
40
41 /*******************************************************************************
42  *
43  * FUNCTION:    Acpi_rs_end_tag_resource
44  *
45  * PARAMETERS:  Byte_stream_buffer      - Pointer to the resource input byte
46  *                                          stream
47  *              Bytes_consumed          - u32 pointer that is filled with
48  *                                          the number of bytes consumed from
49  *                                          the Byte_stream_buffer
50  *              Output_buffer           - Pointer to the user's return buffer
51  *              Structure_size          - u32 pointer that is filled with
52  *                                          the number of bytes in the filled
53  *                                          in structure
54  *
55  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code
56  *
57  * DESCRIPTION: Take the resource byte stream and fill out the appropriate
58  *                  structure pointed to by the Output_buffer. Return the
59  *                  number of bytes consumed from the byte stream.
60  *
61  ******************************************************************************/
62
63 ACPI_STATUS
64 acpi_rs_end_tag_resource (
65         u8                      *byte_stream_buffer,
66         u32                     *bytes_consumed,
67         u8                      **output_buffer,
68         u32                     *structure_size)
69 {
70         RESOURCE                *output_struct = (RESOURCE *) * output_buffer;
71         u32                     struct_size = RESOURCE_LENGTH;
72
73
74         /*
75          * The number of bytes consumed is static
76          */
77         *bytes_consumed = 2;
78
79         /*
80          *  Fill out the structure
81          */
82         output_struct->id = end_tag;
83
84         /*
85          * Set the Length parameter
86          */
87         output_struct->length = 0;
88
89         /*
90          * Return the final size of the structure
91          */
92         *structure_size = struct_size;
93
94         return (AE_OK);
95 }
96
97
98 /*******************************************************************************
99  *
100  * FUNCTION:    Acpi_rs_end_tag_stream
101  *
102  * PARAMETERS:  Linked_list             - Pointer to the resource linked list
103  *              Output_buffer           - Pointer to the user's return buffer
104  *              Bytes_consumed          - u32 pointer that is filled with
105  *                                          the number of bytes of the
106  *                                          Output_buffer used
107  *
108  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code
109  *
110  * DESCRIPTION: Take the linked list resource structure and fills in the
111  *                  the appropriate bytes in a byte stream
112  *
113  ******************************************************************************/
114
115 ACPI_STATUS
116 acpi_rs_end_tag_stream (
117         RESOURCE                *linked_list,
118         u8                      **output_buffer,
119         u32                     *bytes_consumed)
120 {
121         u8                      *buffer = *output_buffer;
122         u8                      temp8 = 0;
123
124
125         /*
126          * The descriptor field is static
127          */
128         *buffer = 0x79;
129         buffer += 1;
130
131         /*
132          * Set the Checksum - zero means that the resource data is treated as if
133          *  the checksum operation succeeded (ACPI Spec 1.0b Section 6.4.2.8)
134          */
135         temp8 = 0;
136
137         *buffer = temp8;
138         buffer += 1;
139
140         /*
141          * Return the number of bytes consumed in this operation
142          */
143         *bytes_consumed = (u32) ((NATIVE_UINT) buffer -
144                            (NATIVE_UINT) *output_buffer);
145
146         return (AE_OK);
147 }
148
149
150 /*******************************************************************************
151  *
152  * FUNCTION:    Acpi_rs_vendor_resource
153  *
154  * PARAMETERS:  Byte_stream_buffer      - Pointer to the resource input byte
155  *                                          stream
156  *              Bytes_consumed          - u32 pointer that is filled with
157  *                                          the number of bytes consumed from
158  *                                          the Byte_stream_buffer
159  *              Output_buffer           - Pointer to the user's return buffer
160  *              Structure_size          - u32 pointer that is filled with
161  *                                          the number of bytes in the filled
162  *                                          in structure
163  *
164  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code
165  *
166  * DESCRIPTION: Take the resource byte stream and fill out the appropriate
167  *                  structure pointed to by the Output_buffer. Return the
168  *                  number of bytes consumed from the byte stream.
169  *
170  ******************************************************************************/
171
172 ACPI_STATUS
173 acpi_rs_vendor_resource (
174         u8                      *byte_stream_buffer,
175         u32                     *bytes_consumed,
176         u8                      **output_buffer,
177         u32                     *structure_size)
178 {
179         u8                      *buffer = byte_stream_buffer;
180         RESOURCE                *output_struct = (RESOURCE *) * output_buffer;
181         u16                     temp16 = 0;
182         u8                      temp8 = 0;
183         u8                      index;
184         u32                     struct_size = sizeof (VENDOR_RESOURCE) +
185                           RESOURCE_LENGTH_NO_DATA;
186
187
188         /*
189          * Dereference the Descriptor to find if this is a large or small item.
190          */
191         temp8 = *buffer;
192
193         if (temp8 & 0x80) {
194                 /*
195                  * Large Item
196                  */
197                 /* Point to the length field */
198
199                 buffer += 1;
200
201                 /* Dereference */
202
203                 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
204
205                 /* Calculate bytes consumed */
206
207                 *bytes_consumed = temp16 + 3;
208
209                 /* Point to the first vendor byte */
210
211                 buffer += 2;
212         }
213
214         else {
215                 /*
216                  * Small Item
217                  */
218
219                 /* Dereference the size */
220
221                 temp16 = (u8)(*buffer & 0x07);
222
223                 /* Calculate bytes consumed */
224
225                 *bytes_consumed = temp16 + 1;
226
227                 /* Point to the first vendor byte */
228
229                 buffer += 1;
230         }
231
232         output_struct->id = vendor_specific;
233         output_struct->data.vendor_specific.length = temp16;
234
235         for (index = 0; index < temp16; index++) {
236                 output_struct->data.vendor_specific.reserved[index] = *buffer;
237                 buffer += 1;
238         }
239
240         /*
241          * In order for the Struct_size to fall on a 32-bit boundry,
242          *  calculate the length of the vendor string and expand the
243          *  Struct_size to the next 32-bit boundry.
244          */
245         struct_size += ROUND_UP_TO_32_bITS (temp16);
246
247         /*
248          * Set the Length parameter
249          */
250         output_struct->length = struct_size;
251
252         /*
253          * Return the final size of the structure
254          */
255         *structure_size = struct_size;
256
257         return (AE_OK);
258 }
259
260
261 /*******************************************************************************
262  *
263  * FUNCTION:    Acpi_rs_vendor_stream
264  *
265  * PARAMETERS:  Linked_list             - Pointer to the resource linked list
266  *              Output_buffer           - Pointer to the user's return buffer
267  *              Bytes_consumed          - u32 pointer that is filled with
268  *                                          the number of bytes of the
269  *                                          Output_buffer used
270  *
271  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code
272  *
273  * DESCRIPTION: Take the linked list resource structure and fills in the
274  *                  the appropriate bytes in a byte stream
275  *
276  ******************************************************************************/
277
278 ACPI_STATUS
279 acpi_rs_vendor_stream (
280         RESOURCE                *linked_list,
281         u8                      **output_buffer,
282         u32                     *bytes_consumed)
283 {
284         u8                      *buffer = *output_buffer;
285         u16                     temp16 = 0;
286         u8                      temp8 = 0;
287         u8                      index;
288
289
290         /*
291          * Dereference the length to find if this is a large or small item.
292          */
293
294         if(linked_list->data.vendor_specific.length > 7) {
295                 /*
296                  * Large Item
297                  */
298                 /*
299                  * Set the descriptor field and length bytes
300                  */
301                 *buffer = 0x84;
302                 buffer += 1;
303
304                 temp16 = (u16) linked_list->data.vendor_specific.length;
305
306                 MOVE_UNALIGNED16_TO_16 (buffer, &temp16);
307                 buffer += 2;
308         }
309
310         else {
311                 /*
312                  * Small Item
313                  */
314
315                 /*
316                  * Set the descriptor field
317                  */
318                 temp8 = 0x70;
319                 temp8 |= linked_list->data.vendor_specific.length;
320
321                 *buffer = temp8;
322                 buffer += 1;
323         }
324
325         /*
326          * Loop through all of the Vendor Specific fields
327          */
328         for (index = 0; index < linked_list->data.vendor_specific.length; index++) {
329                 temp8 = linked_list->data.vendor_specific.reserved[index];
330
331                 *buffer = temp8;
332                 buffer += 1;
333         }
334
335         /*
336          * Return the number of bytes consumed in this operation
337          */
338         *bytes_consumed = (u32) ((NATIVE_UINT) buffer -
339                            (NATIVE_UINT) *output_buffer);
340
341         return (AE_OK);
342 }
343
344
345 /*******************************************************************************
346  *
347  * FUNCTION:    Acpi_rs_start_dependent_functions_resource
348  *
349  * PARAMETERS:  Byte_stream_buffer      - Pointer to the resource input byte
350  *                                          stream
351  *              Bytes_consumed          - u32 pointer that is filled with
352  *                                          the number of bytes consumed from
353  *                                          the Byte_stream_buffer
354  *              Output_buffer           - Pointer to the user's return buffer
355  *              Structure_size          - u32 pointer that is filled with
356  *                                          the number of bytes in the filled
357  *                                          in structure
358  *
359  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code
360  *
361  * DESCRIPTION: Take the resource byte stream and fill out the appropriate
362  *                  structure pointed to by the Output_buffer. Return the
363  *                  number of bytes consumed from the byte stream.
364  *
365  ******************************************************************************/
366
367 ACPI_STATUS
368 acpi_rs_start_dependent_functions_resource (
369         u8                      *byte_stream_buffer,
370         u32                     *bytes_consumed,
371         u8                      **output_buffer,
372         u32                     *structure_size)
373 {
374         u8                      *buffer = byte_stream_buffer;
375         RESOURCE                *output_struct = (RESOURCE *) * output_buffer;
376         u8                      temp8 = 0;
377         u32                     struct_size =
378                           sizeof(START_DEPENDENT_FUNCTIONS_RESOURCE) +
379                           RESOURCE_LENGTH_NO_DATA;
380
381
382         /*
383          * The number of bytes consumed are contained in the descriptor (Bits:0-1)
384          */
385         temp8 = *buffer;
386
387         *bytes_consumed = (temp8 & 0x01) + 1;
388
389         output_struct->id = start_dependent_functions;
390
391         /*
392          * Point to Byte 1 if it is used
393          */
394         if (2 == *bytes_consumed) {
395                 buffer += 1;
396                 temp8 = *buffer;
397
398                 /*
399                  * Check Compatibility priority
400                  */
401                 output_struct->data.start_dependent_functions.compatibility_priority =
402                                 temp8 & 0x03;
403
404                 if (3 == output_struct->data.start_dependent_functions.compatibility_priority) {
405                         return (AE_AML_ERROR);
406                 }
407
408                 /*
409                  * Check Performance/Robustness preference
410                  */
411                 output_struct->data.start_dependent_functions.performance_robustness =
412                                 (temp8 >> 2) & 0x03;
413
414                 if (3 == output_struct->data.start_dependent_functions.performance_robustness) {
415                         return (AE_AML_ERROR);
416                 }
417         }
418
419         else {
420                 output_struct->data.start_dependent_functions.compatibility_priority =
421                                 ACCEPTABLE_CONFIGURATION;
422
423                 output_struct->data.start_dependent_functions.performance_robustness =
424                                 ACCEPTABLE_CONFIGURATION;
425         }
426
427         /*
428          * Set the Length parameter
429          */
430         output_struct->length = struct_size;
431
432         /*
433          * Return the final size of the structure
434          */
435         *structure_size = struct_size;
436
437         return (AE_OK);
438 }
439
440
441 /*******************************************************************************
442  *
443  * FUNCTION:    Acpi_rs_end_dependent_functions_resource
444  *
445  * PARAMETERS:  Byte_stream_buffer      - Pointer to the resource input byte
446  *                                          stream
447  *              Bytes_consumed          - u32 pointer that is filled with
448  *                                          the number of bytes consumed from
449  *                                          the Byte_stream_buffer
450  *              Output_buffer           - Pointer to the user's return buffer
451  *              Structure_size          - u32 pointer that is filled with
452  *                                          the number of bytes in the filled
453  *                                          in structure
454  *
455  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code
456  *
457  * DESCRIPTION: Take the resource byte stream and fill out the appropriate
458  *                  structure pointed to by the Output_buffer. Return the
459  *                  number of bytes consumed from the byte stream.
460  *
461  ******************************************************************************/
462
463 ACPI_STATUS
464 acpi_rs_end_dependent_functions_resource (
465         u8                      *byte_stream_buffer,
466         u32                     *bytes_consumed,
467         u8                      **output_buffer,
468         u32                     *structure_size)
469 {
470         RESOURCE                *output_struct = (RESOURCE *) * output_buffer;
471         u32                     struct_size = RESOURCE_LENGTH;
472
473
474         /*
475          * The number of bytes consumed is static
476          */
477         *bytes_consumed = 1;
478
479         /*
480          *  Fill out the structure
481          */
482         output_struct->id = end_dependent_functions;
483
484         /*
485          * Set the Length parameter
486          */
487         output_struct->length = struct_size;
488
489         /*
490          * Return the final size of the structure
491          */
492         *structure_size = struct_size;
493
494         return (AE_OK);
495 }
496
497
498 /*******************************************************************************
499  *
500  * FUNCTION:    Acpi_rs_start_dependent_functions_stream
501  *
502  * PARAMETERS:  Linked_list             - Pointer to the resource linked list
503  *              Output_buffer           - Pointer to the user's return buffer
504  *              Bytes_consumed          - u32 pointer that is filled with
505  *                                          the number of bytes of the
506  *                                          Output_buffer used
507  *
508  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code
509  *
510  * DESCRIPTION: Take the linked list resource structure and fills in the
511  *                  the appropriate bytes in a byte stream
512  *
513  ******************************************************************************/
514
515 ACPI_STATUS
516 acpi_rs_start_dependent_functions_stream (
517         RESOURCE                *linked_list,
518         u8                      **output_buffer,
519         u32                     *bytes_consumed)
520 {
521         u8                      *buffer = *output_buffer;
522         u8                      temp8 = 0;
523
524
525         /*
526          * The descriptor field is set based upon whether a byte is needed
527          *  to contain Priority data.
528          */
529         if (ACCEPTABLE_CONFIGURATION ==
530                         linked_list->data.start_dependent_functions.compatibility_priority &&
531                 ACCEPTABLE_CONFIGURATION ==
532                         linked_list->data.start_dependent_functions.performance_robustness) {
533                 *buffer = 0x30;
534         }
535         else {
536                 *buffer = 0x31;
537                 buffer += 1;
538
539                 /*
540                  * Set the Priority Byte Definition
541                  */
542                 temp8 = 0;
543                 temp8 = (u8)
544                         ((linked_list->data.start_dependent_functions.performance_robustness &
545                           0x03) << 2);
546                 temp8 |=
547                         (linked_list->data.start_dependent_functions.compatibility_priority &
548                          0x03);
549
550                 *buffer = temp8;
551         }
552
553         buffer += 1;
554
555         /*
556          * Return the number of bytes consumed in this operation
557          */
558         *bytes_consumed = (u32) ((NATIVE_UINT) buffer -
559                            (NATIVE_UINT) *output_buffer);
560
561         return (AE_OK);
562 }
563
564
565 /*******************************************************************************
566  *
567  * FUNCTION:    Acpi_rs_end_dependent_functions_stream
568  *
569  * PARAMETERS:  Linked_list             - Pointer to the resource linked list
570  *              Output_buffer           - Pointer to the user's return buffer
571  *              Bytes_consumed          - u32 pointer that is filled with
572  *                                          the number of bytes of the
573  *                                          Output_buffer used
574  *
575  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code
576  *
577  * DESCRIPTION: Take the linked list resource structure and fills in the
578  *                  the appropriate bytes in a byte stream
579  *
580  ******************************************************************************/
581
582 ACPI_STATUS
583 acpi_rs_end_dependent_functions_stream (
584         RESOURCE                *linked_list,
585         u8                      **output_buffer,
586         u32                     *bytes_consumed
587         )
588 {
589         u8                      *buffer = *output_buffer;
590
591
592         /*
593          * The descriptor field is static
594          */
595         *buffer = 0x38;
596         buffer += 1;
597
598         /*
599          * Return the number of bytes consumed in this operation
600          */
601         *bytes_consumed = (u32) ((NATIVE_UINT) buffer -
602                            (NATIVE_UINT) *output_buffer);
603
604         return (AE_OK);
605 }
606