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