blob: 8d4c6ca781ff07f880bc4d6022aacea82789bfa5 [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
David Herrmann48318022013-04-07 21:13:19 -070065/* flush queued events of type @type, caller must hold client->buffer_lock */
66static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
67{
68 unsigned int i, head, num;
69 unsigned int mask = client->bufsize - 1;
70 bool is_report;
71 struct input_event *ev;
72
73 BUG_ON(type == EV_SYN);
74
75 head = client->tail;
76 client->packet_head = client->tail;
77
78 /* init to 1 so a leading SYN_REPORT will not be dropped */
79 num = 1;
80
81 for (i = client->tail; i != client->head; i = (i + 1) & mask) {
82 ev = &client->buffer[i];
83 is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
84
85 if (ev->type == type) {
86 /* drop matched entry */
87 continue;
88 } else if (is_report && !num) {
89 /* drop empty SYN_REPORT groups */
90 continue;
91 } else if (head != i) {
92 /* move entry to fill the gap */
93 client->buffer[head].time = ev->time;
94 client->buffer[head].type = ev->type;
95 client->buffer[head].code = ev->code;
96 client->buffer[head].value = ev->value;
97 }
98
99 num++;
100 head = (head + 1) & mask;
101
102 if (is_report) {
103 num = 0;
104 client->packet_head = head;
105 }
106 }
107
108 client->head = head;
109}
110
Anshul Garg0c3e9942015-01-15 09:06:50 -0800111/* queue SYN_DROPPED event and flush queue if flush parameter is true */
112static void evdev_queue_syn_dropped(struct evdev_client *client, bool flush)
David Herrmann48318022013-04-07 21:13:19 -0700113{
114 unsigned long flags;
115 struct input_event ev;
116 ktime_t time;
117
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800118 time = client->clk_type == EV_CLK_REAL ?
119 ktime_get_real() :
120 client->clk_type == EV_CLK_MONO ?
121 ktime_get() :
122 ktime_get_boottime();
David Herrmann48318022013-04-07 21:13:19 -0700123
124 ev.time = ktime_to_timeval(time);
125 ev.type = EV_SYN;
126 ev.code = SYN_DROPPED;
127 ev.value = 0;
128
129 spin_lock_irqsave(&client->buffer_lock, flags);
130
Anshul Garg0c3e9942015-01-15 09:06:50 -0800131 if (flush)
132 client->packet_head = client->head = client->tail;
133
David Herrmann48318022013-04-07 21:13:19 -0700134 client->buffer[client->head++] = ev;
135 client->head &= client->bufsize - 1;
136
137 if (unlikely(client->head == client->tail)) {
138 /* drop queue but keep our SYN_DROPPED event */
139 client->tail = (client->head - 1) & (client->bufsize - 1);
140 client->packet_head = client->tail;
141 }
142
143 spin_unlock_irqrestore(&client->buffer_lock, flags);
144}
145
Anshul Garg0c3e9942015-01-15 09:06:50 -0800146static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
147{
148 if (client->clk_type == clkid)
149 return 0;
150
151 switch (clkid) {
152
153 case CLOCK_REALTIME:
154 client->clk_type = EV_CLK_REAL;
155 break;
156 case CLOCK_MONOTONIC:
157 client->clk_type = EV_CLK_MONO;
158 break;
159 case CLOCK_BOOTTIME:
160 client->clk_type = EV_CLK_BOOT;
161 break;
162 default:
163 return -EINVAL;
164 }
165
166 /* Flush pending events and queue SYN_DROPPED event.*/
167 evdev_queue_syn_dropped(client, true);
168
169 return 0;
170}
171
Henrik Rydberga274ac12012-08-29 20:48:02 +0200172static void __pass_event(struct evdev_client *client,
173 const struct input_event *event)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400174{
Jeff Brown9fb0f142011-04-12 23:29:38 -0700175 client->buffer[client->head++] = *event;
176 client->head &= client->bufsize - 1;
177
178 if (unlikely(client->head == client->tail)) {
179 /*
180 * This effectively "drops" all unconsumed events, leaving
181 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
182 */
183 client->tail = (client->head - 2) & (client->bufsize - 1);
184
185 client->buffer[client->tail].time = event->time;
186 client->buffer[client->tail].type = EV_SYN;
187 client->buffer[client->tail].code = SYN_DROPPED;
188 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -0700189
190 client->packet_head = client->tail;
191 }
192
193 if (event->type == EV_SYN && event->code == SYN_REPORT) {
194 client->packet_head = client->head;
195 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -0700196 }
Henrik Rydberga274ac12012-08-29 20:48:02 +0200197}
198
199static void evdev_pass_values(struct evdev_client *client,
200 const struct input_value *vals, unsigned int count,
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800201 ktime_t *ev_time)
Henrik Rydberga274ac12012-08-29 20:48:02 +0200202{
203 struct evdev *evdev = client->evdev;
204 const struct input_value *v;
205 struct input_event event;
206 bool wakeup = false;
207
David Herrmannc7dc6572013-09-07 12:23:05 -0700208 if (client->revoked)
209 return;
210
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800211 event.time = ktime_to_timeval(ev_time[client->clk_type]);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200212
213 /* Interrupts are disabled, just acquire the lock. */
214 spin_lock(&client->buffer_lock);
215
216 for (v = vals; v != vals + count; v++) {
217 event.type = v->type;
218 event.code = v->code;
219 event.value = v->value;
220 __pass_event(client, &event);
221 if (v->type == EV_SYN && v->code == SYN_REPORT)
222 wakeup = true;
223 }
Jeff Brown9fb0f142011-04-12 23:29:38 -0700224
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400225 spin_unlock(&client->buffer_lock);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200226
227 if (wakeup)
228 wake_up_interruptible(&evdev->wait);
229}
230
231/*
232 * Pass incoming events to all connected clients.
233 */
234static void evdev_events(struct input_handle *handle,
235 const struct input_value *vals, unsigned int count)
236{
237 struct evdev *evdev = handle->private;
238 struct evdev_client *client;
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800239 ktime_t ev_time[EV_CLK_MAX];
Henrik Rydberga274ac12012-08-29 20:48:02 +0200240
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800241 ev_time[EV_CLK_MONO] = ktime_get();
242 ev_time[EV_CLK_REAL] = ktime_mono_to_real(ev_time[EV_CLK_MONO]);
243 ev_time[EV_CLK_BOOT] = ktime_mono_to_any(ev_time[EV_CLK_MONO],
244 TK_OFFS_BOOT);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200245
246 rcu_read_lock();
247
248 client = rcu_dereference(evdev->grab);
249
250 if (client)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800251 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200252 else
253 list_for_each_entry_rcu(client, &evdev->client_list, node)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800254 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200255
256 rcu_read_unlock();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400257}
258
259/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400260 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400261 */
262static void evdev_event(struct input_handle *handle,
263 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Henrik Rydberga274ac12012-08-29 20:48:02 +0200265 struct input_value vals[] = { { type, code, value } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Henrik Rydberga274ac12012-08-29 20:48:02 +0200267 evdev_events(handle, vals, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270static int evdev_fasync(int fd, struct file *file, int on)
271{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400272 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400273
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700274 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400277static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400279 struct evdev_client *client = file->private_data;
280 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400281 int retval;
282
283 retval = mutex_lock_interruptible(&evdev->mutex);
284 if (retval)
285 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400286
David Herrmannc7dc6572013-09-07 12:23:05 -0700287 if (!evdev->exist || client->revoked)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400288 retval = -ENODEV;
289 else
290 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400291
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400292 mutex_unlock(&evdev->mutex);
293 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400296static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400298 struct evdev *evdev = container_of(dev, struct evdev, dev);
299
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400300 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 kfree(evdev);
302}
303
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400304/*
305 * Grabs an event device (along with underlying input device).
306 * This function is called with evdev->mutex taken.
307 */
308static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
309{
310 int error;
311
312 if (evdev->grab)
313 return -EBUSY;
314
315 error = input_grab_device(&evdev->handle);
316 if (error)
317 return error;
318
319 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400320
321 return 0;
322}
323
324static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
325{
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700326 struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
327 lockdep_is_held(&evdev->mutex));
328
329 if (grab != client)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400330 return -EINVAL;
331
332 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400333 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400334 input_release_device(&evdev->handle);
335
336 return 0;
337}
338
339static void evdev_attach_client(struct evdev *evdev,
340 struct evdev_client *client)
341{
342 spin_lock(&evdev->client_lock);
343 list_add_tail_rcu(&client->node, &evdev->client_list);
344 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400345}
346
347static void evdev_detach_client(struct evdev *evdev,
348 struct evdev_client *client)
349{
350 spin_lock(&evdev->client_lock);
351 list_del_rcu(&client->node);
352 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400353 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400354}
355
356static int evdev_open_device(struct evdev *evdev)
357{
358 int retval;
359
360 retval = mutex_lock_interruptible(&evdev->mutex);
361 if (retval)
362 return retval;
363
364 if (!evdev->exist)
365 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400366 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400367 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400368 if (retval)
369 evdev->open--;
370 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400371
372 mutex_unlock(&evdev->mutex);
373 return retval;
374}
375
376static void evdev_close_device(struct evdev *evdev)
377{
378 mutex_lock(&evdev->mutex);
379
380 if (evdev->exist && !--evdev->open)
381 input_close_device(&evdev->handle);
382
383 mutex_unlock(&evdev->mutex);
384}
385
386/*
387 * Wake up users waiting for IO so they can disconnect from
388 * dead device.
389 */
390static void evdev_hangup(struct evdev *evdev)
391{
392 struct evdev_client *client;
393
394 spin_lock(&evdev->client_lock);
395 list_for_each_entry(client, &evdev->client_list, node)
396 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
397 spin_unlock(&evdev->client_lock);
398
399 wake_up_interruptible(&evdev->wait);
400}
401
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400402static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400404 struct evdev_client *client = file->private_data;
405 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400407 mutex_lock(&evdev->mutex);
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700408 evdev_ungrab(evdev, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400409 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400411 evdev_detach_client(evdev, client);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700412
413 if (is_vmalloc_addr(client))
414 vfree(client);
415 else
416 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400418 evdev_close_device(evdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
421}
422
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700423static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
424{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700425 unsigned int n_events =
426 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
427 EVDEV_MIN_BUFFER_SIZE);
428
429 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700430}
431
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400432static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700434 struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
435 unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700436 unsigned int size = sizeof(struct evdev_client) +
437 bufsize * sizeof(struct input_event);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400438 struct evdev_client *client;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400439 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Daniel Stone92eb77d2013-10-31 00:25:34 -0700441 client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
442 if (!client)
443 client = vzalloc(size);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700444 if (!client)
445 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700447 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400448 spin_lock_init(&client->buffer_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400449 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400450 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400452 error = evdev_open_device(evdev);
453 if (error)
454 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400456 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800457 nonseekable_open(inode, file);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400460
461 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400462 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400463 kfree(client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400464 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400467static ssize_t evdev_write(struct file *file, const char __user *buffer,
468 size_t count, loff_t *ppos)
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800473 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700475 if (count != 0 && count < input_event_size())
Peter Korsgaard439581e2011-02-25 09:30:46 -0800476 return -EINVAL;
477
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400478 retval = mutex_lock_interruptible(&evdev->mutex);
479 if (retval)
480 return retval;
481
David Herrmannc7dc6572013-09-07 12:23:05 -0700482 if (!evdev->exist || client->revoked) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400483 retval = -ENODEV;
484 goto out;
485 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500486
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700487 while (retval + input_event_size() <= count) {
488
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400489 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400490 retval = -EFAULT;
491 goto out;
492 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800493 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400494
495 input_inject_event(&evdev->handle,
496 event.type, event.code, event.value);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700497 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500498
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400499 out:
500 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500501 return retval;
502}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500503
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400504static int evdev_fetch_next_event(struct evdev_client *client,
505 struct input_event *event)
506{
507 int have_event;
508
509 spin_lock_irq(&client->buffer_lock);
510
Dima Zavin566cf5b2011-12-30 15:16:44 -0800511 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400512 if (have_event) {
513 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700514 client->tail &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400515 }
516
517 spin_unlock_irq(&client->buffer_lock);
518
519 return have_event;
520}
521
522static ssize_t evdev_read(struct file *file, char __user *buffer,
523 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400525 struct evdev_client *client = file->private_data;
526 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400527 struct input_event event;
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700528 size_t read = 0;
529 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700531 if (count != 0 && count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return -EINVAL;
533
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700534 for (;;) {
David Herrmannc7dc6572013-09-07 12:23:05 -0700535 if (!evdev->exist || client->revoked)
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700536 return -ENODEV;
537
538 if (client->packet_head == client->tail &&
539 (file->f_flags & O_NONBLOCK))
540 return -EAGAIN;
541
542 /*
543 * count == 0 is special - no IO is done but we check
544 * for error conditions (see above).
545 */
546 if (count == 0)
547 break;
548
549 while (read + input_event_size() <= count &&
550 evdev_fetch_next_event(client, &event)) {
551
552 if (input_event_to_user(buffer + read, &event))
553 return -EFAULT;
554
555 read += input_event_size();
556 }
557
558 if (read)
559 break;
560
561 if (!(file->f_flags & O_NONBLOCK)) {
562 error = wait_event_interruptible(evdev->wait,
563 client->packet_head != client->tail ||
David Herrmannc7dc6572013-09-07 12:23:05 -0700564 !evdev->exist || client->revoked);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700565 if (error)
566 return error;
567 }
Dima Zavin509f87c2011-12-30 15:16:44 -0800568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700570 return read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/* No kernel lock - fine */
574static unsigned int evdev_poll(struct file *file, poll_table *wait)
575{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400576 struct evdev_client *client = file->private_data;
577 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700578 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400579
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400580 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700581
David Herrmannc7dc6572013-09-07 12:23:05 -0700582 if (evdev->exist && !client->revoked)
583 mask = POLLOUT | POLLWRNORM;
584 else
585 mask = POLLHUP | POLLERR;
586
Jeff Browncdda9112011-04-26 22:16:11 -0700587 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700588 mask |= POLLIN | POLLRDNORM;
589
590 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500593#ifdef CONFIG_COMPAT
594
595#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700596#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500597
598#ifdef __BIG_ENDIAN
599static int bits_to_user(unsigned long *bits, unsigned int maxbit,
600 unsigned int maxlen, void __user *p, int compat)
601{
602 int len, i;
603
604 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700605 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400606 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500607 len = maxlen;
608
609 for (i = 0; i < len / sizeof(compat_long_t); i++)
610 if (copy_to_user((compat_long_t __user *) p + i,
611 (compat_long_t *) bits +
612 i + 1 - ((i % 2) << 1),
613 sizeof(compat_long_t)))
614 return -EFAULT;
615 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700616 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500617 if (len > maxlen)
618 len = maxlen;
619
620 if (copy_to_user(p, bits, len))
621 return -EFAULT;
622 }
623
624 return len;
625}
626#else
627static int bits_to_user(unsigned long *bits, unsigned int maxbit,
628 unsigned int maxlen, void __user *p, int compat)
629{
630 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700631 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
632 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500633
634 if (len > maxlen)
635 len = maxlen;
636
637 return copy_to_user(p, bits, len) ? -EFAULT : len;
638}
639#endif /* __BIG_ENDIAN */
640
641#else
642
643static int bits_to_user(unsigned long *bits, unsigned int maxbit,
644 unsigned int maxlen, void __user *p, int compat)
645{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700646 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500647
648 if (len > maxlen)
649 len = maxlen;
650
651 return copy_to_user(p, bits, len) ? -EFAULT : len;
652}
653
654#endif /* CONFIG_COMPAT */
655
656static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
657{
658 int len;
659
660 if (!str)
661 return -ENOENT;
662
663 len = strlen(str) + 1;
664 if (len > maxlen)
665 len = maxlen;
666
667 return copy_to_user(p, str, len) ? -EFAULT : len;
668}
669
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700670static int handle_eviocgbit(struct input_dev *dev,
671 unsigned int type, unsigned int size,
672 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400673{
674 unsigned long *bits;
675 int len;
676
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700677 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400678
679 case 0: bits = dev->evbit; len = EV_MAX; break;
680 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
681 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
682 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
683 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
684 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
685 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
686 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
687 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
688 default: return -EINVAL;
689 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400690
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700691 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400692}
Linus Torvalds5402a732008-08-05 11:42:42 -0400693
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800694static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
695{
696 struct input_keymap_entry ke = {
697 .len = sizeof(unsigned int),
698 .flags = 0,
699 };
700 int __user *ip = (int __user *)p;
701 int error;
702
703 /* legacy case */
704 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
705 return -EFAULT;
706
707 error = input_get_keycode(dev, &ke);
708 if (error)
709 return error;
710
711 if (put_user(ke.keycode, ip + 1))
712 return -EFAULT;
713
714 return 0;
715}
716
717static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700718{
719 struct input_keymap_entry ke;
720 int error;
721
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800722 if (copy_from_user(&ke, p, sizeof(ke)))
723 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700724
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800725 error = input_get_keycode(dev, &ke);
726 if (error)
727 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700728
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800729 if (copy_to_user(p, &ke, sizeof(ke)))
730 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700731
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700732 return 0;
733}
734
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800735static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
736{
737 struct input_keymap_entry ke = {
738 .len = sizeof(unsigned int),
739 .flags = 0,
740 };
741 int __user *ip = (int __user *)p;
742
743 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
744 return -EFAULT;
745
746 if (get_user(ke.keycode, ip + 1))
747 return -EFAULT;
748
749 return input_set_keycode(dev, &ke);
750}
751
752static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700753{
754 struct input_keymap_entry ke;
755
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800756 if (copy_from_user(&ke, p, sizeof(ke)))
757 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700758
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800759 if (ke.len > sizeof(ke.scancode))
760 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700761
762 return input_set_keycode(dev, &ke);
763}
764
David Herrmann48318022013-04-07 21:13:19 -0700765/*
766 * If we transfer state to the user, we should flush all pending events
767 * of the same type from the client's queue. Otherwise, they might end up
768 * with duplicate events, which can screw up client's state tracking.
769 * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
770 * event so user-space will notice missing events.
771 *
772 * LOCKING:
773 * We need to take event_lock before buffer_lock to avoid dead-locks. But we
774 * need the even_lock only to guarantee consistent state. We can safely release
775 * it while flushing the queue. This allows input-core to handle filters while
776 * we flush the queue.
777 */
778static int evdev_handle_get_val(struct evdev_client *client,
779 struct input_dev *dev, unsigned int type,
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700780 unsigned long *bits, unsigned int maxbit,
781 unsigned int maxlen, void __user *p,
782 int compat)
David Herrmann48318022013-04-07 21:13:19 -0700783{
784 int ret;
785 unsigned long *mem;
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700786 size_t len;
David Herrmann48318022013-04-07 21:13:19 -0700787
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700788 len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
789 mem = kmalloc(len, GFP_KERNEL);
David Herrmann48318022013-04-07 21:13:19 -0700790 if (!mem)
791 return -ENOMEM;
792
793 spin_lock_irq(&dev->event_lock);
794 spin_lock(&client->buffer_lock);
795
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700796 memcpy(mem, bits, len);
David Herrmann48318022013-04-07 21:13:19 -0700797
798 spin_unlock(&dev->event_lock);
799
800 __evdev_flush_queue(client, type);
801
802 spin_unlock_irq(&client->buffer_lock);
803
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700804 ret = bits_to_user(mem, maxbit, maxlen, p, compat);
David Herrmann48318022013-04-07 21:13:19 -0700805 if (ret < 0)
Anshul Garg0c3e9942015-01-15 09:06:50 -0800806 evdev_queue_syn_dropped(client, false);
David Herrmann48318022013-04-07 21:13:19 -0700807
808 kfree(mem);
809
810 return ret;
811}
812
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100813static int evdev_handle_mt_request(struct input_dev *dev,
814 unsigned int size,
815 int __user *ip)
816{
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200817 const struct input_mt *mt = dev->mt;
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100818 unsigned int code;
819 int max_slots;
820 int i;
821
822 if (get_user(code, &ip[0]))
823 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200824 if (!mt || !input_is_mt_value(code))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100825 return -EINVAL;
826
827 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200828 for (i = 0; i < mt->num_slots && i < max_slots; i++) {
829 int value = input_mt_get_value(&mt->slots[i], code);
830 if (put_user(value, &ip[1 + i]))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100831 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200832 }
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100833
834 return 0;
835}
836
David Herrmannc7dc6572013-09-07 12:23:05 -0700837static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
838 struct file *file)
839{
840 client->revoked = true;
841 evdev_ungrab(evdev, client);
842 input_flush_device(&evdev->handle, file);
843 wake_up_interruptible(&evdev->wait);
844
845 return 0;
846}
847
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400848static long evdev_do_ioctl(struct file *file, unsigned int cmd,
849 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400851 struct evdev_client *client = file->private_data;
852 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct input_dev *dev = evdev->handle.dev;
854 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400855 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500856 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800857 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700858 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400859 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700861 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 switch (cmd) {
863
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400864 case EVIOCGVERSION:
865 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400867 case EVIOCGID:
868 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
869 return -EFAULT;
870 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400871
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400872 case EVIOCGREP:
873 if (!test_bit(EV_REP, dev->evbit))
874 return -ENOSYS;
875 if (put_user(dev->rep[REP_DELAY], ip))
876 return -EFAULT;
877 if (put_user(dev->rep[REP_PERIOD], ip + 1))
878 return -EFAULT;
879 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400880
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400881 case EVIOCSREP:
882 if (!test_bit(EV_REP, dev->evbit))
883 return -ENOSYS;
884 if (get_user(u, ip))
885 return -EFAULT;
886 if (get_user(v, ip + 1))
887 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400888
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400889 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
890 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500891
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400894 case EVIOCRMFF:
895 return input_ff_erase(dev, (int)(unsigned long) p, file);
896
897 case EVIOCGEFFECTS:
898 i = test_bit(EV_FF, dev->evbit) ?
899 dev->ff->max_effects : 0;
900 if (put_user(i, ip))
901 return -EFAULT;
902 return 0;
903
904 case EVIOCGRAB:
905 if (p)
906 return evdev_grab(evdev, client);
907 else
908 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800909
David Herrmannc7dc6572013-09-07 12:23:05 -0700910 case EVIOCREVOKE:
911 if (p)
912 return -EINVAL;
913 else
914 return evdev_revoke(evdev, client, file);
915
John Stultza80b83b2012-02-03 00:19:07 -0800916 case EVIOCSCLOCKID:
917 if (copy_from_user(&i, p, sizeof(unsigned int)))
918 return -EFAULT;
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800919
920 return evdev_set_clk_type(client, i);
John Stultza80b83b2012-02-03 00:19:07 -0800921
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800922 case EVIOCGKEYCODE:
923 return evdev_handle_get_keycode(dev, p);
924
925 case EVIOCSKEYCODE:
926 return evdev_handle_set_keycode(dev, p);
927
928 case EVIOCGKEYCODE_V2:
929 return evdev_handle_get_keycode_v2(dev, p);
930
931 case EVIOCSKEYCODE_V2:
932 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700933 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400934
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700935 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400936
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700937 /* Now check variable-length commands */
938#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700939 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400940
Henrik Rydberg85b77202010-12-18 20:51:13 +0100941 case EVIOCGPROP(0):
942 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
943 size, p, compat_mode);
944
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100945 case EVIOCGMTSLOTS(0):
946 return evdev_handle_mt_request(dev, size, ip);
947
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700948 case EVIOCGKEY(0):
David Herrmann48318022013-04-07 21:13:19 -0700949 return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
950 KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400951
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700952 case EVIOCGLED(0):
David Herrmann48318022013-04-07 21:13:19 -0700953 return evdev_handle_get_val(client, dev, EV_LED, dev->led,
954 LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400955
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700956 case EVIOCGSND(0):
David Herrmann48318022013-04-07 21:13:19 -0700957 return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
958 SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400959
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700960 case EVIOCGSW(0):
David Herrmann48318022013-04-07 21:13:19 -0700961 return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
962 SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400963
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700964 case EVIOCGNAME(0):
965 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400966
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700967 case EVIOCGPHYS(0):
968 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400969
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700970 case EVIOCGUNIQ(0):
971 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400972
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700973 case EVIOC_MASK_SIZE(EVIOCSFF):
974 if (input_ff_effect_from_user(p, size, &effect))
975 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400976
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700977 error = input_ff_upload(dev, &effect, file);
Elias Vanderstuyftfc7392a2014-03-29 12:08:45 -0700978 if (error)
979 return error;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400980
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700981 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
982 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400983
Elias Vanderstuyftfc7392a2014-03-29 12:08:45 -0700984 return 0;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700985 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400986
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700987 /* Multi-number variable-length handlers */
988 if (_IOC_TYPE(cmd) != 'E')
989 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700991 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700993 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
994 return handle_eviocgbit(dev,
995 _IOC_NR(cmd) & EV_MAX, size,
996 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700998 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400999
Daniel Mack0a74a1d2010-10-18 08:43:30 -07001000 if (!dev->absinfo)
1001 return -EINVAL;
1002
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001003 t = _IOC_NR(cmd) & ABS_MAX;
1004 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -04001005
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001006 if (copy_to_user(p, &abs, min_t(size_t,
1007 size, sizeof(struct input_absinfo))))
1008 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -04001009
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001010 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001013
Daniel Mackf9ce6eb52010-10-18 08:43:50 -07001014 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001015
1016 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
1017
Daniel Mack0a74a1d2010-10-18 08:43:30 -07001018 if (!dev->absinfo)
1019 return -EINVAL;
1020
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001021 t = _IOC_NR(cmd) & ABS_MAX;
1022
1023 if (copy_from_user(&abs, p, min_t(size_t,
1024 size, sizeof(struct input_absinfo))))
1025 return -EFAULT;
1026
1027 if (size < sizeof(struct input_absinfo))
1028 abs.resolution = 0;
1029
1030 /* We can't change number of reserved MT slots */
1031 if (t == ABS_MT_SLOT)
1032 return -EINVAL;
1033
1034 /*
1035 * Take event lock to ensure that we are not
1036 * changing device parameters in the middle
1037 * of event.
1038 */
1039 spin_lock_irq(&dev->event_lock);
1040 dev->absinfo[t] = abs;
1041 spin_unlock_irq(&dev->event_lock);
1042
1043 return 0;
1044 }
1045 }
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return -EINVAL;
1048}
1049
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001050static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
1051 void __user *p, int compat_mode)
1052{
1053 struct evdev_client *client = file->private_data;
1054 struct evdev *evdev = client->evdev;
1055 int retval;
1056
1057 retval = mutex_lock_interruptible(&evdev->mutex);
1058 if (retval)
1059 return retval;
1060
David Herrmannc7dc6572013-09-07 12:23:05 -07001061 if (!evdev->exist || client->revoked) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001062 retval = -ENODEV;
1063 goto out;
1064 }
1065
1066 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
1067
1068 out:
1069 mutex_unlock(&evdev->mutex);
1070 return retval;
1071}
1072
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001073static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1074{
1075 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
1076}
1077
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001078#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001079static long evdev_ioctl_compat(struct file *file,
1080 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001081{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001082 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001083}
1084#endif
1085
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001086static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001087 .owner = THIS_MODULE,
1088 .read = evdev_read,
1089 .write = evdev_write,
1090 .poll = evdev_poll,
1091 .open = evdev_open,
1092 .release = evdev_release,
1093 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001094#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001095 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001096#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001097 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001098 .flush = evdev_flush,
1099 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100};
1101
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001102/*
1103 * Mark device non-existent. This disables writes, ioctls and
1104 * prevents new users from opening the device. Already posted
1105 * blocking reads will stay, however new ones will fail.
1106 */
1107static void evdev_mark_dead(struct evdev *evdev)
1108{
1109 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001110 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001111 mutex_unlock(&evdev->mutex);
1112}
1113
1114static void evdev_cleanup(struct evdev *evdev)
1115{
1116 struct input_handle *handle = &evdev->handle;
1117
1118 evdev_mark_dead(evdev);
1119 evdev_hangup(evdev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001120
1121 cdev_del(&evdev->cdev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001122
1123 /* evdev is marked dead so no one else accesses evdev->open */
1124 if (evdev->open) {
1125 input_flush_device(handle, NULL);
1126 input_close_device(handle);
1127 }
1128}
1129
1130/*
1131 * Create new evdev device. Note that input core serializes calls
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001132 * to connect and disconnect.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001133 */
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001134static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1135 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct evdev *evdev;
1138 int minor;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001139 int dev_no;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001140 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001142 minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
1143 if (minor < 0) {
1144 error = minor;
1145 pr_err("failed to reserve new minor: %d\n", error);
1146 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001149 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001150 if (!evdev) {
1151 error = -ENOMEM;
1152 goto err_free_minor;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001155 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001156 spin_lock_init(&evdev->client_lock);
1157 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 init_waitqueue_head(&evdev->wait);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001159 evdev->exist = true;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001160
1161 dev_no = minor;
1162 /* Normalize device number if it falls into legacy range */
1163 if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
1164 dev_no -= EVDEV_MINOR_BASE;
1165 dev_set_name(&evdev->dev, "event%d", dev_no);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001166
Dmitry Torokhova7097ff2008-04-01 00:22:53 -04001167 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001168 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 evdev->handle.handler = handler;
1170 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001171
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001172 evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001173 evdev->dev.class = &input_class;
1174 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001175 evdev->dev.release = evdev_free;
1176 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001178 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001179 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001180 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001182 cdev_init(&evdev->cdev, &evdev_fops);
Dmitry Torokhov4a215aa2012-10-21 17:57:20 -07001183 evdev->cdev.kobj.parent = &evdev->dev.kobj;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001184 error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001185 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001186 goto err_unregister_handle;
1187
1188 error = device_add(&evdev->dev);
1189 if (error)
1190 goto err_cleanup_evdev;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001191
1192 return 0;
1193
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001194 err_cleanup_evdev:
1195 evdev_cleanup(evdev);
1196 err_unregister_handle:
1197 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001198 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001199 put_device(&evdev->dev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001200 err_free_minor:
1201 input_free_minor(minor);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001202 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
1205static void evdev_disconnect(struct input_handle *handle)
1206{
1207 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001209 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001210 evdev_cleanup(evdev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001211 input_free_minor(MINOR(evdev->dev.devt));
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001212 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001213 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001216static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 { .driver_info = 1 }, /* Matches all devices */
1218 { }, /* Terminating zero entry */
1219};
1220
1221MODULE_DEVICE_TABLE(input, evdev_ids);
1222
1223static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001224 .event = evdev_event,
Henrik Rydberga274ac12012-08-29 20:48:02 +02001225 .events = evdev_events,
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001226 .connect = evdev_connect,
1227 .disconnect = evdev_disconnect,
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001228 .legacy_minors = true,
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001229 .minor = EVDEV_MINOR_BASE,
1230 .name = "evdev",
1231 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232};
1233
1234static int __init evdev_init(void)
1235{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001236 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239static void __exit evdev_exit(void)
1240{
1241 input_unregister_handler(&evdev_handler);
1242}
1243
1244module_init(evdev_init);
1245module_exit(evdev_exit);
1246
1247MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1248MODULE_DESCRIPTION("Input driver event char devices");
1249MODULE_LICENSE("GPL");