blob: dbf34a5fc1e0a01c5bcc3b28c0230b3807c3be7b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: evxface - External interfaces for ACPI events
4 *
5 *****************************************************************************/
6
7/*
Bob Moore6c9deb72007-02-02 19:48:24 +03008 * Copyright (C) 2000 - 2007, R. Byron Moore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
45#include <acpi/acnamesp.h>
46#include <acpi/acevents.h>
47#include <acpi/acinterp.h>
48
49#define _COMPONENT ACPI_EVENTS
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("evxface")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*******************************************************************************
53 *
54 * FUNCTION: acpi_install_exception_handler
55 *
56 * PARAMETERS: Handler - Pointer to the handler function for the
57 * event
58 *
59 * RETURN: Status
60 *
61 * DESCRIPTION: Saves the pointer to the handler function
62 *
63 ******************************************************************************/
64#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -040065acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Len Brown4be44fc2005-08-05 00:44:28 -040067 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Bob Mooreb229cf92006-04-21 17:15:00 -040069 ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Len Brown4be44fc2005-08-05 00:44:28 -040071 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
72 if (ACPI_FAILURE(status)) {
73 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
76 /* Don't allow two handlers. */
77
78 if (acpi_gbl_exception_handler) {
79 status = AE_ALREADY_EXISTS;
80 goto cleanup;
81 }
82
83 /* Install the handler */
84
85 acpi_gbl_exception_handler = handler;
86
Len Brown4be44fc2005-08-05 00:44:28 -040087 cleanup:
88 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
89 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
Bob Moore83135242006-10-03 00:00:00 -040091
92ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
Len Brown4be44fc2005-08-05 00:44:28 -040093#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*******************************************************************************
95 *
96 * FUNCTION: acpi_install_fixed_event_handler
97 *
98 * PARAMETERS: Event - Event type to enable.
99 * Handler - Pointer to the handler function for the
100 * event
101 * Context - Value passed to the handler on each GPE
102 *
103 * RETURN: Status
104 *
105 * DESCRIPTION: Saves the pointer to the handler function and then enables the
106 * event.
107 *
108 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400110acpi_install_fixed_event_handler(u32 event,
111 acpi_event_handler handler, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Len Brown4be44fc2005-08-05 00:44:28 -0400113 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Bob Mooreb229cf92006-04-21 17:15:00 -0400115 ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 /* Parameter validation */
118
119 if (event > ACPI_EVENT_MAX) {
Len Brown4be44fc2005-08-05 00:44:28 -0400120 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
Len Brown4be44fc2005-08-05 00:44:28 -0400123 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
124 if (ACPI_FAILURE(status)) {
125 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
128 /* Don't allow two handlers. */
129
130 if (NULL != acpi_gbl_fixed_event_handlers[event].handler) {
131 status = AE_ALREADY_EXISTS;
132 goto cleanup;
133 }
134
135 /* Install the handler before enabling the event */
136
137 acpi_gbl_fixed_event_handlers[event].handler = handler;
138 acpi_gbl_fixed_event_handlers[event].context = context;
139
Len Brown4be44fc2005-08-05 00:44:28 -0400140 status = acpi_clear_event(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400142 status = acpi_enable_event(event, 0);
143 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500144 ACPI_WARNING((AE_INFO, "Could not enable fixed event %X",
145 event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /* Remove the handler */
148
149 acpi_gbl_fixed_event_handlers[event].handler = NULL;
150 acpi_gbl_fixed_event_handlers[event].context = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400151 } else {
152 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
153 "Enabled fixed event %X, Handler=%p\n", event,
154 handler));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
Len Brown4be44fc2005-08-05 00:44:28 -0400157 cleanup:
158 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
159 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Bob Moore83135242006-10-03 00:00:00 -0400162ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/*******************************************************************************
165 *
166 * FUNCTION: acpi_remove_fixed_event_handler
167 *
168 * PARAMETERS: Event - Event type to disable.
169 * Handler - Address of the handler
170 *
171 * RETURN: Status
172 *
173 * DESCRIPTION: Disables the event and unregisters the event handler.
174 *
175 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400177acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Len Brown4be44fc2005-08-05 00:44:28 -0400179 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Bob Mooreb229cf92006-04-21 17:15:00 -0400181 ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* Parameter validation */
184
185 if (event > ACPI_EVENT_MAX) {
Len Brown4be44fc2005-08-05 00:44:28 -0400186 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Len Brown4be44fc2005-08-05 00:44:28 -0400189 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
190 if (ACPI_FAILURE(status)) {
191 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
194 /* Disable the event before removing the handler */
195
Len Brown4be44fc2005-08-05 00:44:28 -0400196 status = acpi_disable_event(event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* Always Remove the handler */
199
200 acpi_gbl_fixed_event_handlers[event].handler = NULL;
201 acpi_gbl_fixed_event_handlers[event].context = NULL;
202
Len Brown4be44fc2005-08-05 00:44:28 -0400203 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500204 ACPI_WARNING((AE_INFO,
205 "Could not write to fixed event enable register %X",
206 event));
Len Brown4be44fc2005-08-05 00:44:28 -0400207 } else {
Bob Moore4a90c7e2006-01-13 16:22:00 -0500208 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabled fixed event %X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400209 event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
Len Brown4be44fc2005-08-05 00:44:28 -0400212 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
213 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Bob Moore83135242006-10-03 00:00:00 -0400216ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218/*******************************************************************************
219 *
220 * FUNCTION: acpi_install_notify_handler
221 *
222 * PARAMETERS: Device - The device for which notifies will be handled
223 * handler_type - The type of handler:
224 * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
225 * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
226 * ACPI_ALL_NOTIFY: both system and device
227 * Handler - Address of the handler
228 * Context - Value passed to the handler on each GPE
229 *
230 * RETURN: Status
231 *
232 * DESCRIPTION: Install a handler for notifies on an ACPI device
233 *
234 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400236acpi_install_notify_handler(acpi_handle device,
237 u32 handler_type,
238 acpi_notify_handler handler, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Len Brown4be44fc2005-08-05 00:44:28 -0400240 union acpi_operand_object *obj_desc;
241 union acpi_operand_object *notify_obj;
242 struct acpi_namespace_node *node;
243 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Bob Mooreb229cf92006-04-21 17:15:00 -0400245 ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* Parameter validation */
248
Len Brown4be44fc2005-08-05 00:44:28 -0400249 if ((!device) ||
250 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
251 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Len Brown4be44fc2005-08-05 00:44:28 -0400254 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
255 if (ACPI_FAILURE(status)) {
256 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
259 /* Convert and validate the device handle */
260
Len Brown4be44fc2005-08-05 00:44:28 -0400261 node = acpi_ns_map_handle_to_node(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!node) {
263 status = AE_BAD_PARAMETER;
264 goto unlock_and_exit;
265 }
266
267 /*
268 * Root Object:
269 * Registering a notify handler on the root object indicates that the
270 * caller wishes to receive notifications for all objects. Note that
271 * only one <external> global handler can be regsitered (per notify type).
272 */
273 if (device == ACPI_ROOT_OBJECT) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* Make sure the handler is not already installed */
276
277 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400278 acpi_gbl_system_notify.handler) ||
279 ((handler_type & ACPI_DEVICE_NOTIFY) &&
280 acpi_gbl_device_notify.handler)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 status = AE_ALREADY_EXISTS;
282 goto unlock_and_exit;
283 }
284
285 if (handler_type & ACPI_SYSTEM_NOTIFY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400286 acpi_gbl_system_notify.node = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 acpi_gbl_system_notify.handler = handler;
288 acpi_gbl_system_notify.context = context;
289 }
290
291 if (handler_type & ACPI_DEVICE_NOTIFY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400292 acpi_gbl_device_notify.node = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 acpi_gbl_device_notify.handler = handler;
294 acpi_gbl_device_notify.context = context;
295 }
296
297 /* Global notify handler installed */
298 }
299
300 /*
301 * All Other Objects:
302 * Caller will only receive notifications specific to the target object.
303 * Note that only certain object types can receive notifications.
304 */
305 else {
306 /* Notifies allowed on this object? */
307
Len Brown4be44fc2005-08-05 00:44:28 -0400308 if (!acpi_ev_is_notify_object(node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 status = AE_TYPE;
310 goto unlock_and_exit;
311 }
312
313 /* Check for an existing internal object */
314
Len Brown4be44fc2005-08-05 00:44:28 -0400315 obj_desc = acpi_ns_get_attached_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (obj_desc) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* Object exists - make sure there's no handler */
319
320 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400321 obj_desc->common_notify.system_notify) ||
322 ((handler_type & ACPI_DEVICE_NOTIFY) &&
323 obj_desc->common_notify.device_notify)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 status = AE_ALREADY_EXISTS;
325 goto unlock_and_exit;
326 }
Len Brown4be44fc2005-08-05 00:44:28 -0400327 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* Create a new object */
329
Len Brown4be44fc2005-08-05 00:44:28 -0400330 obj_desc = acpi_ut_create_internal_object(node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (!obj_desc) {
332 status = AE_NO_MEMORY;
333 goto unlock_and_exit;
334 }
335
336 /* Attach new object to the Node */
337
Len Brown4be44fc2005-08-05 00:44:28 -0400338 status =
339 acpi_ns_attach_object(device, obj_desc, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* Remove local reference to the object */
342
Len Brown4be44fc2005-08-05 00:44:28 -0400343 acpi_ut_remove_reference(obj_desc);
344 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto unlock_and_exit;
346 }
347 }
348
349 /* Install the handler */
350
Len Brown4be44fc2005-08-05 00:44:28 -0400351 notify_obj =
352 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!notify_obj) {
354 status = AE_NO_MEMORY;
355 goto unlock_and_exit;
356 }
357
Len Brown4be44fc2005-08-05 00:44:28 -0400358 notify_obj->notify.node = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 notify_obj->notify.handler = handler;
360 notify_obj->notify.context = context;
361
362 if (handler_type & ACPI_SYSTEM_NOTIFY) {
363 obj_desc->common_notify.system_notify = notify_obj;
364 }
365
366 if (handler_type & ACPI_DEVICE_NOTIFY) {
367 obj_desc->common_notify.device_notify = notify_obj;
368 }
369
370 if (handler_type == ACPI_ALL_NOTIFY) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* Extra ref if installed in both */
373
Len Brown4be44fc2005-08-05 00:44:28 -0400374 acpi_ut_add_reference(notify_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 }
377
Len Brown4be44fc2005-08-05 00:44:28 -0400378 unlock_and_exit:
379 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
380 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Bob Moore83135242006-10-03 00:00:00 -0400383ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385/*******************************************************************************
386 *
387 * FUNCTION: acpi_remove_notify_handler
388 *
389 * PARAMETERS: Device - The device for which notifies will be handled
390 * handler_type - The type of handler:
391 * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
392 * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
393 * ACPI_ALL_NOTIFY: both system and device
394 * Handler - Address of the handler
395 *
396 * RETURN: Status
397 *
398 * DESCRIPTION: Remove a handler for notifies on an ACPI device
399 *
400 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400402acpi_remove_notify_handler(acpi_handle device,
403 u32 handler_type, acpi_notify_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Len Brown4be44fc2005-08-05 00:44:28 -0400405 union acpi_operand_object *notify_obj;
406 union acpi_operand_object *obj_desc;
407 struct acpi_namespace_node *node;
408 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Bob Mooreb229cf92006-04-21 17:15:00 -0400410 ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* Parameter validation */
413
Len Brown4be44fc2005-08-05 00:44:28 -0400414 if ((!device) ||
415 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400416 status = AE_BAD_PARAMETER;
417 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
Len Brown4be44fc2005-08-05 00:44:28 -0400420 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
421 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400422 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
425 /* Convert and validate the device handle */
426
Len Brown4be44fc2005-08-05 00:44:28 -0400427 node = acpi_ns_map_handle_to_node(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (!node) {
429 status = AE_BAD_PARAMETER;
Bob Mooref6dd9222006-07-07 20:44:38 -0400430 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 /* Root Object */
434
435 if (device == ACPI_ROOT_OBJECT) {
Len Brown4be44fc2005-08-05 00:44:28 -0400436 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bob Moore4a90c7e2006-01-13 16:22:00 -0500437 "Removing notify handler for namespace root object\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400440 !acpi_gbl_system_notify.handler) ||
441 ((handler_type & ACPI_DEVICE_NOTIFY) &&
442 !acpi_gbl_device_notify.handler)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 status = AE_NOT_EXIST;
Bob Mooref6dd9222006-07-07 20:44:38 -0400444 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 /* Make sure all deferred tasks are completed */
448
Len Brown4be44fc2005-08-05 00:44:28 -0400449 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 acpi_os_wait_events_complete(NULL);
Len Brown4be44fc2005-08-05 00:44:28 -0400451 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
452 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400453 goto exit;
Len Brown4be44fc2005-08-05 00:44:28 -0400454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (handler_type & ACPI_SYSTEM_NOTIFY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400457 acpi_gbl_system_notify.node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 acpi_gbl_system_notify.handler = NULL;
459 acpi_gbl_system_notify.context = NULL;
460 }
461
462 if (handler_type & ACPI_DEVICE_NOTIFY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400463 acpi_gbl_device_notify.node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 acpi_gbl_device_notify.handler = NULL;
465 acpi_gbl_device_notify.context = NULL;
466 }
467 }
468
469 /* All Other Objects */
470
471 else {
472 /* Notifies allowed on this object? */
473
Len Brown4be44fc2005-08-05 00:44:28 -0400474 if (!acpi_ev_is_notify_object(node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 status = AE_TYPE;
Bob Mooref6dd9222006-07-07 20:44:38 -0400476 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
479 /* Check for an existing internal object */
480
Len Brown4be44fc2005-08-05 00:44:28 -0400481 obj_desc = acpi_ns_get_attached_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!obj_desc) {
483 status = AE_NOT_EXIST;
Bob Mooref6dd9222006-07-07 20:44:38 -0400484 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
487 /* Object exists - make sure there's an existing handler */
488
489 if (handler_type & ACPI_SYSTEM_NOTIFY) {
490 notify_obj = obj_desc->common_notify.system_notify;
Bob Mooref6dd9222006-07-07 20:44:38 -0400491 if (!notify_obj) {
492 status = AE_NOT_EXIST;
493 goto unlock_and_exit;
494 }
495
496 if (notify_obj->notify.handler != handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 status = AE_BAD_PARAMETER;
Bob Mooref6dd9222006-07-07 20:44:38 -0400498 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 /* Make sure all deferred tasks are completed */
501
Len Brown4be44fc2005-08-05 00:44:28 -0400502 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 acpi_os_wait_events_complete(NULL);
Len Brown4be44fc2005-08-05 00:44:28 -0400504 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
505 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400506 goto exit;
Len Brown4be44fc2005-08-05 00:44:28 -0400507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* Remove the handler */
510 obj_desc->common_notify.system_notify = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400511 acpi_ut_remove_reference(notify_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
514 if (handler_type & ACPI_DEVICE_NOTIFY) {
515 notify_obj = obj_desc->common_notify.device_notify;
Bob Mooref6dd9222006-07-07 20:44:38 -0400516 if (!notify_obj) {
517 status = AE_NOT_EXIST;
518 goto unlock_and_exit;
519 }
520
521 if (notify_obj->notify.handler != handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 status = AE_BAD_PARAMETER;
Bob Mooref6dd9222006-07-07 20:44:38 -0400523 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 /* Make sure all deferred tasks are completed */
526
Len Brown4be44fc2005-08-05 00:44:28 -0400527 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 acpi_os_wait_events_complete(NULL);
Len Brown4be44fc2005-08-05 00:44:28 -0400529 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
530 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400531 goto exit;
Len Brown4be44fc2005-08-05 00:44:28 -0400532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Remove the handler */
535 obj_desc->common_notify.device_notify = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400536 acpi_ut_remove_reference(notify_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 }
539
Bob Mooref6dd9222006-07-07 20:44:38 -0400540 unlock_and_exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400541 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Bob Mooref6dd9222006-07-07 20:44:38 -0400542 exit:
Thomas Renningere8406b42006-06-02 15:58:00 -0400543 if (ACPI_FAILURE(status))
544 ACPI_EXCEPTION((AE_INFO, status, "Removing notify handler"));
Len Brown4be44fc2005-08-05 00:44:28 -0400545 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Bob Moore83135242006-10-03 00:00:00 -0400548ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550/*******************************************************************************
551 *
552 * FUNCTION: acpi_install_gpe_handler
553 *
Robert Moore44f6c012005-04-18 22:49:35 -0400554 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
555 * defined GPEs)
556 * gpe_number - The GPE number within the GPE block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * Type - Whether this GPE should be treated as an
558 * edge- or level-triggered interrupt.
559 * Address - Address of the handler
560 * Context - Value passed to the handler on each GPE
561 *
562 * RETURN: Status
563 *
564 * DESCRIPTION: Install a handler for a General Purpose Event.
565 *
566 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400568acpi_install_gpe_handler(acpi_handle gpe_device,
569 u32 gpe_number,
570 u32 type, acpi_event_handler address, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Len Brown4be44fc2005-08-05 00:44:28 -0400572 struct acpi_gpe_event_info *gpe_event_info;
573 struct acpi_handler_info *handler;
574 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500575 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Bob Mooreb229cf92006-04-21 17:15:00 -0400577 ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* Parameter validation */
580
581 if ((!address) || (type > ACPI_GPE_XRUPT_TYPE_MASK)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400582 status = AE_BAD_PARAMETER;
583 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Len Brown4be44fc2005-08-05 00:44:28 -0400586 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
587 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400588 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
591 /* Ensure that we have a valid GPE number */
592
Len Brown4be44fc2005-08-05 00:44:28 -0400593 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!gpe_event_info) {
595 status = AE_BAD_PARAMETER;
Bob Mooref6dd9222006-07-07 20:44:38 -0400596 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
599 /* Make sure that there isn't a handler there already */
600
Len Brown4be44fc2005-08-05 00:44:28 -0400601 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
602 ACPI_GPE_DISPATCH_HANDLER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 status = AE_ALREADY_EXISTS;
Bob Mooref6dd9222006-07-07 20:44:38 -0400604 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 /* Allocate and init handler object */
608
Bob Moore83135242006-10-03 00:00:00 -0400609 handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_handler_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!handler) {
611 status = AE_NO_MEMORY;
Bob Mooref6dd9222006-07-07 20:44:38 -0400612 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
Len Brown4be44fc2005-08-05 00:44:28 -0400615 handler->address = address;
616 handler->context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 handler->method_node = gpe_event_info->dispatch.method_node;
618
619 /* Disable the GPE before installing the handler */
620
Len Brown4be44fc2005-08-05 00:44:28 -0400621 status = acpi_ev_disable_gpe(gpe_event_info);
622 if (ACPI_FAILURE(status)) {
Bob Mooref6dd9222006-07-07 20:44:38 -0400623 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
626 /* Install the handler */
627
Len Brown4be44fc2005-08-05 00:44:28 -0400628 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 gpe_event_info->dispatch.handler = handler;
630
631 /* Setup up dispatch flags to indicate handler (vs. method) */
632
Len Brown4be44fc2005-08-05 00:44:28 -0400633 gpe_event_info->flags &= ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK); /* Clear bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
635
Len Brown4be44fc2005-08-05 00:44:28 -0400636 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Bob Mooref6dd9222006-07-07 20:44:38 -0400638 unlock_and_exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400639 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Bob Mooref6dd9222006-07-07 20:44:38 -0400640 exit:
Thomas Renningere8406b42006-06-02 15:58:00 -0400641 if (ACPI_FAILURE(status))
642 ACPI_EXCEPTION((AE_INFO, status,
643 "Installing notify handler failed"));
Len Brown4be44fc2005-08-05 00:44:28 -0400644 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Bob Moore83135242006-10-03 00:00:00 -0400647ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649/*******************************************************************************
650 *
651 * FUNCTION: acpi_remove_gpe_handler
652 *
Robert Moore44f6c012005-04-18 22:49:35 -0400653 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
654 * defined GPEs)
655 * gpe_number - The event to remove a handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * Address - Address of the handler
657 *
658 * RETURN: Status
659 *
660 * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
661 *
662 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400664acpi_remove_gpe_handler(acpi_handle gpe_device,
665 u32 gpe_number, acpi_event_handler address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Len Brown4be44fc2005-08-05 00:44:28 -0400667 struct acpi_gpe_event_info *gpe_event_info;
668 struct acpi_handler_info *handler;
669 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500670 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Bob Mooreb229cf92006-04-21 17:15:00 -0400672 ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* Parameter validation */
675
676 if (!address) {
Len Brown4be44fc2005-08-05 00:44:28 -0400677 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Len Brown4be44fc2005-08-05 00:44:28 -0400680 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
681 if (ACPI_FAILURE(status)) {
682 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 /* Ensure that we have a valid GPE number */
686
Len Brown4be44fc2005-08-05 00:44:28 -0400687 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (!gpe_event_info) {
689 status = AE_BAD_PARAMETER;
690 goto unlock_and_exit;
691 }
692
693 /* Make sure that a handler is indeed installed */
694
Len Brown4be44fc2005-08-05 00:44:28 -0400695 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
696 ACPI_GPE_DISPATCH_HANDLER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 status = AE_NOT_EXIST;
698 goto unlock_and_exit;
699 }
700
701 /* Make sure that the installed handler is the same */
702
703 if (gpe_event_info->dispatch.handler->address != address) {
704 status = AE_BAD_PARAMETER;
705 goto unlock_and_exit;
706 }
707
708 /* Disable the GPE before removing the handler */
709
Len Brown4be44fc2005-08-05 00:44:28 -0400710 status = acpi_ev_disable_gpe(gpe_event_info);
711 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto unlock_and_exit;
713 }
714
715 /* Make sure all deferred tasks are completed */
716
Len Brown4be44fc2005-08-05 00:44:28 -0400717 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 acpi_os_wait_events_complete(NULL);
Len Brown4be44fc2005-08-05 00:44:28 -0400719 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
720 if (ACPI_FAILURE(status)) {
721 return_ACPI_STATUS(status);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /* Remove the handler */
725
Len Brown4be44fc2005-08-05 00:44:28 -0400726 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 handler = gpe_event_info->dispatch.handler;
728
729 /* Restore Method node (if any), set dispatch flags */
730
731 gpe_event_info->dispatch.method_node = handler->method_node;
Len Brown4be44fc2005-08-05 00:44:28 -0400732 gpe_event_info->flags &= ~ACPI_GPE_DISPATCH_MASK; /* Clear bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (handler->method_node) {
734 gpe_event_info->flags |= ACPI_GPE_DISPATCH_METHOD;
735 }
Len Brown4be44fc2005-08-05 00:44:28 -0400736 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* Now we can free the handler object */
739
Bob Moore83135242006-10-03 00:00:00 -0400740 ACPI_FREE(handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Len Brown4be44fc2005-08-05 00:44:28 -0400742 unlock_and_exit:
743 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
744 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Bob Moore83135242006-10-03 00:00:00 -0400747ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/*******************************************************************************
750 *
751 * FUNCTION: acpi_acquire_global_lock
752 *
753 * PARAMETERS: Timeout - How long the caller is willing to wait
Robert Moore44f6c012005-04-18 22:49:35 -0400754 * Handle - Where the handle to the lock is returned
755 * (if acquired)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 *
757 * RETURN: Status
758 *
759 * DESCRIPTION: Acquire the ACPI Global Lock
760 *
Bob Mooreba886cd2008-04-10 19:06:37 +0400761 * Note: Allows callers with the same thread ID to acquire the global lock
762 * multiple times. In other words, externally, the behavior of the global lock
763 * is identical to an AML mutex. On the first acquire, a new handle is
764 * returned. On any subsequent calls to acquire by the same thread, the same
765 * handle is returned.
766 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400768acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Len Brown4be44fc2005-08-05 00:44:28 -0400770 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (!handle) {
773 return (AE_BAD_PARAMETER);
774 }
775
Len Brown4d2acd92007-05-09 22:56:38 -0400776 /* Must lock interpreter to prevent race conditions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Len Brown4d2acd92007-05-09 22:56:38 -0400778 acpi_ex_enter_interpreter();
Bob Mooreba886cd2008-04-10 19:06:37 +0400779
780 status = acpi_ex_acquire_mutex_object(timeout,
781 acpi_gbl_global_lock_mutex,
782 acpi_os_get_thread_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Len Brown4be44fc2005-08-05 00:44:28 -0400784 if (ACPI_SUCCESS(status)) {
Bob Mooreba886cd2008-04-10 19:06:37 +0400785 /*
786 * If this was the first acquisition of the Global Lock by this thread,
787 * create a new handle. Otherwise, return the existing handle.
788 */
789 if (acpi_gbl_global_lock_mutex->mutex.acquisition_depth == 1) {
790 acpi_gbl_global_lock_handle++;
791 }
792
793 /* Return the global lock handle */
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *handle = acpi_gbl_global_lock_handle;
796 }
797
Bob Mooreba886cd2008-04-10 19:06:37 +0400798 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return (status);
800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Bob Moore83135242006-10-03 00:00:00 -0400802ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804/*******************************************************************************
805 *
806 * FUNCTION: acpi_release_global_lock
807 *
808 * PARAMETERS: Handle - Returned from acpi_acquire_global_lock
809 *
810 * RETURN: Status
811 *
Robert Moore44f6c012005-04-18 22:49:35 -0400812 * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 *
814 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400815acpi_status acpi_release_global_lock(u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Len Brown4be44fc2005-08-05 00:44:28 -0400817 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (handle != acpi_gbl_global_lock_handle) {
820 return (AE_NOT_ACQUIRED);
821 }
822
Bob Mooreba886cd2008-04-10 19:06:37 +0400823 status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return (status);
825}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Bob Moore83135242006-10-03 00:00:00 -0400827ACPI_EXPORT_SYMBOL(acpi_release_global_lock)