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); |
| 45 | acpi_enable_wakeup_device_power(dev); |
| 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); |
| 69 | if (!dev->wakeup.flags.valid) |
| 70 | continue; |
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 | */ |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 74 | if (!dev->wakeup.state.enabled || |
| 75 | sleep_state > (u32) dev->wakeup.sleep_state) { |
| 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, |
| 89 | dev->wakeup.gpe_number, ACPI_ISR); |
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 | |
| 104 | ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device"); |
| 105 | |
| 106 | spin_lock(&acpi_device_lock); |
| 107 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 108 | struct acpi_device *dev = |
| 109 | container_of(node, struct acpi_device, wakeup_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 111 | if (!dev->wakeup.flags.valid) |
| 112 | continue; |
| 113 | if (!dev->wakeup.state.enabled || |
| 114 | sleep_state > (u32) dev->wakeup.sleep_state) { |
| 115 | if (dev->wakeup.flags.run_wake) { |
| 116 | spin_unlock(&acpi_device_lock); |
| 117 | acpi_set_gpe_type(dev->wakeup.gpe_device, |
| 118 | dev->wakeup.gpe_number, |
| 119 | ACPI_GPE_TYPE_WAKE_RUN); |
| 120 | /* Re-enable it, since set_gpe_type will disable it */ |
| 121 | acpi_enable_gpe(dev->wakeup.gpe_device, |
| 122 | dev->wakeup.gpe_number, ACPI_NOT_ISR); |
| 123 | spin_lock(&acpi_device_lock); |
| 124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | continue; |
| 126 | } |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | spin_unlock(&acpi_device_lock); |
| 129 | acpi_disable_wakeup_device_power(dev); |
| 130 | /* Never disable run-wake GPE */ |
| 131 | if (!dev->wakeup.flags.run_wake) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 132 | acpi_disable_gpe(dev->wakeup.gpe_device, |
| 133 | dev->wakeup.gpe_number, ACPI_NOT_ISR); |
| 134 | acpi_clear_gpe(dev->wakeup.gpe_device, |
| 135 | dev->wakeup.gpe_number, ACPI_NOT_ISR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | spin_lock(&acpi_device_lock); |
| 138 | } |
| 139 | spin_unlock(&acpi_device_lock); |
| 140 | } |
| 141 | |
| 142 | static int __init acpi_wakeup_device_init(void) |
| 143 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 144 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | if (acpi_disabled) |
| 147 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | spin_lock(&acpi_device_lock); |
| 150 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 151 | struct acpi_device *dev = container_of(node, |
| 152 | struct acpi_device, |
| 153 | wakeup_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* In case user doesn't load button driver */ |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 155 | if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled) |
| 156 | continue; |
| 157 | spin_unlock(&acpi_device_lock); |
| 158 | acpi_set_gpe_type(dev->wakeup.gpe_device, |
| 159 | dev->wakeup.gpe_number, |
| 160 | ACPI_GPE_TYPE_WAKE_RUN); |
| 161 | acpi_enable_gpe(dev->wakeup.gpe_device, |
| 162 | dev->wakeup.gpe_number, ACPI_NOT_ISR); |
| 163 | dev->wakeup.state.enabled = 1; |
| 164 | spin_lock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | spin_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | late_initcall(acpi_wakeup_device_init); |