evxface.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /******************************************************************************
  2. *
  3. * Module Name: evxface - External interfaces for ACPI events
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #define EXPORT_ACPI_INTERFACES
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #include "acnamesp.h"
  46. #include "acevents.h"
  47. #include "acinterp.h"
  48. #define _COMPONENT ACPI_EVENTS
  49. ACPI_MODULE_NAME("evxface")
  50. #if (!ACPI_REDUCED_HARDWARE)
  51. /* Local prototypes */
  52. static acpi_status
  53. acpi_ev_install_gpe_handler(acpi_handle gpe_device,
  54. u32 gpe_number,
  55. u32 type,
  56. u8 is_raw_handler,
  57. acpi_gpe_handler address, void *context);
  58. #endif
  59. /*******************************************************************************
  60. *
  61. * FUNCTION: acpi_install_notify_handler
  62. *
  63. * PARAMETERS: device - The device for which notifies will be handled
  64. * handler_type - The type of handler:
  65. * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
  66. * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
  67. * ACPI_ALL_NOTIFY: Both System and Device
  68. * handler - Address of the handler
  69. * context - Value passed to the handler on each GPE
  70. *
  71. * RETURN: Status
  72. *
  73. * DESCRIPTION: Install a handler for notifications on an ACPI Device,
  74. * thermal_zone, or Processor object.
  75. *
  76. * NOTES: The Root namespace object may have only one handler for each
  77. * type of notify (System/Device). Device/Thermal/Processor objects
  78. * may have one device notify handler, and multiple system notify
  79. * handlers.
  80. *
  81. ******************************************************************************/
  82. acpi_status
  83. acpi_install_notify_handler(acpi_handle device,
  84. u32 handler_type,
  85. acpi_notify_handler handler, void *context)
  86. {
  87. struct acpi_namespace_node *node =
  88. ACPI_CAST_PTR(struct acpi_namespace_node, device);
  89. union acpi_operand_object *obj_desc;
  90. union acpi_operand_object *handler_obj;
  91. acpi_status status;
  92. u32 i;
  93. ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
  94. /* Parameter validation */
  95. if ((!device) || (!handler) || (!handler_type) ||
  96. (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
  97. return_ACPI_STATUS(AE_BAD_PARAMETER);
  98. }
  99. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  100. if (ACPI_FAILURE(status)) {
  101. return_ACPI_STATUS(status);
  102. }
  103. /*
  104. * Root Object:
  105. * Registering a notify handler on the root object indicates that the
  106. * caller wishes to receive notifications for all objects. Note that
  107. * only one global handler can be registered per notify type.
  108. * Ensure that a handler is not already installed.
  109. */
  110. if (device == ACPI_ROOT_OBJECT) {
  111. for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
  112. if (handler_type & (i + 1)) {
  113. if (acpi_gbl_global_notify[i].handler) {
  114. status = AE_ALREADY_EXISTS;
  115. goto unlock_and_exit;
  116. }
  117. acpi_gbl_global_notify[i].handler = handler;
  118. acpi_gbl_global_notify[i].context = context;
  119. }
  120. }
  121. goto unlock_and_exit; /* Global notify handler installed, all done */
  122. }
  123. /*
  124. * All Other Objects:
  125. * Caller will only receive notifications specific to the target
  126. * object. Note that only certain object types are allowed to
  127. * receive notifications.
  128. */
  129. /* Are Notifies allowed on this object? */
  130. if (!acpi_ev_is_notify_object(node)) {
  131. status = AE_TYPE;
  132. goto unlock_and_exit;
  133. }
  134. /* Check for an existing internal object, might not exist */
  135. obj_desc = acpi_ns_get_attached_object(node);
  136. if (!obj_desc) {
  137. /* Create a new object */
  138. obj_desc = acpi_ut_create_internal_object(node->type);
  139. if (!obj_desc) {
  140. status = AE_NO_MEMORY;
  141. goto unlock_and_exit;
  142. }
  143. /* Attach new object to the Node, remove local reference */
  144. status = acpi_ns_attach_object(device, obj_desc, node->type);
  145. acpi_ut_remove_reference(obj_desc);
  146. if (ACPI_FAILURE(status)) {
  147. goto unlock_and_exit;
  148. }
  149. }
  150. /* Ensure that the handler is not already installed in the lists */
  151. for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
  152. if (handler_type & (i + 1)) {
  153. handler_obj = obj_desc->common_notify.notify_list[i];
  154. while (handler_obj) {
  155. if (handler_obj->notify.handler == handler) {
  156. status = AE_ALREADY_EXISTS;
  157. goto unlock_and_exit;
  158. }
  159. handler_obj = handler_obj->notify.next[i];
  160. }
  161. }
  162. }
  163. /* Create and populate a new notify handler object */
  164. handler_obj = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
  165. if (!handler_obj) {
  166. status = AE_NO_MEMORY;
  167. goto unlock_and_exit;
  168. }
  169. handler_obj->notify.node = node;
  170. handler_obj->notify.handler_type = handler_type;
  171. handler_obj->notify.handler = handler;
  172. handler_obj->notify.context = context;
  173. /* Install the handler at the list head(s) */
  174. for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
  175. if (handler_type & (i + 1)) {
  176. handler_obj->notify.next[i] =
  177. obj_desc->common_notify.notify_list[i];
  178. obj_desc->common_notify.notify_list[i] = handler_obj;
  179. }
  180. }
  181. /* Add an extra reference if handler was installed in both lists */
  182. if (handler_type == ACPI_ALL_NOTIFY) {
  183. acpi_ut_add_reference(handler_obj);
  184. }
  185. unlock_and_exit:
  186. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  187. return_ACPI_STATUS(status);
  188. }
  189. ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
  190. /*******************************************************************************
  191. *
  192. * FUNCTION: acpi_remove_notify_handler
  193. *
  194. * PARAMETERS: device - The device for which the handler is installed
  195. * handler_type - The type of handler:
  196. * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
  197. * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
  198. * ACPI_ALL_NOTIFY: Both System and Device
  199. * handler - Address of the handler
  200. *
  201. * RETURN: Status
  202. *
  203. * DESCRIPTION: Remove a handler for notifies on an ACPI device
  204. *
  205. ******************************************************************************/
  206. acpi_status
  207. acpi_remove_notify_handler(acpi_handle device,
  208. u32 handler_type, acpi_notify_handler handler)
  209. {
  210. struct acpi_namespace_node *node =
  211. ACPI_CAST_PTR(struct acpi_namespace_node, device);
  212. union acpi_operand_object *obj_desc;
  213. union acpi_operand_object *handler_obj;
  214. union acpi_operand_object *previous_handler_obj;
  215. acpi_status status = AE_OK;
  216. u32 i;
  217. ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
  218. /* Parameter validation */
  219. if ((!device) || (!handler) || (!handler_type) ||
  220. (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
  221. return_ACPI_STATUS(AE_BAD_PARAMETER);
  222. }
  223. /* Root Object. Global handlers are removed here */
  224. if (device == ACPI_ROOT_OBJECT) {
  225. for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
  226. if (handler_type & (i + 1)) {
  227. status =
  228. acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  229. if (ACPI_FAILURE(status)) {
  230. return_ACPI_STATUS(status);
  231. }
  232. if (!acpi_gbl_global_notify[i].handler ||
  233. (acpi_gbl_global_notify[i].handler !=
  234. handler)) {
  235. status = AE_NOT_EXIST;
  236. goto unlock_and_exit;
  237. }
  238. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  239. "Removing global notify handler\n"));
  240. acpi_gbl_global_notify[i].handler = NULL;
  241. acpi_gbl_global_notify[i].context = NULL;
  242. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  243. /* Make sure all deferred notify tasks are completed */
  244. acpi_os_wait_events_complete();
  245. }
  246. }
  247. return_ACPI_STATUS(AE_OK);
  248. }
  249. /* All other objects: Are Notifies allowed on this object? */
  250. if (!acpi_ev_is_notify_object(node)) {
  251. return_ACPI_STATUS(AE_TYPE);
  252. }
  253. /* Must have an existing internal object */
  254. obj_desc = acpi_ns_get_attached_object(node);
  255. if (!obj_desc) {
  256. return_ACPI_STATUS(AE_NOT_EXIST);
  257. }
  258. /* Internal object exists. Find the handler and remove it */
  259. for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
  260. if (handler_type & (i + 1)) {
  261. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  262. if (ACPI_FAILURE(status)) {
  263. return_ACPI_STATUS(status);
  264. }
  265. handler_obj = obj_desc->common_notify.notify_list[i];
  266. previous_handler_obj = NULL;
  267. /* Attempt to find the handler in the handler list */
  268. while (handler_obj &&
  269. (handler_obj->notify.handler != handler)) {
  270. previous_handler_obj = handler_obj;
  271. handler_obj = handler_obj->notify.next[i];
  272. }
  273. if (!handler_obj) {
  274. status = AE_NOT_EXIST;
  275. goto unlock_and_exit;
  276. }
  277. /* Remove the handler object from the list */
  278. if (previous_handler_obj) { /* Handler is not at the list head */
  279. previous_handler_obj->notify.next[i] =
  280. handler_obj->notify.next[i];
  281. } else { /* Handler is at the list head */
  282. obj_desc->common_notify.notify_list[i] =
  283. handler_obj->notify.next[i];
  284. }
  285. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  286. /* Make sure all deferred notify tasks are completed */
  287. acpi_os_wait_events_complete();
  288. acpi_ut_remove_reference(handler_obj);
  289. }
  290. }
  291. return_ACPI_STATUS(status);
  292. unlock_and_exit:
  293. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  294. return_ACPI_STATUS(status);
  295. }
  296. ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
  297. /*******************************************************************************
  298. *
  299. * FUNCTION: acpi_install_exception_handler
  300. *
  301. * PARAMETERS: handler - Pointer to the handler function for the
  302. * event
  303. *
  304. * RETURN: Status
  305. *
  306. * DESCRIPTION: Saves the pointer to the handler function
  307. *
  308. ******************************************************************************/
  309. #ifdef ACPI_FUTURE_USAGE
  310. acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
  311. {
  312. acpi_status status;
  313. ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
  314. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  315. if (ACPI_FAILURE(status)) {
  316. return_ACPI_STATUS(status);
  317. }
  318. /* Don't allow two handlers. */
  319. if (acpi_gbl_exception_handler) {
  320. status = AE_ALREADY_EXISTS;
  321. goto cleanup;
  322. }
  323. /* Install the handler */
  324. acpi_gbl_exception_handler = handler;
  325. cleanup:
  326. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  327. return_ACPI_STATUS(status);
  328. }
  329. ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
  330. #endif
  331. #if (!ACPI_REDUCED_HARDWARE)
  332. /*******************************************************************************
  333. *
  334. * FUNCTION: acpi_install_sci_handler
  335. *
  336. * PARAMETERS: address - Address of the handler
  337. * context - Value passed to the handler on each SCI
  338. *
  339. * RETURN: Status
  340. *
  341. * DESCRIPTION: Install a handler for a System Control Interrupt.
  342. *
  343. ******************************************************************************/
  344. acpi_status acpi_install_sci_handler(acpi_sci_handler address, void *context)
  345. {
  346. struct acpi_sci_handler_info *new_sci_handler;
  347. struct acpi_sci_handler_info *sci_handler;
  348. acpi_cpu_flags flags;
  349. acpi_status status;
  350. ACPI_FUNCTION_TRACE(acpi_install_sci_handler);
  351. if (!address) {
  352. return_ACPI_STATUS(AE_BAD_PARAMETER);
  353. }
  354. /* Allocate and init a handler object */
  355. new_sci_handler = ACPI_ALLOCATE(sizeof(struct acpi_sci_handler_info));
  356. if (!new_sci_handler) {
  357. return_ACPI_STATUS(AE_NO_MEMORY);
  358. }
  359. new_sci_handler->address = address;
  360. new_sci_handler->context = context;
  361. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  362. if (ACPI_FAILURE(status)) {
  363. goto exit;
  364. }
  365. /* Lock list during installation */
  366. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  367. sci_handler = acpi_gbl_sci_handler_list;
  368. /* Ensure handler does not already exist */
  369. while (sci_handler) {
  370. if (address == sci_handler->address) {
  371. status = AE_ALREADY_EXISTS;
  372. goto unlock_and_exit;
  373. }
  374. sci_handler = sci_handler->next;
  375. }
  376. /* Install the new handler into the global list (at head) */
  377. new_sci_handler->next = acpi_gbl_sci_handler_list;
  378. acpi_gbl_sci_handler_list = new_sci_handler;
  379. unlock_and_exit:
  380. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  381. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  382. exit:
  383. if (ACPI_FAILURE(status)) {
  384. ACPI_FREE(new_sci_handler);
  385. }
  386. return_ACPI_STATUS(status);
  387. }
  388. ACPI_EXPORT_SYMBOL(acpi_install_sci_handler)
  389. /*******************************************************************************
  390. *
  391. * FUNCTION: acpi_remove_sci_handler
  392. *
  393. * PARAMETERS: address - Address of the handler
  394. *
  395. * RETURN: Status
  396. *
  397. * DESCRIPTION: Remove a handler for a System Control Interrupt.
  398. *
  399. ******************************************************************************/
  400. acpi_status acpi_remove_sci_handler(acpi_sci_handler address)
  401. {
  402. struct acpi_sci_handler_info *prev_sci_handler;
  403. struct acpi_sci_handler_info *next_sci_handler;
  404. acpi_cpu_flags flags;
  405. acpi_status status;
  406. ACPI_FUNCTION_TRACE(acpi_remove_sci_handler);
  407. if (!address) {
  408. return_ACPI_STATUS(AE_BAD_PARAMETER);
  409. }
  410. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  411. if (ACPI_FAILURE(status)) {
  412. return_ACPI_STATUS(status);
  413. }
  414. /* Remove the SCI handler with lock */
  415. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  416. prev_sci_handler = NULL;
  417. next_sci_handler = acpi_gbl_sci_handler_list;
  418. while (next_sci_handler) {
  419. if (next_sci_handler->address == address) {
  420. /* Unlink and free the SCI handler info block */
  421. if (prev_sci_handler) {
  422. prev_sci_handler->next = next_sci_handler->next;
  423. } else {
  424. acpi_gbl_sci_handler_list =
  425. next_sci_handler->next;
  426. }
  427. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  428. ACPI_FREE(next_sci_handler);
  429. goto unlock_and_exit;
  430. }
  431. prev_sci_handler = next_sci_handler;
  432. next_sci_handler = next_sci_handler->next;
  433. }
  434. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  435. status = AE_NOT_EXIST;
  436. unlock_and_exit:
  437. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  438. return_ACPI_STATUS(status);
  439. }
  440. ACPI_EXPORT_SYMBOL(acpi_remove_sci_handler)
  441. /*******************************************************************************
  442. *
  443. * FUNCTION: acpi_install_global_event_handler
  444. *
  445. * PARAMETERS: handler - Pointer to the global event handler function
  446. * context - Value passed to the handler on each event
  447. *
  448. * RETURN: Status
  449. *
  450. * DESCRIPTION: Saves the pointer to the handler function. The global handler
  451. * is invoked upon each incoming GPE and Fixed Event. It is
  452. * invoked at interrupt level at the time of the event dispatch.
  453. * Can be used to update event counters, etc.
  454. *
  455. ******************************************************************************/
  456. acpi_status
  457. acpi_install_global_event_handler(acpi_gbl_event_handler handler, void *context)
  458. {
  459. acpi_status status;
  460. ACPI_FUNCTION_TRACE(acpi_install_global_event_handler);
  461. /* Parameter validation */
  462. if (!handler) {
  463. return_ACPI_STATUS(AE_BAD_PARAMETER);
  464. }
  465. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  466. if (ACPI_FAILURE(status)) {
  467. return_ACPI_STATUS(status);
  468. }
  469. /* Don't allow two handlers. */
  470. if (acpi_gbl_global_event_handler) {
  471. status = AE_ALREADY_EXISTS;
  472. goto cleanup;
  473. }
  474. acpi_gbl_global_event_handler = handler;
  475. acpi_gbl_global_event_handler_context = context;
  476. cleanup:
  477. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  478. return_ACPI_STATUS(status);
  479. }
  480. ACPI_EXPORT_SYMBOL(acpi_install_global_event_handler)
  481. /*******************************************************************************
  482. *
  483. * FUNCTION: acpi_install_fixed_event_handler
  484. *
  485. * PARAMETERS: event - Event type to enable.
  486. * handler - Pointer to the handler function for the
  487. * event
  488. * context - Value passed to the handler on each GPE
  489. *
  490. * RETURN: Status
  491. *
  492. * DESCRIPTION: Saves the pointer to the handler function and then enables the
  493. * event.
  494. *
  495. ******************************************************************************/
  496. acpi_status
  497. acpi_install_fixed_event_handler(u32 event,
  498. acpi_event_handler handler, void *context)
  499. {
  500. acpi_status status;
  501. ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
  502. /* Parameter validation */
  503. if (event > ACPI_EVENT_MAX) {
  504. return_ACPI_STATUS(AE_BAD_PARAMETER);
  505. }
  506. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  507. if (ACPI_FAILURE(status)) {
  508. return_ACPI_STATUS(status);
  509. }
  510. /* Do not allow multiple handlers */
  511. if (acpi_gbl_fixed_event_handlers[event].handler) {
  512. status = AE_ALREADY_EXISTS;
  513. goto cleanup;
  514. }
  515. /* Install the handler before enabling the event */
  516. acpi_gbl_fixed_event_handlers[event].handler = handler;
  517. acpi_gbl_fixed_event_handlers[event].context = context;
  518. status = acpi_clear_event(event);
  519. if (ACPI_SUCCESS(status))
  520. status = acpi_enable_event(event, 0);
  521. if (ACPI_FAILURE(status)) {
  522. ACPI_WARNING((AE_INFO,
  523. "Could not enable fixed event - %s (%u)",
  524. acpi_ut_get_event_name(event), event));
  525. /* Remove the handler */
  526. acpi_gbl_fixed_event_handlers[event].handler = NULL;
  527. acpi_gbl_fixed_event_handlers[event].context = NULL;
  528. } else {
  529. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  530. "Enabled fixed event %s (%X), Handler=%p\n",
  531. acpi_ut_get_event_name(event), event,
  532. handler));
  533. }
  534. cleanup:
  535. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  536. return_ACPI_STATUS(status);
  537. }
  538. ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
  539. /*******************************************************************************
  540. *
  541. * FUNCTION: acpi_remove_fixed_event_handler
  542. *
  543. * PARAMETERS: event - Event type to disable.
  544. * handler - Address of the handler
  545. *
  546. * RETURN: Status
  547. *
  548. * DESCRIPTION: Disables the event and unregisters the event handler.
  549. *
  550. ******************************************************************************/
  551. acpi_status
  552. acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
  553. {
  554. acpi_status status = AE_OK;
  555. ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
  556. /* Parameter validation */
  557. if (event > ACPI_EVENT_MAX) {
  558. return_ACPI_STATUS(AE_BAD_PARAMETER);
  559. }
  560. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  561. if (ACPI_FAILURE(status)) {
  562. return_ACPI_STATUS(status);
  563. }
  564. /* Disable the event before removing the handler */
  565. status = acpi_disable_event(event, 0);
  566. /* Always Remove the handler */
  567. acpi_gbl_fixed_event_handlers[event].handler = NULL;
  568. acpi_gbl_fixed_event_handlers[event].context = NULL;
  569. if (ACPI_FAILURE(status)) {
  570. ACPI_WARNING((AE_INFO,
  571. "Could not disable fixed event - %s (%u)",
  572. acpi_ut_get_event_name(event), event));
  573. } else {
  574. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  575. "Disabled fixed event - %s (%X)\n",
  576. acpi_ut_get_event_name(event), event));
  577. }
  578. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  579. return_ACPI_STATUS(status);
  580. }
  581. ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
  582. /*******************************************************************************
  583. *
  584. * FUNCTION: acpi_ev_install_gpe_handler
  585. *
  586. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  587. * defined GPEs)
  588. * gpe_number - The GPE number within the GPE block
  589. * type - Whether this GPE should be treated as an
  590. * edge- or level-triggered interrupt.
  591. * is_raw_handler - Whether this GPE should be handled using
  592. * the special GPE handler mode.
  593. * address - Address of the handler
  594. * context - Value passed to the handler on each GPE
  595. *
  596. * RETURN: Status
  597. *
  598. * DESCRIPTION: Internal function to install a handler for a General Purpose
  599. * Event.
  600. *
  601. ******************************************************************************/
  602. static acpi_status
  603. acpi_ev_install_gpe_handler(acpi_handle gpe_device,
  604. u32 gpe_number,
  605. u32 type,
  606. u8 is_raw_handler,
  607. acpi_gpe_handler address, void *context)
  608. {
  609. struct acpi_gpe_event_info *gpe_event_info;
  610. struct acpi_gpe_handler_info *handler;
  611. acpi_status status;
  612. acpi_cpu_flags flags;
  613. ACPI_FUNCTION_TRACE(ev_install_gpe_handler);
  614. /* Parameter validation */
  615. if ((!address) || (type & ~ACPI_GPE_XRUPT_TYPE_MASK)) {
  616. return_ACPI_STATUS(AE_BAD_PARAMETER);
  617. }
  618. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  619. if (ACPI_FAILURE(status)) {
  620. return_ACPI_STATUS(status);
  621. }
  622. /* Allocate and init handler object (before lock) */
  623. handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_handler_info));
  624. if (!handler) {
  625. status = AE_NO_MEMORY;
  626. goto unlock_and_exit;
  627. }
  628. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  629. /* Ensure that we have a valid GPE number */
  630. gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
  631. if (!gpe_event_info) {
  632. status = AE_BAD_PARAMETER;
  633. goto free_and_exit;
  634. }
  635. /* Make sure that there isn't a handler there already */
  636. if ((ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
  637. ACPI_GPE_DISPATCH_HANDLER) ||
  638. (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
  639. ACPI_GPE_DISPATCH_RAW_HANDLER)) {
  640. status = AE_ALREADY_EXISTS;
  641. goto free_and_exit;
  642. }
  643. handler->address = address;
  644. handler->context = context;
  645. handler->method_node = gpe_event_info->dispatch.method_node;
  646. handler->original_flags = (u8)(gpe_event_info->flags &
  647. (ACPI_GPE_XRUPT_TYPE_MASK |
  648. ACPI_GPE_DISPATCH_MASK));
  649. /*
  650. * If the GPE is associated with a method, it may have been enabled
  651. * automatically during initialization, in which case it has to be
  652. * disabled now to avoid spurious execution of the handler.
  653. */
  654. if (((ACPI_GPE_DISPATCH_TYPE(handler->original_flags) ==
  655. ACPI_GPE_DISPATCH_METHOD) ||
  656. (ACPI_GPE_DISPATCH_TYPE(handler->original_flags) ==
  657. ACPI_GPE_DISPATCH_NOTIFY)) && gpe_event_info->runtime_count) {
  658. handler->originally_enabled = TRUE;
  659. (void)acpi_ev_remove_gpe_reference(gpe_event_info);
  660. /* Sanity check of original type against new type */
  661. if (type !=
  662. (u32)(gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK)) {
  663. ACPI_WARNING((AE_INFO,
  664. "GPE type mismatch (level/edge)"));
  665. }
  666. }
  667. /* Install the handler */
  668. gpe_event_info->dispatch.handler = handler;
  669. /* Setup up dispatch flags to indicate handler (vs. method/notify) */
  670. gpe_event_info->flags &=
  671. ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
  672. gpe_event_info->flags |=
  673. (u8)(type |
  674. (is_raw_handler ? ACPI_GPE_DISPATCH_RAW_HANDLER :
  675. ACPI_GPE_DISPATCH_HANDLER));
  676. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  677. unlock_and_exit:
  678. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  679. return_ACPI_STATUS(status);
  680. free_and_exit:
  681. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  682. ACPI_FREE(handler);
  683. goto unlock_and_exit;
  684. }
  685. /*******************************************************************************
  686. *
  687. * FUNCTION: acpi_install_gpe_handler
  688. *
  689. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  690. * defined GPEs)
  691. * gpe_number - The GPE number within the GPE block
  692. * type - Whether this GPE should be treated as an
  693. * edge- or level-triggered interrupt.
  694. * address - Address of the handler
  695. * context - Value passed to the handler on each GPE
  696. *
  697. * RETURN: Status
  698. *
  699. * DESCRIPTION: Install a handler for a General Purpose Event.
  700. *
  701. ******************************************************************************/
  702. acpi_status
  703. acpi_install_gpe_handler(acpi_handle gpe_device,
  704. u32 gpe_number,
  705. u32 type, acpi_gpe_handler address, void *context)
  706. {
  707. acpi_status status;
  708. ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
  709. status =
  710. acpi_ev_install_gpe_handler(gpe_device, gpe_number, type, FALSE,
  711. address, context);
  712. return_ACPI_STATUS(status);
  713. }
  714. ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
  715. /*******************************************************************************
  716. *
  717. * FUNCTION: acpi_install_gpe_raw_handler
  718. *
  719. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  720. * defined GPEs)
  721. * gpe_number - The GPE number within the GPE block
  722. * type - Whether this GPE should be treated as an
  723. * edge- or level-triggered interrupt.
  724. * address - Address of the handler
  725. * context - Value passed to the handler on each GPE
  726. *
  727. * RETURN: Status
  728. *
  729. * DESCRIPTION: Install a handler for a General Purpose Event.
  730. *
  731. ******************************************************************************/
  732. acpi_status
  733. acpi_install_gpe_raw_handler(acpi_handle gpe_device,
  734. u32 gpe_number,
  735. u32 type, acpi_gpe_handler address, void *context)
  736. {
  737. acpi_status status;
  738. ACPI_FUNCTION_TRACE(acpi_install_gpe_raw_handler);
  739. status = acpi_ev_install_gpe_handler(gpe_device, gpe_number, type, TRUE,
  740. address, context);
  741. return_ACPI_STATUS(status);
  742. }
  743. ACPI_EXPORT_SYMBOL(acpi_install_gpe_raw_handler)
  744. /*******************************************************************************
  745. *
  746. * FUNCTION: acpi_remove_gpe_handler
  747. *
  748. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  749. * defined GPEs)
  750. * gpe_number - The event to remove a handler
  751. * address - Address of the handler
  752. *
  753. * RETURN: Status
  754. *
  755. * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
  756. *
  757. ******************************************************************************/
  758. acpi_status
  759. acpi_remove_gpe_handler(acpi_handle gpe_device,
  760. u32 gpe_number, acpi_gpe_handler address)
  761. {
  762. struct acpi_gpe_event_info *gpe_event_info;
  763. struct acpi_gpe_handler_info *handler;
  764. acpi_status status;
  765. acpi_cpu_flags flags;
  766. ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
  767. /* Parameter validation */
  768. if (!address) {
  769. return_ACPI_STATUS(AE_BAD_PARAMETER);
  770. }
  771. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  772. if (ACPI_FAILURE(status)) {
  773. return_ACPI_STATUS(status);
  774. }
  775. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  776. /* Ensure that we have a valid GPE number */
  777. gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
  778. if (!gpe_event_info) {
  779. status = AE_BAD_PARAMETER;
  780. goto unlock_and_exit;
  781. }
  782. /* Make sure that a handler is indeed installed */
  783. if ((ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) !=
  784. ACPI_GPE_DISPATCH_HANDLER) &&
  785. (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) !=
  786. ACPI_GPE_DISPATCH_RAW_HANDLER)) {
  787. status = AE_NOT_EXIST;
  788. goto unlock_and_exit;
  789. }
  790. /* Make sure that the installed handler is the same */
  791. if (gpe_event_info->dispatch.handler->address != address) {
  792. status = AE_BAD_PARAMETER;
  793. goto unlock_and_exit;
  794. }
  795. /* Remove the handler */
  796. handler = gpe_event_info->dispatch.handler;
  797. gpe_event_info->dispatch.handler = NULL;
  798. /* Restore Method node (if any), set dispatch flags */
  799. gpe_event_info->dispatch.method_node = handler->method_node;
  800. gpe_event_info->flags &=
  801. ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
  802. gpe_event_info->flags |= handler->original_flags;
  803. /*
  804. * If the GPE was previously associated with a method and it was
  805. * enabled, it should be enabled at this point to restore the
  806. * post-initialization configuration.
  807. */
  808. if (((ACPI_GPE_DISPATCH_TYPE(handler->original_flags) ==
  809. ACPI_GPE_DISPATCH_METHOD) ||
  810. (ACPI_GPE_DISPATCH_TYPE(handler->original_flags) ==
  811. ACPI_GPE_DISPATCH_NOTIFY)) && handler->originally_enabled) {
  812. (void)acpi_ev_add_gpe_reference(gpe_event_info);
  813. }
  814. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  815. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  816. /* Make sure all deferred GPE tasks are completed */
  817. acpi_os_wait_events_complete();
  818. /* Now we can free the handler object */
  819. ACPI_FREE(handler);
  820. return_ACPI_STATUS(status);
  821. unlock_and_exit:
  822. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  823. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  824. return_ACPI_STATUS(status);
  825. }
  826. ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
  827. /*******************************************************************************
  828. *
  829. * FUNCTION: acpi_acquire_global_lock
  830. *
  831. * PARAMETERS: timeout - How long the caller is willing to wait
  832. * handle - Where the handle to the lock is returned
  833. * (if acquired)
  834. *
  835. * RETURN: Status
  836. *
  837. * DESCRIPTION: Acquire the ACPI Global Lock
  838. *
  839. * Note: Allows callers with the same thread ID to acquire the global lock
  840. * multiple times. In other words, externally, the behavior of the global lock
  841. * is identical to an AML mutex. On the first acquire, a new handle is
  842. * returned. On any subsequent calls to acquire by the same thread, the same
  843. * handle is returned.
  844. *
  845. ******************************************************************************/
  846. acpi_status acpi_acquire_global_lock(u16 timeout, u32 *handle)
  847. {
  848. acpi_status status;
  849. if (!handle) {
  850. return (AE_BAD_PARAMETER);
  851. }
  852. /* Must lock interpreter to prevent race conditions */
  853. acpi_ex_enter_interpreter();
  854. status = acpi_ex_acquire_mutex_object(timeout,
  855. acpi_gbl_global_lock_mutex,
  856. acpi_os_get_thread_id());
  857. if (ACPI_SUCCESS(status)) {
  858. /* Return the global lock handle (updated in acpi_ev_acquire_global_lock) */
  859. *handle = acpi_gbl_global_lock_handle;
  860. }
  861. acpi_ex_exit_interpreter();
  862. return (status);
  863. }
  864. ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
  865. /*******************************************************************************
  866. *
  867. * FUNCTION: acpi_release_global_lock
  868. *
  869. * PARAMETERS: handle - Returned from acpi_acquire_global_lock
  870. *
  871. * RETURN: Status
  872. *
  873. * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
  874. *
  875. ******************************************************************************/
  876. acpi_status acpi_release_global_lock(u32 handle)
  877. {
  878. acpi_status status;
  879. if (!handle || (handle != acpi_gbl_global_lock_handle)) {
  880. return (AE_NOT_ACQUIRED);
  881. }
  882. status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
  883. return (status);
  884. }
  885. ACPI_EXPORT_SYMBOL(acpi_release_global_lock)
  886. #endif /* !ACPI_REDUCED_HARDWARE */