evxface.c 28 KB

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