1 /* drivers/input/keycombo.c
3 * Copyright (C) 2014 Google, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/input.h>
17 #include <linux/keycombo.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/reboot.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
24 struct keycombo_state {
25 struct input_handler input_handler;
26 unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
27 unsigned long upbit[BITS_TO_LONGS(KEY_CNT)];
28 unsigned long key[BITS_TO_LONGS(KEY_CNT)];
30 struct workqueue_struct *wq;
34 struct delayed_work key_down_work;
36 struct work_struct key_up_work;
37 void (*key_up_fn)(void *);
38 void (*key_down_fn)(void *);
41 struct wakeup_source combo_held_wake_source;
42 struct wakeup_source combo_up_wake_source;
45 static void do_key_down(struct work_struct *work)
47 struct delayed_work *dwork = container_of(work, struct delayed_work,
49 struct keycombo_state *state = container_of(dwork,
50 struct keycombo_state, key_down_work);
51 if (state->key_down_fn)
52 state->key_down_fn(state->priv);
55 static void do_key_up(struct work_struct *work)
57 struct keycombo_state *state = container_of(work, struct keycombo_state,
60 state->key_up_fn(state->priv);
61 __pm_relax(&state->combo_up_wake_source);
64 static void keycombo_event(struct input_handle *handle, unsigned int type,
65 unsigned int code, int value)
68 struct keycombo_state *state = handle->private;
76 if (!test_bit(code, state->keybit))
79 spin_lock_irqsave(&state->lock, flags);
80 if (!test_bit(code, state->key) == !value)
82 __change_bit(code, state->key);
83 if (test_bit(code, state->upbit)) {
94 if (state->key_down == state->key_down_target && state->key_up == 0) {
95 __pm_stay_awake(&state->combo_held_wake_source);
96 state->key_is_down = 1;
97 if (queue_delayed_work(state->wq, &state->key_down_work,
99 pr_debug("Key down work already queued!");
100 } else if (state->key_is_down) {
101 if (!cancel_delayed_work(&state->key_down_work)) {
102 __pm_stay_awake(&state->combo_up_wake_source);
103 queue_work(state->wq, &state->key_up_work);
105 __pm_relax(&state->combo_held_wake_source);
106 state->key_is_down = 0;
109 spin_unlock_irqrestore(&state->lock, flags);
112 static int keycombo_connect(struct input_handler *handler,
113 struct input_dev *dev,
114 const struct input_device_id *id)
118 struct input_handle *handle;
119 struct keycombo_state *state =
120 container_of(handler, struct keycombo_state, input_handler);
121 for (i = 0; i < KEY_MAX; i++) {
122 if (test_bit(i, state->keybit) && test_bit(i, dev->keybit))
128 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
133 handle->handler = handler;
134 handle->name = KEYCOMBO_NAME;
135 handle->private = state;
137 ret = input_register_handle(handle);
139 goto err_input_register_handle;
141 ret = input_open_device(handle);
143 goto err_input_open_device;
147 err_input_open_device:
148 input_unregister_handle(handle);
149 err_input_register_handle:
154 static void keycombo_disconnect(struct input_handle *handle)
156 input_close_device(handle);
157 input_unregister_handle(handle);
161 static const struct input_device_id keycombo_ids[] = {
163 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
164 .evbit = { BIT_MASK(EV_KEY) },
168 MODULE_DEVICE_TABLE(input, keycombo_ids);
170 static int keycombo_probe(struct platform_device *pdev)
174 struct keycombo_state *state;
175 struct keycombo_platform_data *pdata = pdev->dev.platform_data;
180 state = kzalloc(sizeof(*state), GFP_KERNEL);
184 spin_lock_init(&state->lock);
185 keyp = pdata->keys_down;
186 while ((key = *keyp++)) {
189 state->key_down_target++;
190 __set_bit(key, state->keybit);
192 if (pdata->keys_up) {
193 keyp = pdata->keys_up;
194 while ((key = *keyp++)) {
197 __set_bit(key, state->keybit);
198 __set_bit(key, state->upbit);
202 state->wq = alloc_ordered_workqueue("keycombo", 0);
206 state->priv = pdata->priv;
208 if (pdata->key_down_fn)
209 state->key_down_fn = pdata->key_down_fn;
210 INIT_DELAYED_WORK(&state->key_down_work, do_key_down);
212 if (pdata->key_up_fn)
213 state->key_up_fn = pdata->key_up_fn;
214 INIT_WORK(&state->key_up_work, do_key_up);
216 wakeup_source_init(&state->combo_held_wake_source, "key combo");
217 wakeup_source_init(&state->combo_up_wake_source, "key combo up");
218 state->delay = msecs_to_jiffies(pdata->key_down_delay);
220 state->input_handler.event = keycombo_event;
221 state->input_handler.connect = keycombo_connect;
222 state->input_handler.disconnect = keycombo_disconnect;
223 state->input_handler.name = KEYCOMBO_NAME;
224 state->input_handler.id_table = keycombo_ids;
225 ret = input_register_handler(&state->input_handler);
230 platform_set_drvdata(pdev, state);
234 int keycombo_remove(struct platform_device *pdev)
236 struct keycombo_state *state = platform_get_drvdata(pdev);
237 input_unregister_handler(&state->input_handler);
238 destroy_workqueue(state->wq);
244 struct platform_driver keycombo_driver = {
245 .driver.name = KEYCOMBO_NAME,
246 .probe = keycombo_probe,
247 .remove = keycombo_remove,
250 static int __init keycombo_init(void)
252 return platform_driver_register(&keycombo_driver);
255 static void __exit keycombo_exit(void)
257 return platform_driver_unregister(&keycombo_driver);
260 module_init(keycombo_init);
261 module_exit(keycombo_exit);