blob: 54c7e1ec66910fcfc9e9e601ab9cdf46787397a7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 wake_up_interruptible(&evdev->wait);
122}
123
124static int evdev_fasync(int fd, struct file *file, int on)
125{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400126 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400127
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700128 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400131static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400133 struct evdev_client *client = file->private_data;
134 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400135 int retval;
136
137 retval = mutex_lock_interruptible(&evdev->mutex);
138 if (retval)
139 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400140
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400141 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400142 retval = -ENODEV;
143 else
144 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400145
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400146 mutex_unlock(&evdev->mutex);
147 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400150static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400152 struct evdev *evdev = container_of(dev, struct evdev, dev);
153
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400154 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 kfree(evdev);
156}
157
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400158/*
159 * Grabs an event device (along with underlying input device).
160 * This function is called with evdev->mutex taken.
161 */
162static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
163{
164 int error;
165
166 if (evdev->grab)
167 return -EBUSY;
168
169 error = input_grab_device(&evdev->handle);
170 if (error)
171 return error;
172
173 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400174
175 return 0;
176}
177
178static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
179{
180 if (evdev->grab != client)
181 return -EINVAL;
182
183 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400184 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400185 input_release_device(&evdev->handle);
186
187 return 0;
188}
189
190static void evdev_attach_client(struct evdev *evdev,
191 struct evdev_client *client)
192{
193 spin_lock(&evdev->client_lock);
194 list_add_tail_rcu(&client->node, &evdev->client_list);
195 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400196}
197
198static void evdev_detach_client(struct evdev *evdev,
199 struct evdev_client *client)
200{
201 spin_lock(&evdev->client_lock);
202 list_del_rcu(&client->node);
203 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400204 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400205}
206
207static int evdev_open_device(struct evdev *evdev)
208{
209 int retval;
210
211 retval = mutex_lock_interruptible(&evdev->mutex);
212 if (retval)
213 return retval;
214
215 if (!evdev->exist)
216 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400217 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400218 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400219 if (retval)
220 evdev->open--;
221 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400222
223 mutex_unlock(&evdev->mutex);
224 return retval;
225}
226
227static void evdev_close_device(struct evdev *evdev)
228{
229 mutex_lock(&evdev->mutex);
230
231 if (evdev->exist && !--evdev->open)
232 input_close_device(&evdev->handle);
233
234 mutex_unlock(&evdev->mutex);
235}
236
237/*
238 * Wake up users waiting for IO so they can disconnect from
239 * dead device.
240 */
241static void evdev_hangup(struct evdev *evdev)
242{
243 struct evdev_client *client;
244
245 spin_lock(&evdev->client_lock);
246 list_for_each_entry(client, &evdev->client_list, node)
247 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
248 spin_unlock(&evdev->client_lock);
249
250 wake_up_interruptible(&evdev->wait);
251}
252
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400253static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400255 struct evdev_client *client = file->private_data;
256 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400258 mutex_lock(&evdev->mutex);
259 if (evdev->grab == client)
260 evdev_ungrab(evdev, client);
261 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400263 evdev_detach_client(evdev, client);
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700264 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400265 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400267 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400268 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
271}
272
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700273static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
274{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700275 unsigned int n_events =
276 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
277 EVDEV_MIN_BUFFER_SIZE);
278
279 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700280}
281
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400282static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400284 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400285 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700287 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400288 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400290 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -ENODEV;
292
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400293 error = mutex_lock_interruptible(&evdev_table_mutex);
294 if (error)
295 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400296 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400297 if (evdev)
298 get_device(&evdev->dev);
299 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400300
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400301 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400302 return -ENODEV;
303
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700304 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
305
306 client = kzalloc(sizeof(struct evdev_client) +
307 bufsize * sizeof(struct input_event),
308 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400309 if (!client) {
310 error = -ENOMEM;
311 goto err_put_evdev;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700314 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400315 spin_lock_init(&client->buffer_lock);
Mike Chan2a1f3522009-10-27 17:37:44 -0700316 snprintf(client->name, sizeof(client->name), "%s-%d",
317 dev_name(&evdev->dev), task_tgid_vnr(current));
318 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400319 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400320 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400322 error = evdev_open_device(evdev);
323 if (error)
324 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400326 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800327 nonseekable_open(inode, file);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400330
331 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400332 evdev_detach_client(evdev, client);
Benoit Goby0ee56952011-01-19 14:10:37 -0800333 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400334 kfree(client);
335 err_put_evdev:
336 put_device(&evdev->dev);
337 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400340static ssize_t evdev_write(struct file *file, const char __user *buffer,
341 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400343 struct evdev_client *client = file->private_data;
344 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct input_event event;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400346 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Peter Korsgaard439581e2011-02-25 09:30:46 -0800348 if (count < input_event_size())
349 return -EINVAL;
350
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400351 retval = mutex_lock_interruptible(&evdev->mutex);
352 if (retval)
353 return retval;
354
355 if (!evdev->exist) {
356 retval = -ENODEV;
357 goto out;
358 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500359
Peter Korsgaard439581e2011-02-25 09:30:46 -0800360 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400361 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400362 retval = -EFAULT;
363 goto out;
364 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800365 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400366
367 input_inject_event(&evdev->handle,
368 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800369 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500370
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400371 out:
372 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500373 return retval;
374}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500375
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400376static int evdev_fetch_next_event(struct evdev_client *client,
377 struct input_event *event)
378{
379 int have_event;
380
381 spin_lock_irq(&client->buffer_lock);
382
383 have_event = client->head != client->tail;
384 if (have_event) {
385 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700386 client->tail &= client->bufsize - 1;
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700387 if (client->head == client->tail)
388 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400389 }
390
391 spin_unlock_irq(&client->buffer_lock);
392
393 return have_event;
394}
395
396static ssize_t evdev_read(struct file *file, char __user *buffer,
397 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400399 struct evdev_client *client = file->private_data;
400 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400401 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int retval;
403
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400404 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -EINVAL;
406
Jeff Browncdda9112011-04-26 22:16:11 -0700407 if (client->packet_head == client->tail && evdev->exist &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400408 (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return -EAGAIN;
410
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400411 retval = wait_event_interruptible(evdev->wait,
Jeff Browncdda9112011-04-26 22:16:11 -0700412 client->packet_head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (retval)
414 return retval;
415
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400416 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -ENODEV;
418
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400419 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400420 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500421
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400422 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500423 return -EFAULT;
424
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400425 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
428 return retval;
429}
430
431/* No kernel lock - fine */
432static unsigned int evdev_poll(struct file *file, poll_table *wait)
433{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400434 struct evdev_client *client = file->private_data;
435 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700436 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400437
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400438 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700439
440 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700441 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700442 mask |= POLLIN | POLLRDNORM;
443
444 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500447#ifdef CONFIG_COMPAT
448
449#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700450#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500451
452#ifdef __BIG_ENDIAN
453static int bits_to_user(unsigned long *bits, unsigned int maxbit,
454 unsigned int maxlen, void __user *p, int compat)
455{
456 int len, i;
457
458 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700459 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400460 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500461 len = maxlen;
462
463 for (i = 0; i < len / sizeof(compat_long_t); i++)
464 if (copy_to_user((compat_long_t __user *) p + i,
465 (compat_long_t *) bits +
466 i + 1 - ((i % 2) << 1),
467 sizeof(compat_long_t)))
468 return -EFAULT;
469 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700470 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500471 if (len > maxlen)
472 len = maxlen;
473
474 if (copy_to_user(p, bits, len))
475 return -EFAULT;
476 }
477
478 return len;
479}
480#else
481static int bits_to_user(unsigned long *bits, unsigned int maxbit,
482 unsigned int maxlen, void __user *p, int compat)
483{
484 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700485 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
486 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500487
488 if (len > maxlen)
489 len = maxlen;
490
491 return copy_to_user(p, bits, len) ? -EFAULT : len;
492}
493#endif /* __BIG_ENDIAN */
494
495#else
496
497static int bits_to_user(unsigned long *bits, unsigned int maxbit,
498 unsigned int maxlen, void __user *p, int compat)
499{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700500 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500501
502 if (len > maxlen)
503 len = maxlen;
504
505 return copy_to_user(p, bits, len) ? -EFAULT : len;
506}
507
508#endif /* CONFIG_COMPAT */
509
510static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
511{
512 int len;
513
514 if (!str)
515 return -ENOENT;
516
517 len = strlen(str) + 1;
518 if (len > maxlen)
519 len = maxlen;
520
521 return copy_to_user(p, str, len) ? -EFAULT : len;
522}
523
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400524#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700525static int handle_eviocgbit(struct input_dev *dev,
526 unsigned int type, unsigned int size,
527 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400528{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400529 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400530 unsigned long *bits;
531 int len;
532
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700533 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400534
535 case 0: bits = dev->evbit; len = EV_MAX; break;
536 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
537 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
538 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
539 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
540 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
541 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
542 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
543 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
544 default: return -EINVAL;
545 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400546
547 /*
548 * Work around bugs in userspace programs that like to do
549 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
550 * should be in bytes, not in bits.
551 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700552 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400553 len = OLD_KEY_MAX;
554 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800555 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
556 "limiting output to %zu bytes. See "
557 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
558 OLD_KEY_MAX,
559 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400560 }
561
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700562 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400563}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400564#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400565
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800566static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
567{
568 struct input_keymap_entry ke = {
569 .len = sizeof(unsigned int),
570 .flags = 0,
571 };
572 int __user *ip = (int __user *)p;
573 int error;
574
575 /* legacy case */
576 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
577 return -EFAULT;
578
579 error = input_get_keycode(dev, &ke);
580 if (error)
581 return error;
582
583 if (put_user(ke.keycode, ip + 1))
584 return -EFAULT;
585
586 return 0;
587}
588
589static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700590{
591 struct input_keymap_entry ke;
592 int error;
593
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800594 if (copy_from_user(&ke, p, sizeof(ke)))
595 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700596
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800597 error = input_get_keycode(dev, &ke);
598 if (error)
599 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700600
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800601 if (copy_to_user(p, &ke, sizeof(ke)))
602 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700603
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700604 return 0;
605}
606
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800607static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
608{
609 struct input_keymap_entry ke = {
610 .len = sizeof(unsigned int),
611 .flags = 0,
612 };
613 int __user *ip = (int __user *)p;
614
615 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
616 return -EFAULT;
617
618 if (get_user(ke.keycode, ip + 1))
619 return -EFAULT;
620
621 return input_set_keycode(dev, &ke);
622}
623
624static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700625{
626 struct input_keymap_entry ke;
627
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800628 if (copy_from_user(&ke, p, sizeof(ke)))
629 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700630
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800631 if (ke.len > sizeof(ke.scancode))
632 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700633
634 return input_set_keycode(dev, &ke);
635}
636
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400637static long evdev_do_ioctl(struct file *file, unsigned int cmd,
638 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400640 struct evdev_client *client = file->private_data;
641 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct input_dev *dev = evdev->handle.dev;
643 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400644 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500645 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800646 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700647 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400648 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700650 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 switch (cmd) {
652
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400653 case EVIOCGVERSION:
654 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400656 case EVIOCGID:
657 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
658 return -EFAULT;
659 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400660
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400661 case EVIOCGREP:
662 if (!test_bit(EV_REP, dev->evbit))
663 return -ENOSYS;
664 if (put_user(dev->rep[REP_DELAY], ip))
665 return -EFAULT;
666 if (put_user(dev->rep[REP_PERIOD], ip + 1))
667 return -EFAULT;
668 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400669
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400670 case EVIOCSREP:
671 if (!test_bit(EV_REP, dev->evbit))
672 return -ENOSYS;
673 if (get_user(u, ip))
674 return -EFAULT;
675 if (get_user(v, ip + 1))
676 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400677
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400678 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
679 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500680
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400683 case EVIOCRMFF:
684 return input_ff_erase(dev, (int)(unsigned long) p, file);
685
686 case EVIOCGEFFECTS:
687 i = test_bit(EV_FF, dev->evbit) ?
688 dev->ff->max_effects : 0;
689 if (put_user(i, ip))
690 return -EFAULT;
691 return 0;
692
693 case EVIOCGRAB:
694 if (p)
695 return evdev_grab(evdev, client);
696 else
697 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800698
699 case EVIOCGKEYCODE:
700 return evdev_handle_get_keycode(dev, p);
701
702 case EVIOCSKEYCODE:
703 return evdev_handle_set_keycode(dev, p);
704
705 case EVIOCGKEYCODE_V2:
706 return evdev_handle_get_keycode_v2(dev, p);
707
708 case EVIOCSKEYCODE_V2:
709 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700710 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400711
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700712 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400713
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700714 /* Now check variable-length commands */
715#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700716 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400717
Henrik Rydberg85b77202010-12-18 20:51:13 +0100718 case EVIOCGPROP(0):
719 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
720 size, p, compat_mode);
721
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700722 case EVIOCGKEY(0):
723 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400724
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700725 case EVIOCGLED(0):
726 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400727
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700728 case EVIOCGSND(0):
729 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400730
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700731 case EVIOCGSW(0):
732 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400733
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700734 case EVIOCGNAME(0):
735 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400736
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700737 case EVIOCGPHYS(0):
738 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400739
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700740 case EVIOCGUNIQ(0):
741 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400742
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700743 case EVIOC_MASK_SIZE(EVIOCSFF):
744 if (input_ff_effect_from_user(p, size, &effect))
745 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400746
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700747 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400748
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700749 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
750 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400751
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700752 return error;
753 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400754
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700755 /* Multi-number variable-length handlers */
756 if (_IOC_TYPE(cmd) != 'E')
757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700759 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700761 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
762 return handle_eviocgbit(dev,
763 _IOC_NR(cmd) & EV_MAX, size,
764 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700766 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400767
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700768 if (!dev->absinfo)
769 return -EINVAL;
770
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700771 t = _IOC_NR(cmd) & ABS_MAX;
772 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400773
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700774 if (copy_to_user(p, &abs, min_t(size_t,
775 size, sizeof(struct input_absinfo))))
776 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400777
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700778 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700781
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700782 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700783
784 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
785
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700786 if (!dev->absinfo)
787 return -EINVAL;
788
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700789 t = _IOC_NR(cmd) & ABS_MAX;
790
791 if (copy_from_user(&abs, p, min_t(size_t,
792 size, sizeof(struct input_absinfo))))
793 return -EFAULT;
794
795 if (size < sizeof(struct input_absinfo))
796 abs.resolution = 0;
797
798 /* We can't change number of reserved MT slots */
799 if (t == ABS_MT_SLOT)
800 return -EINVAL;
801
802 /*
803 * Take event lock to ensure that we are not
804 * changing device parameters in the middle
805 * of event.
806 */
807 spin_lock_irq(&dev->event_lock);
808 dev->absinfo[t] = abs;
809 spin_unlock_irq(&dev->event_lock);
810
811 return 0;
812 }
813 }
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return -EINVAL;
816}
817
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400818static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
819 void __user *p, int compat_mode)
820{
821 struct evdev_client *client = file->private_data;
822 struct evdev *evdev = client->evdev;
823 int retval;
824
825 retval = mutex_lock_interruptible(&evdev->mutex);
826 if (retval)
827 return retval;
828
829 if (!evdev->exist) {
830 retval = -ENODEV;
831 goto out;
832 }
833
834 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
835
836 out:
837 mutex_unlock(&evdev->mutex);
838 return retval;
839}
840
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500841static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
842{
843 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
844}
845
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500846#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400847static long evdev_ioctl_compat(struct file *file,
848 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500849{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500850 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500851}
852#endif
853
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400854static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400855 .owner = THIS_MODULE,
856 .read = evdev_read,
857 .write = evdev_write,
858 .poll = evdev_poll,
859 .open = evdev_open,
860 .release = evdev_release,
861 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500862#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400863 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500864#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400865 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200866 .flush = evdev_flush,
867 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868};
869
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400870static int evdev_install_chrdev(struct evdev *evdev)
871{
872 /*
873 * No need to do any locking here as calls to connect and
874 * disconnect are serialized by the input core
875 */
876 evdev_table[evdev->minor] = evdev;
877 return 0;
878}
879
880static void evdev_remove_chrdev(struct evdev *evdev)
881{
882 /*
883 * Lock evdev table to prevent race with evdev_open()
884 */
885 mutex_lock(&evdev_table_mutex);
886 evdev_table[evdev->minor] = NULL;
887 mutex_unlock(&evdev_table_mutex);
888}
889
890/*
891 * Mark device non-existent. This disables writes, ioctls and
892 * prevents new users from opening the device. Already posted
893 * blocking reads will stay, however new ones will fail.
894 */
895static void evdev_mark_dead(struct evdev *evdev)
896{
897 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700898 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400899 mutex_unlock(&evdev->mutex);
900}
901
902static void evdev_cleanup(struct evdev *evdev)
903{
904 struct input_handle *handle = &evdev->handle;
905
906 evdev_mark_dead(evdev);
907 evdev_hangup(evdev);
908 evdev_remove_chrdev(evdev);
909
910 /* evdev is marked dead so no one else accesses evdev->open */
911 if (evdev->open) {
912 input_flush_device(handle, NULL);
913 input_close_device(handle);
914 }
915}
916
917/*
918 * Create new evdev device. Note that input core serializes calls
919 * to connect and disconnect so we don't need to lock evdev_table here.
920 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400921static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
922 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct evdev *evdev;
925 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400926 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400928 for (minor = 0; minor < EVDEV_MINORS; minor++)
929 if (!evdev_table[minor])
930 break;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -0800933 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400934 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400937 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
938 if (!evdev)
939 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400941 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400942 spin_lock_init(&evdev->client_lock);
943 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 init_waitqueue_head(&evdev->wait);
945
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700946 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700947 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400949
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400950 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700951 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 evdev->handle.handler = handler;
953 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400954
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400955 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400956 evdev->dev.class = &input_class;
957 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400958 evdev->dev.release = evdev_free;
959 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400961 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400962 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400963 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400965 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400966 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400967 goto err_unregister_handle;
968
969 error = device_add(&evdev->dev);
970 if (error)
971 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400972
973 return 0;
974
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400975 err_cleanup_evdev:
976 evdev_cleanup(evdev);
977 err_unregister_handle:
978 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400979 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400980 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400981 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984static void evdev_disconnect(struct input_handle *handle)
985{
986 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400988 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400989 evdev_cleanup(evdev);
990 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400991 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400994static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 { .driver_info = 1 }, /* Matches all devices */
996 { }, /* Terminating zero entry */
997};
998
999MODULE_DEVICE_TABLE(input, evdev_ids);
1000
1001static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001002 .event = evdev_event,
1003 .connect = evdev_connect,
1004 .disconnect = evdev_disconnect,
1005 .fops = &evdev_fops,
1006 .minor = EVDEV_MINOR_BASE,
1007 .name = "evdev",
1008 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009};
1010
1011static int __init evdev_init(void)
1012{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001013 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016static void __exit evdev_exit(void)
1017{
1018 input_unregister_handler(&evdev_handler);
1019}
1020
1021module_init(evdev_init);
1022module_exit(evdev_exit);
1023
1024MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1025MODULE_DESCRIPTION("Input driver event char devices");
1026MODULE_LICENSE("GPL");