evxface.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /******************************************************************************
  2. *
  3. * Module Name: evxface - External interfaces for ACPI events
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2010, 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 0x%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 0x%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_populate_handler_object
  184. *
  185. * PARAMETERS: handler_obj - Handler object to populate
  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. * next - Address of a handler object to link to
  193. *
  194. * RETURN: None
  195. *
  196. * DESCRIPTION: Populate a handler object.
  197. *
  198. ******************************************************************************/
  199. static void
  200. acpi_populate_handler_object(struct acpi_object_notify_handler *handler_obj,
  201. u32 handler_type,
  202. acpi_notify_handler handler, void *context,
  203. struct acpi_object_notify_handler *next)
  204. {
  205. handler_obj->handler_type = handler_type;
  206. handler_obj->handler = handler;
  207. handler_obj->context = context;
  208. handler_obj->next = next;
  209. }
  210. /*******************************************************************************
  211. *
  212. * FUNCTION: acpi_add_handler_object
  213. *
  214. * PARAMETERS: parent_obj - Parent of the new object
  215. * handler - Address of the handler
  216. * context - Value passed to the handler on each GPE
  217. *
  218. * RETURN: Status
  219. *
  220. * DESCRIPTION: Create a new handler object and populate it.
  221. *
  222. ******************************************************************************/
  223. static acpi_status
  224. acpi_add_handler_object(struct acpi_object_notify_handler *parent_obj,
  225. acpi_notify_handler handler, void *context)
  226. {
  227. struct acpi_object_notify_handler *handler_obj;
  228. /* The parent must not be a defice notify handler object. */
  229. if (parent_obj->handler_type & ACPI_DEVICE_NOTIFY)
  230. return AE_BAD_PARAMETER;
  231. handler_obj = ACPI_ALLOCATE_ZEROED(sizeof(*handler_obj));
  232. if (!handler_obj)
  233. return AE_NO_MEMORY;
  234. acpi_populate_handler_object(handler_obj,
  235. ACPI_SYSTEM_NOTIFY,
  236. handler, context,
  237. parent_obj->next);
  238. parent_obj->next = handler_obj;
  239. return AE_OK;
  240. }
  241. /*******************************************************************************
  242. *
  243. * FUNCTION: acpi_install_notify_handler
  244. *
  245. * PARAMETERS: Device - The device for which notifies will be handled
  246. * handler_type - The type of handler:
  247. * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
  248. * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
  249. * ACPI_ALL_NOTIFY: both system and device
  250. * Handler - Address of the handler
  251. * Context - Value passed to the handler on each GPE
  252. *
  253. * RETURN: Status
  254. *
  255. * DESCRIPTION: Install a handler for notifies on an ACPI device
  256. *
  257. ******************************************************************************/
  258. acpi_status
  259. acpi_install_notify_handler(acpi_handle device,
  260. u32 handler_type,
  261. acpi_notify_handler handler, void *context)
  262. {
  263. union acpi_operand_object *obj_desc;
  264. union acpi_operand_object *notify_obj;
  265. struct acpi_namespace_node *node;
  266. acpi_status status;
  267. ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
  268. /* Parameter validation */
  269. if ((!device) ||
  270. (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
  271. return_ACPI_STATUS(AE_BAD_PARAMETER);
  272. }
  273. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  274. if (ACPI_FAILURE(status)) {
  275. return_ACPI_STATUS(status);
  276. }
  277. /* Convert and validate the device handle */
  278. node = acpi_ns_validate_handle(device);
  279. if (!node) {
  280. status = AE_BAD_PARAMETER;
  281. goto unlock_and_exit;
  282. }
  283. /*
  284. * Root Object:
  285. * Registering a notify handler on the root object indicates that the
  286. * caller wishes to receive notifications for all objects. Note that
  287. * only one <external> global handler can be regsitered (per notify type).
  288. */
  289. if (device == ACPI_ROOT_OBJECT) {
  290. /* Make sure the handler is not already installed */
  291. if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
  292. acpi_gbl_system_notify.handler) ||
  293. ((handler_type & ACPI_DEVICE_NOTIFY) &&
  294. acpi_gbl_device_notify.handler)) {
  295. status = AE_ALREADY_EXISTS;
  296. goto unlock_and_exit;
  297. }
  298. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  299. acpi_gbl_system_notify.node = node;
  300. acpi_gbl_system_notify.handler = handler;
  301. acpi_gbl_system_notify.context = context;
  302. }
  303. if (handler_type & ACPI_DEVICE_NOTIFY) {
  304. acpi_gbl_device_notify.node = node;
  305. acpi_gbl_device_notify.handler = handler;
  306. acpi_gbl_device_notify.context = context;
  307. }
  308. /* Global notify handler installed */
  309. }
  310. /*
  311. * All Other Objects:
  312. * Caller will only receive notifications specific to the target object.
  313. * Note that only certain object types can receive notifications.
  314. */
  315. else {
  316. /* Notifies allowed on this object? */
  317. if (!acpi_ev_is_notify_object(node)) {
  318. status = AE_TYPE;
  319. goto unlock_and_exit;
  320. }
  321. /* Check for an existing internal object */
  322. obj_desc = acpi_ns_get_attached_object(node);
  323. if (obj_desc) {
  324. /* Object exists. */
  325. /* For a device notify, make sure there's no handler. */
  326. if ((handler_type & ACPI_DEVICE_NOTIFY) &&
  327. obj_desc->common_notify.device_notify) {
  328. status = AE_ALREADY_EXISTS;
  329. goto unlock_and_exit;
  330. }
  331. /* System notifies may have more handlers installed. */
  332. notify_obj = obj_desc->common_notify.system_notify;
  333. if ((handler_type & ACPI_SYSTEM_NOTIFY) && notify_obj) {
  334. struct acpi_object_notify_handler *parent_obj;
  335. if (handler_type & ACPI_DEVICE_NOTIFY) {
  336. status = AE_ALREADY_EXISTS;
  337. goto unlock_and_exit;
  338. }
  339. parent_obj = &notify_obj->notify;
  340. status = acpi_add_handler_object(parent_obj,
  341. handler,
  342. context);
  343. goto unlock_and_exit;
  344. }
  345. } else {
  346. /* Create a new object */
  347. obj_desc = acpi_ut_create_internal_object(node->type);
  348. if (!obj_desc) {
  349. status = AE_NO_MEMORY;
  350. goto unlock_and_exit;
  351. }
  352. /* Attach new object to the Node */
  353. status =
  354. acpi_ns_attach_object(device, obj_desc, node->type);
  355. /* Remove local reference to the object */
  356. acpi_ut_remove_reference(obj_desc);
  357. if (ACPI_FAILURE(status)) {
  358. goto unlock_and_exit;
  359. }
  360. }
  361. /* Install the handler */
  362. notify_obj =
  363. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
  364. if (!notify_obj) {
  365. status = AE_NO_MEMORY;
  366. goto unlock_and_exit;
  367. }
  368. acpi_populate_handler_object(&notify_obj->notify,
  369. handler_type,
  370. handler, context,
  371. NULL);
  372. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  373. obj_desc->common_notify.system_notify = notify_obj;
  374. }
  375. if (handler_type & ACPI_DEVICE_NOTIFY) {
  376. obj_desc->common_notify.device_notify = notify_obj;
  377. }
  378. if (handler_type == ACPI_ALL_NOTIFY) {
  379. /* Extra ref if installed in both */
  380. acpi_ut_add_reference(notify_obj);
  381. }
  382. }
  383. unlock_and_exit:
  384. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  385. return_ACPI_STATUS(status);
  386. }
  387. ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
  388. /*******************************************************************************
  389. *
  390. * FUNCTION: acpi_remove_notify_handler
  391. *
  392. * PARAMETERS: Device - The device for which notifies will be handled
  393. * handler_type - The type of handler:
  394. * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
  395. * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
  396. * ACPI_ALL_NOTIFY: both system and device
  397. * Handler - Address of the handler
  398. *
  399. * RETURN: Status
  400. *
  401. * DESCRIPTION: Remove a handler for notifies on an ACPI device
  402. *
  403. ******************************************************************************/
  404. acpi_status
  405. acpi_remove_notify_handler(acpi_handle device,
  406. u32 handler_type, acpi_notify_handler handler)
  407. {
  408. union acpi_operand_object *notify_obj;
  409. union acpi_operand_object *obj_desc;
  410. struct acpi_namespace_node *node;
  411. acpi_status status;
  412. ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
  413. /* Parameter validation */
  414. if ((!device) ||
  415. (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
  416. status = AE_BAD_PARAMETER;
  417. goto exit;
  418. }
  419. /* Make sure all deferred tasks are completed */
  420. acpi_os_wait_events_complete(NULL);
  421. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  422. if (ACPI_FAILURE(status)) {
  423. goto exit;
  424. }
  425. /* Convert and validate the device handle */
  426. node = acpi_ns_validate_handle(device);
  427. if (!node) {
  428. status = AE_BAD_PARAMETER;
  429. goto unlock_and_exit;
  430. }
  431. /* Root Object */
  432. if (device == ACPI_ROOT_OBJECT) {
  433. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  434. "Removing notify handler for namespace root object\n"));
  435. if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
  436. !acpi_gbl_system_notify.handler) ||
  437. ((handler_type & ACPI_DEVICE_NOTIFY) &&
  438. !acpi_gbl_device_notify.handler)) {
  439. status = AE_NOT_EXIST;
  440. goto unlock_and_exit;
  441. }
  442. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  443. acpi_gbl_system_notify.node = NULL;
  444. acpi_gbl_system_notify.handler = NULL;
  445. acpi_gbl_system_notify.context = NULL;
  446. }
  447. if (handler_type & ACPI_DEVICE_NOTIFY) {
  448. acpi_gbl_device_notify.node = NULL;
  449. acpi_gbl_device_notify.handler = NULL;
  450. acpi_gbl_device_notify.context = NULL;
  451. }
  452. }
  453. /* All Other Objects */
  454. else {
  455. /* Notifies allowed on this object? */
  456. if (!acpi_ev_is_notify_object(node)) {
  457. status = AE_TYPE;
  458. goto unlock_and_exit;
  459. }
  460. /* Check for an existing internal object */
  461. obj_desc = acpi_ns_get_attached_object(node);
  462. if (!obj_desc) {
  463. status = AE_NOT_EXIST;
  464. goto unlock_and_exit;
  465. }
  466. /* Object exists - make sure there's an existing handler */
  467. if (handler_type & ACPI_SYSTEM_NOTIFY) {
  468. struct acpi_object_notify_handler *handler_obj;
  469. struct acpi_object_notify_handler *parent_obj;
  470. notify_obj = obj_desc->common_notify.system_notify;
  471. if (!notify_obj) {
  472. status = AE_NOT_EXIST;
  473. goto unlock_and_exit;
  474. }
  475. handler_obj = &notify_obj->notify;
  476. parent_obj = NULL;
  477. while (handler_obj->handler != handler) {
  478. if (handler_obj->next) {
  479. parent_obj = handler_obj;
  480. handler_obj = handler_obj->next;
  481. } else {
  482. break;
  483. }
  484. }
  485. if (handler_obj->handler != handler) {
  486. status = AE_BAD_PARAMETER;
  487. goto unlock_and_exit;
  488. }
  489. /*
  490. * Remove the handler. There are three possible cases.
  491. * First, we may need to remove a non-embedded object.
  492. * Second, we may need to remove the embedded object's
  493. * handler data, while non-embedded objects exist.
  494. * Finally, we may need to remove the embedded object
  495. * entirely along with its container.
  496. */
  497. if (parent_obj) {
  498. /* Non-embedded object is being removed. */
  499. parent_obj->next = handler_obj->next;
  500. ACPI_FREE(handler_obj);
  501. } else if (notify_obj->notify.next) {
  502. /*
  503. * The handler matches the embedded object, but
  504. * there are more handler objects in the list.
  505. * Replace the embedded object's data with the
  506. * first next object's data and remove that
  507. * object.
  508. */
  509. parent_obj = &notify_obj->notify;
  510. handler_obj = notify_obj->notify.next;
  511. *parent_obj = *handler_obj;
  512. ACPI_FREE(handler_obj);
  513. } else {
  514. /* No more handler objects in the list. */
  515. obj_desc->common_notify.system_notify = NULL;
  516. acpi_ut_remove_reference(notify_obj);
  517. }
  518. }
  519. if (handler_type & ACPI_DEVICE_NOTIFY) {
  520. notify_obj = obj_desc->common_notify.device_notify;
  521. if (!notify_obj) {
  522. status = AE_NOT_EXIST;
  523. goto unlock_and_exit;
  524. }
  525. if (notify_obj->notify.handler != handler) {
  526. status = AE_BAD_PARAMETER;
  527. goto unlock_and_exit;
  528. }
  529. /* Remove the handler */
  530. obj_desc->common_notify.device_notify = NULL;
  531. acpi_ut_remove_reference(notify_obj);
  532. }
  533. }
  534. unlock_and_exit:
  535. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  536. exit:
  537. if (ACPI_FAILURE(status))
  538. ACPI_EXCEPTION((AE_INFO, status, "Removing notify handler"));
  539. return_ACPI_STATUS(status);
  540. }
  541. ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
  542. /*******************************************************************************
  543. *
  544. * FUNCTION: acpi_install_gpe_handler
  545. *
  546. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  547. * defined GPEs)
  548. * gpe_number - The GPE number within the GPE block
  549. * Type - Whether this GPE should be treated as an
  550. * edge- or level-triggered interrupt.
  551. * Address - Address of the handler
  552. * Context - Value passed to the handler on each GPE
  553. *
  554. * RETURN: Status
  555. *
  556. * DESCRIPTION: Install a handler for a General Purpose Event.
  557. *
  558. ******************************************************************************/
  559. acpi_status
  560. acpi_install_gpe_handler(acpi_handle gpe_device,
  561. u32 gpe_number,
  562. u32 type, acpi_event_handler address, void *context)
  563. {
  564. struct acpi_gpe_event_info *gpe_event_info;
  565. struct acpi_handler_info *handler;
  566. acpi_status status;
  567. acpi_cpu_flags flags;
  568. ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
  569. /* Parameter validation */
  570. if ((!address) || (type & ~ACPI_GPE_XRUPT_TYPE_MASK)) {
  571. return_ACPI_STATUS(AE_BAD_PARAMETER);
  572. }
  573. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  574. if (ACPI_FAILURE(status)) {
  575. return_ACPI_STATUS(status);
  576. }
  577. /* Allocate memory for the handler object */
  578. handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_handler_info));
  579. if (!handler) {
  580. status = AE_NO_MEMORY;
  581. goto unlock_and_exit;
  582. }
  583. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  584. /* Ensure that we have a valid GPE number */
  585. gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
  586. if (!gpe_event_info) {
  587. status = AE_BAD_PARAMETER;
  588. goto free_and_exit;
  589. }
  590. /* Make sure that there isn't a handler there already */
  591. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
  592. ACPI_GPE_DISPATCH_HANDLER) {
  593. status = AE_ALREADY_EXISTS;
  594. goto free_and_exit;
  595. }
  596. /* Allocate and init handler object */
  597. handler->address = address;
  598. handler->context = context;
  599. handler->method_node = gpe_event_info->dispatch.method_node;
  600. handler->orig_flags = gpe_event_info->flags &
  601. (ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
  602. /*
  603. * If the GPE is associated with a method, it might have been enabled
  604. * automatically during initialization, in which case it has to be
  605. * disabled now to avoid spurious execution of the handler.
  606. */
  607. if ((handler->orig_flags & ACPI_GPE_DISPATCH_METHOD)
  608. && gpe_event_info->runtime_count) {
  609. handler->orig_enabled = 1;
  610. (void)acpi_raw_disable_gpe(gpe_event_info);
  611. }
  612. /* Install the handler */
  613. gpe_event_info->dispatch.handler = handler;
  614. /* Setup up dispatch flags to indicate handler (vs. method) */
  615. gpe_event_info->flags &=
  616. ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
  617. gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
  618. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  619. unlock_and_exit:
  620. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  621. return_ACPI_STATUS(status);
  622. free_and_exit:
  623. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  624. ACPI_FREE(handler);
  625. goto unlock_and_exit;
  626. }
  627. ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
  628. /*******************************************************************************
  629. *
  630. * FUNCTION: acpi_remove_gpe_handler
  631. *
  632. * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
  633. * defined GPEs)
  634. * gpe_number - The event to remove a handler
  635. * Address - Address of the handler
  636. *
  637. * RETURN: Status
  638. *
  639. * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
  640. *
  641. ******************************************************************************/
  642. acpi_status
  643. acpi_remove_gpe_handler(acpi_handle gpe_device,
  644. u32 gpe_number, acpi_event_handler address)
  645. {
  646. struct acpi_gpe_event_info *gpe_event_info;
  647. struct acpi_handler_info *handler;
  648. acpi_status status;
  649. acpi_cpu_flags flags;
  650. ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
  651. /* Parameter validation */
  652. if (!address) {
  653. return_ACPI_STATUS(AE_BAD_PARAMETER);
  654. }
  655. /* Make sure all deferred tasks are completed */
  656. acpi_os_wait_events_complete(NULL);
  657. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  658. if (ACPI_FAILURE(status)) {
  659. return_ACPI_STATUS(status);
  660. }
  661. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  662. /* Ensure that we have a valid GPE number */
  663. gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
  664. if (!gpe_event_info) {
  665. status = AE_BAD_PARAMETER;
  666. goto unlock_and_exit;
  667. }
  668. /* Make sure that a handler is indeed installed */
  669. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
  670. ACPI_GPE_DISPATCH_HANDLER) {
  671. status = AE_NOT_EXIST;
  672. goto unlock_and_exit;
  673. }
  674. /* Make sure that the installed handler is the same */
  675. if (gpe_event_info->dispatch.handler->address != address) {
  676. status = AE_BAD_PARAMETER;
  677. goto unlock_and_exit;
  678. }
  679. /* Remove the handler */
  680. handler = gpe_event_info->dispatch.handler;
  681. /* Restore Method node (if any), set dispatch flags */
  682. gpe_event_info->dispatch.method_node = handler->method_node;
  683. gpe_event_info->flags &=
  684. ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
  685. gpe_event_info->flags |= handler->orig_flags;
  686. /*
  687. * If the GPE was previously associated with a method and it was
  688. * enabled, it should be enabled at this point to restore the
  689. * post-initialization configuration.
  690. */
  691. if ((handler->orig_flags & ACPI_GPE_DISPATCH_METHOD)
  692. && handler->orig_enabled)
  693. (void)acpi_raw_enable_gpe(gpe_event_info);
  694. /* Now we can free the handler object */
  695. ACPI_FREE(handler);
  696. unlock_and_exit:
  697. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  698. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  699. return_ACPI_STATUS(status);
  700. }
  701. ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
  702. /*******************************************************************************
  703. *
  704. * FUNCTION: acpi_acquire_global_lock
  705. *
  706. * PARAMETERS: Timeout - How long the caller is willing to wait
  707. * Handle - Where the handle to the lock is returned
  708. * (if acquired)
  709. *
  710. * RETURN: Status
  711. *
  712. * DESCRIPTION: Acquire the ACPI Global Lock
  713. *
  714. * Note: Allows callers with the same thread ID to acquire the global lock
  715. * multiple times. In other words, externally, the behavior of the global lock
  716. * is identical to an AML mutex. On the first acquire, a new handle is
  717. * returned. On any subsequent calls to acquire by the same thread, the same
  718. * handle is returned.
  719. *
  720. ******************************************************************************/
  721. acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
  722. {
  723. acpi_status status;
  724. if (!handle) {
  725. return (AE_BAD_PARAMETER);
  726. }
  727. /* Must lock interpreter to prevent race conditions */
  728. acpi_ex_enter_interpreter();
  729. status = acpi_ex_acquire_mutex_object(timeout,
  730. acpi_gbl_global_lock_mutex,
  731. acpi_os_get_thread_id());
  732. if (ACPI_SUCCESS(status)) {
  733. /* Return the global lock handle (updated in acpi_ev_acquire_global_lock) */
  734. *handle = acpi_gbl_global_lock_handle;
  735. }
  736. acpi_ex_exit_interpreter();
  737. return (status);
  738. }
  739. ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
  740. /*******************************************************************************
  741. *
  742. * FUNCTION: acpi_release_global_lock
  743. *
  744. * PARAMETERS: Handle - Returned from acpi_acquire_global_lock
  745. *
  746. * RETURN: Status
  747. *
  748. * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
  749. *
  750. ******************************************************************************/
  751. acpi_status acpi_release_global_lock(u32 handle)
  752. {
  753. acpi_status status;
  754. if (!handle || (handle != acpi_gbl_global_lock_handle)) {
  755. return (AE_NOT_ACQUIRED);
  756. }
  757. status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
  758. return (status);
  759. }
  760. ACPI_EXPORT_SYMBOL(acpi_release_global_lock)