:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / drivers / bus / acpi / namespace / nsaccess.c
1 /*******************************************************************************
2  *
3  * Module Name: nsaccess - Top-level functions for accessing ACPI namespace
4  *              $Revision$
5  *
6  ******************************************************************************/
7
8 /*
9  *  Copyright (C) 2000, 2001 R. Byron Moore
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26
27 #include "acpi.h"
28 #include "amlcode.h"
29 #include "acinterp.h"
30 #include "acnamesp.h"
31 #include "acdispat.h"
32
33
34 #define _COMPONENT          ACPI_NAMESPACE
35          MODULE_NAME         ("nsaccess")
36
37
38 /*******************************************************************************
39  *
40  * FUNCTION:    Acpi_ns_root_initialize
41  *
42  * PARAMETERS:  None
43  *
44  * RETURN:      Status
45  *
46  * DESCRIPTION: Allocate and initialize the default root named objects
47  *
48  * MUTEX:       Locks namespace for entire execution
49  *
50  ******************************************************************************/
51
52 ACPI_STATUS
53 acpi_ns_root_initialize (void)
54 {
55         ACPI_STATUS             status = AE_OK;
56         PREDEFINED_NAMES        *init_val = NULL;
57         ACPI_NAMESPACE_NODE     *new_node;
58         ACPI_OPERAND_OBJECT     *obj_desc;
59
60
61         acpi_cm_acquire_mutex (ACPI_MTX_NAMESPACE);
62
63         /*
64          * The global root ptr is initially NULL, so a non-NULL value indicates
65          * that Acpi_ns_root_initialize() has already been called; just return.
66          */
67
68         if (acpi_gbl_root_node) {
69                 status = AE_OK;
70                 goto unlock_and_exit;
71         }
72
73
74         /*
75          * Tell the rest of the subsystem that the root is initialized
76          * (This is OK because the namespace is locked)
77          */
78
79         acpi_gbl_root_node = &acpi_gbl_root_node_struct;
80
81
82         /* Enter the pre-defined names in the name table */
83
84         for (init_val = acpi_gbl_pre_defined_names; init_val->name; init_val++) {
85                 status = acpi_ns_lookup (NULL, init_val->name,
86                                  (OBJECT_TYPE_INTERNAL) init_val->type,
87                                  IMODE_LOAD_PASS2, NS_NO_UPSEARCH,
88                                  NULL, &new_node);
89
90
91                 /*
92                  * Name entered successfully.
93                  * If entry in Pre_defined_names[] specifies an
94                  * initial value, create the initial value.
95                  */
96
97                 if (init_val->val) {
98                         /*
99                          * Entry requests an initial value, allocate a
100                          * descriptor for it.
101                          */
102
103                         obj_desc = acpi_cm_create_internal_object (
104                                           (OBJECT_TYPE_INTERNAL) init_val->type);
105
106                         if (!obj_desc) {
107                                 status = AE_NO_MEMORY;
108                                 goto unlock_and_exit;
109                         }
110
111                         /*
112                          * Convert value string from table entry to
113                          * internal representation. Only types actually
114                          * used for initial values are implemented here.
115                          */
116
117                         switch (init_val->type) {
118
119                         case ACPI_TYPE_INTEGER:
120
121                                 obj_desc->integer.value =
122                                                 (ACPI_INTEGER) STRTOUL (init_val->val, NULL, 10);
123                                 break;
124
125
126                         case ACPI_TYPE_STRING:
127
128                                 obj_desc->string.length = STRLEN (init_val->val);
129
130                                 /*
131                                  * Allocate a buffer for the string.  All
132                                  * String.Pointers must be allocated buffers!
133                                  * (makes deletion simpler)
134                                  */
135                                 obj_desc->string.pointer = acpi_cm_allocate (
136                                                    (obj_desc->string.length + 1));
137                                 if (!obj_desc->string.pointer) {
138                                         acpi_cm_remove_reference (obj_desc);
139                                         status = AE_NO_MEMORY;
140                                         goto unlock_and_exit;
141                                 }
142
143                                 STRCPY (obj_desc->string.pointer, init_val->val);
144                                 break;
145
146
147                         case ACPI_TYPE_MUTEX:
148
149                                 obj_desc->mutex.sync_level =
150                                                  (u16) STRTOUL (init_val->val, NULL, 10);
151
152                                 if (STRCMP (init_val->name, "_GL_") == 0) {
153                                         /*
154                                          * Create a counting semaphore for the
155                                          * global lock
156                                          */
157                                         status = acpi_os_create_semaphore (ACPI_NO_UNIT_LIMIT,
158                                                          1, &obj_desc->mutex.semaphore);
159
160                                         if (ACPI_FAILURE (status)) {
161                                                 goto unlock_and_exit;
162                                         }
163                                         /*
164                                          * We just created the mutex for the
165                                          * global lock, save it
166                                          */
167
168                                         acpi_gbl_global_lock_semaphore = obj_desc->mutex.semaphore;
169                                 }
170
171                                 else {
172                                         /* Create a mutex */
173
174                                         status = acpi_os_create_semaphore (1, 1,
175                                                            &obj_desc->mutex.semaphore);
176
177                                         if (ACPI_FAILURE (status)) {
178                                                 goto unlock_and_exit;
179                                         }
180                                 }
181                                 break;
182
183
184                         default:
185                                 REPORT_ERROR (("Unsupported initial type value %X\n",
186                                         init_val->type));
187                                 acpi_cm_remove_reference (obj_desc);
188                                 obj_desc = NULL;
189                                 continue;
190                         }
191
192                         /* Store pointer to value descriptor in the Node */
193
194                         acpi_ns_attach_object (new_node, obj_desc, obj_desc->common.type);
195                 }
196         }
197
198
199 unlock_and_exit:
200         acpi_cm_release_mutex (ACPI_MTX_NAMESPACE);
201         return (status);
202 }
203
204
205 /*******************************************************************************
206  *
207  * FUNCTION:    Acpi_ns_lookup
208  *
209  * PARAMETERS:  Prefix_node     - Search scope if name is not fully qualified
210  *              Pathname        - Search pathname, in internal format
211  *                                (as represented in the AML stream)
212  *              Type            - Type associated with name
213  *              Interpreter_mode - IMODE_LOAD_PASS2 => add name if not found
214  *              Flags           - Flags describing the search restrictions
215  *              Walk_state      - Current state of the walk
216  *              Return_node     - Where the Node is placed (if found
217  *                                or created successfully)
218  *
219  * RETURN:      Status
220  *
221  * DESCRIPTION: Find or enter the passed name in the name space.
222  *              Log an error if name not found in Exec mode.
223  *
224  * MUTEX:       Assumes namespace is locked.
225  *
226  ******************************************************************************/
227
228 ACPI_STATUS
229 acpi_ns_lookup (
230         ACPI_GENERIC_STATE      *scope_info,
231         NATIVE_CHAR             *pathname,
232         OBJECT_TYPE_INTERNAL    type,
233         OPERATING_MODE          interpreter_mode,
234         u32                     flags,
235         ACPI_WALK_STATE         *walk_state,
236         ACPI_NAMESPACE_NODE     **return_node)
237 {
238         ACPI_STATUS             status;
239         ACPI_NAMESPACE_NODE     *prefix_node;
240         ACPI_NAMESPACE_NODE     *current_node = NULL;
241         ACPI_NAMESPACE_NODE     *scope_to_push = NULL;
242         ACPI_NAMESPACE_NODE     *this_node = NULL;
243         u32                     num_segments;
244         ACPI_NAME               simple_name;
245         u8                      null_name_path = FALSE;
246         OBJECT_TYPE_INTERNAL    type_to_check_for;
247         OBJECT_TYPE_INTERNAL    this_search_type;
248         u32                     local_flags = flags & ~NS_ERROR_IF_FOUND;
249
250
251         if (!return_node) {
252                 return (AE_BAD_PARAMETER);
253         }
254
255
256         acpi_gbl_ns_lookup_count++;
257
258         *return_node = ENTRY_NOT_FOUND;
259
260
261         if (!acpi_gbl_root_node) {
262                 return (AE_NO_NAMESPACE);
263         }
264
265         /*
266          * Get the prefix scope.
267          * A null scope means use the root scope
268          */
269
270         if ((!scope_info) ||
271                 (!scope_info->scope.node)) {
272                 prefix_node = acpi_gbl_root_node;
273         }
274         else {
275                 prefix_node = scope_info->scope.node;
276         }
277
278
279         /*
280          * This check is explicitly split provide relax the Type_to_check_for
281          * conditions for Bank_field_defn. Originally, both Bank_field_defn and
282          * Def_field_defn caused Type_to_check_for to be set to ACPI_TYPE_REGION,
283          * but the Bank_field_defn may also check for a Field definition as well
284          * as an Operation_region.
285          */
286
287         if (INTERNAL_TYPE_DEF_FIELD_DEFN == type) {
288                 /* Def_field_defn defines fields in a Region */
289
290                 type_to_check_for = ACPI_TYPE_REGION;
291         }
292
293         else if (INTERNAL_TYPE_BANK_FIELD_DEFN == type) {
294                 /* Bank_field_defn defines data fields in a Field Object */
295
296                 type_to_check_for = ACPI_TYPE_ANY;
297         }
298
299         else {
300                 type_to_check_for = type;
301         }
302
303
304         /* TBD: [Restructure] - Move the pathname stuff into a new procedure */
305
306         /* Examine the name pointer */
307
308         if (!pathname) {
309                 /*  8-12-98 ASL Grammar Update supports null Name_path  */
310
311                 null_name_path = TRUE;
312                 num_segments = 0;
313                 this_node = acpi_gbl_root_node;
314
315         }
316
317         else {
318                 /*
319                  * Valid name pointer (Internal name format)
320                  *
321                  * Check for prefixes.  As represented in the AML stream, a
322                  * Pathname consists of an optional scope prefix followed by
323                  * a segment part.
324                  *
325                  * If present, the scope prefix is either a Root_prefix (in
326                  * which case the name is fully qualified), or zero or more
327                  * Parent_prefixes (in which case the name's scope is relative
328                  * to the current scope).
329                  *
330                  * The segment part consists of either:
331                  *  - A single 4-byte name segment, or
332                  *  - A Dual_name_prefix followed by two 4-byte name segments, or
333                  *  - A Multi_name_prefix_op, followed by a byte indicating the
334                  *    number of segments and the segments themselves.
335                  */
336
337                 if (*pathname == AML_ROOT_PREFIX) {
338                         /* Pathname is fully qualified, look in root name table */
339
340                         current_node = acpi_gbl_root_node;
341
342                         /* point to segment part */
343
344                         pathname++;
345
346                         /* Direct reference to root, "\" */
347
348                         if (!(*pathname)) {
349                                 this_node = acpi_gbl_root_node;
350                                 goto check_for_new_scope_and_exit;
351                         }
352                 }
353
354                 else {
355                         /* Pathname is relative to current scope, start there */
356
357                         current_node = prefix_node;
358
359                         /*
360                          * Handle up-prefix (carat).  More than one prefix
361                          * is supported
362                          */
363
364                         while (*pathname == AML_PARENT_PREFIX) {
365                                 /* Point to segment part or next Parent_prefix */
366
367                                 pathname++;
368
369                                 /*  Backup to the parent's scope  */
370
371                                 this_node = acpi_ns_get_parent_object (current_node);
372                                 if (!this_node) {
373                                         /* Current scope has no parent scope */
374
375                                         REPORT_ERROR (
376                                                 ("Too many parent prefixes (^) - reached root\n"));
377                                         return (AE_NOT_FOUND);
378                                 }
379
380                                 current_node = this_node;
381                         }
382                 }
383
384
385                 /*
386                  * Examine the name prefix opcode, if any,
387                  * to determine the number of segments
388                  */
389
390                 if (*pathname == AML_DUAL_NAME_PREFIX) {
391                         num_segments = 2;
392
393                         /* point to first segment */
394
395                         pathname++;
396
397                 }
398
399                 else if (*pathname == AML_MULTI_NAME_PREFIX_OP) {
400                         num_segments = (u32)* (u8 *) ++pathname;
401
402                         /* point to first segment */
403
404                         pathname++;
405
406                 }
407
408                 else {
409                         /*
410                          * No Dual or Multi prefix, hence there is only one
411                          * segment and Pathname is already pointing to it.
412                          */
413                         num_segments = 1;
414
415                 }
416
417         }
418
419
420         /*
421          * Search namespace for each segment of the name.
422          * Loop through and verify/add each name segment.
423          */
424
425
426         while (num_segments-- && current_node) {
427                 /*
428                  * Search for the current name segment under the current
429                  * named object.  The Type is significant only at the last (topmost)
430                  * level.  (We don't care about the types along the path, only
431                  * the type of the final target object.)
432                  */
433                 this_search_type = ACPI_TYPE_ANY;
434                 if (!num_segments) {
435                         this_search_type = type;
436                         local_flags = flags;
437                 }
438
439                 /* Pluck one ACPI name from the front of the pathname */
440
441                 MOVE_UNALIGNED32_TO_32 (&simple_name, pathname);
442
443                 /* Try to find the ACPI name */
444
445                 status = acpi_ns_search_and_enter (simple_name, walk_state,
446                                    current_node, interpreter_mode,
447                                    this_search_type, local_flags,
448                                    &this_node);
449
450                 if (ACPI_FAILURE (status)) {
451                         if (status == AE_NOT_FOUND) {
452                                 /* Name not found in ACPI namespace  */
453
454                         }
455
456                         return (status);
457                 }
458
459
460                 /*
461                  * If 1) This is the last segment (Num_segments == 0)
462                  *    2) and looking for a specific type
463                  *       (Not checking for TYPE_ANY)
464                  *    3) Which is not an alias
465                  *    4) which is not a local type (TYPE_DEF_ANY)
466                  *    5) which is not a local type (TYPE_SCOPE)
467                  *    6) which is not a local type (TYPE_INDEX_FIELD_DEFN)
468                  *    7) and type of object is known (not TYPE_ANY)
469                  *    8) and object does not match request
470                  *
471                  * Then we have a type mismatch.  Just warn and ignore it.
472                  */
473                 if ((num_segments       == 0)                               &&
474                         (type_to_check_for  != ACPI_TYPE_ANY)                   &&
475                         (type_to_check_for  != INTERNAL_TYPE_ALIAS)             &&
476                         (type_to_check_for  != INTERNAL_TYPE_DEF_ANY)           &&
477                         (type_to_check_for  != INTERNAL_TYPE_SCOPE)             &&
478                         (type_to_check_for  != INTERNAL_TYPE_INDEX_FIELD_DEFN)  &&
479                         (this_node->type    != ACPI_TYPE_ANY)                   &&
480                         (this_node->type    != type_to_check_for)) {
481                         /* Complain about a type mismatch */
482
483                         REPORT_WARNING (
484                                 ("Ns_lookup: %4.4s, type %X, checking for type %X\n",
485                                 &simple_name, this_node->type, type_to_check_for));
486                 }
487
488                 /*
489                  * If this is the last name segment and we are not looking for a
490                  * specific type, but the type of found object is known, use that type
491                  * to see if it opens a scope.
492                  */
493
494                 if ((0 == num_segments) && (ACPI_TYPE_ANY == type)) {
495                         type = this_node->type;
496                 }
497
498                 if ((num_segments || acpi_ns_opens_scope (type)) &&
499                         (this_node->child == NULL)) {
500                         /*
501                          * More segments or the type implies enclosed scope,
502                          * and the next scope has not been allocated.
503                          */
504
505                 }
506
507                 current_node = this_node;
508
509                 /* point to next name segment */
510
511                 pathname += ACPI_NAME_SIZE;
512         }
513
514
515         /*
516          * Always check if we need to open a new scope
517          */
518
519 check_for_new_scope_and_exit:
520
521         if (!(flags & NS_DONT_OPEN_SCOPE) && (walk_state)) {
522                 /*
523                  * If entry is a type which opens a scope,
524                  * push the new scope on the scope stack.
525                  */
526
527                 if (acpi_ns_opens_scope (type_to_check_for)) {
528                         /*  8-12-98 ASL Grammar Update supports null Name_path  */
529
530                         if (null_name_path) {
531                                 /* TBD: [Investigate] - is this the correct thing to do? */
532
533                                 scope_to_push = NULL;
534                         }
535                         else {
536                                 scope_to_push = this_node;
537                         }
538
539                         status = acpi_ds_scope_stack_push (scope_to_push, type,
540                                            walk_state);
541                         if (ACPI_FAILURE (status)) {
542                                 return (status);
543                         }
544
545                 }
546         }
547
548         *return_node = this_node;
549         return (AE_OK);
550 }
551