ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / acpi / namespace / nsinit.c
1 /******************************************************************************
2  *
3  * Module Name: nsinit - namespace initialization
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2004, R. Byron Moore
9  * All rights reserved.
10  *
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.
25  *
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.
29  *
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.
42  */
43
44
45 #include <acpi/acpi.h>
46 #include <acpi/acnamesp.h>
47 #include <acpi/acdispat.h>
48 #include <acpi/acinterp.h>
49
50 #define _COMPONENT          ACPI_NAMESPACE
51          ACPI_MODULE_NAME    ("nsinit")
52
53
54 /*******************************************************************************
55  *
56  * FUNCTION:    acpi_ns_initialize_objects
57  *
58  * PARAMETERS:  None
59  *
60  * RETURN:      Status
61  *
62  * DESCRIPTION: Walk the entire namespace and perform any necessary
63  *              initialization on the objects found therein
64  *
65  ******************************************************************************/
66
67 acpi_status
68 acpi_ns_initialize_objects (
69         void)
70 {
71         acpi_status                     status;
72         struct acpi_init_walk_info      info;
73
74
75         ACPI_FUNCTION_TRACE ("ns_initialize_objects");
76
77
78         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
79                 "**** Starting initialization of namespace objects ****\n"));
80         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT, "Completing Region/Field/Buffer/Package initialization:"));
81
82         /* Set all init info to zero */
83
84         ACPI_MEMSET (&info, 0, sizeof (struct acpi_init_walk_info));
85
86         /* Walk entire namespace from the supplied root */
87
88         status = acpi_walk_namespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
89                           ACPI_UINT32_MAX, acpi_ns_init_one_object,
90                           &info, NULL);
91         if (ACPI_FAILURE (status)) {
92                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "walk_namespace failed! %s\n",
93                         acpi_format_exception (status)));
94         }
95
96         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT,
97                 "\nInitialized %hd/%hd Regions %hd/%hd Fields %hd/%hd Buffers %hd/%hd Packages (%hd nodes)\n",
98                 info.op_region_init, info.op_region_count,
99                 info.field_init,    info.field_count,
100                 info.buffer_init,   info.buffer_count,
101                 info.package_init,  info.package_count, info.object_count));
102
103         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
104                 "%hd Control Methods found\n", info.method_count));
105         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
106                 "%hd Op Regions found\n", info.op_region_count));
107
108         return_ACPI_STATUS (AE_OK);
109 }
110
111
112 /*******************************************************************************
113  *
114  * FUNCTION:    acpi_ns_initialize_devices
115  *
116  * PARAMETERS:  None
117  *
118  * RETURN:      acpi_status
119  *
120  * DESCRIPTION: Walk the entire namespace and initialize all ACPI devices.
121  *              This means running _INI on all present devices.
122  *
123  *              Note: We install PCI config space handler on region access,
124  *              not here.
125  *
126  ******************************************************************************/
127
128 acpi_status
129 acpi_ns_initialize_devices (
130         void)
131 {
132         acpi_status                     status;
133         struct acpi_device_walk_info    info;
134
135
136         ACPI_FUNCTION_TRACE ("ns_initialize_devices");
137
138
139         /* Init counters */
140
141         info.device_count = 0;
142         info.num_STA = 0;
143         info.num_INI = 0;
144
145         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT, "Executing all Device _STA and_INI methods:"));
146
147         status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
148         if (ACPI_FAILURE (status)) {
149                 return_ACPI_STATUS (status);
150         }
151
152         /* Walk namespace for all objects of type Device or Processor */
153
154         status = acpi_ns_walk_namespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
155                           ACPI_UINT32_MAX, TRUE, acpi_ns_init_one_device, &info, NULL);
156
157         (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
158
159         if (ACPI_FAILURE (status)) {
160                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "walk_namespace failed! %s\n",
161                         acpi_format_exception (status)));
162         }
163
164         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT,
165                 "\n%hd Devices found containing: %hd _STA, %hd _INI methods\n",
166                 info.device_count, info.num_STA, info.num_INI));
167
168         return_ACPI_STATUS (status);
169 }
170
171
172 /*******************************************************************************
173  *
174  * FUNCTION:    acpi_ns_init_one_object
175  *
176  * PARAMETERS:  obj_handle      - Node
177  *              Level           - Current nesting level
178  *              Context         - Points to a init info struct
179  *              return_value    - Not used
180  *
181  * RETURN:      Status
182  *
183  * DESCRIPTION: Callback from acpi_walk_namespace. Invoked for every object
184  *              within the  namespace.
185  *
186  *              Currently, the only objects that require initialization are:
187  *              1) Methods
188  *              2) Op Regions
189  *
190  ******************************************************************************/
191
192 acpi_status
193 acpi_ns_init_one_object (
194         acpi_handle                     obj_handle,
195         u32                             level,
196         void                            *context,
197         void                            **return_value)
198 {
199         acpi_object_type                type;
200         acpi_status                     status;
201         struct acpi_init_walk_info      *info = (struct acpi_init_walk_info *) context;
202         struct acpi_namespace_node      *node = (struct acpi_namespace_node *) obj_handle;
203         union acpi_operand_object       *obj_desc;
204
205
206         ACPI_FUNCTION_NAME ("ns_init_one_object");
207
208
209         info->object_count++;
210
211         /* And even then, we are only interested in a few object types */
212
213         type = acpi_ns_get_type (obj_handle);
214         obj_desc = acpi_ns_get_attached_object (node);
215         if (!obj_desc) {
216                 return (AE_OK);
217         }
218
219         /* Increment counters for object types we are looking for */
220
221         switch (type) {
222         case ACPI_TYPE_REGION:
223                 info->op_region_count++;
224                 break;
225
226         case ACPI_TYPE_BUFFER_FIELD:
227                 info->field_count++;
228                 break;
229
230         case ACPI_TYPE_BUFFER:
231                 info->buffer_count++;
232                 break;
233
234         case ACPI_TYPE_PACKAGE:
235                 info->package_count++;
236                 break;
237
238         default:
239
240                 /* No init required, just exit now */
241                 return (AE_OK);
242         }
243
244         /*
245          * If the object is already initialized, nothing else to do
246          */
247         if (obj_desc->common.flags & AOPOBJ_DATA_VALID) {
248                 return (AE_OK);
249         }
250
251         /*
252          * Must lock the interpreter before executing AML code
253          */
254         status = acpi_ex_enter_interpreter ();
255         if (ACPI_FAILURE (status)) {
256                 return (status);
257         }
258
259         /*
260          * Each of these types can contain executable AML code within
261          * the declaration.
262          */
263         switch (type) {
264         case ACPI_TYPE_REGION:
265
266                 info->op_region_init++;
267                 status = acpi_ds_get_region_arguments (obj_desc);
268                 break;
269
270
271         case ACPI_TYPE_BUFFER_FIELD:
272
273                 info->field_init++;
274                 status = acpi_ds_get_buffer_field_arguments (obj_desc);
275                 break;
276
277
278         case ACPI_TYPE_BUFFER:
279
280                 info->buffer_init++;
281                 status = acpi_ds_get_buffer_arguments (obj_desc);
282                 break;
283
284
285         case ACPI_TYPE_PACKAGE:
286
287                 info->package_init++;
288                 status = acpi_ds_get_package_arguments (obj_desc);
289                 break;
290
291         default:
292                 /* No other types can get here */
293                 break;
294         }
295
296         if (ACPI_FAILURE (status)) {
297                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ERROR, "\n"));
298                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
299                                 "Could not execute arguments for [%4.4s] (%s), %s\n",
300                                 acpi_ut_get_node_name (node), acpi_ut_get_type_name (type),
301                                 acpi_format_exception (status)));
302         }
303
304         /* Print a dot for each object unless we are going to print the entire pathname */
305
306         if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
307                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT, "."));
308         }
309
310         /*
311          * We ignore errors from above, and always return OK, since
312          * we don't want to abort the walk on any single error.
313          */
314         acpi_ex_exit_interpreter ();
315         return (AE_OK);
316 }
317
318
319 /*******************************************************************************
320  *
321  * FUNCTION:    acpi_ns_init_one_device
322  *
323  * PARAMETERS:  acpi_walk_callback
324  *
325  * RETURN:      acpi_status
326  *
327  * DESCRIPTION: This is called once per device soon after ACPI is enabled
328  *              to initialize each device. It determines if the device is
329  *              present, and if so, calls _INI.
330  *
331  ******************************************************************************/
332
333 acpi_status
334 acpi_ns_init_one_device (
335         acpi_handle                     obj_handle,
336         u32                             nesting_level,
337         void                            *context,
338         void                            **return_value)
339 {
340         acpi_status                     status;
341         struct acpi_namespace_node     *node;
342         u32                             flags;
343         struct acpi_device_walk_info   *info = (struct acpi_device_walk_info *) context;
344
345
346         ACPI_FUNCTION_TRACE ("ns_init_one_device");
347
348
349         node = acpi_ns_map_handle_to_node (obj_handle);
350         if (!node) {
351                 return_ACPI_STATUS (AE_BAD_PARAMETER);
352         }
353
354         /*
355          * We will run _STA/_INI on Devices and Processors only
356          */
357         if ((node->type != ACPI_TYPE_DEVICE) &&
358                 (node->type != ACPI_TYPE_PROCESSOR)) {
359                 return_ACPI_STATUS (AE_OK);
360         }
361
362         if ((acpi_dbg_level <= ACPI_LV_ALL_EXCEPTIONS) && (!(acpi_dbg_level & ACPI_LV_INFO))) {
363                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT, "."));
364         }
365
366         info->device_count++;
367
368         /*
369          * Run _STA to determine if we can run _INI on the device.
370          */
371         ACPI_DEBUG_EXEC (acpi_ut_display_init_pathname (ACPI_TYPE_METHOD, node, "_STA"));
372         status = acpi_ut_execute_STA (node, &flags);
373
374         if (ACPI_FAILURE (status)) {
375                 if (node->type == ACPI_TYPE_DEVICE) {
376                         /* Ignore error and move on to next device */
377
378                         return_ACPI_STATUS (AE_OK);
379                 }
380
381                 /* _STA is not required for Processor objects */
382         }
383         else {
384                 info->num_STA++;
385
386                 if (!(flags & 0x01)) {
387                         /* Don't look at children of a not present device */
388
389                         return_ACPI_STATUS(AE_CTRL_DEPTH);
390                 }
391         }
392
393         /*
394          * The device is present. Run _INI.
395          */
396         ACPI_DEBUG_EXEC (acpi_ut_display_init_pathname (ACPI_TYPE_METHOD, obj_handle, "_INI"));
397         status = acpi_ns_evaluate_relative (obj_handle, "_INI", NULL, NULL);
398         if (ACPI_FAILURE (status)) {
399                 /* No _INI (AE_NOT_FOUND) means device requires no initialization */
400
401                 if (status != AE_NOT_FOUND) {
402                         /* Ignore error and move on to next device */
403
404         #ifdef ACPI_DEBUG_OUTPUT
405                         char                *scope_name = acpi_ns_get_external_pathname (obj_handle);
406
407                         ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "%s._INI failed: %s\n",
408                                         scope_name, acpi_format_exception (status)));
409
410                         ACPI_MEM_FREE (scope_name);
411         #endif
412                 }
413
414                 status = AE_OK;
415         }
416         else {
417                 /* Count of successful INIs */
418
419                 info->num_INI++;
420         }
421
422         if (acpi_gbl_init_handler) {
423                 /* External initialization handler is present, call it */
424
425                 status = acpi_gbl_init_handler (obj_handle, ACPI_INIT_DEVICE_INI);
426         }
427
428
429         return_ACPI_STATUS (status);
430 }