[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / acpi / resources / rscalc.c
blob378f58390fc14935fff3f9fa4e519e6d2293e143
1 /*******************************************************************************
3 * Module Name: rscalc - Calculate stream and list lengths
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * All rights reserved.
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.
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.
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.
44 #include <acpi/acpi.h>
45 #include <acpi/acresrc.h>
46 #include <acpi/amlcode.h>
47 #include <acpi/acnamesp.h>
49 #define _COMPONENT ACPI_RESOURCES
50 ACPI_MODULE_NAME("rscalc")
52 /*******************************************************************************
54 * FUNCTION: acpi_rs_get_byte_stream_length
56 * PARAMETERS: linked_list - Pointer to the resource linked list
57 * size_needed - u32 pointer of the size buffer needed
58 * to properly return the parsed data
60 * RETURN: Status
62 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
63 * the size buffer needed to hold the linked list that conveys
64 * the resource data.
66 ******************************************************************************/
67 acpi_status
68 acpi_rs_get_byte_stream_length(struct acpi_resource *linked_list,
69 acpi_size * size_needed)
71 acpi_size byte_stream_size_needed = 0;
72 acpi_size segment_size;
73 u8 done = FALSE;
75 ACPI_FUNCTION_TRACE("rs_get_byte_stream_length");
77 while (!done) {
78 /* Init the variable that will hold the size to add to the total. */
80 segment_size = 0;
82 switch (linked_list->id) {
83 case ACPI_RSTYPE_IRQ:
85 * IRQ Resource
86 * For an IRQ Resource, Byte 3, although optional, will always be
87 * created - it holds IRQ information.
89 segment_size = 4;
90 break;
92 case ACPI_RSTYPE_DMA:
94 * DMA Resource
95 * For this resource the size is static
97 segment_size = 3;
98 break;
100 case ACPI_RSTYPE_START_DPF:
102 * Start Dependent Functions Resource
103 * For a start_dependent_functions Resource, Byte 1, although
104 * optional, will always be created.
106 segment_size = 2;
107 break;
109 case ACPI_RSTYPE_END_DPF:
111 * End Dependent Functions Resource
112 * For this resource the size is static
114 segment_size = 1;
115 break;
117 case ACPI_RSTYPE_IO:
119 * IO Port Resource
120 * For this resource the size is static
122 segment_size = 8;
123 break;
125 case ACPI_RSTYPE_FIXED_IO:
127 * Fixed IO Port Resource
128 * For this resource the size is static
130 segment_size = 4;
131 break;
133 case ACPI_RSTYPE_VENDOR:
135 * Vendor Defined Resource
136 * For a Vendor Specific resource, if the Length is between 1 and 7
137 * it will be created as a Small Resource data type, otherwise it
138 * is a Large Resource data type.
140 if (linked_list->data.vendor_specific.length > 7) {
141 segment_size = 3;
142 } else {
143 segment_size = 1;
145 segment_size +=
146 linked_list->data.vendor_specific.length;
147 break;
149 case ACPI_RSTYPE_END_TAG:
151 * End Tag
152 * For this resource the size is static
154 segment_size = 2;
155 done = TRUE;
156 break;
158 case ACPI_RSTYPE_MEM24:
160 * 24-Bit Memory Resource
161 * For this resource the size is static
163 segment_size = 12;
164 break;
166 case ACPI_RSTYPE_MEM32:
168 * 32-Bit Memory Range Resource
169 * For this resource the size is static
171 segment_size = 20;
172 break;
174 case ACPI_RSTYPE_FIXED_MEM32:
176 * 32-Bit Fixed Memory Resource
177 * For this resource the size is static
179 segment_size = 12;
180 break;
182 case ACPI_RSTYPE_ADDRESS16:
184 * 16-Bit Address Resource
185 * The base size of this byte stream is 16. If a Resource Source
186 * string is not NULL, add 1 for the Index + the length of the null
187 * terminated string Resource Source + 1 for the null.
189 segment_size = 16;
191 if (linked_list->data.address16.resource_source.
192 string_ptr) {
193 segment_size +=
194 linked_list->data.address16.resource_source.
195 string_length;
196 segment_size++;
198 break;
200 case ACPI_RSTYPE_ADDRESS32:
202 * 32-Bit Address Resource
203 * The base size of this byte stream is 26. If a Resource
204 * Source string is not NULL, add 1 for the Index + the
205 * length of the null terminated string Resource Source +
206 * 1 for the null.
208 segment_size = 26;
210 if (linked_list->data.address32.resource_source.
211 string_ptr) {
212 segment_size +=
213 linked_list->data.address32.resource_source.
214 string_length;
215 segment_size++;
217 break;
219 case ACPI_RSTYPE_ADDRESS64:
221 * 64-Bit Address Resource
222 * The base size of this byte stream is 46. If a resource_source
223 * string is not NULL, add 1 for the Index + the length of the null
224 * terminated string Resource Source + 1 for the null.
226 segment_size = 46;
228 if (linked_list->data.address64.resource_source.
229 string_ptr) {
230 segment_size +=
231 linked_list->data.address64.resource_source.
232 string_length;
233 segment_size++;
235 break;
237 case ACPI_RSTYPE_EXT_IRQ:
239 * Extended IRQ Resource
240 * The base size of this byte stream is 9. This is for an Interrupt
241 * table length of 1. For each additional interrupt, add 4.
242 * If a Resource Source string is not NULL, add 1 for the
243 * Index + the length of the null terminated string
244 * Resource Source + 1 for the null.
246 segment_size = 9 + (((acpi_size)
247 linked_list->data.extended_irq.
248 number_of_interrupts - 1) * 4);
250 if (linked_list->data.extended_irq.resource_source.
251 string_ptr) {
252 segment_size +=
253 linked_list->data.extended_irq.
254 resource_source.string_length;
255 segment_size++;
257 break;
259 default:
261 /* If we get here, everything is out of sync, exit with error */
263 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
265 } /* switch (linked_list->Id) */
267 /* Update the total */
269 byte_stream_size_needed += segment_size;
271 /* Point to the next object */
273 linked_list = ACPI_PTR_ADD(struct acpi_resource,
274 linked_list, linked_list->length);
277 /* This is the data the caller needs */
279 *size_needed = byte_stream_size_needed;
280 return_ACPI_STATUS(AE_OK);
283 /*******************************************************************************
285 * FUNCTION: acpi_rs_get_list_length
287 * PARAMETERS: byte_stream_buffer - Pointer to the resource byte stream
288 * byte_stream_buffer_length - Size of byte_stream_buffer
289 * size_needed - u32 pointer of the size buffer
290 * needed to properly return the
291 * parsed data
293 * RETURN: Status
295 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
296 * the size buffer needed to hold the linked list that conveys
297 * the resource data.
299 ******************************************************************************/
301 acpi_status
302 acpi_rs_get_list_length(u8 * byte_stream_buffer,
303 u32 byte_stream_buffer_length, acpi_size * size_needed)
305 u32 buffer_size = 0;
306 u32 bytes_parsed = 0;
307 u8 number_of_interrupts = 0;
308 u8 number_of_channels = 0;
309 u8 resource_type;
310 u32 structure_size;
311 u32 bytes_consumed;
312 u8 *buffer;
313 u8 temp8;
314 u16 temp16;
315 u8 index;
316 u8 additional_bytes;
318 ACPI_FUNCTION_TRACE("rs_get_list_length");
320 while (bytes_parsed < byte_stream_buffer_length) {
321 /* The next byte in the stream is the resource type */
323 resource_type = acpi_rs_get_resource_type(*byte_stream_buffer);
325 switch (resource_type) {
326 case ACPI_RDESC_TYPE_MEMORY_24:
328 * 24-Bit Memory Resource
330 bytes_consumed = 12;
332 structure_size =
333 ACPI_SIZEOF_RESOURCE(struct acpi_resource_mem24);
334 break;
336 case ACPI_RDESC_TYPE_LARGE_VENDOR:
338 * Vendor Defined Resource
340 buffer = byte_stream_buffer;
341 ++buffer;
343 ACPI_MOVE_16_TO_16(&temp16, buffer);
344 bytes_consumed = temp16 + 3;
346 /* Ensure a 32-bit boundary for the structure */
348 temp16 = (u16) ACPI_ROUND_UP_to_32_bITS(temp16);
350 structure_size =
351 ACPI_SIZEOF_RESOURCE(struct acpi_resource_vendor) +
352 (temp16 * sizeof(u8));
353 break;
355 case ACPI_RDESC_TYPE_MEMORY_32:
357 * 32-Bit Memory Range Resource
359 bytes_consumed = 20;
361 structure_size =
362 ACPI_SIZEOF_RESOURCE(struct acpi_resource_mem32);
363 break;
365 case ACPI_RDESC_TYPE_FIXED_MEMORY_32:
367 * 32-Bit Fixed Memory Resource
369 bytes_consumed = 12;
371 structure_size =
372 ACPI_SIZEOF_RESOURCE(struct
373 acpi_resource_fixed_mem32);
374 break;
376 case ACPI_RDESC_TYPE_EXTENDED_ADDRESS_SPACE:
378 * 64-Bit Address Resource
380 buffer = byte_stream_buffer;
382 ++buffer;
383 ACPI_MOVE_16_TO_16(&temp16, buffer);
385 bytes_consumed = temp16 + 3;
386 structure_size =
387 ACPI_SIZEOF_RESOURCE(struct
388 acpi_resource_address64);
389 break;
391 case ACPI_RDESC_TYPE_QWORD_ADDRESS_SPACE:
393 * 64-Bit Address Resource
395 buffer = byte_stream_buffer;
397 ++buffer;
398 ACPI_MOVE_16_TO_16(&temp16, buffer);
400 bytes_consumed = temp16 + 3;
403 * Resource Source Index and Resource Source are optional elements.
404 * Check the length of the Bytestream. If it is greater than 43,
405 * that means that an Index exists and is followed by a null
406 * terminated string. Therefore, set the temp variable to the
407 * length minus the minimum byte stream length plus the byte for
408 * the Index to determine the size of the NULL terminated string.
410 if (43 < temp16) {
411 temp8 = (u8) (temp16 - 44);
412 } else {
413 temp8 = 0;
416 /* Ensure a 64-bit boundary for the structure */
418 temp8 = (u8) ACPI_ROUND_UP_to_64_bITS(temp8);
420 structure_size =
421 ACPI_SIZEOF_RESOURCE(struct acpi_resource_address64)
422 + (temp8 * sizeof(u8));
423 break;
425 case ACPI_RDESC_TYPE_DWORD_ADDRESS_SPACE:
427 * 32-Bit Address Resource
429 buffer = byte_stream_buffer;
431 ++buffer;
432 ACPI_MOVE_16_TO_16(&temp16, buffer);
434 bytes_consumed = temp16 + 3;
437 * Resource Source Index and Resource Source are optional elements.
438 * Check the length of the Bytestream. If it is greater than 23,
439 * that means that an Index exists and is followed by a null
440 * terminated string. Therefore, set the temp variable to the
441 * length minus the minimum byte stream length plus the byte for
442 * the Index to determine the size of the NULL terminated string.
444 if (23 < temp16) {
445 temp8 = (u8) (temp16 - 24);
446 } else {
447 temp8 = 0;
450 /* Ensure a 32-bit boundary for the structure */
452 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS(temp8);
454 structure_size =
455 ACPI_SIZEOF_RESOURCE(struct acpi_resource_address32)
456 + (temp8 * sizeof(u8));
457 break;
459 case ACPI_RDESC_TYPE_WORD_ADDRESS_SPACE:
461 * 16-Bit Address Resource
463 buffer = byte_stream_buffer;
465 ++buffer;
466 ACPI_MOVE_16_TO_16(&temp16, buffer);
468 bytes_consumed = temp16 + 3;
471 * Resource Source Index and Resource Source are optional elements.
472 * Check the length of the Bytestream. If it is greater than 13,
473 * that means that an Index exists and is followed by a null
474 * terminated string. Therefore, set the temp variable to the
475 * length minus the minimum byte stream length plus the byte for
476 * the Index to determine the size of the NULL terminated string.
478 if (13 < temp16) {
479 temp8 = (u8) (temp16 - 14);
480 } else {
481 temp8 = 0;
484 /* Ensure a 32-bit boundary for the structure */
486 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS(temp8);
488 structure_size =
489 ACPI_SIZEOF_RESOURCE(struct acpi_resource_address16)
490 + (temp8 * sizeof(u8));
491 break;
493 case ACPI_RDESC_TYPE_EXTENDED_XRUPT:
495 * Extended IRQ
497 buffer = byte_stream_buffer;
499 ++buffer;
500 ACPI_MOVE_16_TO_16(&temp16, buffer);
502 bytes_consumed = temp16 + 3;
505 * Point past the length field and the Interrupt vector flags to
506 * save off the Interrupt table length to the Temp8 variable.
508 buffer += 3;
509 temp8 = *buffer;
512 * To compensate for multiple interrupt numbers, add 4 bytes for
513 * each additional interrupts greater than 1
515 additional_bytes = (u8) ((temp8 - 1) * 4);
518 * Resource Source Index and Resource Source are optional elements.
519 * Check the length of the Bytestream. If it is greater than 9,
520 * that means that an Index exists and is followed by a null
521 * terminated string. Therefore, set the temp variable to the
522 * length minus the minimum byte stream length plus the byte for
523 * the Index to determine the size of the NULL terminated string.
525 if (9 + additional_bytes < temp16) {
526 temp8 = (u8) (temp16 - (9 + additional_bytes));
527 } else {
528 temp8 = 0;
531 /* Ensure a 32-bit boundary for the structure */
533 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS(temp8);
535 structure_size =
536 ACPI_SIZEOF_RESOURCE(struct acpi_resource_ext_irq) +
537 (additional_bytes * sizeof(u8)) +
538 (temp8 * sizeof(u8));
539 break;
541 case ACPI_RDESC_TYPE_IRQ_FORMAT:
543 * IRQ Resource.
544 * Determine if it there are two or three trailing bytes
546 buffer = byte_stream_buffer;
547 temp8 = *buffer;
549 if (temp8 & 0x01) {
550 bytes_consumed = 4;
551 } else {
552 bytes_consumed = 3;
555 /* Point past the descriptor */
557 ++buffer;
559 /* Look at the number of bits set */
561 ACPI_MOVE_16_TO_16(&temp16, buffer);
563 for (index = 0; index < 16; index++) {
564 if (temp16 & 0x1) {
565 ++number_of_interrupts;
568 temp16 >>= 1;
571 structure_size =
572 ACPI_SIZEOF_RESOURCE(struct acpi_resource_io) +
573 (number_of_interrupts * sizeof(u32));
574 break;
576 case ACPI_RDESC_TYPE_DMA_FORMAT:
578 * DMA Resource
580 buffer = byte_stream_buffer;
581 bytes_consumed = 3;
583 /* Point past the descriptor */
585 ++buffer;
587 /* Look at the number of bits set */
589 temp8 = *buffer;
591 for (index = 0; index < 8; index++) {
592 if (temp8 & 0x1) {
593 ++number_of_channels;
596 temp8 >>= 1;
599 structure_size =
600 ACPI_SIZEOF_RESOURCE(struct acpi_resource_dma) +
601 (number_of_channels * sizeof(u32));
602 break;
604 case ACPI_RDESC_TYPE_START_DEPENDENT:
606 * Start Dependent Functions Resource
607 * Determine if it there are two or three trailing bytes
609 buffer = byte_stream_buffer;
610 temp8 = *buffer;
612 if (temp8 & 0x01) {
613 bytes_consumed = 2;
614 } else {
615 bytes_consumed = 1;
618 structure_size =
619 ACPI_SIZEOF_RESOURCE(struct
620 acpi_resource_start_dpf);
621 break;
623 case ACPI_RDESC_TYPE_END_DEPENDENT:
625 * End Dependent Functions Resource
627 bytes_consumed = 1;
628 structure_size = ACPI_RESOURCE_LENGTH;
629 break;
631 case ACPI_RDESC_TYPE_IO_PORT:
633 * IO Port Resource
635 bytes_consumed = 8;
636 structure_size =
637 ACPI_SIZEOF_RESOURCE(struct acpi_resource_io);
638 break;
640 case ACPI_RDESC_TYPE_FIXED_IO_PORT:
642 * Fixed IO Port Resource
644 bytes_consumed = 4;
645 structure_size =
646 ACPI_SIZEOF_RESOURCE(struct acpi_resource_fixed_io);
647 break;
649 case ACPI_RDESC_TYPE_SMALL_VENDOR:
651 * Vendor Specific Resource
653 buffer = byte_stream_buffer;
655 temp8 = *buffer;
656 temp8 = (u8) (temp8 & 0x7);
657 bytes_consumed = temp8 + 1;
659 /* Ensure a 32-bit boundary for the structure */
661 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS(temp8);
662 structure_size =
663 ACPI_SIZEOF_RESOURCE(struct acpi_resource_vendor) +
664 (temp8 * sizeof(u8));
665 break;
667 case ACPI_RDESC_TYPE_END_TAG:
669 * End Tag
671 bytes_consumed = 2;
672 structure_size = ACPI_RESOURCE_LENGTH;
673 byte_stream_buffer_length = bytes_parsed;
674 break;
676 default:
678 * If we get here, everything is out of sync,
679 * exit with an error
681 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
684 /* Update the return value and counter */
686 buffer_size += (u32) ACPI_ALIGN_RESOURCE_SIZE(structure_size);
687 bytes_parsed += bytes_consumed;
689 /* Set the byte stream to point to the next resource */
691 byte_stream_buffer += bytes_consumed;
694 /* This is the data the caller needs */
696 *size_needed = buffer_size;
697 return_ACPI_STATUS(AE_OK);
700 /*******************************************************************************
702 * FUNCTION: acpi_rs_get_pci_routing_table_length
704 * PARAMETERS: package_object - Pointer to the package object
705 * buffer_size_needed - u32 pointer of the size buffer
706 * needed to properly return the
707 * parsed data
709 * RETURN: Status
711 * DESCRIPTION: Given a package representing a PCI routing table, this
712 * calculates the size of the corresponding linked list of
713 * descriptions.
715 ******************************************************************************/
717 acpi_status
718 acpi_rs_get_pci_routing_table_length(union acpi_operand_object *package_object,
719 acpi_size * buffer_size_needed)
721 u32 number_of_elements;
722 acpi_size temp_size_needed = 0;
723 union acpi_operand_object **top_object_list;
724 u32 index;
725 union acpi_operand_object *package_element;
726 union acpi_operand_object **sub_object_list;
727 u8 name_found;
728 u32 table_index;
730 ACPI_FUNCTION_TRACE("rs_get_pci_routing_table_length");
732 number_of_elements = package_object->package.count;
735 * Calculate the size of the return buffer.
736 * The base size is the number of elements * the sizes of the
737 * structures. Additional space for the strings is added below.
738 * The minus one is to subtract the size of the u8 Source[1]
739 * member because it is added below.
741 * But each PRT_ENTRY structure has a pointer to a string and
742 * the size of that string must be found.
744 top_object_list = package_object->package.elements;
746 for (index = 0; index < number_of_elements; index++) {
747 /* Dereference the sub-package */
749 package_element = *top_object_list;
752 * The sub_object_list will now point to an array of the
753 * four IRQ elements: Address, Pin, Source and source_index
755 sub_object_list = package_element->package.elements;
757 /* Scan the irq_table_elements for the Source Name String */
759 name_found = FALSE;
761 for (table_index = 0; table_index < 4 && !name_found;
762 table_index++) {
763 if ((ACPI_TYPE_STRING ==
764 ACPI_GET_OBJECT_TYPE(*sub_object_list))
766 ((ACPI_TYPE_LOCAL_REFERENCE ==
767 ACPI_GET_OBJECT_TYPE(*sub_object_list))
768 && ((*sub_object_list)->reference.opcode ==
769 AML_INT_NAMEPATH_OP))) {
770 name_found = TRUE;
771 } else {
772 /* Look at the next element */
774 sub_object_list++;
778 temp_size_needed += (sizeof(struct acpi_pci_routing_table) - 4);
780 /* Was a String type found? */
782 if (name_found) {
783 if (ACPI_GET_OBJECT_TYPE(*sub_object_list) ==
784 ACPI_TYPE_STRING) {
786 * The length String.Length field does not include the
787 * terminating NULL, add 1
789 temp_size_needed += ((acpi_size)
790 (*sub_object_list)->string.
791 length + 1);
792 } else {
793 temp_size_needed += acpi_ns_get_pathname_length((*sub_object_list)->reference.node);
795 } else {
797 * If no name was found, then this is a NULL, which is
798 * translated as a u32 zero.
800 temp_size_needed += sizeof(u32);
803 /* Round up the size since each element must be aligned */
805 temp_size_needed = ACPI_ROUND_UP_to_64_bITS(temp_size_needed);
807 /* Point to the next union acpi_operand_object */
809 top_object_list++;
813 * Adding an extra element to the end of the list, essentially a
814 * NULL terminator
816 *buffer_size_needed =
817 temp_size_needed + sizeof(struct acpi_pci_routing_table);
818 return_ACPI_STATUS(AE_OK);