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