blob: 18d4b2c8fe55092aa8e31c411faf051d968fb49d [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,
34 EV_CLK_BOOT,
35 EV_CLK_MAX
36};
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct evdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 int open;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct input_handle handle;
41 wait_queue_head_t wait;
Arnd Bergmann2be85272010-03-04 15:50:28 +010042 struct evdev_client __rcu *grab;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040043 struct list_head client_list;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040044 spinlock_t client_lock; /* protects client_list */
45 struct mutex mutex;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040046 struct device dev;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -070047 struct cdev cdev;
Dmitry Torokhov20da92d2010-07-15 23:27:36 -070048 bool exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040051struct evdev_client {
Jeff Brown9fb0f142011-04-12 23:29:38 -070052 unsigned int head;
53 unsigned int tail;
Jeff Browncdda9112011-04-26 22:16:11 -070054 unsigned int packet_head; /* [future] position of the first element of next packet */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040055 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct fasync_struct *fasync;
57 struct evdev *evdev;
58 struct list_head node;
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -080059 int clk_type;
David Herrmannc7dc6572013-09-07 12:23:05 -070060 bool revoked;
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
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -080065static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
66{
67 switch (clkid) {
68
69 case CLOCK_REALTIME:
70 client->clk_type = EV_CLK_REAL;
71 break;
72 case CLOCK_MONOTONIC:
73 client->clk_type = EV_CLK_MONO;
74 break;
75 case CLOCK_BOOTTIME:
76 client->clk_type = EV_CLK_BOOT;
77 break;
78 default:
79 return -EINVAL;
80 }
81
82 return 0;
83}
84
David Herrmann48318022013-04-07 21:13:19 -070085/* flush queued events of type @type, caller must hold client->buffer_lock */
86static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
87{
88 unsigned int i, head, num;
89 unsigned int mask = client->bufsize - 1;
90 bool is_report;
91 struct input_event *ev;
92
93 BUG_ON(type == EV_SYN);
94
95 head = client->tail;
96 client->packet_head = client->tail;
97
98 /* init to 1 so a leading SYN_REPORT will not be dropped */
99 num = 1;
100
101 for (i = client->tail; i != client->head; i = (i + 1) & mask) {
102 ev = &client->buffer[i];
103 is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
104
105 if (ev->type == type) {
106 /* drop matched entry */
107 continue;
108 } else if (is_report && !num) {
109 /* drop empty SYN_REPORT groups */
110 continue;
111 } else if (head != i) {
112 /* move entry to fill the gap */
113 client->buffer[head].time = ev->time;
114 client->buffer[head].type = ev->type;
115 client->buffer[head].code = ev->code;
116 client->buffer[head].value = ev->value;
117 }
118
119 num++;
120 head = (head + 1) & mask;
121
122 if (is_report) {
123 num = 0;
124 client->packet_head = head;
125 }
126 }
127
128 client->head = head;
129}
130
131/* queue SYN_DROPPED event */
132static void evdev_queue_syn_dropped(struct evdev_client *client)
133{
134 unsigned long flags;
135 struct input_event ev;
136 ktime_t time;
137
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800138 time = client->clk_type == EV_CLK_REAL ?
139 ktime_get_real() :
140 client->clk_type == EV_CLK_MONO ?
141 ktime_get() :
142 ktime_get_boottime();
David Herrmann48318022013-04-07 21:13:19 -0700143
144 ev.time = ktime_to_timeval(time);
145 ev.type = EV_SYN;
146 ev.code = SYN_DROPPED;
147 ev.value = 0;
148
149 spin_lock_irqsave(&client->buffer_lock, flags);
150
151 client->buffer[client->head++] = ev;
152 client->head &= client->bufsize - 1;
153
154 if (unlikely(client->head == client->tail)) {
155 /* drop queue but keep our SYN_DROPPED event */
156 client->tail = (client->head - 1) & (client->bufsize - 1);
157 client->packet_head = client->tail;
158 }
159
160 spin_unlock_irqrestore(&client->buffer_lock, flags);
161}
162
Henrik Rydberga274ac12012-08-29 20:48:02 +0200163static void __pass_event(struct evdev_client *client,
164 const struct input_event *event)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400165{
Jeff Brown9fb0f142011-04-12 23:29:38 -0700166 client->buffer[client->head++] = *event;
167 client->head &= client->bufsize - 1;
168
169 if (unlikely(client->head == client->tail)) {
170 /*
171 * This effectively "drops" all unconsumed events, leaving
172 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
173 */
174 client->tail = (client->head - 2) & (client->bufsize - 1);
175
176 client->buffer[client->tail].time = event->time;
177 client->buffer[client->tail].type = EV_SYN;
178 client->buffer[client->tail].code = SYN_DROPPED;
179 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -0700180
181 client->packet_head = client->tail;
182 }
183
184 if (event->type == EV_SYN && event->code == SYN_REPORT) {
185 client->packet_head = client->head;
186 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -0700187 }
Henrik Rydberga274ac12012-08-29 20:48:02 +0200188}
189
190static void evdev_pass_values(struct evdev_client *client,
191 const struct input_value *vals, unsigned int count,
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800192 ktime_t *ev_time)
Henrik Rydberga274ac12012-08-29 20:48:02 +0200193{
194 struct evdev *evdev = client->evdev;
195 const struct input_value *v;
196 struct input_event event;
197 bool wakeup = false;
198
David Herrmannc7dc6572013-09-07 12:23:05 -0700199 if (client->revoked)
200 return;
201
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800202 event.time = ktime_to_timeval(ev_time[client->clk_type]);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200203
204 /* Interrupts are disabled, just acquire the lock. */
205 spin_lock(&client->buffer_lock);
206
207 for (v = vals; v != vals + count; v++) {
208 event.type = v->type;
209 event.code = v->code;
210 event.value = v->value;
211 __pass_event(client, &event);
212 if (v->type == EV_SYN && v->code == SYN_REPORT)
213 wakeup = true;
214 }
Jeff Brown9fb0f142011-04-12 23:29:38 -0700215
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400216 spin_unlock(&client->buffer_lock);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200217
218 if (wakeup)
219 wake_up_interruptible(&evdev->wait);
220}
221
222/*
223 * Pass incoming events to all connected clients.
224 */
225static void evdev_events(struct input_handle *handle,
226 const struct input_value *vals, unsigned int count)
227{
228 struct evdev *evdev = handle->private;
229 struct evdev_client *client;
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800230 ktime_t ev_time[EV_CLK_MAX];
Henrik Rydberga274ac12012-08-29 20:48:02 +0200231
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800232 ev_time[EV_CLK_MONO] = ktime_get();
233 ev_time[EV_CLK_REAL] = ktime_mono_to_real(ev_time[EV_CLK_MONO]);
234 ev_time[EV_CLK_BOOT] = ktime_mono_to_any(ev_time[EV_CLK_MONO],
235 TK_OFFS_BOOT);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200236
237 rcu_read_lock();
238
239 client = rcu_dereference(evdev->grab);
240
241 if (client)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800242 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200243 else
244 list_for_each_entry_rcu(client, &evdev->client_list, node)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800245 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200246
247 rcu_read_unlock();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400248}
249
250/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400251 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400252 */
253static void evdev_event(struct input_handle *handle,
254 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Henrik Rydberga274ac12012-08-29 20:48:02 +0200256 struct input_value vals[] = { { type, code, value } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Henrik Rydberga274ac12012-08-29 20:48:02 +0200258 evdev_events(handle, vals, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261static int evdev_fasync(int fd, struct file *file, int on)
262{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400263 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400264
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700265 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400268static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400270 struct evdev_client *client = file->private_data;
271 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400272 int retval;
273
274 retval = mutex_lock_interruptible(&evdev->mutex);
275 if (retval)
276 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400277
David Herrmannc7dc6572013-09-07 12:23:05 -0700278 if (!evdev->exist || client->revoked)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400279 retval = -ENODEV;
280 else
281 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400282
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400283 mutex_unlock(&evdev->mutex);
284 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400287static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400289 struct evdev *evdev = container_of(dev, struct evdev, dev);
290
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400291 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 kfree(evdev);
293}
294
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400295/*
296 * Grabs an event device (along with underlying input device).
297 * This function is called with evdev->mutex taken.
298 */
299static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
300{
301 int error;
302
303 if (evdev->grab)
304 return -EBUSY;
305
306 error = input_grab_device(&evdev->handle);
307 if (error)
308 return error;
309
310 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400311
312 return 0;
313}
314
315static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
316{
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700317 struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
318 lockdep_is_held(&evdev->mutex));
319
320 if (grab != client)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400321 return -EINVAL;
322
323 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400324 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400325 input_release_device(&evdev->handle);
326
327 return 0;
328}
329
330static void evdev_attach_client(struct evdev *evdev,
331 struct evdev_client *client)
332{
333 spin_lock(&evdev->client_lock);
334 list_add_tail_rcu(&client->node, &evdev->client_list);
335 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400336}
337
338static void evdev_detach_client(struct evdev *evdev,
339 struct evdev_client *client)
340{
341 spin_lock(&evdev->client_lock);
342 list_del_rcu(&client->node);
343 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400344 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400345}
346
347static int evdev_open_device(struct evdev *evdev)
348{
349 int retval;
350
351 retval = mutex_lock_interruptible(&evdev->mutex);
352 if (retval)
353 return retval;
354
355 if (!evdev->exist)
356 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400357 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400358 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400359 if (retval)
360 evdev->open--;
361 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400362
363 mutex_unlock(&evdev->mutex);
364 return retval;
365}
366
367static void evdev_close_device(struct evdev *evdev)
368{
369 mutex_lock(&evdev->mutex);
370
371 if (evdev->exist && !--evdev->open)
372 input_close_device(&evdev->handle);
373
374 mutex_unlock(&evdev->mutex);
375}
376
377/*
378 * Wake up users waiting for IO so they can disconnect from
379 * dead device.
380 */
381static void evdev_hangup(struct evdev *evdev)
382{
383 struct evdev_client *client;
384
385 spin_lock(&evdev->client_lock);
386 list_for_each_entry(client, &evdev->client_list, node)
387 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
388 spin_unlock(&evdev->client_lock);
389
390 wake_up_interruptible(&evdev->wait);
391}
392
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400393static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400395 struct evdev_client *client = file->private_data;
396 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400398 mutex_lock(&evdev->mutex);
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700399 evdev_ungrab(evdev, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400400 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400402 evdev_detach_client(evdev, client);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700403
404 if (is_vmalloc_addr(client))
405 vfree(client);
406 else
407 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400409 evdev_close_device(evdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
412}
413
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700414static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
415{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700416 unsigned int n_events =
417 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
418 EVDEV_MIN_BUFFER_SIZE);
419
420 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700421}
422
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400423static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700425 struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
426 unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700427 unsigned int size = sizeof(struct evdev_client) +
428 bufsize * sizeof(struct input_event);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400429 struct evdev_client *client;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400430 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Daniel Stone92eb77d2013-10-31 00:25:34 -0700432 client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
433 if (!client)
434 client = vzalloc(size);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700435 if (!client)
436 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700438 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400439 spin_lock_init(&client->buffer_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400440 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400441 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400443 error = evdev_open_device(evdev);
444 if (error)
445 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400447 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800448 nonseekable_open(inode, file);
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400451
452 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400453 evdev_detach_client(evdev, client);
Andrew Morton92788ac2014-12-02 15:59:31 -0800454 kvfree(client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400455 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400458static ssize_t evdev_write(struct file *file, const char __user *buffer,
459 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400461 struct evdev_client *client = file->private_data;
462 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800464 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700466 if (count != 0 && count < input_event_size())
Peter Korsgaard439581e2011-02-25 09:30:46 -0800467 return -EINVAL;
468
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400469 retval = mutex_lock_interruptible(&evdev->mutex);
470 if (retval)
471 return retval;
472
David Herrmannc7dc6572013-09-07 12:23:05 -0700473 if (!evdev->exist || client->revoked) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400474 retval = -ENODEV;
475 goto out;
476 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500477
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700478 while (retval + input_event_size() <= count) {
479
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400480 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400481 retval = -EFAULT;
482 goto out;
483 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800484 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400485
486 input_inject_event(&evdev->handle,
487 event.type, event.code, event.value);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700488 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500489
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400490 out:
491 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500492 return retval;
493}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500494
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400495static int evdev_fetch_next_event(struct evdev_client *client,
496 struct input_event *event)
497{
498 int have_event;
499
500 spin_lock_irq(&client->buffer_lock);
501
Dima Zavin566cf5b2011-12-30 15:16:44 -0800502 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400503 if (have_event) {
504 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700505 client->tail &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400506 }
507
508 spin_unlock_irq(&client->buffer_lock);
509
510 return have_event;
511}
512
513static ssize_t evdev_read(struct file *file, char __user *buffer,
514 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400516 struct evdev_client *client = file->private_data;
517 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400518 struct input_event event;
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700519 size_t read = 0;
520 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700522 if (count != 0 && count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EINVAL;
524
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700525 for (;;) {
David Herrmannc7dc6572013-09-07 12:23:05 -0700526 if (!evdev->exist || client->revoked)
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700527 return -ENODEV;
528
529 if (client->packet_head == client->tail &&
530 (file->f_flags & O_NONBLOCK))
531 return -EAGAIN;
532
533 /*
534 * count == 0 is special - no IO is done but we check
535 * for error conditions (see above).
536 */
537 if (count == 0)
538 break;
539
540 while (read + input_event_size() <= count &&
541 evdev_fetch_next_event(client, &event)) {
542
543 if (input_event_to_user(buffer + read, &event))
544 return -EFAULT;
545
546 read += input_event_size();
547 }
548
549 if (read)
550 break;
551
552 if (!(file->f_flags & O_NONBLOCK)) {
553 error = wait_event_interruptible(evdev->wait,
554 client->packet_head != client->tail ||
David Herrmannc7dc6572013-09-07 12:23:05 -0700555 !evdev->exist || client->revoked);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700556 if (error)
557 return error;
558 }
Dima Zavin509f87c2011-12-30 15:16:44 -0800559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700561 return read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/* No kernel lock - fine */
565static unsigned int evdev_poll(struct file *file, poll_table *wait)
566{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400567 struct evdev_client *client = file->private_data;
568 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700569 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400570
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400571 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700572
David Herrmannc7dc6572013-09-07 12:23:05 -0700573 if (evdev->exist && !client->revoked)
574 mask = POLLOUT | POLLWRNORM;
575 else
576 mask = POLLHUP | POLLERR;
577
Jeff Browncdda9112011-04-26 22:16:11 -0700578 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700579 mask |= POLLIN | POLLRDNORM;
580
581 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500584#ifdef CONFIG_COMPAT
585
586#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700587#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500588
589#ifdef __BIG_ENDIAN
590static int bits_to_user(unsigned long *bits, unsigned int maxbit,
591 unsigned int maxlen, void __user *p, int compat)
592{
593 int len, i;
594
595 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700596 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400597 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500598 len = maxlen;
599
600 for (i = 0; i < len / sizeof(compat_long_t); i++)
601 if (copy_to_user((compat_long_t __user *) p + i,
602 (compat_long_t *) bits +
603 i + 1 - ((i % 2) << 1),
604 sizeof(compat_long_t)))
605 return -EFAULT;
606 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700607 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500608 if (len > maxlen)
609 len = maxlen;
610
611 if (copy_to_user(p, bits, len))
612 return -EFAULT;
613 }
614
615 return len;
616}
617#else
618static int bits_to_user(unsigned long *bits, unsigned int maxbit,
619 unsigned int maxlen, void __user *p, int compat)
620{
621 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700622 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
623 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500624
625 if (len > maxlen)
626 len = maxlen;
627
628 return copy_to_user(p, bits, len) ? -EFAULT : len;
629}
630#endif /* __BIG_ENDIAN */
631
632#else
633
634static int bits_to_user(unsigned long *bits, unsigned int maxbit,
635 unsigned int maxlen, void __user *p, int compat)
636{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700637 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500638
639 if (len > maxlen)
640 len = maxlen;
641
642 return copy_to_user(p, bits, len) ? -EFAULT : len;
643}
644
645#endif /* CONFIG_COMPAT */
646
647static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
648{
649 int len;
650
651 if (!str)
652 return -ENOENT;
653
654 len = strlen(str) + 1;
655 if (len > maxlen)
656 len = maxlen;
657
658 return copy_to_user(p, str, len) ? -EFAULT : len;
659}
660
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700661static int handle_eviocgbit(struct input_dev *dev,
662 unsigned int type, unsigned int size,
663 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400664{
665 unsigned long *bits;
666 int len;
667
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700668 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400669
670 case 0: bits = dev->evbit; len = EV_MAX; break;
671 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
672 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
673 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
674 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
675 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
676 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
677 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
678 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
679 default: return -EINVAL;
680 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400681
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700682 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400683}
Linus Torvalds5402a732008-08-05 11:42:42 -0400684
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800685static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
686{
687 struct input_keymap_entry ke = {
688 .len = sizeof(unsigned int),
689 .flags = 0,
690 };
691 int __user *ip = (int __user *)p;
692 int error;
693
694 /* legacy case */
695 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
696 return -EFAULT;
697
698 error = input_get_keycode(dev, &ke);
699 if (error)
700 return error;
701
702 if (put_user(ke.keycode, ip + 1))
703 return -EFAULT;
704
705 return 0;
706}
707
708static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700709{
710 struct input_keymap_entry ke;
711 int error;
712
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800713 if (copy_from_user(&ke, p, sizeof(ke)))
714 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700715
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800716 error = input_get_keycode(dev, &ke);
717 if (error)
718 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700719
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800720 if (copy_to_user(p, &ke, sizeof(ke)))
721 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700722
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700723 return 0;
724}
725
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800726static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
727{
728 struct input_keymap_entry ke = {
729 .len = sizeof(unsigned int),
730 .flags = 0,
731 };
732 int __user *ip = (int __user *)p;
733
734 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
735 return -EFAULT;
736
737 if (get_user(ke.keycode, ip + 1))
738 return -EFAULT;
739
740 return input_set_keycode(dev, &ke);
741}
742
743static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700744{
745 struct input_keymap_entry ke;
746
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800747 if (copy_from_user(&ke, p, sizeof(ke)))
748 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700749
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800750 if (ke.len > sizeof(ke.scancode))
751 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700752
753 return input_set_keycode(dev, &ke);
754}
755
David Herrmann48318022013-04-07 21:13:19 -0700756/*
757 * If we transfer state to the user, we should flush all pending events
758 * of the same type from the client's queue. Otherwise, they might end up
759 * with duplicate events, which can screw up client's state tracking.
760 * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
761 * event so user-space will notice missing events.
762 *
763 * LOCKING:
764 * We need to take event_lock before buffer_lock to avoid dead-locks. But we
765 * need the even_lock only to guarantee consistent state. We can safely release
766 * it while flushing the queue. This allows input-core to handle filters while
767 * we flush the queue.
768 */
769static int evdev_handle_get_val(struct evdev_client *client,
770 struct input_dev *dev, unsigned int type,
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700771 unsigned long *bits, unsigned int maxbit,
772 unsigned int maxlen, void __user *p,
773 int compat)
David Herrmann48318022013-04-07 21:13:19 -0700774{
775 int ret;
776 unsigned long *mem;
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700777 size_t len;
David Herrmann48318022013-04-07 21:13:19 -0700778
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700779 len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
780 mem = kmalloc(len, GFP_KERNEL);
David Herrmann48318022013-04-07 21:13:19 -0700781 if (!mem)
782 return -ENOMEM;
783
784 spin_lock_irq(&dev->event_lock);
785 spin_lock(&client->buffer_lock);
786
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700787 memcpy(mem, bits, len);
David Herrmann48318022013-04-07 21:13:19 -0700788
789 spin_unlock(&dev->event_lock);
790
791 __evdev_flush_queue(client, type);
792
793 spin_unlock_irq(&client->buffer_lock);
794
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700795 ret = bits_to_user(mem, maxbit, maxlen, p, compat);
David Herrmann48318022013-04-07 21:13:19 -0700796 if (ret < 0)
797 evdev_queue_syn_dropped(client);
798
799 kfree(mem);
800
801 return ret;
802}
803
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100804static int evdev_handle_mt_request(struct input_dev *dev,
805 unsigned int size,
806 int __user *ip)
807{
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200808 const struct input_mt *mt = dev->mt;
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100809 unsigned int code;
810 int max_slots;
811 int i;
812
813 if (get_user(code, &ip[0]))
814 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200815 if (!mt || !input_is_mt_value(code))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100816 return -EINVAL;
817
818 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200819 for (i = 0; i < mt->num_slots && i < max_slots; i++) {
820 int value = input_mt_get_value(&mt->slots[i], code);
821 if (put_user(value, &ip[1 + i]))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100822 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200823 }
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100824
825 return 0;
826}
827
David Herrmannc7dc6572013-09-07 12:23:05 -0700828static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
829 struct file *file)
830{
831 client->revoked = true;
832 evdev_ungrab(evdev, client);
833 input_flush_device(&evdev->handle, file);
834 wake_up_interruptible(&evdev->wait);
835
836 return 0;
837}
838
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400839static long evdev_do_ioctl(struct file *file, unsigned int cmd,
840 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400842 struct evdev_client *client = file->private_data;
843 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct input_dev *dev = evdev->handle.dev;
845 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400846 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500847 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800848 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700849 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400850 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700852 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 switch (cmd) {
854
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400855 case EVIOCGVERSION:
856 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400858 case EVIOCGID:
859 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
860 return -EFAULT;
861 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400862
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400863 case EVIOCGREP:
864 if (!test_bit(EV_REP, dev->evbit))
865 return -ENOSYS;
866 if (put_user(dev->rep[REP_DELAY], ip))
867 return -EFAULT;
868 if (put_user(dev->rep[REP_PERIOD], ip + 1))
869 return -EFAULT;
870 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400871
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400872 case EVIOCSREP:
873 if (!test_bit(EV_REP, dev->evbit))
874 return -ENOSYS;
875 if (get_user(u, ip))
876 return -EFAULT;
877 if (get_user(v, ip + 1))
878 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400879
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400880 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
881 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500882
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400885 case EVIOCRMFF:
886 return input_ff_erase(dev, (int)(unsigned long) p, file);
887
888 case EVIOCGEFFECTS:
889 i = test_bit(EV_FF, dev->evbit) ?
890 dev->ff->max_effects : 0;
891 if (put_user(i, ip))
892 return -EFAULT;
893 return 0;
894
895 case EVIOCGRAB:
896 if (p)
897 return evdev_grab(evdev, client);
898 else
899 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800900
David Herrmannc7dc6572013-09-07 12:23:05 -0700901 case EVIOCREVOKE:
902 if (p)
903 return -EINVAL;
904 else
905 return evdev_revoke(evdev, client, file);
906
John Stultza80b83b2012-02-03 00:19:07 -0800907 case EVIOCSCLOCKID:
908 if (copy_from_user(&i, p, sizeof(unsigned int)))
909 return -EFAULT;
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800910
911 return evdev_set_clk_type(client, i);
John Stultza80b83b2012-02-03 00:19:07 -0800912
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800913 case EVIOCGKEYCODE:
914 return evdev_handle_get_keycode(dev, p);
915
916 case EVIOCSKEYCODE:
917 return evdev_handle_set_keycode(dev, p);
918
919 case EVIOCGKEYCODE_V2:
920 return evdev_handle_get_keycode_v2(dev, p);
921
922 case EVIOCSKEYCODE_V2:
923 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700924 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400925
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700926 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400927
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700928 /* Now check variable-length commands */
929#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700930 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400931
Henrik Rydberg85b77202010-12-18 20:51:13 +0100932 case EVIOCGPROP(0):
933 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
934 size, p, compat_mode);
935
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100936 case EVIOCGMTSLOTS(0):
937 return evdev_handle_mt_request(dev, size, ip);
938
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700939 case EVIOCGKEY(0):
David Herrmann48318022013-04-07 21:13:19 -0700940 return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
941 KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400942
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700943 case EVIOCGLED(0):
David Herrmann48318022013-04-07 21:13:19 -0700944 return evdev_handle_get_val(client, dev, EV_LED, dev->led,
945 LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400946
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700947 case EVIOCGSND(0):
David Herrmann48318022013-04-07 21:13:19 -0700948 return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
949 SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400950
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700951 case EVIOCGSW(0):
David Herrmann48318022013-04-07 21:13:19 -0700952 return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
953 SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400954
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700955 case EVIOCGNAME(0):
956 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400957
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700958 case EVIOCGPHYS(0):
959 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400960
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700961 case EVIOCGUNIQ(0):
962 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400963
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700964 case EVIOC_MASK_SIZE(EVIOCSFF):
965 if (input_ff_effect_from_user(p, size, &effect))
966 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400967
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700968 error = input_ff_upload(dev, &effect, file);
Elias Vanderstuyftfc7392a2014-03-29 12:08:45 -0700969 if (error)
970 return error;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400971
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700972 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
973 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400974
Elias Vanderstuyftfc7392a2014-03-29 12:08:45 -0700975 return 0;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700976 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400977
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700978 /* Multi-number variable-length handlers */
979 if (_IOC_TYPE(cmd) != 'E')
980 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700982 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700984 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
985 return handle_eviocgbit(dev,
986 _IOC_NR(cmd) & EV_MAX, size,
987 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700989 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400990
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700991 if (!dev->absinfo)
992 return -EINVAL;
993
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700994 t = _IOC_NR(cmd) & ABS_MAX;
995 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400996
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700997 if (copy_to_user(p, &abs, min_t(size_t,
998 size, sizeof(struct input_absinfo))))
999 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -04001000
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001001 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001004
Daniel Mackf9ce6eb2010-10-18 08:43:50 -07001005 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001006
1007 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
1008
Daniel Mack0a74a1d2010-10-18 08:43:30 -07001009 if (!dev->absinfo)
1010 return -EINVAL;
1011
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001012 t = _IOC_NR(cmd) & ABS_MAX;
1013
1014 if (copy_from_user(&abs, p, min_t(size_t,
1015 size, sizeof(struct input_absinfo))))
1016 return -EFAULT;
1017
1018 if (size < sizeof(struct input_absinfo))
1019 abs.resolution = 0;
1020
1021 /* We can't change number of reserved MT slots */
1022 if (t == ABS_MT_SLOT)
1023 return -EINVAL;
1024
1025 /*
1026 * Take event lock to ensure that we are not
1027 * changing device parameters in the middle
1028 * of event.
1029 */
1030 spin_lock_irq(&dev->event_lock);
1031 dev->absinfo[t] = abs;
1032 spin_unlock_irq(&dev->event_lock);
1033
1034 return 0;
1035 }
1036 }
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -EINVAL;
1039}
1040
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001041static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
1042 void __user *p, int compat_mode)
1043{
1044 struct evdev_client *client = file->private_data;
1045 struct evdev *evdev = client->evdev;
1046 int retval;
1047
1048 retval = mutex_lock_interruptible(&evdev->mutex);
1049 if (retval)
1050 return retval;
1051
David Herrmannc7dc6572013-09-07 12:23:05 -07001052 if (!evdev->exist || client->revoked) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001053 retval = -ENODEV;
1054 goto out;
1055 }
1056
1057 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
1058
1059 out:
1060 mutex_unlock(&evdev->mutex);
1061 return retval;
1062}
1063
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001064static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1065{
1066 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
1067}
1068
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001069#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001070static long evdev_ioctl_compat(struct file *file,
1071 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001072{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001073 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001074}
1075#endif
1076
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001077static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001078 .owner = THIS_MODULE,
1079 .read = evdev_read,
1080 .write = evdev_write,
1081 .poll = evdev_poll,
1082 .open = evdev_open,
1083 .release = evdev_release,
1084 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001085#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001086 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001087#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001088 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001089 .flush = evdev_flush,
1090 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091};
1092
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001093/*
1094 * Mark device non-existent. This disables writes, ioctls and
1095 * prevents new users from opening the device. Already posted
1096 * blocking reads will stay, however new ones will fail.
1097 */
1098static void evdev_mark_dead(struct evdev *evdev)
1099{
1100 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001101 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001102 mutex_unlock(&evdev->mutex);
1103}
1104
1105static void evdev_cleanup(struct evdev *evdev)
1106{
1107 struct input_handle *handle = &evdev->handle;
1108
1109 evdev_mark_dead(evdev);
1110 evdev_hangup(evdev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001111
1112 cdev_del(&evdev->cdev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001113
1114 /* evdev is marked dead so no one else accesses evdev->open */
1115 if (evdev->open) {
1116 input_flush_device(handle, NULL);
1117 input_close_device(handle);
1118 }
1119}
1120
1121/*
1122 * Create new evdev device. Note that input core serializes calls
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001123 * to connect and disconnect.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001124 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001125static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1126 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct evdev *evdev;
1129 int minor;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001130 int dev_no;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001131 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001133 minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
1134 if (minor < 0) {
1135 error = minor;
1136 pr_err("failed to reserve new minor: %d\n", error);
1137 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001140 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001141 if (!evdev) {
1142 error = -ENOMEM;
1143 goto err_free_minor;
1144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001146 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001147 spin_lock_init(&evdev->client_lock);
1148 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 init_waitqueue_head(&evdev->wait);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001150 evdev->exist = true;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001151
1152 dev_no = minor;
1153 /* Normalize device number if it falls into legacy range */
1154 if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
1155 dev_no -= EVDEV_MINOR_BASE;
1156 dev_set_name(&evdev->dev, "event%d", dev_no);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001157
Dmitry Torokhova7097ff2008-04-01 00:22:53 -04001158 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001159 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 evdev->handle.handler = handler;
1161 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001162
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001163 evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001164 evdev->dev.class = &input_class;
1165 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001166 evdev->dev.release = evdev_free;
1167 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001169 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001170 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001171 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001173 cdev_init(&evdev->cdev, &evdev_fops);
Dmitry Torokhov4a215aa2012-10-21 17:57:20 -07001174 evdev->cdev.kobj.parent = &evdev->dev.kobj;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001175 error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001176 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001177 goto err_unregister_handle;
1178
1179 error = device_add(&evdev->dev);
1180 if (error)
1181 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001182
1183 return 0;
1184
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001185 err_cleanup_evdev:
1186 evdev_cleanup(evdev);
1187 err_unregister_handle:
1188 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001189 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001190 put_device(&evdev->dev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001191 err_free_minor:
1192 input_free_minor(minor);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001193 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196static void evdev_disconnect(struct input_handle *handle)
1197{
1198 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001200 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001201 evdev_cleanup(evdev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001202 input_free_minor(MINOR(evdev->dev.devt));
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001203 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001204 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001207static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 { .driver_info = 1 }, /* Matches all devices */
1209 { }, /* Terminating zero entry */
1210};
1211
1212MODULE_DEVICE_TABLE(input, evdev_ids);
1213
1214static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001215 .event = evdev_event,
Henrik Rydberga274ac12012-08-29 20:48:02 +02001216 .events = evdev_events,
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001217 .connect = evdev_connect,
1218 .disconnect = evdev_disconnect,
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001219 .legacy_minors = true,
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001220 .minor = EVDEV_MINOR_BASE,
1221 .name = "evdev",
1222 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223};
1224
1225static int __init evdev_init(void)
1226{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001227 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230static void __exit evdev_exit(void)
1231{
1232 input_unregister_handler(&evdev_handler);
1233}
1234
1235module_init(evdev_init);
1236module_exit(evdev_exit);
1237
1238MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1239MODULE_DESCRIPTION("Input driver event char devices");
1240MODULE_LICENSE("GPL");