blob: 1848c9e563384df8be46417f5f88c8d5569fece6 [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);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400333 kfree(client);
334 err_put_evdev:
335 put_device(&evdev->dev);
336 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400339static ssize_t evdev_write(struct file *file, const char __user *buffer,
340 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400342 struct evdev_client *client = file->private_data;
343 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct input_event event;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400345 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Peter Korsgaard439581e2011-02-25 09:30:46 -0800347 if (count < input_event_size())
348 return -EINVAL;
349
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400350 retval = mutex_lock_interruptible(&evdev->mutex);
351 if (retval)
352 return retval;
353
354 if (!evdev->exist) {
355 retval = -ENODEV;
356 goto out;
357 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500358
Peter Korsgaard439581e2011-02-25 09:30:46 -0800359 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400360 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400361 retval = -EFAULT;
362 goto out;
363 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800364 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400365
366 input_inject_event(&evdev->handle,
367 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800368 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500369
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400370 out:
371 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500372 return retval;
373}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500374
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400375static int evdev_fetch_next_event(struct evdev_client *client,
376 struct input_event *event)
377{
378 int have_event;
379
380 spin_lock_irq(&client->buffer_lock);
381
382 have_event = client->head != client->tail;
383 if (have_event) {
384 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700385 client->tail &= client->bufsize - 1;
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700386 if (client->head == client->tail)
387 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400388 }
389
390 spin_unlock_irq(&client->buffer_lock);
391
392 return have_event;
393}
394
395static ssize_t evdev_read(struct file *file, char __user *buffer,
396 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400398 struct evdev_client *client = file->private_data;
399 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400400 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int retval;
402
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400403 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return -EINVAL;
405
Jeff Browncdda9112011-04-26 22:16:11 -0700406 if (client->packet_head == client->tail && evdev->exist &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400407 (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -EAGAIN;
409
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400410 retval = wait_event_interruptible(evdev->wait,
Jeff Browncdda9112011-04-26 22:16:11 -0700411 client->packet_head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (retval)
413 return retval;
414
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
427 return retval;
428}
429
430/* No kernel lock - fine */
431static unsigned int evdev_poll(struct file *file, poll_table *wait)
432{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400433 struct evdev_client *client = file->private_data;
434 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700435 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400436
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400437 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700438
439 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700440 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700441 mask |= POLLIN | POLLRDNORM;
442
443 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500446#ifdef CONFIG_COMPAT
447
448#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700449#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500450
451#ifdef __BIG_ENDIAN
452static int bits_to_user(unsigned long *bits, unsigned int maxbit,
453 unsigned int maxlen, void __user *p, int compat)
454{
455 int len, i;
456
457 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700458 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400459 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500460 len = maxlen;
461
462 for (i = 0; i < len / sizeof(compat_long_t); i++)
463 if (copy_to_user((compat_long_t __user *) p + i,
464 (compat_long_t *) bits +
465 i + 1 - ((i % 2) << 1),
466 sizeof(compat_long_t)))
467 return -EFAULT;
468 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700469 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500470 if (len > maxlen)
471 len = maxlen;
472
473 if (copy_to_user(p, bits, len))
474 return -EFAULT;
475 }
476
477 return len;
478}
479#else
480static int bits_to_user(unsigned long *bits, unsigned int maxbit,
481 unsigned int maxlen, void __user *p, int compat)
482{
483 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700484 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
485 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500486
487 if (len > maxlen)
488 len = maxlen;
489
490 return copy_to_user(p, bits, len) ? -EFAULT : len;
491}
492#endif /* __BIG_ENDIAN */
493
494#else
495
496static int bits_to_user(unsigned long *bits, unsigned int maxbit,
497 unsigned int maxlen, void __user *p, int compat)
498{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700499 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500500
501 if (len > maxlen)
502 len = maxlen;
503
504 return copy_to_user(p, bits, len) ? -EFAULT : len;
505}
506
507#endif /* CONFIG_COMPAT */
508
509static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
510{
511 int len;
512
513 if (!str)
514 return -ENOENT;
515
516 len = strlen(str) + 1;
517 if (len > maxlen)
518 len = maxlen;
519
520 return copy_to_user(p, str, len) ? -EFAULT : len;
521}
522
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400523#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700524static int handle_eviocgbit(struct input_dev *dev,
525 unsigned int type, unsigned int size,
526 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400527{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400528 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400529 unsigned long *bits;
530 int len;
531
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700532 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400533
534 case 0: bits = dev->evbit; len = EV_MAX; break;
535 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
536 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
537 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
538 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
539 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
540 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
541 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
542 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
543 default: return -EINVAL;
544 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400545
546 /*
547 * Work around bugs in userspace programs that like to do
548 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
549 * should be in bytes, not in bits.
550 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700551 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400552 len = OLD_KEY_MAX;
553 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800554 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
555 "limiting output to %zu bytes. See "
556 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
557 OLD_KEY_MAX,
558 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400559 }
560
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700561 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400562}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400563#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400564
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800565static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
566{
567 struct input_keymap_entry ke = {
568 .len = sizeof(unsigned int),
569 .flags = 0,
570 };
571 int __user *ip = (int __user *)p;
572 int error;
573
574 /* legacy case */
575 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
576 return -EFAULT;
577
578 error = input_get_keycode(dev, &ke);
579 if (error)
580 return error;
581
582 if (put_user(ke.keycode, ip + 1))
583 return -EFAULT;
584
585 return 0;
586}
587
588static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700589{
590 struct input_keymap_entry ke;
591 int error;
592
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800593 if (copy_from_user(&ke, p, sizeof(ke)))
594 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700595
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800596 error = input_get_keycode(dev, &ke);
597 if (error)
598 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700599
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800600 if (copy_to_user(p, &ke, sizeof(ke)))
601 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700602
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700603 return 0;
604}
605
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800606static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
607{
608 struct input_keymap_entry ke = {
609 .len = sizeof(unsigned int),
610 .flags = 0,
611 };
612 int __user *ip = (int __user *)p;
613
614 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
615 return -EFAULT;
616
617 if (get_user(ke.keycode, ip + 1))
618 return -EFAULT;
619
620 return input_set_keycode(dev, &ke);
621}
622
623static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700624{
625 struct input_keymap_entry ke;
626
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800627 if (copy_from_user(&ke, p, sizeof(ke)))
628 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700629
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800630 if (ke.len > sizeof(ke.scancode))
631 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700632
633 return input_set_keycode(dev, &ke);
634}
635
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400636static long evdev_do_ioctl(struct file *file, unsigned int cmd,
637 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400639 struct evdev_client *client = file->private_data;
640 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct input_dev *dev = evdev->handle.dev;
642 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400643 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500644 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800645 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700646 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400647 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700649 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 switch (cmd) {
651
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400652 case EVIOCGVERSION:
653 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400655 case EVIOCGID:
656 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
657 return -EFAULT;
658 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400659
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400660 case EVIOCGREP:
661 if (!test_bit(EV_REP, dev->evbit))
662 return -ENOSYS;
663 if (put_user(dev->rep[REP_DELAY], ip))
664 return -EFAULT;
665 if (put_user(dev->rep[REP_PERIOD], ip + 1))
666 return -EFAULT;
667 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400668
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400669 case EVIOCSREP:
670 if (!test_bit(EV_REP, dev->evbit))
671 return -ENOSYS;
672 if (get_user(u, ip))
673 return -EFAULT;
674 if (get_user(v, ip + 1))
675 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400676
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400677 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
678 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500679
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400682 case EVIOCRMFF:
683 return input_ff_erase(dev, (int)(unsigned long) p, file);
684
685 case EVIOCGEFFECTS:
686 i = test_bit(EV_FF, dev->evbit) ?
687 dev->ff->max_effects : 0;
688 if (put_user(i, ip))
689 return -EFAULT;
690 return 0;
691
692 case EVIOCGRAB:
693 if (p)
694 return evdev_grab(evdev, client);
695 else
696 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800697
698 case EVIOCGKEYCODE:
699 return evdev_handle_get_keycode(dev, p);
700
701 case EVIOCSKEYCODE:
702 return evdev_handle_set_keycode(dev, p);
703
704 case EVIOCGKEYCODE_V2:
705 return evdev_handle_get_keycode_v2(dev, p);
706
707 case EVIOCSKEYCODE_V2:
708 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700709 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400710
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700711 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400712
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700713 /* Now check variable-length commands */
714#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700715 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400716
Henrik Rydberg85b77202010-12-18 20:51:13 +0100717 case EVIOCGPROP(0):
718 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
719 size, p, compat_mode);
720
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700721 case EVIOCGKEY(0):
722 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400723
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700724 case EVIOCGLED(0):
725 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400726
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700727 case EVIOCGSND(0):
728 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400729
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700730 case EVIOCGSW(0):
731 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400732
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700733 case EVIOCGNAME(0):
734 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400735
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700736 case EVIOCGPHYS(0):
737 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400738
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700739 case EVIOCGUNIQ(0):
740 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400741
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700742 case EVIOC_MASK_SIZE(EVIOCSFF):
743 if (input_ff_effect_from_user(p, size, &effect))
744 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400745
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700746 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400747
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700748 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
749 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400750
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700751 return error;
752 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400753
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700754 /* Multi-number variable-length handlers */
755 if (_IOC_TYPE(cmd) != 'E')
756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700758 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700760 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
761 return handle_eviocgbit(dev,
762 _IOC_NR(cmd) & EV_MAX, size,
763 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700765 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400766
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700767 if (!dev->absinfo)
768 return -EINVAL;
769
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700770 t = _IOC_NR(cmd) & ABS_MAX;
771 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400772
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700773 if (copy_to_user(p, &abs, min_t(size_t,
774 size, sizeof(struct input_absinfo))))
775 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400776
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700777 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700780
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700781 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700782
783 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
784
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700785 if (!dev->absinfo)
786 return -EINVAL;
787
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700788 t = _IOC_NR(cmd) & ABS_MAX;
789
790 if (copy_from_user(&abs, p, min_t(size_t,
791 size, sizeof(struct input_absinfo))))
792 return -EFAULT;
793
794 if (size < sizeof(struct input_absinfo))
795 abs.resolution = 0;
796
797 /* We can't change number of reserved MT slots */
798 if (t == ABS_MT_SLOT)
799 return -EINVAL;
800
801 /*
802 * Take event lock to ensure that we are not
803 * changing device parameters in the middle
804 * of event.
805 */
806 spin_lock_irq(&dev->event_lock);
807 dev->absinfo[t] = abs;
808 spin_unlock_irq(&dev->event_lock);
809
810 return 0;
811 }
812 }
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -EINVAL;
815}
816
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400817static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
818 void __user *p, int compat_mode)
819{
820 struct evdev_client *client = file->private_data;
821 struct evdev *evdev = client->evdev;
822 int retval;
823
824 retval = mutex_lock_interruptible(&evdev->mutex);
825 if (retval)
826 return retval;
827
828 if (!evdev->exist) {
829 retval = -ENODEV;
830 goto out;
831 }
832
833 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
834
835 out:
836 mutex_unlock(&evdev->mutex);
837 return retval;
838}
839
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500840static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
841{
842 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
843}
844
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500845#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400846static long evdev_ioctl_compat(struct file *file,
847 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500848{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500849 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500850}
851#endif
852
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400853static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400854 .owner = THIS_MODULE,
855 .read = evdev_read,
856 .write = evdev_write,
857 .poll = evdev_poll,
858 .open = evdev_open,
859 .release = evdev_release,
860 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500861#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400862 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500863#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400864 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200865 .flush = evdev_flush,
866 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867};
868
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400869static int evdev_install_chrdev(struct evdev *evdev)
870{
871 /*
872 * No need to do any locking here as calls to connect and
873 * disconnect are serialized by the input core
874 */
875 evdev_table[evdev->minor] = evdev;
876 return 0;
877}
878
879static void evdev_remove_chrdev(struct evdev *evdev)
880{
881 /*
882 * Lock evdev table to prevent race with evdev_open()
883 */
884 mutex_lock(&evdev_table_mutex);
885 evdev_table[evdev->minor] = NULL;
886 mutex_unlock(&evdev_table_mutex);
887}
888
889/*
890 * Mark device non-existent. This disables writes, ioctls and
891 * prevents new users from opening the device. Already posted
892 * blocking reads will stay, however new ones will fail.
893 */
894static void evdev_mark_dead(struct evdev *evdev)
895{
896 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700897 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400898 mutex_unlock(&evdev->mutex);
899}
900
901static void evdev_cleanup(struct evdev *evdev)
902{
903 struct input_handle *handle = &evdev->handle;
904
905 evdev_mark_dead(evdev);
906 evdev_hangup(evdev);
907 evdev_remove_chrdev(evdev);
908
909 /* evdev is marked dead so no one else accesses evdev->open */
910 if (evdev->open) {
911 input_flush_device(handle, NULL);
912 input_close_device(handle);
913 }
914}
915
916/*
917 * Create new evdev device. Note that input core serializes calls
918 * to connect and disconnect so we don't need to lock evdev_table here.
919 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400920static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
921 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct evdev *evdev;
924 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400925 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400927 for (minor = 0; minor < EVDEV_MINORS; minor++)
928 if (!evdev_table[minor])
929 break;
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -0800932 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400933 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400936 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
937 if (!evdev)
938 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400940 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400941 spin_lock_init(&evdev->client_lock);
942 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 init_waitqueue_head(&evdev->wait);
944
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700945 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700946 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400948
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400949 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700950 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 evdev->handle.handler = handler;
952 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400953
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400954 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400955 evdev->dev.class = &input_class;
956 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400957 evdev->dev.release = evdev_free;
958 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400960 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400961 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400962 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400964 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400965 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400966 goto err_unregister_handle;
967
968 error = device_add(&evdev->dev);
969 if (error)
970 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400971
972 return 0;
973
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400974 err_cleanup_evdev:
975 evdev_cleanup(evdev);
976 err_unregister_handle:
977 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400978 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400979 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400980 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983static void evdev_disconnect(struct input_handle *handle)
984{
985 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400987 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400988 evdev_cleanup(evdev);
989 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400990 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400993static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 { .driver_info = 1 }, /* Matches all devices */
995 { }, /* Terminating zero entry */
996};
997
998MODULE_DEVICE_TABLE(input, evdev_ids);
999
1000static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001001 .event = evdev_event,
1002 .connect = evdev_connect,
1003 .disconnect = evdev_disconnect,
1004 .fops = &evdev_fops,
1005 .minor = EVDEV_MINOR_BASE,
1006 .name = "evdev",
1007 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008};
1009
1010static int __init evdev_init(void)
1011{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001012 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015static void __exit evdev_exit(void)
1016{
1017 input_unregister_handler(&evdev_handler);
1018}
1019
1020module_init(evdev_init);
1021module_exit(evdev_exit);
1022
1023MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1024MODULE_DESCRIPTION("Input driver event char devices");
1025MODULE_LICENSE("GPL");