blob: 867c2cfd003819dc7ce58a527c8ef806c5f327f7 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Event char devices, giving access to raw input device events.
4 *
5 * Copyright (c) 1999-2002 Vojtech Pavlik
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Joe Perchesda0c4902010-11-29 23:33:07 -08008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#define EVDEV_MINOR_BASE 64
11#define EVDEV_MINORS 32
Henrik Rydberg63a64042010-06-10 12:05:24 -070012#define EVDEV_MIN_BUFFER_SIZE 64U
13#define EVDEV_BUF_PACKETS 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <linux/poll.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040016#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
Daniel Stone92eb77d2013-10-31 00:25:34 -070018#include <linux/vmalloc.h>
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/init.h>
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +010022#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/device.h>
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -070025#include <linux/cdev.h>
Philip Langdale2d56f3a2008-10-16 22:31:42 -040026#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -080028enum evdev_clock_type {
29 EV_CLK_REAL = 0,
30 EV_CLK_MONO,
31 EV_CLK_BOOT,
32 EV_CLK_MAX
33};
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035struct evdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 int open;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct input_handle handle;
38 wait_queue_head_t wait;
Arnd Bergmann2be85272010-03-04 15:50:28 +010039 struct evdev_client __rcu *grab;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040040 struct list_head client_list;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040041 spinlock_t client_lock; /* protects client_list */
42 struct mutex mutex;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040043 struct device dev;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -070044 struct cdev cdev;
Dmitry Torokhov20da92d2010-07-15 23:27:36 -070045 bool exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040048struct evdev_client {
Jeff Brown9fb0f142011-04-12 23:29:38 -070049 unsigned int head;
50 unsigned int tail;
Jeff Browncdda9112011-04-26 22:16:11 -070051 unsigned int packet_head; /* [future] position of the first element of next packet */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040052 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct fasync_struct *fasync;
54 struct evdev *evdev;
55 struct list_head node;
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -070056 unsigned int clk_type;
David Herrmannc7dc6572013-09-07 12:23:05 -070057 bool revoked;
David Herrmann06a16292015-10-24 16:20:18 -070058 unsigned long *evmasks[EV_CNT];
Jeff Brown9fb0f142011-04-12 23:29:38 -070059 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070060 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
David Herrmann06a16292015-10-24 16:20:18 -070063static size_t evdev_get_mask_cnt(unsigned int type)
64{
65 static const size_t counts[EV_CNT] = {
66 /* EV_SYN==0 is EV_CNT, _not_ SYN_CNT, see EVIOCGBIT */
67 [EV_SYN] = EV_CNT,
68 [EV_KEY] = KEY_CNT,
69 [EV_REL] = REL_CNT,
70 [EV_ABS] = ABS_CNT,
71 [EV_MSC] = MSC_CNT,
72 [EV_SW] = SW_CNT,
73 [EV_LED] = LED_CNT,
74 [EV_SND] = SND_CNT,
75 [EV_FF] = FF_CNT,
76 };
77
78 return (type < EV_CNT) ? counts[type] : 0;
79}
80
81/* requires the buffer lock to be held */
82static bool __evdev_is_filtered(struct evdev_client *client,
83 unsigned int type,
84 unsigned int code)
85{
86 unsigned long *mask;
87 size_t cnt;
88
89 /* EV_SYN and unknown codes are never filtered */
90 if (type == EV_SYN || type >= EV_CNT)
91 return false;
92
93 /* first test whether the type is filtered */
94 mask = client->evmasks[0];
95 if (mask && !test_bit(type, mask))
96 return true;
97
98 /* unknown values are never filtered */
99 cnt = evdev_get_mask_cnt(type);
100 if (!cnt || code >= cnt)
101 return false;
102
103 mask = client->evmasks[type];
104 return mask && !test_bit(code, mask);
105}
106
David Herrmann48318022013-04-07 21:13:19 -0700107/* flush queued events of type @type, caller must hold client->buffer_lock */
108static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
109{
110 unsigned int i, head, num;
111 unsigned int mask = client->bufsize - 1;
112 bool is_report;
113 struct input_event *ev;
114
115 BUG_ON(type == EV_SYN);
116
117 head = client->tail;
118 client->packet_head = client->tail;
119
120 /* init to 1 so a leading SYN_REPORT will not be dropped */
121 num = 1;
122
123 for (i = client->tail; i != client->head; i = (i + 1) & mask) {
124 ev = &client->buffer[i];
125 is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
126
127 if (ev->type == type) {
128 /* drop matched entry */
129 continue;
130 } else if (is_report && !num) {
131 /* drop empty SYN_REPORT groups */
132 continue;
133 } else if (head != i) {
134 /* move entry to fill the gap */
Deepa Dinamani152194f2018-01-07 17:44:42 -0800135 client->buffer[head] = *ev;
David Herrmann48318022013-04-07 21:13:19 -0700136 }
137
138 num++;
139 head = (head + 1) & mask;
140
141 if (is_report) {
142 num = 0;
143 client->packet_head = head;
144 }
145 }
146
147 client->head = head;
148}
149
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800150static void __evdev_queue_syn_dropped(struct evdev_client *client)
David Herrmann48318022013-04-07 21:13:19 -0700151{
David Herrmann48318022013-04-07 21:13:19 -0700152 struct input_event ev;
153 ktime_t time;
Deepa Dinamani152194f2018-01-07 17:44:42 -0800154 struct timespec64 ts;
David Herrmann48318022013-04-07 21:13:19 -0700155
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800156 time = client->clk_type == EV_CLK_REAL ?
157 ktime_get_real() :
158 client->clk_type == EV_CLK_MONO ?
159 ktime_get() :
160 ktime_get_boottime();
David Herrmann48318022013-04-07 21:13:19 -0700161
Deepa Dinamani152194f2018-01-07 17:44:42 -0800162 ts = ktime_to_timespec64(time);
163 ev.input_event_sec = ts.tv_sec;
164 ev.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
David Herrmann48318022013-04-07 21:13:19 -0700165 ev.type = EV_SYN;
166 ev.code = SYN_DROPPED;
167 ev.value = 0;
168
David Herrmann48318022013-04-07 21:13:19 -0700169 client->buffer[client->head++] = ev;
170 client->head &= client->bufsize - 1;
171
172 if (unlikely(client->head == client->tail)) {
173 /* drop queue but keep our SYN_DROPPED event */
174 client->tail = (client->head - 1) & (client->bufsize - 1);
175 client->packet_head = client->tail;
176 }
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800177}
David Herrmann48318022013-04-07 21:13:19 -0700178
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800179static void evdev_queue_syn_dropped(struct evdev_client *client)
180{
181 unsigned long flags;
182
183 spin_lock_irqsave(&client->buffer_lock, flags);
184 __evdev_queue_syn_dropped(client);
David Herrmann48318022013-04-07 21:13:19 -0700185 spin_unlock_irqrestore(&client->buffer_lock, flags);
186}
187
Anshul Garg0c3e9942015-01-15 09:06:50 -0800188static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
189{
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800190 unsigned long flags;
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -0700191 unsigned int clk_type;
Anshul Garg0c3e9942015-01-15 09:06:50 -0800192
193 switch (clkid) {
194
195 case CLOCK_REALTIME:
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -0700196 clk_type = EV_CLK_REAL;
Anshul Garg0c3e9942015-01-15 09:06:50 -0800197 break;
198 case CLOCK_MONOTONIC:
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -0700199 clk_type = EV_CLK_MONO;
Anshul Garg0c3e9942015-01-15 09:06:50 -0800200 break;
201 case CLOCK_BOOTTIME:
Aniroop Mathurbf5f18d2015-10-30 04:15:37 -0700202 clk_type = EV_CLK_BOOT;
Anshul Garg0c3e9942015-01-15 09:06:50 -0800203 break;
204 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]);
314 ev_time[EV_CLK_BOOT] = ktime_mono_to_any(ev_time[EV_CLK_MONO],
315 TK_OFFS_BOOT);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200316
317 rcu_read_lock();
318
319 client = rcu_dereference(evdev->grab);
320
321 if (client)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800322 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200323 else
324 list_for_each_entry_rcu(client, &evdev->client_list, node)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800325 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200326
327 rcu_read_unlock();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400328}
329
330/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400331 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400332 */
333static void evdev_event(struct input_handle *handle,
334 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Henrik Rydberga274ac12012-08-29 20:48:02 +0200336 struct input_value vals[] = { { type, code, value } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Henrik Rydberga274ac12012-08-29 20:48:02 +0200338 evdev_events(handle, vals, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341static int evdev_fasync(int fd, struct file *file, int on)
342{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400343 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400344
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700345 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400348static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400350 struct evdev_client *client = file->private_data;
351 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400352
Takashi Iwaieb38f3a2015-09-03 22:20:00 -0700353 mutex_lock(&evdev->mutex);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400354
Takashi Iwaieb38f3a2015-09-03 22:20:00 -0700355 if (evdev->exist && !client->revoked)
356 input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400357
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400358 mutex_unlock(&evdev->mutex);
Takashi Iwaieb38f3a2015-09-03 22:20:00 -0700359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400362static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400364 struct evdev *evdev = container_of(dev, struct evdev, dev);
365
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400366 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 kfree(evdev);
368}
369
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400370/*
371 * Grabs an event device (along with underlying input device).
372 * This function is called with evdev->mutex taken.
373 */
374static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
375{
376 int error;
377
378 if (evdev->grab)
379 return -EBUSY;
380
381 error = input_grab_device(&evdev->handle);
382 if (error)
383 return error;
384
385 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400386
387 return 0;
388}
389
390static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
391{
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700392 struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
393 lockdep_is_held(&evdev->mutex));
394
395 if (grab != client)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400396 return -EINVAL;
397
398 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400399 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400400 input_release_device(&evdev->handle);
401
402 return 0;
403}
404
405static void evdev_attach_client(struct evdev *evdev,
406 struct evdev_client *client)
407{
408 spin_lock(&evdev->client_lock);
409 list_add_tail_rcu(&client->node, &evdev->client_list);
410 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400411}
412
413static void evdev_detach_client(struct evdev *evdev,
414 struct evdev_client *client)
415{
416 spin_lock(&evdev->client_lock);
417 list_del_rcu(&client->node);
418 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400419 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400420}
421
422static int evdev_open_device(struct evdev *evdev)
423{
424 int retval;
425
426 retval = mutex_lock_interruptible(&evdev->mutex);
427 if (retval)
428 return retval;
429
430 if (!evdev->exist)
431 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400432 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400433 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400434 if (retval)
435 evdev->open--;
436 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400437
438 mutex_unlock(&evdev->mutex);
439 return retval;
440}
441
442static void evdev_close_device(struct evdev *evdev)
443{
444 mutex_lock(&evdev->mutex);
445
446 if (evdev->exist && !--evdev->open)
447 input_close_device(&evdev->handle);
448
449 mutex_unlock(&evdev->mutex);
450}
451
452/*
453 * Wake up users waiting for IO so they can disconnect from
454 * dead device.
455 */
456static void evdev_hangup(struct evdev *evdev)
457{
458 struct evdev_client *client;
459
460 spin_lock(&evdev->client_lock);
461 list_for_each_entry(client, &evdev->client_list, node)
462 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
463 spin_unlock(&evdev->client_lock);
464
465 wake_up_interruptible(&evdev->wait);
466}
467
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400468static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400470 struct evdev_client *client = file->private_data;
471 struct evdev *evdev = client->evdev;
David Herrmann06a16292015-10-24 16:20:18 -0700472 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400474 mutex_lock(&evdev->mutex);
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700475 evdev_ungrab(evdev, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400476 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400478 evdev_detach_client(evdev, client);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700479
David Herrmann06a16292015-10-24 16:20:18 -0700480 for (i = 0; i < EV_CNT; ++i)
Andy Shevchenko60780912018-08-01 15:47:47 -0700481 bitmap_free(client->evmasks[i]);
David Herrmann06a16292015-10-24 16:20:18 -0700482
Pekka Enberg67367fd2015-05-15 13:45:40 -0700483 kvfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400485 evdev_close_device(evdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
488}
489
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700490static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
491{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700492 unsigned int n_events =
493 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
494 EVDEV_MIN_BUFFER_SIZE);
495
496 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700497}
498
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400499static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700501 struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
502 unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
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
Gustavo A. R. Silvaecdf3a92019-04-03 10:51:10 -0700506 client = kzalloc(struct_size(client, buffer, bufsize),
507 GFP_KERNEL | __GFP_NOWARN);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700508 if (!client)
Gustavo A. R. Silvaecdf3a92019-04-03 10:51:10 -0700509 client = vzalloc(struct_size(client, buffer, bufsize));
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700510 if (!client)
511 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700513 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400514 spin_lock_init(&client->buffer_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400515 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400516 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400518 error = evdev_open_device(evdev);
519 if (error)
520 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400522 file->private_data = client;
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300523 stream_open(inode, file);
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400526
527 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400528 evdev_detach_client(evdev, client);
Andrew Morton92788ac2014-12-02 15:59:31 -0800529 kvfree(client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400530 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400533static ssize_t evdev_write(struct file *file, const char __user *buffer,
534 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400536 struct evdev_client *client = file->private_data;
537 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800539 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700541 if (count != 0 && count < input_event_size())
Peter Korsgaard439581e2011-02-25 09:30:46 -0800542 return -EINVAL;
543
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400544 retval = mutex_lock_interruptible(&evdev->mutex);
545 if (retval)
546 return retval;
547
David Herrmannc7dc6572013-09-07 12:23:05 -0700548 if (!evdev->exist || client->revoked) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400549 retval = -ENODEV;
550 goto out;
551 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500552
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700553 while (retval + input_event_size() <= count) {
554
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400555 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400556 retval = -EFAULT;
557 goto out;
558 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800559 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400560
561 input_inject_event(&evdev->handle,
562 event.type, event.code, event.value);
Dmitry Torokhov36d25822018-10-04 17:45:54 -0700563 cond_resched();
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700564 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500565
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400566 out:
567 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500568 return retval;
569}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500570
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400571static int evdev_fetch_next_event(struct evdev_client *client,
572 struct input_event *event)
573{
574 int have_event;
575
576 spin_lock_irq(&client->buffer_lock);
577
Dima Zavin566cf5b2011-12-30 15:16:44 -0800578 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400579 if (have_event) {
580 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700581 client->tail &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400582 }
583
584 spin_unlock_irq(&client->buffer_lock);
585
586 return have_event;
587}
588
589static ssize_t evdev_read(struct file *file, char __user *buffer,
590 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400592 struct evdev_client *client = file->private_data;
593 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400594 struct input_event event;
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700595 size_t read = 0;
596 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700598 if (count != 0 && count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return -EINVAL;
600
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700601 for (;;) {
David Herrmannc7dc6572013-09-07 12:23:05 -0700602 if (!evdev->exist || client->revoked)
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700603 return -ENODEV;
604
605 if (client->packet_head == client->tail &&
606 (file->f_flags & O_NONBLOCK))
607 return -EAGAIN;
608
609 /*
610 * count == 0 is special - no IO is done but we check
611 * for error conditions (see above).
612 */
613 if (count == 0)
614 break;
615
616 while (read + input_event_size() <= count &&
617 evdev_fetch_next_event(client, &event)) {
618
619 if (input_event_to_user(buffer + read, &event))
620 return -EFAULT;
621
622 read += input_event_size();
623 }
624
625 if (read)
626 break;
627
628 if (!(file->f_flags & O_NONBLOCK)) {
629 error = wait_event_interruptible(evdev->wait,
630 client->packet_head != client->tail ||
David Herrmannc7dc6572013-09-07 12:23:05 -0700631 !evdev->exist || client->revoked);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700632 if (error)
633 return error;
634 }
Dima Zavin509f87c2011-12-30 15:16:44 -0800635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700637 return read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640/* No kernel lock - fine */
Al Viroafc9a422017-07-03 06:39:46 -0400641static __poll_t evdev_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400643 struct evdev_client *client = file->private_data;
644 struct evdev *evdev = client->evdev;
Al Viroafc9a422017-07-03 06:39:46 -0400645 __poll_t mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400646
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400647 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700648
David Herrmannc7dc6572013-09-07 12:23:05 -0700649 if (evdev->exist && !client->revoked)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800650 mask = EPOLLOUT | EPOLLWRNORM;
David Herrmannc7dc6572013-09-07 12:23:05 -0700651 else
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800652 mask = EPOLLHUP | EPOLLERR;
David Herrmannc7dc6572013-09-07 12:23:05 -0700653
Jeff Browncdda9112011-04-26 22:16:11 -0700654 if (client->packet_head != client->tail)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800655 mask |= EPOLLIN | EPOLLRDNORM;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700656
657 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500660#ifdef CONFIG_COMPAT
661
662#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700663#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500664
665#ifdef __BIG_ENDIAN
666static int bits_to_user(unsigned long *bits, unsigned int maxbit,
667 unsigned int maxlen, void __user *p, int compat)
668{
669 int len, i;
670
671 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700672 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400673 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500674 len = maxlen;
675
676 for (i = 0; i < len / sizeof(compat_long_t); i++)
677 if (copy_to_user((compat_long_t __user *) p + i,
678 (compat_long_t *) bits +
679 i + 1 - ((i % 2) << 1),
680 sizeof(compat_long_t)))
681 return -EFAULT;
682 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700683 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500684 if (len > maxlen)
685 len = maxlen;
686
687 if (copy_to_user(p, bits, len))
688 return -EFAULT;
689 }
690
691 return len;
692}
David Herrmann06a16292015-10-24 16:20:18 -0700693
694static int bits_from_user(unsigned long *bits, unsigned int maxbit,
695 unsigned int maxlen, const void __user *p, int compat)
696{
697 int len, i;
698
699 if (compat) {
700 if (maxlen % sizeof(compat_long_t))
701 return -EINVAL;
702
703 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
704 if (len > maxlen)
705 len = maxlen;
706
707 for (i = 0; i < len / sizeof(compat_long_t); i++)
708 if (copy_from_user((compat_long_t *) bits +
709 i + 1 - ((i % 2) << 1),
710 (compat_long_t __user *) p + i,
711 sizeof(compat_long_t)))
712 return -EFAULT;
713 if (i % 2)
714 *((compat_long_t *) bits + i - 1) = 0;
715
716 } else {
717 if (maxlen % sizeof(long))
718 return -EINVAL;
719
720 len = BITS_TO_LONGS(maxbit) * sizeof(long);
721 if (len > maxlen)
722 len = maxlen;
723
724 if (copy_from_user(bits, p, len))
725 return -EFAULT;
726 }
727
728 return len;
729}
730
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500731#else
David Herrmann06a16292015-10-24 16:20:18 -0700732
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500733static int bits_to_user(unsigned long *bits, unsigned int maxbit,
734 unsigned int maxlen, void __user *p, int compat)
735{
736 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700737 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
738 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500739
740 if (len > maxlen)
741 len = maxlen;
742
743 return copy_to_user(p, bits, len) ? -EFAULT : len;
744}
David Herrmann06a16292015-10-24 16:20:18 -0700745
746static int bits_from_user(unsigned long *bits, unsigned int maxbit,
747 unsigned int maxlen, const void __user *p, int compat)
748{
749 size_t chunk_size = compat ? sizeof(compat_long_t) : sizeof(long);
750 int len;
751
752 if (maxlen % chunk_size)
753 return -EINVAL;
754
755 len = compat ? BITS_TO_LONGS_COMPAT(maxbit) : BITS_TO_LONGS(maxbit);
756 len *= chunk_size;
757 if (len > maxlen)
758 len = maxlen;
759
760 return copy_from_user(bits, p, len) ? -EFAULT : len;
761}
762
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500763#endif /* __BIG_ENDIAN */
764
765#else
766
767static int bits_to_user(unsigned long *bits, unsigned int maxbit,
768 unsigned int maxlen, void __user *p, int compat)
769{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700770 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500771
772 if (len > maxlen)
773 len = maxlen;
774
775 return copy_to_user(p, bits, len) ? -EFAULT : len;
776}
777
David Herrmann06a16292015-10-24 16:20:18 -0700778static int bits_from_user(unsigned long *bits, unsigned int maxbit,
779 unsigned int maxlen, const void __user *p, int compat)
780{
781 int len;
782
783 if (maxlen % sizeof(long))
784 return -EINVAL;
785
786 len = BITS_TO_LONGS(maxbit) * sizeof(long);
787 if (len > maxlen)
788 len = maxlen;
789
790 return copy_from_user(bits, p, len) ? -EFAULT : len;
791}
792
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500793#endif /* CONFIG_COMPAT */
794
795static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
796{
797 int len;
798
799 if (!str)
800 return -ENOENT;
801
802 len = strlen(str) + 1;
803 if (len > maxlen)
804 len = maxlen;
805
806 return copy_to_user(p, str, len) ? -EFAULT : len;
807}
808
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700809static int handle_eviocgbit(struct input_dev *dev,
810 unsigned int type, unsigned int size,
811 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400812{
813 unsigned long *bits;
814 int len;
815
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700816 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400817
818 case 0: bits = dev->evbit; len = EV_MAX; break;
819 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
820 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
821 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
822 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
823 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
824 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
825 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
826 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
827 default: return -EINVAL;
828 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400829
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700830 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400831}
Linus Torvalds5402a732008-08-05 11:42:42 -0400832
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800833static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
834{
835 struct input_keymap_entry ke = {
836 .len = sizeof(unsigned int),
837 .flags = 0,
838 };
839 int __user *ip = (int __user *)p;
840 int error;
841
842 /* legacy case */
843 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
844 return -EFAULT;
845
846 error = input_get_keycode(dev, &ke);
847 if (error)
848 return error;
849
850 if (put_user(ke.keycode, ip + 1))
851 return -EFAULT;
852
853 return 0;
854}
855
856static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700857{
858 struct input_keymap_entry ke;
859 int error;
860
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800861 if (copy_from_user(&ke, p, sizeof(ke)))
862 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700863
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800864 error = input_get_keycode(dev, &ke);
865 if (error)
866 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700867
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800868 if (copy_to_user(p, &ke, sizeof(ke)))
869 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700870
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700871 return 0;
872}
873
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800874static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
875{
876 struct input_keymap_entry ke = {
877 .len = sizeof(unsigned int),
878 .flags = 0,
879 };
880 int __user *ip = (int __user *)p;
881
882 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
883 return -EFAULT;
884
885 if (get_user(ke.keycode, ip + 1))
886 return -EFAULT;
887
888 return input_set_keycode(dev, &ke);
889}
890
891static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700892{
893 struct input_keymap_entry ke;
894
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800895 if (copy_from_user(&ke, p, sizeof(ke)))
896 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700897
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800898 if (ke.len > sizeof(ke.scancode))
899 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700900
901 return input_set_keycode(dev, &ke);
902}
903
David Herrmann48318022013-04-07 21:13:19 -0700904/*
905 * If we transfer state to the user, we should flush all pending events
906 * of the same type from the client's queue. Otherwise, they might end up
907 * with duplicate events, which can screw up client's state tracking.
908 * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
909 * event so user-space will notice missing events.
910 *
911 * LOCKING:
912 * We need to take event_lock before buffer_lock to avoid dead-locks. But we
913 * need the even_lock only to guarantee consistent state. We can safely release
914 * it while flushing the queue. This allows input-core to handle filters while
915 * we flush the queue.
916 */
917static int evdev_handle_get_val(struct evdev_client *client,
918 struct input_dev *dev, unsigned int type,
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700919 unsigned long *bits, unsigned int maxbit,
920 unsigned int maxlen, void __user *p,
921 int compat)
David Herrmann48318022013-04-07 21:13:19 -0700922{
923 int ret;
924 unsigned long *mem;
925
Andy Shevchenko60780912018-08-01 15:47:47 -0700926 mem = bitmap_alloc(maxbit, 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
Andy Shevchenko60780912018-08-01 15:47:47 -0700933 bitmap_copy(mem, bits, maxbit);
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
Andy Shevchenko60780912018-08-01 15:47:47 -0700945 bitmap_free(mem);
David Herrmann48318022013-04-07 21:13:19 -0700946
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
Andy Shevchenko60780912018-08-01 15:47:47 -07001001 mask = bitmap_zalloc(cnt, GFP_KERNEL);
David Herrmann06a16292015-10-24 16:20:18 -07001002 if (!mask)
1003 return -ENOMEM;
1004
1005 error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);
1006 if (error < 0) {
Andy Shevchenko60780912018-08-01 15:47:47 -07001007 bitmap_free(mask);
David Herrmann06a16292015-10-24 16:20:18 -07001008 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
Andy Shevchenko60780912018-08-01 15:47:47 -07001016 bitmap_free(oldmask);
David Herrmann06a16292015-10-24 16:20:18 -07001017
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 Mackf9ce6eb52010-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");