Merge master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / drivers / acpi / namespace / nsutils.c
1 /******************************************************************************
2  *
3  * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4  *                        parents and siblings and Scope manipulation
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2005, R. Byron Moore
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45
46 #include <acpi/acpi.h>
47 #include <acpi/acnamesp.h>
48 #include <acpi/amlcode.h>
49 #include <acpi/actables.h>
50
51 #define _COMPONENT          ACPI_NAMESPACE
52          ACPI_MODULE_NAME    ("nsutils")
53
54 /* Local prototypes */
55
56 static u8
57 acpi_ns_valid_path_separator (
58         char                            sep);
59
60 #ifdef ACPI_OBSOLETE_FUNCTIONS
61 acpi_name
62 acpi_ns_find_parent_name (
63         struct acpi_namespace_node      *node_to_search);
64 #endif
65
66
67 /*******************************************************************************
68  *
69  * FUNCTION:    acpi_ns_report_error
70  *
71  * PARAMETERS:  module_name         - Caller's module name (for error output)
72  *              line_number         - Caller's line number (for error output)
73  *              component_id        - Caller's component ID (for error output)
74  *              internal_name       - Name or path of the namespace node
75  *              lookup_status       - Exception code from NS lookup
76  *
77  * RETURN:      None
78  *
79  * DESCRIPTION: Print warning message with full pathname
80  *
81  ******************************************************************************/
82
83 void
84 acpi_ns_report_error (
85         char                            *module_name,
86         u32                             line_number,
87         u32                             component_id,
88         char                            *internal_name,
89         acpi_status                     lookup_status)
90 {
91         acpi_status                     status;
92         char                            *name = NULL;
93
94
95         acpi_os_printf ("%8s-%04d: *** Error: Looking up ",
96                 module_name, line_number);
97
98         if (lookup_status == AE_BAD_CHARACTER) {
99                 /* There is a non-ascii character in the name */
100
101                 acpi_os_printf ("[0x%4.4X] (NON-ASCII)\n",
102                         *(ACPI_CAST_PTR (u32, internal_name)));
103         }
104         else {
105                 /* Convert path to external format */
106
107                 status = acpi_ns_externalize_name (ACPI_UINT32_MAX,
108                                  internal_name, NULL, &name);
109
110                 /* Print target name */
111
112                 if (ACPI_SUCCESS (status)) {
113                         acpi_os_printf ("[%s]", name);
114                 }
115                 else {
116                         acpi_os_printf ("[COULD NOT EXTERNALIZE NAME]");
117                 }
118
119                 if (name) {
120                         ACPI_MEM_FREE (name);
121                 }
122         }
123
124         acpi_os_printf (" in namespace, %s\n",
125                 acpi_format_exception (lookup_status));
126 }
127
128
129 /*******************************************************************************
130  *
131  * FUNCTION:    acpi_ns_report_method_error
132  *
133  * PARAMETERS:  module_name         - Caller's module name (for error output)
134  *              line_number         - Caller's line number (for error output)
135  *              component_id        - Caller's component ID (for error output)
136  *              Message             - Error message to use on failure
137  *              prefix_node         - Prefix relative to the path
138  *              Path                - Path to the node
139  *              method_status       - Execution status
140  *
141  * RETURN:      None
142  *
143  * DESCRIPTION: Print warning message with full pathname
144  *
145  ******************************************************************************/
146
147 void
148 acpi_ns_report_method_error (
149         char                            *module_name,
150         u32                             line_number,
151         u32                             component_id,
152         char                            *message,
153         struct acpi_namespace_node      *prefix_node,
154         char                            *path,
155         acpi_status                     method_status)
156 {
157         acpi_status                     status;
158         struct acpi_namespace_node      *node = prefix_node;
159
160
161         if (path) {
162                 status = acpi_ns_get_node_by_path (path, prefix_node,
163                                  ACPI_NS_NO_UPSEARCH, &node);
164                 if (ACPI_FAILURE (status)) {
165                         acpi_os_printf ("report_method_error: Could not get node\n");
166                         return;
167                 }
168         }
169
170         acpi_os_printf ("%8s-%04d: *** Error: ", module_name, line_number);
171         acpi_ns_print_node_pathname (node, message);
172         acpi_os_printf (", %s\n", acpi_format_exception (method_status));
173 }
174
175
176 /*******************************************************************************
177  *
178  * FUNCTION:    acpi_ns_print_node_pathname
179  *
180  * PARAMETERS:  Node            - Object
181  *              Message         - Prefix message
182  *
183  * DESCRIPTION: Print an object's full namespace pathname
184  *              Manages allocation/freeing of a pathname buffer
185  *
186  ******************************************************************************/
187
188 void
189 acpi_ns_print_node_pathname (
190         struct acpi_namespace_node      *node,
191         char                            *message)
192 {
193         struct acpi_buffer              buffer;
194         acpi_status                     status;
195
196
197         if (!node) {
198                 acpi_os_printf ("[NULL NAME]");
199                 return;
200         }
201
202         /* Convert handle to full pathname and print it (with supplied message) */
203
204         buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
205
206         status = acpi_ns_handle_to_pathname (node, &buffer);
207         if (ACPI_SUCCESS (status)) {
208                 if (message) {
209                         acpi_os_printf ("%s ", message);
210                 }
211
212                 acpi_os_printf ("[%s] (Node %p)", (char *) buffer.pointer, node);
213                 ACPI_MEM_FREE (buffer.pointer);
214         }
215 }
216
217
218 /*******************************************************************************
219  *
220  * FUNCTION:    acpi_ns_valid_root_prefix
221  *
222  * PARAMETERS:  Prefix          - Character to be checked
223  *
224  * RETURN:      TRUE if a valid prefix
225  *
226  * DESCRIPTION: Check if a character is a valid ACPI Root prefix
227  *
228  ******************************************************************************/
229
230 u8
231 acpi_ns_valid_root_prefix (
232         char                            prefix)
233 {
234
235         return ((u8) (prefix == '\\'));
236 }
237
238
239 /*******************************************************************************
240  *
241  * FUNCTION:    acpi_ns_valid_path_separator
242  *
243  * PARAMETERS:  Sep         - Character to be checked
244  *
245  * RETURN:      TRUE if a valid path separator
246  *
247  * DESCRIPTION: Check if a character is a valid ACPI path separator
248  *
249  ******************************************************************************/
250
251 static u8
252 acpi_ns_valid_path_separator (
253         char                            sep)
254 {
255
256         return ((u8) (sep == '.'));
257 }
258
259
260 /*******************************************************************************
261  *
262  * FUNCTION:    acpi_ns_get_type
263  *
264  * PARAMETERS:  Node        - Parent Node to be examined
265  *
266  * RETURN:      Type field from Node whose handle is passed
267  *
268  * DESCRIPTION: Return the type of a Namespace node
269  *
270  ******************************************************************************/
271
272 acpi_object_type
273 acpi_ns_get_type (
274         struct acpi_namespace_node      *node)
275 {
276         ACPI_FUNCTION_TRACE ("ns_get_type");
277
278
279         if (!node) {
280                 ACPI_REPORT_WARNING (("ns_get_type: Null Node input pointer\n"));
281                 return_VALUE (ACPI_TYPE_ANY);
282         }
283
284         return_VALUE ((acpi_object_type) node->type);
285 }
286
287
288 /*******************************************************************************
289  *
290  * FUNCTION:    acpi_ns_local
291  *
292  * PARAMETERS:  Type        - A namespace object type
293  *
294  * RETURN:      LOCAL if names must be found locally in objects of the
295  *              passed type, 0 if enclosing scopes should be searched
296  *
297  * DESCRIPTION: Returns scope rule for the given object type.
298  *
299  ******************************************************************************/
300
301 u32
302 acpi_ns_local (
303         acpi_object_type                type)
304 {
305         ACPI_FUNCTION_TRACE ("ns_local");
306
307
308         if (!acpi_ut_valid_object_type (type)) {
309                 /* Type code out of range  */
310
311                 ACPI_REPORT_WARNING (("ns_local: Invalid Object Type\n"));
312                 return_VALUE (ACPI_NS_NORMAL);
313         }
314
315         return_VALUE ((u32) acpi_gbl_ns_properties[type] & ACPI_NS_LOCAL);
316 }
317
318
319 /*******************************************************************************
320  *
321  * FUNCTION:    acpi_ns_get_internal_name_length
322  *
323  * PARAMETERS:  Info            - Info struct initialized with the
324  *                                external name pointer.
325  *
326  * RETURN:      None
327  *
328  * DESCRIPTION: Calculate the length of the internal (AML) namestring
329  *              corresponding to the external (ASL) namestring.
330  *
331  ******************************************************************************/
332
333 void
334 acpi_ns_get_internal_name_length (
335         struct acpi_namestring_info     *info)
336 {
337         char                            *next_external_char;
338         u32                             i;
339
340
341         ACPI_FUNCTION_ENTRY ();
342
343
344         next_external_char = info->external_name;
345         info->num_carats = 0;
346         info->num_segments = 0;
347         info->fully_qualified = FALSE;
348
349         /*
350          * For the internal name, the required length is 4 bytes per segment, plus
351          * 1 each for root_prefix, multi_name_prefix_op, segment count, trailing null
352          * (which is not really needed, but no there's harm in putting it there)
353          *
354          * strlen() + 1 covers the first name_seg, which has no path separator
355          */
356         if (acpi_ns_valid_root_prefix (next_external_char[0])) {
357                 info->fully_qualified = TRUE;
358                 next_external_char++;
359         }
360         else {
361                 /*
362                  * Handle Carat prefixes
363                  */
364                 while (*next_external_char == '^') {
365                         info->num_carats++;
366                         next_external_char++;
367                 }
368         }
369
370         /*
371          * Determine the number of ACPI name "segments" by counting the number of
372          * path separators within the string. Start with one segment since the
373          * segment count is [(# separators) + 1], and zero separators is ok.
374          */
375         if (*next_external_char) {
376                 info->num_segments = 1;
377                 for (i = 0; next_external_char[i]; i++) {
378                         if (acpi_ns_valid_path_separator (next_external_char[i])) {
379                                 info->num_segments++;
380                         }
381                 }
382         }
383
384         info->length = (ACPI_NAME_SIZE * info->num_segments) +
385                           4 + info->num_carats;
386
387         info->next_external_char = next_external_char;
388 }
389
390
391 /*******************************************************************************
392  *
393  * FUNCTION:    acpi_ns_build_internal_name
394  *
395  * PARAMETERS:  Info            - Info struct fully initialized
396  *
397  * RETURN:      Status
398  *
399  * DESCRIPTION: Construct the internal (AML) namestring
400  *              corresponding to the external (ASL) namestring.
401  *
402  ******************************************************************************/
403
404 acpi_status
405 acpi_ns_build_internal_name (
406         struct acpi_namestring_info     *info)
407 {
408         u32                             num_segments = info->num_segments;
409         char                            *internal_name = info->internal_name;
410         char                            *external_name = info->next_external_char;
411         char                            *result = NULL;
412         acpi_native_uint                i;
413
414
415         ACPI_FUNCTION_TRACE ("ns_build_internal_name");
416
417
418         /* Setup the correct prefixes, counts, and pointers */
419
420         if (info->fully_qualified) {
421                 internal_name[0] = '\\';
422
423                 if (num_segments <= 1) {
424                         result = &internal_name[1];
425                 }
426                 else if (num_segments == 2) {
427                         internal_name[1] = AML_DUAL_NAME_PREFIX;
428                         result = &internal_name[2];
429                 }
430                 else {
431                         internal_name[1] = AML_MULTI_NAME_PREFIX_OP;
432                         internal_name[2] = (char) num_segments;
433                         result = &internal_name[3];
434                 }
435         }
436         else {
437                 /*
438                  * Not fully qualified.
439                  * Handle Carats first, then append the name segments
440                  */
441                 i = 0;
442                 if (info->num_carats) {
443                         for (i = 0; i < info->num_carats; i++) {
444                                 internal_name[i] = '^';
445                         }
446                 }
447
448                 if (num_segments <= 1) {
449                         result = &internal_name[i];
450                 }
451                 else if (num_segments == 2) {
452                         internal_name[i] = AML_DUAL_NAME_PREFIX;
453                         result = &internal_name[(acpi_native_uint) (i+1)];
454                 }
455                 else {
456                         internal_name[i] = AML_MULTI_NAME_PREFIX_OP;
457                         internal_name[(acpi_native_uint) (i+1)] = (char) num_segments;
458                         result = &internal_name[(acpi_native_uint) (i+2)];
459                 }
460         }
461
462         /* Build the name (minus path separators) */
463
464         for (; num_segments; num_segments--) {
465                 for (i = 0; i < ACPI_NAME_SIZE; i++) {
466                         if (acpi_ns_valid_path_separator (*external_name) ||
467                            (*external_name == 0)) {
468                                 /* Pad the segment with underscore(s) if segment is short */
469
470                                 result[i] = '_';
471                         }
472                         else {
473                                 /* Convert the character to uppercase and save it */
474
475                                 result[i] = (char) ACPI_TOUPPER ((int) *external_name);
476                                 external_name++;
477                         }
478                 }
479
480                 /* Now we must have a path separator, or the pathname is bad */
481
482                 if (!acpi_ns_valid_path_separator (*external_name) &&
483                         (*external_name != 0)) {
484                         return_ACPI_STATUS (AE_BAD_PARAMETER);
485                 }
486
487                 /* Move on the next segment */
488
489                 external_name++;
490                 result += ACPI_NAME_SIZE;
491         }
492
493         /* Terminate the string */
494
495         *result = 0;
496
497         if (info->fully_qualified) {
498                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (abs) \"\\%s\"\n",
499                         internal_name, internal_name));
500         }
501         else {
502                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (rel) \"%s\"\n",
503                         internal_name, internal_name));
504         }
505
506         return_ACPI_STATUS (AE_OK);
507 }
508
509
510 /*******************************************************************************
511  *
512  * FUNCTION:    acpi_ns_internalize_name
513  *
514  * PARAMETERS:  *external_name          - External representation of name
515  *              **Converted Name        - Where to return the resulting
516  *                                        internal represention of the name
517  *
518  * RETURN:      Status
519  *
520  * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
521  *              to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
522  *
523  *******************************************************************************/
524
525 acpi_status
526 acpi_ns_internalize_name (
527         char                            *external_name,
528         char                            **converted_name)
529 {
530         char                            *internal_name;
531         struct acpi_namestring_info     info;
532         acpi_status                     status;
533
534
535         ACPI_FUNCTION_TRACE ("ns_internalize_name");
536
537
538         if ((!external_name)     ||
539                 (*external_name == 0) ||
540                 (!converted_name)) {
541                 return_ACPI_STATUS (AE_BAD_PARAMETER);
542         }
543
544         /* Get the length of the new internal name */
545
546         info.external_name = external_name;
547         acpi_ns_get_internal_name_length (&info);
548
549         /* We need a segment to store the internal  name */
550
551         internal_name = ACPI_MEM_CALLOCATE (info.length);
552         if (!internal_name) {
553                 return_ACPI_STATUS (AE_NO_MEMORY);
554         }
555
556         /* Build the name */
557
558         info.internal_name = internal_name;
559         status = acpi_ns_build_internal_name (&info);
560         if (ACPI_FAILURE (status)) {
561                 ACPI_MEM_FREE (internal_name);
562                 return_ACPI_STATUS (status);
563         }
564
565         *converted_name = internal_name;
566         return_ACPI_STATUS (AE_OK);
567 }
568
569
570 /*******************************************************************************
571  *
572  * FUNCTION:    acpi_ns_externalize_name
573  *
574  * PARAMETERS:  internal_name_length - Lenth of the internal name below
575  *              internal_name       - Internal representation of name
576  *              converted_name_length - Where the length is returned
577  *              converted_name      - Where the resulting external name
578  *                                    is returned
579  *
580  * RETURN:      Status
581  *
582  * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
583  *              to its external (printable) form (e.g. "\_PR_.CPU0")
584  *
585  ******************************************************************************/
586
587 acpi_status
588 acpi_ns_externalize_name (
589         u32                             internal_name_length,
590         char                            *internal_name,
591         u32                             *converted_name_length,
592         char                            **converted_name)
593 {
594         acpi_native_uint                names_index = 0;
595         acpi_native_uint                num_segments = 0;
596         acpi_native_uint                required_length;
597         acpi_native_uint                prefix_length = 0;
598         acpi_native_uint                i = 0;
599         acpi_native_uint                j = 0;
600
601
602         ACPI_FUNCTION_TRACE ("ns_externalize_name");
603
604
605         if (!internal_name_length   ||
606                 !internal_name          ||
607                 !converted_name) {
608                 return_ACPI_STATUS (AE_BAD_PARAMETER);
609         }
610
611         /*
612          * Check for a prefix (one '\' | one or more '^').
613          */
614         switch (internal_name[0]) {
615         case '\\':
616                 prefix_length = 1;
617                 break;
618
619         case '^':
620                 for (i = 0; i < internal_name_length; i++) {
621                         if (internal_name[i] == '^') {
622                                 prefix_length = i + 1;
623                         }
624                         else {
625                                 break;
626                         }
627                 }
628
629                 if (i == internal_name_length) {
630                         prefix_length = i;
631                 }
632
633                 break;
634
635         default:
636                 break;
637         }
638
639         /*
640          * Check for object names.  Note that there could be 0-255 of these
641          * 4-byte elements.
642          */
643         if (prefix_length < internal_name_length) {
644                 switch (internal_name[prefix_length]) {
645                 case AML_MULTI_NAME_PREFIX_OP:
646
647                         /* <count> 4-byte names */
648
649                         names_index = prefix_length + 2;
650                         num_segments = (acpi_native_uint) (u8)
651                                            internal_name[(acpi_native_uint) (prefix_length + 1)];
652                         break;
653
654                 case AML_DUAL_NAME_PREFIX:
655
656                         /* Two 4-byte names */
657
658                         names_index = prefix_length + 1;
659                         num_segments = 2;
660                         break;
661
662                 case 0:
663
664                         /* null_name */
665
666                         names_index = 0;
667                         num_segments = 0;
668                         break;
669
670                 default:
671
672                         /* one 4-byte name */
673
674                         names_index = prefix_length;
675                         num_segments = 1;
676                         break;
677                 }
678         }
679
680         /*
681          * Calculate the length of converted_name, which equals the length
682          * of the prefix, length of all object names, length of any required
683          * punctuation ('.') between object names, plus the NULL terminator.
684          */
685         required_length = prefix_length + (4 * num_segments) +
686                            ((num_segments > 0) ? (num_segments - 1) : 0) + 1;
687
688         /*
689          * Check to see if we're still in bounds.  If not, there's a problem
690          * with internal_name (invalid format).
691          */
692         if (required_length > internal_name_length) {
693                 ACPI_REPORT_ERROR (("ns_externalize_name: Invalid internal name\n"));
694                 return_ACPI_STATUS (AE_BAD_PATHNAME);
695         }
696
697         /*
698          * Build converted_name
699          */
700         *converted_name = ACPI_MEM_CALLOCATE (required_length);
701         if (!(*converted_name)) {
702                 return_ACPI_STATUS (AE_NO_MEMORY);
703         }
704
705         j = 0;
706
707         for (i = 0; i < prefix_length; i++) {
708                 (*converted_name)[j++] = internal_name[i];
709         }
710
711         if (num_segments > 0) {
712                 for (i = 0; i < num_segments; i++) {
713                         if (i > 0) {
714                                 (*converted_name)[j++] = '.';
715                         }
716
717                         (*converted_name)[j++] = internal_name[names_index++];
718                         (*converted_name)[j++] = internal_name[names_index++];
719                         (*converted_name)[j++] = internal_name[names_index++];
720                         (*converted_name)[j++] = internal_name[names_index++];
721                 }
722         }
723
724         if (converted_name_length) {
725                 *converted_name_length = (u32) required_length;
726         }
727
728         return_ACPI_STATUS (AE_OK);
729 }
730
731
732 /*******************************************************************************
733  *
734  * FUNCTION:    acpi_ns_map_handle_to_node
735  *
736  * PARAMETERS:  Handle          - Handle to be converted to an Node
737  *
738  * RETURN:      A Name table entry pointer
739  *
740  * DESCRIPTION: Convert a namespace handle to a real Node
741  *
742  * Note: Real integer handles would allow for more verification
743  *       and keep all pointers within this subsystem - however this introduces
744  *       more (and perhaps unnecessary) overhead.
745  *
746  ******************************************************************************/
747
748 struct acpi_namespace_node *
749 acpi_ns_map_handle_to_node (
750         acpi_handle                     handle)
751 {
752
753         ACPI_FUNCTION_ENTRY ();
754
755
756         /*
757          * Simple implementation.
758          */
759         if (!handle) {
760                 return (NULL);
761         }
762
763         if (handle == ACPI_ROOT_OBJECT) {
764                 return (acpi_gbl_root_node);
765         }
766
767         /* We can at least attempt to verify the handle */
768
769         if (ACPI_GET_DESCRIPTOR_TYPE (handle) != ACPI_DESC_TYPE_NAMED) {
770                 return (NULL);
771         }
772
773         return ((struct acpi_namespace_node *) handle);
774 }
775
776
777 /*******************************************************************************
778  *
779  * FUNCTION:    acpi_ns_convert_entry_to_handle
780  *
781  * PARAMETERS:  Node          - Node to be converted to a Handle
782  *
783  * RETURN:      A user handle
784  *
785  * DESCRIPTION: Convert a real Node to a namespace handle
786  *
787  ******************************************************************************/
788
789 acpi_handle
790 acpi_ns_convert_entry_to_handle (
791         struct acpi_namespace_node          *node)
792 {
793
794
795         /*
796          * Simple implementation for now;
797          */
798         return ((acpi_handle) node);
799
800
801 /* Example future implementation ---------------------
802
803         if (!Node)
804         {
805                 return (NULL);
806         }
807
808         if (Node == acpi_gbl_root_node)
809         {
810                 return (ACPI_ROOT_OBJECT);
811         }
812
813
814         return ((acpi_handle) Node);
815 ------------------------------------------------------*/
816 }
817
818
819 /*******************************************************************************
820  *
821  * FUNCTION:    acpi_ns_terminate
822  *
823  * PARAMETERS:  none
824  *
825  * RETURN:      none
826  *
827  * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
828  *
829  ******************************************************************************/
830
831 void
832 acpi_ns_terminate (
833         void)
834 {
835         union acpi_operand_object       *obj_desc;
836
837
838         ACPI_FUNCTION_TRACE ("ns_terminate");
839
840
841         /*
842          * 1) Free the entire namespace -- all nodes and objects
843          *
844          * Delete all object descriptors attached to namepsace nodes
845          */
846         acpi_ns_delete_namespace_subtree (acpi_gbl_root_node);
847
848         /* Detach any objects attached to the root */
849
850         obj_desc = acpi_ns_get_attached_object (acpi_gbl_root_node);
851         if (obj_desc) {
852                 acpi_ns_detach_object (acpi_gbl_root_node);
853         }
854
855         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Namespace freed\n"));
856
857         /*
858          * 2) Now we can delete the ACPI tables
859          */
860         acpi_tb_delete_all_tables ();
861         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "ACPI Tables freed\n"));
862
863         return_VOID;
864 }
865
866
867 /*******************************************************************************
868  *
869  * FUNCTION:    acpi_ns_opens_scope
870  *
871  * PARAMETERS:  Type        - A valid namespace type
872  *
873  * RETURN:      NEWSCOPE if the passed type "opens a name scope" according
874  *              to the ACPI specification, else 0
875  *
876  ******************************************************************************/
877
878 u32
879 acpi_ns_opens_scope (
880         acpi_object_type                type)
881 {
882         ACPI_FUNCTION_TRACE_STR ("ns_opens_scope", acpi_ut_get_type_name (type));
883
884
885         if (!acpi_ut_valid_object_type (type)) {
886                 /* type code out of range  */
887
888                 ACPI_REPORT_WARNING (("ns_opens_scope: Invalid Object Type %X\n", type));
889                 return_VALUE (ACPI_NS_NORMAL);
890         }
891
892         return_VALUE (((u32) acpi_gbl_ns_properties[type]) & ACPI_NS_NEWSCOPE);
893 }
894
895
896 /*******************************************************************************
897  *
898  * FUNCTION:    acpi_ns_get_node_by_path
899  *
900  * PARAMETERS:  *Pathname   - Name to be found, in external (ASL) format. The
901  *                            \ (backslash) and ^ (carat) prefixes, and the
902  *                            . (period) to separate segments are supported.
903  *              start_node  - Root of subtree to be searched, or NS_ALL for the
904  *                            root of the name space.  If Name is fully
905  *                            qualified (first s8 is '\'), the passed value
906  *                            of Scope will not be accessed.
907  *              Flags       - Used to indicate whether to perform upsearch or
908  *                            not.
909  *              return_node - Where the Node is returned
910  *
911  * DESCRIPTION: Look up a name relative to a given scope and return the
912  *              corresponding Node.  NOTE: Scope can be null.
913  *
914  * MUTEX:       Locks namespace
915  *
916  ******************************************************************************/
917
918 acpi_status
919 acpi_ns_get_node_by_path (
920         char                            *pathname,
921         struct acpi_namespace_node      *start_node,
922         u32                             flags,
923         struct acpi_namespace_node      **return_node)
924 {
925         union acpi_generic_state        scope_info;
926         acpi_status                     status;
927         char                            *internal_path = NULL;
928
929
930         ACPI_FUNCTION_TRACE_PTR ("ns_get_node_by_path", pathname);
931
932
933         if (pathname) {
934                 /* Convert path to internal representation */
935
936                 status = acpi_ns_internalize_name (pathname, &internal_path);
937                 if (ACPI_FAILURE (status)) {
938                         return_ACPI_STATUS (status);
939                 }
940         }
941
942         /* Must lock namespace during lookup */
943
944         status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
945         if (ACPI_FAILURE (status)) {
946                 goto cleanup;
947         }
948
949         /* Setup lookup scope (search starting point) */
950
951         scope_info.scope.node = start_node;
952
953         /* Lookup the name in the namespace */
954
955         status = acpi_ns_lookup (&scope_info, internal_path,
956                          ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
957                          (flags | ACPI_NS_DONT_OPEN_SCOPE),
958                          NULL, return_node);
959         if (ACPI_FAILURE (status)) {
960                 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "%s, %s\n",
961                                 internal_path, acpi_format_exception (status)));
962         }
963
964         (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
965
966 cleanup:
967         if (internal_path) {
968                 ACPI_MEM_FREE (internal_path);
969         }
970         return_ACPI_STATUS (status);
971 }
972
973
974 /*******************************************************************************
975  *
976  * FUNCTION:    acpi_ns_get_parent_node
977  *
978  * PARAMETERS:  Node       - Current table entry
979  *
980  * RETURN:      Parent entry of the given entry
981  *
982  * DESCRIPTION: Obtain the parent entry for a given entry in the namespace.
983  *
984  ******************************************************************************/
985
986 struct acpi_namespace_node *
987 acpi_ns_get_parent_node (
988         struct acpi_namespace_node      *node)
989 {
990         ACPI_FUNCTION_ENTRY ();
991
992
993         if (!node) {
994                 return (NULL);
995         }
996
997         /*
998          * Walk to the end of this peer list. The last entry is marked with a flag
999          * and the peer pointer is really a pointer back to the parent. This saves
1000          * putting a parent back pointer in each and every named object!
1001          */
1002         while (!(node->flags & ANOBJ_END_OF_PEER_LIST)) {
1003                 node = node->peer;
1004         }
1005
1006         return (node->peer);
1007 }
1008
1009
1010 /*******************************************************************************
1011  *
1012  * FUNCTION:    acpi_ns_get_next_valid_node
1013  *
1014  * PARAMETERS:  Node       - Current table entry
1015  *
1016  * RETURN:      Next valid Node in the linked node list. NULL if no more valid
1017  *              nodes.
1018  *
1019  * DESCRIPTION: Find the next valid node within a name table.
1020  *              Useful for implementing NULL-end-of-list loops.
1021  *
1022  ******************************************************************************/
1023
1024 struct acpi_namespace_node *
1025 acpi_ns_get_next_valid_node (
1026         struct acpi_namespace_node      *node)
1027 {
1028
1029         /* If we are at the end of this peer list, return NULL */
1030
1031         if (node->flags & ANOBJ_END_OF_PEER_LIST) {
1032                 return NULL;
1033         }
1034
1035         /* Otherwise just return the next peer */
1036
1037         return (node->peer);
1038 }
1039
1040
1041 #ifdef ACPI_OBSOLETE_FUNCTIONS
1042 /*******************************************************************************
1043  *
1044  * FUNCTION:    acpi_ns_find_parent_name
1045  *
1046  * PARAMETERS:  *child_node            - Named Obj whose name is to be found
1047  *
1048  * RETURN:      The ACPI name
1049  *
1050  * DESCRIPTION: Search for the given obj in its parent scope and return the
1051  *              name segment, or "????" if the parent name can't be found
1052  *              (which "should not happen").
1053  *
1054  ******************************************************************************/
1055
1056 acpi_name
1057 acpi_ns_find_parent_name (
1058         struct acpi_namespace_node      *child_node)
1059 {
1060         struct acpi_namespace_node      *parent_node;
1061
1062
1063         ACPI_FUNCTION_TRACE ("ns_find_parent_name");
1064
1065
1066         if (child_node) {
1067                 /* Valid entry.  Get the parent Node */
1068
1069                 parent_node = acpi_ns_get_parent_node (child_node);
1070                 if (parent_node) {
1071                         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
1072                                 "Parent of %p [%4.4s] is %p [%4.4s]\n",
1073                                 child_node, acpi_ut_get_node_name (child_node),
1074                                 parent_node, acpi_ut_get_node_name (parent_node)));
1075
1076                         if (parent_node->name.integer) {
1077                                 return_VALUE ((acpi_name) parent_node->name.integer);
1078                         }
1079                 }
1080
1081                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
1082                         "Unable to find parent of %p (%4.4s)\n",
1083                         child_node, acpi_ut_get_node_name (child_node)));
1084         }
1085
1086         return_VALUE (ACPI_UNKNOWN_NAME);
1087 }
1088 #endif
1089
1090