blob: 10b8543dd466e593b3d5f589af22cc7041843a24 [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/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, Intel Corp.
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>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acnamesp.h"
47#include "acevents.h"
48#include "acinterp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define _COMPONENT ACPI_EVENTS
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("evxface")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*******************************************************************************
54 *
55 * FUNCTION: acpi_install_exception_handler
56 *
57 * PARAMETERS: Handler - Pointer to the handler function for the
58 * event
59 *
60 * RETURN: Status
61 *
62 * DESCRIPTION: Saves the pointer to the handler function
63 *
64 ******************************************************************************/
65#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -040066acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Len Brown4be44fc2005-08-05 00:44:28 -040068 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Bob Mooreb229cf92006-04-21 17:15:00 -040070 ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Len Brown4be44fc2005-08-05 00:44:28 -040072 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
73 if (ACPI_FAILURE(status)) {
74 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76
77 /* Don't allow two handlers. */
78
79 if (acpi_gbl_exception_handler) {
80 status = AE_ALREADY_EXISTS;
81 goto cleanup;
82 }
83
84 /* Install the handler */
85
86 acpi_gbl_exception_handler = handler;
87
Len Brown4be44fc2005-08-05 00:44:28 -040088 cleanup:
89 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
90 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
Bob Moore83135242006-10-03 00:00:00 -040092
93ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
Len Brown4be44fc2005-08-05 00:44:28 -040094#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*******************************************************************************
96 *
97 * FUNCTION: acpi_install_fixed_event_handler
98 *
99 * PARAMETERS: Event - Event type to enable.
100 * Handler - Pointer to the handler function for the
101 * event
102 * Context - Value passed to the handler on each GPE
103 *
104 * RETURN: Status
105 *
106 * DESCRIPTION: Saves the pointer to the handler function and then enables the
107 * event.
108 *
109 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400111acpi_install_fixed_event_handler(u32 event,
112 acpi_event_handler handler, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Len Brown4be44fc2005-08-05 00:44:28 -0400114 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Bob Mooreb229cf92006-04-21 17:15:00 -0400116 ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 /* Parameter validation */
119
120 if (event > ACPI_EVENT_MAX) {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Len Brown4be44fc2005-08-05 00:44:28 -0400124 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
125 if (ACPI_FAILURE(status)) {
126 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
129 /* Don't allow two handlers. */
130
131 if (NULL != acpi_gbl_fixed_event_handlers[event].handler) {
132 status = AE_ALREADY_EXISTS;
133 goto cleanup;
134 }
135
136 /* Install the handler before enabling the event */
137
138 acpi_gbl_fixed_event_handlers[event].handler = handler;
139 acpi_gbl_fixed_event_handlers[event].context = context;
140
Len Brown4be44fc2005-08-05 00:44:28 -0400141 status = acpi_clear_event(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400143 status = acpi_enable_event(event, 0);
144 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500145 ACPI_WARNING((AE_INFO, "Could not enable fixed event %X",
146 event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* Remove the handler */
149
150 acpi_gbl_fixed_event_handlers[event].handler = NULL;
151 acpi_gbl_fixed_event_handlers[event].context = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400152 } else {
153 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
154 "Enabled fixed event %X, Handler=%p\n", event,
155 handler));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
Len Brown4be44fc2005-08-05 00:44:28 -0400158 cleanup:
159 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
160 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Bob Moore83135242006-10-03 00:00:00 -0400163ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/*******************************************************************************
166 *
167 * FUNCTION: acpi_remove_fixed_event_handler
168 *
169 * PARAMETERS: Event - Event type to disable.
170 * Handler - Address of the handler
171 *
172 * RETURN: Status
173 *
174 * DESCRIPTION: Disables the event and unregisters the event handler.
175 *
176 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400178acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Len Brown4be44fc2005-08-05 00:44:28 -0400180 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Bob Mooreb229cf92006-04-21 17:15:00 -0400182 ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /* Parameter validation */
185
186 if (event > ACPI_EVENT_MAX) {
Len Brown4be44fc2005-08-05 00:44:28 -0400187 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
Len Brown4be44fc2005-08-05 00:44:28 -0400190 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
191 if (ACPI_FAILURE(status)) {
192 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
195 /* Disable the event before removing the handler */
196
Len Brown4be44fc2005-08-05 00:44:28 -0400197 status = acpi_disable_event(event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* Always Remove the handler */
200
201 acpi_gbl_fixed_event_handlers[event].handler = NULL;
202 acpi_gbl_fixed_event_handlers[event].context = NULL;
203
Len Brown4be44fc2005-08-05 00:44:28 -0400204 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500205 ACPI_WARNING((AE_INFO,
206 "Could not write to fixed event enable register %X",
207 event));
Len Brown4be44fc2005-08-05 00:44:28 -0400208 } else {
Bob Moore4a90c7e2006-01-13 16:22:00 -0500209 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabled fixed event %X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400210 event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
Len Brown4be44fc2005-08-05 00:44:28 -0400213 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
214 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Bob Moore83135242006-10-03 00:00:00 -0400217ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219/*******************************************************************************
220 *
221 * FUNCTION: acpi_install_notify_handler
222 *
223 * PARAMETERS: Device - The device for which notifies will be handled
224 * handler_type - The type of handler:
225 * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
226 * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
227 * ACPI_ALL_NOTIFY: both system and device
228 * Handler - Address of the handler
229 * Context - Value passed to the handler on each GPE
230 *
231 * RETURN: Status
232 *
233 * DESCRIPTION: Install a handler for notifies on an ACPI device
234 *
235 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400237acpi_install_notify_handler(acpi_handle device,
238 u32 handler_type,
239 acpi_notify_handler handler, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Len Brown4be44fc2005-08-05 00:44:28 -0400241 union acpi_operand_object *obj_desc;
242 union acpi_operand_object *notify_obj;
243 struct acpi_namespace_node *node;
244 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Bob Mooreb229cf92006-04-21 17:15:00 -0400246 ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Parameter validation */
249
Len Brown4be44fc2005-08-05 00:44:28 -0400250 if ((!device) ||
251 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
252 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
Len Brown4be44fc2005-08-05 00:44:28 -0400255 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
256 if (ACPI_FAILURE(status)) {
257 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
260 /* Convert and validate the device handle */
261
Len Brown4be44fc2005-08-05 00:44:28 -0400262 node = acpi_ns_map_handle_to_node(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!node) {
264 status = AE_BAD_PARAMETER;
265 goto unlock_and_exit;
266 }
267
268 /*
269 * Root Object:
270 * Registering a notify handler on the root object indicates that the
Bob Moore9f15fc62008-11-12 16:01:56 +0800271 * caller wishes to receive notifications for all objects. Note that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * only one <external> global handler can be regsitered (per notify type).
273 */
274 if (device == ACPI_ROOT_OBJECT) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* Make sure the handler is not already installed */
277
278 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400279 acpi_gbl_system_notify.handler) ||
280 ((handler_type & ACPI_DEVICE_NOTIFY) &&
281 acpi_gbl_device_notify.handler)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 status = AE_ALREADY_EXISTS;
283 goto unlock_and_exit;
284 }
285
286 if (handler_type & ACPI_SYSTEM_NOTIFY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400287 acpi_gbl_system_notify.node = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 acpi_gbl_system_notify.handler = handler;
289 acpi_gbl_system_notify.context = context;
290 }
291
292 if (handler_type & ACPI_DEVICE_NOTIFY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400293 acpi_gbl_device_notify.node = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 acpi_gbl_device_notify.handler = handler;
295 acpi_gbl_device_notify.context = context;
296 }
297
298 /* Global notify handler installed */
299 }
300
301 /*
302 * All Other Objects:
303 * Caller will only receive notifications specific to the target object.
304 * Note that only certain object types can receive notifications.
305 */
306 else {
307 /* Notifies allowed on this object? */
308
Len Brown4be44fc2005-08-05 00:44:28 -0400309 if (!acpi_ev_is_notify_object(node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 status = AE_TYPE;
311 goto unlock_and_exit;
312 }
313
314 /* Check for an existing internal object */
315
Len Brown4be44fc2005-08-05 00:44:28 -0400316 obj_desc = acpi_ns_get_attached_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (obj_desc) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* Object exists - make sure there's no handler */
320
321 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400322 obj_desc->common_notify.system_notify) ||
323 ((handler_type & ACPI_DEVICE_NOTIFY) &&
324 obj_desc->common_notify.device_notify)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 status = AE_ALREADY_EXISTS;
326 goto unlock_and_exit;
327 }
Len Brown4be44fc2005-08-05 00:44:28 -0400328 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /* Create a new object */
330
Len Brown4be44fc2005-08-05 00:44:28 -0400331 obj_desc = acpi_ut_create_internal_object(node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!obj_desc) {
333 status = AE_NO_MEMORY;
334 goto unlock_and_exit;
335 }
336
337 /* Attach new object to the Node */
338
Len Brown4be44fc2005-08-05 00:44:28 -0400339 status =
340 acpi_ns_attach_object(device, obj_desc, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Remove local reference to the object */
343
Len Brown4be44fc2005-08-05 00:44:28 -0400344 acpi_ut_remove_reference(obj_desc);
345 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto unlock_and_exit;
347 }
348 }
349
350 /* Install the handler */
351
Len Brown4be44fc2005-08-05 00:44:28 -0400352 notify_obj =
353 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!notify_obj) {
355 status = AE_NO_MEMORY;
356 goto unlock_and_exit;
357 }
358
Len Brown4be44fc2005-08-05 00:44:28 -0400359 notify_obj->notify.node = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 notify_obj->notify.handler = handler;
361 notify_obj->notify.context = context;
362
363 if (handler_type & ACPI_SYSTEM_NOTIFY) {
364 obj_desc->common_notify.system_notify = notify_obj;
365 }
366
367 if (handler_type & ACPI_DEVICE_NOTIFY) {
368 obj_desc->common_notify.device_notify = notify_obj;
369 }
370
371 if (handler_type == ACPI_ALL_NOTIFY) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* Extra ref if installed in both */
374
Len Brown4be44fc2005-08-05 00:44:28 -0400375 acpi_ut_add_reference(notify_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 }
378
Len Brown4be44fc2005-08-05 00:44:28 -0400379 unlock_and_exit:
380 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
381 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Bob Moore83135242006-10-03 00:00:00 -0400384ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386/*******************************************************************************
387 *
388 * FUNCTION: acpi_remove_notify_handler
389 *
390 * PARAMETERS: Device - The device for which notifies will be handled
391 * handler_type - The type of handler:
392 * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
393 * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
394 * ACPI_ALL_NOTIFY: both system and device
395 * Handler - Address of the handler
396 *
397 * RETURN: Status
398 *
399 * DESCRIPTION: Remove a handler for notifies on an ACPI device
400 *
401 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400403acpi_remove_notify_handler(acpi_handle device,
404 u32 handler_type, acpi_notify_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Len Brown4be44fc2005-08-05 00:44:28 -0400406 union acpi_operand_object *notify_obj;
407 union acpi_operand_object *obj_desc;
408 struct acpi_namespace_node *node;
409 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Bob Mooreb229cf92006-04-21 17:15:00 -0400411 ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /* Parameter validation */
414
Len Brown4be44fc2005-08-05 00:44:28 -0400415 if ((!device) ||
416 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400417 status = AE_BAD_PARAMETER;
418 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
Len Brown4be44fc2005-08-05 00:44:28 -0400421 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
422 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400423 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 /* Convert and validate the device handle */
427
Len Brown4be44fc2005-08-05 00:44:28 -0400428 node = acpi_ns_map_handle_to_node(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (!node) {
430 status = AE_BAD_PARAMETER;
Bob Mooref6dd9222006-07-07 20:44:38 -0400431 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
434 /* Root Object */
435
436 if (device == ACPI_ROOT_OBJECT) {
Len Brown4be44fc2005-08-05 00:44:28 -0400437 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bob Moore4a90c7e2006-01-13 16:22:00 -0500438 "Removing notify handler for namespace root object\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400441 !acpi_gbl_system_notify.handler) ||
442 ((handler_type & ACPI_DEVICE_NOTIFY) &&
443 !acpi_gbl_device_notify.handler)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 status = AE_NOT_EXIST;
Bob Mooref6dd9222006-07-07 20:44:38 -0400445 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
448 /* Make sure all deferred tasks are completed */
449
Len Brown4be44fc2005-08-05 00:44:28 -0400450 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 acpi_os_wait_events_complete(NULL);
Len Brown4be44fc2005-08-05 00:44:28 -0400452 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
453 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400454 goto exit;
Len Brown4be44fc2005-08-05 00:44:28 -0400455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 if (handler_type & ACPI_SYSTEM_NOTIFY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400458 acpi_gbl_system_notify.node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 acpi_gbl_system_notify.handler = NULL;
460 acpi_gbl_system_notify.context = NULL;
461 }
462
463 if (handler_type & ACPI_DEVICE_NOTIFY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400464 acpi_gbl_device_notify.node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 acpi_gbl_device_notify.handler = NULL;
466 acpi_gbl_device_notify.context = NULL;
467 }
468 }
469
470 /* All Other Objects */
471
472 else {
473 /* Notifies allowed on this object? */
474
Len Brown4be44fc2005-08-05 00:44:28 -0400475 if (!acpi_ev_is_notify_object(node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 status = AE_TYPE;
Bob Mooref6dd9222006-07-07 20:44:38 -0400477 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479
480 /* Check for an existing internal object */
481
Len Brown4be44fc2005-08-05 00:44:28 -0400482 obj_desc = acpi_ns_get_attached_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!obj_desc) {
484 status = AE_NOT_EXIST;
Bob Mooref6dd9222006-07-07 20:44:38 -0400485 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
488 /* Object exists - make sure there's an existing handler */
489
490 if (handler_type & ACPI_SYSTEM_NOTIFY) {
491 notify_obj = obj_desc->common_notify.system_notify;
Bob Mooref6dd9222006-07-07 20:44:38 -0400492 if (!notify_obj) {
493 status = AE_NOT_EXIST;
494 goto unlock_and_exit;
495 }
496
497 if (notify_obj->notify.handler != handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 status = AE_BAD_PARAMETER;
Bob Mooref6dd9222006-07-07 20:44:38 -0400499 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 /* Make sure all deferred tasks are completed */
502
Len Brown4be44fc2005-08-05 00:44:28 -0400503 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 acpi_os_wait_events_complete(NULL);
Len Brown4be44fc2005-08-05 00:44:28 -0400505 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
506 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400507 goto exit;
Len Brown4be44fc2005-08-05 00:44:28 -0400508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* Remove the handler */
511 obj_desc->common_notify.system_notify = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400512 acpi_ut_remove_reference(notify_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
515 if (handler_type & ACPI_DEVICE_NOTIFY) {
516 notify_obj = obj_desc->common_notify.device_notify;
Bob Mooref6dd9222006-07-07 20:44:38 -0400517 if (!notify_obj) {
518 status = AE_NOT_EXIST;
519 goto unlock_and_exit;
520 }
521
522 if (notify_obj->notify.handler != handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 status = AE_BAD_PARAMETER;
Bob Mooref6dd9222006-07-07 20:44:38 -0400524 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 /* Make sure all deferred tasks are completed */
527
Len Brown4be44fc2005-08-05 00:44:28 -0400528 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 acpi_os_wait_events_complete(NULL);
Len Brown4be44fc2005-08-05 00:44:28 -0400530 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
531 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400532 goto exit;
Len Brown4be44fc2005-08-05 00:44:28 -0400533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* Remove the handler */
536 obj_desc->common_notify.device_notify = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400537 acpi_ut_remove_reference(notify_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 }
540
Bob Mooref6dd9222006-07-07 20:44:38 -0400541 unlock_and_exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400542 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Bob Mooref6dd9222006-07-07 20:44:38 -0400543 exit:
Thomas Renningere8406b42006-06-02 15:58:00 -0400544 if (ACPI_FAILURE(status))
545 ACPI_EXCEPTION((AE_INFO, status, "Removing notify handler"));
Len Brown4be44fc2005-08-05 00:44:28 -0400546 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Bob Moore83135242006-10-03 00:00:00 -0400549ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551/*******************************************************************************
552 *
553 * FUNCTION: acpi_install_gpe_handler
554 *
Robert Moore44f6c012005-04-18 22:49:35 -0400555 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
556 * defined GPEs)
557 * gpe_number - The GPE number within the GPE block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * Type - Whether this GPE should be treated as an
559 * edge- or level-triggered interrupt.
560 * Address - Address of the handler
561 * Context - Value passed to the handler on each GPE
562 *
563 * RETURN: Status
564 *
565 * DESCRIPTION: Install a handler for a General Purpose Event.
566 *
567 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400569acpi_install_gpe_handler(acpi_handle gpe_device,
570 u32 gpe_number,
571 u32 type, acpi_event_handler address, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Len Brown4be44fc2005-08-05 00:44:28 -0400573 struct acpi_gpe_event_info *gpe_event_info;
574 struct acpi_handler_info *handler;
575 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500576 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Bob Mooreb229cf92006-04-21 17:15:00 -0400578 ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* Parameter validation */
581
582 if ((!address) || (type > ACPI_GPE_XRUPT_TYPE_MASK)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400583 status = AE_BAD_PARAMETER;
584 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
Len Brown4be44fc2005-08-05 00:44:28 -0400587 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
588 if (ACPI_FAILURE(status)) {
Thomas Renningere8406b42006-06-02 15:58:00 -0400589 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 /* Ensure that we have a valid GPE number */
593
Len Brown4be44fc2005-08-05 00:44:28 -0400594 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!gpe_event_info) {
596 status = AE_BAD_PARAMETER;
Bob Mooref6dd9222006-07-07 20:44:38 -0400597 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
600 /* Make sure that there isn't a handler there already */
601
Len Brown4be44fc2005-08-05 00:44:28 -0400602 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
603 ACPI_GPE_DISPATCH_HANDLER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 status = AE_ALREADY_EXISTS;
Bob Mooref6dd9222006-07-07 20:44:38 -0400605 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 /* Allocate and init handler object */
609
Bob Moore83135242006-10-03 00:00:00 -0400610 handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_handler_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!handler) {
612 status = AE_NO_MEMORY;
Bob Mooref6dd9222006-07-07 20:44:38 -0400613 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
Len Brown4be44fc2005-08-05 00:44:28 -0400616 handler->address = address;
617 handler->context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 handler->method_node = gpe_event_info->dispatch.method_node;
619
620 /* Disable the GPE before installing the handler */
621
Len Brown4be44fc2005-08-05 00:44:28 -0400622 status = acpi_ev_disable_gpe(gpe_event_info);
623 if (ACPI_FAILURE(status)) {
Bob Mooref6dd9222006-07-07 20:44:38 -0400624 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 /* Install the handler */
628
Len Brown4be44fc2005-08-05 00:44:28 -0400629 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 gpe_event_info->dispatch.handler = handler;
631
632 /* Setup up dispatch flags to indicate handler (vs. method) */
633
Bob Moored4913dc2009-03-06 10:05:18 +0800634 gpe_event_info->flags &=
635 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
637
Len Brown4be44fc2005-08-05 00:44:28 -0400638 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Bob Mooref6dd9222006-07-07 20:44:38 -0400640 unlock_and_exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400641 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Bob Mooref6dd9222006-07-07 20:44:38 -0400642 exit:
Thomas Renningere8406b42006-06-02 15:58:00 -0400643 if (ACPI_FAILURE(status))
644 ACPI_EXCEPTION((AE_INFO, status,
645 "Installing notify handler failed"));
Len Brown4be44fc2005-08-05 00:44:28 -0400646 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Bob Moore83135242006-10-03 00:00:00 -0400649ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651/*******************************************************************************
652 *
653 * FUNCTION: acpi_remove_gpe_handler
654 *
Robert Moore44f6c012005-04-18 22:49:35 -0400655 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
656 * defined GPEs)
657 * gpe_number - The event to remove a handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * Address - Address of the handler
659 *
660 * RETURN: Status
661 *
662 * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
663 *
664 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400666acpi_remove_gpe_handler(acpi_handle gpe_device,
667 u32 gpe_number, acpi_event_handler address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Len Brown4be44fc2005-08-05 00:44:28 -0400669 struct acpi_gpe_event_info *gpe_event_info;
670 struct acpi_handler_info *handler;
671 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500672 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Bob Mooreb229cf92006-04-21 17:15:00 -0400674 ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /* Parameter validation */
677
678 if (!address) {
Len Brown4be44fc2005-08-05 00:44:28 -0400679 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Len Brown4be44fc2005-08-05 00:44:28 -0400682 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
683 if (ACPI_FAILURE(status)) {
684 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 /* Ensure that we have a valid GPE number */
688
Len Brown4be44fc2005-08-05 00:44:28 -0400689 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!gpe_event_info) {
691 status = AE_BAD_PARAMETER;
692 goto unlock_and_exit;
693 }
694
695 /* Make sure that a handler is indeed installed */
696
Len Brown4be44fc2005-08-05 00:44:28 -0400697 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
698 ACPI_GPE_DISPATCH_HANDLER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 status = AE_NOT_EXIST;
700 goto unlock_and_exit;
701 }
702
703 /* Make sure that the installed handler is the same */
704
705 if (gpe_event_info->dispatch.handler->address != address) {
706 status = AE_BAD_PARAMETER;
707 goto unlock_and_exit;
708 }
709
710 /* Disable the GPE before removing the handler */
711
Len Brown4be44fc2005-08-05 00:44:28 -0400712 status = acpi_ev_disable_gpe(gpe_event_info);
713 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto unlock_and_exit;
715 }
716
717 /* Make sure all deferred tasks are completed */
718
Len Brown4be44fc2005-08-05 00:44:28 -0400719 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 acpi_os_wait_events_complete(NULL);
Len Brown4be44fc2005-08-05 00:44:28 -0400721 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
722 if (ACPI_FAILURE(status)) {
723 return_ACPI_STATUS(status);
724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /* Remove the handler */
727
Len Brown4be44fc2005-08-05 00:44:28 -0400728 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 handler = gpe_event_info->dispatch.handler;
730
731 /* Restore Method node (if any), set dispatch flags */
732
733 gpe_event_info->dispatch.method_node = handler->method_node;
Len Brown4be44fc2005-08-05 00:44:28 -0400734 gpe_event_info->flags &= ~ACPI_GPE_DISPATCH_MASK; /* Clear bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (handler->method_node) {
736 gpe_event_info->flags |= ACPI_GPE_DISPATCH_METHOD;
737 }
Len Brown4be44fc2005-08-05 00:44:28 -0400738 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /* Now we can free the handler object */
741
Bob Moore83135242006-10-03 00:00:00 -0400742 ACPI_FREE(handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Len Brown4be44fc2005-08-05 00:44:28 -0400744 unlock_and_exit:
745 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
746 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Bob Moore83135242006-10-03 00:00:00 -0400749ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751/*******************************************************************************
752 *
753 * FUNCTION: acpi_acquire_global_lock
754 *
755 * PARAMETERS: Timeout - How long the caller is willing to wait
Robert Moore44f6c012005-04-18 22:49:35 -0400756 * Handle - Where the handle to the lock is returned
757 * (if acquired)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
759 * RETURN: Status
760 *
761 * DESCRIPTION: Acquire the ACPI Global Lock
762 *
Bob Mooreba886cd2008-04-10 19:06:37 +0400763 * Note: Allows callers with the same thread ID to acquire the global lock
764 * multiple times. In other words, externally, the behavior of the global lock
765 * is identical to an AML mutex. On the first acquire, a new handle is
766 * returned. On any subsequent calls to acquire by the same thread, the same
767 * handle is returned.
768 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400770acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Len Brown4be44fc2005-08-05 00:44:28 -0400772 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (!handle) {
775 return (AE_BAD_PARAMETER);
776 }
777
Len Brown4d2acd92007-05-09 22:56:38 -0400778 /* Must lock interpreter to prevent race conditions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Len Brown4d2acd92007-05-09 22:56:38 -0400780 acpi_ex_enter_interpreter();
Bob Mooreba886cd2008-04-10 19:06:37 +0400781
782 status = acpi_ex_acquire_mutex_object(timeout,
783 acpi_gbl_global_lock_mutex,
784 acpi_os_get_thread_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Len Brown4be44fc2005-08-05 00:44:28 -0400786 if (ACPI_SUCCESS(status)) {
Bob Mooreba886cd2008-04-10 19:06:37 +0400787
Bob Mooree5567af2008-04-10 19:06:38 +0400788 /* Return the global lock handle (updated in acpi_ev_acquire_global_lock) */
Bob Mooreba886cd2008-04-10 19:06:37 +0400789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 *handle = acpi_gbl_global_lock_handle;
791 }
792
Bob Mooreba886cd2008-04-10 19:06:37 +0400793 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return (status);
795}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Bob Moore83135242006-10-03 00:00:00 -0400797ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799/*******************************************************************************
800 *
801 * FUNCTION: acpi_release_global_lock
802 *
803 * PARAMETERS: Handle - Returned from acpi_acquire_global_lock
804 *
805 * RETURN: Status
806 *
Robert Moore44f6c012005-04-18 22:49:35 -0400807 * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 *
809 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400810acpi_status acpi_release_global_lock(u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Len Brown4be44fc2005-08-05 00:44:28 -0400812 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Bob Mooref02e9fa2008-04-10 19:06:37 +0400814 if (!handle || (handle != acpi_gbl_global_lock_handle)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return (AE_NOT_ACQUIRED);
816 }
817
Bob Mooreba886cd2008-04-10 19:06:37 +0400818 status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return (status);
820}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Bob Moore83135242006-10-03 00:00:00 -0400822ACPI_EXPORT_SYMBOL(acpi_release_global_lock)