blob: d73f49c59d715a0172445ab44005392d4f9a1ad1 [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);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064 wake_lock_timeout(&client->wake_lock, 5 * HZ);
Jeff Brown9fb0f142011-04-12 23:29:38 -070065
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -070066 wake_lock_timeout(&client->wake_lock, 5 * HZ);
Jeff Brown9fb0f142011-04-12 23:29:38 -070067 client->buffer[client->head++] = *event;
68 client->head &= client->bufsize - 1;
69
70 if (unlikely(client->head == client->tail)) {
71 /*
72 * This effectively "drops" all unconsumed events, leaving
73 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
74 */
75 client->tail = (client->head - 2) & (client->bufsize - 1);
76
77 client->buffer[client->tail].time = event->time;
78 client->buffer[client->tail].type = EV_SYN;
79 client->buffer[client->tail].code = SYN_DROPPED;
80 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -070081
82 client->packet_head = client->tail;
83 }
84
85 if (event->type == EV_SYN && event->code == SYN_REPORT) {
86 client->packet_head = client->head;
87 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -070088 }
89
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040090 spin_unlock(&client->buffer_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040091}
92
93/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040094 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040095 */
96static void evdev_event(struct input_handle *handle,
97 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400100 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400101 struct input_event event;
Arve Hjønnevåg5d618872008-10-17 15:28:08 -0700102 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Arve Hjønnevåg5d618872008-10-17 15:28:08 -0700104 ktime_get_ts(&ts);
105 event.time.tv_sec = ts.tv_sec;
106 event.time.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400107 event.type = type;
108 event.code = code;
109 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400111 rcu_read_lock();
112
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400113 client = rcu_dereference(evdev->grab);
114 if (client)
115 evdev_pass_event(client, &event);
116 else
117 list_for_each_entry_rcu(client, &evdev->client_list, node)
118 evdev_pass_event(client, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400120 rcu_read_unlock();
121
Dmitry Torokhovda40b0b2011-06-18 02:50:11 -0700122 if (type == EV_SYN && code == SYN_REPORT)
123 wake_up_interruptible(&evdev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static int evdev_fasync(int fd, struct file *file, int on)
127{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400128 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400129
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700130 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400133static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400135 struct evdev_client *client = file->private_data;
136 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400137 int retval;
138
139 retval = mutex_lock_interruptible(&evdev->mutex);
140 if (retval)
141 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400142
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400143 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400144 retval = -ENODEV;
145 else
146 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400147
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400148 mutex_unlock(&evdev->mutex);
149 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400152static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400154 struct evdev *evdev = container_of(dev, struct evdev, dev);
155
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400156 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 kfree(evdev);
158}
159
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400160/*
161 * Grabs an event device (along with underlying input device).
162 * This function is called with evdev->mutex taken.
163 */
164static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
165{
166 int error;
167
168 if (evdev->grab)
169 return -EBUSY;
170
171 error = input_grab_device(&evdev->handle);
172 if (error)
173 return error;
174
175 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400176
177 return 0;
178}
179
180static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
181{
182 if (evdev->grab != client)
183 return -EINVAL;
184
185 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400186 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400187 input_release_device(&evdev->handle);
188
189 return 0;
190}
191
192static void evdev_attach_client(struct evdev *evdev,
193 struct evdev_client *client)
194{
195 spin_lock(&evdev->client_lock);
196 list_add_tail_rcu(&client->node, &evdev->client_list);
197 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400198}
199
200static void evdev_detach_client(struct evdev *evdev,
201 struct evdev_client *client)
202{
203 spin_lock(&evdev->client_lock);
204 list_del_rcu(&client->node);
205 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400206 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400207}
208
209static int evdev_open_device(struct evdev *evdev)
210{
211 int retval;
212
213 retval = mutex_lock_interruptible(&evdev->mutex);
214 if (retval)
215 return retval;
216
217 if (!evdev->exist)
218 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400219 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400220 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400221 if (retval)
222 evdev->open--;
223 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400224
225 mutex_unlock(&evdev->mutex);
226 return retval;
227}
228
229static void evdev_close_device(struct evdev *evdev)
230{
231 mutex_lock(&evdev->mutex);
232
233 if (evdev->exist && !--evdev->open)
234 input_close_device(&evdev->handle);
235
236 mutex_unlock(&evdev->mutex);
237}
238
239/*
240 * Wake up users waiting for IO so they can disconnect from
241 * dead device.
242 */
243static void evdev_hangup(struct evdev *evdev)
244{
245 struct evdev_client *client;
246
247 spin_lock(&evdev->client_lock);
248 list_for_each_entry(client, &evdev->client_list, node)
249 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
250 spin_unlock(&evdev->client_lock);
251
252 wake_up_interruptible(&evdev->wait);
253}
254
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400255static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400257 struct evdev_client *client = file->private_data;
258 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400260 mutex_lock(&evdev->mutex);
261 if (evdev->grab == client)
262 evdev_ungrab(evdev, client);
263 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400265 evdev_detach_client(evdev, client);
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700266 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400267 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400269 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400270 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
273}
274
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700275static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
276{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700277 unsigned int n_events =
278 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
279 EVDEV_MIN_BUFFER_SIZE);
280
281 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700282}
283
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400284static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400286 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400287 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700289 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400290 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400292 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -ENODEV;
294
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400295 error = mutex_lock_interruptible(&evdev_table_mutex);
296 if (error)
297 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400298 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400299 if (evdev)
300 get_device(&evdev->dev);
301 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400302
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400303 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400304 return -ENODEV;
305
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700306 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
307
308 client = kzalloc(sizeof(struct evdev_client) +
309 bufsize * sizeof(struct input_event),
310 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400311 if (!client) {
312 error = -ENOMEM;
313 goto err_put_evdev;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700316 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400317 spin_lock_init(&client->buffer_lock);
Mike Chan2a1f3522009-10-27 17:37:44 -0700318 snprintf(client->name, sizeof(client->name), "%s-%d",
319 dev_name(&evdev->dev), task_tgid_vnr(current));
320 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400321 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400322 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400324 error = evdev_open_device(evdev);
325 if (error)
326 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400328 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800329 nonseekable_open(inode, file);
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400332
333 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400334 evdev_detach_client(evdev, client);
Benoit Goby0ee56952011-01-19 14:10:37 -0800335 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400336 kfree(client);
337 err_put_evdev:
338 put_device(&evdev->dev);
339 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400342static ssize_t evdev_write(struct file *file, const char __user *buffer,
343 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400345 struct evdev_client *client = file->private_data;
346 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct input_event event;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400348 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Peter Korsgaard439581e2011-02-25 09:30:46 -0800350 if (count < input_event_size())
351 return -EINVAL;
352
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400353 retval = mutex_lock_interruptible(&evdev->mutex);
354 if (retval)
355 return retval;
356
357 if (!evdev->exist) {
358 retval = -ENODEV;
359 goto out;
360 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500361
Peter Korsgaard439581e2011-02-25 09:30:46 -0800362 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400363 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400364 retval = -EFAULT;
365 goto out;
366 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800367 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400368
369 input_inject_event(&evdev->handle,
370 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800371 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500372
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400373 out:
374 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500375 return retval;
376}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500377
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400378static int evdev_fetch_next_event(struct evdev_client *client,
379 struct input_event *event)
380{
381 int have_event;
382
383 spin_lock_irq(&client->buffer_lock);
384
385 have_event = client->head != client->tail;
386 if (have_event) {
387 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700388 client->tail &= client->bufsize - 1;
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700389 if (client->head == client->tail)
390 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400391 }
392
393 spin_unlock_irq(&client->buffer_lock);
394
395 return have_event;
396}
397
398static ssize_t evdev_read(struct file *file, char __user *buffer,
399 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400401 struct evdev_client *client = file->private_data;
402 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400403 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int retval;
405
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400406 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return -EINVAL;
408
Jeff Browncdda9112011-04-26 22:16:11 -0700409 if (client->packet_head == client->tail && evdev->exist &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400410 (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return -EAGAIN;
412
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400413 retval = wait_event_interruptible(evdev->wait,
Jeff Browncdda9112011-04-26 22:16:11 -0700414 client->packet_head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (retval)
416 return retval;
417
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400418 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -ENODEV;
420
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400421 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400422 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500423
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400424 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500425 return -EFAULT;
426
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400427 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 return retval;
431}
432
433/* No kernel lock - fine */
434static unsigned int evdev_poll(struct file *file, poll_table *wait)
435{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400436 struct evdev_client *client = file->private_data;
437 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700438 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400439
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400440 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700441
442 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700443 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700444 mask |= POLLIN | POLLRDNORM;
445
446 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500449#ifdef CONFIG_COMPAT
450
451#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700452#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500453
454#ifdef __BIG_ENDIAN
455static int bits_to_user(unsigned long *bits, unsigned int maxbit,
456 unsigned int maxlen, void __user *p, int compat)
457{
458 int len, i;
459
460 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700461 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400462 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500463 len = maxlen;
464
465 for (i = 0; i < len / sizeof(compat_long_t); i++)
466 if (copy_to_user((compat_long_t __user *) p + i,
467 (compat_long_t *) bits +
468 i + 1 - ((i % 2) << 1),
469 sizeof(compat_long_t)))
470 return -EFAULT;
471 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700472 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500473 if (len > maxlen)
474 len = maxlen;
475
476 if (copy_to_user(p, bits, len))
477 return -EFAULT;
478 }
479
480 return len;
481}
482#else
483static int bits_to_user(unsigned long *bits, unsigned int maxbit,
484 unsigned int maxlen, void __user *p, int compat)
485{
486 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700487 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
488 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500489
490 if (len > maxlen)
491 len = maxlen;
492
493 return copy_to_user(p, bits, len) ? -EFAULT : len;
494}
495#endif /* __BIG_ENDIAN */
496
497#else
498
499static int bits_to_user(unsigned long *bits, unsigned int maxbit,
500 unsigned int maxlen, void __user *p, int compat)
501{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700502 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500503
504 if (len > maxlen)
505 len = maxlen;
506
507 return copy_to_user(p, bits, len) ? -EFAULT : len;
508}
509
510#endif /* CONFIG_COMPAT */
511
512static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
513{
514 int len;
515
516 if (!str)
517 return -ENOENT;
518
519 len = strlen(str) + 1;
520 if (len > maxlen)
521 len = maxlen;
522
523 return copy_to_user(p, str, len) ? -EFAULT : len;
524}
525
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400526#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700527static int handle_eviocgbit(struct input_dev *dev,
528 unsigned int type, unsigned int size,
529 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400530{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400531 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400532 unsigned long *bits;
533 int len;
534
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700535 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400536
537 case 0: bits = dev->evbit; len = EV_MAX; break;
538 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
539 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
540 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
541 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
542 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
543 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
544 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
545 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
546 default: return -EINVAL;
547 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400548
549 /*
550 * Work around bugs in userspace programs that like to do
551 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
552 * should be in bytes, not in bits.
553 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700554 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400555 len = OLD_KEY_MAX;
556 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800557 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
558 "limiting output to %zu bytes. See "
559 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
560 OLD_KEY_MAX,
561 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400562 }
563
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700564 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400565}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400566#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400567
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800568static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
569{
570 struct input_keymap_entry ke = {
571 .len = sizeof(unsigned int),
572 .flags = 0,
573 };
574 int __user *ip = (int __user *)p;
575 int error;
576
577 /* legacy case */
578 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
579 return -EFAULT;
580
581 error = input_get_keycode(dev, &ke);
582 if (error)
583 return error;
584
585 if (put_user(ke.keycode, ip + 1))
586 return -EFAULT;
587
588 return 0;
589}
590
591static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700592{
593 struct input_keymap_entry ke;
594 int error;
595
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800596 if (copy_from_user(&ke, p, sizeof(ke)))
597 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700598
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800599 error = input_get_keycode(dev, &ke);
600 if (error)
601 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700602
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800603 if (copy_to_user(p, &ke, sizeof(ke)))
604 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700605
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700606 return 0;
607}
608
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800609static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
610{
611 struct input_keymap_entry ke = {
612 .len = sizeof(unsigned int),
613 .flags = 0,
614 };
615 int __user *ip = (int __user *)p;
616
617 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
618 return -EFAULT;
619
620 if (get_user(ke.keycode, ip + 1))
621 return -EFAULT;
622
623 return input_set_keycode(dev, &ke);
624}
625
626static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700627{
628 struct input_keymap_entry ke;
629
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800630 if (copy_from_user(&ke, p, sizeof(ke)))
631 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700632
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800633 if (ke.len > sizeof(ke.scancode))
634 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700635
636 return input_set_keycode(dev, &ke);
637}
638
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400639static long evdev_do_ioctl(struct file *file, unsigned int cmd,
640 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400642 struct evdev_client *client = file->private_data;
643 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct input_dev *dev = evdev->handle.dev;
645 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400646 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500647 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800648 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700649 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400650 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700652 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 switch (cmd) {
654
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400655 case EVIOCGVERSION:
656 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400658 case EVIOCGID:
659 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
660 return -EFAULT;
661 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400662
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400663 case EVIOCGREP:
664 if (!test_bit(EV_REP, dev->evbit))
665 return -ENOSYS;
666 if (put_user(dev->rep[REP_DELAY], ip))
667 return -EFAULT;
668 if (put_user(dev->rep[REP_PERIOD], ip + 1))
669 return -EFAULT;
670 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400671
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400672 case EVIOCSREP:
673 if (!test_bit(EV_REP, dev->evbit))
674 return -ENOSYS;
675 if (get_user(u, ip))
676 return -EFAULT;
677 if (get_user(v, ip + 1))
678 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400679
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400680 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
681 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500682
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400685 case EVIOCRMFF:
686 return input_ff_erase(dev, (int)(unsigned long) p, file);
687
688 case EVIOCGEFFECTS:
689 i = test_bit(EV_FF, dev->evbit) ?
690 dev->ff->max_effects : 0;
691 if (put_user(i, ip))
692 return -EFAULT;
693 return 0;
694
695 case EVIOCGRAB:
696 if (p)
697 return evdev_grab(evdev, client);
698 else
699 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800700
701 case EVIOCGKEYCODE:
702 return evdev_handle_get_keycode(dev, p);
703
704 case EVIOCSKEYCODE:
705 return evdev_handle_set_keycode(dev, p);
706
707 case EVIOCGKEYCODE_V2:
708 return evdev_handle_get_keycode_v2(dev, p);
709
710 case EVIOCSKEYCODE_V2:
711 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700712 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400713
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700714 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400715
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700716 /* Now check variable-length commands */
717#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700718 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400719
Henrik Rydberg85b77202010-12-18 20:51:13 +0100720 case EVIOCGPROP(0):
721 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
722 size, p, compat_mode);
723
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700724 case EVIOCGKEY(0):
725 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400726
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700727 case EVIOCGLED(0):
728 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400729
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700730 case EVIOCGSND(0):
731 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400732
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700733 case EVIOCGSW(0):
734 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400735
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700736 case EVIOCGNAME(0):
737 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400738
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700739 case EVIOCGPHYS(0):
740 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400741
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700742 case EVIOCGUNIQ(0):
743 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400744
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700745 case EVIOC_MASK_SIZE(EVIOCSFF):
746 if (input_ff_effect_from_user(p, size, &effect))
747 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400748
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700749 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400750
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700751 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
752 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400753
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700754 return error;
755 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400756
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700757 /* Multi-number variable-length handlers */
758 if (_IOC_TYPE(cmd) != 'E')
759 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700761 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700763 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
764 return handle_eviocgbit(dev,
765 _IOC_NR(cmd) & EV_MAX, size,
766 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700768 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400769
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700770 if (!dev->absinfo)
771 return -EINVAL;
772
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700773 t = _IOC_NR(cmd) & ABS_MAX;
774 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400775
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700776 if (copy_to_user(p, &abs, min_t(size_t,
777 size, sizeof(struct input_absinfo))))
778 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400779
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700780 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700783
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700784 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700785
786 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
787
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700788 if (!dev->absinfo)
789 return -EINVAL;
790
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700791 t = _IOC_NR(cmd) & ABS_MAX;
792
793 if (copy_from_user(&abs, p, min_t(size_t,
794 size, sizeof(struct input_absinfo))))
795 return -EFAULT;
796
797 if (size < sizeof(struct input_absinfo))
798 abs.resolution = 0;
799
800 /* We can't change number of reserved MT slots */
801 if (t == ABS_MT_SLOT)
802 return -EINVAL;
803
804 /*
805 * Take event lock to ensure that we are not
806 * changing device parameters in the middle
807 * of event.
808 */
809 spin_lock_irq(&dev->event_lock);
810 dev->absinfo[t] = abs;
811 spin_unlock_irq(&dev->event_lock);
812
813 return 0;
814 }
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return -EINVAL;
818}
819
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400820static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
821 void __user *p, int compat_mode)
822{
823 struct evdev_client *client = file->private_data;
824 struct evdev *evdev = client->evdev;
825 int retval;
826
827 retval = mutex_lock_interruptible(&evdev->mutex);
828 if (retval)
829 return retval;
830
831 if (!evdev->exist) {
832 retval = -ENODEV;
833 goto out;
834 }
835
836 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
837
838 out:
839 mutex_unlock(&evdev->mutex);
840 return retval;
841}
842
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500843static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
844{
845 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
846}
847
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500848#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400849static long evdev_ioctl_compat(struct file *file,
850 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500851{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500852 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500853}
854#endif
855
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400856static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400857 .owner = THIS_MODULE,
858 .read = evdev_read,
859 .write = evdev_write,
860 .poll = evdev_poll,
861 .open = evdev_open,
862 .release = evdev_release,
863 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500864#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400865 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500866#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400867 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200868 .flush = evdev_flush,
869 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870};
871
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400872static int evdev_install_chrdev(struct evdev *evdev)
873{
874 /*
875 * No need to do any locking here as calls to connect and
876 * disconnect are serialized by the input core
877 */
878 evdev_table[evdev->minor] = evdev;
879 return 0;
880}
881
882static void evdev_remove_chrdev(struct evdev *evdev)
883{
884 /*
885 * Lock evdev table to prevent race with evdev_open()
886 */
887 mutex_lock(&evdev_table_mutex);
888 evdev_table[evdev->minor] = NULL;
889 mutex_unlock(&evdev_table_mutex);
890}
891
892/*
893 * Mark device non-existent. This disables writes, ioctls and
894 * prevents new users from opening the device. Already posted
895 * blocking reads will stay, however new ones will fail.
896 */
897static void evdev_mark_dead(struct evdev *evdev)
898{
899 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700900 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400901 mutex_unlock(&evdev->mutex);
902}
903
904static void evdev_cleanup(struct evdev *evdev)
905{
906 struct input_handle *handle = &evdev->handle;
907
908 evdev_mark_dead(evdev);
909 evdev_hangup(evdev);
910 evdev_remove_chrdev(evdev);
911
912 /* evdev is marked dead so no one else accesses evdev->open */
913 if (evdev->open) {
914 input_flush_device(handle, NULL);
915 input_close_device(handle);
916 }
917}
918
919/*
920 * Create new evdev device. Note that input core serializes calls
921 * to connect and disconnect so we don't need to lock evdev_table here.
922 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400923static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
924 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 struct evdev *evdev;
927 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400928 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400930 for (minor = 0; minor < EVDEV_MINORS; minor++)
931 if (!evdev_table[minor])
932 break;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -0800935 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400936 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400939 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
940 if (!evdev)
941 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400943 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400944 spin_lock_init(&evdev->client_lock);
945 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 init_waitqueue_head(&evdev->wait);
947
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700948 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700949 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400951
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400952 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700953 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 evdev->handle.handler = handler;
955 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400956
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400957 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400958 evdev->dev.class = &input_class;
959 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400960 evdev->dev.release = evdev_free;
961 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400963 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400964 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400965 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400967 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400968 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400969 goto err_unregister_handle;
970
971 error = device_add(&evdev->dev);
972 if (error)
973 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400974
975 return 0;
976
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400977 err_cleanup_evdev:
978 evdev_cleanup(evdev);
979 err_unregister_handle:
980 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400981 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400982 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400983 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986static void evdev_disconnect(struct input_handle *handle)
987{
988 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400990 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400991 evdev_cleanup(evdev);
992 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400993 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400996static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 { .driver_info = 1 }, /* Matches all devices */
998 { }, /* Terminating zero entry */
999};
1000
1001MODULE_DEVICE_TABLE(input, evdev_ids);
1002
1003static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001004 .event = evdev_event,
1005 .connect = evdev_connect,
1006 .disconnect = evdev_disconnect,
1007 .fops = &evdev_fops,
1008 .minor = EVDEV_MINOR_BASE,
1009 .name = "evdev",
1010 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011};
1012
1013static int __init evdev_init(void)
1014{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001015 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018static void __exit evdev_exit(void)
1019{
1020 input_unregister_handler(&evdev_handler);
1021}
1022
1023module_init(evdev_init);
1024module_exit(evdev_exit);
1025
1026MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1027MODULE_DESCRIPTION("Input driver event char devices");
1028MODULE_LICENSE("GPL");