blob: 46115a39209821830c1a3789d1cb93d67ff35746 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Event char devices, giving access to raw input device events.
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
10
Joe Perchesda0c4902010-11-29 23:33:07 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#define EVDEV_MINOR_BASE 64
14#define EVDEV_MINORS 32
Henrik Rydberg63a64042010-06-10 12:05:24 -070015#define EVDEV_MIN_BUFFER_SIZE 64U
16#define EVDEV_BUF_PACKETS 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/poll.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
Daniel Stone92eb77d2013-10-31 00:25:34 -070021#include <linux/vmalloc.h>
22#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/init.h>
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +010025#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/device.h>
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -070028#include <linux/cdev.h>
Philip Langdale2d56f3a2008-10-16 22:31:42 -040029#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -080031enum evdev_clock_type {
32 EV_CLK_REAL = 0,
33 EV_CLK_MONO,
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -080034 EV_CLK_MAX
35};
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct evdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 int open;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct input_handle handle;
40 wait_queue_head_t wait;
Arnd Bergmann2be85272010-03-04 15:50:28 +010041 struct evdev_client __rcu *grab;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040042 struct list_head client_list;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040043 spinlock_t client_lock; /* protects client_list */
44 struct mutex mutex;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040045 struct device dev;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -070046 struct cdev cdev;
Dmitry Torokhov20da92d2010-07-15 23:27:36 -070047 bool exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040050struct evdev_client {
Jeff Brown9fb0f142011-04-12 23:29:38 -070051 unsigned int head;
52 unsigned int tail;
Jeff Browncdda9112011-04-26 22:16:11 -070053 unsigned int packet_head; /* [future] position of the first element of next packet */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040054 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct fasync_struct *fasync;
56 struct evdev *evdev;
57 struct list_head node;
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -070058 unsigned int clk_type;
David Herrmannc7dc6572013-09-07 12:23:05 -070059 bool revoked;
David Herrmann06a16292015-10-24 16:20:18 -070060 unsigned long *evmasks[EV_CNT];
Jeff Brown9fb0f142011-04-12 23:29:38 -070061 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070062 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
David Herrmann06a16292015-10-24 16:20:18 -070065static size_t evdev_get_mask_cnt(unsigned int type)
66{
67 static const size_t counts[EV_CNT] = {
68 /* EV_SYN==0 is EV_CNT, _not_ SYN_CNT, see EVIOCGBIT */
69 [EV_SYN] = EV_CNT,
70 [EV_KEY] = KEY_CNT,
71 [EV_REL] = REL_CNT,
72 [EV_ABS] = ABS_CNT,
73 [EV_MSC] = MSC_CNT,
74 [EV_SW] = SW_CNT,
75 [EV_LED] = LED_CNT,
76 [EV_SND] = SND_CNT,
77 [EV_FF] = FF_CNT,
78 };
79
80 return (type < EV_CNT) ? counts[type] : 0;
81}
82
83/* requires the buffer lock to be held */
84static bool __evdev_is_filtered(struct evdev_client *client,
85 unsigned int type,
86 unsigned int code)
87{
88 unsigned long *mask;
89 size_t cnt;
90
91 /* EV_SYN and unknown codes are never filtered */
92 if (type == EV_SYN || type >= EV_CNT)
93 return false;
94
95 /* first test whether the type is filtered */
96 mask = client->evmasks[0];
97 if (mask && !test_bit(type, mask))
98 return true;
99
100 /* unknown values are never filtered */
101 cnt = evdev_get_mask_cnt(type);
102 if (!cnt || code >= cnt)
103 return false;
104
105 mask = client->evmasks[type];
106 return mask && !test_bit(code, mask);
107}
108
David Herrmann48318022013-04-07 21:13:19 -0700109/* flush queued events of type @type, caller must hold client->buffer_lock */
110static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
111{
112 unsigned int i, head, num;
113 unsigned int mask = client->bufsize - 1;
114 bool is_report;
115 struct input_event *ev;
116
117 BUG_ON(type == EV_SYN);
118
119 head = client->tail;
120 client->packet_head = client->tail;
121
122 /* init to 1 so a leading SYN_REPORT will not be dropped */
123 num = 1;
124
125 for (i = client->tail; i != client->head; i = (i + 1) & mask) {
126 ev = &client->buffer[i];
127 is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
128
129 if (ev->type == type) {
130 /* drop matched entry */
131 continue;
132 } else if (is_report && !num) {
133 /* drop empty SYN_REPORT groups */
134 continue;
135 } else if (head != i) {
136 /* move entry to fill the gap */
Deepa Dinamani152194f2018-01-07 17:44:42 -0800137 client->buffer[head] = *ev;
David Herrmann48318022013-04-07 21:13:19 -0700138 }
139
140 num++;
141 head = (head + 1) & mask;
142
143 if (is_report) {
144 num = 0;
145 client->packet_head = head;
146 }
147 }
148
149 client->head = head;
150}
151
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800152static void __evdev_queue_syn_dropped(struct evdev_client *client)
David Herrmann48318022013-04-07 21:13:19 -0700153{
David Herrmann48318022013-04-07 21:13:19 -0700154 struct input_event ev;
155 ktime_t time;
Deepa Dinamani152194f2018-01-07 17:44:42 -0800156 struct timespec64 ts;
David Herrmann48318022013-04-07 21:13:19 -0700157
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800158 time = client->clk_type == EV_CLK_REAL ?
159 ktime_get_real() :
160 client->clk_type == EV_CLK_MONO ?
161 ktime_get() :
162 ktime_get_boottime();
David Herrmann48318022013-04-07 21:13:19 -0700163
Deepa Dinamani152194f2018-01-07 17:44:42 -0800164 ts = ktime_to_timespec64(time);
165 ev.input_event_sec = ts.tv_sec;
166 ev.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
David Herrmann48318022013-04-07 21:13:19 -0700167 ev.type = EV_SYN;
168 ev.code = SYN_DROPPED;
169 ev.value = 0;
170
David Herrmann48318022013-04-07 21:13:19 -0700171 client->buffer[client->head++] = ev;
172 client->head &= client->bufsize - 1;
173
174 if (unlikely(client->head == client->tail)) {
175 /* drop queue but keep our SYN_DROPPED event */
176 client->tail = (client->head - 1) & (client->bufsize - 1);
177 client->packet_head = client->tail;
178 }
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800179}
David Herrmann48318022013-04-07 21:13:19 -0700180
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800181static void evdev_queue_syn_dropped(struct evdev_client *client)
182{
183 unsigned long flags;
184
185 spin_lock_irqsave(&client->buffer_lock, flags);
186 __evdev_queue_syn_dropped(client);
David Herrmann48318022013-04-07 21:13:19 -0700187 spin_unlock_irqrestore(&client->buffer_lock, flags);
188}
189
Anshul Garg0c3e9942015-01-15 09:06:50 -0800190static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
191{
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800192 unsigned long flags;
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -0700193 unsigned int clk_type;
Anshul Garg0c3e9942015-01-15 09:06:50 -0800194
195 switch (clkid) {
196
197 case CLOCK_REALTIME:
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -0700198 clk_type = EV_CLK_REAL;
Anshul Garg0c3e9942015-01-15 09:06:50 -0800199 break;
Thomas Gleixnerf2d6fdb2018-03-01 17:33:34 +0100200 case CLOCK_BOOTTIME:
Anshul Garg0c3e9942015-01-15 09:06:50 -0800201 case CLOCK_MONOTONIC:
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -0700202 clk_type = EV_CLK_MONO;
Anshul Garg0c3e9942015-01-15 09:06:50 -0800203 break;
Anshul Garg0c3e9942015-01-15 09:06:50 -0800204 default:
205 return -EINVAL;
206 }
207
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -0700208 if (client->clk_type != clk_type) {
209 client->clk_type = clk_type;
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800210
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -0700211 /*
212 * Flush pending events and queue SYN_DROPPED event,
213 * but only if the queue is not empty.
214 */
215 spin_lock_irqsave(&client->buffer_lock, flags);
216
217 if (client->head != client->tail) {
218 client->packet_head = client->head = client->tail;
219 __evdev_queue_syn_dropped(client);
220 }
221
222 spin_unlock_irqrestore(&client->buffer_lock, flags);
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800223 }
224
Anshul Garg0c3e9942015-01-15 09:06:50 -0800225 return 0;
226}
227
Henrik Rydberga274ac12012-08-29 20:48:02 +0200228static void __pass_event(struct evdev_client *client,
229 const struct input_event *event)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400230{
Jeff Brown9fb0f142011-04-12 23:29:38 -0700231 client->buffer[client->head++] = *event;
232 client->head &= client->bufsize - 1;
233
234 if (unlikely(client->head == client->tail)) {
235 /*
236 * This effectively "drops" all unconsumed events, leaving
237 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
238 */
239 client->tail = (client->head - 2) & (client->bufsize - 1);
240
Deepa Dinamani152194f2018-01-07 17:44:42 -0800241 client->buffer[client->tail].input_event_sec =
242 event->input_event_sec;
243 client->buffer[client->tail].input_event_usec =
244 event->input_event_usec;
Jeff Brown9fb0f142011-04-12 23:29:38 -0700245 client->buffer[client->tail].type = EV_SYN;
246 client->buffer[client->tail].code = SYN_DROPPED;
247 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -0700248
249 client->packet_head = client->tail;
250 }
251
252 if (event->type == EV_SYN && event->code == SYN_REPORT) {
253 client->packet_head = client->head;
254 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -0700255 }
Henrik Rydberga274ac12012-08-29 20:48:02 +0200256}
257
258static void evdev_pass_values(struct evdev_client *client,
259 const struct input_value *vals, unsigned int count,
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800260 ktime_t *ev_time)
Henrik Rydberga274ac12012-08-29 20:48:02 +0200261{
262 struct evdev *evdev = client->evdev;
263 const struct input_value *v;
264 struct input_event event;
Deepa Dinamani152194f2018-01-07 17:44:42 -0800265 struct timespec64 ts;
Henrik Rydberga274ac12012-08-29 20:48:02 +0200266 bool wakeup = false;
267
David Herrmannc7dc6572013-09-07 12:23:05 -0700268 if (client->revoked)
269 return;
270
Deepa Dinamani152194f2018-01-07 17:44:42 -0800271 ts = ktime_to_timespec64(ev_time[client->clk_type]);
272 event.input_event_sec = ts.tv_sec;
273 event.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
Henrik Rydberga274ac12012-08-29 20:48:02 +0200274
275 /* Interrupts are disabled, just acquire the lock. */
276 spin_lock(&client->buffer_lock);
277
278 for (v = vals; v != vals + count; v++) {
David Herrmann06a16292015-10-24 16:20:18 -0700279 if (__evdev_is_filtered(client, v->type, v->code))
280 continue;
281
282 if (v->type == EV_SYN && v->code == SYN_REPORT) {
283 /* drop empty SYN_REPORT */
284 if (client->packet_head == client->head)
285 continue;
286
287 wakeup = true;
288 }
289
Henrik Rydberga274ac12012-08-29 20:48:02 +0200290 event.type = v->type;
291 event.code = v->code;
292 event.value = v->value;
293 __pass_event(client, &event);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200294 }
Jeff Brown9fb0f142011-04-12 23:29:38 -0700295
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400296 spin_unlock(&client->buffer_lock);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200297
298 if (wakeup)
299 wake_up_interruptible(&evdev->wait);
300}
301
302/*
303 * Pass incoming events to all connected clients.
304 */
305static void evdev_events(struct input_handle *handle,
306 const struct input_value *vals, unsigned int count)
307{
308 struct evdev *evdev = handle->private;
309 struct evdev_client *client;
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800310 ktime_t ev_time[EV_CLK_MAX];
Henrik Rydberga274ac12012-08-29 20:48:02 +0200311
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800312 ev_time[EV_CLK_MONO] = ktime_get();
313 ev_time[EV_CLK_REAL] = ktime_mono_to_real(ev_time[EV_CLK_MONO]);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200314
315 rcu_read_lock();
316
317 client = rcu_dereference(evdev->grab);
318
319 if (client)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800320 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200321 else
322 list_for_each_entry_rcu(client, &evdev->client_list, node)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800323 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200324
325 rcu_read_unlock();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400326}
327
328/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400329 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400330 */
331static void evdev_event(struct input_handle *handle,
332 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Henrik Rydberga274ac12012-08-29 20:48:02 +0200334 struct input_value vals[] = { { type, code, value } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Henrik Rydberga274ac12012-08-29 20:48:02 +0200336 evdev_events(handle, vals, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339static int evdev_fasync(int fd, struct file *file, int on)
340{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400341 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400342
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700343 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400346static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400348 struct evdev_client *client = file->private_data;
349 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400350
Takashi Iwaieb38f3a2015-09-03 22:20:00 -0700351 mutex_lock(&evdev->mutex);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400352
Takashi Iwaieb38f3a2015-09-03 22:20:00 -0700353 if (evdev->exist && !client->revoked)
354 input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400355
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400356 mutex_unlock(&evdev->mutex);
Takashi Iwaieb38f3a2015-09-03 22:20:00 -0700357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400360static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400362 struct evdev *evdev = container_of(dev, struct evdev, dev);
363
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400364 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 kfree(evdev);
366}
367
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400368/*
369 * Grabs an event device (along with underlying input device).
370 * This function is called with evdev->mutex taken.
371 */
372static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
373{
374 int error;
375
376 if (evdev->grab)
377 return -EBUSY;
378
379 error = input_grab_device(&evdev->handle);
380 if (error)
381 return error;
382
383 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400384
385 return 0;
386}
387
388static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
389{
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700390 struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
391 lockdep_is_held(&evdev->mutex));
392
393 if (grab != client)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400394 return -EINVAL;
395
396 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400397 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400398 input_release_device(&evdev->handle);
399
400 return 0;
401}
402
403static void evdev_attach_client(struct evdev *evdev,
404 struct evdev_client *client)
405{
406 spin_lock(&evdev->client_lock);
407 list_add_tail_rcu(&client->node, &evdev->client_list);
408 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400409}
410
411static void evdev_detach_client(struct evdev *evdev,
412 struct evdev_client *client)
413{
414 spin_lock(&evdev->client_lock);
415 list_del_rcu(&client->node);
416 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400417 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400418}
419
420static int evdev_open_device(struct evdev *evdev)
421{
422 int retval;
423
424 retval = mutex_lock_interruptible(&evdev->mutex);
425 if (retval)
426 return retval;
427
428 if (!evdev->exist)
429 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400430 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400431 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400432 if (retval)
433 evdev->open--;
434 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400435
436 mutex_unlock(&evdev->mutex);
437 return retval;
438}
439
440static void evdev_close_device(struct evdev *evdev)
441{
442 mutex_lock(&evdev->mutex);
443
444 if (evdev->exist && !--evdev->open)
445 input_close_device(&evdev->handle);
446
447 mutex_unlock(&evdev->mutex);
448}
449
450/*
451 * Wake up users waiting for IO so they can disconnect from
452 * dead device.
453 */
454static void evdev_hangup(struct evdev *evdev)
455{
456 struct evdev_client *client;
457
458 spin_lock(&evdev->client_lock);
459 list_for_each_entry(client, &evdev->client_list, node)
460 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
461 spin_unlock(&evdev->client_lock);
462
463 wake_up_interruptible(&evdev->wait);
464}
465
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400466static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400468 struct evdev_client *client = file->private_data;
469 struct evdev *evdev = client->evdev;
David Herrmann06a16292015-10-24 16:20:18 -0700470 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400472 mutex_lock(&evdev->mutex);
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700473 evdev_ungrab(evdev, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400474 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400476 evdev_detach_client(evdev, client);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700477
David Herrmann06a16292015-10-24 16:20:18 -0700478 for (i = 0; i < EV_CNT; ++i)
479 kfree(client->evmasks[i]);
480
Pekka Enberg67367fd2015-05-15 13:45:40 -0700481 kvfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400483 evdev_close_device(evdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486}
487
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700488static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
489{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700490 unsigned int n_events =
491 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
492 EVDEV_MIN_BUFFER_SIZE);
493
494 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700495}
496
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400497static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700499 struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
500 unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700501 unsigned int size = sizeof(struct evdev_client) +
502 bufsize * sizeof(struct input_event);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400503 struct evdev_client *client;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400504 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Daniel Stone92eb77d2013-10-31 00:25:34 -0700506 client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
507 if (!client)
508 client = vzalloc(size);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700509 if (!client)
510 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700512 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400513 spin_lock_init(&client->buffer_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400514 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400515 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400517 error = evdev_open_device(evdev);
518 if (error)
519 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400521 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800522 nonseekable_open(inode, file);
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400525
526 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400527 evdev_detach_client(evdev, client);
Andrew Morton92788ac2014-12-02 15:59:31 -0800528 kvfree(client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400529 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400532static ssize_t evdev_write(struct file *file, const char __user *buffer,
533 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400535 struct evdev_client *client = file->private_data;
536 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800538 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700540 if (count != 0 && count < input_event_size())
Peter Korsgaard439581e2011-02-25 09:30:46 -0800541 return -EINVAL;
542
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400543 retval = mutex_lock_interruptible(&evdev->mutex);
544 if (retval)
545 return retval;
546
David Herrmannc7dc6572013-09-07 12:23:05 -0700547 if (!evdev->exist || client->revoked) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400548 retval = -ENODEV;
549 goto out;
550 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500551
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700552 while (retval + input_event_size() <= count) {
553
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400554 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400555 retval = -EFAULT;
556 goto out;
557 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800558 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400559
560 input_inject_event(&evdev->handle,
561 event.type, event.code, event.value);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700562 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500563
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400564 out:
565 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500566 return retval;
567}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500568
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400569static int evdev_fetch_next_event(struct evdev_client *client,
570 struct input_event *event)
571{
572 int have_event;
573
574 spin_lock_irq(&client->buffer_lock);
575
Dima Zavin566cf5b2011-12-30 15:16:44 -0800576 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400577 if (have_event) {
578 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700579 client->tail &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400580 }
581
582 spin_unlock_irq(&client->buffer_lock);
583
584 return have_event;
585}
586
587static ssize_t evdev_read(struct file *file, char __user *buffer,
588 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400590 struct evdev_client *client = file->private_data;
591 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400592 struct input_event event;
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700593 size_t read = 0;
594 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700596 if (count != 0 && count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -EINVAL;
598
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700599 for (;;) {
David Herrmannc7dc6572013-09-07 12:23:05 -0700600 if (!evdev->exist || client->revoked)
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700601 return -ENODEV;
602
603 if (client->packet_head == client->tail &&
604 (file->f_flags & O_NONBLOCK))
605 return -EAGAIN;
606
607 /*
608 * count == 0 is special - no IO is done but we check
609 * for error conditions (see above).
610 */
611 if (count == 0)
612 break;
613
614 while (read + input_event_size() <= count &&
615 evdev_fetch_next_event(client, &event)) {
616
617 if (input_event_to_user(buffer + read, &event))
618 return -EFAULT;
619
620 read += input_event_size();
621 }
622
623 if (read)
624 break;
625
626 if (!(file->f_flags & O_NONBLOCK)) {
627 error = wait_event_interruptible(evdev->wait,
628 client->packet_head != client->tail ||
David Herrmannc7dc6572013-09-07 12:23:05 -0700629 !evdev->exist || client->revoked);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700630 if (error)
631 return error;
632 }
Dima Zavin509f87c2011-12-30 15:16:44 -0800633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700635 return read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/* No kernel lock - fine */
Al Viroafc9a422017-07-03 06:39:46 -0400639static __poll_t evdev_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400641 struct evdev_client *client = file->private_data;
642 struct evdev *evdev = client->evdev;
Al Viroafc9a422017-07-03 06:39:46 -0400643 __poll_t mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400644
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400645 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700646
David Herrmannc7dc6572013-09-07 12:23:05 -0700647 if (evdev->exist && !client->revoked)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800648 mask = EPOLLOUT | EPOLLWRNORM;
David Herrmannc7dc6572013-09-07 12:23:05 -0700649 else
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800650 mask = EPOLLHUP | EPOLLERR;
David Herrmannc7dc6572013-09-07 12:23:05 -0700651
Jeff Browncdda9112011-04-26 22:16:11 -0700652 if (client->packet_head != client->tail)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800653 mask |= EPOLLIN | EPOLLRDNORM;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700654
655 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500658#ifdef CONFIG_COMPAT
659
660#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700661#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500662
663#ifdef __BIG_ENDIAN
664static int bits_to_user(unsigned long *bits, unsigned int maxbit,
665 unsigned int maxlen, void __user *p, int compat)
666{
667 int len, i;
668
669 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700670 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400671 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500672 len = maxlen;
673
674 for (i = 0; i < len / sizeof(compat_long_t); i++)
675 if (copy_to_user((compat_long_t __user *) p + i,
676 (compat_long_t *) bits +
677 i + 1 - ((i % 2) << 1),
678 sizeof(compat_long_t)))
679 return -EFAULT;
680 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700681 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500682 if (len > maxlen)
683 len = maxlen;
684
685 if (copy_to_user(p, bits, len))
686 return -EFAULT;
687 }
688
689 return len;
690}
David Herrmann06a16292015-10-24 16:20:18 -0700691
692static int bits_from_user(unsigned long *bits, unsigned int maxbit,
693 unsigned int maxlen, const void __user *p, int compat)
694{
695 int len, i;
696
697 if (compat) {
698 if (maxlen % sizeof(compat_long_t))
699 return -EINVAL;
700
701 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
702 if (len > maxlen)
703 len = maxlen;
704
705 for (i = 0; i < len / sizeof(compat_long_t); i++)
706 if (copy_from_user((compat_long_t *) bits +
707 i + 1 - ((i % 2) << 1),
708 (compat_long_t __user *) p + i,
709 sizeof(compat_long_t)))
710 return -EFAULT;
711 if (i % 2)
712 *((compat_long_t *) bits + i - 1) = 0;
713
714 } else {
715 if (maxlen % sizeof(long))
716 return -EINVAL;
717
718 len = BITS_TO_LONGS(maxbit) * sizeof(long);
719 if (len > maxlen)
720 len = maxlen;
721
722 if (copy_from_user(bits, p, len))
723 return -EFAULT;
724 }
725
726 return len;
727}
728
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500729#else
David Herrmann06a16292015-10-24 16:20:18 -0700730
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500731static int bits_to_user(unsigned long *bits, unsigned int maxbit,
732 unsigned int maxlen, void __user *p, int compat)
733{
734 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700735 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
736 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500737
738 if (len > maxlen)
739 len = maxlen;
740
741 return copy_to_user(p, bits, len) ? -EFAULT : len;
742}
David Herrmann06a16292015-10-24 16:20:18 -0700743
744static int bits_from_user(unsigned long *bits, unsigned int maxbit,
745 unsigned int maxlen, const void __user *p, int compat)
746{
747 size_t chunk_size = compat ? sizeof(compat_long_t) : sizeof(long);
748 int len;
749
750 if (maxlen % chunk_size)
751 return -EINVAL;
752
753 len = compat ? BITS_TO_LONGS_COMPAT(maxbit) : BITS_TO_LONGS(maxbit);
754 len *= chunk_size;
755 if (len > maxlen)
756 len = maxlen;
757
758 return copy_from_user(bits, p, len) ? -EFAULT : len;
759}
760
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500761#endif /* __BIG_ENDIAN */
762
763#else
764
765static int bits_to_user(unsigned long *bits, unsigned int maxbit,
766 unsigned int maxlen, void __user *p, int compat)
767{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700768 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500769
770 if (len > maxlen)
771 len = maxlen;
772
773 return copy_to_user(p, bits, len) ? -EFAULT : len;
774}
775
David Herrmann06a16292015-10-24 16:20:18 -0700776static int bits_from_user(unsigned long *bits, unsigned int maxbit,
777 unsigned int maxlen, const void __user *p, int compat)
778{
779 int len;
780
781 if (maxlen % sizeof(long))
782 return -EINVAL;
783
784 len = BITS_TO_LONGS(maxbit) * sizeof(long);
785 if (len > maxlen)
786 len = maxlen;
787
788 return copy_from_user(bits, p, len) ? -EFAULT : len;
789}
790
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500791#endif /* CONFIG_COMPAT */
792
793static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
794{
795 int len;
796
797 if (!str)
798 return -ENOENT;
799
800 len = strlen(str) + 1;
801 if (len > maxlen)
802 len = maxlen;
803
804 return copy_to_user(p, str, len) ? -EFAULT : len;
805}
806
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700807static int handle_eviocgbit(struct input_dev *dev,
808 unsigned int type, unsigned int size,
809 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400810{
811 unsigned long *bits;
812 int len;
813
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700814 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400815
816 case 0: bits = dev->evbit; len = EV_MAX; break;
817 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
818 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
819 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
820 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
821 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
822 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
823 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
824 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
825 default: return -EINVAL;
826 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400827
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700828 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400829}
Linus Torvalds5402a732008-08-05 11:42:42 -0400830
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800831static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
832{
833 struct input_keymap_entry ke = {
834 .len = sizeof(unsigned int),
835 .flags = 0,
836 };
837 int __user *ip = (int __user *)p;
838 int error;
839
840 /* legacy case */
841 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
842 return -EFAULT;
843
844 error = input_get_keycode(dev, &ke);
845 if (error)
846 return error;
847
848 if (put_user(ke.keycode, ip + 1))
849 return -EFAULT;
850
851 return 0;
852}
853
854static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700855{
856 struct input_keymap_entry ke;
857 int error;
858
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800859 if (copy_from_user(&ke, p, sizeof(ke)))
860 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700861
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800862 error = input_get_keycode(dev, &ke);
863 if (error)
864 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700865
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800866 if (copy_to_user(p, &ke, sizeof(ke)))
867 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700868
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700869 return 0;
870}
871
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800872static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
873{
874 struct input_keymap_entry ke = {
875 .len = sizeof(unsigned int),
876 .flags = 0,
877 };
878 int __user *ip = (int __user *)p;
879
880 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
881 return -EFAULT;
882
883 if (get_user(ke.keycode, ip + 1))
884 return -EFAULT;
885
886 return input_set_keycode(dev, &ke);
887}
888
889static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700890{
891 struct input_keymap_entry ke;
892
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800893 if (copy_from_user(&ke, p, sizeof(ke)))
894 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700895
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800896 if (ke.len > sizeof(ke.scancode))
897 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700898
899 return input_set_keycode(dev, &ke);
900}
901
David Herrmann48318022013-04-07 21:13:19 -0700902/*
903 * If we transfer state to the user, we should flush all pending events
904 * of the same type from the client's queue. Otherwise, they might end up
905 * with duplicate events, which can screw up client's state tracking.
906 * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
907 * event so user-space will notice missing events.
908 *
909 * LOCKING:
910 * We need to take event_lock before buffer_lock to avoid dead-locks. But we
911 * need the even_lock only to guarantee consistent state. We can safely release
912 * it while flushing the queue. This allows input-core to handle filters while
913 * we flush the queue.
914 */
915static int evdev_handle_get_val(struct evdev_client *client,
916 struct input_dev *dev, unsigned int type,
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700917 unsigned long *bits, unsigned int maxbit,
918 unsigned int maxlen, void __user *p,
919 int compat)
David Herrmann48318022013-04-07 21:13:19 -0700920{
921 int ret;
922 unsigned long *mem;
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700923 size_t len;
David Herrmann48318022013-04-07 21:13:19 -0700924
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700925 len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
926 mem = kmalloc(len, GFP_KERNEL);
David Herrmann48318022013-04-07 21:13:19 -0700927 if (!mem)
928 return -ENOMEM;
929
930 spin_lock_irq(&dev->event_lock);
931 spin_lock(&client->buffer_lock);
932
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700933 memcpy(mem, bits, len);
David Herrmann48318022013-04-07 21:13:19 -0700934
935 spin_unlock(&dev->event_lock);
936
937 __evdev_flush_queue(client, type);
938
939 spin_unlock_irq(&client->buffer_lock);
940
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700941 ret = bits_to_user(mem, maxbit, maxlen, p, compat);
David Herrmann48318022013-04-07 21:13:19 -0700942 if (ret < 0)
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800943 evdev_queue_syn_dropped(client);
David Herrmann48318022013-04-07 21:13:19 -0700944
945 kfree(mem);
946
947 return ret;
948}
949
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100950static int evdev_handle_mt_request(struct input_dev *dev,
951 unsigned int size,
952 int __user *ip)
953{
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200954 const struct input_mt *mt = dev->mt;
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100955 unsigned int code;
956 int max_slots;
957 int i;
958
959 if (get_user(code, &ip[0]))
960 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200961 if (!mt || !input_is_mt_value(code))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100962 return -EINVAL;
963
964 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200965 for (i = 0; i < mt->num_slots && i < max_slots; i++) {
966 int value = input_mt_get_value(&mt->slots[i], code);
967 if (put_user(value, &ip[1 + i]))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100968 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200969 }
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100970
971 return 0;
972}
973
David Herrmannc7dc6572013-09-07 12:23:05 -0700974static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
975 struct file *file)
976{
977 client->revoked = true;
978 evdev_ungrab(evdev, client);
979 input_flush_device(&evdev->handle, file);
980 wake_up_interruptible(&evdev->wait);
981
982 return 0;
983}
984
David Herrmann06a16292015-10-24 16:20:18 -0700985/* must be called with evdev-mutex held */
986static int evdev_set_mask(struct evdev_client *client,
987 unsigned int type,
988 const void __user *codes,
989 u32 codes_size,
990 int compat)
991{
992 unsigned long flags, *mask, *oldmask;
993 size_t cnt;
994 int error;
995
996 /* we allow unknown types and 'codes_size > size' for forward-compat */
997 cnt = evdev_get_mask_cnt(type);
998 if (!cnt)
999 return 0;
1000
1001 mask = kcalloc(sizeof(unsigned long), BITS_TO_LONGS(cnt), GFP_KERNEL);
1002 if (!mask)
1003 return -ENOMEM;
1004
1005 error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);
1006 if (error < 0) {
1007 kfree(mask);
1008 return error;
1009 }
1010
1011 spin_lock_irqsave(&client->buffer_lock, flags);
1012 oldmask = client->evmasks[type];
1013 client->evmasks[type] = mask;
1014 spin_unlock_irqrestore(&client->buffer_lock, flags);
1015
1016 kfree(oldmask);
1017
1018 return 0;
1019}
1020
1021/* must be called with evdev-mutex held */
1022static int evdev_get_mask(struct evdev_client *client,
1023 unsigned int type,
1024 void __user *codes,
1025 u32 codes_size,
1026 int compat)
1027{
1028 unsigned long *mask;
1029 size_t cnt, size, xfer_size;
1030 int i;
1031 int error;
1032
1033 /* we allow unknown types and 'codes_size > size' for forward-compat */
1034 cnt = evdev_get_mask_cnt(type);
1035 size = sizeof(unsigned long) * BITS_TO_LONGS(cnt);
1036 xfer_size = min_t(size_t, codes_size, size);
1037
1038 if (cnt > 0) {
1039 mask = client->evmasks[type];
1040 if (mask) {
1041 error = bits_to_user(mask, cnt - 1,
1042 xfer_size, codes, compat);
1043 if (error < 0)
1044 return error;
1045 } else {
1046 /* fake mask with all bits set */
1047 for (i = 0; i < xfer_size; i++)
1048 if (put_user(0xffU, (u8 __user *)codes + i))
1049 return -EFAULT;
1050 }
1051 }
1052
1053 if (xfer_size < codes_size)
1054 if (clear_user(codes + xfer_size, codes_size - xfer_size))
1055 return -EFAULT;
1056
1057 return 0;
1058}
1059
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001060static long evdev_do_ioctl(struct file *file, unsigned int cmd,
1061 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001063 struct evdev_client *client = file->private_data;
1064 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct input_dev *dev = evdev->handle.dev;
1066 struct input_absinfo abs;
David Herrmann06a16292015-10-24 16:20:18 -07001067 struct input_mask mask;
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001068 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001069 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -08001070 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001071 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001072 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001074 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 switch (cmd) {
1076
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001077 case EVIOCGVERSION:
1078 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001080 case EVIOCGID:
1081 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
1082 return -EFAULT;
1083 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -04001084
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001085 case EVIOCGREP:
1086 if (!test_bit(EV_REP, dev->evbit))
1087 return -ENOSYS;
1088 if (put_user(dev->rep[REP_DELAY], ip))
1089 return -EFAULT;
1090 if (put_user(dev->rep[REP_PERIOD], ip + 1))
1091 return -EFAULT;
1092 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -04001093
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001094 case EVIOCSREP:
1095 if (!test_bit(EV_REP, dev->evbit))
1096 return -ENOSYS;
1097 if (get_user(u, ip))
1098 return -EFAULT;
1099 if (get_user(v, ip + 1))
1100 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -04001101
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001102 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
1103 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001104
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001107 case EVIOCRMFF:
1108 return input_ff_erase(dev, (int)(unsigned long) p, file);
1109
1110 case EVIOCGEFFECTS:
1111 i = test_bit(EV_FF, dev->evbit) ?
1112 dev->ff->max_effects : 0;
1113 if (put_user(i, ip))
1114 return -EFAULT;
1115 return 0;
1116
1117 case EVIOCGRAB:
1118 if (p)
1119 return evdev_grab(evdev, client);
1120 else
1121 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -08001122
David Herrmannc7dc6572013-09-07 12:23:05 -07001123 case EVIOCREVOKE:
1124 if (p)
1125 return -EINVAL;
1126 else
1127 return evdev_revoke(evdev, client, file);
1128
David Herrmann06a16292015-10-24 16:20:18 -07001129 case EVIOCGMASK: {
1130 void __user *codes_ptr;
1131
1132 if (copy_from_user(&mask, p, sizeof(mask)))
1133 return -EFAULT;
1134
1135 codes_ptr = (void __user *)(unsigned long)mask.codes_ptr;
1136 return evdev_get_mask(client,
1137 mask.type, codes_ptr, mask.codes_size,
1138 compat_mode);
1139 }
1140
1141 case EVIOCSMASK: {
1142 const void __user *codes_ptr;
1143
1144 if (copy_from_user(&mask, p, sizeof(mask)))
1145 return -EFAULT;
1146
1147 codes_ptr = (const void __user *)(unsigned long)mask.codes_ptr;
1148 return evdev_set_mask(client,
1149 mask.type, codes_ptr, mask.codes_size,
1150 compat_mode);
1151 }
1152
John Stultza80b83b2012-02-03 00:19:07 -08001153 case EVIOCSCLOCKID:
1154 if (copy_from_user(&i, p, sizeof(unsigned int)))
1155 return -EFAULT;
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -08001156
1157 return evdev_set_clk_type(client, i);
John Stultza80b83b2012-02-03 00:19:07 -08001158
Dmitry Torokhovab4e0192010-12-14 23:53:21 -08001159 case EVIOCGKEYCODE:
1160 return evdev_handle_get_keycode(dev, p);
1161
1162 case EVIOCSKEYCODE:
1163 return evdev_handle_set_keycode(dev, p);
1164
1165 case EVIOCGKEYCODE_V2:
1166 return evdev_handle_get_keycode_v2(dev, p);
1167
1168 case EVIOCSKEYCODE_V2:
1169 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001170 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001171
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001172 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001173
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001174 /* Now check variable-length commands */
1175#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001176 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001177
Henrik Rydberg85b77202010-12-18 20:51:13 +01001178 case EVIOCGPROP(0):
1179 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
1180 size, p, compat_mode);
1181
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +01001182 case EVIOCGMTSLOTS(0):
1183 return evdev_handle_mt_request(dev, size, ip);
1184
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001185 case EVIOCGKEY(0):
David Herrmann48318022013-04-07 21:13:19 -07001186 return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
1187 KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001188
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001189 case EVIOCGLED(0):
David Herrmann48318022013-04-07 21:13:19 -07001190 return evdev_handle_get_val(client, dev, EV_LED, dev->led,
1191 LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001192
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001193 case EVIOCGSND(0):
David Herrmann48318022013-04-07 21:13:19 -07001194 return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
1195 SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001196
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001197 case EVIOCGSW(0):
David Herrmann48318022013-04-07 21:13:19 -07001198 return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
1199 SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001200
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001201 case EVIOCGNAME(0):
1202 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001203
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001204 case EVIOCGPHYS(0):
1205 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001206
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001207 case EVIOCGUNIQ(0):
1208 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001209
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001210 case EVIOC_MASK_SIZE(EVIOCSFF):
1211 if (input_ff_effect_from_user(p, size, &effect))
1212 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001213
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001214 error = input_ff_upload(dev, &effect, file);
Elias Vanderstuyftfc7392a2014-03-29 12:08:45 -07001215 if (error)
1216 return error;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001217
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001218 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
1219 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001220
Elias Vanderstuyftfc7392a2014-03-29 12:08:45 -07001221 return 0;
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001222 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001223
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001224 /* Multi-number variable-length handlers */
1225 if (_IOC_TYPE(cmd) != 'E')
1226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001228 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001230 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
1231 return handle_eviocgbit(dev,
1232 _IOC_NR(cmd) & EV_MAX, size,
1233 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001235 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -04001236
Daniel Mack0a74a1d2010-10-18 08:43:30 -07001237 if (!dev->absinfo)
1238 return -EINVAL;
1239
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001240 t = _IOC_NR(cmd) & ABS_MAX;
1241 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -04001242
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001243 if (copy_to_user(p, &abs, min_t(size_t,
1244 size, sizeof(struct input_absinfo))))
1245 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -04001246
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001247 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001250
Daniel Mackf9ce6eb2010-10-18 08:43:50 -07001251 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001252
1253 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
1254
Daniel Mack0a74a1d2010-10-18 08:43:30 -07001255 if (!dev->absinfo)
1256 return -EINVAL;
1257
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001258 t = _IOC_NR(cmd) & ABS_MAX;
1259
1260 if (copy_from_user(&abs, p, min_t(size_t,
1261 size, sizeof(struct input_absinfo))))
1262 return -EFAULT;
1263
1264 if (size < sizeof(struct input_absinfo))
1265 abs.resolution = 0;
1266
1267 /* We can't change number of reserved MT slots */
1268 if (t == ABS_MT_SLOT)
1269 return -EINVAL;
1270
1271 /*
1272 * Take event lock to ensure that we are not
1273 * changing device parameters in the middle
1274 * of event.
1275 */
1276 spin_lock_irq(&dev->event_lock);
1277 dev->absinfo[t] = abs;
1278 spin_unlock_irq(&dev->event_lock);
1279
1280 return 0;
1281 }
1282 }
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EINVAL;
1285}
1286
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001287static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
1288 void __user *p, int compat_mode)
1289{
1290 struct evdev_client *client = file->private_data;
1291 struct evdev *evdev = client->evdev;
1292 int retval;
1293
1294 retval = mutex_lock_interruptible(&evdev->mutex);
1295 if (retval)
1296 return retval;
1297
David Herrmannc7dc6572013-09-07 12:23:05 -07001298 if (!evdev->exist || client->revoked) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001299 retval = -ENODEV;
1300 goto out;
1301 }
1302
1303 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
1304
1305 out:
1306 mutex_unlock(&evdev->mutex);
1307 return retval;
1308}
1309
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001310static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1311{
1312 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
1313}
1314
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001315#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001316static long evdev_ioctl_compat(struct file *file,
1317 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001318{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001319 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001320}
1321#endif
1322
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001323static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001324 .owner = THIS_MODULE,
1325 .read = evdev_read,
1326 .write = evdev_write,
1327 .poll = evdev_poll,
1328 .open = evdev_open,
1329 .release = evdev_release,
1330 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001331#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001332 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001333#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001334 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001335 .flush = evdev_flush,
1336 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337};
1338
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001339/*
1340 * Mark device non-existent. This disables writes, ioctls and
1341 * prevents new users from opening the device. Already posted
1342 * blocking reads will stay, however new ones will fail.
1343 */
1344static void evdev_mark_dead(struct evdev *evdev)
1345{
1346 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001347 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001348 mutex_unlock(&evdev->mutex);
1349}
1350
1351static void evdev_cleanup(struct evdev *evdev)
1352{
1353 struct input_handle *handle = &evdev->handle;
1354
1355 evdev_mark_dead(evdev);
1356 evdev_hangup(evdev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001357
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001358 /* evdev is marked dead so no one else accesses evdev->open */
1359 if (evdev->open) {
1360 input_flush_device(handle, NULL);
1361 input_close_device(handle);
1362 }
1363}
1364
1365/*
1366 * Create new evdev device. Note that input core serializes calls
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001367 * to connect and disconnect.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001368 */
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001369static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1370 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 struct evdev *evdev;
1373 int minor;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001374 int dev_no;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001375 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001377 minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
1378 if (minor < 0) {
1379 error = minor;
1380 pr_err("failed to reserve new minor: %d\n", error);
1381 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001384 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001385 if (!evdev) {
1386 error = -ENOMEM;
1387 goto err_free_minor;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001390 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001391 spin_lock_init(&evdev->client_lock);
1392 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 init_waitqueue_head(&evdev->wait);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001394 evdev->exist = true;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001395
1396 dev_no = minor;
1397 /* Normalize device number if it falls into legacy range */
1398 if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
1399 dev_no -= EVDEV_MINOR_BASE;
1400 dev_set_name(&evdev->dev, "event%d", dev_no);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001401
Dmitry Torokhova7097ff2008-04-01 00:22:53 -04001402 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001403 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 evdev->handle.handler = handler;
1405 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001406
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001407 evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001408 evdev->dev.class = &input_class;
1409 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001410 evdev->dev.release = evdev_free;
1411 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001413 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001414 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001415 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001417 cdev_init(&evdev->cdev, &evdev_fops);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001418
Logan Gunthorpe358a89c2017-03-17 12:48:11 -06001419 error = cdev_device_add(&evdev->cdev, &evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001420 if (error)
1421 goto err_cleanup_evdev;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001422
1423 return 0;
1424
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001425 err_cleanup_evdev:
1426 evdev_cleanup(evdev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001427 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001428 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001429 put_device(&evdev->dev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001430 err_free_minor:
1431 input_free_minor(minor);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001432 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435static void evdev_disconnect(struct input_handle *handle)
1436{
1437 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Logan Gunthorpe358a89c2017-03-17 12:48:11 -06001439 cdev_device_del(&evdev->cdev, &evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001440 evdev_cleanup(evdev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001441 input_free_minor(MINOR(evdev->dev.devt));
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001442 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001443 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001446static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 { .driver_info = 1 }, /* Matches all devices */
1448 { }, /* Terminating zero entry */
1449};
1450
1451MODULE_DEVICE_TABLE(input, evdev_ids);
1452
1453static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001454 .event = evdev_event,
Henrik Rydberga274ac12012-08-29 20:48:02 +02001455 .events = evdev_events,
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001456 .connect = evdev_connect,
1457 .disconnect = evdev_disconnect,
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001458 .legacy_minors = true,
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001459 .minor = EVDEV_MINOR_BASE,
1460 .name = "evdev",
1461 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462};
1463
1464static int __init evdev_init(void)
1465{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001466 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
1469static void __exit evdev_exit(void)
1470{
1471 input_unregister_handler(&evdev_handler);
1472}
1473
1474module_init(evdev_init);
1475module_exit(evdev_exit);
1476
1477MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1478MODULE_DESCRIPTION("Input driver event char devices");
1479MODULE_LICENSE("GPL");