[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / drivers / acpi / events / evrgnini.c
blob4983a3378be51310dfb7215ef972d9b470ed7e42
1 /******************************************************************************
3 * Module Name: evrgnini- ACPI address_space (op_region) init
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
46 #include <acpi/acevents.h>
47 #include <acpi/acnamesp.h>
49 #define _COMPONENT ACPI_EVENTS
50 ACPI_MODULE_NAME ("evrgnini")
53 /*******************************************************************************
55 * FUNCTION: acpi_ev_system_memory_region_setup
57 * PARAMETERS: Handle - Region we are interested in
58 * Function - Start or stop
59 * handler_context - Address space handler context
60 * region_context - Region specific context
62 * RETURN: Status
64 * DESCRIPTION: Do any prep work for region handling, a nop for now
66 ******************************************************************************/
68 acpi_status
69 acpi_ev_system_memory_region_setup (
70 acpi_handle handle,
71 u32 function,
72 void *handler_context,
73 void **region_context)
75 union acpi_operand_object *region_desc = (union acpi_operand_object *) handle;
76 struct acpi_mem_space_context *local_region_context;
79 ACPI_FUNCTION_TRACE ("ev_system_memory_region_setup");
82 if (function == ACPI_REGION_DEACTIVATE) {
83 if (*region_context) {
84 ACPI_MEM_FREE (*region_context);
85 *region_context = NULL;
87 return_ACPI_STATUS (AE_OK);
90 /* Create a new context */
92 local_region_context = ACPI_MEM_CALLOCATE (sizeof (struct acpi_mem_space_context));
93 if (!(local_region_context)) {
94 return_ACPI_STATUS (AE_NO_MEMORY);
97 /* Save the region length and address for use in the handler */
99 local_region_context->length = region_desc->region.length;
100 local_region_context->address = region_desc->region.address;
102 *region_context = local_region_context;
103 return_ACPI_STATUS (AE_OK);
107 /*******************************************************************************
109 * FUNCTION: acpi_ev_io_space_region_setup
111 * PARAMETERS: Handle - Region we are interested in
112 * Function - Start or stop
113 * handler_context - Address space handler context
114 * region_context - Region specific context
116 * RETURN: Status
118 * DESCRIPTION: Do any prep work for region handling
120 ******************************************************************************/
122 acpi_status
123 acpi_ev_io_space_region_setup (
124 acpi_handle handle,
125 u32 function,
126 void *handler_context,
127 void **region_context)
129 ACPI_FUNCTION_TRACE ("ev_io_space_region_setup");
132 if (function == ACPI_REGION_DEACTIVATE) {
133 *region_context = NULL;
135 else {
136 *region_context = handler_context;
139 return_ACPI_STATUS (AE_OK);
143 /*******************************************************************************
145 * FUNCTION: acpi_ev_pci_config_region_setup
147 * PARAMETERS: Handle - Region we are interested in
148 * Function - Start or stop
149 * handler_context - Address space handler context
150 * region_context - Region specific context
152 * RETURN: Status
154 * DESCRIPTION: Do any prep work for region handling
156 * MUTEX: Assumes namespace is not locked
158 ******************************************************************************/
160 acpi_status
161 acpi_ev_pci_config_region_setup (
162 acpi_handle handle,
163 u32 function,
164 void *handler_context,
165 void **region_context)
167 acpi_status status = AE_OK;
168 acpi_integer pci_value;
169 struct acpi_pci_id *pci_id = *region_context;
170 union acpi_operand_object *handler_obj;
171 struct acpi_namespace_node *parent_node;
172 struct acpi_namespace_node *pci_root_node;
173 union acpi_operand_object *region_obj = (union acpi_operand_object *) handle;
174 struct acpi_device_id object_hID;
177 ACPI_FUNCTION_TRACE ("ev_pci_config_region_setup");
180 handler_obj = region_obj->region.handler;
181 if (!handler_obj) {
183 * No installed handler. This shouldn't happen because the dispatch
184 * routine checks before we get here, but we check again just in case.
186 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
187 "Attempting to init a region %p, with no handler\n", region_obj));
188 return_ACPI_STATUS (AE_NOT_EXIST);
191 *region_context = NULL;
192 if (function == ACPI_REGION_DEACTIVATE) {
193 if (pci_id) {
194 ACPI_MEM_FREE (pci_id);
196 return_ACPI_STATUS (status);
199 parent_node = acpi_ns_get_parent_node (region_obj->region.node);
202 * Get the _SEG and _BBN values from the device upon which the handler
203 * is installed.
205 * We need to get the _SEG and _BBN objects relative to the PCI BUS device.
206 * This is the device the handler has been registered to handle.
210 * If the address_space.Node is still pointing to the root, we need
211 * to scan upward for a PCI Root bridge and re-associate the op_region
212 * handlers with that device.
214 if (handler_obj->address_space.node == acpi_gbl_root_node) {
215 /* Start search from the parent object */
217 pci_root_node = parent_node;
218 while (pci_root_node != acpi_gbl_root_node) {
219 status = acpi_ut_execute_HID (pci_root_node, &object_hID);
220 if (ACPI_SUCCESS (status)) {
221 /* Got a valid _HID, check if this is a PCI root */
223 if (!(ACPI_STRNCMP (object_hID.value, PCI_ROOT_HID_STRING,
224 sizeof (PCI_ROOT_HID_STRING)))) {
225 /* Install a handler for this PCI root bridge */
227 status = acpi_install_address_space_handler ((acpi_handle) pci_root_node,
228 ACPI_ADR_SPACE_PCI_CONFIG,
229 ACPI_DEFAULT_HANDLER, NULL, NULL);
230 if (ACPI_FAILURE (status)) {
231 if (status == AE_SAME_HANDLER) {
233 * It is OK if the handler is already installed on the root
234 * bridge. Still need to return a context object for the
235 * new PCI_Config operation region, however.
237 status = AE_OK;
239 else {
240 ACPI_REPORT_ERROR ((
241 "Could not install pci_config handler for Root Bridge %4.4s, %s\n",
242 acpi_ut_get_node_name (pci_root_node), acpi_format_exception (status)));
245 break;
249 pci_root_node = acpi_ns_get_parent_node (pci_root_node);
252 /* PCI root bridge not found, use namespace root node */
254 else {
255 pci_root_node = handler_obj->address_space.node;
259 * If this region is now initialized, we are done.
260 * (install_address_space_handler could have initialized it)
262 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
263 return_ACPI_STATUS (AE_OK);
266 /* Region is still not initialized. Create a new context */
268 pci_id = ACPI_MEM_CALLOCATE (sizeof (struct acpi_pci_id));
269 if (!pci_id) {
270 return_ACPI_STATUS (AE_NO_MEMORY);
274 * For PCI_Config space access, we need the segment, bus,
275 * device and function numbers. Acquire them here.
279 * Get the PCI device and function numbers from the _ADR object
280 * contained in the parent's scope.
282 status = acpi_ut_evaluate_numeric_object (METHOD_NAME__ADR, parent_node, &pci_value);
285 * The default is zero, and since the allocation above zeroed
286 * the data, just do nothing on failure.
288 if (ACPI_SUCCESS (status)) {
289 pci_id->device = ACPI_HIWORD (ACPI_LODWORD (pci_value));
290 pci_id->function = ACPI_LOWORD (ACPI_LODWORD (pci_value));
293 /* The PCI segment number comes from the _SEG method */
295 status = acpi_ut_evaluate_numeric_object (METHOD_NAME__SEG, pci_root_node, &pci_value);
296 if (ACPI_SUCCESS (status)) {
297 pci_id->segment = ACPI_LOWORD (pci_value);
300 /* The PCI bus number comes from the _BBN method */
302 status = acpi_ut_evaluate_numeric_object (METHOD_NAME__BBN, pci_root_node, &pci_value);
303 if (ACPI_SUCCESS (status)) {
304 pci_id->bus = ACPI_LOWORD (pci_value);
307 /* Complete this device's pci_id */
309 acpi_os_derive_pci_id (pci_root_node, region_obj->region.node, &pci_id);
311 *region_context = pci_id;
312 return_ACPI_STATUS (AE_OK);
316 /*******************************************************************************
318 * FUNCTION: acpi_ev_pci_bar_region_setup
320 * PARAMETERS: Handle - Region we are interested in
321 * Function - Start or stop
322 * handler_context - Address space handler context
323 * region_context - Region specific context
325 * RETURN: Status
327 * DESCRIPTION: Do any prep work for region handling
329 * MUTEX: Assumes namespace is not locked
331 ******************************************************************************/
333 acpi_status
334 acpi_ev_pci_bar_region_setup (
335 acpi_handle handle,
336 u32 function,
337 void *handler_context,
338 void **region_context)
340 ACPI_FUNCTION_TRACE ("ev_pci_bar_region_setup");
343 return_ACPI_STATUS (AE_OK);
347 /*******************************************************************************
349 * FUNCTION: acpi_ev_cmos_region_setup
351 * PARAMETERS: Handle - Region we are interested in
352 * Function - Start or stop
353 * handler_context - Address space handler context
354 * region_context - Region specific context
356 * RETURN: Status
358 * DESCRIPTION: Do any prep work for region handling
360 * MUTEX: Assumes namespace is not locked
362 ******************************************************************************/
364 acpi_status
365 acpi_ev_cmos_region_setup (
366 acpi_handle handle,
367 u32 function,
368 void *handler_context,
369 void **region_context)
371 ACPI_FUNCTION_TRACE ("ev_cmos_region_setup");
374 return_ACPI_STATUS (AE_OK);
378 /*******************************************************************************
380 * FUNCTION: acpi_ev_default_region_setup
382 * PARAMETERS: Handle - Region we are interested in
383 * Function - Start or stop
384 * handler_context - Address space handler context
385 * region_context - Region specific context
387 * RETURN: Status
389 * DESCRIPTION: Do any prep work for region handling
391 ******************************************************************************/
393 acpi_status
394 acpi_ev_default_region_setup (
395 acpi_handle handle,
396 u32 function,
397 void *handler_context,
398 void **region_context)
400 ACPI_FUNCTION_TRACE ("ev_default_region_setup");
403 if (function == ACPI_REGION_DEACTIVATE) {
404 *region_context = NULL;
406 else {
407 *region_context = handler_context;
410 return_ACPI_STATUS (AE_OK);
414 /*******************************************************************************
416 * FUNCTION: acpi_ev_initialize_region
418 * PARAMETERS: region_obj - Region we are initializing
419 * acpi_ns_locked - Is namespace locked?
421 * RETURN: Status
423 * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
424 * for execution at a later time
426 * Get the appropriate address space handler for a newly
427 * created region.
429 * This also performs address space specific initialization. For
430 * example, PCI regions must have an _ADR object that contains
431 * a PCI address in the scope of the definition. This address is
432 * required to perform an access to PCI config space.
434 ******************************************************************************/
436 acpi_status
437 acpi_ev_initialize_region (
438 union acpi_operand_object *region_obj,
439 u8 acpi_ns_locked)
441 union acpi_operand_object *handler_obj;
442 union acpi_operand_object *obj_desc;
443 acpi_adr_space_type space_id;
444 struct acpi_namespace_node *node;
445 acpi_status status;
446 struct acpi_namespace_node *method_node;
447 acpi_name *reg_name_ptr = (acpi_name *) METHOD_NAME__REG;
448 union acpi_operand_object *region_obj2;
451 ACPI_FUNCTION_TRACE_U32 ("ev_initialize_region", acpi_ns_locked);
454 if (!region_obj) {
455 return_ACPI_STATUS (AE_BAD_PARAMETER);
458 if (region_obj->common.flags & AOPOBJ_OBJECT_INITIALIZED) {
459 return_ACPI_STATUS (AE_OK);
462 region_obj2 = acpi_ns_get_secondary_object (region_obj);
463 if (!region_obj2) {
464 return_ACPI_STATUS (AE_NOT_EXIST);
467 node = acpi_ns_get_parent_node (region_obj->region.node);
468 space_id = region_obj->region.space_id;
470 /* Setup defaults */
472 region_obj->region.handler = NULL;
473 region_obj2->extra.method_REG = NULL;
474 region_obj->common.flags &= ~(AOPOBJ_SETUP_COMPLETE);
475 region_obj->common.flags |= AOPOBJ_OBJECT_INITIALIZED;
477 /* Find any "_REG" method associated with this region definition */
479 status = acpi_ns_search_node (*reg_name_ptr, node,
480 ACPI_TYPE_METHOD, &method_node);
481 if (ACPI_SUCCESS (status)) {
483 * The _REG method is optional and there can be only one per region
484 * definition. This will be executed when the handler is attached
485 * or removed
487 region_obj2->extra.method_REG = method_node;
491 * The following loop depends upon the root Node having no parent
492 * ie: acpi_gbl_root_node->parent_entry being set to NULL
494 while (node) {
495 /* Check to see if a handler exists */
497 handler_obj = NULL;
498 obj_desc = acpi_ns_get_attached_object (node);
499 if (obj_desc) {
500 /* Can only be a handler if the object exists */
502 switch (node->type) {
503 case ACPI_TYPE_DEVICE:
505 handler_obj = obj_desc->device.handler;
506 break;
508 case ACPI_TYPE_PROCESSOR:
510 handler_obj = obj_desc->processor.handler;
511 break;
513 case ACPI_TYPE_THERMAL:
515 handler_obj = obj_desc->thermal_zone.handler;
516 break;
518 default:
519 /* Ignore other objects */
520 break;
523 while (handler_obj) {
524 /* Is this handler of the correct type? */
526 if (handler_obj->address_space.space_id == space_id) {
527 /* Found correct handler */
529 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
530 "Found handler %p for region %p in obj %p\n",
531 handler_obj, region_obj, obj_desc));
533 status = acpi_ev_attach_region (handler_obj, region_obj,
534 acpi_ns_locked);
537 * Tell all users that this region is usable by running the _REG
538 * method
540 if (acpi_ns_locked) {
541 status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
542 if (ACPI_FAILURE (status)) {
543 return_ACPI_STATUS (status);
547 status = acpi_ev_execute_reg_method (region_obj, 1);
549 if (acpi_ns_locked) {
550 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
551 if (ACPI_FAILURE (status)) {
552 return_ACPI_STATUS (status);
556 return_ACPI_STATUS (AE_OK);
559 /* Try next handler in the list */
561 handler_obj = handler_obj->address_space.next;
566 * This node does not have the handler we need;
567 * Pop up one level
569 node = acpi_ns_get_parent_node (node);
572 /* If we get here, there is no handler for this region */
574 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
575 "No handler for region_type %s(%X) (region_obj %p)\n",
576 acpi_ut_get_region_name (space_id), space_id, region_obj));
578 return_ACPI_STATUS (AE_NOT_EXIST);