blob: 5c5f9db280755cfe7cc4742a1d5fbb9ddc5d511e [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
Dima Zavin183a1fc2011-10-04 17:10:45 -0700408 if (!(file->f_flags & O_NONBLOCK)) {
409 retval = wait_event_interruptible(evdev->wait,
410 client->packet_head != client->tail || !evdev->exist);
411 if (retval)
412 return retval;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400415 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -ENODEV;
417
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400418 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400419 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500420
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400421 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500422 return -EFAULT;
423
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400424 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
Dima Zavin587b1eb2011-10-04 22:31:48 -0700427 if (retval == 0 && file->f_flags & O_NONBLOCK)
428 retval = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return retval;
430}
431
432/* No kernel lock - fine */
433static unsigned int evdev_poll(struct file *file, poll_table *wait)
434{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400435 struct evdev_client *client = file->private_data;
436 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700437 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400438
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400439 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700440
441 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700442 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700443 mask |= POLLIN | POLLRDNORM;
444
445 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500448#ifdef CONFIG_COMPAT
449
450#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700451#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500452
453#ifdef __BIG_ENDIAN
454static int bits_to_user(unsigned long *bits, unsigned int maxbit,
455 unsigned int maxlen, void __user *p, int compat)
456{
457 int len, i;
458
459 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700460 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400461 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500462 len = maxlen;
463
464 for (i = 0; i < len / sizeof(compat_long_t); i++)
465 if (copy_to_user((compat_long_t __user *) p + i,
466 (compat_long_t *) bits +
467 i + 1 - ((i % 2) << 1),
468 sizeof(compat_long_t)))
469 return -EFAULT;
470 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700471 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500472 if (len > maxlen)
473 len = maxlen;
474
475 if (copy_to_user(p, bits, len))
476 return -EFAULT;
477 }
478
479 return len;
480}
481#else
482static int bits_to_user(unsigned long *bits, unsigned int maxbit,
483 unsigned int maxlen, void __user *p, int compat)
484{
485 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700486 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
487 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500488
489 if (len > maxlen)
490 len = maxlen;
491
492 return copy_to_user(p, bits, len) ? -EFAULT : len;
493}
494#endif /* __BIG_ENDIAN */
495
496#else
497
498static int bits_to_user(unsigned long *bits, unsigned int maxbit,
499 unsigned int maxlen, void __user *p, int compat)
500{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700501 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500502
503 if (len > maxlen)
504 len = maxlen;
505
506 return copy_to_user(p, bits, len) ? -EFAULT : len;
507}
508
509#endif /* CONFIG_COMPAT */
510
511static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
512{
513 int len;
514
515 if (!str)
516 return -ENOENT;
517
518 len = strlen(str) + 1;
519 if (len > maxlen)
520 len = maxlen;
521
522 return copy_to_user(p, str, len) ? -EFAULT : len;
523}
524
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400525#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700526static int handle_eviocgbit(struct input_dev *dev,
527 unsigned int type, unsigned int size,
528 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400529{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400530 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400531 unsigned long *bits;
532 int len;
533
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700534 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400535
536 case 0: bits = dev->evbit; len = EV_MAX; break;
537 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
538 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
539 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
540 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
541 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
542 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
543 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
544 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
545 default: return -EINVAL;
546 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400547
548 /*
549 * Work around bugs in userspace programs that like to do
550 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
551 * should be in bytes, not in bits.
552 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700553 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400554 len = OLD_KEY_MAX;
555 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800556 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
557 "limiting output to %zu bytes. See "
558 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
559 OLD_KEY_MAX,
560 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400561 }
562
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700563 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400564}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400565#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400566
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800567static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
568{
569 struct input_keymap_entry ke = {
570 .len = sizeof(unsigned int),
571 .flags = 0,
572 };
573 int __user *ip = (int __user *)p;
574 int error;
575
576 /* legacy case */
577 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
578 return -EFAULT;
579
580 error = input_get_keycode(dev, &ke);
581 if (error)
582 return error;
583
584 if (put_user(ke.keycode, ip + 1))
585 return -EFAULT;
586
587 return 0;
588}
589
590static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700591{
592 struct input_keymap_entry ke;
593 int error;
594
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800595 if (copy_from_user(&ke, p, sizeof(ke)))
596 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700597
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800598 error = input_get_keycode(dev, &ke);
599 if (error)
600 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700601
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800602 if (copy_to_user(p, &ke, sizeof(ke)))
603 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700604
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700605 return 0;
606}
607
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800608static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
609{
610 struct input_keymap_entry ke = {
611 .len = sizeof(unsigned int),
612 .flags = 0,
613 };
614 int __user *ip = (int __user *)p;
615
616 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
617 return -EFAULT;
618
619 if (get_user(ke.keycode, ip + 1))
620 return -EFAULT;
621
622 return input_set_keycode(dev, &ke);
623}
624
625static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700626{
627 struct input_keymap_entry ke;
628
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800629 if (copy_from_user(&ke, p, sizeof(ke)))
630 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700631
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800632 if (ke.len > sizeof(ke.scancode))
633 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700634
635 return input_set_keycode(dev, &ke);
636}
637
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400638static long evdev_do_ioctl(struct file *file, unsigned int cmd,
639 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400641 struct evdev_client *client = file->private_data;
642 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct input_dev *dev = evdev->handle.dev;
644 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400645 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500646 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800647 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700648 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400649 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700651 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 switch (cmd) {
653
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400654 case EVIOCGVERSION:
655 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400657 case EVIOCGID:
658 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
659 return -EFAULT;
660 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400661
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400662 case EVIOCGREP:
663 if (!test_bit(EV_REP, dev->evbit))
664 return -ENOSYS;
665 if (put_user(dev->rep[REP_DELAY], ip))
666 return -EFAULT;
667 if (put_user(dev->rep[REP_PERIOD], ip + 1))
668 return -EFAULT;
669 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400670
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400671 case EVIOCSREP:
672 if (!test_bit(EV_REP, dev->evbit))
673 return -ENOSYS;
674 if (get_user(u, ip))
675 return -EFAULT;
676 if (get_user(v, ip + 1))
677 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400678
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400679 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
680 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500681
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400684 case EVIOCRMFF:
685 return input_ff_erase(dev, (int)(unsigned long) p, file);
686
687 case EVIOCGEFFECTS:
688 i = test_bit(EV_FF, dev->evbit) ?
689 dev->ff->max_effects : 0;
690 if (put_user(i, ip))
691 return -EFAULT;
692 return 0;
693
694 case EVIOCGRAB:
695 if (p)
696 return evdev_grab(evdev, client);
697 else
698 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800699
700 case EVIOCGKEYCODE:
701 return evdev_handle_get_keycode(dev, p);
702
703 case EVIOCSKEYCODE:
704 return evdev_handle_set_keycode(dev, p);
705
706 case EVIOCGKEYCODE_V2:
707 return evdev_handle_get_keycode_v2(dev, p);
708
709 case EVIOCSKEYCODE_V2:
710 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700711 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400712
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700713 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400714
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700715 /* Now check variable-length commands */
716#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700717 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400718
Henrik Rydberg85b77202010-12-18 20:51:13 +0100719 case EVIOCGPROP(0):
720 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
721 size, p, compat_mode);
722
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700723 case EVIOCGKEY(0):
724 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400725
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700726 case EVIOCGLED(0):
727 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400728
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700729 case EVIOCGSND(0):
730 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400731
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700732 case EVIOCGSW(0):
733 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400734
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700735 case EVIOCGNAME(0):
736 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400737
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700738 case EVIOCGPHYS(0):
739 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400740
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700741 case EVIOCGUNIQ(0):
742 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400743
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700744 case EVIOC_MASK_SIZE(EVIOCSFF):
745 if (input_ff_effect_from_user(p, size, &effect))
746 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400747
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700748 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400749
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700750 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
751 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400752
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700753 return error;
754 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400755
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700756 /* Multi-number variable-length handlers */
757 if (_IOC_TYPE(cmd) != 'E')
758 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700760 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700762 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
763 return handle_eviocgbit(dev,
764 _IOC_NR(cmd) & EV_MAX, size,
765 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700767 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400768
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700769 if (!dev->absinfo)
770 return -EINVAL;
771
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700772 t = _IOC_NR(cmd) & ABS_MAX;
773 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400774
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700775 if (copy_to_user(p, &abs, min_t(size_t,
776 size, sizeof(struct input_absinfo))))
777 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400778
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700779 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700782
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700783 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700784
785 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
786
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700787 if (!dev->absinfo)
788 return -EINVAL;
789
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700790 t = _IOC_NR(cmd) & ABS_MAX;
791
792 if (copy_from_user(&abs, p, min_t(size_t,
793 size, sizeof(struct input_absinfo))))
794 return -EFAULT;
795
796 if (size < sizeof(struct input_absinfo))
797 abs.resolution = 0;
798
799 /* We can't change number of reserved MT slots */
800 if (t == ABS_MT_SLOT)
801 return -EINVAL;
802
803 /*
804 * Take event lock to ensure that we are not
805 * changing device parameters in the middle
806 * of event.
807 */
808 spin_lock_irq(&dev->event_lock);
809 dev->absinfo[t] = abs;
810 spin_unlock_irq(&dev->event_lock);
811
812 return 0;
813 }
814 }
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -EINVAL;
817}
818
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400819static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
820 void __user *p, int compat_mode)
821{
822 struct evdev_client *client = file->private_data;
823 struct evdev *evdev = client->evdev;
824 int retval;
825
826 retval = mutex_lock_interruptible(&evdev->mutex);
827 if (retval)
828 return retval;
829
830 if (!evdev->exist) {
831 retval = -ENODEV;
832 goto out;
833 }
834
835 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
836
837 out:
838 mutex_unlock(&evdev->mutex);
839 return retval;
840}
841
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500842static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
843{
844 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
845}
846
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500847#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400848static long evdev_ioctl_compat(struct file *file,
849 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500850{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500851 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500852}
853#endif
854
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400855static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400856 .owner = THIS_MODULE,
857 .read = evdev_read,
858 .write = evdev_write,
859 .poll = evdev_poll,
860 .open = evdev_open,
861 .release = evdev_release,
862 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500863#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400864 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500865#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400866 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200867 .flush = evdev_flush,
868 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869};
870
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400871static int evdev_install_chrdev(struct evdev *evdev)
872{
873 /*
874 * No need to do any locking here as calls to connect and
875 * disconnect are serialized by the input core
876 */
877 evdev_table[evdev->minor] = evdev;
878 return 0;
879}
880
881static void evdev_remove_chrdev(struct evdev *evdev)
882{
883 /*
884 * Lock evdev table to prevent race with evdev_open()
885 */
886 mutex_lock(&evdev_table_mutex);
887 evdev_table[evdev->minor] = NULL;
888 mutex_unlock(&evdev_table_mutex);
889}
890
891/*
892 * Mark device non-existent. This disables writes, ioctls and
893 * prevents new users from opening the device. Already posted
894 * blocking reads will stay, however new ones will fail.
895 */
896static void evdev_mark_dead(struct evdev *evdev)
897{
898 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700899 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400900 mutex_unlock(&evdev->mutex);
901}
902
903static void evdev_cleanup(struct evdev *evdev)
904{
905 struct input_handle *handle = &evdev->handle;
906
907 evdev_mark_dead(evdev);
908 evdev_hangup(evdev);
909 evdev_remove_chrdev(evdev);
910
911 /* evdev is marked dead so no one else accesses evdev->open */
912 if (evdev->open) {
913 input_flush_device(handle, NULL);
914 input_close_device(handle);
915 }
916}
917
918/*
919 * Create new evdev device. Note that input core serializes calls
920 * to connect and disconnect so we don't need to lock evdev_table here.
921 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400922static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
923 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 struct evdev *evdev;
926 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400927 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400929 for (minor = 0; minor < EVDEV_MINORS; minor++)
930 if (!evdev_table[minor])
931 break;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -0800934 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400935 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400938 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
939 if (!evdev)
940 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400942 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400943 spin_lock_init(&evdev->client_lock);
944 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 init_waitqueue_head(&evdev->wait);
946
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700947 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700948 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400950
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400951 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700952 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 evdev->handle.handler = handler;
954 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400955
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400956 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400957 evdev->dev.class = &input_class;
958 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400959 evdev->dev.release = evdev_free;
960 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400962 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400963 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400964 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400966 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400967 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400968 goto err_unregister_handle;
969
970 error = device_add(&evdev->dev);
971 if (error)
972 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400973
974 return 0;
975
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400976 err_cleanup_evdev:
977 evdev_cleanup(evdev);
978 err_unregister_handle:
979 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400980 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400981 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400982 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985static void evdev_disconnect(struct input_handle *handle)
986{
987 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400989 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400990 evdev_cleanup(evdev);
991 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400992 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400995static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 { .driver_info = 1 }, /* Matches all devices */
997 { }, /* Terminating zero entry */
998};
999
1000MODULE_DEVICE_TABLE(input, evdev_ids);
1001
1002static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001003 .event = evdev_event,
1004 .connect = evdev_connect,
1005 .disconnect = evdev_disconnect,
1006 .fops = &evdev_fops,
1007 .minor = EVDEV_MINOR_BASE,
1008 .name = "evdev",
1009 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010};
1011
1012static int __init evdev_init(void)
1013{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001014 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017static void __exit evdev_exit(void)
1018{
1019 input_unregister_handler(&evdev_handler);
1020}
1021
1022module_init(evdev_init);
1023module_exit(evdev_exit);
1024
1025MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1026MODULE_DESCRIPTION("Input driver event char devices");
1027MODULE_LICENSE("GPL");