blob: a93743879c01fb8c81cb17225a54cfd2032a6d79 [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
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700326 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400327 spin_lock_init(&client->buffer_lock);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700328 snprintf(client->name, sizeof(client->name), "%s-%d",
329 dev_name(&evdev->dev), task_tgid_vnr(current));
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400330 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400331 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400333 error = evdev_open_device(evdev);
334 if (error)
335 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400337 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800338 nonseekable_open(inode, file);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400341
342 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400343 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400344 kfree(client);
345 err_put_evdev:
346 put_device(&evdev->dev);
347 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400350static ssize_t evdev_write(struct file *file, const char __user *buffer,
351 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400353 struct evdev_client *client = file->private_data;
354 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800356 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Peter Korsgaard439581e2011-02-25 09:30:46 -0800358 if (count < input_event_size())
359 return -EINVAL;
360
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400361 retval = mutex_lock_interruptible(&evdev->mutex);
362 if (retval)
363 return retval;
364
365 if (!evdev->exist) {
366 retval = -ENODEV;
367 goto out;
368 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500369
Peter Korsgaard439581e2011-02-25 09:30:46 -0800370 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400371 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400372 retval = -EFAULT;
373 goto out;
374 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800375 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400376
377 input_inject_event(&evdev->handle,
378 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800379 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500380
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400381 out:
382 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500383 return retval;
384}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500385
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400386static int evdev_fetch_next_event(struct evdev_client *client,
387 struct input_event *event)
388{
389 int have_event;
390
391 spin_lock_irq(&client->buffer_lock);
392
Dima Zavin566cf5b2011-12-30 15:16:44 -0800393 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400394 if (have_event) {
395 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700396 client->tail &= client->bufsize - 1;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700397 if (client->use_wake_lock &&
398 client->packet_head == client->tail)
399 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400400 }
401
402 spin_unlock_irq(&client->buffer_lock);
403
404 return have_event;
405}
406
407static ssize_t evdev_read(struct file *file, char __user *buffer,
408 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400410 struct evdev_client *client = file->private_data;
411 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400412 struct input_event event;
Heiko Stübner42f57872012-02-01 09:12:23 -0800413 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400415 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -EINVAL;
417
Dima Zavin509f87c2011-12-30 15:16:44 -0800418 if (!(file->f_flags & O_NONBLOCK)) {
419 retval = wait_event_interruptible(evdev->wait,
420 client->packet_head != client->tail ||
421 !evdev->exist);
422 if (retval)
423 return retval;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400426 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -ENODEV;
428
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400429 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400430 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500431
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400432 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500433 return -EFAULT;
434
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400435 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
Dima Zavine90f8692011-12-30 15:16:44 -0800438 if (retval == 0 && (file->f_flags & O_NONBLOCK))
439 return -EAGAIN;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return retval;
442}
443
444/* No kernel lock - fine */
445static unsigned int evdev_poll(struct file *file, poll_table *wait)
446{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400447 struct evdev_client *client = file->private_data;
448 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700449 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400450
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400451 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700452
453 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700454 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700455 mask |= POLLIN | POLLRDNORM;
456
457 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500460#ifdef CONFIG_COMPAT
461
462#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700463#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500464
465#ifdef __BIG_ENDIAN
466static int bits_to_user(unsigned long *bits, unsigned int maxbit,
467 unsigned int maxlen, void __user *p, int compat)
468{
469 int len, i;
470
471 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700472 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400473 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500474 len = maxlen;
475
476 for (i = 0; i < len / sizeof(compat_long_t); i++)
477 if (copy_to_user((compat_long_t __user *) p + i,
478 (compat_long_t *) bits +
479 i + 1 - ((i % 2) << 1),
480 sizeof(compat_long_t)))
481 return -EFAULT;
482 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700483 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500484 if (len > maxlen)
485 len = maxlen;
486
487 if (copy_to_user(p, bits, len))
488 return -EFAULT;
489 }
490
491 return len;
492}
493#else
494static int bits_to_user(unsigned long *bits, unsigned int maxbit,
495 unsigned int maxlen, void __user *p, int compat)
496{
497 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700498 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
499 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500500
501 if (len > maxlen)
502 len = maxlen;
503
504 return copy_to_user(p, bits, len) ? -EFAULT : len;
505}
506#endif /* __BIG_ENDIAN */
507
508#else
509
510static int bits_to_user(unsigned long *bits, unsigned int maxbit,
511 unsigned int maxlen, void __user *p, int compat)
512{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700513 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500514
515 if (len > maxlen)
516 len = maxlen;
517
518 return copy_to_user(p, bits, len) ? -EFAULT : len;
519}
520
521#endif /* CONFIG_COMPAT */
522
523static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
524{
525 int len;
526
527 if (!str)
528 return -ENOENT;
529
530 len = strlen(str) + 1;
531 if (len > maxlen)
532 len = maxlen;
533
534 return copy_to_user(p, str, len) ? -EFAULT : len;
535}
536
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400537#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700538static int handle_eviocgbit(struct input_dev *dev,
539 unsigned int type, unsigned int size,
540 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400541{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400542 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400543 unsigned long *bits;
544 int len;
545
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700546 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400547
548 case 0: bits = dev->evbit; len = EV_MAX; break;
549 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
550 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
551 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
552 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
553 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
554 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
555 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
556 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
557 default: return -EINVAL;
558 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400559
560 /*
561 * Work around bugs in userspace programs that like to do
562 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
563 * should be in bytes, not in bits.
564 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700565 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400566 len = OLD_KEY_MAX;
567 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800568 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
569 "limiting output to %zu bytes. See "
570 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
571 OLD_KEY_MAX,
572 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400573 }
574
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700575 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400576}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400577#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400578
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800579static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
580{
581 struct input_keymap_entry ke = {
582 .len = sizeof(unsigned int),
583 .flags = 0,
584 };
585 int __user *ip = (int __user *)p;
586 int error;
587
588 /* legacy case */
589 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
590 return -EFAULT;
591
592 error = input_get_keycode(dev, &ke);
593 if (error)
594 return error;
595
596 if (put_user(ke.keycode, ip + 1))
597 return -EFAULT;
598
599 return 0;
600}
601
602static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700603{
604 struct input_keymap_entry ke;
605 int error;
606
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800607 if (copy_from_user(&ke, p, sizeof(ke)))
608 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700609
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800610 error = input_get_keycode(dev, &ke);
611 if (error)
612 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700613
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800614 if (copy_to_user(p, &ke, sizeof(ke)))
615 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700616
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700617 return 0;
618}
619
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800620static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
621{
622 struct input_keymap_entry ke = {
623 .len = sizeof(unsigned int),
624 .flags = 0,
625 };
626 int __user *ip = (int __user *)p;
627
628 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
629 return -EFAULT;
630
631 if (get_user(ke.keycode, ip + 1))
632 return -EFAULT;
633
634 return input_set_keycode(dev, &ke);
635}
636
637static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700638{
639 struct input_keymap_entry ke;
640
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800641 if (copy_from_user(&ke, p, sizeof(ke)))
642 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700643
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800644 if (ke.len > sizeof(ke.scancode))
645 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700646
647 return input_set_keycode(dev, &ke);
648}
649
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100650static int evdev_handle_mt_request(struct input_dev *dev,
651 unsigned int size,
652 int __user *ip)
653{
654 const struct input_mt_slot *mt = dev->mt;
655 unsigned int code;
656 int max_slots;
657 int i;
658
659 if (get_user(code, &ip[0]))
660 return -EFAULT;
661 if (!input_is_mt_value(code))
662 return -EINVAL;
663
664 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
665 for (i = 0; i < dev->mtsize && i < max_slots; i++)
666 if (put_user(input_mt_get_value(&mt[i], code), &ip[1 + i]))
667 return -EFAULT;
668
669 return 0;
670}
671
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700672static int evdev_enable_suspend_block(struct evdev *evdev,
673 struct evdev_client *client)
674{
675 if (client->use_wake_lock)
676 return 0;
677
678 spin_lock_irq(&client->buffer_lock);
679 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
680 client->use_wake_lock = true;
681 if (client->packet_head != client->tail)
682 wake_lock(&client->wake_lock);
683 spin_unlock_irq(&client->buffer_lock);
684 return 0;
685}
686
687static int evdev_disable_suspend_block(struct evdev *evdev,
688 struct evdev_client *client)
689{
690 if (!client->use_wake_lock)
691 return 0;
692
693 spin_lock_irq(&client->buffer_lock);
694 client->use_wake_lock = false;
695 wake_lock_destroy(&client->wake_lock);
696 spin_unlock_irq(&client->buffer_lock);
697
698 return 0;
699}
700
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400701static long evdev_do_ioctl(struct file *file, unsigned int cmd,
702 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400704 struct evdev_client *client = file->private_data;
705 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct input_dev *dev = evdev->handle.dev;
707 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400708 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500709 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800710 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700711 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400712 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700714 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 switch (cmd) {
716
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400717 case EVIOCGVERSION:
718 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400720 case EVIOCGID:
721 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
722 return -EFAULT;
723 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400724
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400725 case EVIOCGREP:
726 if (!test_bit(EV_REP, dev->evbit))
727 return -ENOSYS;
728 if (put_user(dev->rep[REP_DELAY], ip))
729 return -EFAULT;
730 if (put_user(dev->rep[REP_PERIOD], ip + 1))
731 return -EFAULT;
732 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400733
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400734 case EVIOCSREP:
735 if (!test_bit(EV_REP, dev->evbit))
736 return -ENOSYS;
737 if (get_user(u, ip))
738 return -EFAULT;
739 if (get_user(v, ip + 1))
740 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400741
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400742 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
743 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500744
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400747 case EVIOCRMFF:
748 return input_ff_erase(dev, (int)(unsigned long) p, file);
749
750 case EVIOCGEFFECTS:
751 i = test_bit(EV_FF, dev->evbit) ?
752 dev->ff->max_effects : 0;
753 if (put_user(i, ip))
754 return -EFAULT;
755 return 0;
756
757 case EVIOCGRAB:
758 if (p)
759 return evdev_grab(evdev, client);
760 else
761 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800762
John Stultza80b83b2012-02-03 00:19:07 -0800763 case EVIOCSCLOCKID:
764 if (copy_from_user(&i, p, sizeof(unsigned int)))
765 return -EFAULT;
766 if (i != CLOCK_MONOTONIC && i != CLOCK_REALTIME)
767 return -EINVAL;
768 client->clkid = i;
769 return 0;
770
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800771 case EVIOCGKEYCODE:
772 return evdev_handle_get_keycode(dev, p);
773
774 case EVIOCSKEYCODE:
775 return evdev_handle_set_keycode(dev, p);
776
777 case EVIOCGKEYCODE_V2:
778 return evdev_handle_get_keycode_v2(dev, p);
779
780 case EVIOCSKEYCODE_V2:
781 return evdev_handle_set_keycode_v2(dev, p);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700782
783 case EVIOCGSUSPENDBLOCK:
784 return put_user(client->use_wake_lock, ip);
785
786 case EVIOCSSUSPENDBLOCK:
787 if (p)
788 return evdev_enable_suspend_block(evdev, client);
789 else
790 return evdev_disable_suspend_block(evdev, client);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700791 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400792
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700793 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400794
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700795 /* Now check variable-length commands */
796#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700797 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400798
Henrik Rydberg85b77202010-12-18 20:51:13 +0100799 case EVIOCGPROP(0):
800 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
801 size, p, compat_mode);
802
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100803 case EVIOCGMTSLOTS(0):
804 return evdev_handle_mt_request(dev, size, ip);
805
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700806 case EVIOCGKEY(0):
807 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400808
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700809 case EVIOCGLED(0):
810 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400811
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700812 case EVIOCGSND(0):
813 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400814
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700815 case EVIOCGSW(0):
816 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400817
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700818 case EVIOCGNAME(0):
819 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400820
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700821 case EVIOCGPHYS(0):
822 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400823
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700824 case EVIOCGUNIQ(0):
825 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400826
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700827 case EVIOC_MASK_SIZE(EVIOCSFF):
828 if (input_ff_effect_from_user(p, size, &effect))
829 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400830
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700831 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400832
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700833 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
834 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400835
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700836 return error;
837 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400838
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700839 /* Multi-number variable-length handlers */
840 if (_IOC_TYPE(cmd) != 'E')
841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700843 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700845 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
846 return handle_eviocgbit(dev,
847 _IOC_NR(cmd) & EV_MAX, size,
848 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700850 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400851
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700852 if (!dev->absinfo)
853 return -EINVAL;
854
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700855 t = _IOC_NR(cmd) & ABS_MAX;
856 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400857
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700858 if (copy_to_user(p, &abs, min_t(size_t,
859 size, sizeof(struct input_absinfo))))
860 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400861
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700862 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700865
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700866 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700867
868 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
869
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700870 if (!dev->absinfo)
871 return -EINVAL;
872
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700873 t = _IOC_NR(cmd) & ABS_MAX;
874
875 if (copy_from_user(&abs, p, min_t(size_t,
876 size, sizeof(struct input_absinfo))))
877 return -EFAULT;
878
879 if (size < sizeof(struct input_absinfo))
880 abs.resolution = 0;
881
882 /* We can't change number of reserved MT slots */
883 if (t == ABS_MT_SLOT)
884 return -EINVAL;
885
886 /*
887 * Take event lock to ensure that we are not
888 * changing device parameters in the middle
889 * of event.
890 */
891 spin_lock_irq(&dev->event_lock);
892 dev->absinfo[t] = abs;
893 spin_unlock_irq(&dev->event_lock);
894
895 return 0;
896 }
897 }
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -EINVAL;
900}
901
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400902static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
903 void __user *p, int compat_mode)
904{
905 struct evdev_client *client = file->private_data;
906 struct evdev *evdev = client->evdev;
907 int retval;
908
909 retval = mutex_lock_interruptible(&evdev->mutex);
910 if (retval)
911 return retval;
912
913 if (!evdev->exist) {
914 retval = -ENODEV;
915 goto out;
916 }
917
918 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
919
920 out:
921 mutex_unlock(&evdev->mutex);
922 return retval;
923}
924
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500925static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
926{
927 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
928}
929
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500930#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400931static long evdev_ioctl_compat(struct file *file,
932 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500933{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500934 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500935}
936#endif
937
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400938static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400939 .owner = THIS_MODULE,
940 .read = evdev_read,
941 .write = evdev_write,
942 .poll = evdev_poll,
943 .open = evdev_open,
944 .release = evdev_release,
945 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500946#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400947 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500948#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400949 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200950 .flush = evdev_flush,
951 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952};
953
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400954static int evdev_install_chrdev(struct evdev *evdev)
955{
956 /*
957 * No need to do any locking here as calls to connect and
958 * disconnect are serialized by the input core
959 */
960 evdev_table[evdev->minor] = evdev;
961 return 0;
962}
963
964static void evdev_remove_chrdev(struct evdev *evdev)
965{
966 /*
967 * Lock evdev table to prevent race with evdev_open()
968 */
969 mutex_lock(&evdev_table_mutex);
970 evdev_table[evdev->minor] = NULL;
971 mutex_unlock(&evdev_table_mutex);
972}
973
974/*
975 * Mark device non-existent. This disables writes, ioctls and
976 * prevents new users from opening the device. Already posted
977 * blocking reads will stay, however new ones will fail.
978 */
979static void evdev_mark_dead(struct evdev *evdev)
980{
981 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700982 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400983 mutex_unlock(&evdev->mutex);
984}
985
986static void evdev_cleanup(struct evdev *evdev)
987{
988 struct input_handle *handle = &evdev->handle;
989
990 evdev_mark_dead(evdev);
991 evdev_hangup(evdev);
992 evdev_remove_chrdev(evdev);
993
994 /* evdev is marked dead so no one else accesses evdev->open */
995 if (evdev->open) {
996 input_flush_device(handle, NULL);
997 input_close_device(handle);
998 }
999}
1000
1001/*
1002 * Create new evdev device. Note that input core serializes calls
1003 * to connect and disconnect so we don't need to lock evdev_table here.
1004 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001005static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1006 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct evdev *evdev;
1009 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001010 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001012 for (minor = 0; minor < EVDEV_MINORS; minor++)
1013 if (!evdev_table[minor])
1014 break;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -08001017 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001018 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001021 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
1022 if (!evdev)
1023 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001025 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001026 spin_lock_init(&evdev->client_lock);
1027 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 init_waitqueue_head(&evdev->wait);
1029
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001030 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001031 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001033
Dmitry Torokhova7097ff2008-04-01 00:22:53 -04001034 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001035 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 evdev->handle.handler = handler;
1037 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001038
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001039 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001040 evdev->dev.class = &input_class;
1041 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001042 evdev->dev.release = evdev_free;
1043 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001045 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001046 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001047 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001049 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001050 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001051 goto err_unregister_handle;
1052
1053 error = device_add(&evdev->dev);
1054 if (error)
1055 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001056
1057 return 0;
1058
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001059 err_cleanup_evdev:
1060 evdev_cleanup(evdev);
1061 err_unregister_handle:
1062 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001063 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001064 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001065 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068static void evdev_disconnect(struct input_handle *handle)
1069{
1070 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001072 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001073 evdev_cleanup(evdev);
1074 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001075 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001078static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 { .driver_info = 1 }, /* Matches all devices */
1080 { }, /* Terminating zero entry */
1081};
1082
1083MODULE_DEVICE_TABLE(input, evdev_ids);
1084
1085static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001086 .event = evdev_event,
1087 .connect = evdev_connect,
1088 .disconnect = evdev_disconnect,
1089 .fops = &evdev_fops,
1090 .minor = EVDEV_MINOR_BASE,
1091 .name = "evdev",
1092 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093};
1094
1095static int __init evdev_init(void)
1096{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001097 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100static void __exit evdev_exit(void)
1101{
1102 input_unregister_handler(&evdev_handler);
1103}
1104
1105module_init(evdev_init);
1106module_exit(evdev_exit);
1107
1108MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1109MODULE_DESCRIPTION("Input driver event char devices");
1110MODULE_LICENSE("GPL");