blob: 48baf6f9cb591a863ea7619f16f6cbff9aa6d73c [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>
Arve Hjønnevåg783820b2008-10-17 15:20:55 -070026#include <linux/wakelock.h>
Philip Langdale2d56f3a2008-10-16 22:31:42 -040027#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29struct evdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 int open;
31 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 struct input_handle handle;
33 wait_queue_head_t wait;
Arnd Bergmann2be85272010-03-04 15:50:28 +010034 struct evdev_client __rcu *grab;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040035 struct list_head client_list;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040036 spinlock_t client_lock; /* protects client_list */
37 struct mutex mutex;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040038 struct device dev;
Dmitry Torokhov20da92d2010-07-15 23:27:36 -070039 bool exist;
Sasha Levitskiy2d4e6f652013-06-28 11:06:31 -070040 int hw_ts_sec;
41 int hw_ts_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040044struct evdev_client {
Jeff Brown9fb0f142011-04-12 23:29:38 -070045 unsigned int head;
46 unsigned int tail;
Jeff Browncdda9112011-04-26 22:16:11 -070047 unsigned int packet_head; /* [future] position of the first element of next packet */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040048 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Arve Hjønnevåg783820b2008-10-17 15:20:55 -070049 struct wake_lock wake_lock;
50 bool use_wake_lock;
51 char name[28];
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct fasync_struct *fasync;
53 struct evdev *evdev;
54 struct list_head node;
John Stultza80b83b2012-02-03 00:19:07 -080055 int clkid;
Jeff Brown9fb0f142011-04-12 23:29:38 -070056 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070057 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
60static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040061static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040063static void evdev_pass_event(struct evdev_client *client,
John Stultza80b83b2012-02-03 00:19:07 -080064 struct input_event *event,
65 ktime_t mono, ktime_t real)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040066{
John Stultza80b83b2012-02-03 00:19:07 -080067 event->time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ?
68 mono : real);
69
Jeff Brown9fb0f142011-04-12 23:29:38 -070070 /* Interrupts are disabled, just acquire the lock. */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040071 spin_lock(&client->buffer_lock);
Jeff Brown9fb0f142011-04-12 23:29:38 -070072
73 client->buffer[client->head++] = *event;
74 client->head &= client->bufsize - 1;
75
76 if (unlikely(client->head == client->tail)) {
77 /*
78 * This effectively "drops" all unconsumed events, leaving
79 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
80 */
81 client->tail = (client->head - 2) & (client->bufsize - 1);
82
83 client->buffer[client->tail].time = event->time;
84 client->buffer[client->tail].type = EV_SYN;
85 client->buffer[client->tail].code = SYN_DROPPED;
86 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -070087
88 client->packet_head = client->tail;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -070089 if (client->use_wake_lock)
90 wake_unlock(&client->wake_lock);
Jeff Browncdda9112011-04-26 22:16:11 -070091 }
92
93 if (event->type == EV_SYN && event->code == SYN_REPORT) {
94 client->packet_head = client->head;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -070095 if (client->use_wake_lock)
96 wake_lock(&client->wake_lock);
Jeff Browncdda9112011-04-26 22:16:11 -070097 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -070098 }
99
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400100 spin_unlock(&client->buffer_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400101}
102
103/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400104 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400105 */
106static void evdev_event(struct input_handle *handle,
107 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400110 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400111 struct input_event event;
John Stultza80b83b2012-02-03 00:19:07 -0800112 ktime_t time_mono, time_real;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Sasha Levitskiy2d4e6f652013-06-28 11:06:31 -0700114 if (type == EV_SYN && code == SYN_TIME_SEC) {
115 evdev->hw_ts_sec = value;
116 return;
117 }
118 if (type == EV_SYN && code == SYN_TIME_NSEC) {
119 evdev->hw_ts_nsec = value;
120 return;
121 }
122
123 if (evdev->hw_ts_sec != -1 && evdev->hw_ts_nsec != -1)
124 time_mono = ktime_set(evdev->hw_ts_sec, evdev->hw_ts_nsec);
125 else
126 time_mono = ktime_get();
127
John Stultza80b83b2012-02-03 00:19:07 -0800128 time_real = ktime_sub(time_mono, ktime_get_monotonic_offset());
129
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400130 event.type = type;
131 event.code = code;
132 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400134 rcu_read_lock();
135
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400136 client = rcu_dereference(evdev->grab);
John Stultza80b83b2012-02-03 00:19:07 -0800137
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400138 if (client)
John Stultza80b83b2012-02-03 00:19:07 -0800139 evdev_pass_event(client, &event, time_mono, time_real);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400140 else
141 list_for_each_entry_rcu(client, &evdev->client_list, node)
John Stultza80b83b2012-02-03 00:19:07 -0800142 evdev_pass_event(client, &event, time_mono, time_real);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400144 rcu_read_unlock();
145
Sasha Levitskiy2d4e6f652013-06-28 11:06:31 -0700146 if (type == EV_SYN && code == SYN_REPORT) {
147 evdev->hw_ts_sec = -1;
148 evdev->hw_ts_nsec = -1;
Dmitry Torokhovda40b0b2011-06-18 02:50:11 -0700149 wake_up_interruptible(&evdev->wait);
Sasha Levitskiy2d4e6f652013-06-28 11:06:31 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static int evdev_fasync(int fd, struct file *file, int on)
154{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400155 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400156
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700157 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400160static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400162 struct evdev_client *client = file->private_data;
163 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400164 int retval;
165
166 retval = mutex_lock_interruptible(&evdev->mutex);
167 if (retval)
168 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400169
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400170 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400171 retval = -ENODEV;
172 else
173 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400174
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400175 mutex_unlock(&evdev->mutex);
176 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400179static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400181 struct evdev *evdev = container_of(dev, struct evdev, dev);
182
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400183 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 kfree(evdev);
185}
186
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400187/*
188 * Grabs an event device (along with underlying input device).
189 * This function is called with evdev->mutex taken.
190 */
191static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
192{
193 int error;
194
195 if (evdev->grab)
196 return -EBUSY;
197
198 error = input_grab_device(&evdev->handle);
199 if (error)
200 return error;
201
202 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400203
204 return 0;
205}
206
207static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
208{
209 if (evdev->grab != client)
210 return -EINVAL;
211
212 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400213 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400214 input_release_device(&evdev->handle);
215
216 return 0;
217}
218
219static void evdev_attach_client(struct evdev *evdev,
220 struct evdev_client *client)
221{
222 spin_lock(&evdev->client_lock);
223 list_add_tail_rcu(&client->node, &evdev->client_list);
224 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400225}
226
227static void evdev_detach_client(struct evdev *evdev,
228 struct evdev_client *client)
229{
230 spin_lock(&evdev->client_lock);
231 list_del_rcu(&client->node);
232 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400233 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400234}
235
236static int evdev_open_device(struct evdev *evdev)
237{
238 int retval;
239
240 retval = mutex_lock_interruptible(&evdev->mutex);
241 if (retval)
242 return retval;
243
244 if (!evdev->exist)
245 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400246 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400247 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400248 if (retval)
249 evdev->open--;
250 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400251
252 mutex_unlock(&evdev->mutex);
253 return retval;
254}
255
256static void evdev_close_device(struct evdev *evdev)
257{
258 mutex_lock(&evdev->mutex);
259
260 if (evdev->exist && !--evdev->open)
261 input_close_device(&evdev->handle);
262
263 mutex_unlock(&evdev->mutex);
264}
265
266/*
267 * Wake up users waiting for IO so they can disconnect from
268 * dead device.
269 */
270static void evdev_hangup(struct evdev *evdev)
271{
272 struct evdev_client *client;
273
274 spin_lock(&evdev->client_lock);
275 list_for_each_entry(client, &evdev->client_list, node)
276 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
277 spin_unlock(&evdev->client_lock);
278
279 wake_up_interruptible(&evdev->wait);
280}
281
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400282static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400284 struct evdev_client *client = file->private_data;
285 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400287 mutex_lock(&evdev->mutex);
288 if (evdev->grab == client)
289 evdev_ungrab(evdev, client);
290 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400292 evdev_detach_client(evdev, client);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700293 if (client->use_wake_lock)
294 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400295 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400297 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400298 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
301}
302
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700303static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
304{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700305 unsigned int n_events =
306 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
307 EVDEV_MIN_BUFFER_SIZE);
308
309 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700310}
311
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400312static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400314 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400315 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700317 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400318 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400320 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -ENODEV;
322
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400323 error = mutex_lock_interruptible(&evdev_table_mutex);
324 if (error)
325 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400326 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400327 if (evdev)
328 get_device(&evdev->dev);
329 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400330
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400331 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400332 return -ENODEV;
333
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700334 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
335
336 client = kzalloc(sizeof(struct evdev_client) +
337 bufsize * sizeof(struct input_event),
338 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400339 if (!client) {
340 error = -ENOMEM;
341 goto err_put_evdev;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Steve Mucklef132c6c2012-06-06 18:30:57 -0700344 client->clkid = CLOCK_MONOTONIC;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700345 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400346 spin_lock_init(&client->buffer_lock);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700347 snprintf(client->name, sizeof(client->name), "%s-%d",
348 dev_name(&evdev->dev), task_tgid_vnr(current));
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400349 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400350 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400352 error = evdev_open_device(evdev);
353 if (error)
354 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400356 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800357 nonseekable_open(inode, file);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400360
361 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400362 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400363 kfree(client);
364 err_put_evdev:
365 put_device(&evdev->dev);
366 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400369static ssize_t evdev_write(struct file *file, const char __user *buffer,
370 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400372 struct evdev_client *client = file->private_data;
373 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800375 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Peter Korsgaard439581e2011-02-25 09:30:46 -0800377 if (count < input_event_size())
378 return -EINVAL;
379
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400380 retval = mutex_lock_interruptible(&evdev->mutex);
381 if (retval)
382 return retval;
383
384 if (!evdev->exist) {
385 retval = -ENODEV;
386 goto out;
387 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500388
Peter Korsgaard439581e2011-02-25 09:30:46 -0800389 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400390 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400391 retval = -EFAULT;
392 goto out;
393 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800394 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400395
396 input_inject_event(&evdev->handle,
397 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800398 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500399
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400400 out:
401 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500402 return retval;
403}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500404
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400405static int evdev_fetch_next_event(struct evdev_client *client,
406 struct input_event *event)
407{
408 int have_event;
409
410 spin_lock_irq(&client->buffer_lock);
411
Dima Zavin566cf5b2011-12-30 15:16:44 -0800412 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400413 if (have_event) {
414 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700415 client->tail &= client->bufsize - 1;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700416 if (client->use_wake_lock &&
417 client->packet_head == client->tail)
418 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400419 }
420
421 spin_unlock_irq(&client->buffer_lock);
422
423 return have_event;
424}
425
426static ssize_t evdev_read(struct file *file, char __user *buffer,
427 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400429 struct evdev_client *client = file->private_data;
430 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400431 struct input_event event;
Heiko Stübner42f57872012-02-01 09:12:23 -0800432 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400434 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return -EINVAL;
436
Dima Zavin509f87c2011-12-30 15:16:44 -0800437 if (!(file->f_flags & O_NONBLOCK)) {
438 retval = wait_event_interruptible(evdev->wait,
439 client->packet_head != client->tail ||
440 !evdev->exist);
441 if (retval)
442 return retval;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400445 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return -ENODEV;
447
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400448 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400449 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500450
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400451 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500452 return -EFAULT;
453
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400454 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
Dima Zavine90f8692011-12-30 15:16:44 -0800457 if (retval == 0 && (file->f_flags & O_NONBLOCK))
458 return -EAGAIN;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return retval;
461}
462
463/* No kernel lock - fine */
464static unsigned int evdev_poll(struct file *file, poll_table *wait)
465{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400466 struct evdev_client *client = file->private_data;
467 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700468 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400469
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400470 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700471
472 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700473 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700474 mask |= POLLIN | POLLRDNORM;
475
476 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500479#ifdef CONFIG_COMPAT
480
481#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700482#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500483
484#ifdef __BIG_ENDIAN
485static int bits_to_user(unsigned long *bits, unsigned int maxbit,
486 unsigned int maxlen, void __user *p, int compat)
487{
488 int len, i;
489
490 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700491 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400492 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500493 len = maxlen;
494
495 for (i = 0; i < len / sizeof(compat_long_t); i++)
496 if (copy_to_user((compat_long_t __user *) p + i,
497 (compat_long_t *) bits +
498 i + 1 - ((i % 2) << 1),
499 sizeof(compat_long_t)))
500 return -EFAULT;
501 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700502 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500503 if (len > maxlen)
504 len = maxlen;
505
506 if (copy_to_user(p, bits, len))
507 return -EFAULT;
508 }
509
510 return len;
511}
512#else
513static int bits_to_user(unsigned long *bits, unsigned int maxbit,
514 unsigned int maxlen, void __user *p, int compat)
515{
516 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700517 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
518 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500519
520 if (len > maxlen)
521 len = maxlen;
522
523 return copy_to_user(p, bits, len) ? -EFAULT : len;
524}
525#endif /* __BIG_ENDIAN */
526
527#else
528
529static int bits_to_user(unsigned long *bits, unsigned int maxbit,
530 unsigned int maxlen, void __user *p, int compat)
531{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700532 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500533
534 if (len > maxlen)
535 len = maxlen;
536
537 return copy_to_user(p, bits, len) ? -EFAULT : len;
538}
539
540#endif /* CONFIG_COMPAT */
541
542static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
543{
544 int len;
545
546 if (!str)
547 return -ENOENT;
548
549 len = strlen(str) + 1;
550 if (len > maxlen)
551 len = maxlen;
552
553 return copy_to_user(p, str, len) ? -EFAULT : len;
554}
555
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400556#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700557static int handle_eviocgbit(struct input_dev *dev,
558 unsigned int type, unsigned int size,
559 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400560{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400561 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400562 unsigned long *bits;
563 int len;
564
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700565 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400566
567 case 0: bits = dev->evbit; len = EV_MAX; break;
568 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
569 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
570 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
571 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
572 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
573 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
574 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
575 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
576 default: return -EINVAL;
577 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400578
579 /*
580 * Work around bugs in userspace programs that like to do
581 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
582 * should be in bytes, not in bits.
583 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700584 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400585 len = OLD_KEY_MAX;
586 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800587 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
588 "limiting output to %zu bytes. See "
589 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
590 OLD_KEY_MAX,
591 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400592 }
593
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700594 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400595}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400596#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400597
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800598static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
599{
600 struct input_keymap_entry ke = {
601 .len = sizeof(unsigned int),
602 .flags = 0,
603 };
604 int __user *ip = (int __user *)p;
605 int error;
606
607 /* legacy case */
608 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
609 return -EFAULT;
610
611 error = input_get_keycode(dev, &ke);
612 if (error)
613 return error;
614
615 if (put_user(ke.keycode, ip + 1))
616 return -EFAULT;
617
618 return 0;
619}
620
621static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700622{
623 struct input_keymap_entry ke;
624 int error;
625
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800626 if (copy_from_user(&ke, p, sizeof(ke)))
627 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700628
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800629 error = input_get_keycode(dev, &ke);
630 if (error)
631 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700632
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800633 if (copy_to_user(p, &ke, sizeof(ke)))
634 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700635
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700636 return 0;
637}
638
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800639static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
640{
641 struct input_keymap_entry ke = {
642 .len = sizeof(unsigned int),
643 .flags = 0,
644 };
645 int __user *ip = (int __user *)p;
646
647 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
648 return -EFAULT;
649
650 if (get_user(ke.keycode, ip + 1))
651 return -EFAULT;
652
653 return input_set_keycode(dev, &ke);
654}
655
656static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700657{
658 struct input_keymap_entry ke;
659
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800660 if (copy_from_user(&ke, p, sizeof(ke)))
661 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700662
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800663 if (ke.len > sizeof(ke.scancode))
664 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700665
666 return input_set_keycode(dev, &ke);
667}
668
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100669static int evdev_handle_mt_request(struct input_dev *dev,
670 unsigned int size,
671 int __user *ip)
672{
673 const struct input_mt_slot *mt = dev->mt;
674 unsigned int code;
675 int max_slots;
676 int i;
677
678 if (get_user(code, &ip[0]))
679 return -EFAULT;
680 if (!input_is_mt_value(code))
681 return -EINVAL;
682
683 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
684 for (i = 0; i < dev->mtsize && i < max_slots; i++)
685 if (put_user(input_mt_get_value(&mt[i], code), &ip[1 + i]))
686 return -EFAULT;
687
688 return 0;
689}
690
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700691static int evdev_enable_suspend_block(struct evdev *evdev,
692 struct evdev_client *client)
693{
694 if (client->use_wake_lock)
695 return 0;
696
697 spin_lock_irq(&client->buffer_lock);
698 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
699 client->use_wake_lock = true;
700 if (client->packet_head != client->tail)
701 wake_lock(&client->wake_lock);
702 spin_unlock_irq(&client->buffer_lock);
703 return 0;
704}
705
706static int evdev_disable_suspend_block(struct evdev *evdev,
707 struct evdev_client *client)
708{
709 if (!client->use_wake_lock)
710 return 0;
711
712 spin_lock_irq(&client->buffer_lock);
713 client->use_wake_lock = false;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700714 spin_unlock_irq(&client->buffer_lock);
Anurag Singhe218cdb2013-03-19 17:14:21 -0700715 wake_lock_destroy(&client->wake_lock);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700716
717 return 0;
718}
719
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400720static long evdev_do_ioctl(struct file *file, unsigned int cmd,
721 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400723 struct evdev_client *client = file->private_data;
724 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct input_dev *dev = evdev->handle.dev;
726 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400727 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500728 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800729 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700730 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400731 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700733 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 switch (cmd) {
735
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400736 case EVIOCGVERSION:
737 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400739 case EVIOCGID:
740 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
741 return -EFAULT;
742 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400743
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400744 case EVIOCGREP:
745 if (!test_bit(EV_REP, dev->evbit))
746 return -ENOSYS;
747 if (put_user(dev->rep[REP_DELAY], ip))
748 return -EFAULT;
749 if (put_user(dev->rep[REP_PERIOD], ip + 1))
750 return -EFAULT;
751 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400752
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400753 case EVIOCSREP:
754 if (!test_bit(EV_REP, dev->evbit))
755 return -ENOSYS;
756 if (get_user(u, ip))
757 return -EFAULT;
758 if (get_user(v, ip + 1))
759 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400760
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400761 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
762 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500763
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400766 case EVIOCRMFF:
767 return input_ff_erase(dev, (int)(unsigned long) p, file);
768
769 case EVIOCGEFFECTS:
770 i = test_bit(EV_FF, dev->evbit) ?
771 dev->ff->max_effects : 0;
772 if (put_user(i, ip))
773 return -EFAULT;
774 return 0;
775
776 case EVIOCGRAB:
777 if (p)
778 return evdev_grab(evdev, client);
779 else
780 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800781
John Stultza80b83b2012-02-03 00:19:07 -0800782 case EVIOCSCLOCKID:
783 if (copy_from_user(&i, p, sizeof(unsigned int)))
784 return -EFAULT;
785 if (i != CLOCK_MONOTONIC && i != CLOCK_REALTIME)
786 return -EINVAL;
787 client->clkid = i;
788 return 0;
789
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800790 case EVIOCGKEYCODE:
791 return evdev_handle_get_keycode(dev, p);
792
793 case EVIOCSKEYCODE:
794 return evdev_handle_set_keycode(dev, p);
795
796 case EVIOCGKEYCODE_V2:
797 return evdev_handle_get_keycode_v2(dev, p);
798
799 case EVIOCSKEYCODE_V2:
800 return evdev_handle_set_keycode_v2(dev, p);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700801
802 case EVIOCGSUSPENDBLOCK:
803 return put_user(client->use_wake_lock, ip);
804
805 case EVIOCSSUSPENDBLOCK:
806 if (p)
807 return evdev_enable_suspend_block(evdev, client);
808 else
809 return evdev_disable_suspend_block(evdev, client);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700810 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400811
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700812 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400813
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700814 /* Now check variable-length commands */
815#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700816 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400817
Henrik Rydberg85b77202010-12-18 20:51:13 +0100818 case EVIOCGPROP(0):
819 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
820 size, p, compat_mode);
821
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100822 case EVIOCGMTSLOTS(0):
823 return evdev_handle_mt_request(dev, size, ip);
824
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700825 case EVIOCGKEY(0):
826 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400827
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700828 case EVIOCGLED(0):
829 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400830
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700831 case EVIOCGSND(0):
832 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400833
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700834 case EVIOCGSW(0):
835 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400836
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700837 case EVIOCGNAME(0):
838 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400839
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700840 case EVIOCGPHYS(0):
841 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400842
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700843 case EVIOCGUNIQ(0):
844 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400845
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700846 case EVIOC_MASK_SIZE(EVIOCSFF):
847 if (input_ff_effect_from_user(p, size, &effect))
848 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400849
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700850 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400851
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700852 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
853 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400854
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700855 return error;
856 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400857
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700858 /* Multi-number variable-length handlers */
859 if (_IOC_TYPE(cmd) != 'E')
860 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700862 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700864 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
865 return handle_eviocgbit(dev,
866 _IOC_NR(cmd) & EV_MAX, size,
867 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700869 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400870
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700871 if (!dev->absinfo)
872 return -EINVAL;
873
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700874 t = _IOC_NR(cmd) & ABS_MAX;
875 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400876
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700877 if (copy_to_user(p, &abs, min_t(size_t,
878 size, sizeof(struct input_absinfo))))
879 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400880
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700881 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700884
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700885 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700886
887 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
888
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700889 if (!dev->absinfo)
890 return -EINVAL;
891
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700892 t = _IOC_NR(cmd) & ABS_MAX;
893
894 if (copy_from_user(&abs, p, min_t(size_t,
895 size, sizeof(struct input_absinfo))))
896 return -EFAULT;
897
898 if (size < sizeof(struct input_absinfo))
899 abs.resolution = 0;
900
901 /* We can't change number of reserved MT slots */
902 if (t == ABS_MT_SLOT)
903 return -EINVAL;
904
905 /*
906 * Take event lock to ensure that we are not
907 * changing device parameters in the middle
908 * of event.
909 */
910 spin_lock_irq(&dev->event_lock);
911 dev->absinfo[t] = abs;
912 spin_unlock_irq(&dev->event_lock);
913
914 return 0;
915 }
916 }
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return -EINVAL;
919}
920
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400921static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
922 void __user *p, int compat_mode)
923{
924 struct evdev_client *client = file->private_data;
925 struct evdev *evdev = client->evdev;
926 int retval;
927
928 retval = mutex_lock_interruptible(&evdev->mutex);
929 if (retval)
930 return retval;
931
932 if (!evdev->exist) {
933 retval = -ENODEV;
934 goto out;
935 }
936
937 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
938
939 out:
940 mutex_unlock(&evdev->mutex);
941 return retval;
942}
943
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500944static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
945{
946 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
947}
948
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500949#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400950static long evdev_ioctl_compat(struct file *file,
951 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500952{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500953 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500954}
955#endif
956
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400957static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400958 .owner = THIS_MODULE,
959 .read = evdev_read,
960 .write = evdev_write,
961 .poll = evdev_poll,
962 .open = evdev_open,
963 .release = evdev_release,
964 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500965#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400966 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500967#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400968 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200969 .flush = evdev_flush,
970 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971};
972
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400973static int evdev_install_chrdev(struct evdev *evdev)
974{
975 /*
976 * No need to do any locking here as calls to connect and
977 * disconnect are serialized by the input core
978 */
979 evdev_table[evdev->minor] = evdev;
980 return 0;
981}
982
983static void evdev_remove_chrdev(struct evdev *evdev)
984{
985 /*
986 * Lock evdev table to prevent race with evdev_open()
987 */
988 mutex_lock(&evdev_table_mutex);
989 evdev_table[evdev->minor] = NULL;
990 mutex_unlock(&evdev_table_mutex);
991}
992
993/*
994 * Mark device non-existent. This disables writes, ioctls and
995 * prevents new users from opening the device. Already posted
996 * blocking reads will stay, however new ones will fail.
997 */
998static void evdev_mark_dead(struct evdev *evdev)
999{
1000 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001001 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001002 mutex_unlock(&evdev->mutex);
1003}
1004
1005static void evdev_cleanup(struct evdev *evdev)
1006{
1007 struct input_handle *handle = &evdev->handle;
1008
1009 evdev_mark_dead(evdev);
1010 evdev_hangup(evdev);
1011 evdev_remove_chrdev(evdev);
1012
1013 /* evdev is marked dead so no one else accesses evdev->open */
1014 if (evdev->open) {
1015 input_flush_device(handle, NULL);
1016 input_close_device(handle);
1017 }
1018}
1019
1020/*
1021 * Create new evdev device. Note that input core serializes calls
1022 * to connect and disconnect so we don't need to lock evdev_table here.
1023 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001024static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1025 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct evdev *evdev;
1028 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001029 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001031 for (minor = 0; minor < EVDEV_MINORS; minor++)
1032 if (!evdev_table[minor])
1033 break;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -08001036 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001037 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001040 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
1041 if (!evdev)
1042 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001044 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001045 spin_lock_init(&evdev->client_lock);
1046 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 init_waitqueue_head(&evdev->wait);
1048
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001049 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001050 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 evdev->minor = minor;
Sasha Levitskiy2d4e6f652013-06-28 11:06:31 -07001052 evdev->hw_ts_sec = -1;
1053 evdev->hw_ts_nsec = -1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001054
Dmitry Torokhova7097ff2008-04-01 00:22:53 -04001055 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001056 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 evdev->handle.handler = handler;
1058 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001059
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001060 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001061 evdev->dev.class = &input_class;
1062 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001063 evdev->dev.release = evdev_free;
1064 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001066 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001067 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001068 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001070 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001071 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001072 goto err_unregister_handle;
1073
1074 error = device_add(&evdev->dev);
1075 if (error)
1076 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001077
1078 return 0;
1079
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001080 err_cleanup_evdev:
1081 evdev_cleanup(evdev);
1082 err_unregister_handle:
1083 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001084 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001085 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001086 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089static void evdev_disconnect(struct input_handle *handle)
1090{
1091 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001093 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001094 evdev_cleanup(evdev);
1095 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001096 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001099static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 { .driver_info = 1 }, /* Matches all devices */
1101 { }, /* Terminating zero entry */
1102};
1103
1104MODULE_DEVICE_TABLE(input, evdev_ids);
1105
1106static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001107 .event = evdev_event,
1108 .connect = evdev_connect,
1109 .disconnect = evdev_disconnect,
1110 .fops = &evdev_fops,
1111 .minor = EVDEV_MINOR_BASE,
1112 .name = "evdev",
1113 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114};
1115
1116static int __init evdev_init(void)
1117{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001118 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121static void __exit evdev_exit(void)
1122{
1123 input_unregister_handler(&evdev_handler);
1124}
1125
1126module_init(evdev_init);
1127module_exit(evdev_exit);
1128
1129MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1130MODULE_DESCRIPTION("Input driver event char devices");
1131MODULE_LICENSE("GPL");