blob: 88d8e4cb419a1d0d6cc17828894dc4d3b67e0201 [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>
Philip Langdale2d56f3a2008-10-16 22:31:42 -040026#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28struct evdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 int open;
30 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 struct input_handle handle;
32 wait_queue_head_t wait;
Arnd Bergmann2be85272010-03-04 15:50:28 +010033 struct evdev_client __rcu *grab;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040034 struct list_head client_list;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040035 spinlock_t client_lock; /* protects client_list */
36 struct mutex mutex;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040037 struct device dev;
Dmitry Torokhov20da92d2010-07-15 23:27:36 -070038 bool exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040041struct evdev_client {
Jeff Brown9fb0f142011-04-12 23:29:38 -070042 unsigned int head;
43 unsigned int tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040044 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct fasync_struct *fasync;
46 struct evdev *evdev;
47 struct list_head node;
Jeff Brown9fb0f142011-04-12 23:29:38 -070048 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070049 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040053static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040055static void evdev_pass_event(struct evdev_client *client,
56 struct input_event *event)
57{
Jeff Brown9fb0f142011-04-12 23:29:38 -070058 /* Interrupts are disabled, just acquire the lock. */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040059 spin_lock(&client->buffer_lock);
Jeff Brown9fb0f142011-04-12 23:29:38 -070060
61 client->buffer[client->head++] = *event;
62 client->head &= client->bufsize - 1;
63
64 if (unlikely(client->head == client->tail)) {
65 /*
66 * This effectively "drops" all unconsumed events, leaving
67 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
68 */
69 client->tail = (client->head - 2) & (client->bufsize - 1);
70
71 client->buffer[client->tail].time = event->time;
72 client->buffer[client->tail].type = EV_SYN;
73 client->buffer[client->tail].code = SYN_DROPPED;
74 client->buffer[client->tail].value = 0;
75 }
76
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040077 spin_unlock(&client->buffer_lock);
78
Adam Jackson30a589f2010-01-05 17:56:04 -080079 if (event->type == EV_SYN)
80 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040081}
82
83/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040084 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040085 */
86static void evdev_event(struct input_handle *handle,
87 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040090 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040091 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040093 do_gettimeofday(&event.time);
94 event.type = type;
95 event.code = code;
96 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040098 rcu_read_lock();
99
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400100 client = rcu_dereference(evdev->grab);
101 if (client)
102 evdev_pass_event(client, &event);
103 else
104 list_for_each_entry_rcu(client, &evdev->client_list, node)
105 evdev_pass_event(client, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400107 rcu_read_unlock();
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 wake_up_interruptible(&evdev->wait);
110}
111
112static int evdev_fasync(int fd, struct file *file, int on)
113{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400114 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400115
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700116 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400119static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400121 struct evdev_client *client = file->private_data;
122 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400123 int retval;
124
125 retval = mutex_lock_interruptible(&evdev->mutex);
126 if (retval)
127 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400128
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400129 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400130 retval = -ENODEV;
131 else
132 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400133
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400134 mutex_unlock(&evdev->mutex);
135 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400138static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400140 struct evdev *evdev = container_of(dev, struct evdev, dev);
141
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400142 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 kfree(evdev);
144}
145
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400146/*
147 * Grabs an event device (along with underlying input device).
148 * This function is called with evdev->mutex taken.
149 */
150static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
151{
152 int error;
153
154 if (evdev->grab)
155 return -EBUSY;
156
157 error = input_grab_device(&evdev->handle);
158 if (error)
159 return error;
160
161 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400162 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400163
164 return 0;
165}
166
167static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
168{
169 if (evdev->grab != client)
170 return -EINVAL;
171
172 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400173 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400174 input_release_device(&evdev->handle);
175
176 return 0;
177}
178
179static void evdev_attach_client(struct evdev *evdev,
180 struct evdev_client *client)
181{
182 spin_lock(&evdev->client_lock);
183 list_add_tail_rcu(&client->node, &evdev->client_list);
184 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400185 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400186}
187
188static void evdev_detach_client(struct evdev *evdev,
189 struct evdev_client *client)
190{
191 spin_lock(&evdev->client_lock);
192 list_del_rcu(&client->node);
193 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400194 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400195}
196
197static int evdev_open_device(struct evdev *evdev)
198{
199 int retval;
200
201 retval = mutex_lock_interruptible(&evdev->mutex);
202 if (retval)
203 return retval;
204
205 if (!evdev->exist)
206 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400207 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400208 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400209 if (retval)
210 evdev->open--;
211 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400212
213 mutex_unlock(&evdev->mutex);
214 return retval;
215}
216
217static void evdev_close_device(struct evdev *evdev)
218{
219 mutex_lock(&evdev->mutex);
220
221 if (evdev->exist && !--evdev->open)
222 input_close_device(&evdev->handle);
223
224 mutex_unlock(&evdev->mutex);
225}
226
227/*
228 * Wake up users waiting for IO so they can disconnect from
229 * dead device.
230 */
231static void evdev_hangup(struct evdev *evdev)
232{
233 struct evdev_client *client;
234
235 spin_lock(&evdev->client_lock);
236 list_for_each_entry(client, &evdev->client_list, node)
237 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
238 spin_unlock(&evdev->client_lock);
239
240 wake_up_interruptible(&evdev->wait);
241}
242
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400243static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400245 struct evdev_client *client = file->private_data;
246 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400248 mutex_lock(&evdev->mutex);
249 if (evdev->grab == client)
250 evdev_ungrab(evdev, client);
251 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400253 evdev_detach_client(evdev, client);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400254 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400256 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400257 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 0;
260}
261
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700262static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
263{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700264 unsigned int n_events =
265 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
266 EVDEV_MIN_BUFFER_SIZE);
267
268 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700269}
270
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400271static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400273 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400274 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700276 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400277 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400279 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -ENODEV;
281
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400282 error = mutex_lock_interruptible(&evdev_table_mutex);
283 if (error)
284 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400285 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400286 if (evdev)
287 get_device(&evdev->dev);
288 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400289
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400290 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400291 return -ENODEV;
292
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700293 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
294
295 client = kzalloc(sizeof(struct evdev_client) +
296 bufsize * sizeof(struct input_event),
297 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400298 if (!client) {
299 error = -ENOMEM;
300 goto err_put_evdev;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700303 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400304 spin_lock_init(&client->buffer_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400305 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400306 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400308 error = evdev_open_device(evdev);
309 if (error)
310 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400312 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800313 nonseekable_open(inode, file);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400316
317 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400318 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400319 kfree(client);
320 err_put_evdev:
321 put_device(&evdev->dev);
322 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400325static ssize_t evdev_write(struct file *file, const char __user *buffer,
326 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400328 struct evdev_client *client = file->private_data;
329 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct input_event event;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400331 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Peter Korsgaard439581e2011-02-25 09:30:46 -0800333 if (count < input_event_size())
334 return -EINVAL;
335
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400336 retval = mutex_lock_interruptible(&evdev->mutex);
337 if (retval)
338 return retval;
339
340 if (!evdev->exist) {
341 retval = -ENODEV;
342 goto out;
343 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500344
Peter Korsgaard439581e2011-02-25 09:30:46 -0800345 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400346 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400347 retval = -EFAULT;
348 goto out;
349 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800350 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400351
352 input_inject_event(&evdev->handle,
353 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800354 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500355
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400356 out:
357 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500358 return retval;
359}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500360
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400361static int evdev_fetch_next_event(struct evdev_client *client,
362 struct input_event *event)
363{
364 int have_event;
365
366 spin_lock_irq(&client->buffer_lock);
367
368 have_event = client->head != client->tail;
369 if (have_event) {
370 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700371 client->tail &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400372 }
373
374 spin_unlock_irq(&client->buffer_lock);
375
376 return have_event;
377}
378
379static ssize_t evdev_read(struct file *file, char __user *buffer,
380 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400382 struct evdev_client *client = file->private_data;
383 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400384 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int retval;
386
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400387 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EINVAL;
389
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400390 if (client->head == client->tail && evdev->exist &&
391 (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -EAGAIN;
393
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400394 retval = wait_event_interruptible(evdev->wait,
395 client->head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (retval)
397 return retval;
398
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400399 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return -ENODEV;
401
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400402 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400403 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500404
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400405 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500406 return -EFAULT;
407
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400408 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
411 return retval;
412}
413
414/* No kernel lock - fine */
415static unsigned int evdev_poll(struct file *file, poll_table *wait)
416{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400417 struct evdev_client *client = file->private_data;
418 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700419 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400420
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400421 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700422
423 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
424 if (client->head != client->tail)
425 mask |= POLLIN | POLLRDNORM;
426
427 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500430#ifdef CONFIG_COMPAT
431
432#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700433#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500434
435#ifdef __BIG_ENDIAN
436static int bits_to_user(unsigned long *bits, unsigned int maxbit,
437 unsigned int maxlen, void __user *p, int compat)
438{
439 int len, i;
440
441 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700442 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400443 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500444 len = maxlen;
445
446 for (i = 0; i < len / sizeof(compat_long_t); i++)
447 if (copy_to_user((compat_long_t __user *) p + i,
448 (compat_long_t *) bits +
449 i + 1 - ((i % 2) << 1),
450 sizeof(compat_long_t)))
451 return -EFAULT;
452 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700453 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500454 if (len > maxlen)
455 len = maxlen;
456
457 if (copy_to_user(p, bits, len))
458 return -EFAULT;
459 }
460
461 return len;
462}
463#else
464static int bits_to_user(unsigned long *bits, unsigned int maxbit,
465 unsigned int maxlen, void __user *p, int compat)
466{
467 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700468 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
469 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500470
471 if (len > maxlen)
472 len = maxlen;
473
474 return copy_to_user(p, bits, len) ? -EFAULT : len;
475}
476#endif /* __BIG_ENDIAN */
477
478#else
479
480static int bits_to_user(unsigned long *bits, unsigned int maxbit,
481 unsigned int maxlen, void __user *p, int compat)
482{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700483 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500484
485 if (len > maxlen)
486 len = maxlen;
487
488 return copy_to_user(p, bits, len) ? -EFAULT : len;
489}
490
491#endif /* CONFIG_COMPAT */
492
493static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
494{
495 int len;
496
497 if (!str)
498 return -ENOENT;
499
500 len = strlen(str) + 1;
501 if (len > maxlen)
502 len = maxlen;
503
504 return copy_to_user(p, str, len) ? -EFAULT : len;
505}
506
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400507#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700508static int handle_eviocgbit(struct input_dev *dev,
509 unsigned int type, unsigned int size,
510 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400511{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400512 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400513 unsigned long *bits;
514 int len;
515
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700516 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400517
518 case 0: bits = dev->evbit; len = EV_MAX; break;
519 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
520 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
521 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
522 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
523 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
524 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
525 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
526 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
527 default: return -EINVAL;
528 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400529
530 /*
531 * Work around bugs in userspace programs that like to do
532 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
533 * should be in bytes, not in bits.
534 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700535 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400536 len = OLD_KEY_MAX;
537 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800538 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
539 "limiting output to %zu bytes. See "
540 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
541 OLD_KEY_MAX,
542 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400543 }
544
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700545 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400546}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400547#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400548
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800549static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
550{
551 struct input_keymap_entry ke = {
552 .len = sizeof(unsigned int),
553 .flags = 0,
554 };
555 int __user *ip = (int __user *)p;
556 int error;
557
558 /* legacy case */
559 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
560 return -EFAULT;
561
562 error = input_get_keycode(dev, &ke);
563 if (error)
564 return error;
565
566 if (put_user(ke.keycode, ip + 1))
567 return -EFAULT;
568
569 return 0;
570}
571
572static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700573{
574 struct input_keymap_entry ke;
575 int error;
576
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800577 if (copy_from_user(&ke, p, sizeof(ke)))
578 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700579
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800580 error = input_get_keycode(dev, &ke);
581 if (error)
582 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700583
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800584 if (copy_to_user(p, &ke, sizeof(ke)))
585 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700586
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700587 return 0;
588}
589
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800590static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
591{
592 struct input_keymap_entry ke = {
593 .len = sizeof(unsigned int),
594 .flags = 0,
595 };
596 int __user *ip = (int __user *)p;
597
598 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
599 return -EFAULT;
600
601 if (get_user(ke.keycode, ip + 1))
602 return -EFAULT;
603
604 return input_set_keycode(dev, &ke);
605}
606
607static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700608{
609 struct input_keymap_entry ke;
610
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800611 if (copy_from_user(&ke, p, sizeof(ke)))
612 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700613
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800614 if (ke.len > sizeof(ke.scancode))
615 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700616
617 return input_set_keycode(dev, &ke);
618}
619
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400620static long evdev_do_ioctl(struct file *file, unsigned int cmd,
621 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400623 struct evdev_client *client = file->private_data;
624 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct input_dev *dev = evdev->handle.dev;
626 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400627 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500628 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800629 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700630 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400631 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700633 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 switch (cmd) {
635
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400636 case EVIOCGVERSION:
637 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400639 case EVIOCGID:
640 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
641 return -EFAULT;
642 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400643
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400644 case EVIOCGREP:
645 if (!test_bit(EV_REP, dev->evbit))
646 return -ENOSYS;
647 if (put_user(dev->rep[REP_DELAY], ip))
648 return -EFAULT;
649 if (put_user(dev->rep[REP_PERIOD], ip + 1))
650 return -EFAULT;
651 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400652
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400653 case EVIOCSREP:
654 if (!test_bit(EV_REP, dev->evbit))
655 return -ENOSYS;
656 if (get_user(u, ip))
657 return -EFAULT;
658 if (get_user(v, ip + 1))
659 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400660
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400661 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
662 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500663
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400666 case EVIOCRMFF:
667 return input_ff_erase(dev, (int)(unsigned long) p, file);
668
669 case EVIOCGEFFECTS:
670 i = test_bit(EV_FF, dev->evbit) ?
671 dev->ff->max_effects : 0;
672 if (put_user(i, ip))
673 return -EFAULT;
674 return 0;
675
676 case EVIOCGRAB:
677 if (p)
678 return evdev_grab(evdev, client);
679 else
680 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800681
682 case EVIOCGKEYCODE:
683 return evdev_handle_get_keycode(dev, p);
684
685 case EVIOCSKEYCODE:
686 return evdev_handle_set_keycode(dev, p);
687
688 case EVIOCGKEYCODE_V2:
689 return evdev_handle_get_keycode_v2(dev, p);
690
691 case EVIOCSKEYCODE_V2:
692 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700693 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400694
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700695 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400696
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700697 /* Now check variable-length commands */
698#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700699 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400700
Henrik Rydberg85b77202010-12-18 20:51:13 +0100701 case EVIOCGPROP(0):
702 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
703 size, p, compat_mode);
704
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700705 case EVIOCGKEY(0):
706 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400707
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700708 case EVIOCGLED(0):
709 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400710
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700711 case EVIOCGSND(0):
712 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400713
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700714 case EVIOCGSW(0):
715 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400716
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700717 case EVIOCGNAME(0):
718 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400719
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700720 case EVIOCGPHYS(0):
721 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400722
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700723 case EVIOCGUNIQ(0):
724 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400725
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700726 case EVIOC_MASK_SIZE(EVIOCSFF):
727 if (input_ff_effect_from_user(p, size, &effect))
728 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400729
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700730 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400731
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700732 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
733 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400734
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700735 return error;
736 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400737
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700738 /* Multi-number variable-length handlers */
739 if (_IOC_TYPE(cmd) != 'E')
740 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700742 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700744 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
745 return handle_eviocgbit(dev,
746 _IOC_NR(cmd) & EV_MAX, size,
747 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700749 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400750
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700751 if (!dev->absinfo)
752 return -EINVAL;
753
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700754 t = _IOC_NR(cmd) & ABS_MAX;
755 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400756
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700757 if (copy_to_user(p, &abs, min_t(size_t,
758 size, sizeof(struct input_absinfo))))
759 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400760
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700761 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700764
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700765 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700766
767 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
768
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700769 if (!dev->absinfo)
770 return -EINVAL;
771
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700772 t = _IOC_NR(cmd) & ABS_MAX;
773
774 if (copy_from_user(&abs, p, min_t(size_t,
775 size, sizeof(struct input_absinfo))))
776 return -EFAULT;
777
778 if (size < sizeof(struct input_absinfo))
779 abs.resolution = 0;
780
781 /* We can't change number of reserved MT slots */
782 if (t == ABS_MT_SLOT)
783 return -EINVAL;
784
785 /*
786 * Take event lock to ensure that we are not
787 * changing device parameters in the middle
788 * of event.
789 */
790 spin_lock_irq(&dev->event_lock);
791 dev->absinfo[t] = abs;
792 spin_unlock_irq(&dev->event_lock);
793
794 return 0;
795 }
796 }
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -EINVAL;
799}
800
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400801static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
802 void __user *p, int compat_mode)
803{
804 struct evdev_client *client = file->private_data;
805 struct evdev *evdev = client->evdev;
806 int retval;
807
808 retval = mutex_lock_interruptible(&evdev->mutex);
809 if (retval)
810 return retval;
811
812 if (!evdev->exist) {
813 retval = -ENODEV;
814 goto out;
815 }
816
817 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
818
819 out:
820 mutex_unlock(&evdev->mutex);
821 return retval;
822}
823
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500824static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
825{
826 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
827}
828
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500829#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400830static long evdev_ioctl_compat(struct file *file,
831 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500832{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500833 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500834}
835#endif
836
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400837static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400838 .owner = THIS_MODULE,
839 .read = evdev_read,
840 .write = evdev_write,
841 .poll = evdev_poll,
842 .open = evdev_open,
843 .release = evdev_release,
844 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500845#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400846 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500847#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400848 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200849 .flush = evdev_flush,
850 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851};
852
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400853static int evdev_install_chrdev(struct evdev *evdev)
854{
855 /*
856 * No need to do any locking here as calls to connect and
857 * disconnect are serialized by the input core
858 */
859 evdev_table[evdev->minor] = evdev;
860 return 0;
861}
862
863static void evdev_remove_chrdev(struct evdev *evdev)
864{
865 /*
866 * Lock evdev table to prevent race with evdev_open()
867 */
868 mutex_lock(&evdev_table_mutex);
869 evdev_table[evdev->minor] = NULL;
870 mutex_unlock(&evdev_table_mutex);
871}
872
873/*
874 * Mark device non-existent. This disables writes, ioctls and
875 * prevents new users from opening the device. Already posted
876 * blocking reads will stay, however new ones will fail.
877 */
878static void evdev_mark_dead(struct evdev *evdev)
879{
880 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700881 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400882 mutex_unlock(&evdev->mutex);
883}
884
885static void evdev_cleanup(struct evdev *evdev)
886{
887 struct input_handle *handle = &evdev->handle;
888
889 evdev_mark_dead(evdev);
890 evdev_hangup(evdev);
891 evdev_remove_chrdev(evdev);
892
893 /* evdev is marked dead so no one else accesses evdev->open */
894 if (evdev->open) {
895 input_flush_device(handle, NULL);
896 input_close_device(handle);
897 }
898}
899
900/*
901 * Create new evdev device. Note that input core serializes calls
902 * to connect and disconnect so we don't need to lock evdev_table here.
903 */
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400904static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
905 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct evdev *evdev;
908 int minor;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400909 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400911 for (minor = 0; minor < EVDEV_MINORS; minor++)
912 if (!evdev_table[minor])
913 break;
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -0800916 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400917 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400920 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
921 if (!evdev)
922 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400924 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400925 spin_lock_init(&evdev->client_lock);
926 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 init_waitqueue_head(&evdev->wait);
928
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700929 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700930 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400932
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400933 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700934 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 evdev->handle.handler = handler;
936 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400937
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400938 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400939 evdev->dev.class = &input_class;
940 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400941 evdev->dev.release = evdev_free;
942 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400944 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400945 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400946 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400948 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400949 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400950 goto err_unregister_handle;
951
952 error = device_add(&evdev->dev);
953 if (error)
954 goto err_cleanup_evdev;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400955
956 return 0;
957
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400958 err_cleanup_evdev:
959 evdev_cleanup(evdev);
960 err_unregister_handle:
961 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400962 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400963 put_device(&evdev->dev);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400964 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967static void evdev_disconnect(struct input_handle *handle)
968{
969 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400971 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400972 evdev_cleanup(evdev);
973 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400974 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400977static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 { .driver_info = 1 }, /* Matches all devices */
979 { }, /* Terminating zero entry */
980};
981
982MODULE_DEVICE_TABLE(input, evdev_ids);
983
984static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400985 .event = evdev_event,
986 .connect = evdev_connect,
987 .disconnect = evdev_disconnect,
988 .fops = &evdev_fops,
989 .minor = EVDEV_MINOR_BASE,
990 .name = "evdev",
991 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992};
993
994static int __init evdev_init(void)
995{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400996 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static void __exit evdev_exit(void)
1000{
1001 input_unregister_handler(&evdev_handler);
1002}
1003
1004module_init(evdev_init);
1005module_exit(evdev_exit);
1006
1007MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1008MODULE_DESCRIPTION("Input driver event char devices");
1009MODULE_LICENSE("GPL");