blob: cff7bf9351a849caa77a53c6b2b039c1f0fa4397 [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
11#define EVDEV_MINOR_BASE 64
12#define EVDEV_MINORS 32
Henrik Rydbergb58f7082010-06-23 09:17:56 -070013#define EVDEV_MIN_BUFFER_SIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <linux/poll.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040016#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/input.h>
21#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/device.h>
Philip Langdale2d56f3a2008-10-16 22:31:42 -040023#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25struct evdev {
26 int exist;
27 int open;
28 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 struct input_handle handle;
30 wait_queue_head_t wait;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040031 struct evdev_client *grab;
32 struct list_head client_list;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040033 spinlock_t client_lock; /* protects client_list */
34 struct mutex mutex;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040035 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040038struct evdev_client {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 int head;
40 int tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040041 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct fasync_struct *fasync;
43 struct evdev *evdev;
44 struct list_head node;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070045 int bufsize;
46 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
49static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040050static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040052static void evdev_pass_event(struct evdev_client *client,
53 struct input_event *event)
54{
55 /*
56 * Interrupts are disabled, just acquire the lock
57 */
58 spin_lock(&client->buffer_lock);
59 client->buffer[client->head++] = *event;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070060 client->head &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040061 spin_unlock(&client->buffer_lock);
62
Adam Jackson30a589f2010-01-05 17:56:04 -080063 if (event->type == EV_SYN)
64 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040065}
66
67/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040068 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040069 */
70static void evdev_event(struct input_handle *handle,
71 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040074 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040075 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040077 do_gettimeofday(&event.time);
78 event.type = type;
79 event.code = code;
80 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040082 rcu_read_lock();
83
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040084 client = rcu_dereference(evdev->grab);
85 if (client)
86 evdev_pass_event(client, &event);
87 else
88 list_for_each_entry_rcu(client, &evdev->client_list, node)
89 evdev_pass_event(client, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040091 rcu_read_unlock();
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 wake_up_interruptible(&evdev->wait);
94}
95
96static int evdev_fasync(int fd, struct file *file, int on)
97{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040098 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040099
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700100 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400103static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400105 struct evdev_client *client = file->private_data;
106 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400107 int retval;
108
109 retval = mutex_lock_interruptible(&evdev->mutex);
110 if (retval)
111 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400112
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400113 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400114 retval = -ENODEV;
115 else
116 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400117
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400118 mutex_unlock(&evdev->mutex);
119 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400122static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400124 struct evdev *evdev = container_of(dev, struct evdev, dev);
125
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400126 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 kfree(evdev);
128}
129
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400130/*
131 * Grabs an event device (along with underlying input device).
132 * This function is called with evdev->mutex taken.
133 */
134static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
135{
136 int error;
137
138 if (evdev->grab)
139 return -EBUSY;
140
141 error = input_grab_device(&evdev->handle);
142 if (error)
143 return error;
144
145 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400146 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400147
148 return 0;
149}
150
151static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
152{
153 if (evdev->grab != client)
154 return -EINVAL;
155
156 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400157 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400158 input_release_device(&evdev->handle);
159
160 return 0;
161}
162
163static void evdev_attach_client(struct evdev *evdev,
164 struct evdev_client *client)
165{
166 spin_lock(&evdev->client_lock);
167 list_add_tail_rcu(&client->node, &evdev->client_list);
168 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400169 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400170}
171
172static void evdev_detach_client(struct evdev *evdev,
173 struct evdev_client *client)
174{
175 spin_lock(&evdev->client_lock);
176 list_del_rcu(&client->node);
177 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400178 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400179}
180
181static int evdev_open_device(struct evdev *evdev)
182{
183 int retval;
184
185 retval = mutex_lock_interruptible(&evdev->mutex);
186 if (retval)
187 return retval;
188
189 if (!evdev->exist)
190 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400191 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400192 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400193 if (retval)
194 evdev->open--;
195 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400196
197 mutex_unlock(&evdev->mutex);
198 return retval;
199}
200
201static void evdev_close_device(struct evdev *evdev)
202{
203 mutex_lock(&evdev->mutex);
204
205 if (evdev->exist && !--evdev->open)
206 input_close_device(&evdev->handle);
207
208 mutex_unlock(&evdev->mutex);
209}
210
211/*
212 * Wake up users waiting for IO so they can disconnect from
213 * dead device.
214 */
215static void evdev_hangup(struct evdev *evdev)
216{
217 struct evdev_client *client;
218
219 spin_lock(&evdev->client_lock);
220 list_for_each_entry(client, &evdev->client_list, node)
221 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
222 spin_unlock(&evdev->client_lock);
223
224 wake_up_interruptible(&evdev->wait);
225}
226
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400227static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400229 struct evdev_client *client = file->private_data;
230 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400232 mutex_lock(&evdev->mutex);
233 if (evdev->grab == client)
234 evdev_ungrab(evdev, client);
235 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400237 evdev_detach_client(evdev, client);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400238 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400240 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400241 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700246static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
247{
248 return EVDEV_MIN_BUFFER_SIZE;
249}
250
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400251static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400253 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400254 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700256 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400257 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400259 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -ENODEV;
261
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400262 error = mutex_lock_interruptible(&evdev_table_mutex);
263 if (error)
264 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400265 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400266 if (evdev)
267 get_device(&evdev->dev);
268 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400269
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400270 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400271 return -ENODEV;
272
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700273 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
274
275 client = kzalloc(sizeof(struct evdev_client) +
276 bufsize * sizeof(struct input_event),
277 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400278 if (!client) {
279 error = -ENOMEM;
280 goto err_put_evdev;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700283 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400284 spin_lock_init(&client->buffer_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400285 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400286 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400288 error = evdev_open_device(evdev);
289 if (error)
290 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400292 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800293 nonseekable_open(inode, file);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400296
297 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400298 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400299 kfree(client);
300 err_put_evdev:
301 put_device(&evdev->dev);
302 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400305static ssize_t evdev_write(struct file *file, const char __user *buffer,
306 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400308 struct evdev_client *client = file->private_data;
309 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct input_event event;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400311 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400313 retval = mutex_lock_interruptible(&evdev->mutex);
314 if (retval)
315 return retval;
316
317 if (!evdev->exist) {
318 retval = -ENODEV;
319 goto out;
320 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500321
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500322 while (retval < count) {
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500323
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400324 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400325 retval = -EFAULT;
326 goto out;
327 }
328
329 input_inject_event(&evdev->handle,
330 event.type, event.code, event.value);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400331 retval += input_event_size();
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500332 }
333
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400334 out:
335 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500336 return retval;
337}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500338
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400339static int evdev_fetch_next_event(struct evdev_client *client,
340 struct input_event *event)
341{
342 int have_event;
343
344 spin_lock_irq(&client->buffer_lock);
345
346 have_event = client->head != client->tail;
347 if (have_event) {
348 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700349 client->tail &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400350 }
351
352 spin_unlock_irq(&client->buffer_lock);
353
354 return have_event;
355}
356
357static ssize_t evdev_read(struct file *file, char __user *buffer,
358 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400360 struct evdev_client *client = file->private_data;
361 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400362 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int retval;
364
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400365 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return -EINVAL;
367
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400368 if (client->head == client->tail && evdev->exist &&
369 (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return -EAGAIN;
371
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400372 retval = wait_event_interruptible(evdev->wait,
373 client->head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (retval)
375 return retval;
376
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400377 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return -ENODEV;
379
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400380 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400381 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500382
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400383 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500384 return -EFAULT;
385
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400386 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 return retval;
390}
391
392/* No kernel lock - fine */
393static unsigned int evdev_poll(struct file *file, poll_table *wait)
394{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400395 struct evdev_client *client = file->private_data;
396 struct evdev *evdev = client->evdev;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400397
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400398 poll_wait(file, &evdev->wait, wait);
399 return ((client->head == client->tail) ? 0 : (POLLIN | POLLRDNORM)) |
400 (evdev->exist ? 0 : (POLLHUP | POLLERR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500403#ifdef CONFIG_COMPAT
404
405#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700406#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500407
408#ifdef __BIG_ENDIAN
409static int bits_to_user(unsigned long *bits, unsigned int maxbit,
410 unsigned int maxlen, void __user *p, int compat)
411{
412 int len, i;
413
414 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700415 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400416 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500417 len = maxlen;
418
419 for (i = 0; i < len / sizeof(compat_long_t); i++)
420 if (copy_to_user((compat_long_t __user *) p + i,
421 (compat_long_t *) bits +
422 i + 1 - ((i % 2) << 1),
423 sizeof(compat_long_t)))
424 return -EFAULT;
425 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700426 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500427 if (len > maxlen)
428 len = maxlen;
429
430 if (copy_to_user(p, bits, len))
431 return -EFAULT;
432 }
433
434 return len;
435}
436#else
437static int bits_to_user(unsigned long *bits, unsigned int maxbit,
438 unsigned int maxlen, void __user *p, int compat)
439{
440 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700441 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
442 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500443
444 if (len > maxlen)
445 len = maxlen;
446
447 return copy_to_user(p, bits, len) ? -EFAULT : len;
448}
449#endif /* __BIG_ENDIAN */
450
451#else
452
453static int bits_to_user(unsigned long *bits, unsigned int maxbit,
454 unsigned int maxlen, void __user *p, int compat)
455{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700456 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500457
458 if (len > maxlen)
459 len = maxlen;
460
461 return copy_to_user(p, bits, len) ? -EFAULT : len;
462}
463
464#endif /* CONFIG_COMPAT */
465
466static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
467{
468 int len;
469
470 if (!str)
471 return -ENOENT;
472
473 len = strlen(str) + 1;
474 if (len > maxlen)
475 len = maxlen;
476
477 return copy_to_user(p, str, len) ? -EFAULT : len;
478}
479
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400480#define OLD_KEY_MAX 0x1ff
Linus Torvalds5402a732008-08-05 11:42:42 -0400481static int handle_eviocgbit(struct input_dev *dev, unsigned int cmd, void __user *p, int compat_mode)
482{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400483 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400484 unsigned long *bits;
485 int len;
486
487 switch (_IOC_NR(cmd) & EV_MAX) {
488
489 case 0: bits = dev->evbit; len = EV_MAX; break;
490 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
491 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
492 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
493 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
494 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
495 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
496 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
497 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
498 default: return -EINVAL;
499 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400500
501 /*
502 * Work around bugs in userspace programs that like to do
503 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
504 * should be in bytes, not in bits.
505 */
506 if ((_IOC_NR(cmd) & EV_MAX) == EV_KEY && _IOC_SIZE(cmd) == OLD_KEY_MAX) {
507 len = OLD_KEY_MAX;
508 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
509 printk(KERN_WARNING
Geert Uytterhoevenc85e2032008-08-19 11:28:23 -0400510 "evdev.c(EVIOCGBIT): Suspicious buffer size %u, "
511 "limiting output to %zu bytes. See "
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400512 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
513 OLD_KEY_MAX,
514 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
515 }
516
Linus Torvalds5402a732008-08-05 11:42:42 -0400517 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
518}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400519#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400520
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400521static long evdev_do_ioctl(struct file *file, unsigned int cmd,
522 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400524 struct evdev_client *client = file->private_data;
525 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct input_dev *dev = evdev->handle.dev;
527 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400528 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500529 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800530 unsigned int i, t, u, v;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400531 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 switch (cmd) {
534
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400535 case EVIOCGVERSION:
536 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400538 case EVIOCGID:
539 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
540 return -EFAULT;
541 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400542
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400543 case EVIOCGREP:
544 if (!test_bit(EV_REP, dev->evbit))
545 return -ENOSYS;
546 if (put_user(dev->rep[REP_DELAY], ip))
547 return -EFAULT;
548 if (put_user(dev->rep[REP_PERIOD], ip + 1))
549 return -EFAULT;
550 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400551
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400552 case EVIOCSREP:
553 if (!test_bit(EV_REP, dev->evbit))
554 return -ENOSYS;
555 if (get_user(u, ip))
556 return -EFAULT;
557 if (get_user(v, ip + 1))
558 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400559
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400560 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
561 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500562
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400565 case EVIOCGKEYCODE:
566 if (get_user(t, ip))
567 return -EFAULT;
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400568
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400569 error = input_get_keycode(dev, t, &v);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400570 if (error)
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400571 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400573 if (put_user(v, ip + 1))
574 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400578 case EVIOCSKEYCODE:
579 if (get_user(t, ip) || get_user(v, ip + 1))
580 return -EFAULT;
581
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400582 return input_set_keycode(dev, t, v);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400583
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400584 case EVIOCRMFF:
585 return input_ff_erase(dev, (int)(unsigned long) p, file);
586
587 case EVIOCGEFFECTS:
588 i = test_bit(EV_FF, dev->evbit) ?
589 dev->ff->max_effects : 0;
590 if (put_user(i, ip))
591 return -EFAULT;
592 return 0;
593
594 case EVIOCGRAB:
595 if (p)
596 return evdev_grab(evdev, client);
597 else
598 return evdev_ungrab(evdev, client);
599
600 default:
601
602 if (_IOC_TYPE(cmd) != 'E')
603 return -EINVAL;
604
605 if (_IOC_DIR(cmd) == _IOC_READ) {
606
Linus Torvalds5402a732008-08-05 11:42:42 -0400607 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
608 return handle_eviocgbit(dev, cmd, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400609
610 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
611 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
612 p, compat_mode);
613
614 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
615 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd),
616 p, compat_mode);
617
618 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
619 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd),
620 p, compat_mode);
621
622 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
623 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
624 p, compat_mode);
625
626 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
Daniel Mackf9366012009-07-13 22:22:49 -0700627 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400628
629 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
630 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
631
632 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
633 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
634
635 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
636
637 t = _IOC_NR(cmd) & ABS_MAX;
638
639 abs.value = dev->abs[t];
640 abs.minimum = dev->absmin[t];
641 abs.maximum = dev->absmax[t];
642 abs.fuzz = dev->absfuzz[t];
643 abs.flat = dev->absflat[t];
Tero Saarniec20a022009-06-10 23:27:24 -0700644 abs.resolution = dev->absres[t];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400645
Tero Saarniec20a022009-06-10 23:27:24 -0700646 if (copy_to_user(p, &abs, min_t(size_t,
647 _IOC_SIZE(cmd),
648 sizeof(struct input_absinfo))))
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400649 return -EFAULT;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652 }
653
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400656 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400658 if (_IOC_NR(cmd) == _IOC_NR(EVIOCSFF)) {
659
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400660 if (input_ff_effect_from_user(p, _IOC_SIZE(cmd), &effect))
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400661 return -EFAULT;
662
663 error = input_ff_upload(dev, &effect, file);
664
665 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
666 return -EFAULT;
667
668 return error;
669 }
670
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400671 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400673 t = _IOC_NR(cmd) & ABS_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Tero Saarniec20a022009-06-10 23:27:24 -0700675 if (copy_from_user(&abs, p, min_t(size_t,
676 _IOC_SIZE(cmd),
677 sizeof(struct input_absinfo))))
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400678 return -EFAULT;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500679
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400680 /*
681 * Take event lock to ensure that we are not
682 * changing device parameters in the middle
683 * of event.
684 */
685 spin_lock_irq(&dev->event_lock);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500686
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400687 dev->abs[t] = abs.value;
688 dev->absmin[t] = abs.minimum;
689 dev->absmax[t] = abs.maximum;
690 dev->absfuzz[t] = abs.fuzz;
691 dev->absflat[t] = abs.flat;
Tero Saarniec20a022009-06-10 23:27:24 -0700692 dev->absres[t] = _IOC_SIZE(cmd) < sizeof(struct input_absinfo) ?
693 0 : abs.resolution;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500694
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400695 spin_unlock_irq(&dev->event_lock);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500696
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701 return -EINVAL;
702}
703
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400704static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
705 void __user *p, int compat_mode)
706{
707 struct evdev_client *client = file->private_data;
708 struct evdev *evdev = client->evdev;
709 int retval;
710
711 retval = mutex_lock_interruptible(&evdev->mutex);
712 if (retval)
713 return retval;
714
715 if (!evdev->exist) {
716 retval = -ENODEV;
717 goto out;
718 }
719
720 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
721
722 out:
723 mutex_unlock(&evdev->mutex);
724 return retval;
725}
726
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500727static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
728{
729 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
730}
731
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500732#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400733static long evdev_ioctl_compat(struct file *file,
734 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500735{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500736 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500737}
738#endif
739
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400740static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400741 .owner = THIS_MODULE,
742 .read = evdev_read,
743 .write = evdev_write,
744 .poll = evdev_poll,
745 .open = evdev_open,
746 .release = evdev_release,
747 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500748#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400749 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500750#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400751 .fasync = evdev_fasync,
752 .flush = evdev_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753};
754
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400755static int evdev_install_chrdev(struct evdev *evdev)
756{
757 /*
758 * No need to do any locking here as calls to connect and
759 * disconnect are serialized by the input core
760 */
761 evdev_table[evdev->minor] = evdev;
762 return 0;
763}
764
765static void evdev_remove_chrdev(struct evdev *evdev)
766{
767 /*
768 * Lock evdev table to prevent race with evdev_open()
769 */
770 mutex_lock(&evdev_table_mutex);
771 evdev_table[evdev->minor] = NULL;
772 mutex_unlock(&evdev_table_mutex);
773}
774
775/*
776 * Mark device non-existent. This disables writes, ioctls and
777 * prevents new users from opening the device. Already posted
778 * blocking reads will stay, however new ones will fail.
779 */
780static void evdev_mark_dead(struct evdev *evdev)
781{
782 mutex_lock(&evdev->mutex);
783 evdev->exist = 0;
784 mutex_unlock(&evdev->mutex);
785}
786
787static void evdev_cleanup(struct evdev *evdev)
788{
789 struct input_handle *handle = &evdev->handle;
790
791 evdev_mark_dead(evdev);
792 evdev_hangup(evdev);
793 evdev_remove_chrdev(evdev);
794
795 /* evdev is marked dead so no one else accesses evdev->open */
796 if (evdev->open) {
797 input_flush_device(handle, NULL);
798 input_close_device(handle);
799 }
800}
801
802/*
803 * Create new evdev device. Note that input core serializes calls
804 * to connect and disconnect so we don't need to lock evdev_table here.
805 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400806static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
807 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct evdev *evdev;
810 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400811 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400813 for (minor = 0; minor < EVDEV_MINORS; minor++)
814 if (!evdev_table[minor])
815 break;
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (minor == EVDEV_MINORS) {
818 printk(KERN_ERR "evdev: no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400819 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400822 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
823 if (!evdev)
824 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400826 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400827 spin_lock_init(&evdev->client_lock);
828 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 init_waitqueue_head(&evdev->wait);
830
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700831 dev_set_name(&evdev->dev, "event%d", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 evdev->exist = 1;
833 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400834
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400835 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700836 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 evdev->handle.handler = handler;
838 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400839
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400840 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400841 evdev->dev.class = &input_class;
842 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400843 evdev->dev.release = evdev_free;
844 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400846 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400847 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400848 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400850 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400851 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400852 goto err_unregister_handle;
853
854 error = device_add(&evdev->dev);
855 if (error)
856 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400857
858 return 0;
859
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400860 err_cleanup_evdev:
861 evdev_cleanup(evdev);
862 err_unregister_handle:
863 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400864 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400865 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400866 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869static void evdev_disconnect(struct input_handle *handle)
870{
871 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400873 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400874 evdev_cleanup(evdev);
875 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400876 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400879static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 { .driver_info = 1 }, /* Matches all devices */
881 { }, /* Terminating zero entry */
882};
883
884MODULE_DEVICE_TABLE(input, evdev_ids);
885
886static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400887 .event = evdev_event,
888 .connect = evdev_connect,
889 .disconnect = evdev_disconnect,
890 .fops = &evdev_fops,
891 .minor = EVDEV_MINOR_BASE,
892 .name = "evdev",
893 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894};
895
896static int __init evdev_init(void)
897{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400898 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static void __exit evdev_exit(void)
902{
903 input_unregister_handler(&evdev_handler);
904}
905
906module_init(evdev_init);
907module_exit(evdev_exit);
908
909MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
910MODULE_DESCRIPTION("Input driver event char devices");
911MODULE_LICENSE("GPL");