Input: rework handle creation code
[deliverable/linux.git] / drivers / input / power.c
CommitLineData
1da177e4
LT
1/*
2 * $Id: power.c,v 1.10 2001/09/25 09:17:15 vojtech Exp $
3 *
4 * Copyright (c) 2001 "Crazy" James Simmons
5 *
6 * Input driver Power Management.
7 *
8 * Sponsored by Transvirtual Technology.
9 */
10
11/*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Should you need to contact me, the author, you can do so by
27 * e-mail - mail your message to <jsimmons@transvirtual.com>.
28 */
29
30#include <linux/module.h>
1da177e4
LT
31#include <linux/input.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/tty.h>
35#include <linux/delay.h>
36#include <linux/pm.h>
37
38static struct input_handler power_handler;
39
40/*
41 * Power management can't be done in a interrupt context. So we have to
42 * use keventd.
43 */
5b2a0826
DT
44static int suspend_button_pushed;
45static void suspend_button_task_handler(struct work_struct *work)
1da177e4
LT
46{
47 udelay(200); /* debounce */
48 suspend_button_pushed = 0;
49}
50
5b2a0826 51static DECLARE_WORK(suspend_button_task, suspend_button_task_handler);
1da177e4
LT
52
53static void power_event(struct input_handle *handle, unsigned int type,
54 unsigned int code, int down)
55{
56 struct input_dev *dev = handle->dev;
57
58 printk("Entering power_event\n");
59
60 if (type == EV_PWR) {
61 switch (code) {
62 case KEY_SUSPEND:
63 printk("Powering down entire device\n");
64
65 if (!suspend_button_pushed) {
5b2a0826
DT
66 suspend_button_pushed = 1;
67 schedule_work(&suspend_button_task);
68 }
1da177e4
LT
69 break;
70 case KEY_POWER:
71 /* Hum power down the machine. */
72 break;
73 default:
74 return;
75 }
76 }
77
78 if (type == EV_KEY) {
79 switch (code) {
80 case KEY_SUSPEND:
81 printk("Powering down input device\n");
82 /* This is risky. See pm.h for details. */
83 if (dev->state != PM_RESUME)
84 dev->state = PM_RESUME;
85 else
86 dev->state = PM_SUSPEND;
5b2a0826 87 /* pm_send(dev->pm_dev, dev->state, dev); */
1da177e4
LT
88 break;
89 case KEY_POWER:
90 /* Turn the input device off completely ? */
91 break;
92 default:
93 return;
94 }
95 }
96 return;
97}
98
5b2a0826
DT
99static int power_connect(struct input_handler *handler, struct input_dev *dev,
100 const struct input_device_id *id)
1da177e4
LT
101{
102 struct input_handle *handle;
5b2a0826 103 int error;
1da177e4 104
5b2a0826
DT
105 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
106 if (!handle)
107 return -ENOMEM;
1da177e4
LT
108
109 handle->dev = dev;
110 handle->handler = handler;
5b2a0826 111 handle->name = "power";
1da177e4 112
5b2a0826
DT
113 error = input_register_handle(handle);
114 if (error)
115 goto err_free_handle;
1da177e4 116
5b2a0826
DT
117 error = input_open_device(handle);
118 if (error)
119 goto err_unregister_handle;
120
121 return 0;
122
123 err_unregister_handle:
124 input_unregister_handle(handle);
125 err_free_handle:
126 kfree(handle);
127 return error;
1da177e4
LT
128}
129
130static void power_disconnect(struct input_handle *handle)
131{
132 input_close_device(handle);
5b2a0826 133 input_unregister_handle(handle);
1da177e4
LT
134 kfree(handle);
135}
136
66e66118 137static const struct input_device_id power_ids[] = {
1da177e4
LT
138 {
139 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
140 .evbit = { BIT(EV_KEY) },
141 .keybit = { [LONG(KEY_SUSPEND)] = BIT(KEY_SUSPEND) }
142 },
143 {
144 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
145 .evbit = { BIT(EV_KEY) },
146 .keybit = { [LONG(KEY_POWER)] = BIT(KEY_POWER) }
147 },
148 {
149 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
150 .evbit = { BIT(EV_PWR) },
151 },
5b2a0826 152 { }, /* Terminating entry */
1da177e4
LT
153};
154
155MODULE_DEVICE_TABLE(input, power_ids);
156
157static struct input_handler power_handler = {
158 .event = power_event,
159 .connect = power_connect,
160 .disconnect = power_disconnect,
161 .name = "power",
162 .id_table = power_ids,
163};
164
165static int __init power_init(void)
166{
4263cf0f 167 return input_register_handler(&power_handler);
1da177e4
LT
168}
169
170static void __exit power_exit(void)
171{
172 input_unregister_handler(&power_handler);
173}
174
175module_init(power_init);
176module_exit(power_exit);
177
178MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
179MODULE_DESCRIPTION("Input Power Management driver");
180MODULE_LICENSE("GPL");
This page took 0.14972 seconds and 5 git commands to generate.