• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* drivers/input/keycombo.c
2  *
3  * Copyright (C) 2014 Google, Inc.
4  *
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.
8  *
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.
13  *
14  */
15 
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>
23 
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)];
29 	spinlock_t lock;
30 	struct  workqueue_struct *wq;
31 	int key_down_target;
32 	int key_down;
33 	int key_up;
34 	struct delayed_work key_down_work;
35 	int delay;
36 	struct work_struct key_up_work;
37 	void (*key_up_fn)(void *);
38 	void (*key_down_fn)(void *);
39 	void *priv;
40 	int key_is_down;
41 	struct wakeup_source combo_held_wake_source;
42 	struct wakeup_source combo_up_wake_source;
43 };
44 
do_key_down(struct work_struct * work)45 static void do_key_down(struct work_struct *work)
46 {
47 	struct delayed_work *dwork = container_of(work, struct delayed_work,
48 									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);
53 }
54 
do_key_up(struct work_struct * work)55 static void do_key_up(struct work_struct *work)
56 {
57 	struct keycombo_state *state = container_of(work, struct keycombo_state,
58 								key_up_work);
59 	if (state->key_up_fn)
60 		state->key_up_fn(state->priv);
61 	__pm_relax(&state->combo_up_wake_source);
62 }
63 
keycombo_event(struct input_handle * handle,unsigned int type,unsigned int code,int value)64 static void keycombo_event(struct input_handle *handle, unsigned int type,
65 		unsigned int code, int value)
66 {
67 	unsigned long flags;
68 	struct keycombo_state *state = handle->private;
69 
70 	if (type != EV_KEY)
71 		return;
72 
73 	if (code >= KEY_MAX)
74 		return;
75 
76 	if (!test_bit(code, state->keybit))
77 		return;
78 
79 	spin_lock_irqsave(&state->lock, flags);
80 	if (!test_bit(code, state->key) == !value)
81 		goto done;
82 	__change_bit(code, state->key);
83 	if (test_bit(code, state->upbit)) {
84 		if (value)
85 			state->key_up++;
86 		else
87 			state->key_up--;
88 	} else {
89 		if (value)
90 			state->key_down++;
91 		else
92 			state->key_down--;
93 	}
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,
98 								state->delay))
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);
104 		}
105 		__pm_relax(&state->combo_held_wake_source);
106 		state->key_is_down = 0;
107 	}
108 done:
109 	spin_unlock_irqrestore(&state->lock, flags);
110 }
111 
keycombo_connect(struct input_handler * handler,struct input_dev * dev,const struct input_device_id * id)112 static int keycombo_connect(struct input_handler *handler,
113 		struct input_dev *dev,
114 		const struct input_device_id *id)
115 {
116 	int i;
117 	int ret;
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))
123 			break;
124 	}
125 	if (i == KEY_MAX)
126 		return -ENODEV;
127 
128 	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
129 	if (!handle)
130 		return -ENOMEM;
131 
132 	handle->dev = dev;
133 	handle->handler = handler;
134 	handle->name = KEYCOMBO_NAME;
135 	handle->private = state;
136 
137 	ret = input_register_handle(handle);
138 	if (ret)
139 		goto err_input_register_handle;
140 
141 	ret = input_open_device(handle);
142 	if (ret)
143 		goto err_input_open_device;
144 
145 	return 0;
146 
147 err_input_open_device:
148 	input_unregister_handle(handle);
149 err_input_register_handle:
150 	kfree(handle);
151 	return ret;
152 }
153 
keycombo_disconnect(struct input_handle * handle)154 static void keycombo_disconnect(struct input_handle *handle)
155 {
156 	input_close_device(handle);
157 	input_unregister_handle(handle);
158 	kfree(handle);
159 }
160 
161 static const struct input_device_id keycombo_ids[] = {
162 		{
163 				.flags = INPUT_DEVICE_ID_MATCH_EVBIT,
164 				.evbit = { BIT_MASK(EV_KEY) },
165 		},
166 		{ },
167 };
168 MODULE_DEVICE_TABLE(input, keycombo_ids);
169 
keycombo_probe(struct platform_device * pdev)170 static int keycombo_probe(struct platform_device *pdev)
171 {
172 	int ret;
173 	int key, *keyp;
174 	struct keycombo_state *state;
175 	struct keycombo_platform_data *pdata = pdev->dev.platform_data;
176 
177 	if (!pdata)
178 		return -EINVAL;
179 
180 	state = kzalloc(sizeof(*state), GFP_KERNEL);
181 	if (!state)
182 		return -ENOMEM;
183 
184 	spin_lock_init(&state->lock);
185 	keyp = pdata->keys_down;
186 	while ((key = *keyp++)) {
187 		if (key >= KEY_MAX)
188 			continue;
189 		state->key_down_target++;
190 		__set_bit(key, state->keybit);
191 	}
192 	if (pdata->keys_up) {
193 		keyp = pdata->keys_up;
194 		while ((key = *keyp++)) {
195 			if (key >= KEY_MAX)
196 				continue;
197 			__set_bit(key, state->keybit);
198 			__set_bit(key, state->upbit);
199 		}
200 	}
201 
202 	state->wq = alloc_ordered_workqueue("keycombo", 0);
203 	if (!state->wq)
204 		return -ENOMEM;
205 
206 	state->priv = pdata->priv;
207 
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);
211 
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);
215 
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);
219 
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);
226 	if (ret) {
227 		kfree(state);
228 		return ret;
229 	}
230 	platform_set_drvdata(pdev, state);
231 	return 0;
232 }
233 
keycombo_remove(struct platform_device * pdev)234 int keycombo_remove(struct platform_device *pdev)
235 {
236 	struct keycombo_state *state = platform_get_drvdata(pdev);
237 	input_unregister_handler(&state->input_handler);
238 	destroy_workqueue(state->wq);
239 	kfree(state);
240 	return 0;
241 }
242 
243 
244 struct platform_driver keycombo_driver = {
245 		.driver.name = KEYCOMBO_NAME,
246 		.probe = keycombo_probe,
247 		.remove = keycombo_remove,
248 };
249 
keycombo_init(void)250 static int __init keycombo_init(void)
251 {
252 	return platform_driver_register(&keycombo_driver);
253 }
254 
keycombo_exit(void)255 static void __exit keycombo_exit(void)
256 {
257 	return platform_driver_unregister(&keycombo_driver);
258 }
259 
260 module_init(keycombo_init);
261 module_exit(keycombo_exit);
262