blob: 76cfe71c6b287cdd823ea216b5a95c6f2a06987e [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 Moore77848132012-01-12 13:27:23 +08008 * Copyright (C) 2000 - 2012, 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
Paul Gortmaker214f2c92011-10-26 16:22:14 -040044#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "acnamesp.h"
48#include "acevents.h"
49#include "acinterp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define _COMPONENT ACPI_EVENTS
Len Brown4be44fc2005-08-05 00:44:28 -040052ACPI_MODULE_NAME("evxface")
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*******************************************************************************
56 *
57 * FUNCTION: acpi_install_notify_handler
58 *
Lv Zheng75c80442012-12-19 05:36:49 +000059 * PARAMETERS: device - The device for which notifies will be handled
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * handler_type - The type of handler:
Bob Moore86ed4bc2012-05-03 11:08:19 +080061 * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
62 * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
63 * ACPI_ALL_NOTIFY: Both System and Device
Lv Zheng75c80442012-12-19 05:36:49 +000064 * handler - Address of the handler
65 * context - Value passed to the handler on each GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *
67 * RETURN: Status
68 *
Bob Moore86ed4bc2012-05-03 11:08:19 +080069 * DESCRIPTION: Install a handler for notifications on an ACPI Device,
70 * thermal_zone, or Processor object.
71 *
72 * NOTES: The Root namespace object may have only one handler for each
73 * type of notify (System/Device). Device/Thermal/Processor objects
74 * may have one device notify handler, and multiple system notify
75 * handlers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070078acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040079acpi_install_notify_handler(acpi_handle device,
80 u32 handler_type,
81 acpi_notify_handler handler, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Bob Moore86ed4bc2012-05-03 11:08:19 +080083 struct acpi_namespace_node *node =
84 ACPI_CAST_PTR(struct acpi_namespace_node, device);
Len Brown4be44fc2005-08-05 00:44:28 -040085 union acpi_operand_object *obj_desc;
Bob Moore86ed4bc2012-05-03 11:08:19 +080086 union acpi_operand_object *handler_obj;
Len Brown4be44fc2005-08-05 00:44:28 -040087 acpi_status status;
Bob Moore86ed4bc2012-05-03 11:08:19 +080088 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Bob Mooreb229cf92006-04-21 17:15:00 -040090 ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* Parameter validation */
93
Bob Moore86ed4bc2012-05-03 11:08:19 +080094 if ((!device) || (!handler) || (!handler_type) ||
95 (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
Len Brown4be44fc2005-08-05 00:44:28 -040096 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
Len Brown4be44fc2005-08-05 00:44:28 -040099 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
100 if (ACPI_FAILURE(status)) {
101 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /*
105 * Root Object:
106 * Registering a notify handler on the root object indicates that the
Bob Moore9f15fc62008-11-12 16:01:56 +0800107 * caller wishes to receive notifications for all objects. Note that
Bob Moore86ed4bc2012-05-03 11:08:19 +0800108 * only one global handler can be registered per notify type.
109 * Ensure that a handler is not already installed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
111 if (device == ACPI_ROOT_OBJECT) {
Bob Moore86ed4bc2012-05-03 11:08:19 +0800112 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
113 if (handler_type & (i + 1)) {
114 if (acpi_gbl_global_notify[i].handler) {
Rafael J. Wysocki3f0be672010-02-17 23:42:59 +0100115 status = AE_ALREADY_EXISTS;
116 goto unlock_and_exit;
117 }
118
Bob Moore86ed4bc2012-05-03 11:08:19 +0800119 acpi_gbl_global_notify[i].handler = handler;
120 acpi_gbl_global_notify[i].context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 }
123
Bob Moore86ed4bc2012-05-03 11:08:19 +0800124 goto unlock_and_exit; /* Global notify handler installed, all done */
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Bob Moore86ed4bc2012-05-03 11:08:19 +0800127 /*
128 * All Other Objects:
129 * Caller will only receive notifications specific to the target
130 * object. Note that only certain object types are allowed to
131 * receive notifications.
132 */
133
134 /* Are Notifies allowed on this object? */
135
136 if (!acpi_ev_is_notify_object(node)) {
137 status = AE_TYPE;
138 goto unlock_and_exit;
139 }
140
141 /* Check for an existing internal object, might not exist */
142
143 obj_desc = acpi_ns_get_attached_object(node);
144 if (!obj_desc) {
145
146 /* Create a new object */
147
148 obj_desc = acpi_ut_create_internal_object(node->type);
149 if (!obj_desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 status = AE_NO_MEMORY;
151 goto unlock_and_exit;
152 }
153
Bob Moore86ed4bc2012-05-03 11:08:19 +0800154 /* Attach new object to the Node, remove local reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Bob Moore86ed4bc2012-05-03 11:08:19 +0800156 status = acpi_ns_attach_object(device, obj_desc, node->type);
157 acpi_ut_remove_reference(obj_desc);
158 if (ACPI_FAILURE(status)) {
159 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161 }
162
Bob Moore86ed4bc2012-05-03 11:08:19 +0800163 /* Ensure that the handler is not already installed in the lists */
164
165 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
166 if (handler_type & (i + 1)) {
167 handler_obj = obj_desc->common_notify.notify_list[i];
168 while (handler_obj) {
169 if (handler_obj->notify.handler == handler) {
170 status = AE_ALREADY_EXISTS;
171 goto unlock_and_exit;
172 }
173
174 handler_obj = handler_obj->notify.next[i];
175 }
176 }
177 }
178
179 /* Create and populate a new notify handler object */
180
181 handler_obj = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
182 if (!handler_obj) {
183 status = AE_NO_MEMORY;
184 goto unlock_and_exit;
185 }
186
187 handler_obj->notify.node = node;
188 handler_obj->notify.handler_type = handler_type;
189 handler_obj->notify.handler = handler;
190 handler_obj->notify.context = context;
191
192 /* Install the handler at the list head(s) */
193
194 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
195 if (handler_type & (i + 1)) {
196 handler_obj->notify.next[i] =
197 obj_desc->common_notify.notify_list[i];
198
199 obj_desc->common_notify.notify_list[i] = handler_obj;
200 }
201 }
202
203 /* Add an extra reference if handler was installed in both lists */
204
205 if (handler_type == ACPI_ALL_NOTIFY) {
206 acpi_ut_add_reference(handler_obj);
207 }
208
209unlock_and_exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400210 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
211 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Bob Moore83135242006-10-03 00:00:00 -0400214ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216/*******************************************************************************
217 *
218 * FUNCTION: acpi_remove_notify_handler
219 *
Lv Zheng75c80442012-12-19 05:36:49 +0000220 * PARAMETERS: device - The device for which the handler is installed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * handler_type - The type of handler:
Bob Moore86ed4bc2012-05-03 11:08:19 +0800222 * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
223 * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
224 * ACPI_ALL_NOTIFY: Both System and Device
Lv Zheng75c80442012-12-19 05:36:49 +0000225 * handler - Address of the handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
227 * RETURN: Status
228 *
229 * DESCRIPTION: Remove a handler for notifies on an ACPI device
230 *
231 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400233acpi_remove_notify_handler(acpi_handle device,
234 u32 handler_type, acpi_notify_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Bob Moore86ed4bc2012-05-03 11:08:19 +0800236 struct acpi_namespace_node *node =
237 ACPI_CAST_PTR(struct acpi_namespace_node, device);
Len Brown4be44fc2005-08-05 00:44:28 -0400238 union acpi_operand_object *obj_desc;
Bob Moore86ed4bc2012-05-03 11:08:19 +0800239 union acpi_operand_object *handler_obj;
240 union acpi_operand_object *previous_handler_obj;
Len Brown4be44fc2005-08-05 00:44:28 -0400241 acpi_status status;
Bob Moore86ed4bc2012-05-03 11:08:19 +0800242 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Bob Mooreb229cf92006-04-21 17:15:00 -0400244 ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* Parameter validation */
247
Bob Moore86ed4bc2012-05-03 11:08:19 +0800248 if ((!device) || (!handler) || (!handler_type) ||
249 (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
250 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Lv Zheng75c80442012-12-19 05:36:49 +0000252 /* Make sure all deferred notify tasks are completed */
Bob Moore86ed4bc2012-05-03 11:08:19 +0800253
Lin Mingbd6f10a2012-05-22 16:43:49 +0800254 acpi_os_wait_events_complete();
Rafael J. Wysocki3f0be672010-02-17 23:42:59 +0100255
Len Brown4be44fc2005-08-05 00:44:28 -0400256 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
257 if (ACPI_FAILURE(status)) {
Bob Moore86ed4bc2012-05-03 11:08:19 +0800258 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Bob Moore86ed4bc2012-05-03 11:08:19 +0800261 /* Root Object. Global handlers are removed here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Bob Moore86ed4bc2012-05-03 11:08:19 +0800263 if (device == ACPI_ROOT_OBJECT) {
264 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
265 if (handler_type & (i + 1)) {
266 if (!acpi_gbl_global_notify[i].handler ||
267 (acpi_gbl_global_notify[i].handler !=
268 handler)) {
269 status = AE_NOT_EXIST;
270 goto unlock_and_exit;
271 }
272
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
274 "Removing global notify handler\n"));
275
276 acpi_gbl_global_notify[i].handler = NULL;
277 acpi_gbl_global_notify[i].context = NULL;
278 }
279 }
280
Bob Mooref6dd9222006-07-07 20:44:38 -0400281 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
Bob Moore86ed4bc2012-05-03 11:08:19 +0800284 /* All other objects: Are Notifies allowed on this object? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Bob Moore86ed4bc2012-05-03 11:08:19 +0800286 if (!acpi_ev_is_notify_object(node)) {
287 status = AE_TYPE;
288 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Bob Moore86ed4bc2012-05-03 11:08:19 +0800291 /* Must have an existing internal object */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Bob Moore86ed4bc2012-05-03 11:08:19 +0800293 obj_desc = acpi_ns_get_attached_object(node);
294 if (!obj_desc) {
295 status = AE_NOT_EXIST;
296 goto unlock_and_exit;
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Bob Moore86ed4bc2012-05-03 11:08:19 +0800299 /* Internal object exists. Find the handler and remove it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Bob Moore86ed4bc2012-05-03 11:08:19 +0800301 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
302 if (handler_type & (i + 1)) {
303 handler_obj = obj_desc->common_notify.notify_list[i];
304 previous_handler_obj = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Bob Moore86ed4bc2012-05-03 11:08:19 +0800306 /* Attempt to find the handler in the handler list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Bob Moore86ed4bc2012-05-03 11:08:19 +0800308 while (handler_obj &&
309 (handler_obj->notify.handler != handler)) {
310 previous_handler_obj = handler_obj;
311 handler_obj = handler_obj->notify.next[i];
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Bob Moore86ed4bc2012-05-03 11:08:19 +0800314 if (!handler_obj) {
Bob Mooref6dd9222006-07-07 20:44:38 -0400315 status = AE_NOT_EXIST;
316 goto unlock_and_exit;
317 }
318
Bob Moore86ed4bc2012-05-03 11:08:19 +0800319 /* Remove the handler object from the list */
320
321 if (previous_handler_obj) { /* Handler is not at the list head */
322 previous_handler_obj->notify.next[i] =
323 handler_obj->notify.next[i];
324 } else { /* Handler is at the list head */
325
326 obj_desc->common_notify.notify_list[i] =
327 handler_obj->notify.next[i];
Rafael J. Wysocki3f0be672010-02-17 23:42:59 +0100328 }
329
Bob Moore86ed4bc2012-05-03 11:08:19 +0800330 acpi_ut_remove_reference(handler_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332 }
333
Bob Moore86ed4bc2012-05-03 11:08:19 +0800334unlock_and_exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400335 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
336 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Bob Moore83135242006-10-03 00:00:00 -0400339ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/*******************************************************************************
342 *
Bob Moore33620c52012-02-14 18:14:27 +0800343 * FUNCTION: acpi_install_exception_handler
344 *
Bob Mooreba494be2012-07-12 09:40:10 +0800345 * PARAMETERS: handler - Pointer to the handler function for the
Bob Moore33620c52012-02-14 18:14:27 +0800346 * event
347 *
348 * RETURN: Status
349 *
350 * DESCRIPTION: Saves the pointer to the handler function
351 *
352 ******************************************************************************/
353#ifdef ACPI_FUTURE_USAGE
354acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
355{
356 acpi_status status;
357
358 ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
359
360 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
361 if (ACPI_FAILURE(status)) {
362 return_ACPI_STATUS(status);
363 }
364
365 /* Don't allow two handlers. */
366
367 if (acpi_gbl_exception_handler) {
368 status = AE_ALREADY_EXISTS;
369 goto cleanup;
370 }
371
372 /* Install the handler */
373
374 acpi_gbl_exception_handler = handler;
375
376 cleanup:
377 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
378 return_ACPI_STATUS(status);
379}
380
381ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
382#endif /* ACPI_FUTURE_USAGE */
383
384#if (!ACPI_REDUCED_HARDWARE)
385/*******************************************************************************
386 *
387 * FUNCTION: acpi_install_global_event_handler
388 *
Bob Mooreba494be2012-07-12 09:40:10 +0800389 * PARAMETERS: handler - Pointer to the global event handler function
390 * context - Value passed to the handler on each event
Bob Moore33620c52012-02-14 18:14:27 +0800391 *
392 * RETURN: Status
393 *
394 * DESCRIPTION: Saves the pointer to the handler function. The global handler
395 * is invoked upon each incoming GPE and Fixed Event. It is
396 * invoked at interrupt level at the time of the event dispatch.
397 * Can be used to update event counters, etc.
398 *
399 ******************************************************************************/
400acpi_status
Lv Zheng644ef742012-10-31 02:25:36 +0000401acpi_install_global_event_handler(acpi_gbl_event_handler handler, void *context)
Bob Moore33620c52012-02-14 18:14:27 +0800402{
403 acpi_status status;
404
405 ACPI_FUNCTION_TRACE(acpi_install_global_event_handler);
406
407 /* Parameter validation */
408
409 if (!handler) {
410 return_ACPI_STATUS(AE_BAD_PARAMETER);
411 }
412
413 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
414 if (ACPI_FAILURE(status)) {
415 return_ACPI_STATUS(status);
416 }
417
418 /* Don't allow two handlers. */
419
420 if (acpi_gbl_global_event_handler) {
421 status = AE_ALREADY_EXISTS;
422 goto cleanup;
423 }
424
425 acpi_gbl_global_event_handler = handler;
426 acpi_gbl_global_event_handler_context = context;
427
428 cleanup:
429 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
430 return_ACPI_STATUS(status);
431}
432
433ACPI_EXPORT_SYMBOL(acpi_install_global_event_handler)
434
435/*******************************************************************************
436 *
437 * FUNCTION: acpi_install_fixed_event_handler
438 *
Bob Mooreba494be2012-07-12 09:40:10 +0800439 * PARAMETERS: event - Event type to enable.
440 * handler - Pointer to the handler function for the
Bob Moore33620c52012-02-14 18:14:27 +0800441 * event
Bob Mooreba494be2012-07-12 09:40:10 +0800442 * context - Value passed to the handler on each GPE
Bob Moore33620c52012-02-14 18:14:27 +0800443 *
444 * RETURN: Status
445 *
446 * DESCRIPTION: Saves the pointer to the handler function and then enables the
447 * event.
448 *
449 ******************************************************************************/
450acpi_status
451acpi_install_fixed_event_handler(u32 event,
452 acpi_event_handler handler, void *context)
453{
454 acpi_status status;
455
456 ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
457
458 /* Parameter validation */
459
460 if (event > ACPI_EVENT_MAX) {
461 return_ACPI_STATUS(AE_BAD_PARAMETER);
462 }
463
464 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
465 if (ACPI_FAILURE(status)) {
466 return_ACPI_STATUS(status);
467 }
468
469 /* Don't allow two handlers. */
470
471 if (NULL != acpi_gbl_fixed_event_handlers[event].handler) {
472 status = AE_ALREADY_EXISTS;
473 goto cleanup;
474 }
475
476 /* Install the handler before enabling the event */
477
478 acpi_gbl_fixed_event_handlers[event].handler = handler;
479 acpi_gbl_fixed_event_handlers[event].context = context;
480
481 status = acpi_clear_event(event);
482 if (ACPI_SUCCESS(status))
483 status = acpi_enable_event(event, 0);
484 if (ACPI_FAILURE(status)) {
485 ACPI_WARNING((AE_INFO, "Could not enable fixed event 0x%X",
486 event));
487
488 /* Remove the handler */
489
490 acpi_gbl_fixed_event_handlers[event].handler = NULL;
491 acpi_gbl_fixed_event_handlers[event].context = NULL;
492 } else {
493 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
494 "Enabled fixed event %X, Handler=%p\n", event,
495 handler));
496 }
497
498 cleanup:
499 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
500 return_ACPI_STATUS(status);
501}
502
503ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
504
505/*******************************************************************************
506 *
507 * FUNCTION: acpi_remove_fixed_event_handler
508 *
Bob Mooreba494be2012-07-12 09:40:10 +0800509 * PARAMETERS: event - Event type to disable.
510 * handler - Address of the handler
Bob Moore33620c52012-02-14 18:14:27 +0800511 *
512 * RETURN: Status
513 *
514 * DESCRIPTION: Disables the event and unregisters the event handler.
515 *
516 ******************************************************************************/
517acpi_status
518acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
519{
520 acpi_status status = AE_OK;
521
522 ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
523
524 /* Parameter validation */
525
526 if (event > ACPI_EVENT_MAX) {
527 return_ACPI_STATUS(AE_BAD_PARAMETER);
528 }
529
530 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
531 if (ACPI_FAILURE(status)) {
532 return_ACPI_STATUS(status);
533 }
534
535 /* Disable the event before removing the handler */
536
537 status = acpi_disable_event(event, 0);
538
539 /* Always Remove the handler */
540
541 acpi_gbl_fixed_event_handlers[event].handler = NULL;
542 acpi_gbl_fixed_event_handlers[event].context = NULL;
543
544 if (ACPI_FAILURE(status)) {
545 ACPI_WARNING((AE_INFO,
546 "Could not write to fixed event enable register 0x%X",
547 event));
548 } else {
549 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabled fixed event %X\n",
550 event));
551 }
552
553 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
554 return_ACPI_STATUS(status);
555}
556
557ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
558
559/*******************************************************************************
560 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * FUNCTION: acpi_install_gpe_handler
562 *
Robert Moore44f6c012005-04-18 22:49:35 -0400563 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
564 * defined GPEs)
565 * gpe_number - The GPE number within the GPE block
Bob Mooreba494be2012-07-12 09:40:10 +0800566 * type - Whether this GPE should be treated as an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * edge- or level-triggered interrupt.
Bob Mooreba494be2012-07-12 09:40:10 +0800568 * address - Address of the handler
569 * context - Value passed to the handler on each GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 *
571 * RETURN: Status
572 *
573 * DESCRIPTION: Install a handler for a General Purpose Event.
574 *
575 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400577acpi_install_gpe_handler(acpi_handle gpe_device,
578 u32 gpe_number,
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800579 u32 type, acpi_gpe_handler address, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Len Brown4be44fc2005-08-05 00:44:28 -0400581 struct acpi_gpe_event_info *gpe_event_info;
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800582 struct acpi_gpe_handler_info *handler;
Len Brown4be44fc2005-08-05 00:44:28 -0400583 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500584 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Bob Mooreb229cf92006-04-21 17:15:00 -0400586 ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* Parameter validation */
589
Lin Ming0f849d22010-04-06 14:52:37 +0800590 if ((!address) || (type & ~ACPI_GPE_XRUPT_TYPE_MASK)) {
591 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Len Brown4be44fc2005-08-05 00:44:28 -0400594 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
595 if (ACPI_FAILURE(status)) {
Lin Ming0f849d22010-04-06 14:52:37 +0800596 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
Lv Zheng75c80442012-12-19 05:36:49 +0000599 /* Allocate and init handler object (before lock) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800601 handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_handler_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!handler) {
603 status = AE_NO_MEMORY;
Bob Mooref6dd9222006-07-07 20:44:38 -0400604 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200607 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
608
609 /* Ensure that we have a valid GPE number */
610
611 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
612 if (!gpe_event_info) {
613 status = AE_BAD_PARAMETER;
614 goto free_and_exit;
615 }
616
617 /* Make sure that there isn't a handler there already */
618
619 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
620 ACPI_GPE_DISPATCH_HANDLER) {
621 status = AE_ALREADY_EXISTS;
622 goto free_and_exit;
623 }
624
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200625
Len Brown4be44fc2005-08-05 00:44:28 -0400626 handler->address = address;
627 handler->context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 handler->method_node = gpe_event_info->dispatch.method_node;
Lin Ming3a378982010-12-13 13:36:15 +0800629 handler->original_flags = gpe_event_info->flags &
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200630 (ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
631
632 /*
Lv Zheng75c80442012-12-19 05:36:49 +0000633 * If the GPE is associated with a method, it may have been enabled
Rafael J. Wysockia2100802010-09-16 00:30:43 +0200634 * automatically during initialization, in which case it has to be
635 * disabled now to avoid spurious execution of the handler.
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200636 */
637
Lin Ming3a378982010-12-13 13:36:15 +0800638 if ((handler->original_flags & ACPI_GPE_DISPATCH_METHOD)
Rafael J. Wysockia2100802010-09-16 00:30:43 +0200639 && gpe_event_info->runtime_count) {
Lin Ming3a378982010-12-13 13:36:15 +0800640 handler->originally_enabled = 1;
641 (void)acpi_ev_remove_gpe_reference(gpe_event_info);
Rafael J. Wysockia2100802010-09-16 00:30:43 +0200642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* Install the handler */
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 gpe_event_info->dispatch.handler = handler;
647
Lv Zheng75c80442012-12-19 05:36:49 +0000648 /* Setup up dispatch flags to indicate handler (vs. method/notify) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Bob Moored4913dc2009-03-06 10:05:18 +0800650 gpe_event_info->flags &=
651 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
653
Len Brown4be44fc2005-08-05 00:44:28 -0400654 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Lin Ming0f849d22010-04-06 14:52:37 +0800656unlock_and_exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400657 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
658 return_ACPI_STATUS(status);
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200659
660free_and_exit:
661 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
662 ACPI_FREE(handler);
663 goto unlock_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Bob Moore83135242006-10-03 00:00:00 -0400666ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668/*******************************************************************************
669 *
670 * FUNCTION: acpi_remove_gpe_handler
671 *
Robert Moore44f6c012005-04-18 22:49:35 -0400672 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
673 * defined GPEs)
674 * gpe_number - The event to remove a handler
Bob Mooreba494be2012-07-12 09:40:10 +0800675 * address - Address of the handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 *
677 * RETURN: Status
678 *
679 * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
680 *
681 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400683acpi_remove_gpe_handler(acpi_handle gpe_device,
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800684 u32 gpe_number, acpi_gpe_handler address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Len Brown4be44fc2005-08-05 00:44:28 -0400686 struct acpi_gpe_event_info *gpe_event_info;
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800687 struct acpi_gpe_handler_info *handler;
Len Brown4be44fc2005-08-05 00:44:28 -0400688 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500689 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Bob Mooreb229cf92006-04-21 17:15:00 -0400691 ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* Parameter validation */
694
695 if (!address) {
Len Brown4be44fc2005-08-05 00:44:28 -0400696 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Lv Zheng75c80442012-12-19 05:36:49 +0000699 /* Make sure all deferred GPE tasks are completed */
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200700
Lin Mingbd6f10a2012-05-22 16:43:49 +0800701 acpi_os_wait_events_complete();
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200702
Len Brown4be44fc2005-08-05 00:44:28 -0400703 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
704 if (ACPI_FAILURE(status)) {
705 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200708 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Ensure that we have a valid GPE number */
711
Len Brown4be44fc2005-08-05 00:44:28 -0400712 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!gpe_event_info) {
714 status = AE_BAD_PARAMETER;
715 goto unlock_and_exit;
716 }
717
718 /* Make sure that a handler is indeed installed */
719
Len Brown4be44fc2005-08-05 00:44:28 -0400720 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
721 ACPI_GPE_DISPATCH_HANDLER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 status = AE_NOT_EXIST;
723 goto unlock_and_exit;
724 }
725
726 /* Make sure that the installed handler is the same */
727
728 if (gpe_event_info->dispatch.handler->address != address) {
729 status = AE_BAD_PARAMETER;
730 goto unlock_and_exit;
731 }
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* Remove the handler */
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 handler = gpe_event_info->dispatch.handler;
736
737 /* Restore Method node (if any), set dispatch flags */
738
739 gpe_event_info->dispatch.method_node = handler->method_node;
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200740 gpe_event_info->flags &=
741 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
Lin Ming3a378982010-12-13 13:36:15 +0800742 gpe_event_info->flags |= handler->original_flags;
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200743
744 /*
Rafael J. Wysockia2100802010-09-16 00:30:43 +0200745 * If the GPE was previously associated with a method and it was
746 * enabled, it should be enabled at this point to restore the
747 * post-initialization configuration.
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200748 */
749
Lin Ming3a378982010-12-13 13:36:15 +0800750 if ((handler->original_flags & ACPI_GPE_DISPATCH_METHOD)
751 && handler->originally_enabled)
752 (void)acpi_ev_add_gpe_reference(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Now we can free the handler object */
755
Bob Moore83135242006-10-03 00:00:00 -0400756 ACPI_FREE(handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200758unlock_and_exit:
759 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
760
Len Brown4be44fc2005-08-05 00:44:28 -0400761 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
762 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Bob Moore83135242006-10-03 00:00:00 -0400765ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767/*******************************************************************************
768 *
769 * FUNCTION: acpi_acquire_global_lock
770 *
Bob Mooreba494be2012-07-12 09:40:10 +0800771 * PARAMETERS: timeout - How long the caller is willing to wait
772 * handle - Where the handle to the lock is returned
Robert Moore44f6c012005-04-18 22:49:35 -0400773 * (if acquired)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
775 * RETURN: Status
776 *
777 * DESCRIPTION: Acquire the ACPI Global Lock
778 *
Bob Mooreba886cd2008-04-10 19:06:37 +0400779 * Note: Allows callers with the same thread ID to acquire the global lock
780 * multiple times. In other words, externally, the behavior of the global lock
781 * is identical to an AML mutex. On the first acquire, a new handle is
782 * returned. On any subsequent calls to acquire by the same thread, the same
783 * handle is returned.
784 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400786acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Len Brown4be44fc2005-08-05 00:44:28 -0400788 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (!handle) {
791 return (AE_BAD_PARAMETER);
792 }
793
Len Brown4d2acd92007-05-09 22:56:38 -0400794 /* Must lock interpreter to prevent race conditions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Len Brown4d2acd92007-05-09 22:56:38 -0400796 acpi_ex_enter_interpreter();
Bob Mooreba886cd2008-04-10 19:06:37 +0400797
798 status = acpi_ex_acquire_mutex_object(timeout,
799 acpi_gbl_global_lock_mutex,
800 acpi_os_get_thread_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Len Brown4be44fc2005-08-05 00:44:28 -0400802 if (ACPI_SUCCESS(status)) {
Bob Mooreba886cd2008-04-10 19:06:37 +0400803
Bob Mooree5567af2008-04-10 19:06:38 +0400804 /* Return the global lock handle (updated in acpi_ev_acquire_global_lock) */
Bob Mooreba886cd2008-04-10 19:06:37 +0400805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *handle = acpi_gbl_global_lock_handle;
807 }
808
Bob Mooreba886cd2008-04-10 19:06:37 +0400809 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return (status);
811}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Bob Moore83135242006-10-03 00:00:00 -0400813ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815/*******************************************************************************
816 *
817 * FUNCTION: acpi_release_global_lock
818 *
Bob Mooreba494be2012-07-12 09:40:10 +0800819 * PARAMETERS: handle - Returned from acpi_acquire_global_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
821 * RETURN: Status
822 *
Robert Moore44f6c012005-04-18 22:49:35 -0400823 * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 *
825 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400826acpi_status acpi_release_global_lock(u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Len Brown4be44fc2005-08-05 00:44:28 -0400828 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Bob Mooref02e9fa2008-04-10 19:06:37 +0400830 if (!handle || (handle != acpi_gbl_global_lock_handle)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return (AE_NOT_ACQUIRED);
832 }
833
Bob Mooreba886cd2008-04-10 19:06:37 +0400834 status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return (status);
836}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Bob Moore83135242006-10-03 00:00:00 -0400838ACPI_EXPORT_SYMBOL(acpi_release_global_lock)
Bob Moore33620c52012-02-14 18:14:27 +0800839#endif /* !ACPI_REDUCED_HARDWARE */