blob: 9d35499faca46bb3067138e795e5544527d956d6 [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
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800111static void __evdev_queue_syn_dropped(struct evdev_client *client)
David Herrmann48318022013-04-07 21:13:19 -0700112{
David Herrmann48318022013-04-07 21:13:19 -0700113 struct input_event ev;
114 ktime_t time;
115
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800116 time = client->clk_type == EV_CLK_REAL ?
117 ktime_get_real() :
118 client->clk_type == EV_CLK_MONO ?
119 ktime_get() :
120 ktime_get_boottime();
David Herrmann48318022013-04-07 21:13:19 -0700121
122 ev.time = ktime_to_timeval(time);
123 ev.type = EV_SYN;
124 ev.code = SYN_DROPPED;
125 ev.value = 0;
126
David Herrmann48318022013-04-07 21:13:19 -0700127 client->buffer[client->head++] = ev;
128 client->head &= client->bufsize - 1;
129
130 if (unlikely(client->head == client->tail)) {
131 /* drop queue but keep our SYN_DROPPED event */
132 client->tail = (client->head - 1) & (client->bufsize - 1);
133 client->packet_head = client->tail;
134 }
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800135}
David Herrmann48318022013-04-07 21:13:19 -0700136
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800137static void evdev_queue_syn_dropped(struct evdev_client *client)
138{
139 unsigned long flags;
140
141 spin_lock_irqsave(&client->buffer_lock, flags);
142 __evdev_queue_syn_dropped(client);
David Herrmann48318022013-04-07 21:13:19 -0700143 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{
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800148 unsigned long flags;
149
Anshul Garg0c3e9942015-01-15 09:06:50 -0800150 if (client->clk_type == clkid)
151 return 0;
152
153 switch (clkid) {
154
155 case CLOCK_REALTIME:
156 client->clk_type = EV_CLK_REAL;
157 break;
158 case CLOCK_MONOTONIC:
159 client->clk_type = EV_CLK_MONO;
160 break;
161 case CLOCK_BOOTTIME:
162 client->clk_type = EV_CLK_BOOT;
163 break;
164 default:
165 return -EINVAL;
166 }
167
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800168 /*
169 * Flush pending events and queue SYN_DROPPED event,
170 * but only if the queue is not empty.
171 */
172 spin_lock_irqsave(&client->buffer_lock, flags);
173
174 if (client->head != client->tail) {
175 client->packet_head = client->head = client->tail;
176 __evdev_queue_syn_dropped(client);
177 }
178
179 spin_unlock_irqrestore(&client->buffer_lock, flags);
Anshul Garg0c3e9942015-01-15 09:06:50 -0800180
181 return 0;
182}
183
Henrik Rydberga274ac12012-08-29 20:48:02 +0200184static void __pass_event(struct evdev_client *client,
185 const struct input_event *event)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400186{
Jeff Brown9fb0f142011-04-12 23:29:38 -0700187 client->buffer[client->head++] = *event;
188 client->head &= client->bufsize - 1;
189
190 if (unlikely(client->head == client->tail)) {
191 /*
192 * This effectively "drops" all unconsumed events, leaving
193 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
194 */
195 client->tail = (client->head - 2) & (client->bufsize - 1);
196
197 client->buffer[client->tail].time = event->time;
198 client->buffer[client->tail].type = EV_SYN;
199 client->buffer[client->tail].code = SYN_DROPPED;
200 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -0700201
202 client->packet_head = client->tail;
203 }
204
205 if (event->type == EV_SYN && event->code == SYN_REPORT) {
206 client->packet_head = client->head;
207 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -0700208 }
Henrik Rydberga274ac12012-08-29 20:48:02 +0200209}
210
211static void evdev_pass_values(struct evdev_client *client,
212 const struct input_value *vals, unsigned int count,
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800213 ktime_t *ev_time)
Henrik Rydberga274ac12012-08-29 20:48:02 +0200214{
215 struct evdev *evdev = client->evdev;
216 const struct input_value *v;
217 struct input_event event;
218 bool wakeup = false;
219
David Herrmannc7dc6572013-09-07 12:23:05 -0700220 if (client->revoked)
221 return;
222
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800223 event.time = ktime_to_timeval(ev_time[client->clk_type]);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200224
225 /* Interrupts are disabled, just acquire the lock. */
226 spin_lock(&client->buffer_lock);
227
228 for (v = vals; v != vals + count; v++) {
229 event.type = v->type;
230 event.code = v->code;
231 event.value = v->value;
232 __pass_event(client, &event);
233 if (v->type == EV_SYN && v->code == SYN_REPORT)
234 wakeup = true;
235 }
Jeff Brown9fb0f142011-04-12 23:29:38 -0700236
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400237 spin_unlock(&client->buffer_lock);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200238
239 if (wakeup)
240 wake_up_interruptible(&evdev->wait);
241}
242
243/*
244 * Pass incoming events to all connected clients.
245 */
246static void evdev_events(struct input_handle *handle,
247 const struct input_value *vals, unsigned int count)
248{
249 struct evdev *evdev = handle->private;
250 struct evdev_client *client;
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800251 ktime_t ev_time[EV_CLK_MAX];
Henrik Rydberga274ac12012-08-29 20:48:02 +0200252
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800253 ev_time[EV_CLK_MONO] = ktime_get();
254 ev_time[EV_CLK_REAL] = ktime_mono_to_real(ev_time[EV_CLK_MONO]);
255 ev_time[EV_CLK_BOOT] = ktime_mono_to_any(ev_time[EV_CLK_MONO],
256 TK_OFFS_BOOT);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200257
258 rcu_read_lock();
259
260 client = rcu_dereference(evdev->grab);
261
262 if (client)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800263 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200264 else
265 list_for_each_entry_rcu(client, &evdev->client_list, node)
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800266 evdev_pass_values(client, vals, count, ev_time);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200267
268 rcu_read_unlock();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400269}
270
271/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400272 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400273 */
274static void evdev_event(struct input_handle *handle,
275 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Henrik Rydberga274ac12012-08-29 20:48:02 +0200277 struct input_value vals[] = { { type, code, value } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Henrik Rydberga274ac12012-08-29 20:48:02 +0200279 evdev_events(handle, vals, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static int evdev_fasync(int fd, struct file *file, int on)
283{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400284 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400285
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700286 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400289static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400291 struct evdev_client *client = file->private_data;
292 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400293 int retval;
294
295 retval = mutex_lock_interruptible(&evdev->mutex);
296 if (retval)
297 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400298
David Herrmannc7dc6572013-09-07 12:23:05 -0700299 if (!evdev->exist || client->revoked)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400300 retval = -ENODEV;
301 else
302 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400303
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400304 mutex_unlock(&evdev->mutex);
305 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400308static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400310 struct evdev *evdev = container_of(dev, struct evdev, dev);
311
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400312 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 kfree(evdev);
314}
315
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400316/*
317 * Grabs an event device (along with underlying input device).
318 * This function is called with evdev->mutex taken.
319 */
320static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
321{
322 int error;
323
324 if (evdev->grab)
325 return -EBUSY;
326
327 error = input_grab_device(&evdev->handle);
328 if (error)
329 return error;
330
331 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400332
333 return 0;
334}
335
336static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
337{
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700338 struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
339 lockdep_is_held(&evdev->mutex));
340
341 if (grab != client)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400342 return -EINVAL;
343
344 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400345 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400346 input_release_device(&evdev->handle);
347
348 return 0;
349}
350
351static void evdev_attach_client(struct evdev *evdev,
352 struct evdev_client *client)
353{
354 spin_lock(&evdev->client_lock);
355 list_add_tail_rcu(&client->node, &evdev->client_list);
356 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400357}
358
359static void evdev_detach_client(struct evdev *evdev,
360 struct evdev_client *client)
361{
362 spin_lock(&evdev->client_lock);
363 list_del_rcu(&client->node);
364 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400365 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400366}
367
368static int evdev_open_device(struct evdev *evdev)
369{
370 int retval;
371
372 retval = mutex_lock_interruptible(&evdev->mutex);
373 if (retval)
374 return retval;
375
376 if (!evdev->exist)
377 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400378 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400379 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400380 if (retval)
381 evdev->open--;
382 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400383
384 mutex_unlock(&evdev->mutex);
385 return retval;
386}
387
388static void evdev_close_device(struct evdev *evdev)
389{
390 mutex_lock(&evdev->mutex);
391
392 if (evdev->exist && !--evdev->open)
393 input_close_device(&evdev->handle);
394
395 mutex_unlock(&evdev->mutex);
396}
397
398/*
399 * Wake up users waiting for IO so they can disconnect from
400 * dead device.
401 */
402static void evdev_hangup(struct evdev *evdev)
403{
404 struct evdev_client *client;
405
406 spin_lock(&evdev->client_lock);
407 list_for_each_entry(client, &evdev->client_list, node)
408 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
409 spin_unlock(&evdev->client_lock);
410
411 wake_up_interruptible(&evdev->wait);
412}
413
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400414static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400416 struct evdev_client *client = file->private_data;
417 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400419 mutex_lock(&evdev->mutex);
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700420 evdev_ungrab(evdev, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400421 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400423 evdev_detach_client(evdev, client);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700424
Pekka Enberg67367fd2015-05-15 13:45:40 -0700425 kvfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400427 evdev_close_device(evdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700432static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
433{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700434 unsigned int n_events =
435 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
436 EVDEV_MIN_BUFFER_SIZE);
437
438 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700439}
440
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400441static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700443 struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
444 unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
Daniel Stone92eb77d2013-10-31 00:25:34 -0700445 unsigned int size = sizeof(struct evdev_client) +
446 bufsize * sizeof(struct input_event);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400447 struct evdev_client *client;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400448 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Daniel Stone92eb77d2013-10-31 00:25:34 -0700450 client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
451 if (!client)
452 client = vzalloc(size);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -0700453 if (!client)
454 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700456 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400457 spin_lock_init(&client->buffer_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400458 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400459 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400461 error = evdev_open_device(evdev);
462 if (error)
463 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400465 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800466 nonseekable_open(inode, file);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400469
470 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400471 evdev_detach_client(evdev, client);
Andrew Morton92788ac2014-12-02 15:59:31 -0800472 kvfree(client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400473 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400476static ssize_t evdev_write(struct file *file, const char __user *buffer,
477 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400479 struct evdev_client *client = file->private_data;
480 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800482 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700484 if (count != 0 && count < input_event_size())
Peter Korsgaard439581e2011-02-25 09:30:46 -0800485 return -EINVAL;
486
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400487 retval = mutex_lock_interruptible(&evdev->mutex);
488 if (retval)
489 return retval;
490
David Herrmannc7dc6572013-09-07 12:23:05 -0700491 if (!evdev->exist || client->revoked) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400492 retval = -ENODEV;
493 goto out;
494 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500495
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700496 while (retval + input_event_size() <= count) {
497
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400498 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400499 retval = -EFAULT;
500 goto out;
501 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800502 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400503
504 input_inject_event(&evdev->handle,
505 event.type, event.code, event.value);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700506 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500507
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400508 out:
509 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500510 return retval;
511}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500512
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400513static int evdev_fetch_next_event(struct evdev_client *client,
514 struct input_event *event)
515{
516 int have_event;
517
518 spin_lock_irq(&client->buffer_lock);
519
Dima Zavin566cf5b2011-12-30 15:16:44 -0800520 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400521 if (have_event) {
522 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700523 client->tail &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400524 }
525
526 spin_unlock_irq(&client->buffer_lock);
527
528 return have_event;
529}
530
531static ssize_t evdev_read(struct file *file, char __user *buffer,
532 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400534 struct evdev_client *client = file->private_data;
535 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400536 struct input_event event;
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700537 size_t read = 0;
538 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700540 if (count != 0 && count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -EINVAL;
542
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700543 for (;;) {
David Herrmannc7dc6572013-09-07 12:23:05 -0700544 if (!evdev->exist || client->revoked)
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700545 return -ENODEV;
546
547 if (client->packet_head == client->tail &&
548 (file->f_flags & O_NONBLOCK))
549 return -EAGAIN;
550
551 /*
552 * count == 0 is special - no IO is done but we check
553 * for error conditions (see above).
554 */
555 if (count == 0)
556 break;
557
558 while (read + input_event_size() <= count &&
559 evdev_fetch_next_event(client, &event)) {
560
561 if (input_event_to_user(buffer + read, &event))
562 return -EFAULT;
563
564 read += input_event_size();
565 }
566
567 if (read)
568 break;
569
570 if (!(file->f_flags & O_NONBLOCK)) {
571 error = wait_event_interruptible(evdev->wait,
572 client->packet_head != client->tail ||
David Herrmannc7dc6572013-09-07 12:23:05 -0700573 !evdev->exist || client->revoked);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700574 if (error)
575 return error;
576 }
Dima Zavin509f87c2011-12-30 15:16:44 -0800577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700579 return read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582/* No kernel lock - fine */
583static unsigned int evdev_poll(struct file *file, poll_table *wait)
584{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400585 struct evdev_client *client = file->private_data;
586 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700587 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400588
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400589 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700590
David Herrmannc7dc6572013-09-07 12:23:05 -0700591 if (evdev->exist && !client->revoked)
592 mask = POLLOUT | POLLWRNORM;
593 else
594 mask = POLLHUP | POLLERR;
595
Jeff Browncdda9112011-04-26 22:16:11 -0700596 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700597 mask |= POLLIN | POLLRDNORM;
598
599 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500602#ifdef CONFIG_COMPAT
603
604#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700605#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500606
607#ifdef __BIG_ENDIAN
608static int bits_to_user(unsigned long *bits, unsigned int maxbit,
609 unsigned int maxlen, void __user *p, int compat)
610{
611 int len, i;
612
613 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700614 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400615 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500616 len = maxlen;
617
618 for (i = 0; i < len / sizeof(compat_long_t); i++)
619 if (copy_to_user((compat_long_t __user *) p + i,
620 (compat_long_t *) bits +
621 i + 1 - ((i % 2) << 1),
622 sizeof(compat_long_t)))
623 return -EFAULT;
624 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700625 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500626 if (len > maxlen)
627 len = maxlen;
628
629 if (copy_to_user(p, bits, len))
630 return -EFAULT;
631 }
632
633 return len;
634}
635#else
636static int bits_to_user(unsigned long *bits, unsigned int maxbit,
637 unsigned int maxlen, void __user *p, int compat)
638{
639 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700640 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
641 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500642
643 if (len > maxlen)
644 len = maxlen;
645
646 return copy_to_user(p, bits, len) ? -EFAULT : len;
647}
648#endif /* __BIG_ENDIAN */
649
650#else
651
652static int bits_to_user(unsigned long *bits, unsigned int maxbit,
653 unsigned int maxlen, void __user *p, int compat)
654{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700655 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500656
657 if (len > maxlen)
658 len = maxlen;
659
660 return copy_to_user(p, bits, len) ? -EFAULT : len;
661}
662
663#endif /* CONFIG_COMPAT */
664
665static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
666{
667 int len;
668
669 if (!str)
670 return -ENOENT;
671
672 len = strlen(str) + 1;
673 if (len > maxlen)
674 len = maxlen;
675
676 return copy_to_user(p, str, len) ? -EFAULT : len;
677}
678
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700679static int handle_eviocgbit(struct input_dev *dev,
680 unsigned int type, unsigned int size,
681 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400682{
683 unsigned long *bits;
684 int len;
685
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700686 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400687
688 case 0: bits = dev->evbit; len = EV_MAX; break;
689 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
690 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
691 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
692 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
693 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
694 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
695 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
696 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
697 default: return -EINVAL;
698 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400699
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700700 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400701}
Linus Torvalds5402a732008-08-05 11:42:42 -0400702
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800703static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
704{
705 struct input_keymap_entry ke = {
706 .len = sizeof(unsigned int),
707 .flags = 0,
708 };
709 int __user *ip = (int __user *)p;
710 int error;
711
712 /* legacy case */
713 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
714 return -EFAULT;
715
716 error = input_get_keycode(dev, &ke);
717 if (error)
718 return error;
719
720 if (put_user(ke.keycode, ip + 1))
721 return -EFAULT;
722
723 return 0;
724}
725
726static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700727{
728 struct input_keymap_entry ke;
729 int error;
730
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800731 if (copy_from_user(&ke, p, sizeof(ke)))
732 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700733
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800734 error = input_get_keycode(dev, &ke);
735 if (error)
736 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700737
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800738 if (copy_to_user(p, &ke, sizeof(ke)))
739 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700740
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700741 return 0;
742}
743
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800744static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
745{
746 struct input_keymap_entry ke = {
747 .len = sizeof(unsigned int),
748 .flags = 0,
749 };
750 int __user *ip = (int __user *)p;
751
752 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
753 return -EFAULT;
754
755 if (get_user(ke.keycode, ip + 1))
756 return -EFAULT;
757
758 return input_set_keycode(dev, &ke);
759}
760
761static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700762{
763 struct input_keymap_entry ke;
764
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800765 if (copy_from_user(&ke, p, sizeof(ke)))
766 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700767
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800768 if (ke.len > sizeof(ke.scancode))
769 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700770
771 return input_set_keycode(dev, &ke);
772}
773
David Herrmann48318022013-04-07 21:13:19 -0700774/*
775 * If we transfer state to the user, we should flush all pending events
776 * of the same type from the client's queue. Otherwise, they might end up
777 * with duplicate events, which can screw up client's state tracking.
778 * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
779 * event so user-space will notice missing events.
780 *
781 * LOCKING:
782 * We need to take event_lock before buffer_lock to avoid dead-locks. But we
783 * need the even_lock only to guarantee consistent state. We can safely release
784 * it while flushing the queue. This allows input-core to handle filters while
785 * we flush the queue.
786 */
787static int evdev_handle_get_val(struct evdev_client *client,
788 struct input_dev *dev, unsigned int type,
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700789 unsigned long *bits, unsigned int maxbit,
790 unsigned int maxlen, void __user *p,
791 int compat)
David Herrmann48318022013-04-07 21:13:19 -0700792{
793 int ret;
794 unsigned long *mem;
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700795 size_t len;
David Herrmann48318022013-04-07 21:13:19 -0700796
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700797 len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
798 mem = kmalloc(len, GFP_KERNEL);
David Herrmann48318022013-04-07 21:13:19 -0700799 if (!mem)
800 return -ENOMEM;
801
802 spin_lock_irq(&dev->event_lock);
803 spin_lock(&client->buffer_lock);
804
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700805 memcpy(mem, bits, len);
David Herrmann48318022013-04-07 21:13:19 -0700806
807 spin_unlock(&dev->event_lock);
808
809 __evdev_flush_queue(client, type);
810
811 spin_unlock_irq(&client->buffer_lock);
812
Dmitry Torokhov7c4f5602014-10-06 10:55:49 -0700813 ret = bits_to_user(mem, maxbit, maxlen, p, compat);
David Herrmann48318022013-04-07 21:13:19 -0700814 if (ret < 0)
Dmitry Torokhovb881d532015-02-05 15:56:28 -0800815 evdev_queue_syn_dropped(client);
David Herrmann48318022013-04-07 21:13:19 -0700816
817 kfree(mem);
818
819 return ret;
820}
821
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100822static int evdev_handle_mt_request(struct input_dev *dev,
823 unsigned int size,
824 int __user *ip)
825{
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200826 const struct input_mt *mt = dev->mt;
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100827 unsigned int code;
828 int max_slots;
829 int i;
830
831 if (get_user(code, &ip[0]))
832 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200833 if (!mt || !input_is_mt_value(code))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100834 return -EINVAL;
835
836 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200837 for (i = 0; i < mt->num_slots && i < max_slots; i++) {
838 int value = input_mt_get_value(&mt->slots[i], code);
839 if (put_user(value, &ip[1 + i]))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100840 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200841 }
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100842
843 return 0;
844}
845
David Herrmannc7dc6572013-09-07 12:23:05 -0700846static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
847 struct file *file)
848{
849 client->revoked = true;
850 evdev_ungrab(evdev, client);
851 input_flush_device(&evdev->handle, file);
852 wake_up_interruptible(&evdev->wait);
853
854 return 0;
855}
856
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400857static long evdev_do_ioctl(struct file *file, unsigned int cmd,
858 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400860 struct evdev_client *client = file->private_data;
861 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct input_dev *dev = evdev->handle.dev;
863 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400864 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500865 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800866 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700867 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400868 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700870 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 switch (cmd) {
872
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400873 case EVIOCGVERSION:
874 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400876 case EVIOCGID:
877 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
878 return -EFAULT;
879 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400880
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400881 case EVIOCGREP:
882 if (!test_bit(EV_REP, dev->evbit))
883 return -ENOSYS;
884 if (put_user(dev->rep[REP_DELAY], ip))
885 return -EFAULT;
886 if (put_user(dev->rep[REP_PERIOD], ip + 1))
887 return -EFAULT;
888 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400889
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400890 case EVIOCSREP:
891 if (!test_bit(EV_REP, dev->evbit))
892 return -ENOSYS;
893 if (get_user(u, ip))
894 return -EFAULT;
895 if (get_user(v, ip + 1))
896 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400897
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400898 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
899 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500900
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400901 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400903 case EVIOCRMFF:
904 return input_ff_erase(dev, (int)(unsigned long) p, file);
905
906 case EVIOCGEFFECTS:
907 i = test_bit(EV_FF, dev->evbit) ?
908 dev->ff->max_effects : 0;
909 if (put_user(i, ip))
910 return -EFAULT;
911 return 0;
912
913 case EVIOCGRAB:
914 if (p)
915 return evdev_grab(evdev, client);
916 else
917 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800918
David Herrmannc7dc6572013-09-07 12:23:05 -0700919 case EVIOCREVOKE:
920 if (p)
921 return -EINVAL;
922 else
923 return evdev_revoke(evdev, client, file);
924
John Stultza80b83b2012-02-03 00:19:07 -0800925 case EVIOCSCLOCKID:
926 if (copy_from_user(&i, p, sizeof(unsigned int)))
927 return -EFAULT;
Aniroop Mathuraac8bcf2014-12-17 15:33:06 -0800928
929 return evdev_set_clk_type(client, i);
John Stultza80b83b2012-02-03 00:19:07 -0800930
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800931 case EVIOCGKEYCODE:
932 return evdev_handle_get_keycode(dev, p);
933
934 case EVIOCSKEYCODE:
935 return evdev_handle_set_keycode(dev, p);
936
937 case EVIOCGKEYCODE_V2:
938 return evdev_handle_get_keycode_v2(dev, p);
939
940 case EVIOCSKEYCODE_V2:
941 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700942 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400943
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700944 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400945
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700946 /* Now check variable-length commands */
947#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700948 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400949
Henrik Rydberg85b77202010-12-18 20:51:13 +0100950 case EVIOCGPROP(0):
951 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
952 size, p, compat_mode);
953
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100954 case EVIOCGMTSLOTS(0):
955 return evdev_handle_mt_request(dev, size, ip);
956
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700957 case EVIOCGKEY(0):
David Herrmann48318022013-04-07 21:13:19 -0700958 return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
959 KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400960
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700961 case EVIOCGLED(0):
David Herrmann48318022013-04-07 21:13:19 -0700962 return evdev_handle_get_val(client, dev, EV_LED, dev->led,
963 LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400964
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700965 case EVIOCGSND(0):
David Herrmann48318022013-04-07 21:13:19 -0700966 return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
967 SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400968
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700969 case EVIOCGSW(0):
David Herrmann48318022013-04-07 21:13:19 -0700970 return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
971 SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400972
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700973 case EVIOCGNAME(0):
974 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400975
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700976 case EVIOCGPHYS(0):
977 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400978
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700979 case EVIOCGUNIQ(0):
980 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400981
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700982 case EVIOC_MASK_SIZE(EVIOCSFF):
983 if (input_ff_effect_from_user(p, size, &effect))
984 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400985
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700986 error = input_ff_upload(dev, &effect, file);
Elias Vanderstuyftfc7392a2014-03-29 12:08:45 -0700987 if (error)
988 return error;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400989
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700990 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
991 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400992
Elias Vanderstuyftfc7392a2014-03-29 12:08:45 -0700993 return 0;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700994 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400995
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700996 /* Multi-number variable-length handlers */
997 if (_IOC_TYPE(cmd) != 'E')
998 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001000 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001002 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
1003 return handle_eviocgbit(dev,
1004 _IOC_NR(cmd) & EV_MAX, size,
1005 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001007 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -04001008
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 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -04001014
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001015 if (copy_to_user(p, &abs, min_t(size_t,
1016 size, sizeof(struct input_absinfo))))
1017 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -04001018
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001019 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001022
Daniel Mackf9ce6eb2010-10-18 08:43:50 -07001023 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001024
1025 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
1026
Daniel Mack0a74a1d2010-10-18 08:43:30 -07001027 if (!dev->absinfo)
1028 return -EINVAL;
1029
Dmitry Torokhov448cd162010-08-02 20:29:10 -07001030 t = _IOC_NR(cmd) & ABS_MAX;
1031
1032 if (copy_from_user(&abs, p, min_t(size_t,
1033 size, sizeof(struct input_absinfo))))
1034 return -EFAULT;
1035
1036 if (size < sizeof(struct input_absinfo))
1037 abs.resolution = 0;
1038
1039 /* We can't change number of reserved MT slots */
1040 if (t == ABS_MT_SLOT)
1041 return -EINVAL;
1042
1043 /*
1044 * Take event lock to ensure that we are not
1045 * changing device parameters in the middle
1046 * of event.
1047 */
1048 spin_lock_irq(&dev->event_lock);
1049 dev->absinfo[t] = abs;
1050 spin_unlock_irq(&dev->event_lock);
1051
1052 return 0;
1053 }
1054 }
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return -EINVAL;
1057}
1058
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001059static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
1060 void __user *p, int compat_mode)
1061{
1062 struct evdev_client *client = file->private_data;
1063 struct evdev *evdev = client->evdev;
1064 int retval;
1065
1066 retval = mutex_lock_interruptible(&evdev->mutex);
1067 if (retval)
1068 return retval;
1069
David Herrmannc7dc6572013-09-07 12:23:05 -07001070 if (!evdev->exist || client->revoked) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001071 retval = -ENODEV;
1072 goto out;
1073 }
1074
1075 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
1076
1077 out:
1078 mutex_unlock(&evdev->mutex);
1079 return retval;
1080}
1081
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001082static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1083{
1084 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
1085}
1086
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001087#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001088static long evdev_ioctl_compat(struct file *file,
1089 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001090{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001091 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001092}
1093#endif
1094
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001095static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001096 .owner = THIS_MODULE,
1097 .read = evdev_read,
1098 .write = evdev_write,
1099 .poll = evdev_poll,
1100 .open = evdev_open,
1101 .release = evdev_release,
1102 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001103#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001104 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001105#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001106 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001107 .flush = evdev_flush,
1108 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109};
1110
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001111/*
1112 * Mark device non-existent. This disables writes, ioctls and
1113 * prevents new users from opening the device. Already posted
1114 * blocking reads will stay, however new ones will fail.
1115 */
1116static void evdev_mark_dead(struct evdev *evdev)
1117{
1118 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001119 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001120 mutex_unlock(&evdev->mutex);
1121}
1122
1123static void evdev_cleanup(struct evdev *evdev)
1124{
1125 struct input_handle *handle = &evdev->handle;
1126
1127 evdev_mark_dead(evdev);
1128 evdev_hangup(evdev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001129
1130 cdev_del(&evdev->cdev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001131
1132 /* evdev is marked dead so no one else accesses evdev->open */
1133 if (evdev->open) {
1134 input_flush_device(handle, NULL);
1135 input_close_device(handle);
1136 }
1137}
1138
1139/*
1140 * Create new evdev device. Note that input core serializes calls
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001141 * to connect and disconnect.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001142 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001143static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1144 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 struct evdev *evdev;
1147 int minor;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001148 int dev_no;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001149 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001151 minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
1152 if (minor < 0) {
1153 error = minor;
1154 pr_err("failed to reserve new minor: %d\n", error);
1155 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001158 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001159 if (!evdev) {
1160 error = -ENOMEM;
1161 goto err_free_minor;
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001164 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001165 spin_lock_init(&evdev->client_lock);
1166 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 init_waitqueue_head(&evdev->wait);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001168 evdev->exist = true;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001169
1170 dev_no = minor;
1171 /* Normalize device number if it falls into legacy range */
1172 if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
1173 dev_no -= EVDEV_MINOR_BASE;
1174 dev_set_name(&evdev->dev, "event%d", dev_no);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001175
Dmitry Torokhova7097ff2008-04-01 00:22:53 -04001176 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001177 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 evdev->handle.handler = handler;
1179 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001180
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001181 evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001182 evdev->dev.class = &input_class;
1183 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001184 evdev->dev.release = evdev_free;
1185 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001187 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001188 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001189 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001191 cdev_init(&evdev->cdev, &evdev_fops);
Dmitry Torokhov4a215aa2012-10-21 17:57:20 -07001192 evdev->cdev.kobj.parent = &evdev->dev.kobj;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001193 error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001194 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001195 goto err_unregister_handle;
1196
1197 error = device_add(&evdev->dev);
1198 if (error)
1199 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001200
1201 return 0;
1202
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001203 err_cleanup_evdev:
1204 evdev_cleanup(evdev);
1205 err_unregister_handle:
1206 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001207 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001208 put_device(&evdev->dev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001209 err_free_minor:
1210 input_free_minor(minor);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001211 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214static void evdev_disconnect(struct input_handle *handle)
1215{
1216 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001218 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001219 evdev_cleanup(evdev);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001220 input_free_minor(MINOR(evdev->dev.devt));
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001221 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001222 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001225static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 { .driver_info = 1 }, /* Matches all devices */
1227 { }, /* Terminating zero entry */
1228};
1229
1230MODULE_DEVICE_TABLE(input, evdev_ids);
1231
1232static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001233 .event = evdev_event,
Henrik Rydberga274ac12012-08-29 20:48:02 +02001234 .events = evdev_events,
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001235 .connect = evdev_connect,
1236 .disconnect = evdev_disconnect,
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001237 .legacy_minors = true,
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001238 .minor = EVDEV_MINOR_BASE,
1239 .name = "evdev",
1240 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241};
1242
1243static int __init evdev_init(void)
1244{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001245 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248static void __exit evdev_exit(void)
1249{
1250 input_unregister_handler(&evdev_handler);
1251}
1252
1253module_init(evdev_init);
1254module_exit(evdev_exit);
1255
1256MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1257MODULE_DESCRIPTION("Input driver event char devices");
1258MODULE_LICENSE("GPL");