evxface.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /******************************************************************************
  2. *
  3. * Module Name: evxface - External interfaces for ACPI events
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2012, 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 <linux/export.h>
  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. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_populate_handler_object
  53. *
  54. * PARAMETERS: handler_obj - Handler object to populate
  55. * handler_type - The type of handler:
  56. * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
  57. * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
  58. * ACPI_ALL_NOTIFY: both system and device
  59. * handler - Address of the handler
  60. * context - Value passed to the handler on each GPE
  61. * next - Address of a handler object to link to
  62. *
  63. * RETURN: None
  64. *
  65. * DESCRIPTION: Populate a handler object.
  66. *
  67. ******************************************************************************/
  68. static void
  69. acpi_populate_handler_object(struct acpi_object_notify_handler *handler_obj,
  70. u32 handler_type,
  71. acpi_notify_handler handler, void *context,
  72. struct acpi_object_notify_handler *next)
  73. {
  74. handler_obj->handler_type = handler_type;
  75. handler_obj->handler = handler;
  76. handler_obj->context = context;
  77. handler_obj->next = next;
  78. }
  79. /*******************************************************************************
  80. *
  81. * FUNCTION: acpi_add_handler_object
  82. *
  83. * PARAMETERS: parent_obj - Parent of the new object
  84. * handler - Address of the handler
  85. * context - Value passed to the handler on each GPE
  86. *
  87. * RETURN: Status
  88. *
  89. * DESCRIPTION: Create a new handler object and populate it.
  90. *
  91. ******************************************************************************/
  92. static acpi_status
  93. acpi_add_handler_object(struct acpi_object_notify_handler *parent_obj,
  94. acpi_notify_handler handler, void *context)
  95. {
  96. struct acpi_object_notify_handler *handler_obj;
  97. /* The parent must not be a defice notify handler object. */
  98. if (parent_obj->handler_type & ACPI_DEVICE_NOTIFY)
  99. return AE_BAD_PARAMETER;
  100. handler_obj = ACPI_ALLOCATE_ZEROED(sizeof(*handler_obj));
  101. if (!handler_obj)
  102. return AE_NO_MEMORY;
  103. acpi_populate_handler_object(handler_obj,
  104. ACPI_SYSTEM_NOTIFY,
  105. handler, context,
  106. parent_obj->next);
  107. parent_obj->next = handler_obj;
  108. return AE_OK;
  109. }
  110. /*******************************************************************************
  111. *
  112. * FUNCTION: acpi_install_notify_handler
  113. *
  114. * PARAMETERS: Device - The device for which notifies will be handled
  115. * handler_type - The type of handler:
  116. * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
  117. * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
  118. * ACPI_ALL_NOTIFY: both system and device
  119. * Handler - Address of the handler
  120. * Context - Value passed to the handler on each GPE
  121. *
  122. * RETURN: Status
  123. *
  124. * DESCRIPTION: Install a handler for notifies on an ACPI device
  125. *
  126. ******************************************************************************/
  127. acpi_status
  128. acpi_install_notify_handler(acpi_handle device,
  129. u32 handler_type,
  130. acpi_notify_handler handler, void *context)
  131. {
  132. union acpi_operand_object *obj_desc;
  133. union acpi_operand_object *notify_obj;
  134. struct acpi_namespace_node *node;
  135. acpi_status status;
  136. ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
  137. /* Parameter validation */
  138. if ((!device) ||
  139. (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
  140. return_ACPI_STATUS(AE_BAD_PARAMETER);
  141. }
  142. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  143. if (ACPI_FAILURE(status)) {
  144. return_ACPI_STATUS(status);
  145. }
  146. /* Convert and validate the device handle */
  147. node = acpi_ns_validate_handle(device);
  148. if (!node) {
  149. status = AE_BAD_PARAMETER;
  150. goto unlock_and_exit;
  151. }
  152. /*
  153. * Root Object:
  154. * Registering a notify handler on the root object indicates that the
  155. * caller wishes to receive notifications for all objects. Note that
  156. * only one <external> global handler can be regsitered (per notify type).
  157. */
  158. if (device == ACPI_ROOT_OBJECT) {
  159. /* Make sure the handler is not already installed */
  160. if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
  161. acpi_gbl_system_notify.handler) ||
  162. ((handler_type & ACPI_DEVICE_NOTIFY) &&
  163. acpi_gbl_device_notify.handler)) {
  164. status = AE_ALREADY_EXISTS;
  165. goto unlock_and_exit;
  166. }
  167. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  168. acpi_gbl_system_notify.node = node;
  169. acpi_gbl_system_notify.handler = handler;
  170. acpi_gbl_system_notify.context = context;
  171. }
  172. if (handler_type & ACPI_DEVICE_NOTIFY) {
  173. acpi_gbl_device_notify.node = node;
  174. acpi_gbl_device_notify.handler = handler;
  175. acpi_gbl_device_notify.context = context;
  176. }
  177. /* Global notify handler installed */
  178. }
  179. /*
  180. * All Other Objects:
  181. * Caller will only receive notifications specific to the target object.
  182. * Note that only certain object types can receive notifications.
  183. */
  184. else {
  185. /* Notifies allowed on this object? */
  186. if (!acpi_ev_is_notify_object(node)) {
  187. status = AE_TYPE;
  188. goto unlock_and_exit;
  189. }
  190. /* Check for an existing internal object */
  191. obj_desc = acpi_ns_get_attached_object(node);
  192. if (obj_desc) {
  193. /* Object exists. */
  194. /* For a device notify, make sure there's no handler. */
  195. if ((handler_type & ACPI_DEVICE_NOTIFY) &&
  196. obj_desc->common_notify.device_notify) {
  197. status = AE_ALREADY_EXISTS;
  198. goto unlock_and_exit;
  199. }
  200. /* System notifies may have more handlers installed. */
  201. notify_obj = obj_desc->common_notify.system_notify;
  202. if ((handler_type & ACPI_SYSTEM_NOTIFY) && notify_obj) {
  203. struct acpi_object_notify_handler *parent_obj;
  204. if (handler_type & ACPI_DEVICE_NOTIFY) {
  205. status = AE_ALREADY_EXISTS;
  206. goto unlock_and_exit;
  207. }
  208. parent_obj = &notify_obj->notify;
  209. status = acpi_add_handler_object(parent_obj,
  210. handler,
  211. context);
  212. goto unlock_and_exit;
  213. }
  214. } else {
  215. /* Create a new object */
  216. obj_desc = acpi_ut_create_internal_object(node->type);
  217. if (!obj_desc) {
  218. status = AE_NO_MEMORY;
  219. goto unlock_and_exit;
  220. }
  221. /* Attach new object to the Node */
  222. status =
  223. acpi_ns_attach_object(device, obj_desc, node->type);
  224. /* Remove local reference to the object */
  225. acpi_ut_remove_reference(obj_desc);
  226. if (ACPI_FAILURE(status)) {
  227. goto unlock_and_exit;
  228. }
  229. }
  230. /* Install the handler */
  231. notify_obj =
  232. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
  233. if (!notify_obj) {
  234. status = AE_NO_MEMORY;
  235. goto unlock_and_exit;
  236. }
  237. acpi_populate_handler_object(&notify_obj->notify,
  238. handler_type,
  239. handler, context,
  240. NULL);
  241. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  242. obj_desc->common_notify.system_notify = notify_obj;
  243. }
  244. if (handler_type & ACPI_DEVICE_NOTIFY) {
  245. obj_desc->common_notify.device_notify = notify_obj;
  246. }
  247. if (handler_type == ACPI_ALL_NOTIFY) {
  248. /* Extra ref if installed in both */
  249. acpi_ut_add_reference(notify_obj);
  250. }
  251. }
  252. unlock_and_exit:
  253. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  254. return_ACPI_STATUS(status);
  255. }
  256. ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
  257. /*******************************************************************************
  258. *
  259. * FUNCTION: acpi_remove_notify_handler
  260. *
  261. * PARAMETERS: Device - The device for which notifies will be handled
  262. * handler_type - The type of handler:
  263. * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
  264. * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
  265. * ACPI_ALL_NOTIFY: both system and device
  266. * Handler - Address of the handler
  267. *
  268. * RETURN: Status
  269. *
  270. * DESCRIPTION: Remove a handler for notifies on an ACPI device
  271. *
  272. ******************************************************************************/
  273. acpi_status
  274. acpi_remove_notify_handler(acpi_handle device,
  275. u32 handler_type, acpi_notify_handler handler)
  276. {
  277. union acpi_operand_object *notify_obj;
  278. union acpi_operand_object *obj_desc;
  279. struct acpi_namespace_node *node;
  280. acpi_status status;
  281. ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
  282. /* Parameter validation */
  283. if ((!device) ||
  284. (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
  285. status = AE_BAD_PARAMETER;
  286. goto exit;
  287. }
  288. /* Make sure all deferred tasks are completed */
  289. acpi_os_wait_events_complete(NULL);
  290. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  291. if (ACPI_FAILURE(status)) {
  292. goto exit;
  293. }
  294. /* Convert and validate the device handle */
  295. node = acpi_ns_validate_handle(device);
  296. if (!node) {
  297. status = AE_BAD_PARAMETER;
  298. goto unlock_and_exit;
  299. }
  300. /* Root Object */
  301. if (device == ACPI_ROOT_OBJECT) {
  302. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  303. "Removing notify handler for namespace root object\n"));
  304. if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
  305. !acpi_gbl_system_notify.handler) ||
  306. ((handler_type & ACPI_DEVICE_NOTIFY) &&
  307. !acpi_gbl_device_notify.handler)) {
  308. status = AE_NOT_EXIST;
  309. goto unlock_and_exit;
  310. }
  311. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  312. acpi_gbl_system_notify.node = NULL;
  313. acpi_gbl_system_notify.handler = NULL;
  314. acpi_gbl_system_notify.context = NULL;
  315. }
  316. if (handler_type & ACPI_DEVICE_NOTIFY) {
  317. acpi_gbl_device_notify.node = NULL;
  318. acpi_gbl_device_notify.handler = NULL;
  319. acpi_gbl_device_notify.context = NULL;
  320. }
  321. }
  322. /* All Other Objects */
  323. else {
  324. /* Notifies allowed on this object? */
  325. if (!acpi_ev_is_notify_object(node)) {
  326. status = AE_TYPE;
  327. goto unlock_and_exit;
  328. }
  329. /* Check for an existing internal object */
  330. obj_desc = acpi_ns_get_attached_object(node);
  331. if (!obj_desc) {
  332. status = AE_NOT_EXIST;
  333. goto unlock_and_exit;
  334. }
  335. /* Object exists - make sure there's an existing handler */
  336. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  337. struct acpi_object_notify_handler *handler_obj;
  338. struct acpi_object_notify_handler *parent_obj;
  339. notify_obj = obj_desc->common_notify.system_notify;
  340. if (!notify_obj) {
  341. status = AE_NOT_EXIST;
  342. goto unlock_and_exit;
  343. }
  344. handler_obj = &notify_obj->notify;
  345. parent_obj = NULL;
  346. while (handler_obj->handler != handler) {
  347. if (handler_obj->next) {
  348. parent_obj = handler_obj;
  349. handler_obj = handler_obj->next;
  350. } else {
  351. break;
  352. }
  353. }
  354. if (handler_obj->handler != handler) {
  355. status = AE_BAD_PARAMETER;
  356. goto unlock_and_exit;
  357. }
  358. /*
  359. * Remove the handler. There are three possible cases.
  360. * First, we may need to remove a non-embedded object.
  361. * Second, we may need to remove the embedded object's
  362. * handler data, while non-embedded objects exist.
  363. * Finally, we may need to remove the embedded object
  364. * entirely along with its container.
  365. */
  366. if (parent_obj) {
  367. /* Non-embedded object is being removed. */
  368. parent_obj->next = handler_obj->next;
  369. ACPI_FREE(handler_obj);
  370. } else if (notify_obj->notify.next) {
  371. /*
  372. * The handler matches the embedded object, but
  373. * there are more handler objects in the list.
  374. * Replace the embedded object's data with the
  375. * first next object's data and remove that
  376. * object.
  377. */
  378. parent_obj = &notify_obj->notify;
  379. handler_obj = notify_obj->notify.next;
  380. *parent_obj = *handler_obj;
  381. ACPI_FREE(handler_obj);
  382. } else {
  383. /* No more handler objects in the list. */
  384. obj_desc->common_notify.system_notify = NULL;
  385. acpi_ut_remove_reference(notify_obj);
  386. }
  387. }
  388. if (handler_type & ACPI_DEVICE_NOTIFY) {
  389. notify_obj = obj_desc->common_notify.device_notify;
  390. if (!notify_obj) {
  391. status = AE_NOT_EXIST;
  392. goto unlock_and_exit;
  393. }
  394. if (notify_obj->notify.handler != handler) {
  395. status = AE_BAD_PARAMETER;
  396. goto unlock_and_exit;
  397. }
  398. /* Remove the handler */
  399. obj_desc->common_notify.device_notify = NULL;
  400. acpi_ut_remove_reference(notify_obj);
  401. }
  402. }
  403. unlock_and_exit:
  404. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  405. exit:
  406. if (ACPI_FAILURE(status))
  407. ACPI_EXCEPTION((AE_INFO, status, "Removing notify handler"));
  408. return_ACPI_STATUS(status);
  409. }
  410. ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
  411. /*******************************************************************************
  412. *
  413. * FUNCTION: acpi_install_exception_handler
  414. *
  415. * PARAMETERS: Handler - Pointer to the handler function for the
  416. * event
  417. *
  418. * RETURN: Status
  419. *
  420. * DESCRIPTION: Saves the pointer to the handler function
  421. *
  422. ******************************************************************************/
  423. #ifdef ACPI_FUTURE_USAGE
  424. acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
  425. {
  426. acpi_status status;
  427. ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
  428. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  429. if (ACPI_FAILURE(status)) {
  430. return_ACPI_STATUS(status);
  431. }
  432. /* Don't allow two handlers. */
  433. if (acpi_gbl_exception_handler) {
  434. status = AE_ALREADY_EXISTS;
  435. goto cleanup;
  436. }
  437. /* Install the handler */
  438. acpi_gbl_exception_handler = handler;
  439. cleanup:
  440. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  441. return_ACPI_STATUS(status);
  442. }
  443. ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
  444. #endif /* ACPI_FUTURE_USAGE */
  445. #if (!ACPI_REDUCED_HARDWARE)
  446. /*******************************************************************************
  447. *
  448. * FUNCTION: acpi_install_global_event_handler
  449. *
  450. * PARAMETERS: Handler - Pointer to the global event handler function
  451. * Context - Value passed to the handler on each event
  452. *
  453. * RETURN: Status
  454. *
  455. * DESCRIPTION: Saves the pointer to the handler function. The global handler
  456. * is invoked upon each incoming GPE and Fixed Event. It is
  457. * invoked at interrupt level at the time of the event dispatch.
  458. * Can be used to update event counters, etc.
  459. *
  460. ******************************************************************************/
  461. acpi_status
  462. acpi_install_global_event_handler(ACPI_GBL_EVENT_HANDLER handler, void *context)
  463. {
  464. acpi_status status;
  465. ACPI_FUNCTION_TRACE(acpi_install_global_event_handler);
  466. /* Parameter validation */
  467. if (!handler) {
  468. return_ACPI_STATUS(AE_BAD_PARAMETER);
  469. }
  470. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  471. if (ACPI_FAILURE(status)) {
  472. return_ACPI_STATUS(status);
  473. }
  474. /* Don't allow two handlers. */
  475. if (acpi_gbl_global_event_handler) {
  476. status = AE_ALREADY_EXISTS;
  477. goto cleanup;
  478. }
  479. acpi_gbl_global_event_handler = handler;
  480. acpi_gbl_global_event_handler_context = context;
  481. cleanup:
  482. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  483. return_ACPI_STATUS(status);
  484. }
  485. ACPI_EXPORT_SYMBOL(acpi_install_global_event_handler)
  486. /*******************************************************************************
  487. *
  488. * FUNCTION: acpi_install_fixed_event_handler
  489. *
  490. * PARAMETERS: Event - Event type to enable.
  491. * Handler - Pointer to the handler function for the
  492. * event
  493. * Context - Value passed to the handler on each GPE
  494. *
  495. * RETURN: Status
  496. *
  497. * DESCRIPTION: Saves the pointer to the handler function and then enables the
  498. * event.
  499. *
  500. ******************************************************************************/
  501. acpi_status
  502. acpi_install_fixed_event_handler(u32 event,
  503. acpi_event_handler handler, void *context)
  504. {
  505. acpi_status status;
  506. ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
  507. /* Parameter validation */
  508. if (event > ACPI_EVENT_MAX) {
  509. return_ACPI_STATUS(AE_BAD_PARAMETER);
  510. }
  511. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  512. if (ACPI_FAILURE(status)) {
  513. return_ACPI_STATUS(status);
  514. }
  515. /* Don't allow two handlers. */
  516. if (NULL != acpi_gbl_fixed_event_handlers[event].handler) {
  517. status = AE_ALREADY_EXISTS;
  518. goto cleanup;
  519. }
  520. /* Install the handler before enabling the event */
  521. acpi_gbl_fixed_event_handlers[event].handler = handler;
  522. acpi_gbl_fixed_event_handlers[event].context = context;
  523. status = acpi_clear_event(event);
  524. if (ACPI_SUCCESS(status))
  525. status = acpi_enable_event(event, 0);
  526. if (ACPI_FAILURE(status)) {
  527. ACPI_WARNING((AE_INFO, "Could not enable fixed event 0x%X",
  528. event));
  529. /* Remove the handler */
  530. acpi_gbl_fixed_event_handlers[event].handler = NULL;
  531. acpi_gbl_fixed_event_handlers[event].context = NULL;
  532. } else {
  533. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  534. "Enabled fixed event %X, Handler=%p\n", event,
  535. handler));
  536. }
  537. cleanup:
  538. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  539. return_ACPI_STATUS(status);
  540. }
  541. ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
  542. /*******************************************************************************
  543. *
  544. * FUNCTION: acpi_remove_fixed_event_handler
  545. *
  546. * PARAMETERS: Event - Event type to disable.
  547. * Handler - Address of the handler
  548. *
  549. * RETURN: Status
  550. *
  551. * DESCRIPTION: Disables the event and unregisters the event handler.
  552. *
  553. ******************************************************************************/
  554. acpi_status
  555. acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
  556. {
  557. acpi_status status = AE_OK;
  558. ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
  559. /* Parameter validation */
  560. if (event > ACPI_EVENT_MAX) {
  561. return_ACPI_STATUS(AE_BAD_PARAMETER);
  562. }
  563. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  564. if (ACPI_FAILURE(status)) {
  565. return_ACPI_STATUS(status);
  566. }
  567. /* Disable the event before removing the handler */
  568. status = acpi_disable_event(event, 0);
  569. /* Always Remove the handler */
  570. acpi_gbl_fixed_event_handlers[event].handler = NULL;
  571. acpi_gbl_fixed_event_handlers[event].context = NULL;
  572. if (ACPI_FAILURE(status)) {
  573. ACPI_WARNING((AE_INFO,
  574. "Could not write to fixed event enable register 0x%X",
  575. event));
  576. } else {
  577. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabled fixed event %X\n",
  578. event));
  579. }
  580. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  581. return_ACPI_STATUS(status);
  582. }
  583. ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
  584. /*******************************************************************************
  585. *
  586. * FUNCTION: acpi_install_gpe_handler
  587. *
  588. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  589. * defined GPEs)
  590. * gpe_number - The GPE number within the GPE block
  591. * Type - Whether this GPE should be treated as an
  592. * edge- or level-triggered interrupt.
  593. * Address - Address of the handler
  594. * Context - Value passed to the handler on each GPE
  595. *
  596. * RETURN: Status
  597. *
  598. * DESCRIPTION: Install a handler for a General Purpose Event.
  599. *
  600. ******************************************************************************/
  601. acpi_status
  602. acpi_install_gpe_handler(acpi_handle gpe_device,
  603. u32 gpe_number,
  604. u32 type, acpi_gpe_handler address, void *context)
  605. {
  606. struct acpi_gpe_event_info *gpe_event_info;
  607. struct acpi_gpe_handler_info *handler;
  608. acpi_status status;
  609. acpi_cpu_flags flags;
  610. ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
  611. /* Parameter validation */
  612. if ((!address) || (type & ~ACPI_GPE_XRUPT_TYPE_MASK)) {
  613. return_ACPI_STATUS(AE_BAD_PARAMETER);
  614. }
  615. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  616. if (ACPI_FAILURE(status)) {
  617. return_ACPI_STATUS(status);
  618. }
  619. /* Allocate memory for the handler object */
  620. handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_handler_info));
  621. if (!handler) {
  622. status = AE_NO_MEMORY;
  623. goto unlock_and_exit;
  624. }
  625. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  626. /* Ensure that we have a valid GPE number */
  627. gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
  628. if (!gpe_event_info) {
  629. status = AE_BAD_PARAMETER;
  630. goto free_and_exit;
  631. }
  632. /* Make sure that there isn't a handler there already */
  633. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
  634. ACPI_GPE_DISPATCH_HANDLER) {
  635. status = AE_ALREADY_EXISTS;
  636. goto free_and_exit;
  637. }
  638. /* Allocate and init handler object */
  639. handler->address = address;
  640. handler->context = context;
  641. handler->method_node = gpe_event_info->dispatch.method_node;
  642. handler->original_flags = gpe_event_info->flags &
  643. (ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
  644. /*
  645. * If the GPE is associated with a method, it might have been enabled
  646. * automatically during initialization, in which case it has to be
  647. * disabled now to avoid spurious execution of the handler.
  648. */
  649. if ((handler->original_flags & ACPI_GPE_DISPATCH_METHOD)
  650. && gpe_event_info->runtime_count) {
  651. handler->originally_enabled = 1;
  652. (void)acpi_ev_remove_gpe_reference(gpe_event_info);
  653. }
  654. /* Install the handler */
  655. gpe_event_info->dispatch.handler = handler;
  656. /* Setup up dispatch flags to indicate handler (vs. method) */
  657. gpe_event_info->flags &=
  658. ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
  659. gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
  660. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  661. unlock_and_exit:
  662. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  663. return_ACPI_STATUS(status);
  664. free_and_exit:
  665. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  666. ACPI_FREE(handler);
  667. goto unlock_and_exit;
  668. }
  669. ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
  670. /*******************************************************************************
  671. *
  672. * FUNCTION: acpi_remove_gpe_handler
  673. *
  674. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  675. * defined GPEs)
  676. * gpe_number - The event to remove a handler
  677. * Address - Address of the handler
  678. *
  679. * RETURN: Status
  680. *
  681. * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
  682. *
  683. ******************************************************************************/
  684. acpi_status
  685. acpi_remove_gpe_handler(acpi_handle gpe_device,
  686. u32 gpe_number, acpi_gpe_handler address)
  687. {
  688. struct acpi_gpe_event_info *gpe_event_info;
  689. struct acpi_gpe_handler_info *handler;
  690. acpi_status status;
  691. acpi_cpu_flags flags;
  692. ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
  693. /* Parameter validation */
  694. if (!address) {
  695. return_ACPI_STATUS(AE_BAD_PARAMETER);
  696. }
  697. /* Make sure all deferred tasks are completed */
  698. acpi_os_wait_events_complete(NULL);
  699. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  700. if (ACPI_FAILURE(status)) {
  701. return_ACPI_STATUS(status);
  702. }
  703. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  704. /* Ensure that we have a valid GPE number */
  705. gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
  706. if (!gpe_event_info) {
  707. status = AE_BAD_PARAMETER;
  708. goto unlock_and_exit;
  709. }
  710. /* Make sure that a handler is indeed installed */
  711. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
  712. ACPI_GPE_DISPATCH_HANDLER) {
  713. status = AE_NOT_EXIST;
  714. goto unlock_and_exit;
  715. }
  716. /* Make sure that the installed handler is the same */
  717. if (gpe_event_info->dispatch.handler->address != address) {
  718. status = AE_BAD_PARAMETER;
  719. goto unlock_and_exit;
  720. }
  721. /* Remove the handler */
  722. handler = gpe_event_info->dispatch.handler;
  723. /* Restore Method node (if any), set dispatch flags */
  724. gpe_event_info->dispatch.method_node = handler->method_node;
  725. gpe_event_info->flags &=
  726. ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
  727. gpe_event_info->flags |= handler->original_flags;
  728. /*
  729. * If the GPE was previously associated with a method and it was
  730. * enabled, it should be enabled at this point to restore the
  731. * post-initialization configuration.
  732. */
  733. if ((handler->original_flags & ACPI_GPE_DISPATCH_METHOD)
  734. && handler->originally_enabled)
  735. (void)acpi_ev_add_gpe_reference(gpe_event_info);
  736. /* Now we can free the handler object */
  737. ACPI_FREE(handler);
  738. unlock_and_exit:
  739. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  740. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  741. return_ACPI_STATUS(status);
  742. }
  743. ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
  744. /*******************************************************************************
  745. *
  746. * FUNCTION: acpi_acquire_global_lock
  747. *
  748. * PARAMETERS: Timeout - How long the caller is willing to wait
  749. * Handle - Where the handle to the lock is returned
  750. * (if acquired)
  751. *
  752. * RETURN: Status
  753. *
  754. * DESCRIPTION: Acquire the ACPI Global Lock
  755. *
  756. * Note: Allows callers with the same thread ID to acquire the global lock
  757. * multiple times. In other words, externally, the behavior of the global lock
  758. * is identical to an AML mutex. On the first acquire, a new handle is
  759. * returned. On any subsequent calls to acquire by the same thread, the same
  760. * handle is returned.
  761. *
  762. ******************************************************************************/
  763. acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
  764. {
  765. acpi_status status;
  766. if (!handle) {
  767. return (AE_BAD_PARAMETER);
  768. }
  769. /* Must lock interpreter to prevent race conditions */
  770. acpi_ex_enter_interpreter();
  771. status = acpi_ex_acquire_mutex_object(timeout,
  772. acpi_gbl_global_lock_mutex,
  773. acpi_os_get_thread_id());
  774. if (ACPI_SUCCESS(status)) {
  775. /* Return the global lock handle (updated in acpi_ev_acquire_global_lock) */
  776. *handle = acpi_gbl_global_lock_handle;
  777. }
  778. acpi_ex_exit_interpreter();
  779. return (status);
  780. }
  781. ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
  782. /*******************************************************************************
  783. *
  784. * FUNCTION: acpi_release_global_lock
  785. *
  786. * PARAMETERS: Handle - Returned from acpi_acquire_global_lock
  787. *
  788. * RETURN: Status
  789. *
  790. * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
  791. *
  792. ******************************************************************************/
  793. acpi_status acpi_release_global_lock(u32 handle)
  794. {
  795. acpi_status status;
  796. if (!handle || (handle != acpi_gbl_global_lock_handle)) {
  797. return (AE_NOT_ACQUIRED);
  798. }
  799. status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
  800. return (status);
  801. }
  802. ACPI_EXPORT_SYMBOL(acpi_release_global_lock)
  803. #endif /* !ACPI_REDUCED_HARDWARE */