evxface.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /******************************************************************************
  2. *
  3. * Module Name: evxface - External interfaces for ACPI events
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, 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. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acnamesp.h"
  45. #include "acevents.h"
  46. #include "acinterp.h"
  47. #define _COMPONENT ACPI_EVENTS
  48. ACPI_MODULE_NAME("evxface")
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_install_exception_handler
  52. *
  53. * PARAMETERS: Handler - Pointer to the handler function for the
  54. * event
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Saves the pointer to the handler function
  59. *
  60. ******************************************************************************/
  61. #ifdef ACPI_FUTURE_USAGE
  62. acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
  63. {
  64. acpi_status status;
  65. ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
  66. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  67. if (ACPI_FAILURE(status)) {
  68. return_ACPI_STATUS(status);
  69. }
  70. /* Don't allow two handlers. */
  71. if (acpi_gbl_exception_handler) {
  72. status = AE_ALREADY_EXISTS;
  73. goto cleanup;
  74. }
  75. /* Install the handler */
  76. acpi_gbl_exception_handler = handler;
  77. cleanup:
  78. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  79. return_ACPI_STATUS(status);
  80. }
  81. ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
  82. #endif /* ACPI_FUTURE_USAGE */
  83. /*******************************************************************************
  84. *
  85. * FUNCTION: acpi_install_fixed_event_handler
  86. *
  87. * PARAMETERS: Event - Event type to enable.
  88. * Handler - Pointer to the handler function for the
  89. * event
  90. * Context - Value passed to the handler on each GPE
  91. *
  92. * RETURN: Status
  93. *
  94. * DESCRIPTION: Saves the pointer to the handler function and then enables the
  95. * event.
  96. *
  97. ******************************************************************************/
  98. acpi_status
  99. acpi_install_fixed_event_handler(u32 event,
  100. acpi_event_handler handler, void *context)
  101. {
  102. acpi_status status;
  103. ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
  104. /* Parameter validation */
  105. if (event > ACPI_EVENT_MAX) {
  106. return_ACPI_STATUS(AE_BAD_PARAMETER);
  107. }
  108. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  109. if (ACPI_FAILURE(status)) {
  110. return_ACPI_STATUS(status);
  111. }
  112. /* Don't allow two handlers. */
  113. if (NULL != acpi_gbl_fixed_event_handlers[event].handler) {
  114. status = AE_ALREADY_EXISTS;
  115. goto cleanup;
  116. }
  117. /* Install the handler before enabling the event */
  118. acpi_gbl_fixed_event_handlers[event].handler = handler;
  119. acpi_gbl_fixed_event_handlers[event].context = context;
  120. status = acpi_clear_event(event);
  121. if (ACPI_SUCCESS(status))
  122. status = acpi_enable_event(event, 0);
  123. if (ACPI_FAILURE(status)) {
  124. ACPI_WARNING((AE_INFO, "Could not enable fixed event %X",
  125. event));
  126. /* Remove the handler */
  127. acpi_gbl_fixed_event_handlers[event].handler = NULL;
  128. acpi_gbl_fixed_event_handlers[event].context = NULL;
  129. } else {
  130. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  131. "Enabled fixed event %X, Handler=%p\n", event,
  132. handler));
  133. }
  134. cleanup:
  135. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  136. return_ACPI_STATUS(status);
  137. }
  138. ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
  139. /*******************************************************************************
  140. *
  141. * FUNCTION: acpi_remove_fixed_event_handler
  142. *
  143. * PARAMETERS: Event - Event type to disable.
  144. * Handler - Address of the handler
  145. *
  146. * RETURN: Status
  147. *
  148. * DESCRIPTION: Disables the event and unregisters the event handler.
  149. *
  150. ******************************************************************************/
  151. acpi_status
  152. acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
  153. {
  154. acpi_status status = AE_OK;
  155. ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
  156. /* Parameter validation */
  157. if (event > ACPI_EVENT_MAX) {
  158. return_ACPI_STATUS(AE_BAD_PARAMETER);
  159. }
  160. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  161. if (ACPI_FAILURE(status)) {
  162. return_ACPI_STATUS(status);
  163. }
  164. /* Disable the event before removing the handler */
  165. status = acpi_disable_event(event, 0);
  166. /* Always Remove the handler */
  167. acpi_gbl_fixed_event_handlers[event].handler = NULL;
  168. acpi_gbl_fixed_event_handlers[event].context = NULL;
  169. if (ACPI_FAILURE(status)) {
  170. ACPI_WARNING((AE_INFO,
  171. "Could not write to fixed event enable register %X",
  172. event));
  173. } else {
  174. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabled fixed event %X\n",
  175. event));
  176. }
  177. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  178. return_ACPI_STATUS(status);
  179. }
  180. ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
  181. /*******************************************************************************
  182. *
  183. * FUNCTION: acpi_install_notify_handler
  184. *
  185. * PARAMETERS: Device - The device for which notifies will be handled
  186. * handler_type - The type of handler:
  187. * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
  188. * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
  189. * ACPI_ALL_NOTIFY: both system and device
  190. * Handler - Address of the handler
  191. * Context - Value passed to the handler on each GPE
  192. *
  193. * RETURN: Status
  194. *
  195. * DESCRIPTION: Install a handler for notifies on an ACPI device
  196. *
  197. ******************************************************************************/
  198. acpi_status
  199. acpi_install_notify_handler(acpi_handle device,
  200. u32 handler_type,
  201. acpi_notify_handler handler, void *context)
  202. {
  203. union acpi_operand_object *obj_desc;
  204. union acpi_operand_object *notify_obj;
  205. struct acpi_namespace_node *node;
  206. acpi_status status;
  207. ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
  208. /* Parameter validation */
  209. if ((!device) ||
  210. (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
  211. return_ACPI_STATUS(AE_BAD_PARAMETER);
  212. }
  213. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  214. if (ACPI_FAILURE(status)) {
  215. return_ACPI_STATUS(status);
  216. }
  217. /* Convert and validate the device handle */
  218. node = acpi_ns_map_handle_to_node(device);
  219. if (!node) {
  220. status = AE_BAD_PARAMETER;
  221. goto unlock_and_exit;
  222. }
  223. /*
  224. * Root Object:
  225. * Registering a notify handler on the root object indicates that the
  226. * caller wishes to receive notifications for all objects. Note that
  227. * only one <external> global handler can be regsitered (per notify type).
  228. */
  229. if (device == ACPI_ROOT_OBJECT) {
  230. /* Make sure the handler is not already installed */
  231. if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
  232. acpi_gbl_system_notify.handler) ||
  233. ((handler_type & ACPI_DEVICE_NOTIFY) &&
  234. acpi_gbl_device_notify.handler)) {
  235. status = AE_ALREADY_EXISTS;
  236. goto unlock_and_exit;
  237. }
  238. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  239. acpi_gbl_system_notify.node = node;
  240. acpi_gbl_system_notify.handler = handler;
  241. acpi_gbl_system_notify.context = context;
  242. }
  243. if (handler_type & ACPI_DEVICE_NOTIFY) {
  244. acpi_gbl_device_notify.node = node;
  245. acpi_gbl_device_notify.handler = handler;
  246. acpi_gbl_device_notify.context = context;
  247. }
  248. /* Global notify handler installed */
  249. }
  250. /*
  251. * All Other Objects:
  252. * Caller will only receive notifications specific to the target object.
  253. * Note that only certain object types can receive notifications.
  254. */
  255. else {
  256. /* Notifies allowed on this object? */
  257. if (!acpi_ev_is_notify_object(node)) {
  258. status = AE_TYPE;
  259. goto unlock_and_exit;
  260. }
  261. /* Check for an existing internal object */
  262. obj_desc = acpi_ns_get_attached_object(node);
  263. if (obj_desc) {
  264. /* Object exists - make sure there's no handler */
  265. if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
  266. obj_desc->common_notify.system_notify) ||
  267. ((handler_type & ACPI_DEVICE_NOTIFY) &&
  268. obj_desc->common_notify.device_notify)) {
  269. status = AE_ALREADY_EXISTS;
  270. goto unlock_and_exit;
  271. }
  272. } else {
  273. /* Create a new object */
  274. obj_desc = acpi_ut_create_internal_object(node->type);
  275. if (!obj_desc) {
  276. status = AE_NO_MEMORY;
  277. goto unlock_and_exit;
  278. }
  279. /* Attach new object to the Node */
  280. status =
  281. acpi_ns_attach_object(device, obj_desc, node->type);
  282. /* Remove local reference to the object */
  283. acpi_ut_remove_reference(obj_desc);
  284. if (ACPI_FAILURE(status)) {
  285. goto unlock_and_exit;
  286. }
  287. }
  288. /* Install the handler */
  289. notify_obj =
  290. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
  291. if (!notify_obj) {
  292. status = AE_NO_MEMORY;
  293. goto unlock_and_exit;
  294. }
  295. notify_obj->notify.node = node;
  296. notify_obj->notify.handler = handler;
  297. notify_obj->notify.context = context;
  298. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  299. obj_desc->common_notify.system_notify = notify_obj;
  300. }
  301. if (handler_type & ACPI_DEVICE_NOTIFY) {
  302. obj_desc->common_notify.device_notify = notify_obj;
  303. }
  304. if (handler_type == ACPI_ALL_NOTIFY) {
  305. /* Extra ref if installed in both */
  306. acpi_ut_add_reference(notify_obj);
  307. }
  308. }
  309. unlock_and_exit:
  310. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  311. return_ACPI_STATUS(status);
  312. }
  313. ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
  314. /*******************************************************************************
  315. *
  316. * FUNCTION: acpi_remove_notify_handler
  317. *
  318. * PARAMETERS: Device - The device for which notifies will be handled
  319. * handler_type - The type of handler:
  320. * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
  321. * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
  322. * ACPI_ALL_NOTIFY: both system and device
  323. * Handler - Address of the handler
  324. *
  325. * RETURN: Status
  326. *
  327. * DESCRIPTION: Remove a handler for notifies on an ACPI device
  328. *
  329. ******************************************************************************/
  330. acpi_status
  331. acpi_remove_notify_handler(acpi_handle device,
  332. u32 handler_type, acpi_notify_handler handler)
  333. {
  334. union acpi_operand_object *notify_obj;
  335. union acpi_operand_object *obj_desc;
  336. struct acpi_namespace_node *node;
  337. acpi_status status;
  338. ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
  339. /* Parameter validation */
  340. if ((!device) ||
  341. (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
  342. status = AE_BAD_PARAMETER;
  343. goto exit;
  344. }
  345. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  346. if (ACPI_FAILURE(status)) {
  347. goto exit;
  348. }
  349. /* Convert and validate the device handle */
  350. node = acpi_ns_map_handle_to_node(device);
  351. if (!node) {
  352. status = AE_BAD_PARAMETER;
  353. goto unlock_and_exit;
  354. }
  355. /* Root Object */
  356. if (device == ACPI_ROOT_OBJECT) {
  357. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  358. "Removing notify handler for namespace root object\n"));
  359. if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
  360. !acpi_gbl_system_notify.handler) ||
  361. ((handler_type & ACPI_DEVICE_NOTIFY) &&
  362. !acpi_gbl_device_notify.handler)) {
  363. status = AE_NOT_EXIST;
  364. goto unlock_and_exit;
  365. }
  366. /* Make sure all deferred tasks are completed */
  367. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  368. acpi_os_wait_events_complete(NULL);
  369. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  370. if (ACPI_FAILURE(status)) {
  371. goto exit;
  372. }
  373. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  374. acpi_gbl_system_notify.node = NULL;
  375. acpi_gbl_system_notify.handler = NULL;
  376. acpi_gbl_system_notify.context = NULL;
  377. }
  378. if (handler_type & ACPI_DEVICE_NOTIFY) {
  379. acpi_gbl_device_notify.node = NULL;
  380. acpi_gbl_device_notify.handler = NULL;
  381. acpi_gbl_device_notify.context = NULL;
  382. }
  383. }
  384. /* All Other Objects */
  385. else {
  386. /* Notifies allowed on this object? */
  387. if (!acpi_ev_is_notify_object(node)) {
  388. status = AE_TYPE;
  389. goto unlock_and_exit;
  390. }
  391. /* Check for an existing internal object */
  392. obj_desc = acpi_ns_get_attached_object(node);
  393. if (!obj_desc) {
  394. status = AE_NOT_EXIST;
  395. goto unlock_and_exit;
  396. }
  397. /* Object exists - make sure there's an existing handler */
  398. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  399. notify_obj = obj_desc->common_notify.system_notify;
  400. if (!notify_obj) {
  401. status = AE_NOT_EXIST;
  402. goto unlock_and_exit;
  403. }
  404. if (notify_obj->notify.handler != handler) {
  405. status = AE_BAD_PARAMETER;
  406. goto unlock_and_exit;
  407. }
  408. /* Make sure all deferred tasks are completed */
  409. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  410. acpi_os_wait_events_complete(NULL);
  411. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  412. if (ACPI_FAILURE(status)) {
  413. goto exit;
  414. }
  415. /* Remove the handler */
  416. obj_desc->common_notify.system_notify = NULL;
  417. acpi_ut_remove_reference(notify_obj);
  418. }
  419. if (handler_type & ACPI_DEVICE_NOTIFY) {
  420. notify_obj = obj_desc->common_notify.device_notify;
  421. if (!notify_obj) {
  422. status = AE_NOT_EXIST;
  423. goto unlock_and_exit;
  424. }
  425. if (notify_obj->notify.handler != handler) {
  426. status = AE_BAD_PARAMETER;
  427. goto unlock_and_exit;
  428. }
  429. /* Make sure all deferred tasks are completed */
  430. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  431. acpi_os_wait_events_complete(NULL);
  432. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  433. if (ACPI_FAILURE(status)) {
  434. goto exit;
  435. }
  436. /* Remove the handler */
  437. obj_desc->common_notify.device_notify = NULL;
  438. acpi_ut_remove_reference(notify_obj);
  439. }
  440. }
  441. unlock_and_exit:
  442. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  443. exit:
  444. if (ACPI_FAILURE(status))
  445. ACPI_EXCEPTION((AE_INFO, status, "Removing notify handler"));
  446. return_ACPI_STATUS(status);
  447. }
  448. ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
  449. /*******************************************************************************
  450. *
  451. * FUNCTION: acpi_install_gpe_handler
  452. *
  453. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  454. * defined GPEs)
  455. * gpe_number - The GPE number within the GPE block
  456. * Type - Whether this GPE should be treated as an
  457. * edge- or level-triggered interrupt.
  458. * Address - Address of the handler
  459. * Context - Value passed to the handler on each GPE
  460. *
  461. * RETURN: Status
  462. *
  463. * DESCRIPTION: Install a handler for a General Purpose Event.
  464. *
  465. ******************************************************************************/
  466. acpi_status
  467. acpi_install_gpe_handler(acpi_handle gpe_device,
  468. u32 gpe_number,
  469. u32 type, acpi_event_handler address, void *context)
  470. {
  471. struct acpi_gpe_event_info *gpe_event_info;
  472. struct acpi_handler_info *handler;
  473. acpi_status status;
  474. acpi_cpu_flags flags;
  475. ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
  476. /* Parameter validation */
  477. if ((!address) || (type > ACPI_GPE_XRUPT_TYPE_MASK)) {
  478. status = AE_BAD_PARAMETER;
  479. goto exit;
  480. }
  481. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  482. if (ACPI_FAILURE(status)) {
  483. goto exit;
  484. }
  485. /* Ensure that we have a valid GPE number */
  486. gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
  487. if (!gpe_event_info) {
  488. status = AE_BAD_PARAMETER;
  489. goto unlock_and_exit;
  490. }
  491. /* Make sure that there isn't a handler there already */
  492. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
  493. ACPI_GPE_DISPATCH_HANDLER) {
  494. status = AE_ALREADY_EXISTS;
  495. goto unlock_and_exit;
  496. }
  497. /* Allocate and init handler object */
  498. handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_handler_info));
  499. if (!handler) {
  500. status = AE_NO_MEMORY;
  501. goto unlock_and_exit;
  502. }
  503. handler->address = address;
  504. handler->context = context;
  505. handler->method_node = gpe_event_info->dispatch.method_node;
  506. /* Disable the GPE before installing the handler */
  507. status = acpi_ev_disable_gpe(gpe_event_info);
  508. if (ACPI_FAILURE(status)) {
  509. goto unlock_and_exit;
  510. }
  511. /* Install the handler */
  512. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  513. gpe_event_info->dispatch.handler = handler;
  514. /* Setup up dispatch flags to indicate handler (vs. method) */
  515. gpe_event_info->flags &=
  516. ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
  517. gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
  518. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  519. unlock_and_exit:
  520. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  521. exit:
  522. if (ACPI_FAILURE(status))
  523. ACPI_EXCEPTION((AE_INFO, status,
  524. "Installing notify handler failed"));
  525. return_ACPI_STATUS(status);
  526. }
  527. ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
  528. /*******************************************************************************
  529. *
  530. * FUNCTION: acpi_remove_gpe_handler
  531. *
  532. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  533. * defined GPEs)
  534. * gpe_number - The event to remove a handler
  535. * Address - Address of the handler
  536. *
  537. * RETURN: Status
  538. *
  539. * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
  540. *
  541. ******************************************************************************/
  542. acpi_status
  543. acpi_remove_gpe_handler(acpi_handle gpe_device,
  544. u32 gpe_number, acpi_event_handler address)
  545. {
  546. struct acpi_gpe_event_info *gpe_event_info;
  547. struct acpi_handler_info *handler;
  548. acpi_status status;
  549. acpi_cpu_flags flags;
  550. ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
  551. /* Parameter validation */
  552. if (!address) {
  553. return_ACPI_STATUS(AE_BAD_PARAMETER);
  554. }
  555. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  556. if (ACPI_FAILURE(status)) {
  557. return_ACPI_STATUS(status);
  558. }
  559. /* Ensure that we have a valid GPE number */
  560. gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
  561. if (!gpe_event_info) {
  562. status = AE_BAD_PARAMETER;
  563. goto unlock_and_exit;
  564. }
  565. /* Make sure that a handler is indeed installed */
  566. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
  567. ACPI_GPE_DISPATCH_HANDLER) {
  568. status = AE_NOT_EXIST;
  569. goto unlock_and_exit;
  570. }
  571. /* Make sure that the installed handler is the same */
  572. if (gpe_event_info->dispatch.handler->address != address) {
  573. status = AE_BAD_PARAMETER;
  574. goto unlock_and_exit;
  575. }
  576. /* Disable the GPE before removing the handler */
  577. status = acpi_ev_disable_gpe(gpe_event_info);
  578. if (ACPI_FAILURE(status)) {
  579. goto unlock_and_exit;
  580. }
  581. /* Make sure all deferred tasks are completed */
  582. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  583. acpi_os_wait_events_complete(NULL);
  584. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  585. if (ACPI_FAILURE(status)) {
  586. return_ACPI_STATUS(status);
  587. }
  588. /* Remove the handler */
  589. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  590. handler = gpe_event_info->dispatch.handler;
  591. /* Restore Method node (if any), set dispatch flags */
  592. gpe_event_info->dispatch.method_node = handler->method_node;
  593. gpe_event_info->flags &= ~ACPI_GPE_DISPATCH_MASK; /* Clear bits */
  594. if (handler->method_node) {
  595. gpe_event_info->flags |= ACPI_GPE_DISPATCH_METHOD;
  596. }
  597. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  598. /* Now we can free the handler object */
  599. ACPI_FREE(handler);
  600. unlock_and_exit:
  601. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  602. return_ACPI_STATUS(status);
  603. }
  604. ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
  605. /*******************************************************************************
  606. *
  607. * FUNCTION: acpi_acquire_global_lock
  608. *
  609. * PARAMETERS: Timeout - How long the caller is willing to wait
  610. * Handle - Where the handle to the lock is returned
  611. * (if acquired)
  612. *
  613. * RETURN: Status
  614. *
  615. * DESCRIPTION: Acquire the ACPI Global Lock
  616. *
  617. * Note: Allows callers with the same thread ID to acquire the global lock
  618. * multiple times. In other words, externally, the behavior of the global lock
  619. * is identical to an AML mutex. On the first acquire, a new handle is
  620. * returned. On any subsequent calls to acquire by the same thread, the same
  621. * handle is returned.
  622. *
  623. ******************************************************************************/
  624. acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
  625. {
  626. acpi_status status;
  627. if (!handle) {
  628. return (AE_BAD_PARAMETER);
  629. }
  630. /* Must lock interpreter to prevent race conditions */
  631. acpi_ex_enter_interpreter();
  632. status = acpi_ex_acquire_mutex_object(timeout,
  633. acpi_gbl_global_lock_mutex,
  634. acpi_os_get_thread_id());
  635. if (ACPI_SUCCESS(status)) {
  636. /* Return the global lock handle (updated in acpi_ev_acquire_global_lock) */
  637. *handle = acpi_gbl_global_lock_handle;
  638. }
  639. acpi_ex_exit_interpreter();
  640. return (status);
  641. }
  642. ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
  643. /*******************************************************************************
  644. *
  645. * FUNCTION: acpi_release_global_lock
  646. *
  647. * PARAMETERS: Handle - Returned from acpi_acquire_global_lock
  648. *
  649. * RETURN: Status
  650. *
  651. * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
  652. *
  653. ******************************************************************************/
  654. acpi_status acpi_release_global_lock(u32 handle)
  655. {
  656. acpi_status status;
  657. if (!handle || (handle != acpi_gbl_global_lock_handle)) {
  658. return (AE_NOT_ACQUIRED);
  659. }
  660. status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
  661. return (status);
  662. }
  663. ACPI_EXPORT_SYMBOL(acpi_release_global_lock)