Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * wakeup.c - support wakeup devices |
| 3 | * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/acpi.h> |
| 8 | #include <acpi/acpi_drivers.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <acpi/acevents.h> |
| 12 | #include "sleep.h" |
| 13 | |
| 14 | #define _COMPONENT ACPI_SYSTEM_COMPONENT |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 15 | ACPI_MODULE_NAME("wakeup_devices") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 17 | extern struct list_head acpi_wakeup_device_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | extern spinlock_t acpi_device_lock; |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | /** |
| 21 | * acpi_enable_wakeup_device_prep - prepare wakeup devices |
| 22 | * @sleep_state: ACPI state |
| 23 | * Enable all wakup devices power if the devices' wakeup level |
| 24 | * is higher than requested sleep level |
| 25 | */ |
| 26 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 27 | void acpi_enable_wakeup_device_prep(u8 sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 29 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device_prep"); |
| 32 | |
| 33 | spin_lock(&acpi_device_lock); |
| 34 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 35 | struct acpi_device *dev = container_of(node, |
| 36 | struct acpi_device, |
| 37 | wakeup_list); |
| 38 | |
| 39 | if (!dev->wakeup.flags.valid || |
| 40 | !dev->wakeup.state.enabled || |
| 41 | (sleep_state > (u32) dev->wakeup.sleep_state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | continue; |
| 43 | |
| 44 | spin_unlock(&acpi_device_lock); |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 45 | acpi_enable_wakeup_device_power(dev, sleep_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | spin_lock(&acpi_device_lock); |
| 47 | } |
| 48 | spin_unlock(&acpi_device_lock); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * acpi_enable_wakeup_device - enable wakeup devices |
| 53 | * @sleep_state: ACPI state |
| 54 | * Enable all wakup devices's GPE |
| 55 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 56 | void acpi_enable_wakeup_device(u8 sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 58 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Caution: this routine must be invoked when interrupt is disabled |
| 62 | * Refer ACPI2.0: P212 |
| 63 | */ |
| 64 | ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device"); |
| 65 | spin_lock(&acpi_device_lock); |
| 66 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 67 | struct acpi_device *dev = |
| 68 | container_of(node, struct acpi_device, wakeup_list); |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 69 | |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 70 | if (!dev->wakeup.flags.valid) |
| 71 | continue; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* If users want to disable run-wake GPE, |
| 74 | * we only disable it for wake and leave it for runtime |
| 75 | */ |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 76 | if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared) |
| 77 | || sleep_state > (u32) dev->wakeup.sleep_state) { |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 78 | if (dev->wakeup.flags.run_wake) { |
| 79 | spin_unlock(&acpi_device_lock); |
| 80 | /* set_gpe_type will disable GPE, leave it like that */ |
| 81 | acpi_set_gpe_type(dev->wakeup.gpe_device, |
| 82 | dev->wakeup.gpe_number, |
| 83 | ACPI_GPE_TYPE_RUNTIME); |
| 84 | spin_lock(&acpi_device_lock); |
| 85 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | continue; |
| 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | spin_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if (!dev->wakeup.flags.run_wake) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 90 | acpi_enable_gpe(dev->wakeup.gpe_device, |
Alexey Starikovskiy | 0b7084a | 2008-10-25 21:48:46 +0400 | [diff] [blame^] | 91 | dev->wakeup.gpe_number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | spin_lock(&acpi_device_lock); |
| 93 | } |
| 94 | spin_unlock(&acpi_device_lock); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * acpi_disable_wakeup_device - disable devices' wakeup capability |
| 99 | * @sleep_state: ACPI state |
| 100 | * Disable all wakup devices's GPE and wakeup capability |
| 101 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 102 | void acpi_disable_wakeup_device(u8 sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 104 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device"); |
| 107 | |
| 108 | spin_lock(&acpi_device_lock); |
| 109 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 110 | struct acpi_device *dev = |
| 111 | container_of(node, struct acpi_device, wakeup_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 113 | if (!dev->wakeup.flags.valid) |
| 114 | continue; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 115 | |
| 116 | if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared) |
| 117 | || sleep_state > (u32) dev->wakeup.sleep_state) { |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 118 | if (dev->wakeup.flags.run_wake) { |
| 119 | spin_unlock(&acpi_device_lock); |
| 120 | acpi_set_gpe_type(dev->wakeup.gpe_device, |
| 121 | dev->wakeup.gpe_number, |
| 122 | ACPI_GPE_TYPE_WAKE_RUN); |
| 123 | /* Re-enable it, since set_gpe_type will disable it */ |
| 124 | acpi_enable_gpe(dev->wakeup.gpe_device, |
Alexey Starikovskiy | 0b7084a | 2008-10-25 21:48:46 +0400 | [diff] [blame^] | 125 | dev->wakeup.gpe_number); |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 126 | spin_lock(&acpi_device_lock); |
| 127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | spin_unlock(&acpi_device_lock); |
| 132 | acpi_disable_wakeup_device_power(dev); |
| 133 | /* Never disable run-wake GPE */ |
| 134 | if (!dev->wakeup.flags.run_wake) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 135 | acpi_disable_gpe(dev->wakeup.gpe_device, |
Alexey Starikovskiy | 0b7084a | 2008-10-25 21:48:46 +0400 | [diff] [blame^] | 136 | dev->wakeup.gpe_number); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 137 | acpi_clear_gpe(dev->wakeup.gpe_device, |
| 138 | dev->wakeup.gpe_number, ACPI_NOT_ISR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | spin_lock(&acpi_device_lock); |
| 141 | } |
| 142 | spin_unlock(&acpi_device_lock); |
| 143 | } |
| 144 | |
| 145 | static int __init acpi_wakeup_device_init(void) |
| 146 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 147 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | if (acpi_disabled) |
| 150 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | spin_lock(&acpi_device_lock); |
| 153 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 154 | struct acpi_device *dev = container_of(node, |
| 155 | struct acpi_device, |
| 156 | wakeup_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* In case user doesn't load button driver */ |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 158 | if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled) |
| 159 | continue; |
| 160 | spin_unlock(&acpi_device_lock); |
| 161 | acpi_set_gpe_type(dev->wakeup.gpe_device, |
| 162 | dev->wakeup.gpe_number, |
| 163 | ACPI_GPE_TYPE_WAKE_RUN); |
| 164 | acpi_enable_gpe(dev->wakeup.gpe_device, |
Alexey Starikovskiy | 0b7084a | 2008-10-25 21:48:46 +0400 | [diff] [blame^] | 165 | dev->wakeup.gpe_number); |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 166 | dev->wakeup.state.enabled = 1; |
| 167 | spin_lock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | spin_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | late_initcall(acpi_wakeup_device_init); |