1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Event char devices, giving access to raw input device events.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (c) 1999-2002 Vojtech Pavlik
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
8da0c4901SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9da0c4901SJoe Perches
101da177e4SLinus Torvalds #define EVDEV_MINOR_BASE 64
111da177e4SLinus Torvalds #define EVDEV_MINORS 32
1263a6404dSHenrik Rydberg #define EVDEV_MIN_BUFFER_SIZE 64U
1363a6404dSHenrik Rydberg #define EVDEV_BUF_PACKETS 8
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds #include <linux/poll.h>
16a99bbaf5SAlexey Dobriyan #include <linux/sched.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
1892eb77d0SDaniel Stone #include <linux/vmalloc.h>
1992eb77d0SDaniel Stone #include <linux/mm.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/init.h>
221cf0c6e6SHenrik Rydberg #include <linux/input/mt.h>
231da177e4SLinus Torvalds #include <linux/major.h>
241da177e4SLinus Torvalds #include <linux/device.h>
257f8d4cadSDmitry Torokhov #include <linux/cdev.h>
262d56f3a3SPhilip Langdale #include "input-compat.h"
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds struct evdev {
291da177e4SLinus Torvalds int open;
301da177e4SLinus Torvalds struct input_handle handle;
312be85279SArnd Bergmann struct evdev_client __rcu *grab;
32d0ffb9beSDmitry Torokhov struct list_head client_list;
336addb1d6SDmitry Torokhov spinlock_t client_lock; /* protects client_list */
346addb1d6SDmitry Torokhov struct mutex mutex;
359657d75cSDmitry Torokhov struct device dev;
367f8d4cadSDmitry Torokhov struct cdev cdev;
3720da92deSDmitry Torokhov bool exist;
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds
40d0ffb9beSDmitry Torokhov struct evdev_client {
419fb0f14eSJeff Brown unsigned int head;
429fb0f14eSJeff Brown unsigned int tail;
43cdda911cSJeff Brown unsigned int packet_head; /* [future] position of the first element of next packet */
446addb1d6SDmitry Torokhov spinlock_t buffer_lock; /* protects access to buffer, head and tail */
454ba8b8aeSKenny Levinsen wait_queue_head_t wait;
461da177e4SLinus Torvalds struct fasync_struct *fasync;
471da177e4SLinus Torvalds struct evdev *evdev;
481da177e4SLinus Torvalds struct list_head node;
493b51c44bSAtif Niyaz enum input_clock_type clk_type;
50c7dc6573SDavid Herrmann bool revoked;
5106a16293SDavid Herrmann unsigned long *evmasks[EV_CNT];
529fb0f14eSJeff Brown unsigned int bufsize;
538927e688SKees Cook struct input_event buffer[] __counted_by(bufsize);
541da177e4SLinus Torvalds };
551da177e4SLinus Torvalds
evdev_get_mask_cnt(unsigned int type)5606a16293SDavid Herrmann static size_t evdev_get_mask_cnt(unsigned int type)
5706a16293SDavid Herrmann {
5806a16293SDavid Herrmann static const size_t counts[EV_CNT] = {
5906a16293SDavid Herrmann /* EV_SYN==0 is EV_CNT, _not_ SYN_CNT, see EVIOCGBIT */
6006a16293SDavid Herrmann [EV_SYN] = EV_CNT,
6106a16293SDavid Herrmann [EV_KEY] = KEY_CNT,
6206a16293SDavid Herrmann [EV_REL] = REL_CNT,
6306a16293SDavid Herrmann [EV_ABS] = ABS_CNT,
6406a16293SDavid Herrmann [EV_MSC] = MSC_CNT,
6506a16293SDavid Herrmann [EV_SW] = SW_CNT,
6606a16293SDavid Herrmann [EV_LED] = LED_CNT,
6706a16293SDavid Herrmann [EV_SND] = SND_CNT,
6806a16293SDavid Herrmann [EV_FF] = FF_CNT,
6906a16293SDavid Herrmann };
7006a16293SDavid Herrmann
7106a16293SDavid Herrmann return (type < EV_CNT) ? counts[type] : 0;
7206a16293SDavid Herrmann }
7306a16293SDavid Herrmann
7406a16293SDavid Herrmann /* requires the buffer lock to be held */
__evdev_is_filtered(struct evdev_client * client,unsigned int type,unsigned int code)7506a16293SDavid Herrmann static bool __evdev_is_filtered(struct evdev_client *client,
7606a16293SDavid Herrmann unsigned int type,
7706a16293SDavid Herrmann unsigned int code)
7806a16293SDavid Herrmann {
7906a16293SDavid Herrmann unsigned long *mask;
8006a16293SDavid Herrmann size_t cnt;
8106a16293SDavid Herrmann
8206a16293SDavid Herrmann /* EV_SYN and unknown codes are never filtered */
8306a16293SDavid Herrmann if (type == EV_SYN || type >= EV_CNT)
8406a16293SDavid Herrmann return false;
8506a16293SDavid Herrmann
8606a16293SDavid Herrmann /* first test whether the type is filtered */
8706a16293SDavid Herrmann mask = client->evmasks[0];
8806a16293SDavid Herrmann if (mask && !test_bit(type, mask))
8906a16293SDavid Herrmann return true;
9006a16293SDavid Herrmann
9106a16293SDavid Herrmann /* unknown values are never filtered */
9206a16293SDavid Herrmann cnt = evdev_get_mask_cnt(type);
9306a16293SDavid Herrmann if (!cnt || code >= cnt)
9406a16293SDavid Herrmann return false;
9506a16293SDavid Herrmann
9606a16293SDavid Herrmann mask = client->evmasks[type];
9706a16293SDavid Herrmann return mask && !test_bit(code, mask);
9806a16293SDavid Herrmann }
9906a16293SDavid Herrmann
10048318028SDavid Herrmann /* flush queued events of type @type, caller must hold client->buffer_lock */
__evdev_flush_queue(struct evdev_client * client,unsigned int type)10148318028SDavid Herrmann static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
10248318028SDavid Herrmann {
10348318028SDavid Herrmann unsigned int i, head, num;
10448318028SDavid Herrmann unsigned int mask = client->bufsize - 1;
10548318028SDavid Herrmann bool is_report;
10648318028SDavid Herrmann struct input_event *ev;
10748318028SDavid Herrmann
10848318028SDavid Herrmann BUG_ON(type == EV_SYN);
10948318028SDavid Herrmann
11048318028SDavid Herrmann head = client->tail;
11148318028SDavid Herrmann client->packet_head = client->tail;
11248318028SDavid Herrmann
11348318028SDavid Herrmann /* init to 1 so a leading SYN_REPORT will not be dropped */
11448318028SDavid Herrmann num = 1;
11548318028SDavid Herrmann
11648318028SDavid Herrmann for (i = client->tail; i != client->head; i = (i + 1) & mask) {
11748318028SDavid Herrmann ev = &client->buffer[i];
11848318028SDavid Herrmann is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
11948318028SDavid Herrmann
12048318028SDavid Herrmann if (ev->type == type) {
12148318028SDavid Herrmann /* drop matched entry */
12248318028SDavid Herrmann continue;
12348318028SDavid Herrmann } else if (is_report && !num) {
12448318028SDavid Herrmann /* drop empty SYN_REPORT groups */
12548318028SDavid Herrmann continue;
12648318028SDavid Herrmann } else if (head != i) {
12748318028SDavid Herrmann /* move entry to fill the gap */
128152194feSDeepa Dinamani client->buffer[head] = *ev;
12948318028SDavid Herrmann }
13048318028SDavid Herrmann
13148318028SDavid Herrmann num++;
13248318028SDavid Herrmann head = (head + 1) & mask;
13348318028SDavid Herrmann
13448318028SDavid Herrmann if (is_report) {
13548318028SDavid Herrmann num = 0;
13648318028SDavid Herrmann client->packet_head = head;
13748318028SDavid Herrmann }
13848318028SDavid Herrmann }
13948318028SDavid Herrmann
14048318028SDavid Herrmann client->head = head;
14148318028SDavid Herrmann }
14248318028SDavid Herrmann
__evdev_queue_syn_dropped(struct evdev_client * client)143b881d537SDmitry Torokhov static void __evdev_queue_syn_dropped(struct evdev_client *client)
14448318028SDavid Herrmann {
1453b51c44bSAtif Niyaz ktime_t *ev_time = input_get_timestamp(client->evdev->handle.dev);
1463b51c44bSAtif Niyaz struct timespec64 ts = ktime_to_timespec64(ev_time[client->clk_type]);
14748318028SDavid Herrmann struct input_event ev;
14848318028SDavid Herrmann
149152194feSDeepa Dinamani ev.input_event_sec = ts.tv_sec;
150152194feSDeepa Dinamani ev.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
15148318028SDavid Herrmann ev.type = EV_SYN;
15248318028SDavid Herrmann ev.code = SYN_DROPPED;
15348318028SDavid Herrmann ev.value = 0;
15448318028SDavid Herrmann
15548318028SDavid Herrmann client->buffer[client->head++] = ev;
15648318028SDavid Herrmann client->head &= client->bufsize - 1;
15748318028SDavid Herrmann
15848318028SDavid Herrmann if (unlikely(client->head == client->tail)) {
15948318028SDavid Herrmann /* drop queue but keep our SYN_DROPPED event */
16048318028SDavid Herrmann client->tail = (client->head - 1) & (client->bufsize - 1);
16148318028SDavid Herrmann client->packet_head = client->tail;
16248318028SDavid Herrmann }
163b881d537SDmitry Torokhov }
16448318028SDavid Herrmann
evdev_queue_syn_dropped(struct evdev_client * client)165b881d537SDmitry Torokhov static void evdev_queue_syn_dropped(struct evdev_client *client)
166b881d537SDmitry Torokhov {
167b881d537SDmitry Torokhov unsigned long flags;
168b881d537SDmitry Torokhov
169b881d537SDmitry Torokhov spin_lock_irqsave(&client->buffer_lock, flags);
170b881d537SDmitry Torokhov __evdev_queue_syn_dropped(client);
17148318028SDavid Herrmann spin_unlock_irqrestore(&client->buffer_lock, flags);
17248318028SDavid Herrmann }
17348318028SDavid Herrmann
evdev_set_clk_type(struct evdev_client * client,unsigned int clkid)1740c3e9943SAnshul Garg static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
1750c3e9943SAnshul Garg {
176b881d537SDmitry Torokhov unsigned long flags;
1773b51c44bSAtif Niyaz enum input_clock_type clk_type;
1780c3e9943SAnshul Garg
1790c3e9943SAnshul Garg switch (clkid) {
1800c3e9943SAnshul Garg
1810c3e9943SAnshul Garg case CLOCK_REALTIME:
1823b51c44bSAtif Niyaz clk_type = INPUT_CLK_REAL;
1830c3e9943SAnshul Garg break;
1840c3e9943SAnshul Garg case CLOCK_MONOTONIC:
1853b51c44bSAtif Niyaz clk_type = INPUT_CLK_MONO;
1860c3e9943SAnshul Garg break;
1870c3e9943SAnshul Garg case CLOCK_BOOTTIME:
1883b51c44bSAtif Niyaz clk_type = INPUT_CLK_BOOT;
1890c3e9943SAnshul Garg break;
1900c3e9943SAnshul Garg default:
1910c3e9943SAnshul Garg return -EINVAL;
1920c3e9943SAnshul Garg }
1930c3e9943SAnshul Garg
194bf5f18d7SAniroop Mathur if (client->clk_type != clk_type) {
195bf5f18d7SAniroop Mathur client->clk_type = clk_type;
196bf5f18d7SAniroop Mathur
197b881d537SDmitry Torokhov /*
198b881d537SDmitry Torokhov * Flush pending events and queue SYN_DROPPED event,
199b881d537SDmitry Torokhov * but only if the queue is not empty.
200b881d537SDmitry Torokhov */
201b881d537SDmitry Torokhov spin_lock_irqsave(&client->buffer_lock, flags);
202b881d537SDmitry Torokhov
203b881d537SDmitry Torokhov if (client->head != client->tail) {
204b881d537SDmitry Torokhov client->packet_head = client->head = client->tail;
205b881d537SDmitry Torokhov __evdev_queue_syn_dropped(client);
206b881d537SDmitry Torokhov }
207b881d537SDmitry Torokhov
208b881d537SDmitry Torokhov spin_unlock_irqrestore(&client->buffer_lock, flags);
209bf5f18d7SAniroop Mathur }
2100c3e9943SAnshul Garg
2110c3e9943SAnshul Garg return 0;
2120c3e9943SAnshul Garg }
2130c3e9943SAnshul Garg
__pass_event(struct evdev_client * client,const struct input_event * event)214a274ac15SHenrik Rydberg static void __pass_event(struct evdev_client *client,
215a274ac15SHenrik Rydberg const struct input_event *event)
2161da177e4SLinus Torvalds {
2176addb1d6SDmitry Torokhov client->buffer[client->head++] = *event;
218b58f7086SHenrik Rydberg client->head &= client->bufsize - 1;
2199fb0f14eSJeff Brown
2209fb0f14eSJeff Brown if (unlikely(client->head == client->tail)) {
2219fb0f14eSJeff Brown /*
2229fb0f14eSJeff Brown * This effectively "drops" all unconsumed events, leaving
2239fb0f14eSJeff Brown * EV_SYN/SYN_DROPPED plus the newest event in the queue.
2249fb0f14eSJeff Brown */
2259fb0f14eSJeff Brown client->tail = (client->head - 2) & (client->bufsize - 1);
2269fb0f14eSJeff Brown
227f729a1b0SArnd Bergmann client->buffer[client->tail] = (struct input_event) {
228f729a1b0SArnd Bergmann .input_event_sec = event->input_event_sec,
229f729a1b0SArnd Bergmann .input_event_usec = event->input_event_usec,
230f729a1b0SArnd Bergmann .type = EV_SYN,
231f729a1b0SArnd Bergmann .code = SYN_DROPPED,
232f729a1b0SArnd Bergmann .value = 0,
233f729a1b0SArnd Bergmann };
234cdda911cSJeff Brown
235cdda911cSJeff Brown client->packet_head = client->tail;
236cdda911cSJeff Brown }
237cdda911cSJeff Brown
238cdda911cSJeff Brown if (event->type == EV_SYN && event->code == SYN_REPORT) {
239cdda911cSJeff Brown client->packet_head = client->head;
240cdda911cSJeff Brown kill_fasync(&client->fasync, SIGIO, POLL_IN);
2419fb0f14eSJeff Brown }
242a274ac15SHenrik Rydberg }
243a274ac15SHenrik Rydberg
evdev_pass_values(struct evdev_client * client,const struct input_value * vals,unsigned int count,ktime_t * ev_time)244a274ac15SHenrik Rydberg static void evdev_pass_values(struct evdev_client *client,
245a274ac15SHenrik Rydberg const struct input_value *vals, unsigned int count,
246aac8bcf1SAniroop Mathur ktime_t *ev_time)
247a274ac15SHenrik Rydberg {
248a274ac15SHenrik Rydberg const struct input_value *v;
249a274ac15SHenrik Rydberg struct input_event event;
250152194feSDeepa Dinamani struct timespec64 ts;
251a274ac15SHenrik Rydberg bool wakeup = false;
252a274ac15SHenrik Rydberg
253c7dc6573SDavid Herrmann if (client->revoked)
254c7dc6573SDavid Herrmann return;
255c7dc6573SDavid Herrmann
256152194feSDeepa Dinamani ts = ktime_to_timespec64(ev_time[client->clk_type]);
257152194feSDeepa Dinamani event.input_event_sec = ts.tv_sec;
258152194feSDeepa Dinamani event.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
259a274ac15SHenrik Rydberg
260a274ac15SHenrik Rydberg /* Interrupts are disabled, just acquire the lock. */
261a274ac15SHenrik Rydberg spin_lock(&client->buffer_lock);
262a274ac15SHenrik Rydberg
263a274ac15SHenrik Rydberg for (v = vals; v != vals + count; v++) {
26406a16293SDavid Herrmann if (__evdev_is_filtered(client, v->type, v->code))
26506a16293SDavid Herrmann continue;
26606a16293SDavid Herrmann
26706a16293SDavid Herrmann if (v->type == EV_SYN && v->code == SYN_REPORT) {
26806a16293SDavid Herrmann /* drop empty SYN_REPORT */
26906a16293SDavid Herrmann if (client->packet_head == client->head)
27006a16293SDavid Herrmann continue;
27106a16293SDavid Herrmann
27206a16293SDavid Herrmann wakeup = true;
27306a16293SDavid Herrmann }
27406a16293SDavid Herrmann
275a274ac15SHenrik Rydberg event.type = v->type;
276a274ac15SHenrik Rydberg event.code = v->code;
277a274ac15SHenrik Rydberg event.value = v->value;
278a274ac15SHenrik Rydberg __pass_event(client, &event);
279a274ac15SHenrik Rydberg }
2809fb0f14eSJeff Brown
2816addb1d6SDmitry Torokhov spin_unlock(&client->buffer_lock);
282a274ac15SHenrik Rydberg
283a274ac15SHenrik Rydberg if (wakeup)
2844ba8b8aeSKenny Levinsen wake_up_interruptible_poll(&client->wait,
28581b4d1d2SKenny Levinsen EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM);
286a274ac15SHenrik Rydberg }
287a274ac15SHenrik Rydberg
288a274ac15SHenrik Rydberg /*
289a274ac15SHenrik Rydberg * Pass incoming events to all connected clients.
290a274ac15SHenrik Rydberg */
evdev_events(struct input_handle * handle,struct input_value * vals,unsigned int count)29114498e99SDmitry Torokhov static unsigned int evdev_events(struct input_handle *handle,
29214498e99SDmitry Torokhov struct input_value *vals, unsigned int count)
293a274ac15SHenrik Rydberg {
294a274ac15SHenrik Rydberg struct evdev *evdev = handle->private;
295a274ac15SHenrik Rydberg struct evdev_client *client;
2963b51c44bSAtif Niyaz ktime_t *ev_time = input_get_timestamp(handle->dev);
297a274ac15SHenrik Rydberg
298a274ac15SHenrik Rydberg rcu_read_lock();
299a274ac15SHenrik Rydberg
300a274ac15SHenrik Rydberg client = rcu_dereference(evdev->grab);
301a274ac15SHenrik Rydberg
302a274ac15SHenrik Rydberg if (client)
303aac8bcf1SAniroop Mathur evdev_pass_values(client, vals, count, ev_time);
304a274ac15SHenrik Rydberg else
305a274ac15SHenrik Rydberg list_for_each_entry_rcu(client, &evdev->client_list, node)
306aac8bcf1SAniroop Mathur evdev_pass_values(client, vals, count, ev_time);
307a274ac15SHenrik Rydberg
308a274ac15SHenrik Rydberg rcu_read_unlock();
30914498e99SDmitry Torokhov
31014498e99SDmitry Torokhov return count;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds
evdev_fasync(int fd,struct file * file,int on)3131da177e4SLinus Torvalds static int evdev_fasync(int fd, struct file *file, int on)
3141da177e4SLinus Torvalds {
315d0ffb9beSDmitry Torokhov struct evdev_client *client = file->private_data;
3161e0afb28SDmitry Torokhov
31760aa4924SJonathan Corbet return fasync_helper(fd, file, on, &client->fasync);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds
evdev_free(struct device * dev)3209657d75cSDmitry Torokhov static void evdev_free(struct device *dev)
3211da177e4SLinus Torvalds {
3229657d75cSDmitry Torokhov struct evdev *evdev = container_of(dev, struct evdev, dev);
3239657d75cSDmitry Torokhov
324a7097ff8SDmitry Torokhov input_put_device(evdev->handle.dev);
3251da177e4SLinus Torvalds kfree(evdev);
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds
3286addb1d6SDmitry Torokhov /*
3296addb1d6SDmitry Torokhov * Grabs an event device (along with underlying input device).
3306addb1d6SDmitry Torokhov * This function is called with evdev->mutex taken.
3316addb1d6SDmitry Torokhov */
evdev_grab(struct evdev * evdev,struct evdev_client * client)3326addb1d6SDmitry Torokhov static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
3336addb1d6SDmitry Torokhov {
3346addb1d6SDmitry Torokhov int error;
3356addb1d6SDmitry Torokhov
3366addb1d6SDmitry Torokhov if (evdev->grab)
3376addb1d6SDmitry Torokhov return -EBUSY;
3386addb1d6SDmitry Torokhov
3396addb1d6SDmitry Torokhov error = input_grab_device(&evdev->handle);
3406addb1d6SDmitry Torokhov if (error)
3416addb1d6SDmitry Torokhov return error;
3426addb1d6SDmitry Torokhov
3436addb1d6SDmitry Torokhov rcu_assign_pointer(evdev->grab, client);
3446addb1d6SDmitry Torokhov
3456addb1d6SDmitry Torokhov return 0;
3466addb1d6SDmitry Torokhov }
3476addb1d6SDmitry Torokhov
evdev_ungrab(struct evdev * evdev,struct evdev_client * client)3486addb1d6SDmitry Torokhov static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
3496addb1d6SDmitry Torokhov {
350dba42580SDmitry Torokhov struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
351dba42580SDmitry Torokhov lockdep_is_held(&evdev->mutex));
352dba42580SDmitry Torokhov
353dba42580SDmitry Torokhov if (grab != client)
3546addb1d6SDmitry Torokhov return -EINVAL;
3556addb1d6SDmitry Torokhov
3566addb1d6SDmitry Torokhov rcu_assign_pointer(evdev->grab, NULL);
35782ba56c2SDmitry Torokhov synchronize_rcu();
3586addb1d6SDmitry Torokhov input_release_device(&evdev->handle);
3596addb1d6SDmitry Torokhov
3606addb1d6SDmitry Torokhov return 0;
3616addb1d6SDmitry Torokhov }
3626addb1d6SDmitry Torokhov
evdev_attach_client(struct evdev * evdev,struct evdev_client * client)3636addb1d6SDmitry Torokhov static void evdev_attach_client(struct evdev *evdev,
3646addb1d6SDmitry Torokhov struct evdev_client *client)
3656addb1d6SDmitry Torokhov {
3666addb1d6SDmitry Torokhov spin_lock(&evdev->client_lock);
3676addb1d6SDmitry Torokhov list_add_tail_rcu(&client->node, &evdev->client_list);
3686addb1d6SDmitry Torokhov spin_unlock(&evdev->client_lock);
3696addb1d6SDmitry Torokhov }
3706addb1d6SDmitry Torokhov
evdev_detach_client(struct evdev * evdev,struct evdev_client * client)3716addb1d6SDmitry Torokhov static void evdev_detach_client(struct evdev *evdev,
3726addb1d6SDmitry Torokhov struct evdev_client *client)
3736addb1d6SDmitry Torokhov {
3746addb1d6SDmitry Torokhov spin_lock(&evdev->client_lock);
3756addb1d6SDmitry Torokhov list_del_rcu(&client->node);
3766addb1d6SDmitry Torokhov spin_unlock(&evdev->client_lock);
37782ba56c2SDmitry Torokhov synchronize_rcu();
3786addb1d6SDmitry Torokhov }
3796addb1d6SDmitry Torokhov
evdev_open_device(struct evdev * evdev)3806addb1d6SDmitry Torokhov static int evdev_open_device(struct evdev *evdev)
3816addb1d6SDmitry Torokhov {
3826addb1d6SDmitry Torokhov int retval;
3836addb1d6SDmitry Torokhov
3846addb1d6SDmitry Torokhov retval = mutex_lock_interruptible(&evdev->mutex);
3856addb1d6SDmitry Torokhov if (retval)
3866addb1d6SDmitry Torokhov return retval;
3876addb1d6SDmitry Torokhov
3886addb1d6SDmitry Torokhov if (!evdev->exist)
3896addb1d6SDmitry Torokhov retval = -ENODEV;
39006445014SOliver Neukum else if (!evdev->open++) {
3916addb1d6SDmitry Torokhov retval = input_open_device(&evdev->handle);
39206445014SOliver Neukum if (retval)
39306445014SOliver Neukum evdev->open--;
39406445014SOliver Neukum }
3956addb1d6SDmitry Torokhov
3966addb1d6SDmitry Torokhov mutex_unlock(&evdev->mutex);
3976addb1d6SDmitry Torokhov return retval;
3986addb1d6SDmitry Torokhov }
3996addb1d6SDmitry Torokhov
evdev_close_device(struct evdev * evdev)4006addb1d6SDmitry Torokhov static void evdev_close_device(struct evdev *evdev)
4016addb1d6SDmitry Torokhov {
4026addb1d6SDmitry Torokhov mutex_lock(&evdev->mutex);
4036addb1d6SDmitry Torokhov
4046addb1d6SDmitry Torokhov if (evdev->exist && !--evdev->open)
4056addb1d6SDmitry Torokhov input_close_device(&evdev->handle);
4066addb1d6SDmitry Torokhov
4076addb1d6SDmitry Torokhov mutex_unlock(&evdev->mutex);
4086addb1d6SDmitry Torokhov }
4096addb1d6SDmitry Torokhov
4106addb1d6SDmitry Torokhov /*
4116addb1d6SDmitry Torokhov * Wake up users waiting for IO so they can disconnect from
4126addb1d6SDmitry Torokhov * dead device.
4136addb1d6SDmitry Torokhov */
evdev_hangup(struct evdev * evdev)4146addb1d6SDmitry Torokhov static void evdev_hangup(struct evdev *evdev)
4156addb1d6SDmitry Torokhov {
4166addb1d6SDmitry Torokhov struct evdev_client *client;
4176addb1d6SDmitry Torokhov
4186addb1d6SDmitry Torokhov spin_lock(&evdev->client_lock);
4194ba8b8aeSKenny Levinsen list_for_each_entry(client, &evdev->client_list, node) {
4206addb1d6SDmitry Torokhov kill_fasync(&client->fasync, SIGIO, POLL_HUP);
4214ba8b8aeSKenny Levinsen wake_up_interruptible_poll(&client->wait, EPOLLHUP | EPOLLERR);
4224ba8b8aeSKenny Levinsen }
4236addb1d6SDmitry Torokhov spin_unlock(&evdev->client_lock);
4246addb1d6SDmitry Torokhov }
4256addb1d6SDmitry Torokhov
evdev_release(struct inode * inode,struct file * file)4261da177e4SLinus Torvalds static int evdev_release(struct inode *inode, struct file *file)
4271da177e4SLinus Torvalds {
428d0ffb9beSDmitry Torokhov struct evdev_client *client = file->private_data;
429d0ffb9beSDmitry Torokhov struct evdev *evdev = client->evdev;
43006a16293SDavid Herrmann unsigned int i;
4311da177e4SLinus Torvalds
4326addb1d6SDmitry Torokhov mutex_lock(&evdev->mutex);
43309264098SBrendan Shanks
43409264098SBrendan Shanks if (evdev->exist && !client->revoked)
43509264098SBrendan Shanks input_flush_device(&evdev->handle, file);
43609264098SBrendan Shanks
4376addb1d6SDmitry Torokhov evdev_ungrab(evdev, client);
4386addb1d6SDmitry Torokhov mutex_unlock(&evdev->mutex);
4391da177e4SLinus Torvalds
4406addb1d6SDmitry Torokhov evdev_detach_client(evdev, client);
44192eb77d0SDaniel Stone
44206a16293SDavid Herrmann for (i = 0; i < EV_CNT; ++i)
4436078091cSAndy Shevchenko bitmap_free(client->evmasks[i]);
44406a16293SDavid Herrmann
44567367fd2SPekka Enberg kvfree(client);
4461da177e4SLinus Torvalds
4476addb1d6SDmitry Torokhov evdev_close_device(evdev);
4481da177e4SLinus Torvalds
4491da177e4SLinus Torvalds return 0;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds
evdev_compute_buffer_size(struct input_dev * dev)452b58f7086SHenrik Rydberg static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
453b58f7086SHenrik Rydberg {
45463a6404dSHenrik Rydberg unsigned int n_events =
45563a6404dSHenrik Rydberg max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
45663a6404dSHenrik Rydberg EVDEV_MIN_BUFFER_SIZE);
45763a6404dSHenrik Rydberg
45863a6404dSHenrik Rydberg return roundup_pow_of_two(n_events);
459b58f7086SHenrik Rydberg }
460b58f7086SHenrik Rydberg
evdev_open(struct inode * inode,struct file * file)4611da177e4SLinus Torvalds static int evdev_open(struct inode *inode, struct file *file)
4621da177e4SLinus Torvalds {
4637f8d4cadSDmitry Torokhov struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
4647f8d4cadSDmitry Torokhov unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
4656addb1d6SDmitry Torokhov struct evdev_client *client;
466d542ed82SDmitry Torokhov int error;
4671da177e4SLinus Torvalds
4687f439bc2SMiles Chen client = kvzalloc(struct_size(client, buffer, bufsize), GFP_KERNEL);
4697f8d4cadSDmitry Torokhov if (!client)
4707f8d4cadSDmitry Torokhov return -ENOMEM;
4711da177e4SLinus Torvalds
4724ba8b8aeSKenny Levinsen init_waitqueue_head(&client->wait);
473b58f7086SHenrik Rydberg client->bufsize = bufsize;
4746addb1d6SDmitry Torokhov spin_lock_init(&client->buffer_lock);
475d0ffb9beSDmitry Torokhov client->evdev = evdev;
4766addb1d6SDmitry Torokhov evdev_attach_client(evdev, client);
4771da177e4SLinus Torvalds
4786addb1d6SDmitry Torokhov error = evdev_open_device(evdev);
4799657d75cSDmitry Torokhov if (error)
4809657d75cSDmitry Torokhov goto err_free_client;
4811da177e4SLinus Torvalds
482d0ffb9beSDmitry Torokhov file->private_data = client;
483c5bf68feSKirill Smelkov stream_open(inode, file);
4843d7bbd45SDmitry Torokhov
4851da177e4SLinus Torvalds return 0;
4869657d75cSDmitry Torokhov
4879657d75cSDmitry Torokhov err_free_client:
4886addb1d6SDmitry Torokhov evdev_detach_client(evdev, client);
48992788ac1SAndrew Morton kvfree(client);
4909657d75cSDmitry Torokhov return error;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds
evdev_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)4936addb1d6SDmitry Torokhov static ssize_t evdev_write(struct file *file, const char __user *buffer,
4946addb1d6SDmitry Torokhov size_t count, loff_t *ppos)
4951da177e4SLinus Torvalds {
496d0ffb9beSDmitry Torokhov struct evdev_client *client = file->private_data;
497d0ffb9beSDmitry Torokhov struct evdev *evdev = client->evdev;
4981da177e4SLinus Torvalds struct input_event event;
49902dfc496SHeiko Stübner int retval = 0;
5001da177e4SLinus Torvalds
501*6994d8b8SDmitry Torokhov /*
502*6994d8b8SDmitry Torokhov * Limit amount of data we inject into the input subsystem so that
503*6994d8b8SDmitry Torokhov * we do not hold evdev->mutex for too long. 4096 bytes corresponds
504*6994d8b8SDmitry Torokhov * to 170 input events.
505*6994d8b8SDmitry Torokhov */
506*6994d8b8SDmitry Torokhov count = min(count, 4096);
507*6994d8b8SDmitry Torokhov
5082872a9b5SDmitry Torokhov if (count != 0 && count < input_event_size())
509439581ecSPeter Korsgaard return -EINVAL;
510439581ecSPeter Korsgaard
5116addb1d6SDmitry Torokhov retval = mutex_lock_interruptible(&evdev->mutex);
5126addb1d6SDmitry Torokhov if (retval)
5136addb1d6SDmitry Torokhov return retval;
5146addb1d6SDmitry Torokhov
515c7dc6573SDavid Herrmann if (!evdev->exist || client->revoked) {
5166addb1d6SDmitry Torokhov retval = -ENODEV;
5176addb1d6SDmitry Torokhov goto out;
5186addb1d6SDmitry Torokhov }
51952658bb6SJuergen Kreileder
5202872a9b5SDmitry Torokhov while (retval + input_event_size() <= count) {
5212872a9b5SDmitry Torokhov
5222d56f3a3SPhilip Langdale if (input_event_from_user(buffer + retval, &event)) {
5236addb1d6SDmitry Torokhov retval = -EFAULT;
5246addb1d6SDmitry Torokhov goto out;
5256addb1d6SDmitry Torokhov }
526439581ecSPeter Korsgaard retval += input_event_size();
5276addb1d6SDmitry Torokhov
5286addb1d6SDmitry Torokhov input_inject_event(&evdev->handle,
5296addb1d6SDmitry Torokhov event.type, event.code, event.value);
53036d2582fSDmitry Torokhov cond_resched();
5312872a9b5SDmitry Torokhov }
53252658bb6SJuergen Kreileder
5336addb1d6SDmitry Torokhov out:
5346addb1d6SDmitry Torokhov mutex_unlock(&evdev->mutex);
53552658bb6SJuergen Kreileder return retval;
53652658bb6SJuergen Kreileder }
53752658bb6SJuergen Kreileder
evdev_fetch_next_event(struct evdev_client * client,struct input_event * event)5386addb1d6SDmitry Torokhov static int evdev_fetch_next_event(struct evdev_client *client,
5396addb1d6SDmitry Torokhov struct input_event *event)
5406addb1d6SDmitry Torokhov {
5416addb1d6SDmitry Torokhov int have_event;
5426addb1d6SDmitry Torokhov
5436addb1d6SDmitry Torokhov spin_lock_irq(&client->buffer_lock);
5446addb1d6SDmitry Torokhov
545566cf5b6SDima Zavin have_event = client->packet_head != client->tail;
5466addb1d6SDmitry Torokhov if (have_event) {
5476addb1d6SDmitry Torokhov *event = client->buffer[client->tail++];
548b58f7086SHenrik Rydberg client->tail &= client->bufsize - 1;
5496addb1d6SDmitry Torokhov }
5506addb1d6SDmitry Torokhov
5516addb1d6SDmitry Torokhov spin_unlock_irq(&client->buffer_lock);
5526addb1d6SDmitry Torokhov
5536addb1d6SDmitry Torokhov return have_event;
5546addb1d6SDmitry Torokhov }
5556addb1d6SDmitry Torokhov
evdev_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)5566addb1d6SDmitry Torokhov static ssize_t evdev_read(struct file *file, char __user *buffer,
5576addb1d6SDmitry Torokhov size_t count, loff_t *ppos)
5581da177e4SLinus Torvalds {
559d0ffb9beSDmitry Torokhov struct evdev_client *client = file->private_data;
560d0ffb9beSDmitry Torokhov struct evdev *evdev = client->evdev;
5616addb1d6SDmitry Torokhov struct input_event event;
5622872a9b5SDmitry Torokhov size_t read = 0;
5632872a9b5SDmitry Torokhov int error;
5641da177e4SLinus Torvalds
5652872a9b5SDmitry Torokhov if (count != 0 && count < input_event_size())
5661da177e4SLinus Torvalds return -EINVAL;
5671da177e4SLinus Torvalds
5682872a9b5SDmitry Torokhov for (;;) {
569c7dc6573SDavid Herrmann if (!evdev->exist || client->revoked)
5701da177e4SLinus Torvalds return -ENODEV;
5711da177e4SLinus Torvalds
5722872a9b5SDmitry Torokhov if (client->packet_head == client->tail &&
5732872a9b5SDmitry Torokhov (file->f_flags & O_NONBLOCK))
574e90f869cSDima Zavin return -EAGAIN;
575e90f869cSDima Zavin
5762872a9b5SDmitry Torokhov /*
5772872a9b5SDmitry Torokhov * count == 0 is special - no IO is done but we check
5782872a9b5SDmitry Torokhov * for error conditions (see above).
5792872a9b5SDmitry Torokhov */
5802872a9b5SDmitry Torokhov if (count == 0)
5812872a9b5SDmitry Torokhov break;
5822872a9b5SDmitry Torokhov
5832872a9b5SDmitry Torokhov while (read + input_event_size() <= count &&
5842872a9b5SDmitry Torokhov evdev_fetch_next_event(client, &event)) {
5852872a9b5SDmitry Torokhov
5862872a9b5SDmitry Torokhov if (input_event_to_user(buffer + read, &event))
5872872a9b5SDmitry Torokhov return -EFAULT;
5882872a9b5SDmitry Torokhov
5892872a9b5SDmitry Torokhov read += input_event_size();
5902872a9b5SDmitry Torokhov }
5912872a9b5SDmitry Torokhov
5922872a9b5SDmitry Torokhov if (read)
5932872a9b5SDmitry Torokhov break;
5942872a9b5SDmitry Torokhov
5952872a9b5SDmitry Torokhov if (!(file->f_flags & O_NONBLOCK)) {
5964ba8b8aeSKenny Levinsen error = wait_event_interruptible(client->wait,
5972872a9b5SDmitry Torokhov client->packet_head != client->tail ||
598c7dc6573SDavid Herrmann !evdev->exist || client->revoked);
5992872a9b5SDmitry Torokhov if (error)
6002872a9b5SDmitry Torokhov return error;
6012872a9b5SDmitry Torokhov }
6022872a9b5SDmitry Torokhov }
6032872a9b5SDmitry Torokhov
6042872a9b5SDmitry Torokhov return read;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds
6071da177e4SLinus Torvalds /* No kernel lock - fine */
evdev_poll(struct file * file,poll_table * wait)608afc9a42bSAl Viro static __poll_t evdev_poll(struct file *file, poll_table *wait)
6091da177e4SLinus Torvalds {
610d0ffb9beSDmitry Torokhov struct evdev_client *client = file->private_data;
611d0ffb9beSDmitry Torokhov struct evdev *evdev = client->evdev;
612afc9a42bSAl Viro __poll_t mask;
6131e0afb28SDmitry Torokhov
6144ba8b8aeSKenny Levinsen poll_wait(file, &client->wait, wait);
615c18fb139SDmitry Torokhov
616c7dc6573SDavid Herrmann if (evdev->exist && !client->revoked)
617a9a08845SLinus Torvalds mask = EPOLLOUT | EPOLLWRNORM;
618c7dc6573SDavid Herrmann else
619a9a08845SLinus Torvalds mask = EPOLLHUP | EPOLLERR;
620c7dc6573SDavid Herrmann
621cdda911cSJeff Brown if (client->packet_head != client->tail)
622a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM;
623c18fb139SDmitry Torokhov
624c18fb139SDmitry Torokhov return mask;
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds
6273a51f7c4SDmitry Torokhov #ifdef CONFIG_COMPAT
6283a51f7c4SDmitry Torokhov
6293a51f7c4SDmitry Torokhov #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
6307b19ada2SJiri Slaby #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
6313a51f7c4SDmitry Torokhov
6323a51f7c4SDmitry Torokhov #ifdef __BIG_ENDIAN
bits_to_user(unsigned long * bits,unsigned int maxbit,unsigned int maxlen,void __user * p,int compat)6333a51f7c4SDmitry Torokhov static int bits_to_user(unsigned long *bits, unsigned int maxbit,
6343a51f7c4SDmitry Torokhov unsigned int maxlen, void __user *p, int compat)
6353a51f7c4SDmitry Torokhov {
6363a51f7c4SDmitry Torokhov int len, i;
6373a51f7c4SDmitry Torokhov
6383a51f7c4SDmitry Torokhov if (compat) {
6397b19ada2SJiri Slaby len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
640bf61f8d3SKenichi Nagai if (len > maxlen)
6413a51f7c4SDmitry Torokhov len = maxlen;
6423a51f7c4SDmitry Torokhov
6433a51f7c4SDmitry Torokhov for (i = 0; i < len / sizeof(compat_long_t); i++)
6443a51f7c4SDmitry Torokhov if (copy_to_user((compat_long_t __user *) p + i,
6453a51f7c4SDmitry Torokhov (compat_long_t *) bits +
6463a51f7c4SDmitry Torokhov i + 1 - ((i % 2) << 1),
6473a51f7c4SDmitry Torokhov sizeof(compat_long_t)))
6483a51f7c4SDmitry Torokhov return -EFAULT;
6493a51f7c4SDmitry Torokhov } else {
6507b19ada2SJiri Slaby len = BITS_TO_LONGS(maxbit) * sizeof(long);
6513a51f7c4SDmitry Torokhov if (len > maxlen)
6523a51f7c4SDmitry Torokhov len = maxlen;
6533a51f7c4SDmitry Torokhov
6543a51f7c4SDmitry Torokhov if (copy_to_user(p, bits, len))
6553a51f7c4SDmitry Torokhov return -EFAULT;
6563a51f7c4SDmitry Torokhov }
6573a51f7c4SDmitry Torokhov
6583a51f7c4SDmitry Torokhov return len;
6593a51f7c4SDmitry Torokhov }
66006a16293SDavid Herrmann
bits_from_user(unsigned long * bits,unsigned int maxbit,unsigned int maxlen,const void __user * p,int compat)66106a16293SDavid Herrmann static int bits_from_user(unsigned long *bits, unsigned int maxbit,
66206a16293SDavid Herrmann unsigned int maxlen, const void __user *p, int compat)
66306a16293SDavid Herrmann {
66406a16293SDavid Herrmann int len, i;
66506a16293SDavid Herrmann
66606a16293SDavid Herrmann if (compat) {
66706a16293SDavid Herrmann if (maxlen % sizeof(compat_long_t))
66806a16293SDavid Herrmann return -EINVAL;
66906a16293SDavid Herrmann
67006a16293SDavid Herrmann len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
67106a16293SDavid Herrmann if (len > maxlen)
67206a16293SDavid Herrmann len = maxlen;
67306a16293SDavid Herrmann
67406a16293SDavid Herrmann for (i = 0; i < len / sizeof(compat_long_t); i++)
67506a16293SDavid Herrmann if (copy_from_user((compat_long_t *) bits +
67606a16293SDavid Herrmann i + 1 - ((i % 2) << 1),
67706a16293SDavid Herrmann (compat_long_t __user *) p + i,
67806a16293SDavid Herrmann sizeof(compat_long_t)))
67906a16293SDavid Herrmann return -EFAULT;
68006a16293SDavid Herrmann if (i % 2)
68106a16293SDavid Herrmann *((compat_long_t *) bits + i - 1) = 0;
68206a16293SDavid Herrmann
68306a16293SDavid Herrmann } else {
68406a16293SDavid Herrmann if (maxlen % sizeof(long))
68506a16293SDavid Herrmann return -EINVAL;
68606a16293SDavid Herrmann
68706a16293SDavid Herrmann len = BITS_TO_LONGS(maxbit) * sizeof(long);
68806a16293SDavid Herrmann if (len > maxlen)
68906a16293SDavid Herrmann len = maxlen;
69006a16293SDavid Herrmann
69106a16293SDavid Herrmann if (copy_from_user(bits, p, len))
69206a16293SDavid Herrmann return -EFAULT;
69306a16293SDavid Herrmann }
69406a16293SDavid Herrmann
69506a16293SDavid Herrmann return len;
69606a16293SDavid Herrmann }
69706a16293SDavid Herrmann
6983a51f7c4SDmitry Torokhov #else
69906a16293SDavid Herrmann
bits_to_user(unsigned long * bits,unsigned int maxbit,unsigned int maxlen,void __user * p,int compat)7003a51f7c4SDmitry Torokhov static int bits_to_user(unsigned long *bits, unsigned int maxbit,
7013a51f7c4SDmitry Torokhov unsigned int maxlen, void __user *p, int compat)
7023a51f7c4SDmitry Torokhov {
7033a51f7c4SDmitry Torokhov int len = compat ?
7047b19ada2SJiri Slaby BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
7057b19ada2SJiri Slaby BITS_TO_LONGS(maxbit) * sizeof(long);
7063a51f7c4SDmitry Torokhov
7073a51f7c4SDmitry Torokhov if (len > maxlen)
7083a51f7c4SDmitry Torokhov len = maxlen;
7093a51f7c4SDmitry Torokhov
7103a51f7c4SDmitry Torokhov return copy_to_user(p, bits, len) ? -EFAULT : len;
7113a51f7c4SDmitry Torokhov }
71206a16293SDavid Herrmann
bits_from_user(unsigned long * bits,unsigned int maxbit,unsigned int maxlen,const void __user * p,int compat)71306a16293SDavid Herrmann static int bits_from_user(unsigned long *bits, unsigned int maxbit,
71406a16293SDavid Herrmann unsigned int maxlen, const void __user *p, int compat)
71506a16293SDavid Herrmann {
71606a16293SDavid Herrmann size_t chunk_size = compat ? sizeof(compat_long_t) : sizeof(long);
71706a16293SDavid Herrmann int len;
71806a16293SDavid Herrmann
71906a16293SDavid Herrmann if (maxlen % chunk_size)
72006a16293SDavid Herrmann return -EINVAL;
72106a16293SDavid Herrmann
72206a16293SDavid Herrmann len = compat ? BITS_TO_LONGS_COMPAT(maxbit) : BITS_TO_LONGS(maxbit);
72306a16293SDavid Herrmann len *= chunk_size;
72406a16293SDavid Herrmann if (len > maxlen)
72506a16293SDavid Herrmann len = maxlen;
72606a16293SDavid Herrmann
72706a16293SDavid Herrmann return copy_from_user(bits, p, len) ? -EFAULT : len;
72806a16293SDavid Herrmann }
72906a16293SDavid Herrmann
7303a51f7c4SDmitry Torokhov #endif /* __BIG_ENDIAN */
7313a51f7c4SDmitry Torokhov
7323a51f7c4SDmitry Torokhov #else
7333a51f7c4SDmitry Torokhov
bits_to_user(unsigned long * bits,unsigned int maxbit,unsigned int maxlen,void __user * p,int compat)7343a51f7c4SDmitry Torokhov static int bits_to_user(unsigned long *bits, unsigned int maxbit,
7353a51f7c4SDmitry Torokhov unsigned int maxlen, void __user *p, int compat)
7363a51f7c4SDmitry Torokhov {
7377b19ada2SJiri Slaby int len = BITS_TO_LONGS(maxbit) * sizeof(long);
7383a51f7c4SDmitry Torokhov
7393a51f7c4SDmitry Torokhov if (len > maxlen)
7403a51f7c4SDmitry Torokhov len = maxlen;
7413a51f7c4SDmitry Torokhov
7423a51f7c4SDmitry Torokhov return copy_to_user(p, bits, len) ? -EFAULT : len;
7433a51f7c4SDmitry Torokhov }
7443a51f7c4SDmitry Torokhov
bits_from_user(unsigned long * bits,unsigned int maxbit,unsigned int maxlen,const void __user * p,int compat)74506a16293SDavid Herrmann static int bits_from_user(unsigned long *bits, unsigned int maxbit,
74606a16293SDavid Herrmann unsigned int maxlen, const void __user *p, int compat)
74706a16293SDavid Herrmann {
74806a16293SDavid Herrmann int len;
74906a16293SDavid Herrmann
75006a16293SDavid Herrmann if (maxlen % sizeof(long))
75106a16293SDavid Herrmann return -EINVAL;
75206a16293SDavid Herrmann
75306a16293SDavid Herrmann len = BITS_TO_LONGS(maxbit) * sizeof(long);
75406a16293SDavid Herrmann if (len > maxlen)
75506a16293SDavid Herrmann len = maxlen;
75606a16293SDavid Herrmann
75706a16293SDavid Herrmann return copy_from_user(bits, p, len) ? -EFAULT : len;
75806a16293SDavid Herrmann }
75906a16293SDavid Herrmann
7603a51f7c4SDmitry Torokhov #endif /* CONFIG_COMPAT */
7613a51f7c4SDmitry Torokhov
str_to_user(const char * str,unsigned int maxlen,void __user * p)7623a51f7c4SDmitry Torokhov static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
7633a51f7c4SDmitry Torokhov {
7643a51f7c4SDmitry Torokhov int len;
7653a51f7c4SDmitry Torokhov
7663a51f7c4SDmitry Torokhov if (!str)
7673a51f7c4SDmitry Torokhov return -ENOENT;
7683a51f7c4SDmitry Torokhov
7693a51f7c4SDmitry Torokhov len = strlen(str) + 1;
7703a51f7c4SDmitry Torokhov if (len > maxlen)
7713a51f7c4SDmitry Torokhov len = maxlen;
7723a51f7c4SDmitry Torokhov
7733a51f7c4SDmitry Torokhov return copy_to_user(p, str, len) ? -EFAULT : len;
7743a51f7c4SDmitry Torokhov }
7753a51f7c4SDmitry Torokhov
handle_eviocgbit(struct input_dev * dev,unsigned int type,unsigned int size,void __user * p,int compat_mode)776448cd166SDmitry Torokhov static int handle_eviocgbit(struct input_dev *dev,
777448cd166SDmitry Torokhov unsigned int type, unsigned int size,
778448cd166SDmitry Torokhov void __user *p, int compat_mode)
7795402a734SLinus Torvalds {
7805402a734SLinus Torvalds unsigned long *bits;
7815402a734SLinus Torvalds int len;
7825402a734SLinus Torvalds
783448cd166SDmitry Torokhov switch (type) {
7845402a734SLinus Torvalds
7855402a734SLinus Torvalds case 0: bits = dev->evbit; len = EV_MAX; break;
7865402a734SLinus Torvalds case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
7875402a734SLinus Torvalds case EV_REL: bits = dev->relbit; len = REL_MAX; break;
7885402a734SLinus Torvalds case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
7895402a734SLinus Torvalds case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
7905402a734SLinus Torvalds case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
7915402a734SLinus Torvalds case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
7925402a734SLinus Torvalds case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
7935402a734SLinus Torvalds case EV_SW: bits = dev->swbit; len = SW_MAX; break;
7945402a734SLinus Torvalds default: return -EINVAL;
7955402a734SLinus Torvalds }
796f2afa771SDmitry Torokhov
797448cd166SDmitry Torokhov return bits_to_user(bits, len, size, p, compat_mode);
798f2afa771SDmitry Torokhov }
7995402a734SLinus Torvalds
evdev_handle_get_keycode(struct input_dev * dev,void __user * p)800ab4e0192SDmitry Torokhov static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
8018613e4c2SMauro Carvalho Chehab {
802ab4e0192SDmitry Torokhov struct input_keymap_entry ke = {
803ab4e0192SDmitry Torokhov .len = sizeof(unsigned int),
804ab4e0192SDmitry Torokhov .flags = 0,
805ab4e0192SDmitry Torokhov };
806ab4e0192SDmitry Torokhov int __user *ip = (int __user *)p;
8078613e4c2SMauro Carvalho Chehab int error;
8088613e4c2SMauro Carvalho Chehab
8098613e4c2SMauro Carvalho Chehab /* legacy case */
8108613e4c2SMauro Carvalho Chehab if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
8118613e4c2SMauro Carvalho Chehab return -EFAULT;
8128613e4c2SMauro Carvalho Chehab
8138613e4c2SMauro Carvalho Chehab error = input_get_keycode(dev, &ke);
8148613e4c2SMauro Carvalho Chehab if (error)
8158613e4c2SMauro Carvalho Chehab return error;
8168613e4c2SMauro Carvalho Chehab
8178613e4c2SMauro Carvalho Chehab if (put_user(ke.keycode, ip + 1))
8188613e4c2SMauro Carvalho Chehab return -EFAULT;
8198613e4c2SMauro Carvalho Chehab
820ab4e0192SDmitry Torokhov return 0;
821ab4e0192SDmitry Torokhov }
8228613e4c2SMauro Carvalho Chehab
evdev_handle_get_keycode_v2(struct input_dev * dev,void __user * p)823ab4e0192SDmitry Torokhov static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
824ab4e0192SDmitry Torokhov {
825ab4e0192SDmitry Torokhov struct input_keymap_entry ke;
826ab4e0192SDmitry Torokhov int error;
827ab4e0192SDmitry Torokhov
828ab4e0192SDmitry Torokhov if (copy_from_user(&ke, p, sizeof(ke)))
8298613e4c2SMauro Carvalho Chehab return -EFAULT;
8308613e4c2SMauro Carvalho Chehab
8318613e4c2SMauro Carvalho Chehab error = input_get_keycode(dev, &ke);
8328613e4c2SMauro Carvalho Chehab if (error)
8338613e4c2SMauro Carvalho Chehab return error;
8348613e4c2SMauro Carvalho Chehab
835ab4e0192SDmitry Torokhov if (copy_to_user(p, &ke, sizeof(ke)))
8368613e4c2SMauro Carvalho Chehab return -EFAULT;
837ab4e0192SDmitry Torokhov
8388613e4c2SMauro Carvalho Chehab return 0;
8398613e4c2SMauro Carvalho Chehab }
8408613e4c2SMauro Carvalho Chehab
evdev_handle_set_keycode(struct input_dev * dev,void __user * p)841ab4e0192SDmitry Torokhov static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
8428613e4c2SMauro Carvalho Chehab {
843ab4e0192SDmitry Torokhov struct input_keymap_entry ke = {
844ab4e0192SDmitry Torokhov .len = sizeof(unsigned int),
845ab4e0192SDmitry Torokhov .flags = 0,
846ab4e0192SDmitry Torokhov };
8478613e4c2SMauro Carvalho Chehab int __user *ip = (int __user *)p;
8488613e4c2SMauro Carvalho Chehab
8498613e4c2SMauro Carvalho Chehab if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
8508613e4c2SMauro Carvalho Chehab return -EFAULT;
8518613e4c2SMauro Carvalho Chehab
8528613e4c2SMauro Carvalho Chehab if (get_user(ke.keycode, ip + 1))
8538613e4c2SMauro Carvalho Chehab return -EFAULT;
8548613e4c2SMauro Carvalho Chehab
855ab4e0192SDmitry Torokhov return input_set_keycode(dev, &ke);
856ab4e0192SDmitry Torokhov }
8578613e4c2SMauro Carvalho Chehab
evdev_handle_set_keycode_v2(struct input_dev * dev,void __user * p)858ab4e0192SDmitry Torokhov static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
859ab4e0192SDmitry Torokhov {
860ab4e0192SDmitry Torokhov struct input_keymap_entry ke;
8618613e4c2SMauro Carvalho Chehab
862ab4e0192SDmitry Torokhov if (copy_from_user(&ke, p, sizeof(ke)))
8638613e4c2SMauro Carvalho Chehab return -EFAULT;
8648613e4c2SMauro Carvalho Chehab
8658613e4c2SMauro Carvalho Chehab if (ke.len > sizeof(ke.scancode))
8668613e4c2SMauro Carvalho Chehab return -EINVAL;
8678613e4c2SMauro Carvalho Chehab
8688613e4c2SMauro Carvalho Chehab return input_set_keycode(dev, &ke);
8698613e4c2SMauro Carvalho Chehab }
8708613e4c2SMauro Carvalho Chehab
87148318028SDavid Herrmann /*
87248318028SDavid Herrmann * If we transfer state to the user, we should flush all pending events
87348318028SDavid Herrmann * of the same type from the client's queue. Otherwise, they might end up
87448318028SDavid Herrmann * with duplicate events, which can screw up client's state tracking.
87548318028SDavid Herrmann * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
87648318028SDavid Herrmann * event so user-space will notice missing events.
87748318028SDavid Herrmann *
87848318028SDavid Herrmann * LOCKING:
87948318028SDavid Herrmann * We need to take event_lock before buffer_lock to avoid dead-locks. But we
88048318028SDavid Herrmann * need the even_lock only to guarantee consistent state. We can safely release
88148318028SDavid Herrmann * it while flushing the queue. This allows input-core to handle filters while
88248318028SDavid Herrmann * we flush the queue.
88348318028SDavid Herrmann */
evdev_handle_get_val(struct evdev_client * client,struct input_dev * dev,unsigned int type,unsigned long * bits,unsigned int maxbit,unsigned int maxlen,void __user * p,int compat)88448318028SDavid Herrmann static int evdev_handle_get_val(struct evdev_client *client,
88548318028SDavid Herrmann struct input_dev *dev, unsigned int type,
8867c4f5607SDmitry Torokhov unsigned long *bits, unsigned int maxbit,
8877c4f5607SDmitry Torokhov unsigned int maxlen, void __user *p,
8887c4f5607SDmitry Torokhov int compat)
88948318028SDavid Herrmann {
89048318028SDavid Herrmann int ret;
89148318028SDavid Herrmann unsigned long *mem;
89248318028SDavid Herrmann
8936078091cSAndy Shevchenko mem = bitmap_alloc(maxbit, GFP_KERNEL);
89448318028SDavid Herrmann if (!mem)
89548318028SDavid Herrmann return -ENOMEM;
89648318028SDavid Herrmann
89748318028SDavid Herrmann spin_lock_irq(&dev->event_lock);
89848318028SDavid Herrmann spin_lock(&client->buffer_lock);
89948318028SDavid Herrmann
9006078091cSAndy Shevchenko bitmap_copy(mem, bits, maxbit);
90148318028SDavid Herrmann
90248318028SDavid Herrmann spin_unlock(&dev->event_lock);
90348318028SDavid Herrmann
90448318028SDavid Herrmann __evdev_flush_queue(client, type);
90548318028SDavid Herrmann
90648318028SDavid Herrmann spin_unlock_irq(&client->buffer_lock);
90748318028SDavid Herrmann
9087c4f5607SDmitry Torokhov ret = bits_to_user(mem, maxbit, maxlen, p, compat);
90948318028SDavid Herrmann if (ret < 0)
910b881d537SDmitry Torokhov evdev_queue_syn_dropped(client);
91148318028SDavid Herrmann
9126078091cSAndy Shevchenko bitmap_free(mem);
91348318028SDavid Herrmann
91448318028SDavid Herrmann return ret;
91548318028SDavid Herrmann }
91648318028SDavid Herrmann
evdev_handle_mt_request(struct input_dev * dev,unsigned int size,int __user * ip)9171cf0c6e6SHenrik Rydberg static int evdev_handle_mt_request(struct input_dev *dev,
9181cf0c6e6SHenrik Rydberg unsigned int size,
9191cf0c6e6SHenrik Rydberg int __user *ip)
9201cf0c6e6SHenrik Rydberg {
9218d18fba2SHenrik Rydberg const struct input_mt *mt = dev->mt;
9221cf0c6e6SHenrik Rydberg unsigned int code;
9231cf0c6e6SHenrik Rydberg int max_slots;
9241cf0c6e6SHenrik Rydberg int i;
9251cf0c6e6SHenrik Rydberg
9261cf0c6e6SHenrik Rydberg if (get_user(code, &ip[0]))
9271cf0c6e6SHenrik Rydberg return -EFAULT;
9288d18fba2SHenrik Rydberg if (!mt || !input_is_mt_value(code))
9291cf0c6e6SHenrik Rydberg return -EINVAL;
9301cf0c6e6SHenrik Rydberg
9311cf0c6e6SHenrik Rydberg max_slots = (size - sizeof(__u32)) / sizeof(__s32);
9328d18fba2SHenrik Rydberg for (i = 0; i < mt->num_slots && i < max_slots; i++) {
9338d18fba2SHenrik Rydberg int value = input_mt_get_value(&mt->slots[i], code);
9348d18fba2SHenrik Rydberg if (put_user(value, &ip[1 + i]))
9351cf0c6e6SHenrik Rydberg return -EFAULT;
9368d18fba2SHenrik Rydberg }
9371cf0c6e6SHenrik Rydberg
9381cf0c6e6SHenrik Rydberg return 0;
9391cf0c6e6SHenrik Rydberg }
9401cf0c6e6SHenrik Rydberg
evdev_revoke(struct evdev * evdev,struct evdev_client * client,struct file * file)941c7dc6573SDavid Herrmann static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
942c7dc6573SDavid Herrmann struct file *file)
943c7dc6573SDavid Herrmann {
944c7dc6573SDavid Herrmann client->revoked = true;
945c7dc6573SDavid Herrmann evdev_ungrab(evdev, client);
946c7dc6573SDavid Herrmann input_flush_device(&evdev->handle, file);
9474ba8b8aeSKenny Levinsen wake_up_interruptible_poll(&client->wait, EPOLLHUP | EPOLLERR);
948c7dc6573SDavid Herrmann
949c7dc6573SDavid Herrmann return 0;
950c7dc6573SDavid Herrmann }
951c7dc6573SDavid Herrmann
95206a16293SDavid Herrmann /* must be called with evdev-mutex held */
evdev_set_mask(struct evdev_client * client,unsigned int type,const void __user * codes,u32 codes_size,int compat)95306a16293SDavid Herrmann static int evdev_set_mask(struct evdev_client *client,
95406a16293SDavid Herrmann unsigned int type,
95506a16293SDavid Herrmann const void __user *codes,
95606a16293SDavid Herrmann u32 codes_size,
95706a16293SDavid Herrmann int compat)
95806a16293SDavid Herrmann {
95906a16293SDavid Herrmann unsigned long flags, *mask, *oldmask;
96006a16293SDavid Herrmann size_t cnt;
96106a16293SDavid Herrmann int error;
96206a16293SDavid Herrmann
96306a16293SDavid Herrmann /* we allow unknown types and 'codes_size > size' for forward-compat */
96406a16293SDavid Herrmann cnt = evdev_get_mask_cnt(type);
96506a16293SDavid Herrmann if (!cnt)
96606a16293SDavid Herrmann return 0;
96706a16293SDavid Herrmann
9686078091cSAndy Shevchenko mask = bitmap_zalloc(cnt, GFP_KERNEL);
96906a16293SDavid Herrmann if (!mask)
97006a16293SDavid Herrmann return -ENOMEM;
97106a16293SDavid Herrmann
97206a16293SDavid Herrmann error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);
97306a16293SDavid Herrmann if (error < 0) {
9746078091cSAndy Shevchenko bitmap_free(mask);
97506a16293SDavid Herrmann return error;
97606a16293SDavid Herrmann }
97706a16293SDavid Herrmann
97806a16293SDavid Herrmann spin_lock_irqsave(&client->buffer_lock, flags);
97906a16293SDavid Herrmann oldmask = client->evmasks[type];
98006a16293SDavid Herrmann client->evmasks[type] = mask;
98106a16293SDavid Herrmann spin_unlock_irqrestore(&client->buffer_lock, flags);
98206a16293SDavid Herrmann
9836078091cSAndy Shevchenko bitmap_free(oldmask);
98406a16293SDavid Herrmann
98506a16293SDavid Herrmann return 0;
98606a16293SDavid Herrmann }
98706a16293SDavid Herrmann
98806a16293SDavid Herrmann /* must be called with evdev-mutex held */
evdev_get_mask(struct evdev_client * client,unsigned int type,void __user * codes,u32 codes_size,int compat)98906a16293SDavid Herrmann static int evdev_get_mask(struct evdev_client *client,
99006a16293SDavid Herrmann unsigned int type,
99106a16293SDavid Herrmann void __user *codes,
99206a16293SDavid Herrmann u32 codes_size,
99306a16293SDavid Herrmann int compat)
99406a16293SDavid Herrmann {
99506a16293SDavid Herrmann unsigned long *mask;
99606a16293SDavid Herrmann size_t cnt, size, xfer_size;
99706a16293SDavid Herrmann int i;
99806a16293SDavid Herrmann int error;
99906a16293SDavid Herrmann
100006a16293SDavid Herrmann /* we allow unknown types and 'codes_size > size' for forward-compat */
100106a16293SDavid Herrmann cnt = evdev_get_mask_cnt(type);
100206a16293SDavid Herrmann size = sizeof(unsigned long) * BITS_TO_LONGS(cnt);
100306a16293SDavid Herrmann xfer_size = min_t(size_t, codes_size, size);
100406a16293SDavid Herrmann
100506a16293SDavid Herrmann if (cnt > 0) {
100606a16293SDavid Herrmann mask = client->evmasks[type];
100706a16293SDavid Herrmann if (mask) {
100806a16293SDavid Herrmann error = bits_to_user(mask, cnt - 1,
100906a16293SDavid Herrmann xfer_size, codes, compat);
101006a16293SDavid Herrmann if (error < 0)
101106a16293SDavid Herrmann return error;
101206a16293SDavid Herrmann } else {
101306a16293SDavid Herrmann /* fake mask with all bits set */
101406a16293SDavid Herrmann for (i = 0; i < xfer_size; i++)
101506a16293SDavid Herrmann if (put_user(0xffU, (u8 __user *)codes + i))
101606a16293SDavid Herrmann return -EFAULT;
101706a16293SDavid Herrmann }
101806a16293SDavid Herrmann }
101906a16293SDavid Herrmann
102006a16293SDavid Herrmann if (xfer_size < codes_size)
102106a16293SDavid Herrmann if (clear_user(codes + xfer_size, codes_size - xfer_size))
102206a16293SDavid Herrmann return -EFAULT;
102306a16293SDavid Herrmann
102406a16293SDavid Herrmann return 0;
102506a16293SDavid Herrmann }
102606a16293SDavid Herrmann
evdev_do_ioctl(struct file * file,unsigned int cmd,void __user * p,int compat_mode)10276addb1d6SDmitry Torokhov static long evdev_do_ioctl(struct file *file, unsigned int cmd,
10283a51f7c4SDmitry Torokhov void __user *p, int compat_mode)
10291da177e4SLinus Torvalds {
1030d0ffb9beSDmitry Torokhov struct evdev_client *client = file->private_data;
1031d0ffb9beSDmitry Torokhov struct evdev *evdev = client->evdev;
10321da177e4SLinus Torvalds struct input_dev *dev = evdev->handle.dev;
10331da177e4SLinus Torvalds struct input_absinfo abs;
103406a16293SDavid Herrmann struct input_mask mask;
1035509ca1a9SAnssi Hannula struct ff_effect effect;
10363a51f7c4SDmitry Torokhov int __user *ip = (int __user *)p;
103758b93995SDmitry Torokhov unsigned int i, t, u, v;
1038448cd166SDmitry Torokhov unsigned int size;
1039509ca1a9SAnssi Hannula int error;
10401da177e4SLinus Torvalds
1041448cd166SDmitry Torokhov /* First we check for fixed-length commands */
10421da177e4SLinus Torvalds switch (cmd) {
10431da177e4SLinus Torvalds
10441da177e4SLinus Torvalds case EVIOCGVERSION:
10451da177e4SLinus Torvalds return put_user(EV_VERSION, ip);
10461da177e4SLinus Torvalds
10471da177e4SLinus Torvalds case EVIOCGID:
10483a51f7c4SDmitry Torokhov if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
10493a51f7c4SDmitry Torokhov return -EFAULT;
105008791e5cSDmitry Torokhov return 0;
105108791e5cSDmitry Torokhov
105208791e5cSDmitry Torokhov case EVIOCGREP:
105308791e5cSDmitry Torokhov if (!test_bit(EV_REP, dev->evbit))
105408791e5cSDmitry Torokhov return -ENOSYS;
105508791e5cSDmitry Torokhov if (put_user(dev->rep[REP_DELAY], ip))
105608791e5cSDmitry Torokhov return -EFAULT;
105708791e5cSDmitry Torokhov if (put_user(dev->rep[REP_PERIOD], ip + 1))
105808791e5cSDmitry Torokhov return -EFAULT;
105908791e5cSDmitry Torokhov return 0;
106008791e5cSDmitry Torokhov
106108791e5cSDmitry Torokhov case EVIOCSREP:
106208791e5cSDmitry Torokhov if (!test_bit(EV_REP, dev->evbit))
106308791e5cSDmitry Torokhov return -ENOSYS;
106408791e5cSDmitry Torokhov if (get_user(u, ip))
106508791e5cSDmitry Torokhov return -EFAULT;
106608791e5cSDmitry Torokhov if (get_user(v, ip + 1))
106708791e5cSDmitry Torokhov return -EFAULT;
106808791e5cSDmitry Torokhov
10690e739d28SDmitry Torokhov input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
10700e739d28SDmitry Torokhov input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
10713a51f7c4SDmitry Torokhov
10723a51f7c4SDmitry Torokhov return 0;
10731da177e4SLinus Torvalds
10741da177e4SLinus Torvalds case EVIOCRMFF:
1075509ca1a9SAnssi Hannula return input_ff_erase(dev, (int)(unsigned long) p, file);
10761da177e4SLinus Torvalds
10771da177e4SLinus Torvalds case EVIOCGEFFECTS:
10786addb1d6SDmitry Torokhov i = test_bit(EV_FF, dev->evbit) ?
10796addb1d6SDmitry Torokhov dev->ff->max_effects : 0;
1080509ca1a9SAnssi Hannula if (put_user(i, ip))
10811da177e4SLinus Torvalds return -EFAULT;
10821da177e4SLinus Torvalds return 0;
10831da177e4SLinus Torvalds
10841da177e4SLinus Torvalds case EVIOCGRAB:
10856addb1d6SDmitry Torokhov if (p)
10866addb1d6SDmitry Torokhov return evdev_grab(evdev, client);
10876addb1d6SDmitry Torokhov else
10886addb1d6SDmitry Torokhov return evdev_ungrab(evdev, client);
1089ab4e0192SDmitry Torokhov
1090c7dc6573SDavid Herrmann case EVIOCREVOKE:
1091c7dc6573SDavid Herrmann if (p)
1092c7dc6573SDavid Herrmann return -EINVAL;
1093c7dc6573SDavid Herrmann else
1094c7dc6573SDavid Herrmann return evdev_revoke(evdev, client, file);
1095c7dc6573SDavid Herrmann
109606a16293SDavid Herrmann case EVIOCGMASK: {
109706a16293SDavid Herrmann void __user *codes_ptr;
109806a16293SDavid Herrmann
109906a16293SDavid Herrmann if (copy_from_user(&mask, p, sizeof(mask)))
110006a16293SDavid Herrmann return -EFAULT;
110106a16293SDavid Herrmann
110206a16293SDavid Herrmann codes_ptr = (void __user *)(unsigned long)mask.codes_ptr;
110306a16293SDavid Herrmann return evdev_get_mask(client,
110406a16293SDavid Herrmann mask.type, codes_ptr, mask.codes_size,
110506a16293SDavid Herrmann compat_mode);
110606a16293SDavid Herrmann }
110706a16293SDavid Herrmann
110806a16293SDavid Herrmann case EVIOCSMASK: {
110906a16293SDavid Herrmann const void __user *codes_ptr;
111006a16293SDavid Herrmann
111106a16293SDavid Herrmann if (copy_from_user(&mask, p, sizeof(mask)))
111206a16293SDavid Herrmann return -EFAULT;
111306a16293SDavid Herrmann
111406a16293SDavid Herrmann codes_ptr = (const void __user *)(unsigned long)mask.codes_ptr;
111506a16293SDavid Herrmann return evdev_set_mask(client,
111606a16293SDavid Herrmann mask.type, codes_ptr, mask.codes_size,
111706a16293SDavid Herrmann compat_mode);
111806a16293SDavid Herrmann }
111906a16293SDavid Herrmann
1120a80b83b7SJohn Stultz case EVIOCSCLOCKID:
1121a80b83b7SJohn Stultz if (copy_from_user(&i, p, sizeof(unsigned int)))
1122a80b83b7SJohn Stultz return -EFAULT;
1123aac8bcf1SAniroop Mathur
1124aac8bcf1SAniroop Mathur return evdev_set_clk_type(client, i);
1125a80b83b7SJohn Stultz
1126ab4e0192SDmitry Torokhov case EVIOCGKEYCODE:
1127ab4e0192SDmitry Torokhov return evdev_handle_get_keycode(dev, p);
1128ab4e0192SDmitry Torokhov
1129ab4e0192SDmitry Torokhov case EVIOCSKEYCODE:
1130ab4e0192SDmitry Torokhov return evdev_handle_set_keycode(dev, p);
1131ab4e0192SDmitry Torokhov
1132ab4e0192SDmitry Torokhov case EVIOCGKEYCODE_V2:
1133ab4e0192SDmitry Torokhov return evdev_handle_get_keycode_v2(dev, p);
1134ab4e0192SDmitry Torokhov
1135ab4e0192SDmitry Torokhov case EVIOCSKEYCODE_V2:
1136ab4e0192SDmitry Torokhov return evdev_handle_set_keycode_v2(dev, p);
113752658bb6SJuergen Kreileder }
11383a51f7c4SDmitry Torokhov
1139448cd166SDmitry Torokhov size = _IOC_SIZE(cmd);
114041e979f8SVojtech Pavlik
1141448cd166SDmitry Torokhov /* Now check variable-length commands */
1142448cd166SDmitry Torokhov #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
1143448cd166SDmitry Torokhov switch (EVIOC_MASK_SIZE(cmd)) {
1144f2278f31SAdam Dawidowski
114585b77200SHenrik Rydberg case EVIOCGPROP(0):
114685b77200SHenrik Rydberg return bits_to_user(dev->propbit, INPUT_PROP_MAX,
114785b77200SHenrik Rydberg size, p, compat_mode);
114885b77200SHenrik Rydberg
11491cf0c6e6SHenrik Rydberg case EVIOCGMTSLOTS(0):
11501cf0c6e6SHenrik Rydberg return evdev_handle_mt_request(dev, size, ip);
11511cf0c6e6SHenrik Rydberg
1152448cd166SDmitry Torokhov case EVIOCGKEY(0):
115348318028SDavid Herrmann return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
115448318028SDavid Herrmann KEY_MAX, size, p, compat_mode);
1155448cd166SDmitry Torokhov
1156448cd166SDmitry Torokhov case EVIOCGLED(0):
115748318028SDavid Herrmann return evdev_handle_get_val(client, dev, EV_LED, dev->led,
115848318028SDavid Herrmann LED_MAX, size, p, compat_mode);
1159448cd166SDmitry Torokhov
1160448cd166SDmitry Torokhov case EVIOCGSND(0):
116148318028SDavid Herrmann return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
116248318028SDavid Herrmann SND_MAX, size, p, compat_mode);
1163448cd166SDmitry Torokhov
1164448cd166SDmitry Torokhov case EVIOCGSW(0):
116548318028SDavid Herrmann return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
116648318028SDavid Herrmann SW_MAX, size, p, compat_mode);
1167448cd166SDmitry Torokhov
1168448cd166SDmitry Torokhov case EVIOCGNAME(0):
1169448cd166SDmitry Torokhov return str_to_user(dev->name, size, p);
1170448cd166SDmitry Torokhov
1171448cd166SDmitry Torokhov case EVIOCGPHYS(0):
1172448cd166SDmitry Torokhov return str_to_user(dev->phys, size, p);
1173448cd166SDmitry Torokhov
1174448cd166SDmitry Torokhov case EVIOCGUNIQ(0):
1175448cd166SDmitry Torokhov return str_to_user(dev->uniq, size, p);
1176448cd166SDmitry Torokhov
1177448cd166SDmitry Torokhov case EVIOC_MASK_SIZE(EVIOCSFF):
1178448cd166SDmitry Torokhov if (input_ff_effect_from_user(p, size, &effect))
1179f2278f31SAdam Dawidowski return -EFAULT;
1180f2278f31SAdam Dawidowski
1181f2278f31SAdam Dawidowski error = input_ff_upload(dev, &effect, file);
1182fc7392aaSElias Vanderstuyft if (error)
1183fc7392aaSElias Vanderstuyft return error;
1184f2278f31SAdam Dawidowski
1185f2278f31SAdam Dawidowski if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
1186f2278f31SAdam Dawidowski return -EFAULT;
1187f2278f31SAdam Dawidowski
1188fc7392aaSElias Vanderstuyft return 0;
1189f2278f31SAdam Dawidowski }
1190f2278f31SAdam Dawidowski
1191448cd166SDmitry Torokhov /* Multi-number variable-length handlers */
1192448cd166SDmitry Torokhov if (_IOC_TYPE(cmd) != 'E')
1193448cd166SDmitry Torokhov return -EINVAL;
1194448cd166SDmitry Torokhov
1195448cd166SDmitry Torokhov if (_IOC_DIR(cmd) == _IOC_READ) {
1196448cd166SDmitry Torokhov
1197448cd166SDmitry Torokhov if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
1198448cd166SDmitry Torokhov return handle_eviocgbit(dev,
1199448cd166SDmitry Torokhov _IOC_NR(cmd) & EV_MAX, size,
1200448cd166SDmitry Torokhov p, compat_mode);
1201448cd166SDmitry Torokhov
1202448cd166SDmitry Torokhov if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
1203448cd166SDmitry Torokhov
12040a74a1dfSDaniel Mack if (!dev->absinfo)
12050a74a1dfSDaniel Mack return -EINVAL;
12060a74a1dfSDaniel Mack
1207448cd166SDmitry Torokhov t = _IOC_NR(cmd) & ABS_MAX;
1208448cd166SDmitry Torokhov abs = dev->absinfo[t];
1209448cd166SDmitry Torokhov
1210448cd166SDmitry Torokhov if (copy_to_user(p, &abs, min_t(size_t,
1211448cd166SDmitry Torokhov size, sizeof(struct input_absinfo))))
1212448cd166SDmitry Torokhov return -EFAULT;
1213448cd166SDmitry Torokhov
1214448cd166SDmitry Torokhov return 0;
1215448cd166SDmitry Torokhov }
1216448cd166SDmitry Torokhov }
1217448cd166SDmitry Torokhov
1218f9ce6eb5SDaniel Mack if (_IOC_DIR(cmd) == _IOC_WRITE) {
1219448cd166SDmitry Torokhov
122052658bb6SJuergen Kreileder if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
122152658bb6SJuergen Kreileder
12220a74a1dfSDaniel Mack if (!dev->absinfo)
12230a74a1dfSDaniel Mack return -EINVAL;
12240a74a1dfSDaniel Mack
1225ce305b6aSDmitry Torokhov t = _IOC_NR(cmd) & ABS_MAX;
122652658bb6SJuergen Kreileder
1227ec20a022STero Saarni if (copy_from_user(&abs, p, min_t(size_t,
1228448cd166SDmitry Torokhov size, sizeof(struct input_absinfo))))
122952658bb6SJuergen Kreileder return -EFAULT;
123052658bb6SJuergen Kreileder
1231448cd166SDmitry Torokhov if (size < sizeof(struct input_absinfo))
1232d31b2865SDaniel Mack abs.resolution = 0;
1233d31b2865SDaniel Mack
123440d007e7SHenrik Rydberg /* We can't change number of reserved MT slots */
123540d007e7SHenrik Rydberg if (t == ABS_MT_SLOT)
123640d007e7SHenrik Rydberg return -EINVAL;
123740d007e7SHenrik Rydberg
12386addb1d6SDmitry Torokhov /*
12396addb1d6SDmitry Torokhov * Take event lock to ensure that we are not
12406addb1d6SDmitry Torokhov * changing device parameters in the middle
12416addb1d6SDmitry Torokhov * of event.
12426addb1d6SDmitry Torokhov */
12436addb1d6SDmitry Torokhov spin_lock_irq(&dev->event_lock);
1244d31b2865SDaniel Mack dev->absinfo[t] = abs;
12456addb1d6SDmitry Torokhov spin_unlock_irq(&dev->event_lock);
12466addb1d6SDmitry Torokhov
124752658bb6SJuergen Kreileder return 0;
124852658bb6SJuergen Kreileder }
124952658bb6SJuergen Kreileder }
1250448cd166SDmitry Torokhov
125152658bb6SJuergen Kreileder return -EINVAL;
125252658bb6SJuergen Kreileder }
12533a51f7c4SDmitry Torokhov
evdev_ioctl_handler(struct file * file,unsigned int cmd,void __user * p,int compat_mode)12546addb1d6SDmitry Torokhov static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
12556addb1d6SDmitry Torokhov void __user *p, int compat_mode)
12566addb1d6SDmitry Torokhov {
12576addb1d6SDmitry Torokhov struct evdev_client *client = file->private_data;
12586addb1d6SDmitry Torokhov struct evdev *evdev = client->evdev;
12596addb1d6SDmitry Torokhov int retval;
12606addb1d6SDmitry Torokhov
12616addb1d6SDmitry Torokhov retval = mutex_lock_interruptible(&evdev->mutex);
12626addb1d6SDmitry Torokhov if (retval)
12636addb1d6SDmitry Torokhov return retval;
12646addb1d6SDmitry Torokhov
1265c7dc6573SDavid Herrmann if (!evdev->exist || client->revoked) {
12666addb1d6SDmitry Torokhov retval = -ENODEV;
12676addb1d6SDmitry Torokhov goto out;
12686addb1d6SDmitry Torokhov }
12696addb1d6SDmitry Torokhov
12706addb1d6SDmitry Torokhov retval = evdev_do_ioctl(file, cmd, p, compat_mode);
12716addb1d6SDmitry Torokhov
12726addb1d6SDmitry Torokhov out:
12736addb1d6SDmitry Torokhov mutex_unlock(&evdev->mutex);
12746addb1d6SDmitry Torokhov return retval;
12756addb1d6SDmitry Torokhov }
12766addb1d6SDmitry Torokhov
evdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)12773a51f7c4SDmitry Torokhov static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
12783a51f7c4SDmitry Torokhov {
12793a51f7c4SDmitry Torokhov return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
12803a51f7c4SDmitry Torokhov }
12813a51f7c4SDmitry Torokhov
12823a51f7c4SDmitry Torokhov #ifdef CONFIG_COMPAT
evdev_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)12836addb1d6SDmitry Torokhov static long evdev_ioctl_compat(struct file *file,
12846addb1d6SDmitry Torokhov unsigned int cmd, unsigned long arg)
12853a51f7c4SDmitry Torokhov {
12863a51f7c4SDmitry Torokhov return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
12873a51f7c4SDmitry Torokhov }
128852658bb6SJuergen Kreileder #endif
128952658bb6SJuergen Kreileder
129066e66118SDmitry Torokhov static const struct file_operations evdev_fops = {
12911da177e4SLinus Torvalds .owner = THIS_MODULE,
12921da177e4SLinus Torvalds .read = evdev_read,
12931da177e4SLinus Torvalds .write = evdev_write,
12941da177e4SLinus Torvalds .poll = evdev_poll,
12951da177e4SLinus Torvalds .open = evdev_open,
12961da177e4SLinus Torvalds .release = evdev_release,
129752658bb6SJuergen Kreileder .unlocked_ioctl = evdev_ioctl,
129852658bb6SJuergen Kreileder #ifdef CONFIG_COMPAT
129952658bb6SJuergen Kreileder .compat_ioctl = evdev_ioctl_compat,
130052658bb6SJuergen Kreileder #endif
13011da177e4SLinus Torvalds .fasync = evdev_fasync,
13026038f373SArnd Bergmann };
13031da177e4SLinus Torvalds
13041da177e4SLinus Torvalds /*
13056addb1d6SDmitry Torokhov * Mark device non-existent. This disables writes, ioctls and
13066addb1d6SDmitry Torokhov * prevents new users from opening the device. Already posted
13076addb1d6SDmitry Torokhov * blocking reads will stay, however new ones will fail.
13086addb1d6SDmitry Torokhov */
evdev_mark_dead(struct evdev * evdev)13096addb1d6SDmitry Torokhov static void evdev_mark_dead(struct evdev *evdev)
13106addb1d6SDmitry Torokhov {
13116addb1d6SDmitry Torokhov mutex_lock(&evdev->mutex);
13126addb1d6SDmitry Torokhov evdev->exist = false;
131320da92deSDmitry Torokhov mutex_unlock(&evdev->mutex);
13146addb1d6SDmitry Torokhov }
13156addb1d6SDmitry Torokhov
evdev_cleanup(struct evdev * evdev)13166addb1d6SDmitry Torokhov static void evdev_cleanup(struct evdev *evdev)
13176addb1d6SDmitry Torokhov {
13186addb1d6SDmitry Torokhov struct input_handle *handle = &evdev->handle;
13196addb1d6SDmitry Torokhov
13206addb1d6SDmitry Torokhov evdev_mark_dead(evdev);
13216addb1d6SDmitry Torokhov evdev_hangup(evdev);
13226addb1d6SDmitry Torokhov
13237f8d4cadSDmitry Torokhov /* evdev is marked dead so no one else accesses evdev->open */
13246addb1d6SDmitry Torokhov if (evdev->open) {
13256addb1d6SDmitry Torokhov input_flush_device(handle, NULL);
13266addb1d6SDmitry Torokhov input_close_device(handle);
13276addb1d6SDmitry Torokhov }
13286addb1d6SDmitry Torokhov }
13296addb1d6SDmitry Torokhov
13306addb1d6SDmitry Torokhov /*
13316addb1d6SDmitry Torokhov * Create new evdev device. Note that input core serializes calls
13326addb1d6SDmitry Torokhov * to connect and disconnect.
13337f8d4cadSDmitry Torokhov */
evdev_connect(struct input_handler * handler,struct input_dev * dev,const struct input_device_id * id)13346addb1d6SDmitry Torokhov static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
13355b2a0826SDmitry Torokhov const struct input_device_id *id)
133666e66118SDmitry Torokhov {
13371da177e4SLinus Torvalds struct evdev *evdev;
13381da177e4SLinus Torvalds int minor;
13391da177e4SLinus Torvalds int dev_no;
13407f8d4cadSDmitry Torokhov int error;
13415b2a0826SDmitry Torokhov
13421da177e4SLinus Torvalds minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
13437f8d4cadSDmitry Torokhov if (minor < 0) {
13447f8d4cadSDmitry Torokhov error = minor;
13457f8d4cadSDmitry Torokhov pr_err("failed to reserve new minor: %d\n", error);
13467f8d4cadSDmitry Torokhov return error;
13477f8d4cadSDmitry Torokhov }
13481da177e4SLinus Torvalds
13491da177e4SLinus Torvalds evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
13505b2a0826SDmitry Torokhov if (!evdev) {
13517f8d4cadSDmitry Torokhov error = -ENOMEM;
13527f8d4cadSDmitry Torokhov goto err_free_minor;
13537f8d4cadSDmitry Torokhov }
13547f8d4cadSDmitry Torokhov
13551da177e4SLinus Torvalds INIT_LIST_HEAD(&evdev->client_list);
1356d0ffb9beSDmitry Torokhov spin_lock_init(&evdev->client_lock);
13576addb1d6SDmitry Torokhov mutex_init(&evdev->mutex);
13586addb1d6SDmitry Torokhov evdev->exist = true;
135920da92deSDmitry Torokhov
13607f8d4cadSDmitry Torokhov dev_no = minor;
13617f8d4cadSDmitry Torokhov /* Normalize device number if it falls into legacy range */
13627f8d4cadSDmitry Torokhov if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
13637f8d4cadSDmitry Torokhov dev_no -= EVDEV_MINOR_BASE;
13647f8d4cadSDmitry Torokhov dev_set_name(&evdev->dev, "event%d", dev_no);
13657f8d4cadSDmitry Torokhov
13666addb1d6SDmitry Torokhov evdev->handle.dev = input_get_device(dev);
1367a7097ff8SDmitry Torokhov evdev->handle.name = dev_name(&evdev->dev);
13683d5cb60eSThadeu Lima de Souza Cascardo evdev->handle.handler = handler;
13691da177e4SLinus Torvalds evdev->handle.private = evdev;
13701da177e4SLinus Torvalds
13719657d75cSDmitry Torokhov evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
13727f8d4cadSDmitry Torokhov evdev->dev.class = &input_class;
13739657d75cSDmitry Torokhov evdev->dev.parent = &dev->dev;
13749657d75cSDmitry Torokhov evdev->dev.release = evdev_free;
13759657d75cSDmitry Torokhov device_initialize(&evdev->dev);
13769657d75cSDmitry Torokhov
13771da177e4SLinus Torvalds error = input_register_handle(&evdev->handle);
13786addb1d6SDmitry Torokhov if (error)
13795b2a0826SDmitry Torokhov goto err_free_evdev;
13809657d75cSDmitry Torokhov
13811da177e4SLinus Torvalds cdev_init(&evdev->cdev, &evdev_fops);
13827f8d4cadSDmitry Torokhov
13836addb1d6SDmitry Torokhov error = cdev_device_add(&evdev->cdev, &evdev->dev);
1384358a89caSLogan Gunthorpe if (error)
13856addb1d6SDmitry Torokhov goto err_cleanup_evdev;
13866addb1d6SDmitry Torokhov
13875b2a0826SDmitry Torokhov return 0;
13885b2a0826SDmitry Torokhov
13895b2a0826SDmitry Torokhov err_cleanup_evdev:
13906addb1d6SDmitry Torokhov evdev_cleanup(evdev);
13916addb1d6SDmitry Torokhov input_unregister_handle(&evdev->handle);
13926addb1d6SDmitry Torokhov err_free_evdev:
13935b2a0826SDmitry Torokhov put_device(&evdev->dev);
13949657d75cSDmitry Torokhov err_free_minor:
13957f8d4cadSDmitry Torokhov input_free_minor(minor);
13967f8d4cadSDmitry Torokhov return error;
13975b2a0826SDmitry Torokhov }
13981da177e4SLinus Torvalds
evdev_disconnect(struct input_handle * handle)13991da177e4SLinus Torvalds static void evdev_disconnect(struct input_handle *handle)
14001da177e4SLinus Torvalds {
14011da177e4SLinus Torvalds struct evdev *evdev = handle->private;
14021da177e4SLinus Torvalds
14031da177e4SLinus Torvalds cdev_device_del(&evdev->cdev, &evdev->dev);
1404358a89caSLogan Gunthorpe evdev_cleanup(evdev);
14056addb1d6SDmitry Torokhov input_free_minor(MINOR(evdev->dev.devt));
14067f8d4cadSDmitry Torokhov input_unregister_handle(handle);
14076addb1d6SDmitry Torokhov put_device(&evdev->dev);
14089657d75cSDmitry Torokhov }
14091da177e4SLinus Torvalds
14101da177e4SLinus Torvalds static const struct input_device_id evdev_ids[] = {
141166e66118SDmitry Torokhov { .driver_info = 1 }, /* Matches all devices */
14121da177e4SLinus Torvalds { }, /* Terminating zero entry */
14131da177e4SLinus Torvalds };
14141da177e4SLinus Torvalds
14151da177e4SLinus Torvalds MODULE_DEVICE_TABLE(input, evdev_ids);
14161da177e4SLinus Torvalds
14171da177e4SLinus Torvalds static struct input_handler evdev_handler = {
14181da177e4SLinus Torvalds .events = evdev_events,
1419a274ac15SHenrik Rydberg .connect = evdev_connect,
14201da177e4SLinus Torvalds .disconnect = evdev_disconnect,
14211da177e4SLinus Torvalds .legacy_minors = true,
14227f8d4cadSDmitry Torokhov .minor = EVDEV_MINOR_BASE,
14231da177e4SLinus Torvalds .name = "evdev",
14241da177e4SLinus Torvalds .id_table = evdev_ids,
14251da177e4SLinus Torvalds };
14261da177e4SLinus Torvalds
evdev_init(void)14271da177e4SLinus Torvalds static int __init evdev_init(void)
14281da177e4SLinus Torvalds {
14291da177e4SLinus Torvalds return input_register_handler(&evdev_handler);
14304263cf0fSDmitry Torokhov }
14311da177e4SLinus Torvalds
evdev_exit(void)14321da177e4SLinus Torvalds static void __exit evdev_exit(void)
14331da177e4SLinus Torvalds {
14341da177e4SLinus Torvalds input_unregister_handler(&evdev_handler);
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds
14371da177e4SLinus Torvalds module_init(evdev_init);
14381da177e4SLinus Torvalds module_exit(evdev_exit);
14391da177e4SLinus Torvalds
14401da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
14411da177e4SLinus Torvalds MODULE_DESCRIPTION("Input driver event char devices");
14421da177e4SLinus Torvalds MODULE_LICENSE("GPL");
14431da177e4SLinus Torvalds