Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * wakeup.c - support wakeup devices
0004  * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com>
0005  */
0006 
0007 #include <linux/init.h>
0008 #include <linux/acpi.h>
0009 #include <linux/kernel.h>
0010 #include <linux/types.h>
0011 
0012 #include "internal.h"
0013 #include "sleep.h"
0014 
0015 struct acpi_wakeup_handler {
0016     struct list_head list_node;
0017     bool (*wakeup)(void *context);
0018     void *context;
0019 };
0020 
0021 static LIST_HEAD(acpi_wakeup_handler_head);
0022 static DEFINE_MUTEX(acpi_wakeup_handler_mutex);
0023 
0024 /*
0025  * We didn't lock acpi_device_lock in the file, because it invokes oops in
0026  * suspend/resume and isn't really required as this is called in S-state. At
0027  * that time, there is no device hotplug
0028  **/
0029 
0030 /**
0031  * acpi_enable_wakeup_devices - Enable wake-up device GPEs.
0032  * @sleep_state: ACPI system sleep state.
0033  *
0034  * Enable wakeup device power of devices with the state.enable flag set and set
0035  * the wakeup enable mask bits in the GPE registers that correspond to wakeup
0036  * devices.
0037  */
0038 void acpi_enable_wakeup_devices(u8 sleep_state)
0039 {
0040     struct acpi_device *dev, *tmp;
0041 
0042     list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
0043                  wakeup_list) {
0044         if (!dev->wakeup.flags.valid
0045             || sleep_state > (u32) dev->wakeup.sleep_state
0046             || !(device_may_wakeup(&dev->dev)
0047              || dev->wakeup.prepare_count))
0048             continue;
0049 
0050         if (device_may_wakeup(&dev->dev))
0051             acpi_enable_wakeup_device_power(dev, sleep_state);
0052 
0053         /* The wake-up power should have been enabled already. */
0054         acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
0055                 ACPI_GPE_ENABLE);
0056     }
0057 }
0058 
0059 /**
0060  * acpi_disable_wakeup_devices - Disable devices' wakeup capability.
0061  * @sleep_state: ACPI system sleep state.
0062  */
0063 void acpi_disable_wakeup_devices(u8 sleep_state)
0064 {
0065     struct acpi_device *dev, *tmp;
0066 
0067     list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
0068                  wakeup_list) {
0069         if (!dev->wakeup.flags.valid
0070             || sleep_state > (u32) dev->wakeup.sleep_state
0071             || !(device_may_wakeup(&dev->dev)
0072              || dev->wakeup.prepare_count))
0073             continue;
0074 
0075         acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
0076                 ACPI_GPE_DISABLE);
0077 
0078         if (device_may_wakeup(&dev->dev))
0079             acpi_disable_wakeup_device_power(dev);
0080     }
0081 }
0082 
0083 int __init acpi_wakeup_device_init(void)
0084 {
0085     struct acpi_device *dev, *tmp;
0086 
0087     mutex_lock(&acpi_device_lock);
0088     list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
0089                  wakeup_list) {
0090         if (device_can_wakeup(&dev->dev)) {
0091             /* Button GPEs are supposed to be always enabled. */
0092             acpi_enable_gpe(dev->wakeup.gpe_device,
0093                     dev->wakeup.gpe_number);
0094             device_set_wakeup_enable(&dev->dev, true);
0095         }
0096     }
0097     mutex_unlock(&acpi_device_lock);
0098     return 0;
0099 }
0100 
0101 /**
0102  * acpi_register_wakeup_handler - Register wakeup handler
0103  * @wake_irq: The IRQ through which the device may receive wakeups
0104  * @wakeup:   Wakeup-handler to call when the SCI has triggered a wakeup
0105  * @context:  Context to pass to the handler when calling it
0106  *
0107  * Drivers which may share an IRQ with the SCI can use this to register
0108  * a handler which returns true when the device they are managing wants
0109  * to trigger a wakeup.
0110  */
0111 int acpi_register_wakeup_handler(int wake_irq, bool (*wakeup)(void *context),
0112                  void *context)
0113 {
0114     struct acpi_wakeup_handler *handler;
0115 
0116     /*
0117      * If the device is not sharing its IRQ with the SCI, there is no
0118      * need to register the handler.
0119      */
0120     if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq)
0121         return 0;
0122 
0123     handler = kmalloc(sizeof(*handler), GFP_KERNEL);
0124     if (!handler)
0125         return -ENOMEM;
0126 
0127     handler->wakeup = wakeup;
0128     handler->context = context;
0129 
0130     mutex_lock(&acpi_wakeup_handler_mutex);
0131     list_add(&handler->list_node, &acpi_wakeup_handler_head);
0132     mutex_unlock(&acpi_wakeup_handler_mutex);
0133 
0134     return 0;
0135 }
0136 EXPORT_SYMBOL_GPL(acpi_register_wakeup_handler);
0137 
0138 /**
0139  * acpi_unregister_wakeup_handler - Unregister wakeup handler
0140  * @wakeup:   Wakeup-handler passed to acpi_register_wakeup_handler()
0141  * @context:  Context passed to acpi_register_wakeup_handler()
0142  */
0143 void acpi_unregister_wakeup_handler(bool (*wakeup)(void *context),
0144                     void *context)
0145 {
0146     struct acpi_wakeup_handler *handler;
0147 
0148     mutex_lock(&acpi_wakeup_handler_mutex);
0149     list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) {
0150         if (handler->wakeup == wakeup && handler->context == context) {
0151             list_del(&handler->list_node);
0152             kfree(handler);
0153             break;
0154         }
0155     }
0156     mutex_unlock(&acpi_wakeup_handler_mutex);
0157 }
0158 EXPORT_SYMBOL_GPL(acpi_unregister_wakeup_handler);
0159 
0160 bool acpi_check_wakeup_handlers(void)
0161 {
0162     struct acpi_wakeup_handler *handler;
0163 
0164     /* No need to lock, nothing else is running when we're called. */
0165     list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) {
0166         if (handler->wakeup(handler->context))
0167             return true;
0168     }
0169 
0170     return false;
0171 }