blob: 6288d7d84fa7e061d9ad3967e37782fbc4a17c24 [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>
23#include <linux/input.h>
24#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/device.h>
Arve Hjønnevågca5d75f2008-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ågca5d75f2008-10-17 15:20:55 -070047 struct wake_lock wake_lock;
Arve Hjønnevåg4dc43d72008-10-17 15:20:55 -070048 bool use_wake_lock;
Mike Chan2a1f3522009-10-27 17:37:44 -070049 char name[28];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct fasync_struct *fasync;
51 struct evdev *evdev;
52 struct list_head node;
Jeff Brown9fb0f142011-04-12 23:29:38 -070053 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070054 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040058static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040060static void evdev_pass_event(struct evdev_client *client,
61 struct input_event *event)
62{
Jeff Brown9fb0f142011-04-12 23:29:38 -070063 /* Interrupts are disabled, just acquire the lock. */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040064 spin_lock(&client->buffer_lock);
Jeff Brown9fb0f142011-04-12 23:29:38 -070065
66 client->buffer[client->head++] = *event;
67 client->head &= client->bufsize - 1;
68
69 if (unlikely(client->head == client->tail)) {
70 /*
71 * This effectively "drops" all unconsumed events, leaving
72 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
73 */
74 client->tail = (client->head - 2) & (client->bufsize - 1);
75
76 client->buffer[client->tail].time = event->time;
77 client->buffer[client->tail].type = EV_SYN;
78 client->buffer[client->tail].code = SYN_DROPPED;
79 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -070080
81 client->packet_head = client->tail;
Arve Hjønnevåg4dc43d72008-10-17 15:20:55 -070082 if (client->use_wake_lock)
83 wake_unlock(&client->wake_lock);
Jeff Browncdda9112011-04-26 22:16:11 -070084 }
85
86 if (event->type == EV_SYN && event->code == SYN_REPORT) {
87 client->packet_head = client->head;
Arve Hjønnevåg4dc43d72008-10-17 15:20:55 -070088 if (client->use_wake_lock)
89 wake_lock(&client->wake_lock);
Jeff Browncdda9112011-04-26 22:16:11 -070090 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -070091 }
92
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040093 spin_unlock(&client->buffer_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040094}
95
96/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040097 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040098 */
99static void evdev_event(struct input_handle *handle,
100 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400103 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400104 struct input_event event;
Arve Hjønnevåg5d618872008-10-17 15:28:08 -0700105 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Arve Hjønnevåg5d618872008-10-17 15:28:08 -0700107 ktime_get_ts(&ts);
108 event.time.tv_sec = ts.tv_sec;
109 event.time.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400110 event.type = type;
111 event.code = code;
112 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400114 rcu_read_lock();
115
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400116 client = rcu_dereference(evdev->grab);
117 if (client)
118 evdev_pass_event(client, &event);
119 else
120 list_for_each_entry_rcu(client, &evdev->client_list, node)
121 evdev_pass_event(client, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400123 rcu_read_unlock();
124
Dmitry Torokhovda40b0b2011-06-18 02:50:11 -0700125 if (type == EV_SYN && code == SYN_REPORT)
126 wake_up_interruptible(&evdev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static int evdev_fasync(int fd, struct file *file, int on)
130{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400131 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400132
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700133 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400136static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400138 struct evdev_client *client = file->private_data;
139 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400140 int retval;
141
142 retval = mutex_lock_interruptible(&evdev->mutex);
143 if (retval)
144 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400145
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400146 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400147 retval = -ENODEV;
148 else
149 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400150
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400151 mutex_unlock(&evdev->mutex);
152 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400155static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400157 struct evdev *evdev = container_of(dev, struct evdev, dev);
158
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400159 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 kfree(evdev);
161}
162
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400163/*
164 * Grabs an event device (along with underlying input device).
165 * This function is called with evdev->mutex taken.
166 */
167static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
168{
169 int error;
170
171 if (evdev->grab)
172 return -EBUSY;
173
174 error = input_grab_device(&evdev->handle);
175 if (error)
176 return error;
177
178 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400179
180 return 0;
181}
182
183static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
184{
185 if (evdev->grab != client)
186 return -EINVAL;
187
188 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400189 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400190 input_release_device(&evdev->handle);
191
192 return 0;
193}
194
195static void evdev_attach_client(struct evdev *evdev,
196 struct evdev_client *client)
197{
198 spin_lock(&evdev->client_lock);
199 list_add_tail_rcu(&client->node, &evdev->client_list);
200 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400201}
202
203static void evdev_detach_client(struct evdev *evdev,
204 struct evdev_client *client)
205{
206 spin_lock(&evdev->client_lock);
207 list_del_rcu(&client->node);
208 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400209 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400210}
211
212static int evdev_open_device(struct evdev *evdev)
213{
214 int retval;
215
216 retval = mutex_lock_interruptible(&evdev->mutex);
217 if (retval)
218 return retval;
219
220 if (!evdev->exist)
221 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400222 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400223 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400224 if (retval)
225 evdev->open--;
226 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400227
228 mutex_unlock(&evdev->mutex);
229 return retval;
230}
231
232static void evdev_close_device(struct evdev *evdev)
233{
234 mutex_lock(&evdev->mutex);
235
236 if (evdev->exist && !--evdev->open)
237 input_close_device(&evdev->handle);
238
239 mutex_unlock(&evdev->mutex);
240}
241
242/*
243 * Wake up users waiting for IO so they can disconnect from
244 * dead device.
245 */
246static void evdev_hangup(struct evdev *evdev)
247{
248 struct evdev_client *client;
249
250 spin_lock(&evdev->client_lock);
251 list_for_each_entry(client, &evdev->client_list, node)
252 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
253 spin_unlock(&evdev->client_lock);
254
255 wake_up_interruptible(&evdev->wait);
256}
257
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400258static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400260 struct evdev_client *client = file->private_data;
261 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400263 mutex_lock(&evdev->mutex);
264 if (evdev->grab == client)
265 evdev_ungrab(evdev, client);
266 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400268 evdev_detach_client(evdev, client);
Arve Hjønnevåg4dc43d72008-10-17 15:20:55 -0700269 if (client->use_wake_lock)
270 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400271 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400273 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400274 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277}
278
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700279static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
280{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700281 unsigned int n_events =
282 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
283 EVDEV_MIN_BUFFER_SIZE);
284
285 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700286}
287
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400288static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400290 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400291 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700293 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400294 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400296 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return -ENODEV;
298
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400299 error = mutex_lock_interruptible(&evdev_table_mutex);
300 if (error)
301 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400302 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400303 if (evdev)
304 get_device(&evdev->dev);
305 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400306
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400307 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400308 return -ENODEV;
309
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700310 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
311
312 client = kzalloc(sizeof(struct evdev_client) +
313 bufsize * sizeof(struct input_event),
314 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400315 if (!client) {
316 error = -ENOMEM;
317 goto err_put_evdev;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700320 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400321 spin_lock_init(&client->buffer_lock);
Mike Chan2a1f3522009-10-27 17:37:44 -0700322 snprintf(client->name, sizeof(client->name), "%s-%d",
323 dev_name(&evdev->dev), task_tgid_vnr(current));
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400324 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400325 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400327 error = evdev_open_device(evdev);
328 if (error)
329 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400331 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800332 nonseekable_open(inode, file);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400335
336 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400337 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400338 kfree(client);
339 err_put_evdev:
340 put_device(&evdev->dev);
341 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400344static ssize_t evdev_write(struct file *file, const char __user *buffer,
345 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400347 struct evdev_client *client = file->private_data;
348 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct input_event event;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400350 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Peter Korsgaard439581e2011-02-25 09:30:46 -0800352 if (count < input_event_size())
353 return -EINVAL;
354
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400355 retval = mutex_lock_interruptible(&evdev->mutex);
356 if (retval)
357 return retval;
358
359 if (!evdev->exist) {
360 retval = -ENODEV;
361 goto out;
362 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500363
Peter Korsgaard439581e2011-02-25 09:30:46 -0800364 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400365 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400366 retval = -EFAULT;
367 goto out;
368 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800369 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400370
371 input_inject_event(&evdev->handle,
372 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800373 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500374
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400375 out:
376 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500377 return retval;
378}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500379
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400380static int evdev_fetch_next_event(struct evdev_client *client,
381 struct input_event *event)
382{
383 int have_event;
384
385 spin_lock_irq(&client->buffer_lock);
386
Dima Zavin48731932011-10-04 16:58:03 -0700387 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400388 if (have_event) {
389 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700390 client->tail &= client->bufsize - 1;
Arve Hjønnevåg4dc43d72008-10-17 15:20:55 -0700391 if (client->use_wake_lock &&
392 client->packet_head == client->tail)
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700393 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400394 }
395
396 spin_unlock_irq(&client->buffer_lock);
397
398 return have_event;
399}
400
401static ssize_t evdev_read(struct file *file, char __user *buffer,
402 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400404 struct evdev_client *client = file->private_data;
405 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400406 struct input_event event;
Heiko Stuebner1f8c5ce2012-02-01 10:33:01 -0800407 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400409 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -EINVAL;
411
Dima Zavin183a1fc2011-10-04 17:10:45 -0700412 if (!(file->f_flags & O_NONBLOCK)) {
413 retval = wait_event_interruptible(evdev->wait,
414 client->packet_head != client->tail || !evdev->exist);
415 if (retval)
416 return retval;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400419 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -ENODEV;
421
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400422 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400423 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500424
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400425 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500426 return -EFAULT;
427
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400428 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
Dima Zavin587b1eb2011-10-04 22:31:48 -0700431 if (retval == 0 && file->f_flags & O_NONBLOCK)
432 retval = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return retval;
434}
435
436/* No kernel lock - fine */
437static unsigned int evdev_poll(struct file *file, poll_table *wait)
438{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400439 struct evdev_client *client = file->private_data;
440 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700441 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400442
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400443 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700444
445 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700446 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700447 mask |= POLLIN | POLLRDNORM;
448
449 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500452#ifdef CONFIG_COMPAT
453
454#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700455#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500456
457#ifdef __BIG_ENDIAN
458static int bits_to_user(unsigned long *bits, unsigned int maxbit,
459 unsigned int maxlen, void __user *p, int compat)
460{
461 int len, i;
462
463 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700464 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400465 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500466 len = maxlen;
467
468 for (i = 0; i < len / sizeof(compat_long_t); i++)
469 if (copy_to_user((compat_long_t __user *) p + i,
470 (compat_long_t *) bits +
471 i + 1 - ((i % 2) << 1),
472 sizeof(compat_long_t)))
473 return -EFAULT;
474 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700475 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500476 if (len > maxlen)
477 len = maxlen;
478
479 if (copy_to_user(p, bits, len))
480 return -EFAULT;
481 }
482
483 return len;
484}
485#else
486static int bits_to_user(unsigned long *bits, unsigned int maxbit,
487 unsigned int maxlen, void __user *p, int compat)
488{
489 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700490 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
491 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500492
493 if (len > maxlen)
494 len = maxlen;
495
496 return copy_to_user(p, bits, len) ? -EFAULT : len;
497}
498#endif /* __BIG_ENDIAN */
499
500#else
501
502static int bits_to_user(unsigned long *bits, unsigned int maxbit,
503 unsigned int maxlen, void __user *p, int compat)
504{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700505 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500506
507 if (len > maxlen)
508 len = maxlen;
509
510 return copy_to_user(p, bits, len) ? -EFAULT : len;
511}
512
513#endif /* CONFIG_COMPAT */
514
515static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
516{
517 int len;
518
519 if (!str)
520 return -ENOENT;
521
522 len = strlen(str) + 1;
523 if (len > maxlen)
524 len = maxlen;
525
526 return copy_to_user(p, str, len) ? -EFAULT : len;
527}
528
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400529#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700530static int handle_eviocgbit(struct input_dev *dev,
531 unsigned int type, unsigned int size,
532 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400533{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400534 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400535 unsigned long *bits;
536 int len;
537
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700538 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400539
540 case 0: bits = dev->evbit; len = EV_MAX; break;
541 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
542 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
543 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
544 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
545 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
546 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
547 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
548 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
549 default: return -EINVAL;
550 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400551
552 /*
553 * Work around bugs in userspace programs that like to do
554 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
555 * should be in bytes, not in bits.
556 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700557 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400558 len = OLD_KEY_MAX;
559 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800560 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
561 "limiting output to %zu bytes. See "
562 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
563 OLD_KEY_MAX,
564 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400565 }
566
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700567 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400568}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400569#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400570
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800571static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
572{
573 struct input_keymap_entry ke = {
574 .len = sizeof(unsigned int),
575 .flags = 0,
576 };
577 int __user *ip = (int __user *)p;
578 int error;
579
580 /* legacy case */
581 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
582 return -EFAULT;
583
584 error = input_get_keycode(dev, &ke);
585 if (error)
586 return error;
587
588 if (put_user(ke.keycode, ip + 1))
589 return -EFAULT;
590
591 return 0;
592}
593
594static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700595{
596 struct input_keymap_entry ke;
597 int error;
598
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800599 if (copy_from_user(&ke, p, sizeof(ke)))
600 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700601
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800602 error = input_get_keycode(dev, &ke);
603 if (error)
604 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700605
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800606 if (copy_to_user(p, &ke, sizeof(ke)))
607 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700608
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700609 return 0;
610}
611
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800612static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
613{
614 struct input_keymap_entry ke = {
615 .len = sizeof(unsigned int),
616 .flags = 0,
617 };
618 int __user *ip = (int __user *)p;
619
620 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
621 return -EFAULT;
622
623 if (get_user(ke.keycode, ip + 1))
624 return -EFAULT;
625
626 return input_set_keycode(dev, &ke);
627}
628
629static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700630{
631 struct input_keymap_entry ke;
632
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800633 if (copy_from_user(&ke, p, sizeof(ke)))
634 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700635
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800636 if (ke.len > sizeof(ke.scancode))
637 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700638
639 return input_set_keycode(dev, &ke);
640}
641
Arve Hjønnevåg4dc43d72008-10-17 15:20:55 -0700642static int evdev_enable_suspend_block(struct evdev *evdev,
643 struct evdev_client *client)
644{
645 if (client->use_wake_lock)
646 return 0;
647
648 spin_lock_irq(&client->buffer_lock);
649 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
650 client->use_wake_lock = true;
651 if (client->packet_head != client->tail)
652 wake_lock(&client->wake_lock);
653 spin_unlock_irq(&client->buffer_lock);
654 return 0;
655}
656
657static int evdev_disable_suspend_block(struct evdev *evdev,
658 struct evdev_client *client)
659{
660 if (!client->use_wake_lock)
661 return 0;
662
663 spin_lock_irq(&client->buffer_lock);
664 client->use_wake_lock = false;
665 wake_lock_destroy(&client->wake_lock);
666 spin_unlock_irq(&client->buffer_lock);
667
668 return 0;
669}
670
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400671static long evdev_do_ioctl(struct file *file, unsigned int cmd,
672 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400674 struct evdev_client *client = file->private_data;
675 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct input_dev *dev = evdev->handle.dev;
677 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400678 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500679 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800680 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700681 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400682 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700684 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 switch (cmd) {
686
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400687 case EVIOCGVERSION:
688 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400690 case EVIOCGID:
691 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
692 return -EFAULT;
693 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400694
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400695 case EVIOCGREP:
696 if (!test_bit(EV_REP, dev->evbit))
697 return -ENOSYS;
698 if (put_user(dev->rep[REP_DELAY], ip))
699 return -EFAULT;
700 if (put_user(dev->rep[REP_PERIOD], ip + 1))
701 return -EFAULT;
702 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400703
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400704 case EVIOCSREP:
705 if (!test_bit(EV_REP, dev->evbit))
706 return -ENOSYS;
707 if (get_user(u, ip))
708 return -EFAULT;
709 if (get_user(v, ip + 1))
710 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400711
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400712 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
713 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500714
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400717 case EVIOCRMFF:
718 return input_ff_erase(dev, (int)(unsigned long) p, file);
719
720 case EVIOCGEFFECTS:
721 i = test_bit(EV_FF, dev->evbit) ?
722 dev->ff->max_effects : 0;
723 if (put_user(i, ip))
724 return -EFAULT;
725 return 0;
726
727 case EVIOCGRAB:
728 if (p)
729 return evdev_grab(evdev, client);
730 else
731 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800732
733 case EVIOCGKEYCODE:
734 return evdev_handle_get_keycode(dev, p);
735
736 case EVIOCSKEYCODE:
737 return evdev_handle_set_keycode(dev, p);
738
739 case EVIOCGKEYCODE_V2:
740 return evdev_handle_get_keycode_v2(dev, p);
741
742 case EVIOCSKEYCODE_V2:
743 return evdev_handle_set_keycode_v2(dev, p);
Arve Hjønnevåg4dc43d72008-10-17 15:20:55 -0700744
745 case EVIOCGSUSPENDBLOCK:
746 return put_user(client->use_wake_lock, ip);
747
748 case EVIOCSSUSPENDBLOCK:
749 if (p)
750 return evdev_enable_suspend_block(evdev, client);
751 else
752 return evdev_disable_suspend_block(evdev, client);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700753 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400754
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700755 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400756
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700757 /* Now check variable-length commands */
758#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700759 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400760
Henrik Rydberg85b77202010-12-18 20:51:13 +0100761 case EVIOCGPROP(0):
762 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
763 size, p, compat_mode);
764
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700765 case EVIOCGKEY(0):
766 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400767
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700768 case EVIOCGLED(0):
769 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400770
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700771 case EVIOCGSND(0):
772 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400773
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700774 case EVIOCGSW(0):
775 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400776
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700777 case EVIOCGNAME(0):
778 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400779
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700780 case EVIOCGPHYS(0):
781 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400782
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700783 case EVIOCGUNIQ(0):
784 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400785
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700786 case EVIOC_MASK_SIZE(EVIOCSFF):
787 if (input_ff_effect_from_user(p, size, &effect))
788 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400789
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700790 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400791
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700792 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
793 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400794
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700795 return error;
796 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400797
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700798 /* Multi-number variable-length handlers */
799 if (_IOC_TYPE(cmd) != 'E')
800 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700802 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700804 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
805 return handle_eviocgbit(dev,
806 _IOC_NR(cmd) & EV_MAX, size,
807 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700809 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400810
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700811 if (!dev->absinfo)
812 return -EINVAL;
813
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700814 t = _IOC_NR(cmd) & ABS_MAX;
815 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400816
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700817 if (copy_to_user(p, &abs, min_t(size_t,
818 size, sizeof(struct input_absinfo))))
819 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400820
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700821 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700824
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700825 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700826
827 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
828
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700829 if (!dev->absinfo)
830 return -EINVAL;
831
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700832 t = _IOC_NR(cmd) & ABS_MAX;
833
834 if (copy_from_user(&abs, p, min_t(size_t,
835 size, sizeof(struct input_absinfo))))
836 return -EFAULT;
837
838 if (size < sizeof(struct input_absinfo))
839 abs.resolution = 0;
840
841 /* We can't change number of reserved MT slots */
842 if (t == ABS_MT_SLOT)
843 return -EINVAL;
844
845 /*
846 * Take event lock to ensure that we are not
847 * changing device parameters in the middle
848 * of event.
849 */
850 spin_lock_irq(&dev->event_lock);
851 dev->absinfo[t] = abs;
852 spin_unlock_irq(&dev->event_lock);
853
854 return 0;
855 }
856 }
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EINVAL;
859}
860
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400861static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
862 void __user *p, int compat_mode)
863{
864 struct evdev_client *client = file->private_data;
865 struct evdev *evdev = client->evdev;
866 int retval;
867
868 retval = mutex_lock_interruptible(&evdev->mutex);
869 if (retval)
870 return retval;
871
872 if (!evdev->exist) {
873 retval = -ENODEV;
874 goto out;
875 }
876
877 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
878
879 out:
880 mutex_unlock(&evdev->mutex);
881 return retval;
882}
883
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500884static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
885{
886 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
887}
888
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500889#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400890static long evdev_ioctl_compat(struct file *file,
891 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500892{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500893 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500894}
895#endif
896
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400897static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400898 .owner = THIS_MODULE,
899 .read = evdev_read,
900 .write = evdev_write,
901 .poll = evdev_poll,
902 .open = evdev_open,
903 .release = evdev_release,
904 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500905#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400906 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500907#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400908 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200909 .flush = evdev_flush,
910 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911};
912
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400913static int evdev_install_chrdev(struct evdev *evdev)
914{
915 /*
916 * No need to do any locking here as calls to connect and
917 * disconnect are serialized by the input core
918 */
919 evdev_table[evdev->minor] = evdev;
920 return 0;
921}
922
923static void evdev_remove_chrdev(struct evdev *evdev)
924{
925 /*
926 * Lock evdev table to prevent race with evdev_open()
927 */
928 mutex_lock(&evdev_table_mutex);
929 evdev_table[evdev->minor] = NULL;
930 mutex_unlock(&evdev_table_mutex);
931}
932
933/*
934 * Mark device non-existent. This disables writes, ioctls and
935 * prevents new users from opening the device. Already posted
936 * blocking reads will stay, however new ones will fail.
937 */
938static void evdev_mark_dead(struct evdev *evdev)
939{
940 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700941 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400942 mutex_unlock(&evdev->mutex);
943}
944
945static void evdev_cleanup(struct evdev *evdev)
946{
947 struct input_handle *handle = &evdev->handle;
948
949 evdev_mark_dead(evdev);
950 evdev_hangup(evdev);
951 evdev_remove_chrdev(evdev);
952
953 /* evdev is marked dead so no one else accesses evdev->open */
954 if (evdev->open) {
955 input_flush_device(handle, NULL);
956 input_close_device(handle);
957 }
958}
959
960/*
961 * Create new evdev device. Note that input core serializes calls
962 * to connect and disconnect so we don't need to lock evdev_table here.
963 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400964static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
965 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 struct evdev *evdev;
968 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400969 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400971 for (minor = 0; minor < EVDEV_MINORS; minor++)
972 if (!evdev_table[minor])
973 break;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -0800976 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400977 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400980 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
981 if (!evdev)
982 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400984 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400985 spin_lock_init(&evdev->client_lock);
986 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 init_waitqueue_head(&evdev->wait);
988
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700989 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700990 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400992
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400993 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700994 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 evdev->handle.handler = handler;
996 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400997
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400998 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400999 evdev->dev.class = &input_class;
1000 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001001 evdev->dev.release = evdev_free;
1002 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001004 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001005 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001006 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001008 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001009 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001010 goto err_unregister_handle;
1011
1012 error = device_add(&evdev->dev);
1013 if (error)
1014 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001015
1016 return 0;
1017
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001018 err_cleanup_evdev:
1019 evdev_cleanup(evdev);
1020 err_unregister_handle:
1021 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001022 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001023 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001024 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027static void evdev_disconnect(struct input_handle *handle)
1028{
1029 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001031 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001032 evdev_cleanup(evdev);
1033 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001034 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001037static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 { .driver_info = 1 }, /* Matches all devices */
1039 { }, /* Terminating zero entry */
1040};
1041
1042MODULE_DEVICE_TABLE(input, evdev_ids);
1043
1044static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001045 .event = evdev_event,
1046 .connect = evdev_connect,
1047 .disconnect = evdev_disconnect,
1048 .fops = &evdev_fops,
1049 .minor = EVDEV_MINOR_BASE,
1050 .name = "evdev",
1051 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052};
1053
1054static int __init evdev_init(void)
1055{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001056 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059static void __exit evdev_exit(void)
1060{
1061 input_unregister_handler(&evdev_handler);
1062}
1063
1064module_init(evdev_init);
1065module_exit(evdev_exit);
1066
1067MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1068MODULE_DESCRIPTION("Input driver event char devices");
1069MODULE_LICENSE("GPL");