summaryrefslogtreecommitdiff
path: root/drivers/input/power.c
blob: 526e6070600c8a02a29df0283d55fcae79217b0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * $Id: power.c,v 1.10 2001/09/25 09:17:15 vojtech Exp $
 *
 *  Copyright (c) 2001 "Crazy" James Simmons
 *
 *  Input driver Power Management.
 *
 *  Sponsored by Transvirtual Technology.
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Should you need to contact me, the author, you can do so by
 * e-mail - mail your message to <jsimmons@transvirtual.com>.
 */

#include <linux/module.h>
#include <linux/config.h>
#include <linux/input.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/tty.h>
#include <linux/delay.h>
#include <linux/pm.h>

static struct input_handler power_handler;

/*
 * Power management can't be done in a interrupt context. So we have to
 * use keventd.
 */
static int suspend_button_pushed = 0;
static void suspend_button_task_handler(void *data)
{
        udelay(200); /* debounce */
        suspend_button_pushed = 0;
}

static DECLARE_WORK(suspend_button_task, suspend_button_task_handler, NULL);

static void power_event(struct input_handle *handle, unsigned int type,
		        unsigned int code, int down)
{
	struct input_dev *dev = handle->dev;

	printk("Entering power_event\n");

	if (type == EV_PWR) {
		switch (code) {
			case KEY_SUSPEND:
				printk("Powering down entire device\n");

				if (!suspend_button_pushed) {
                			suspend_button_pushed = 1;
                        		schedule_work(&suspend_button_task);
                		}
				break;
			case KEY_POWER:
				/* Hum power down the machine. */
				break;
			default:
				return;
		}
	}

	if (type == EV_KEY) {
		switch (code) {
			case KEY_SUSPEND:
				printk("Powering down input device\n");
				/* This is risky. See pm.h for details. */
				if (dev->state != PM_RESUME)
					dev->state = PM_RESUME;
				else
					dev->state = PM_SUSPEND;
				pm_send(dev->pm_dev, dev->state, dev);
				break;
			case KEY_POWER:
				/* Turn the input device off completely ? */
				break;
			default:
				return;
		}
	}
	return;
}

static struct input_handle *power_connect(struct input_handler *handler,
					  struct input_dev *dev,
					  struct input_device_id *id)
{
	struct input_handle *handle;

	if (!(handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL)))
		return NULL;

	handle->dev = dev;
	handle->handler = handler;

	input_open_device(handle);

	printk(KERN_INFO "power.c: Adding power management to input layer\n");
	return handle;
}

static void power_disconnect(struct input_handle *handle)
{
	input_close_device(handle);
	kfree(handle);
}

static struct input_device_id power_ids[] = {
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
		.evbit = { BIT(EV_KEY) },
		.keybit = { [LONG(KEY_SUSPEND)] = BIT(KEY_SUSPEND) }
	},
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
		.evbit = { BIT(EV_KEY) },
		.keybit = { [LONG(KEY_POWER)] = BIT(KEY_POWER) }
	},
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT,
		.evbit = { BIT(EV_PWR) },
	},
	{ }, 	/* Terminating entry */
};

MODULE_DEVICE_TABLE(input, power_ids);

static struct input_handler power_handler = {
	.event =	power_event,
	.connect =	power_connect,
	.disconnect =	power_disconnect,
	.name =		"power",
	.id_table =	power_ids,
};

static int __init power_init(void)
{
	input_register_handler(&power_handler);
	return 0;
}

static void __exit power_exit(void)
{
	input_unregister_handler(&power_handler);
}

module_init(power_init);
module_exit(power_exit);

MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
MODULE_DESCRIPTION("Input Power Management driver");
MODULE_LICENSE("GPL");