blob: e0ee0c036f5a674a882f190a4f8812989bfd276a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Helgaase60cc7a2009-03-13 12:08:26 -060011
12#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "sleep.h"
14
Shaohua Li90905892009-04-07 10:24:29 +080015/*
16 * We didn't lock acpi_device_lock in the file, because it invokes oops in
17 * suspend/resume and isn't really required as this is called in S-state. At
18 * that time, there is no device hotplug
19 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define _COMPONENT ACPI_SYSTEM_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040021ACPI_MODULE_NAME("wakeup_devices")
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/**
24 * acpi_enable_wakeup_device_prep - prepare wakeup devices
25 * @sleep_state: ACPI state
26 * Enable all wakup devices power if the devices' wakeup level
27 * is higher than requested sleep level
28 */
29
Len Brown4be44fc2005-08-05 00:44:28 -040030void acpi_enable_wakeup_device_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Len Brown4be44fc2005-08-05 00:44:28 -040032 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -040035 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 Torvalds1da177e2005-04-16 15:20:36 -070042 continue;
43
Rafael J. Wysocki77e76602008-07-07 03:33:34 +020044 acpi_enable_wakeup_device_power(dev, sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48/**
49 * acpi_enable_wakeup_device - enable wakeup devices
50 * @sleep_state: ACPI state
51 * Enable all wakup devices's GPE
52 */
Len Brown4be44fc2005-08-05 00:44:28 -040053void acpi_enable_wakeup_device(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Len Brown4be44fc2005-08-05 00:44:28 -040055 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 /*
58 * Caution: this routine must be invoked when interrupt is disabled
59 * Refer ACPI2.0: P212
60 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040062 struct acpi_device *dev =
63 container_of(node, struct acpi_device, wakeup_list);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020064
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040065 if (!dev->wakeup.flags.valid)
66 continue;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* If users want to disable run-wake GPE,
69 * we only disable it for wake and leave it for runtime
70 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020071 if ((!dev->wakeup.state.enabled && !dev->wakeup.prepare_count)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020072 || sleep_state > (u32) dev->wakeup.sleep_state) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040073 if (dev->wakeup.flags.run_wake) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040074 /* set_gpe_type will disable GPE, leave it like that */
75 acpi_set_gpe_type(dev->wakeup.gpe_device,
76 dev->wakeup.gpe_number,
77 ACPI_GPE_TYPE_RUNTIME);
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 continue;
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (!dev->wakeup.flags.run_wake)
Len Brown4be44fc2005-08-05 00:44:28 -040082 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +040083 dev->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/**
88 * acpi_disable_wakeup_device - disable devices' wakeup capability
89 * @sleep_state: ACPI state
90 * Disable all wakup devices's GPE and wakeup capability
91 */
Len Brown4be44fc2005-08-05 00:44:28 -040092void acpi_disable_wakeup_device(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Len Brown4be44fc2005-08-05 00:44:28 -040094 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040097 struct acpi_device *dev =
98 container_of(node, struct acpi_device, wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400100 if (!dev->wakeup.flags.valid)
101 continue;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200102
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200103 if ((!dev->wakeup.state.enabled && !dev->wakeup.prepare_count)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200104 || sleep_state > (u32) dev->wakeup.sleep_state) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400105 if (dev->wakeup.flags.run_wake) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400106 acpi_set_gpe_type(dev->wakeup.gpe_device,
107 dev->wakeup.gpe_number,
108 ACPI_GPE_TYPE_WAKE_RUN);
109 /* Re-enable it, since set_gpe_type will disable it */
110 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400111 dev->wakeup.gpe_number);
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 continue;
114 }
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 acpi_disable_wakeup_device_power(dev);
117 /* Never disable run-wake GPE */
118 if (!dev->wakeup.flags.run_wake) {
Len Brown4be44fc2005-08-05 00:44:28 -0400119 acpi_disable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400120 dev->wakeup.gpe_number);
Len Brown4be44fc2005-08-05 00:44:28 -0400121 acpi_clear_gpe(dev->wakeup.gpe_device,
122 dev->wakeup.gpe_number, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Bjorn Helgaas201b8c62009-03-24 16:50:19 -0600127int __init acpi_wakeup_device_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Len Brown4be44fc2005-08-05 00:44:28 -0400129 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Shaohua Li90905892009-04-07 10:24:29 +0800131 mutex_lock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400133 struct acpi_device *dev = container_of(node,
134 struct acpi_device,
135 wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* In case user doesn't load button driver */
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400137 if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
138 continue;
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400139 acpi_set_gpe_type(dev->wakeup.gpe_device,
140 dev->wakeup.gpe_number,
141 ACPI_GPE_TYPE_WAKE_RUN);
142 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400143 dev->wakeup.gpe_number);
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400144 dev->wakeup.state.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Shaohua Li90905892009-04-07 10:24:29 +0800146 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148}