blob: 41f79be6f4054a9224867cce61bea1bdabf380c5 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040042struct evdev_client {
Jeff Brown9fb0f142011-04-12 23:29:38 -070043 unsigned int head;
44 unsigned int tail;
Jeff Browncdda9112011-04-26 22:16:11 -070045 unsigned int packet_head; /* [future] position of the first element of next packet */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040046 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Arve Hjønnevåg783820b2008-10-17 15:20:55 -070047 struct wake_lock wake_lock;
48 bool use_wake_lock;
49 char name[28];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct fasync_struct *fasync;
51 struct evdev *evdev;
52 struct list_head node;
John Stultza80b83b2012-02-03 00:19:07 -080053 int clkid;
Jeff Brown9fb0f142011-04-12 23:29:38 -070054 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070055 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040059static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040061static void evdev_pass_event(struct evdev_client *client,
John Stultza80b83b2012-02-03 00:19:07 -080062 struct input_event *event,
63 ktime_t mono, ktime_t real)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040064{
John Stultza80b83b2012-02-03 00:19:07 -080065 event->time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ?
66 mono : real);
67
Jeff Brown9fb0f142011-04-12 23:29:38 -070068 /* Interrupts are disabled, just acquire the lock. */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040069 spin_lock(&client->buffer_lock);
Jeff Brown9fb0f142011-04-12 23:29:38 -070070
71 client->buffer[client->head++] = *event;
72 client->head &= client->bufsize - 1;
73
74 if (unlikely(client->head == client->tail)) {
75 /*
76 * This effectively "drops" all unconsumed events, leaving
77 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
78 */
79 client->tail = (client->head - 2) & (client->bufsize - 1);
80
81 client->buffer[client->tail].time = event->time;
82 client->buffer[client->tail].type = EV_SYN;
83 client->buffer[client->tail].code = SYN_DROPPED;
84 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -070085
86 client->packet_head = client->tail;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -070087 if (client->use_wake_lock)
88 wake_unlock(&client->wake_lock);
Jeff Browncdda9112011-04-26 22:16:11 -070089 }
90
91 if (event->type == EV_SYN && event->code == SYN_REPORT) {
92 client->packet_head = client->head;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -070093 if (client->use_wake_lock)
94 wake_lock(&client->wake_lock);
Jeff Browncdda9112011-04-26 22:16:11 -070095 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -070096 }
97
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040098 spin_unlock(&client->buffer_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040099}
100
101/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400102 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400103 */
104static void evdev_event(struct input_handle *handle,
105 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400108 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400109 struct input_event event;
John Stultza80b83b2012-02-03 00:19:07 -0800110 ktime_t time_mono, time_real;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
John Stultza80b83b2012-02-03 00:19:07 -0800112 time_mono = ktime_get();
113 time_real = ktime_sub(time_mono, ktime_get_monotonic_offset());
114
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400115 event.type = type;
116 event.code = code;
117 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400119 rcu_read_lock();
120
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400121 client = rcu_dereference(evdev->grab);
John Stultza80b83b2012-02-03 00:19:07 -0800122
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400123 if (client)
John Stultza80b83b2012-02-03 00:19:07 -0800124 evdev_pass_event(client, &event, time_mono, time_real);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400125 else
126 list_for_each_entry_rcu(client, &evdev->client_list, node)
John Stultza80b83b2012-02-03 00:19:07 -0800127 evdev_pass_event(client, &event, time_mono, time_real);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400129 rcu_read_unlock();
130
Dmitry Torokhovda40b0b2011-06-18 02:50:11 -0700131 if (type == EV_SYN && code == SYN_REPORT)
132 wake_up_interruptible(&evdev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135static int evdev_fasync(int fd, struct file *file, int on)
136{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400137 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400138
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700139 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400142static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400144 struct evdev_client *client = file->private_data;
145 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400146 int retval;
147
148 retval = mutex_lock_interruptible(&evdev->mutex);
149 if (retval)
150 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400151
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400152 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400153 retval = -ENODEV;
154 else
155 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400156
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400157 mutex_unlock(&evdev->mutex);
158 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400161static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400163 struct evdev *evdev = container_of(dev, struct evdev, dev);
164
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400165 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 kfree(evdev);
167}
168
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400169/*
170 * Grabs an event device (along with underlying input device).
171 * This function is called with evdev->mutex taken.
172 */
173static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
174{
175 int error;
176
177 if (evdev->grab)
178 return -EBUSY;
179
180 error = input_grab_device(&evdev->handle);
181 if (error)
182 return error;
183
184 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400185
186 return 0;
187}
188
189static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
190{
191 if (evdev->grab != client)
192 return -EINVAL;
193
194 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400195 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400196 input_release_device(&evdev->handle);
197
198 return 0;
199}
200
201static void evdev_attach_client(struct evdev *evdev,
202 struct evdev_client *client)
203{
204 spin_lock(&evdev->client_lock);
205 list_add_tail_rcu(&client->node, &evdev->client_list);
206 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400207}
208
209static void evdev_detach_client(struct evdev *evdev,
210 struct evdev_client *client)
211{
212 spin_lock(&evdev->client_lock);
213 list_del_rcu(&client->node);
214 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400215 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400216}
217
218static int evdev_open_device(struct evdev *evdev)
219{
220 int retval;
221
222 retval = mutex_lock_interruptible(&evdev->mutex);
223 if (retval)
224 return retval;
225
226 if (!evdev->exist)
227 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400228 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400229 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400230 if (retval)
231 evdev->open--;
232 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400233
234 mutex_unlock(&evdev->mutex);
235 return retval;
236}
237
238static void evdev_close_device(struct evdev *evdev)
239{
240 mutex_lock(&evdev->mutex);
241
242 if (evdev->exist && !--evdev->open)
243 input_close_device(&evdev->handle);
244
245 mutex_unlock(&evdev->mutex);
246}
247
248/*
249 * Wake up users waiting for IO so they can disconnect from
250 * dead device.
251 */
252static void evdev_hangup(struct evdev *evdev)
253{
254 struct evdev_client *client;
255
256 spin_lock(&evdev->client_lock);
257 list_for_each_entry(client, &evdev->client_list, node)
258 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
259 spin_unlock(&evdev->client_lock);
260
261 wake_up_interruptible(&evdev->wait);
262}
263
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400264static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400266 struct evdev_client *client = file->private_data;
267 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400269 mutex_lock(&evdev->mutex);
270 if (evdev->grab == client)
271 evdev_ungrab(evdev, client);
272 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400274 evdev_detach_client(evdev, client);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700275 if (client->use_wake_lock)
276 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400277 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400279 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400280 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283}
284
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700285static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
286{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700287 unsigned int n_events =
288 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
289 EVDEV_MIN_BUFFER_SIZE);
290
291 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700292}
293
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400294static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400296 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400297 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700299 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400300 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400302 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return -ENODEV;
304
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400305 error = mutex_lock_interruptible(&evdev_table_mutex);
306 if (error)
307 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400308 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400309 if (evdev)
310 get_device(&evdev->dev);
311 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400312
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400313 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400314 return -ENODEV;
315
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700316 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
317
318 client = kzalloc(sizeof(struct evdev_client) +
319 bufsize * sizeof(struct input_event),
320 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400321 if (!client) {
322 error = -ENOMEM;
323 goto err_put_evdev;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Steve Mucklef132c6c2012-06-06 18:30:57 -0700326 client->clkid = CLOCK_MONOTONIC;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700327 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400328 spin_lock_init(&client->buffer_lock);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700329 snprintf(client->name, sizeof(client->name), "%s-%d",
330 dev_name(&evdev->dev), task_tgid_vnr(current));
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400331 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400332 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400334 error = evdev_open_device(evdev);
335 if (error)
336 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400338 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800339 nonseekable_open(inode, file);
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400342
343 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400344 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400345 kfree(client);
346 err_put_evdev:
347 put_device(&evdev->dev);
348 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400351static ssize_t evdev_write(struct file *file, const char __user *buffer,
352 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400354 struct evdev_client *client = file->private_data;
355 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800357 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Peter Korsgaard439581e2011-02-25 09:30:46 -0800359 if (count < input_event_size())
360 return -EINVAL;
361
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400362 retval = mutex_lock_interruptible(&evdev->mutex);
363 if (retval)
364 return retval;
365
366 if (!evdev->exist) {
367 retval = -ENODEV;
368 goto out;
369 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500370
Peter Korsgaard439581e2011-02-25 09:30:46 -0800371 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400372 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400373 retval = -EFAULT;
374 goto out;
375 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800376 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400377
378 input_inject_event(&evdev->handle,
379 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800380 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500381
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400382 out:
383 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500384 return retval;
385}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500386
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400387static int evdev_fetch_next_event(struct evdev_client *client,
388 struct input_event *event)
389{
390 int have_event;
391
392 spin_lock_irq(&client->buffer_lock);
393
Dima Zavin566cf5b2011-12-30 15:16:44 -0800394 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400395 if (have_event) {
396 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700397 client->tail &= client->bufsize - 1;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700398 if (client->use_wake_lock &&
399 client->packet_head == client->tail)
400 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400401 }
402
403 spin_unlock_irq(&client->buffer_lock);
404
405 return have_event;
406}
407
408static ssize_t evdev_read(struct file *file, char __user *buffer,
409 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400411 struct evdev_client *client = file->private_data;
412 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400413 struct input_event event;
Heiko Stübner42f57872012-02-01 09:12:23 -0800414 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400416 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -EINVAL;
418
Dima Zavin509f87c2011-12-30 15:16:44 -0800419 if (!(file->f_flags & O_NONBLOCK)) {
420 retval = wait_event_interruptible(evdev->wait,
421 client->packet_head != client->tail ||
422 !evdev->exist);
423 if (retval)
424 return retval;
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400427 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -ENODEV;
429
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400430 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400431 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500432
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400433 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500434 return -EFAULT;
435
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400436 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
Dima Zavine90f8692011-12-30 15:16:44 -0800439 if (retval == 0 && (file->f_flags & O_NONBLOCK))
440 return -EAGAIN;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return retval;
443}
444
445/* No kernel lock - fine */
446static unsigned int evdev_poll(struct file *file, poll_table *wait)
447{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400448 struct evdev_client *client = file->private_data;
449 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700450 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400451
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400452 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700453
454 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700455 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700456 mask |= POLLIN | POLLRDNORM;
457
458 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500461#ifdef CONFIG_COMPAT
462
463#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700464#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500465
466#ifdef __BIG_ENDIAN
467static int bits_to_user(unsigned long *bits, unsigned int maxbit,
468 unsigned int maxlen, void __user *p, int compat)
469{
470 int len, i;
471
472 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700473 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400474 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500475 len = maxlen;
476
477 for (i = 0; i < len / sizeof(compat_long_t); i++)
478 if (copy_to_user((compat_long_t __user *) p + i,
479 (compat_long_t *) bits +
480 i + 1 - ((i % 2) << 1),
481 sizeof(compat_long_t)))
482 return -EFAULT;
483 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700484 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500485 if (len > maxlen)
486 len = maxlen;
487
488 if (copy_to_user(p, bits, len))
489 return -EFAULT;
490 }
491
492 return len;
493}
494#else
495static int bits_to_user(unsigned long *bits, unsigned int maxbit,
496 unsigned int maxlen, void __user *p, int compat)
497{
498 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700499 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
500 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500501
502 if (len > maxlen)
503 len = maxlen;
504
505 return copy_to_user(p, bits, len) ? -EFAULT : len;
506}
507#endif /* __BIG_ENDIAN */
508
509#else
510
511static int bits_to_user(unsigned long *bits, unsigned int maxbit,
512 unsigned int maxlen, void __user *p, int compat)
513{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700514 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500515
516 if (len > maxlen)
517 len = maxlen;
518
519 return copy_to_user(p, bits, len) ? -EFAULT : len;
520}
521
522#endif /* CONFIG_COMPAT */
523
524static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
525{
526 int len;
527
528 if (!str)
529 return -ENOENT;
530
531 len = strlen(str) + 1;
532 if (len > maxlen)
533 len = maxlen;
534
535 return copy_to_user(p, str, len) ? -EFAULT : len;
536}
537
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400538#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700539static int handle_eviocgbit(struct input_dev *dev,
540 unsigned int type, unsigned int size,
541 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400542{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400543 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400544 unsigned long *bits;
545 int len;
546
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700547 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400548
549 case 0: bits = dev->evbit; len = EV_MAX; break;
550 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
551 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
552 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
553 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
554 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
555 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
556 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
557 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
558 default: return -EINVAL;
559 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400560
561 /*
562 * Work around bugs in userspace programs that like to do
563 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
564 * should be in bytes, not in bits.
565 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700566 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400567 len = OLD_KEY_MAX;
568 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800569 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
570 "limiting output to %zu bytes. See "
571 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
572 OLD_KEY_MAX,
573 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400574 }
575
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700576 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400577}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400578#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400579
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800580static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
581{
582 struct input_keymap_entry ke = {
583 .len = sizeof(unsigned int),
584 .flags = 0,
585 };
586 int __user *ip = (int __user *)p;
587 int error;
588
589 /* legacy case */
590 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
591 return -EFAULT;
592
593 error = input_get_keycode(dev, &ke);
594 if (error)
595 return error;
596
597 if (put_user(ke.keycode, ip + 1))
598 return -EFAULT;
599
600 return 0;
601}
602
603static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700604{
605 struct input_keymap_entry ke;
606 int error;
607
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800608 if (copy_from_user(&ke, p, sizeof(ke)))
609 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700610
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800611 error = input_get_keycode(dev, &ke);
612 if (error)
613 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700614
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800615 if (copy_to_user(p, &ke, sizeof(ke)))
616 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700617
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700618 return 0;
619}
620
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800621static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
622{
623 struct input_keymap_entry ke = {
624 .len = sizeof(unsigned int),
625 .flags = 0,
626 };
627 int __user *ip = (int __user *)p;
628
629 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
630 return -EFAULT;
631
632 if (get_user(ke.keycode, ip + 1))
633 return -EFAULT;
634
635 return input_set_keycode(dev, &ke);
636}
637
638static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700639{
640 struct input_keymap_entry ke;
641
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800642 if (copy_from_user(&ke, p, sizeof(ke)))
643 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700644
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800645 if (ke.len > sizeof(ke.scancode))
646 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700647
648 return input_set_keycode(dev, &ke);
649}
650
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100651static int evdev_handle_mt_request(struct input_dev *dev,
652 unsigned int size,
653 int __user *ip)
654{
655 const struct input_mt_slot *mt = dev->mt;
656 unsigned int code;
657 int max_slots;
658 int i;
659
660 if (get_user(code, &ip[0]))
661 return -EFAULT;
662 if (!input_is_mt_value(code))
663 return -EINVAL;
664
665 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
666 for (i = 0; i < dev->mtsize && i < max_slots; i++)
667 if (put_user(input_mt_get_value(&mt[i], code), &ip[1 + i]))
668 return -EFAULT;
669
670 return 0;
671}
672
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700673static int evdev_enable_suspend_block(struct evdev *evdev,
674 struct evdev_client *client)
675{
676 if (client->use_wake_lock)
677 return 0;
678
679 spin_lock_irq(&client->buffer_lock);
680 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
681 client->use_wake_lock = true;
682 if (client->packet_head != client->tail)
683 wake_lock(&client->wake_lock);
684 spin_unlock_irq(&client->buffer_lock);
685 return 0;
686}
687
688static int evdev_disable_suspend_block(struct evdev *evdev,
689 struct evdev_client *client)
690{
691 if (!client->use_wake_lock)
692 return 0;
693
694 spin_lock_irq(&client->buffer_lock);
695 client->use_wake_lock = false;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700696 spin_unlock_irq(&client->buffer_lock);
Anurag Singhe218cdb2013-03-19 17:14:21 -0700697 wake_lock_destroy(&client->wake_lock);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700698
699 return 0;
700}
701
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400702static long evdev_do_ioctl(struct file *file, unsigned int cmd,
703 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400705 struct evdev_client *client = file->private_data;
706 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct input_dev *dev = evdev->handle.dev;
708 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400709 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500710 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800711 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700712 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400713 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700715 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 switch (cmd) {
717
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400718 case EVIOCGVERSION:
719 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400721 case EVIOCGID:
722 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
723 return -EFAULT;
724 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400725
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400726 case EVIOCGREP:
727 if (!test_bit(EV_REP, dev->evbit))
728 return -ENOSYS;
729 if (put_user(dev->rep[REP_DELAY], ip))
730 return -EFAULT;
731 if (put_user(dev->rep[REP_PERIOD], ip + 1))
732 return -EFAULT;
733 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400734
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400735 case EVIOCSREP:
736 if (!test_bit(EV_REP, dev->evbit))
737 return -ENOSYS;
738 if (get_user(u, ip))
739 return -EFAULT;
740 if (get_user(v, ip + 1))
741 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400742
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400743 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
744 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500745
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400748 case EVIOCRMFF:
749 return input_ff_erase(dev, (int)(unsigned long) p, file);
750
751 case EVIOCGEFFECTS:
752 i = test_bit(EV_FF, dev->evbit) ?
753 dev->ff->max_effects : 0;
754 if (put_user(i, ip))
755 return -EFAULT;
756 return 0;
757
758 case EVIOCGRAB:
759 if (p)
760 return evdev_grab(evdev, client);
761 else
762 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800763
John Stultza80b83b2012-02-03 00:19:07 -0800764 case EVIOCSCLOCKID:
765 if (copy_from_user(&i, p, sizeof(unsigned int)))
766 return -EFAULT;
767 if (i != CLOCK_MONOTONIC && i != CLOCK_REALTIME)
768 return -EINVAL;
769 client->clkid = i;
770 return 0;
771
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800772 case EVIOCGKEYCODE:
773 return evdev_handle_get_keycode(dev, p);
774
775 case EVIOCSKEYCODE:
776 return evdev_handle_set_keycode(dev, p);
777
778 case EVIOCGKEYCODE_V2:
779 return evdev_handle_get_keycode_v2(dev, p);
780
781 case EVIOCSKEYCODE_V2:
782 return evdev_handle_set_keycode_v2(dev, p);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700783
784 case EVIOCGSUSPENDBLOCK:
785 return put_user(client->use_wake_lock, ip);
786
787 case EVIOCSSUSPENDBLOCK:
788 if (p)
789 return evdev_enable_suspend_block(evdev, client);
790 else
791 return evdev_disable_suspend_block(evdev, client);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700792 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400793
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700794 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400795
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700796 /* Now check variable-length commands */
797#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700798 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400799
Henrik Rydberg85b77202010-12-18 20:51:13 +0100800 case EVIOCGPROP(0):
801 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
802 size, p, compat_mode);
803
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100804 case EVIOCGMTSLOTS(0):
805 return evdev_handle_mt_request(dev, size, ip);
806
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700807 case EVIOCGKEY(0):
808 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400809
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700810 case EVIOCGLED(0):
811 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400812
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700813 case EVIOCGSND(0):
814 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400815
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700816 case EVIOCGSW(0):
817 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400818
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700819 case EVIOCGNAME(0):
820 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400821
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700822 case EVIOCGPHYS(0):
823 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400824
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700825 case EVIOCGUNIQ(0):
826 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400827
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700828 case EVIOC_MASK_SIZE(EVIOCSFF):
829 if (input_ff_effect_from_user(p, size, &effect))
830 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400831
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700832 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400833
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700834 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
835 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400836
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700837 return error;
838 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400839
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700840 /* Multi-number variable-length handlers */
841 if (_IOC_TYPE(cmd) != 'E')
842 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700844 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700846 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
847 return handle_eviocgbit(dev,
848 _IOC_NR(cmd) & EV_MAX, size,
849 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700851 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400852
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700853 if (!dev->absinfo)
854 return -EINVAL;
855
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700856 t = _IOC_NR(cmd) & ABS_MAX;
857 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400858
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700859 if (copy_to_user(p, &abs, min_t(size_t,
860 size, sizeof(struct input_absinfo))))
861 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400862
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700863 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700866
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700867 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700868
869 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
870
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
876 if (copy_from_user(&abs, p, min_t(size_t,
877 size, sizeof(struct input_absinfo))))
878 return -EFAULT;
879
880 if (size < sizeof(struct input_absinfo))
881 abs.resolution = 0;
882
883 /* We can't change number of reserved MT slots */
884 if (t == ABS_MT_SLOT)
885 return -EINVAL;
886
887 /*
888 * Take event lock to ensure that we are not
889 * changing device parameters in the middle
890 * of event.
891 */
892 spin_lock_irq(&dev->event_lock);
893 dev->absinfo[t] = abs;
894 spin_unlock_irq(&dev->event_lock);
895
896 return 0;
897 }
898 }
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return -EINVAL;
901}
902
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400903static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
904 void __user *p, int compat_mode)
905{
906 struct evdev_client *client = file->private_data;
907 struct evdev *evdev = client->evdev;
908 int retval;
909
910 retval = mutex_lock_interruptible(&evdev->mutex);
911 if (retval)
912 return retval;
913
914 if (!evdev->exist) {
915 retval = -ENODEV;
916 goto out;
917 }
918
919 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
920
921 out:
922 mutex_unlock(&evdev->mutex);
923 return retval;
924}
925
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500926static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
927{
928 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
929}
930
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500931#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400932static long evdev_ioctl_compat(struct file *file,
933 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500934{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500935 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500936}
937#endif
938
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400939static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400940 .owner = THIS_MODULE,
941 .read = evdev_read,
942 .write = evdev_write,
943 .poll = evdev_poll,
944 .open = evdev_open,
945 .release = evdev_release,
946 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500947#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400948 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500949#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400950 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200951 .flush = evdev_flush,
952 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953};
954
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400955static int evdev_install_chrdev(struct evdev *evdev)
956{
957 /*
958 * No need to do any locking here as calls to connect and
959 * disconnect are serialized by the input core
960 */
961 evdev_table[evdev->minor] = evdev;
962 return 0;
963}
964
965static void evdev_remove_chrdev(struct evdev *evdev)
966{
967 /*
968 * Lock evdev table to prevent race with evdev_open()
969 */
970 mutex_lock(&evdev_table_mutex);
971 evdev_table[evdev->minor] = NULL;
972 mutex_unlock(&evdev_table_mutex);
973}
974
975/*
976 * Mark device non-existent. This disables writes, ioctls and
977 * prevents new users from opening the device. Already posted
978 * blocking reads will stay, however new ones will fail.
979 */
980static void evdev_mark_dead(struct evdev *evdev)
981{
982 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700983 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400984 mutex_unlock(&evdev->mutex);
985}
986
987static void evdev_cleanup(struct evdev *evdev)
988{
989 struct input_handle *handle = &evdev->handle;
990
991 evdev_mark_dead(evdev);
992 evdev_hangup(evdev);
993 evdev_remove_chrdev(evdev);
994
995 /* evdev is marked dead so no one else accesses evdev->open */
996 if (evdev->open) {
997 input_flush_device(handle, NULL);
998 input_close_device(handle);
999 }
1000}
1001
1002/*
1003 * Create new evdev device. Note that input core serializes calls
1004 * to connect and disconnect so we don't need to lock evdev_table here.
1005 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001006static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1007 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct evdev *evdev;
1010 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001011 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001013 for (minor = 0; minor < EVDEV_MINORS; minor++)
1014 if (!evdev_table[minor])
1015 break;
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -08001018 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001019 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001022 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
1023 if (!evdev)
1024 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001026 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001027 spin_lock_init(&evdev->client_lock);
1028 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 init_waitqueue_head(&evdev->wait);
1030
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001031 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001032 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001034
Dmitry Torokhova7097ff2008-04-01 00:22:53 -04001035 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001036 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 evdev->handle.handler = handler;
1038 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001039
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001040 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001041 evdev->dev.class = &input_class;
1042 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001043 evdev->dev.release = evdev_free;
1044 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001046 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001047 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001048 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001050 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001051 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001052 goto err_unregister_handle;
1053
1054 error = device_add(&evdev->dev);
1055 if (error)
1056 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001057
1058 return 0;
1059
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001060 err_cleanup_evdev:
1061 evdev_cleanup(evdev);
1062 err_unregister_handle:
1063 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001064 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001065 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001066 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069static void evdev_disconnect(struct input_handle *handle)
1070{
1071 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001073 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001074 evdev_cleanup(evdev);
1075 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001076 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001079static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 { .driver_info = 1 }, /* Matches all devices */
1081 { }, /* Terminating zero entry */
1082};
1083
1084MODULE_DEVICE_TABLE(input, evdev_ids);
1085
1086static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001087 .event = evdev_event,
1088 .connect = evdev_connect,
1089 .disconnect = evdev_disconnect,
1090 .fops = &evdev_fops,
1091 .minor = EVDEV_MINOR_BASE,
1092 .name = "evdev",
1093 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094};
1095
1096static int __init evdev_init(void)
1097{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001098 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101static void __exit evdev_exit(void)
1102{
1103 input_unregister_handler(&evdev_handler);
1104}
1105
1106module_init(evdev_init);
1107module_exit(evdev_exit);
1108
1109MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1110MODULE_DESCRIPTION("Input driver event char devices");
1111MODULE_LICENSE("GPL");