blob: 10ae1c966a3512919148cbdbafbcf3c45e2e1e79 [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;
Mike Chan2a1f3522009-10-27 17:37:44 -070048 char name[28];
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct fasync_struct *fasync;
50 struct evdev *evdev;
51 struct list_head node;
Jeff Brown9fb0f142011-04-12 23:29:38 -070052 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070053 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040057static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040059static void evdev_pass_event(struct evdev_client *client,
60 struct input_event *event)
61{
Jeff Brown9fb0f142011-04-12 23:29:38 -070062 /* Interrupts are disabled, just acquire the lock. */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040063 spin_lock(&client->buffer_lock);
Jeff Brown9fb0f142011-04-12 23:29:38 -070064
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -070065 wake_lock_timeout(&client->wake_lock, 5 * HZ);
Jeff Brown9fb0f142011-04-12 23:29:38 -070066 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;
82 }
83
84 if (event->type == EV_SYN && event->code == SYN_REPORT) {
85 client->packet_head = client->head;
86 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -070087 }
88
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040089 spin_unlock(&client->buffer_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040090}
91
92/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040093 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040094 */
95static void evdev_event(struct input_handle *handle,
96 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040099 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400100 struct input_event event;
Arve Hjønnevåg5d618872008-10-17 15:28:08 -0700101 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Arve Hjønnevåg5d618872008-10-17 15:28:08 -0700103 ktime_get_ts(&ts);
104 event.time.tv_sec = ts.tv_sec;
105 event.time.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400106 event.type = type;
107 event.code = code;
108 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400110 rcu_read_lock();
111
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400112 client = rcu_dereference(evdev->grab);
113 if (client)
114 evdev_pass_event(client, &event);
115 else
116 list_for_each_entry_rcu(client, &evdev->client_list, node)
117 evdev_pass_event(client, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400119 rcu_read_unlock();
120
Dmitry Torokhovda40b0b2011-06-18 02:50:11 -0700121 if (type == EV_SYN && code == SYN_REPORT)
122 wake_up_interruptible(&evdev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static int evdev_fasync(int fd, struct file *file, int on)
126{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400127 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400128
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700129 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400132static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400134 struct evdev_client *client = file->private_data;
135 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400136 int retval;
137
138 retval = mutex_lock_interruptible(&evdev->mutex);
139 if (retval)
140 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400141
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400142 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400143 retval = -ENODEV;
144 else
145 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400146
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400147 mutex_unlock(&evdev->mutex);
148 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400151static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400153 struct evdev *evdev = container_of(dev, struct evdev, dev);
154
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400155 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 kfree(evdev);
157}
158
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400159/*
160 * Grabs an event device (along with underlying input device).
161 * This function is called with evdev->mutex taken.
162 */
163static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
164{
165 int error;
166
167 if (evdev->grab)
168 return -EBUSY;
169
170 error = input_grab_device(&evdev->handle);
171 if (error)
172 return error;
173
174 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400175
176 return 0;
177}
178
179static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
180{
181 if (evdev->grab != client)
182 return -EINVAL;
183
184 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400185 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400186 input_release_device(&evdev->handle);
187
188 return 0;
189}
190
191static void evdev_attach_client(struct evdev *evdev,
192 struct evdev_client *client)
193{
194 spin_lock(&evdev->client_lock);
195 list_add_tail_rcu(&client->node, &evdev->client_list);
196 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400197}
198
199static void evdev_detach_client(struct evdev *evdev,
200 struct evdev_client *client)
201{
202 spin_lock(&evdev->client_lock);
203 list_del_rcu(&client->node);
204 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400205 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400206}
207
208static int evdev_open_device(struct evdev *evdev)
209{
210 int retval;
211
212 retval = mutex_lock_interruptible(&evdev->mutex);
213 if (retval)
214 return retval;
215
216 if (!evdev->exist)
217 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400218 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400219 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400220 if (retval)
221 evdev->open--;
222 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400223
224 mutex_unlock(&evdev->mutex);
225 return retval;
226}
227
228static void evdev_close_device(struct evdev *evdev)
229{
230 mutex_lock(&evdev->mutex);
231
232 if (evdev->exist && !--evdev->open)
233 input_close_device(&evdev->handle);
234
235 mutex_unlock(&evdev->mutex);
236}
237
238/*
239 * Wake up users waiting for IO so they can disconnect from
240 * dead device.
241 */
242static void evdev_hangup(struct evdev *evdev)
243{
244 struct evdev_client *client;
245
246 spin_lock(&evdev->client_lock);
247 list_for_each_entry(client, &evdev->client_list, node)
248 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
249 spin_unlock(&evdev->client_lock);
250
251 wake_up_interruptible(&evdev->wait);
252}
253
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400254static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400256 struct evdev_client *client = file->private_data;
257 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400259 mutex_lock(&evdev->mutex);
260 if (evdev->grab == client)
261 evdev_ungrab(evdev, client);
262 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400264 evdev_detach_client(evdev, client);
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700265 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400266 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400268 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400269 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272}
273
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700274static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
275{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700276 unsigned int n_events =
277 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
278 EVDEV_MIN_BUFFER_SIZE);
279
280 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700281}
282
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400283static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400285 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400286 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700288 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400289 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400291 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return -ENODEV;
293
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400294 error = mutex_lock_interruptible(&evdev_table_mutex);
295 if (error)
296 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400297 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400298 if (evdev)
299 get_device(&evdev->dev);
300 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400301
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400302 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400303 return -ENODEV;
304
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700305 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
306
307 client = kzalloc(sizeof(struct evdev_client) +
308 bufsize * sizeof(struct input_event),
309 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400310 if (!client) {
311 error = -ENOMEM;
312 goto err_put_evdev;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700315 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400316 spin_lock_init(&client->buffer_lock);
Mike Chan2a1f3522009-10-27 17:37:44 -0700317 snprintf(client->name, sizeof(client->name), "%s-%d",
318 dev_name(&evdev->dev), task_tgid_vnr(current));
319 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400320 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400321 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400323 error = evdev_open_device(evdev);
324 if (error)
325 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400327 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800328 nonseekable_open(inode, file);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400331
332 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400333 evdev_detach_client(evdev, client);
Benoit Goby0ee56952011-01-19 14:10:37 -0800334 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400335 kfree(client);
336 err_put_evdev:
337 put_device(&evdev->dev);
338 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400341static ssize_t evdev_write(struct file *file, const char __user *buffer,
342 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400344 struct evdev_client *client = file->private_data;
345 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct input_event event;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400347 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Peter Korsgaard439581e2011-02-25 09:30:46 -0800349 if (count < input_event_size())
350 return -EINVAL;
351
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400352 retval = mutex_lock_interruptible(&evdev->mutex);
353 if (retval)
354 return retval;
355
356 if (!evdev->exist) {
357 retval = -ENODEV;
358 goto out;
359 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500360
Peter Korsgaard439581e2011-02-25 09:30:46 -0800361 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400362 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400363 retval = -EFAULT;
364 goto out;
365 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800366 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400367
368 input_inject_event(&evdev->handle,
369 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800370 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500371
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400372 out:
373 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500374 return retval;
375}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500376
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400377static int evdev_fetch_next_event(struct evdev_client *client,
378 struct input_event *event)
379{
380 int have_event;
381
382 spin_lock_irq(&client->buffer_lock);
383
Dima Zavin48731932011-10-04 16:58:03 -0700384 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400385 if (have_event) {
386 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700387 client->tail &= client->bufsize - 1;
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700388 if (client->head == client->tail)
389 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400390 }
391
392 spin_unlock_irq(&client->buffer_lock);
393
394 return have_event;
395}
396
397static ssize_t evdev_read(struct file *file, char __user *buffer,
398 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400400 struct evdev_client *client = file->private_data;
401 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400402 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int retval;
404
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400405 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -EINVAL;
407
Jeff Browncdda9112011-04-26 22:16:11 -0700408 if (client->packet_head == client->tail && evdev->exist &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400409 (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -EAGAIN;
411
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400412 retval = wait_event_interruptible(evdev->wait,
Jeff Browncdda9112011-04-26 22:16:11 -0700413 client->packet_head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (retval)
415 return retval;
416
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400417 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -ENODEV;
419
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400420 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400421 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500422
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400423 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500424 return -EFAULT;
425
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400426 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Dima Zavin587b1eb2011-10-04 22:31:48 -0700429 if (retval == 0 && file->f_flags & O_NONBLOCK)
430 retval = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return retval;
432}
433
434/* No kernel lock - fine */
435static unsigned int evdev_poll(struct file *file, poll_table *wait)
436{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400437 struct evdev_client *client = file->private_data;
438 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700439 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400440
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400441 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700442
443 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700444 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700445 mask |= POLLIN | POLLRDNORM;
446
447 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500450#ifdef CONFIG_COMPAT
451
452#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700453#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500454
455#ifdef __BIG_ENDIAN
456static int bits_to_user(unsigned long *bits, unsigned int maxbit,
457 unsigned int maxlen, void __user *p, int compat)
458{
459 int len, i;
460
461 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700462 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400463 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500464 len = maxlen;
465
466 for (i = 0; i < len / sizeof(compat_long_t); i++)
467 if (copy_to_user((compat_long_t __user *) p + i,
468 (compat_long_t *) bits +
469 i + 1 - ((i % 2) << 1),
470 sizeof(compat_long_t)))
471 return -EFAULT;
472 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700473 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500474 if (len > maxlen)
475 len = maxlen;
476
477 if (copy_to_user(p, bits, len))
478 return -EFAULT;
479 }
480
481 return len;
482}
483#else
484static int bits_to_user(unsigned long *bits, unsigned int maxbit,
485 unsigned int maxlen, void __user *p, int compat)
486{
487 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700488 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
489 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500490
491 if (len > maxlen)
492 len = maxlen;
493
494 return copy_to_user(p, bits, len) ? -EFAULT : len;
495}
496#endif /* __BIG_ENDIAN */
497
498#else
499
500static int bits_to_user(unsigned long *bits, unsigned int maxbit,
501 unsigned int maxlen, void __user *p, int compat)
502{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700503 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500504
505 if (len > maxlen)
506 len = maxlen;
507
508 return copy_to_user(p, bits, len) ? -EFAULT : len;
509}
510
511#endif /* CONFIG_COMPAT */
512
513static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
514{
515 int len;
516
517 if (!str)
518 return -ENOENT;
519
520 len = strlen(str) + 1;
521 if (len > maxlen)
522 len = maxlen;
523
524 return copy_to_user(p, str, len) ? -EFAULT : len;
525}
526
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400527#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700528static int handle_eviocgbit(struct input_dev *dev,
529 unsigned int type, unsigned int size,
530 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400531{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400532 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400533 unsigned long *bits;
534 int len;
535
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700536 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400537
538 case 0: bits = dev->evbit; len = EV_MAX; break;
539 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
540 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
541 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
542 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
543 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
544 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
545 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
546 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
547 default: return -EINVAL;
548 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400549
550 /*
551 * Work around bugs in userspace programs that like to do
552 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
553 * should be in bytes, not in bits.
554 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700555 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400556 len = OLD_KEY_MAX;
557 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800558 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
559 "limiting output to %zu bytes. See "
560 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
561 OLD_KEY_MAX,
562 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400563 }
564
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700565 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400566}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400567#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400568
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800569static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
570{
571 struct input_keymap_entry ke = {
572 .len = sizeof(unsigned int),
573 .flags = 0,
574 };
575 int __user *ip = (int __user *)p;
576 int error;
577
578 /* legacy case */
579 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
580 return -EFAULT;
581
582 error = input_get_keycode(dev, &ke);
583 if (error)
584 return error;
585
586 if (put_user(ke.keycode, ip + 1))
587 return -EFAULT;
588
589 return 0;
590}
591
592static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700593{
594 struct input_keymap_entry ke;
595 int error;
596
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800597 if (copy_from_user(&ke, p, sizeof(ke)))
598 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700599
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800600 error = input_get_keycode(dev, &ke);
601 if (error)
602 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700603
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800604 if (copy_to_user(p, &ke, sizeof(ke)))
605 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700606
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700607 return 0;
608}
609
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800610static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
611{
612 struct input_keymap_entry ke = {
613 .len = sizeof(unsigned int),
614 .flags = 0,
615 };
616 int __user *ip = (int __user *)p;
617
618 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
619 return -EFAULT;
620
621 if (get_user(ke.keycode, ip + 1))
622 return -EFAULT;
623
624 return input_set_keycode(dev, &ke);
625}
626
627static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700628{
629 struct input_keymap_entry ke;
630
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800631 if (copy_from_user(&ke, p, sizeof(ke)))
632 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700633
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800634 if (ke.len > sizeof(ke.scancode))
635 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700636
637 return input_set_keycode(dev, &ke);
638}
639
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400640static long evdev_do_ioctl(struct file *file, unsigned int cmd,
641 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400643 struct evdev_client *client = file->private_data;
644 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct input_dev *dev = evdev->handle.dev;
646 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400647 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500648 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800649 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700650 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400651 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700653 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 switch (cmd) {
655
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400656 case EVIOCGVERSION:
657 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400659 case EVIOCGID:
660 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
661 return -EFAULT;
662 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400663
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400664 case EVIOCGREP:
665 if (!test_bit(EV_REP, dev->evbit))
666 return -ENOSYS;
667 if (put_user(dev->rep[REP_DELAY], ip))
668 return -EFAULT;
669 if (put_user(dev->rep[REP_PERIOD], ip + 1))
670 return -EFAULT;
671 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400672
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400673 case EVIOCSREP:
674 if (!test_bit(EV_REP, dev->evbit))
675 return -ENOSYS;
676 if (get_user(u, ip))
677 return -EFAULT;
678 if (get_user(v, ip + 1))
679 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400680
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400681 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
682 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500683
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400686 case EVIOCRMFF:
687 return input_ff_erase(dev, (int)(unsigned long) p, file);
688
689 case EVIOCGEFFECTS:
690 i = test_bit(EV_FF, dev->evbit) ?
691 dev->ff->max_effects : 0;
692 if (put_user(i, ip))
693 return -EFAULT;
694 return 0;
695
696 case EVIOCGRAB:
697 if (p)
698 return evdev_grab(evdev, client);
699 else
700 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800701
702 case EVIOCGKEYCODE:
703 return evdev_handle_get_keycode(dev, p);
704
705 case EVIOCSKEYCODE:
706 return evdev_handle_set_keycode(dev, p);
707
708 case EVIOCGKEYCODE_V2:
709 return evdev_handle_get_keycode_v2(dev, p);
710
711 case EVIOCSKEYCODE_V2:
712 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700713 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400714
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700715 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400716
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700717 /* Now check variable-length commands */
718#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700719 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400720
Henrik Rydberg85b77202010-12-18 20:51:13 +0100721 case EVIOCGPROP(0):
722 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
723 size, p, compat_mode);
724
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700725 case EVIOCGKEY(0):
726 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400727
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700728 case EVIOCGLED(0):
729 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400730
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700731 case EVIOCGSND(0):
732 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400733
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700734 case EVIOCGSW(0):
735 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400736
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700737 case EVIOCGNAME(0):
738 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400739
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700740 case EVIOCGPHYS(0):
741 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400742
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700743 case EVIOCGUNIQ(0):
744 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400745
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700746 case EVIOC_MASK_SIZE(EVIOCSFF):
747 if (input_ff_effect_from_user(p, size, &effect))
748 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400749
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700750 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400751
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700752 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
753 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400754
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700755 return error;
756 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400757
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700758 /* Multi-number variable-length handlers */
759 if (_IOC_TYPE(cmd) != 'E')
760 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700762 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700764 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
765 return handle_eviocgbit(dev,
766 _IOC_NR(cmd) & EV_MAX, size,
767 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700769 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400770
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700771 if (!dev->absinfo)
772 return -EINVAL;
773
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700774 t = _IOC_NR(cmd) & ABS_MAX;
775 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400776
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700777 if (copy_to_user(p, &abs, min_t(size_t,
778 size, sizeof(struct input_absinfo))))
779 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400780
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700781 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700784
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700785 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700786
787 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
788
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700789 if (!dev->absinfo)
790 return -EINVAL;
791
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700792 t = _IOC_NR(cmd) & ABS_MAX;
793
794 if (copy_from_user(&abs, p, min_t(size_t,
795 size, sizeof(struct input_absinfo))))
796 return -EFAULT;
797
798 if (size < sizeof(struct input_absinfo))
799 abs.resolution = 0;
800
801 /* We can't change number of reserved MT slots */
802 if (t == ABS_MT_SLOT)
803 return -EINVAL;
804
805 /*
806 * Take event lock to ensure that we are not
807 * changing device parameters in the middle
808 * of event.
809 */
810 spin_lock_irq(&dev->event_lock);
811 dev->absinfo[t] = abs;
812 spin_unlock_irq(&dev->event_lock);
813
814 return 0;
815 }
816 }
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return -EINVAL;
819}
820
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400821static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
822 void __user *p, int compat_mode)
823{
824 struct evdev_client *client = file->private_data;
825 struct evdev *evdev = client->evdev;
826 int retval;
827
828 retval = mutex_lock_interruptible(&evdev->mutex);
829 if (retval)
830 return retval;
831
832 if (!evdev->exist) {
833 retval = -ENODEV;
834 goto out;
835 }
836
837 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
838
839 out:
840 mutex_unlock(&evdev->mutex);
841 return retval;
842}
843
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500844static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
845{
846 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
847}
848
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500849#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400850static long evdev_ioctl_compat(struct file *file,
851 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500852{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500853 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500854}
855#endif
856
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400857static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400858 .owner = THIS_MODULE,
859 .read = evdev_read,
860 .write = evdev_write,
861 .poll = evdev_poll,
862 .open = evdev_open,
863 .release = evdev_release,
864 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500865#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400866 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500867#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400868 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200869 .flush = evdev_flush,
870 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871};
872
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400873static int evdev_install_chrdev(struct evdev *evdev)
874{
875 /*
876 * No need to do any locking here as calls to connect and
877 * disconnect are serialized by the input core
878 */
879 evdev_table[evdev->minor] = evdev;
880 return 0;
881}
882
883static void evdev_remove_chrdev(struct evdev *evdev)
884{
885 /*
886 * Lock evdev table to prevent race with evdev_open()
887 */
888 mutex_lock(&evdev_table_mutex);
889 evdev_table[evdev->minor] = NULL;
890 mutex_unlock(&evdev_table_mutex);
891}
892
893/*
894 * Mark device non-existent. This disables writes, ioctls and
895 * prevents new users from opening the device. Already posted
896 * blocking reads will stay, however new ones will fail.
897 */
898static void evdev_mark_dead(struct evdev *evdev)
899{
900 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700901 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400902 mutex_unlock(&evdev->mutex);
903}
904
905static void evdev_cleanup(struct evdev *evdev)
906{
907 struct input_handle *handle = &evdev->handle;
908
909 evdev_mark_dead(evdev);
910 evdev_hangup(evdev);
911 evdev_remove_chrdev(evdev);
912
913 /* evdev is marked dead so no one else accesses evdev->open */
914 if (evdev->open) {
915 input_flush_device(handle, NULL);
916 input_close_device(handle);
917 }
918}
919
920/*
921 * Create new evdev device. Note that input core serializes calls
922 * to connect and disconnect so we don't need to lock evdev_table here.
923 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400924static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
925 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 struct evdev *evdev;
928 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400929 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400931 for (minor = 0; minor < EVDEV_MINORS; minor++)
932 if (!evdev_table[minor])
933 break;
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -0800936 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400937 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400940 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
941 if (!evdev)
942 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400944 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400945 spin_lock_init(&evdev->client_lock);
946 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 init_waitqueue_head(&evdev->wait);
948
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700949 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700950 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400952
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400953 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700954 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 evdev->handle.handler = handler;
956 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400957
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400958 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400959 evdev->dev.class = &input_class;
960 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400961 evdev->dev.release = evdev_free;
962 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400964 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400965 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400966 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400968 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400969 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400970 goto err_unregister_handle;
971
972 error = device_add(&evdev->dev);
973 if (error)
974 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400975
976 return 0;
977
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400978 err_cleanup_evdev:
979 evdev_cleanup(evdev);
980 err_unregister_handle:
981 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400982 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400983 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400984 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987static void evdev_disconnect(struct input_handle *handle)
988{
989 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400991 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400992 evdev_cleanup(evdev);
993 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400994 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400997static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 { .driver_info = 1 }, /* Matches all devices */
999 { }, /* Terminating zero entry */
1000};
1001
1002MODULE_DEVICE_TABLE(input, evdev_ids);
1003
1004static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001005 .event = evdev_event,
1006 .connect = evdev_connect,
1007 .disconnect = evdev_disconnect,
1008 .fops = &evdev_fops,
1009 .minor = EVDEV_MINOR_BASE,
1010 .name = "evdev",
1011 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012};
1013
1014static int __init evdev_init(void)
1015{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001016 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019static void __exit evdev_exit(void)
1020{
1021 input_unregister_handler(&evdev_handler);
1022}
1023
1024module_init(evdev_init);
1025module_exit(evdev_exit);
1026
1027MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1028MODULE_DESCRIPTION("Input driver event char devices");
1029MODULE_LICENSE("GPL");