ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / acpi / events / evxfregn.c
1 /******************************************************************************
2  *
3  * Module Name: evxfregn - External Interfaces, ACPI Operation Regions and
4  *                         Address Spaces.
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2004, 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/acevents.h>
49 #include <acpi/acinterp.h>
50
51 #define _COMPONENT          ACPI_EVENTS
52          ACPI_MODULE_NAME    ("evxfregn")
53
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    acpi_install_address_space_handler
58  *
59  * PARAMETERS:  Device          - Handle for the device
60  *              space_id        - The address space ID
61  *              Handler         - Address of the handler
62  *              Setup           - Address of the setup function
63  *              Context         - Value passed to the handler on each access
64  *
65  * RETURN:      Status
66  *
67  * DESCRIPTION: Install a handler for all op_regions of a given space_id.
68  *
69  ******************************************************************************/
70
71 acpi_status
72 acpi_install_address_space_handler (
73         acpi_handle                     device,
74         acpi_adr_space_type             space_id,
75         acpi_adr_space_handler          handler,
76         acpi_adr_space_setup            setup,
77         void                            *context)
78 {
79         union acpi_operand_object       *obj_desc;
80         union acpi_operand_object       *handler_obj;
81         struct acpi_namespace_node      *node;
82         acpi_status                     status;
83         acpi_object_type                type;
84         u16                             flags = 0;
85
86
87         ACPI_FUNCTION_TRACE ("acpi_install_address_space_handler");
88
89
90         /* Parameter validation */
91
92         if (!device) {
93                 return_ACPI_STATUS (AE_BAD_PARAMETER);
94         }
95
96         status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
97         if (ACPI_FAILURE (status)) {
98                 return_ACPI_STATUS (status);
99         }
100
101         /* Convert and validate the device handle */
102
103         node = acpi_ns_map_handle_to_node (device);
104         if (!node) {
105                 status = AE_BAD_PARAMETER;
106                 goto unlock_and_exit;
107         }
108
109         /*
110          * This registration is valid for only the types below
111          * and the root.  This is where the default handlers
112          * get placed.
113          */
114         if ((node->type != ACPI_TYPE_DEVICE)     &&
115                 (node->type != ACPI_TYPE_PROCESSOR)  &&
116                 (node->type != ACPI_TYPE_THERMAL)    &&
117                 (node != acpi_gbl_root_node)) {
118                 status = AE_BAD_PARAMETER;
119                 goto unlock_and_exit;
120         }
121
122         if (handler == ACPI_DEFAULT_HANDLER) {
123                 flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
124
125                 switch (space_id) {
126                 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
127                         handler = acpi_ex_system_memory_space_handler;
128                         setup   = acpi_ev_system_memory_region_setup;
129                         break;
130
131                 case ACPI_ADR_SPACE_SYSTEM_IO:
132                         handler = acpi_ex_system_io_space_handler;
133                         setup   = acpi_ev_io_space_region_setup;
134                         break;
135
136                 case ACPI_ADR_SPACE_PCI_CONFIG:
137                         handler = acpi_ex_pci_config_space_handler;
138                         setup   = acpi_ev_pci_config_region_setup;
139                         break;
140
141                 case ACPI_ADR_SPACE_CMOS:
142                         handler = acpi_ex_cmos_space_handler;
143                         setup   = acpi_ev_cmos_region_setup;
144                         break;
145
146                 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
147                         handler = acpi_ex_pci_bar_space_handler;
148                         setup   = acpi_ev_pci_bar_region_setup;
149                         break;
150
151                 case ACPI_ADR_SPACE_DATA_TABLE:
152                         handler = acpi_ex_data_table_space_handler;
153                         setup   = NULL;
154                         break;
155
156                 default:
157                         status = AE_BAD_PARAMETER;
158                         goto unlock_and_exit;
159                 }
160         }
161
162         /* If the caller hasn't specified a setup routine, use the default */
163
164         if (!setup) {
165                 setup = acpi_ev_default_region_setup;
166         }
167
168         /* Check for an existing internal object */
169
170         obj_desc = acpi_ns_get_attached_object (node);
171         if (obj_desc) {
172                 /*
173                  * The attached device object already exists.
174                  * Make sure the handler is not already installed.
175                  */
176                 handler_obj = obj_desc->device.handler;
177
178                 /* Walk the handler list for this device */
179
180                 while (handler_obj) {
181                         /* Same space_id indicates a handler already installed */
182
183                         if(handler_obj->address_space.space_id == space_id) {
184                                 if (handler_obj->address_space.handler == handler) {
185                                         /*
186                                          * It is (relatively) OK to attempt to install the SAME
187                                          * handler twice. This can easily happen with PCI_Config space.
188                                          */
189                                         status = AE_SAME_HANDLER;
190                                         goto unlock_and_exit;
191                                 }
192                                 else {
193                                         /* A handler is already installed */
194
195                                         status = AE_ALREADY_EXISTS;
196                                 }
197                                 goto unlock_and_exit;
198                         }
199
200                         /* Walk the linked list of handlers */
201
202                         handler_obj = handler_obj->address_space.next;
203                 }
204         }
205         else {
206                 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
207                         "Creating object on Device %p while installing handler\n", node));
208
209                 /* obj_desc does not exist, create one */
210
211                 if (node->type == ACPI_TYPE_ANY) {
212                         type = ACPI_TYPE_DEVICE;
213                 }
214                 else {
215                         type = node->type;
216                 }
217
218                 obj_desc = acpi_ut_create_internal_object (type);
219                 if (!obj_desc) {
220                         status = AE_NO_MEMORY;
221                         goto unlock_and_exit;
222                 }
223
224                 /* Init new descriptor */
225
226                 obj_desc->common.type = (u8) type;
227
228                 /* Attach the new object to the Node */
229
230                 status = acpi_ns_attach_object (node, obj_desc, type);
231
232                 /* Remove local reference to the object */
233
234                 acpi_ut_remove_reference (obj_desc);
235
236                 if (ACPI_FAILURE (status)) {
237                         goto unlock_and_exit;
238                 }
239         }
240
241         ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
242                 "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
243                 acpi_ut_get_region_name (space_id), space_id,
244                 acpi_ut_get_node_name (node), node, obj_desc));
245
246         /*
247          * Install the handler
248          *
249          * At this point there is no existing handler.
250          * Just allocate the object for the handler and link it
251          * into the list.
252          */
253         handler_obj = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
254         if (!handler_obj) {
255                 status = AE_NO_MEMORY;
256                 goto unlock_and_exit;
257         }
258
259         /* Init handler obj */
260
261         handler_obj->address_space.space_id  = (u8) space_id;
262         handler_obj->address_space.hflags    = flags;
263         handler_obj->address_space.region_list = NULL;
264         handler_obj->address_space.node      = node;
265         handler_obj->address_space.handler   = handler;
266         handler_obj->address_space.context   = context;
267         handler_obj->address_space.setup     = setup;
268
269         /* Install at head of Device.address_space list */
270
271         handler_obj->address_space.next      = obj_desc->device.handler;
272
273         /*
274          * The Device object is the first reference on the handler_obj.
275          * Each region that uses the handler adds a reference.
276          */
277         obj_desc->device.handler = handler_obj;
278
279         /*
280          * Walk the namespace finding all of the regions this
281          * handler will manage.
282          *
283          * Start at the device and search the branch toward
284          * the leaf nodes until either the leaf is encountered or
285          * a device is detected that has an address handler of the
286          * same type.
287          *
288          * In either case, back up and search down the remainder
289          * of the branch
290          */
291         status = acpi_ns_walk_namespace (ACPI_TYPE_ANY, device, ACPI_UINT32_MAX,
292                           ACPI_NS_WALK_UNLOCK, acpi_ev_install_handler,
293                           handler_obj, NULL);
294
295         /*
296          * Now we can run the _REG methods for all Regions for this
297          * space ID.  This is a separate walk in order to handle any
298          * interdependencies between regions and _REG methods.  (i.e. handlers
299          * must be installed for all regions of this Space ID before we
300          * can run any _REG methods.
301          */
302         status = acpi_ns_walk_namespace (ACPI_TYPE_ANY, device, ACPI_UINT32_MAX,
303                           ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
304                           handler_obj, NULL);
305
306 unlock_and_exit:
307         (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
308         return_ACPI_STATUS (status);
309 }
310
311
312 /*******************************************************************************
313  *
314  * FUNCTION:    acpi_remove_address_space_handler
315  *
316  * PARAMETERS:  Device          - Handle for the device
317  *              space_id        - The address space ID
318  *              Handler         - Address of the handler
319  *
320  * RETURN:      Status
321  *
322  * DESCRIPTION: Remove a previously installed handler.
323  *
324  ******************************************************************************/
325
326 acpi_status
327 acpi_remove_address_space_handler (
328         acpi_handle                     device,
329         acpi_adr_space_type             space_id,
330         acpi_adr_space_handler          handler)
331 {
332         union acpi_operand_object       *obj_desc;
333         union acpi_operand_object       *handler_obj;
334         union acpi_operand_object       *region_obj;
335         union acpi_operand_object       **last_obj_ptr;
336         struct acpi_namespace_node      *node;
337         acpi_status                     status;
338
339
340         ACPI_FUNCTION_TRACE ("acpi_remove_address_space_handler");
341
342
343         /* Parameter validation */
344
345         if (!device) {
346                 return_ACPI_STATUS (AE_BAD_PARAMETER);
347         }
348
349         status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
350         if (ACPI_FAILURE (status)) {
351                 return_ACPI_STATUS (status);
352         }
353
354         /* Convert and validate the device handle */
355
356         node = acpi_ns_map_handle_to_node (device);
357         if (!node) {
358                 status = AE_BAD_PARAMETER;
359                 goto unlock_and_exit;
360         }
361
362         /* Make sure the internal object exists */
363
364         obj_desc = acpi_ns_get_attached_object (node);
365         if (!obj_desc) {
366                 status = AE_NOT_EXIST;
367                 goto unlock_and_exit;
368         }
369
370         /* Find the address handler the user requested */
371
372         handler_obj = obj_desc->device.handler;
373         last_obj_ptr = &obj_desc->device.handler;
374         while (handler_obj) {
375                 /* We have a handler, see if user requested this one */
376
377                 if (handler_obj->address_space.space_id == space_id) {
378                         /* Matched space_id, first dereference this in the Regions */
379
380                         ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
381                                 "Removing address handler %p(%p) for region %s on Device %p(%p)\n",
382                                 handler_obj, handler, acpi_ut_get_region_name (space_id),
383                                 node, obj_desc));
384
385                         region_obj = handler_obj->address_space.region_list;
386
387                         /* Walk the handler's region list */
388
389                         while (region_obj) {
390                                 /*
391                                  * First disassociate the handler from the region.
392                                  *
393                                  * NOTE: this doesn't mean that the region goes away
394                                  * The region is just inaccessible as indicated to
395                                  * the _REG method
396                                  */
397                                 acpi_ev_detach_region (region_obj, TRUE);
398
399                                 /*
400                                  * Walk the list: Just grab the head because the
401                                  * detach_region removed the previous head.
402                                  */
403                                 region_obj = handler_obj->address_space.region_list;
404
405                         }
406
407                         /* Remove this Handler object from the list */
408
409                         *last_obj_ptr = handler_obj->address_space.next;
410
411                         /* Now we can delete the handler object */
412
413                         acpi_ut_remove_reference (handler_obj);
414                         goto unlock_and_exit;
415                 }
416
417                 /* Walk the linked list of handlers */
418
419                 last_obj_ptr = &handler_obj->address_space.next;
420                 handler_obj = handler_obj->address_space.next;
421         }
422
423         /* The handler does not exist */
424
425         ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
426                 "Unable to remove address handler %p for %s(%X), dev_node %p, obj %p\n",
427                 handler, acpi_ut_get_region_name (space_id), space_id, node, obj_desc));
428
429         status = AE_NOT_EXIST;
430
431 unlock_and_exit:
432         (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
433         return_ACPI_STATUS (status);
434 }
435
436