wakeup.c 4.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * wakeup.c - support wakeup devices
 * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com>
 */

#include <linux/init.h>
#include <linux/acpi.h>
#include <acpi/acpi_drivers.h>
#include <linux/kernel.h>
#include <linux/types.h>
11 12

#include "internal.h"
L
Linus Torvalds 已提交
13 14 15
#include "sleep.h"

#define _COMPONENT		ACPI_SYSTEM_COMPONENT
L
Len Brown 已提交
16
ACPI_MODULE_NAME("wakeup_devices")
L
Linus Torvalds 已提交
17

L
Len Brown 已提交
18
extern struct list_head acpi_wakeup_device_list;
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27
extern spinlock_t acpi_device_lock;

/**
 * acpi_enable_wakeup_device_prep - prepare wakeup devices
 *	@sleep_state:	ACPI state
 * Enable all wakup devices power if the devices' wakeup level
 * is higher than requested sleep level
 */

L
Len Brown 已提交
28
void acpi_enable_wakeup_device_prep(u8 sleep_state)
L
Linus Torvalds 已提交
29
{
L
Len Brown 已提交
30
	struct list_head *node, *next;
L
Linus Torvalds 已提交
31 32 33

	spin_lock(&acpi_device_lock);
	list_for_each_safe(node, next, &acpi_wakeup_device_list) {
L
Len Brown 已提交
34 35 36 37 38 39 40
		struct acpi_device *dev = container_of(node,
						       struct acpi_device,
						       wakeup_list);

		if (!dev->wakeup.flags.valid ||
		    !dev->wakeup.state.enabled ||
		    (sleep_state > (u32) dev->wakeup.sleep_state))
L
Linus Torvalds 已提交
41 42 43
			continue;

		spin_unlock(&acpi_device_lock);
44
		acpi_enable_wakeup_device_power(dev, sleep_state);
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54
		spin_lock(&acpi_device_lock);
	}
	spin_unlock(&acpi_device_lock);
}

/**
 * acpi_enable_wakeup_device - enable wakeup devices
 *	@sleep_state:	ACPI state
 * Enable all wakup devices's GPE
 */
L
Len Brown 已提交
55
void acpi_enable_wakeup_device(u8 sleep_state)
L
Linus Torvalds 已提交
56
{
L
Len Brown 已提交
57
	struct list_head *node, *next;
L
Linus Torvalds 已提交
58 59 60 61 62 63 64

	/* 
	 * Caution: this routine must be invoked when interrupt is disabled 
	 * Refer ACPI2.0: P212
	 */
	spin_lock(&acpi_device_lock);
	list_for_each_safe(node, next, &acpi_wakeup_device_list) {
65 66
		struct acpi_device *dev =
			container_of(node, struct acpi_device, wakeup_list);
67

68 69
		if (!dev->wakeup.flags.valid)
			continue;
70

L
Linus Torvalds 已提交
71 72 73
		/* If users want to disable run-wake GPE,
		 * we only disable it for wake and leave it for runtime
		 */
74 75
		if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
		    || sleep_state > (u32) dev->wakeup.sleep_state) {
76 77 78 79 80 81 82 83
			if (dev->wakeup.flags.run_wake) {
				spin_unlock(&acpi_device_lock);
				/* set_gpe_type will disable GPE, leave it like that */
				acpi_set_gpe_type(dev->wakeup.gpe_device,
						  dev->wakeup.gpe_number,
						  ACPI_GPE_TYPE_RUNTIME);
				spin_lock(&acpi_device_lock);
			}
L
Linus Torvalds 已提交
84 85 86 87
			continue;
		}
		spin_unlock(&acpi_device_lock);
		if (!dev->wakeup.flags.run_wake)
L
Len Brown 已提交
88
			acpi_enable_gpe(dev->wakeup.gpe_device,
89
					dev->wakeup.gpe_number);
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97 98 99
		spin_lock(&acpi_device_lock);
	}
	spin_unlock(&acpi_device_lock);
}

/**
 * acpi_disable_wakeup_device - disable devices' wakeup capability
 *	@sleep_state:	ACPI state
 * Disable all wakup devices's GPE and wakeup capability
 */
L
Len Brown 已提交
100
void acpi_disable_wakeup_device(u8 sleep_state)
L
Linus Torvalds 已提交
101
{
L
Len Brown 已提交
102
	struct list_head *node, *next;
L
Linus Torvalds 已提交
103 104 105

	spin_lock(&acpi_device_lock);
	list_for_each_safe(node, next, &acpi_wakeup_device_list) {
106 107
		struct acpi_device *dev =
			container_of(node, struct acpi_device, wakeup_list);
L
Linus Torvalds 已提交
108

109
		if (!dev->wakeup.flags.valid)
L
Linus Torvalds 已提交
110
			continue;
111 112 113

		if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
		    || sleep_state > (u32) dev->wakeup.sleep_state) {
114 115 116 117 118 119 120
			if (dev->wakeup.flags.run_wake) {
				spin_unlock(&acpi_device_lock);
				acpi_set_gpe_type(dev->wakeup.gpe_device,
						  dev->wakeup.gpe_number,
						  ACPI_GPE_TYPE_WAKE_RUN);
				/* Re-enable it, since set_gpe_type will disable it */
				acpi_enable_gpe(dev->wakeup.gpe_device,
121
						dev->wakeup.gpe_number);
122 123
				spin_lock(&acpi_device_lock);
			}
L
Linus Torvalds 已提交
124
			continue;
125
		}
L
Linus Torvalds 已提交
126 127 128 129 130

		spin_unlock(&acpi_device_lock);
		acpi_disable_wakeup_device_power(dev);
		/* Never disable run-wake GPE */
		if (!dev->wakeup.flags.run_wake) {
L
Len Brown 已提交
131
			acpi_disable_gpe(dev->wakeup.gpe_device,
132
					 dev->wakeup.gpe_number);
L
Len Brown 已提交
133 134
			acpi_clear_gpe(dev->wakeup.gpe_device,
				       dev->wakeup.gpe_number, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142
		}
		spin_lock(&acpi_device_lock);
	}
	spin_unlock(&acpi_device_lock);
}

static int __init acpi_wakeup_device_init(void)
{
L
Len Brown 已提交
143
	struct list_head *node, *next;
L
Linus Torvalds 已提交
144 145 146 147 148 149

	if (acpi_disabled)
		return 0;

	spin_lock(&acpi_device_lock);
	list_for_each_safe(node, next, &acpi_wakeup_device_list) {
L
Len Brown 已提交
150 151 152
		struct acpi_device *dev = container_of(node,
						       struct acpi_device,
						       wakeup_list);
L
Linus Torvalds 已提交
153
		/* In case user doesn't load button driver */
154 155 156 157 158 159 160
		if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
			continue;
		spin_unlock(&acpi_device_lock);
		acpi_set_gpe_type(dev->wakeup.gpe_device,
				  dev->wakeup.gpe_number,
				  ACPI_GPE_TYPE_WAKE_RUN);
		acpi_enable_gpe(dev->wakeup.gpe_device,
161
				dev->wakeup.gpe_number);
162 163
		dev->wakeup.state.enabled = 1;
		spin_lock(&acpi_device_lock);
L
Linus Torvalds 已提交
164 165 166 167 168 169
	}
	spin_unlock(&acpi_device_lock);
	return 0;
}

late_initcall(acpi_wakeup_device_init);