blob: 118d0300f1fb72a1324006f544b790a6085affa2 [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>
21#include <linux/module.h>
22#include <linux/init.h>
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +010023#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/device.h>
Philip Langdale2d56f3a2008-10-16 22:31:42 -040026#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28struct evdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 int open;
30 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 struct input_handle handle;
32 wait_queue_head_t wait;
Arnd Bergmann2be85272010-03-04 15:50:28 +010033 struct evdev_client __rcu *grab;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040034 struct list_head client_list;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040035 spinlock_t client_lock; /* protects client_list */
36 struct mutex mutex;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040037 struct device dev;
Dmitry Torokhov20da92d2010-07-15 23:27:36 -070038 bool exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040041struct evdev_client {
Jeff Brown9fb0f142011-04-12 23:29:38 -070042 unsigned int head;
43 unsigned int tail;
Jeff Browncdda9112011-04-26 22:16:11 -070044 unsigned int packet_head; /* [future] position of the first element of next packet */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040045 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct fasync_struct *fasync;
47 struct evdev *evdev;
48 struct list_head node;
John Stultza80b83b2012-02-03 00:19:07 -080049 int clkid;
Jeff Brown9fb0f142011-04-12 23:29:38 -070050 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070051 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040055static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Henrik Rydberga274ac12012-08-29 20:48:02 +020057static void __pass_event(struct evdev_client *client,
58 const struct input_event *event)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040059{
Jeff Brown9fb0f142011-04-12 23:29:38 -070060 client->buffer[client->head++] = *event;
61 client->head &= client->bufsize - 1;
62
63 if (unlikely(client->head == client->tail)) {
64 /*
65 * This effectively "drops" all unconsumed events, leaving
66 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
67 */
68 client->tail = (client->head - 2) & (client->bufsize - 1);
69
70 client->buffer[client->tail].time = event->time;
71 client->buffer[client->tail].type = EV_SYN;
72 client->buffer[client->tail].code = SYN_DROPPED;
73 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -070074
75 client->packet_head = client->tail;
76 }
77
78 if (event->type == EV_SYN && event->code == SYN_REPORT) {
79 client->packet_head = client->head;
80 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -070081 }
Henrik Rydberga274ac12012-08-29 20:48:02 +020082}
83
84static void evdev_pass_values(struct evdev_client *client,
85 const struct input_value *vals, unsigned int count,
86 ktime_t mono, ktime_t real)
87{
88 struct evdev *evdev = client->evdev;
89 const struct input_value *v;
90 struct input_event event;
91 bool wakeup = false;
92
93 event.time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ?
94 mono : real);
95
96 /* Interrupts are disabled, just acquire the lock. */
97 spin_lock(&client->buffer_lock);
98
99 for (v = vals; v != vals + count; v++) {
100 event.type = v->type;
101 event.code = v->code;
102 event.value = v->value;
103 __pass_event(client, &event);
104 if (v->type == EV_SYN && v->code == SYN_REPORT)
105 wakeup = true;
106 }
Jeff Brown9fb0f142011-04-12 23:29:38 -0700107
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400108 spin_unlock(&client->buffer_lock);
Henrik Rydberga274ac12012-08-29 20:48:02 +0200109
110 if (wakeup)
111 wake_up_interruptible(&evdev->wait);
112}
113
114/*
115 * Pass incoming events to all connected clients.
116 */
117static void evdev_events(struct input_handle *handle,
118 const struct input_value *vals, unsigned int count)
119{
120 struct evdev *evdev = handle->private;
121 struct evdev_client *client;
122 ktime_t time_mono, time_real;
123
124 time_mono = ktime_get();
125 time_real = ktime_sub(time_mono, ktime_get_monotonic_offset());
126
127 rcu_read_lock();
128
129 client = rcu_dereference(evdev->grab);
130
131 if (client)
132 evdev_pass_values(client, vals, count, time_mono, time_real);
133 else
134 list_for_each_entry_rcu(client, &evdev->client_list, node)
135 evdev_pass_values(client, vals, count,
136 time_mono, time_real);
137
138 rcu_read_unlock();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400139}
140
141/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400142 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400143 */
144static void evdev_event(struct input_handle *handle,
145 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Henrik Rydberga274ac12012-08-29 20:48:02 +0200147 struct input_value vals[] = { { type, code, value } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Henrik Rydberga274ac12012-08-29 20:48:02 +0200149 evdev_events(handle, vals, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152static int evdev_fasync(int fd, struct file *file, int on)
153{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400154 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400155
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700156 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400159static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400161 struct evdev_client *client = file->private_data;
162 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400163 int retval;
164
165 retval = mutex_lock_interruptible(&evdev->mutex);
166 if (retval)
167 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400168
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400169 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400170 retval = -ENODEV;
171 else
172 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400173
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400174 mutex_unlock(&evdev->mutex);
175 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400178static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400180 struct evdev *evdev = container_of(dev, struct evdev, dev);
181
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400182 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kfree(evdev);
184}
185
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400186/*
187 * Grabs an event device (along with underlying input device).
188 * This function is called with evdev->mutex taken.
189 */
190static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
191{
192 int error;
193
194 if (evdev->grab)
195 return -EBUSY;
196
197 error = input_grab_device(&evdev->handle);
198 if (error)
199 return error;
200
201 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400202
203 return 0;
204}
205
206static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
207{
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700208 struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
209 lockdep_is_held(&evdev->mutex));
210
211 if (grab != client)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400212 return -EINVAL;
213
214 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400215 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400216 input_release_device(&evdev->handle);
217
218 return 0;
219}
220
221static void evdev_attach_client(struct evdev *evdev,
222 struct evdev_client *client)
223{
224 spin_lock(&evdev->client_lock);
225 list_add_tail_rcu(&client->node, &evdev->client_list);
226 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400227}
228
229static void evdev_detach_client(struct evdev *evdev,
230 struct evdev_client *client)
231{
232 spin_lock(&evdev->client_lock);
233 list_del_rcu(&client->node);
234 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400235 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400236}
237
238static int evdev_open_device(struct evdev *evdev)
239{
240 int retval;
241
242 retval = mutex_lock_interruptible(&evdev->mutex);
243 if (retval)
244 return retval;
245
246 if (!evdev->exist)
247 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400248 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400249 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400250 if (retval)
251 evdev->open--;
252 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400253
254 mutex_unlock(&evdev->mutex);
255 return retval;
256}
257
258static void evdev_close_device(struct evdev *evdev)
259{
260 mutex_lock(&evdev->mutex);
261
262 if (evdev->exist && !--evdev->open)
263 input_close_device(&evdev->handle);
264
265 mutex_unlock(&evdev->mutex);
266}
267
268/*
269 * Wake up users waiting for IO so they can disconnect from
270 * dead device.
271 */
272static void evdev_hangup(struct evdev *evdev)
273{
274 struct evdev_client *client;
275
276 spin_lock(&evdev->client_lock);
277 list_for_each_entry(client, &evdev->client_list, node)
278 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
279 spin_unlock(&evdev->client_lock);
280
281 wake_up_interruptible(&evdev->wait);
282}
283
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400284static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400286 struct evdev_client *client = file->private_data;
287 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400289 mutex_lock(&evdev->mutex);
Dmitry Torokhovdba42582012-05-02 00:13:36 -0700290 evdev_ungrab(evdev, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400291 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400293 evdev_detach_client(evdev, client);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400294 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400296 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400297 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
300}
301
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700302static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
303{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700304 unsigned int n_events =
305 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
306 EVDEV_MIN_BUFFER_SIZE);
307
308 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700309}
310
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400311static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400313 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400314 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700316 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400317 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400319 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -ENODEV;
321
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400322 error = mutex_lock_interruptible(&evdev_table_mutex);
323 if (error)
324 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400325 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400326 if (evdev)
327 get_device(&evdev->dev);
328 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400329
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400330 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400331 return -ENODEV;
332
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700333 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
334
335 client = kzalloc(sizeof(struct evdev_client) +
336 bufsize * sizeof(struct input_event),
337 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400338 if (!client) {
339 error = -ENOMEM;
340 goto err_put_evdev;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700343 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400344 spin_lock_init(&client->buffer_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400345 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400346 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400348 error = evdev_open_device(evdev);
349 if (error)
350 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400352 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800353 nonseekable_open(inode, file);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400356
357 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400358 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400359 kfree(client);
360 err_put_evdev:
361 put_device(&evdev->dev);
362 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400365static ssize_t evdev_write(struct file *file, const char __user *buffer,
366 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400368 struct evdev_client *client = file->private_data;
369 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800371 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700373 if (count != 0 && count < input_event_size())
Peter Korsgaard439581e2011-02-25 09:30:46 -0800374 return -EINVAL;
375
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400376 retval = mutex_lock_interruptible(&evdev->mutex);
377 if (retval)
378 return retval;
379
380 if (!evdev->exist) {
381 retval = -ENODEV;
382 goto out;
383 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500384
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700385 while (retval + input_event_size() <= count) {
386
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400387 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400388 retval = -EFAULT;
389 goto out;
390 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800391 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400392
393 input_inject_event(&evdev->handle,
394 event.type, event.code, event.value);
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700395 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500396
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400397 out:
398 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500399 return retval;
400}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500401
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400402static int evdev_fetch_next_event(struct evdev_client *client,
403 struct input_event *event)
404{
405 int have_event;
406
407 spin_lock_irq(&client->buffer_lock);
408
Dima Zavin566cf5b2011-12-30 15:16:44 -0800409 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400410 if (have_event) {
411 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700412 client->tail &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400413 }
414
415 spin_unlock_irq(&client->buffer_lock);
416
417 return have_event;
418}
419
420static ssize_t evdev_read(struct file *file, char __user *buffer,
421 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400423 struct evdev_client *client = file->private_data;
424 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400425 struct input_event event;
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700426 size_t read = 0;
427 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700429 if (count != 0 && count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -EINVAL;
431
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700432 for (;;) {
433 if (!evdev->exist)
434 return -ENODEV;
435
436 if (client->packet_head == client->tail &&
437 (file->f_flags & O_NONBLOCK))
438 return -EAGAIN;
439
440 /*
441 * count == 0 is special - no IO is done but we check
442 * for error conditions (see above).
443 */
444 if (count == 0)
445 break;
446
447 while (read + input_event_size() <= count &&
448 evdev_fetch_next_event(client, &event)) {
449
450 if (input_event_to_user(buffer + read, &event))
451 return -EFAULT;
452
453 read += input_event_size();
454 }
455
456 if (read)
457 break;
458
459 if (!(file->f_flags & O_NONBLOCK)) {
460 error = wait_event_interruptible(evdev->wait,
461 client->packet_head != client->tail ||
462 !evdev->exist);
463 if (error)
464 return error;
465 }
Dima Zavin509f87c2011-12-30 15:16:44 -0800466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dmitry Torokhov2872a9b2012-05-02 00:13:37 -0700468 return read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471/* No kernel lock - fine */
472static unsigned int evdev_poll(struct file *file, poll_table *wait)
473{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400474 struct evdev_client *client = file->private_data;
475 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700476 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400477
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400478 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700479
480 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700481 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700482 mask |= POLLIN | POLLRDNORM;
483
484 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500487#ifdef CONFIG_COMPAT
488
489#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700490#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500491
492#ifdef __BIG_ENDIAN
493static int bits_to_user(unsigned long *bits, unsigned int maxbit,
494 unsigned int maxlen, void __user *p, int compat)
495{
496 int len, i;
497
498 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700499 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400500 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500501 len = maxlen;
502
503 for (i = 0; i < len / sizeof(compat_long_t); i++)
504 if (copy_to_user((compat_long_t __user *) p + i,
505 (compat_long_t *) bits +
506 i + 1 - ((i % 2) << 1),
507 sizeof(compat_long_t)))
508 return -EFAULT;
509 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700510 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500511 if (len > maxlen)
512 len = maxlen;
513
514 if (copy_to_user(p, bits, len))
515 return -EFAULT;
516 }
517
518 return len;
519}
520#else
521static int bits_to_user(unsigned long *bits, unsigned int maxbit,
522 unsigned int maxlen, void __user *p, int compat)
523{
524 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700525 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
526 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500527
528 if (len > maxlen)
529 len = maxlen;
530
531 return copy_to_user(p, bits, len) ? -EFAULT : len;
532}
533#endif /* __BIG_ENDIAN */
534
535#else
536
537static int bits_to_user(unsigned long *bits, unsigned int maxbit,
538 unsigned int maxlen, void __user *p, int compat)
539{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700540 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500541
542 if (len > maxlen)
543 len = maxlen;
544
545 return copy_to_user(p, bits, len) ? -EFAULT : len;
546}
547
548#endif /* CONFIG_COMPAT */
549
550static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
551{
552 int len;
553
554 if (!str)
555 return -ENOENT;
556
557 len = strlen(str) + 1;
558 if (len > maxlen)
559 len = maxlen;
560
561 return copy_to_user(p, str, len) ? -EFAULT : len;
562}
563
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400564#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700565static int handle_eviocgbit(struct input_dev *dev,
566 unsigned int type, unsigned int size,
567 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400568{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400569 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400570 unsigned long *bits;
571 int len;
572
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700573 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400574
575 case 0: bits = dev->evbit; len = EV_MAX; break;
576 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
577 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
578 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
579 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
580 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
581 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
582 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
583 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
584 default: return -EINVAL;
585 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400586
587 /*
588 * Work around bugs in userspace programs that like to do
589 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
590 * should be in bytes, not in bits.
591 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700592 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400593 len = OLD_KEY_MAX;
594 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800595 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
596 "limiting output to %zu bytes. See "
597 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
598 OLD_KEY_MAX,
599 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400600 }
601
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700602 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400603}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400604#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400605
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800606static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
607{
608 struct input_keymap_entry ke = {
609 .len = sizeof(unsigned int),
610 .flags = 0,
611 };
612 int __user *ip = (int __user *)p;
613 int error;
614
615 /* legacy case */
616 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
617 return -EFAULT;
618
619 error = input_get_keycode(dev, &ke);
620 if (error)
621 return error;
622
623 if (put_user(ke.keycode, ip + 1))
624 return -EFAULT;
625
626 return 0;
627}
628
629static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700630{
631 struct input_keymap_entry ke;
632 int error;
633
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800634 if (copy_from_user(&ke, p, sizeof(ke)))
635 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700636
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800637 error = input_get_keycode(dev, &ke);
638 if (error)
639 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700640
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800641 if (copy_to_user(p, &ke, sizeof(ke)))
642 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700643
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700644 return 0;
645}
646
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800647static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
648{
649 struct input_keymap_entry ke = {
650 .len = sizeof(unsigned int),
651 .flags = 0,
652 };
653 int __user *ip = (int __user *)p;
654
655 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
656 return -EFAULT;
657
658 if (get_user(ke.keycode, ip + 1))
659 return -EFAULT;
660
661 return input_set_keycode(dev, &ke);
662}
663
664static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700665{
666 struct input_keymap_entry ke;
667
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800668 if (copy_from_user(&ke, p, sizeof(ke)))
669 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700670
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800671 if (ke.len > sizeof(ke.scancode))
672 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700673
674 return input_set_keycode(dev, &ke);
675}
676
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100677static int evdev_handle_mt_request(struct input_dev *dev,
678 unsigned int size,
679 int __user *ip)
680{
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200681 const struct input_mt *mt = dev->mt;
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100682 unsigned int code;
683 int max_slots;
684 int i;
685
686 if (get_user(code, &ip[0]))
687 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200688 if (!mt || !input_is_mt_value(code))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100689 return -EINVAL;
690
691 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200692 for (i = 0; i < mt->num_slots && i < max_slots; i++) {
693 int value = input_mt_get_value(&mt->slots[i], code);
694 if (put_user(value, &ip[1 + i]))
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100695 return -EFAULT;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200696 }
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100697
698 return 0;
699}
700
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400701static long evdev_do_ioctl(struct file *file, unsigned int cmd,
702 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400704 struct evdev_client *client = file->private_data;
705 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct input_dev *dev = evdev->handle.dev;
707 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400708 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500709 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800710 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700711 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400712 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700714 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 switch (cmd) {
716
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400717 case EVIOCGVERSION:
718 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400720 case EVIOCGID:
721 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
722 return -EFAULT;
723 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400724
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400725 case EVIOCGREP:
726 if (!test_bit(EV_REP, dev->evbit))
727 return -ENOSYS;
728 if (put_user(dev->rep[REP_DELAY], ip))
729 return -EFAULT;
730 if (put_user(dev->rep[REP_PERIOD], ip + 1))
731 return -EFAULT;
732 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400733
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400734 case EVIOCSREP:
735 if (!test_bit(EV_REP, dev->evbit))
736 return -ENOSYS;
737 if (get_user(u, ip))
738 return -EFAULT;
739 if (get_user(v, ip + 1))
740 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400741
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400742 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
743 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500744
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400747 case EVIOCRMFF:
748 return input_ff_erase(dev, (int)(unsigned long) p, file);
749
750 case EVIOCGEFFECTS:
751 i = test_bit(EV_FF, dev->evbit) ?
752 dev->ff->max_effects : 0;
753 if (put_user(i, ip))
754 return -EFAULT;
755 return 0;
756
757 case EVIOCGRAB:
758 if (p)
759 return evdev_grab(evdev, client);
760 else
761 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800762
John Stultza80b83b2012-02-03 00:19:07 -0800763 case EVIOCSCLOCKID:
764 if (copy_from_user(&i, p, sizeof(unsigned int)))
765 return -EFAULT;
766 if (i != CLOCK_MONOTONIC && i != CLOCK_REALTIME)
767 return -EINVAL;
768 client->clkid = i;
769 return 0;
770
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800771 case EVIOCGKEYCODE:
772 return evdev_handle_get_keycode(dev, p);
773
774 case EVIOCSKEYCODE:
775 return evdev_handle_set_keycode(dev, p);
776
777 case EVIOCGKEYCODE_V2:
778 return evdev_handle_get_keycode_v2(dev, p);
779
780 case EVIOCSKEYCODE_V2:
781 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700782 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400783
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700784 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400785
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700786 /* Now check variable-length commands */
787#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700788 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400789
Henrik Rydberg85b77202010-12-18 20:51:13 +0100790 case EVIOCGPROP(0):
791 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
792 size, p, compat_mode);
793
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100794 case EVIOCGMTSLOTS(0):
795 return evdev_handle_mt_request(dev, size, ip);
796
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700797 case EVIOCGKEY(0):
798 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400799
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700800 case EVIOCGLED(0):
801 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400802
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700803 case EVIOCGSND(0):
804 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400805
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700806 case EVIOCGSW(0):
807 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400808
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700809 case EVIOCGNAME(0):
810 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400811
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700812 case EVIOCGPHYS(0):
813 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400814
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700815 case EVIOCGUNIQ(0):
816 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400817
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700818 case EVIOC_MASK_SIZE(EVIOCSFF):
819 if (input_ff_effect_from_user(p, size, &effect))
820 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400821
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700822 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400823
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700824 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
825 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400826
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700827 return error;
828 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400829
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700830 /* Multi-number variable-length handlers */
831 if (_IOC_TYPE(cmd) != 'E')
832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700834 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700836 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
837 return handle_eviocgbit(dev,
838 _IOC_NR(cmd) & EV_MAX, size,
839 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700841 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400842
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700843 if (!dev->absinfo)
844 return -EINVAL;
845
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700846 t = _IOC_NR(cmd) & ABS_MAX;
847 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400848
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700849 if (copy_to_user(p, &abs, min_t(size_t,
850 size, sizeof(struct input_absinfo))))
851 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400852
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700853 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700856
Daniel Mackf9ce6eb52010-10-18 08:43:50 -0700857 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700858
859 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
860
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700861 if (!dev->absinfo)
862 return -EINVAL;
863
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700864 t = _IOC_NR(cmd) & ABS_MAX;
865
866 if (copy_from_user(&abs, p, min_t(size_t,
867 size, sizeof(struct input_absinfo))))
868 return -EFAULT;
869
870 if (size < sizeof(struct input_absinfo))
871 abs.resolution = 0;
872
873 /* We can't change number of reserved MT slots */
874 if (t == ABS_MT_SLOT)
875 return -EINVAL;
876
877 /*
878 * Take event lock to ensure that we are not
879 * changing device parameters in the middle
880 * of event.
881 */
882 spin_lock_irq(&dev->event_lock);
883 dev->absinfo[t] = abs;
884 spin_unlock_irq(&dev->event_lock);
885
886 return 0;
887 }
888 }
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -EINVAL;
891}
892
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400893static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
894 void __user *p, int compat_mode)
895{
896 struct evdev_client *client = file->private_data;
897 struct evdev *evdev = client->evdev;
898 int retval;
899
900 retval = mutex_lock_interruptible(&evdev->mutex);
901 if (retval)
902 return retval;
903
904 if (!evdev->exist) {
905 retval = -ENODEV;
906 goto out;
907 }
908
909 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
910
911 out:
912 mutex_unlock(&evdev->mutex);
913 return retval;
914}
915
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500916static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
917{
918 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
919}
920
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500921#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400922static long evdev_ioctl_compat(struct file *file,
923 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500924{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500925 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500926}
927#endif
928
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400929static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400930 .owner = THIS_MODULE,
931 .read = evdev_read,
932 .write = evdev_write,
933 .poll = evdev_poll,
934 .open = evdev_open,
935 .release = evdev_release,
936 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500937#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400938 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500939#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400940 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200941 .flush = evdev_flush,
942 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943};
944
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400945static int evdev_install_chrdev(struct evdev *evdev)
946{
947 /*
948 * No need to do any locking here as calls to connect and
949 * disconnect are serialized by the input core
950 */
951 evdev_table[evdev->minor] = evdev;
952 return 0;
953}
954
955static void evdev_remove_chrdev(struct evdev *evdev)
956{
957 /*
958 * Lock evdev table to prevent race with evdev_open()
959 */
960 mutex_lock(&evdev_table_mutex);
961 evdev_table[evdev->minor] = NULL;
962 mutex_unlock(&evdev_table_mutex);
963}
964
965/*
966 * Mark device non-existent. This disables writes, ioctls and
967 * prevents new users from opening the device. Already posted
968 * blocking reads will stay, however new ones will fail.
969 */
970static void evdev_mark_dead(struct evdev *evdev)
971{
972 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700973 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400974 mutex_unlock(&evdev->mutex);
975}
976
977static void evdev_cleanup(struct evdev *evdev)
978{
979 struct input_handle *handle = &evdev->handle;
980
981 evdev_mark_dead(evdev);
982 evdev_hangup(evdev);
983 evdev_remove_chrdev(evdev);
984
985 /* evdev is marked dead so no one else accesses evdev->open */
986 if (evdev->open) {
987 input_flush_device(handle, NULL);
988 input_close_device(handle);
989 }
990}
991
992/*
993 * Create new evdev device. Note that input core serializes calls
994 * to connect and disconnect so we don't need to lock evdev_table here.
995 */
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400996static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
997 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct evdev *evdev;
1000 int minor;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001001 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001003 for (minor = 0; minor < EVDEV_MINORS; minor++)
1004 if (!evdev_table[minor])
1005 break;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -08001008 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001009 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001012 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
1013 if (!evdev)
1014 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001016 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001017 spin_lock_init(&evdev->client_lock);
1018 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 init_waitqueue_head(&evdev->wait);
1020
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001021 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001022 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001024
Dmitry Torokhova7097ff2008-04-01 00:22:53 -04001025 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001026 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 evdev->handle.handler = handler;
1028 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001029
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001030 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001031 evdev->dev.class = &input_class;
1032 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001033 evdev->dev.release = evdev_free;
1034 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001036 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001037 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001038 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001040 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001041 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001042 goto err_unregister_handle;
1043
1044 error = device_add(&evdev->dev);
1045 if (error)
1046 goto err_cleanup_evdev;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001047
1048 return 0;
1049
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001050 err_cleanup_evdev:
1051 evdev_cleanup(evdev);
1052 err_unregister_handle:
1053 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001054 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001055 put_device(&evdev->dev);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001056 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059static void evdev_disconnect(struct input_handle *handle)
1060{
1061 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001063 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001064 evdev_cleanup(evdev);
1065 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001066 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001069static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 { .driver_info = 1 }, /* Matches all devices */
1071 { }, /* Terminating zero entry */
1072};
1073
1074MODULE_DEVICE_TABLE(input, evdev_ids);
1075
1076static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001077 .event = evdev_event,
Henrik Rydberga274ac12012-08-29 20:48:02 +02001078 .events = evdev_events,
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001079 .connect = evdev_connect,
1080 .disconnect = evdev_disconnect,
1081 .fops = &evdev_fops,
1082 .minor = EVDEV_MINOR_BASE,
1083 .name = "evdev",
1084 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085};
1086
1087static int __init evdev_init(void)
1088{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001089 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092static void __exit evdev_exit(void)
1093{
1094 input_unregister_handler(&evdev_handler);
1095}
1096
1097module_init(evdev_init);
1098module_exit(evdev_exit);
1099
1100MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1101MODULE_DESCRIPTION("Input driver event char devices");
1102MODULE_LICENSE("GPL");