blob: 75c4e86830331dbedd933a07b13cc723135df517 [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;
Jeff Browncdda9112011-04-26 22:16:11 -070044 unsigned int packet_head; /* [future] position of the first element of next packet */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040045 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct fasync_struct *fasync;
47 struct evdev *evdev;
48 struct list_head node;
Jeff Brown9fb0f142011-04-12 23:29:38 -070049 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070050 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040054static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040056static void evdev_pass_event(struct evdev_client *client,
57 struct input_event *event)
58{
Jeff Brown9fb0f142011-04-12 23:29:38 -070059 /* Interrupts are disabled, just acquire the lock. */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040060 spin_lock(&client->buffer_lock);
Jeff Brown9fb0f142011-04-12 23:29:38 -070061
62 client->buffer[client->head++] = *event;
63 client->head &= client->bufsize - 1;
64
65 if (unlikely(client->head == client->tail)) {
66 /*
67 * This effectively "drops" all unconsumed events, leaving
68 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
69 */
70 client->tail = (client->head - 2) & (client->bufsize - 1);
71
72 client->buffer[client->tail].time = event->time;
73 client->buffer[client->tail].type = EV_SYN;
74 client->buffer[client->tail].code = SYN_DROPPED;
75 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -070076
77 client->packet_head = client->tail;
78 }
79
80 if (event->type == EV_SYN && event->code == SYN_REPORT) {
81 client->packet_head = client->head;
82 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -070083 }
84
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040085 spin_unlock(&client->buffer_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040086}
87
88/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040089 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040090 */
91static void evdev_event(struct input_handle *handle,
92 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040095 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040096 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040098 do_gettimeofday(&event.time);
99 event.type = type;
100 event.code = code;
101 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400103 rcu_read_lock();
104
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400105 client = rcu_dereference(evdev->grab);
106 if (client)
107 evdev_pass_event(client, &event);
108 else
109 list_for_each_entry_rcu(client, &evdev->client_list, node)
110 evdev_pass_event(client, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400112 rcu_read_unlock();
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 wake_up_interruptible(&evdev->wait);
115}
116
117static int evdev_fasync(int fd, struct file *file, int on)
118{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400119 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400120
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700121 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400124static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400126 struct evdev_client *client = file->private_data;
127 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400128 int retval;
129
130 retval = mutex_lock_interruptible(&evdev->mutex);
131 if (retval)
132 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400133
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400134 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400135 retval = -ENODEV;
136 else
137 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400138
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400139 mutex_unlock(&evdev->mutex);
140 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400143static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400145 struct evdev *evdev = container_of(dev, struct evdev, dev);
146
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400147 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 kfree(evdev);
149}
150
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400151/*
152 * Grabs an event device (along with underlying input device).
153 * This function is called with evdev->mutex taken.
154 */
155static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
156{
157 int error;
158
159 if (evdev->grab)
160 return -EBUSY;
161
162 error = input_grab_device(&evdev->handle);
163 if (error)
164 return error;
165
166 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400167 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400168
169 return 0;
170}
171
172static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
173{
174 if (evdev->grab != client)
175 return -EINVAL;
176
177 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400178 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400179 input_release_device(&evdev->handle);
180
181 return 0;
182}
183
184static void evdev_attach_client(struct evdev *evdev,
185 struct evdev_client *client)
186{
187 spin_lock(&evdev->client_lock);
188 list_add_tail_rcu(&client->node, &evdev->client_list);
189 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400190 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400191}
192
193static void evdev_detach_client(struct evdev *evdev,
194 struct evdev_client *client)
195{
196 spin_lock(&evdev->client_lock);
197 list_del_rcu(&client->node);
198 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400199 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400200}
201
202static int evdev_open_device(struct evdev *evdev)
203{
204 int retval;
205
206 retval = mutex_lock_interruptible(&evdev->mutex);
207 if (retval)
208 return retval;
209
210 if (!evdev->exist)
211 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400212 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400213 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400214 if (retval)
215 evdev->open--;
216 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400217
218 mutex_unlock(&evdev->mutex);
219 return retval;
220}
221
222static void evdev_close_device(struct evdev *evdev)
223{
224 mutex_lock(&evdev->mutex);
225
226 if (evdev->exist && !--evdev->open)
227 input_close_device(&evdev->handle);
228
229 mutex_unlock(&evdev->mutex);
230}
231
232/*
233 * Wake up users waiting for IO so they can disconnect from
234 * dead device.
235 */
236static void evdev_hangup(struct evdev *evdev)
237{
238 struct evdev_client *client;
239
240 spin_lock(&evdev->client_lock);
241 list_for_each_entry(client, &evdev->client_list, node)
242 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
243 spin_unlock(&evdev->client_lock);
244
245 wake_up_interruptible(&evdev->wait);
246}
247
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400248static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400250 struct evdev_client *client = file->private_data;
251 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400253 mutex_lock(&evdev->mutex);
254 if (evdev->grab == client)
255 evdev_ungrab(evdev, client);
256 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400258 evdev_detach_client(evdev, client);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400259 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400261 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400262 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return 0;
265}
266
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700267static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
268{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700269 unsigned int n_events =
270 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
271 EVDEV_MIN_BUFFER_SIZE);
272
273 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700274}
275
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400276static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400278 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400279 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700281 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400282 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400284 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -ENODEV;
286
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400287 error = mutex_lock_interruptible(&evdev_table_mutex);
288 if (error)
289 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400290 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400291 if (evdev)
292 get_device(&evdev->dev);
293 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400294
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400295 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400296 return -ENODEV;
297
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700298 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
299
300 client = kzalloc(sizeof(struct evdev_client) +
301 bufsize * sizeof(struct input_event),
302 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400303 if (!client) {
304 error = -ENOMEM;
305 goto err_put_evdev;
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700308 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400309 spin_lock_init(&client->buffer_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400310 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400311 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400313 error = evdev_open_device(evdev);
314 if (error)
315 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400317 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800318 nonseekable_open(inode, file);
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400321
322 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400323 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400324 kfree(client);
325 err_put_evdev:
326 put_device(&evdev->dev);
327 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400330static ssize_t evdev_write(struct file *file, const char __user *buffer,
331 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400333 struct evdev_client *client = file->private_data;
334 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct input_event event;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400336 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Peter Korsgaard439581e2011-02-25 09:30:46 -0800338 if (count < input_event_size())
339 return -EINVAL;
340
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400341 retval = mutex_lock_interruptible(&evdev->mutex);
342 if (retval)
343 return retval;
344
345 if (!evdev->exist) {
346 retval = -ENODEV;
347 goto out;
348 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500349
Peter Korsgaard439581e2011-02-25 09:30:46 -0800350 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400351 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400352 retval = -EFAULT;
353 goto out;
354 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800355 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400356
357 input_inject_event(&evdev->handle,
358 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800359 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500360
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400361 out:
362 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500363 return retval;
364}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500365
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400366static int evdev_fetch_next_event(struct evdev_client *client,
367 struct input_event *event)
368{
369 int have_event;
370
371 spin_lock_irq(&client->buffer_lock);
372
373 have_event = client->head != client->tail;
374 if (have_event) {
375 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700376 client->tail &= client->bufsize - 1;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400377 }
378
379 spin_unlock_irq(&client->buffer_lock);
380
381 return have_event;
382}
383
384static ssize_t evdev_read(struct file *file, char __user *buffer,
385 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400387 struct evdev_client *client = file->private_data;
388 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400389 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int retval;
391
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400392 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -EINVAL;
394
Jeff Browncdda9112011-04-26 22:16:11 -0700395 if (client->packet_head == client->tail && evdev->exist &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400396 (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -EAGAIN;
398
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400399 retval = wait_event_interruptible(evdev->wait,
Jeff Browncdda9112011-04-26 22:16:11 -0700400 client->packet_head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (retval)
402 return retval;
403
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400404 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -ENODEV;
406
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400407 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400408 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500409
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400410 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500411 return -EFAULT;
412
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400413 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
416 return retval;
417}
418
419/* No kernel lock - fine */
420static unsigned int evdev_poll(struct file *file, poll_table *wait)
421{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400422 struct evdev_client *client = file->private_data;
423 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700424 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400425
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400426 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700427
428 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700429 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700430 mask |= POLLIN | POLLRDNORM;
431
432 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500435#ifdef CONFIG_COMPAT
436
437#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700438#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500439
440#ifdef __BIG_ENDIAN
441static int bits_to_user(unsigned long *bits, unsigned int maxbit,
442 unsigned int maxlen, void __user *p, int compat)
443{
444 int len, i;
445
446 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700447 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400448 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500449 len = maxlen;
450
451 for (i = 0; i < len / sizeof(compat_long_t); i++)
452 if (copy_to_user((compat_long_t __user *) p + i,
453 (compat_long_t *) bits +
454 i + 1 - ((i % 2) << 1),
455 sizeof(compat_long_t)))
456 return -EFAULT;
457 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700458 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500459 if (len > maxlen)
460 len = maxlen;
461
462 if (copy_to_user(p, bits, len))
463 return -EFAULT;
464 }
465
466 return len;
467}
468#else
469static int bits_to_user(unsigned long *bits, unsigned int maxbit,
470 unsigned int maxlen, void __user *p, int compat)
471{
472 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700473 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
474 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500475
476 if (len > maxlen)
477 len = maxlen;
478
479 return copy_to_user(p, bits, len) ? -EFAULT : len;
480}
481#endif /* __BIG_ENDIAN */
482
483#else
484
485static int bits_to_user(unsigned long *bits, unsigned int maxbit,
486 unsigned int maxlen, void __user *p, int compat)
487{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700488 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500489
490 if (len > maxlen)
491 len = maxlen;
492
493 return copy_to_user(p, bits, len) ? -EFAULT : len;
494}
495
496#endif /* CONFIG_COMPAT */
497
498static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
499{
500 int len;
501
502 if (!str)
503 return -ENOENT;
504
505 len = strlen(str) + 1;
506 if (len > maxlen)
507 len = maxlen;
508
509 return copy_to_user(p, str, len) ? -EFAULT : len;
510}
511
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400512#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700513static int handle_eviocgbit(struct input_dev *dev,
514 unsigned int type, unsigned int size,
515 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400516{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400517 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400518 unsigned long *bits;
519 int len;
520
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700521 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400522
523 case 0: bits = dev->evbit; len = EV_MAX; break;
524 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
525 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
526 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
527 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
528 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
529 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
530 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
531 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
532 default: return -EINVAL;
533 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400534
535 /*
536 * Work around bugs in userspace programs that like to do
537 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
538 * should be in bytes, not in bits.
539 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700540 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400541 len = OLD_KEY_MAX;
542 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800543 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
544 "limiting output to %zu bytes. See "
545 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
546 OLD_KEY_MAX,
547 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400548 }
549
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700550 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400551}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400552#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400553
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800554static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
555{
556 struct input_keymap_entry ke = {
557 .len = sizeof(unsigned int),
558 .flags = 0,
559 };
560 int __user *ip = (int __user *)p;
561 int error;
562
563 /* legacy case */
564 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
565 return -EFAULT;
566
567 error = input_get_keycode(dev, &ke);
568 if (error)
569 return error;
570
571 if (put_user(ke.keycode, ip + 1))
572 return -EFAULT;
573
574 return 0;
575}
576
577static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700578{
579 struct input_keymap_entry ke;
580 int error;
581
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800582 if (copy_from_user(&ke, p, sizeof(ke)))
583 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700584
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800585 error = input_get_keycode(dev, &ke);
586 if (error)
587 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700588
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800589 if (copy_to_user(p, &ke, sizeof(ke)))
590 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700591
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700592 return 0;
593}
594
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800595static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
596{
597 struct input_keymap_entry ke = {
598 .len = sizeof(unsigned int),
599 .flags = 0,
600 };
601 int __user *ip = (int __user *)p;
602
603 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
604 return -EFAULT;
605
606 if (get_user(ke.keycode, ip + 1))
607 return -EFAULT;
608
609 return input_set_keycode(dev, &ke);
610}
611
612static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700613{
614 struct input_keymap_entry ke;
615
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800616 if (copy_from_user(&ke, p, sizeof(ke)))
617 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700618
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800619 if (ke.len > sizeof(ke.scancode))
620 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700621
622 return input_set_keycode(dev, &ke);
623}
624
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400625static long evdev_do_ioctl(struct file *file, unsigned int cmd,
626 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400628 struct evdev_client *client = file->private_data;
629 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct input_dev *dev = evdev->handle.dev;
631 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400632 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500633 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800634 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700635 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400636 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700638 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 switch (cmd) {
640
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400641 case EVIOCGVERSION:
642 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400644 case EVIOCGID:
645 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
646 return -EFAULT;
647 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400648
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400649 case EVIOCGREP:
650 if (!test_bit(EV_REP, dev->evbit))
651 return -ENOSYS;
652 if (put_user(dev->rep[REP_DELAY], ip))
653 return -EFAULT;
654 if (put_user(dev->rep[REP_PERIOD], ip + 1))
655 return -EFAULT;
656 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400657
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400658 case EVIOCSREP:
659 if (!test_bit(EV_REP, dev->evbit))
660 return -ENOSYS;
661 if (get_user(u, ip))
662 return -EFAULT;
663 if (get_user(v, ip + 1))
664 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400665
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400666 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
667 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500668
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400671 case EVIOCRMFF:
672 return input_ff_erase(dev, (int)(unsigned long) p, file);
673
674 case EVIOCGEFFECTS:
675 i = test_bit(EV_FF, dev->evbit) ?
676 dev->ff->max_effects : 0;
677 if (put_user(i, ip))
678 return -EFAULT;
679 return 0;
680
681 case EVIOCGRAB:
682 if (p)
683 return evdev_grab(evdev, client);
684 else
685 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800686
687 case EVIOCGKEYCODE:
688 return evdev_handle_get_keycode(dev, p);
689
690 case EVIOCSKEYCODE:
691 return evdev_handle_set_keycode(dev, p);
692
693 case EVIOCGKEYCODE_V2:
694 return evdev_handle_get_keycode_v2(dev, p);
695
696 case EVIOCSKEYCODE_V2:
697 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700698 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400699
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700700 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400701
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700702 /* Now check variable-length commands */
703#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700704 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400705
Henrik Rydberg85b77202010-12-18 20:51:13 +0100706 case EVIOCGPROP(0):
707 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
708 size, p, compat_mode);
709
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700710 case EVIOCGKEY(0):
711 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400712
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700713 case EVIOCGLED(0):
714 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400715
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700716 case EVIOCGSND(0):
717 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400718
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700719 case EVIOCGSW(0):
720 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400721
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700722 case EVIOCGNAME(0):
723 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400724
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700725 case EVIOCGPHYS(0):
726 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400727
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700728 case EVIOCGUNIQ(0):
729 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400730
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700731 case EVIOC_MASK_SIZE(EVIOCSFF):
732 if (input_ff_effect_from_user(p, size, &effect))
733 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400734
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700735 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400736
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700737 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
738 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400739
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700740 return error;
741 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400742
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700743 /* Multi-number variable-length handlers */
744 if (_IOC_TYPE(cmd) != 'E')
745 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700747 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700749 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
750 return handle_eviocgbit(dev,
751 _IOC_NR(cmd) & EV_MAX, size,
752 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700754 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400755
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700756 if (!dev->absinfo)
757 return -EINVAL;
758
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700759 t = _IOC_NR(cmd) & ABS_MAX;
760 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400761
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700762 if (copy_to_user(p, &abs, min_t(size_t,
763 size, sizeof(struct input_absinfo))))
764 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400765
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700766 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700769
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700770 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700771
772 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
773
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700774 if (!dev->absinfo)
775 return -EINVAL;
776
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700777 t = _IOC_NR(cmd) & ABS_MAX;
778
779 if (copy_from_user(&abs, p, min_t(size_t,
780 size, sizeof(struct input_absinfo))))
781 return -EFAULT;
782
783 if (size < sizeof(struct input_absinfo))
784 abs.resolution = 0;
785
786 /* We can't change number of reserved MT slots */
787 if (t == ABS_MT_SLOT)
788 return -EINVAL;
789
790 /*
791 * Take event lock to ensure that we are not
792 * changing device parameters in the middle
793 * of event.
794 */
795 spin_lock_irq(&dev->event_lock);
796 dev->absinfo[t] = abs;
797 spin_unlock_irq(&dev->event_lock);
798
799 return 0;
800 }
801 }
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -EINVAL;
804}
805
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400806static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
807 void __user *p, int compat_mode)
808{
809 struct evdev_client *client = file->private_data;
810 struct evdev *evdev = client->evdev;
811 int retval;
812
813 retval = mutex_lock_interruptible(&evdev->mutex);
814 if (retval)
815 return retval;
816
817 if (!evdev->exist) {
818 retval = -ENODEV;
819 goto out;
820 }
821
822 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
823
824 out:
825 mutex_unlock(&evdev->mutex);
826 return retval;
827}
828
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500829static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
830{
831 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
832}
833
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500834#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400835static long evdev_ioctl_compat(struct file *file,
836 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500837{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500838 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500839}
840#endif
841
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400842static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400843 .owner = THIS_MODULE,
844 .read = evdev_read,
845 .write = evdev_write,
846 .poll = evdev_poll,
847 .open = evdev_open,
848 .release = evdev_release,
849 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500850#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400851 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500852#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400853 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200854 .flush = evdev_flush,
855 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856};
857
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400858static int evdev_install_chrdev(struct evdev *evdev)
859{
860 /*
861 * No need to do any locking here as calls to connect and
862 * disconnect are serialized by the input core
863 */
864 evdev_table[evdev->minor] = evdev;
865 return 0;
866}
867
868static void evdev_remove_chrdev(struct evdev *evdev)
869{
870 /*
871 * Lock evdev table to prevent race with evdev_open()
872 */
873 mutex_lock(&evdev_table_mutex);
874 evdev_table[evdev->minor] = NULL;
875 mutex_unlock(&evdev_table_mutex);
876}
877
878/*
879 * Mark device non-existent. This disables writes, ioctls and
880 * prevents new users from opening the device. Already posted
881 * blocking reads will stay, however new ones will fail.
882 */
883static void evdev_mark_dead(struct evdev *evdev)
884{
885 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700886 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400887 mutex_unlock(&evdev->mutex);
888}
889
890static void evdev_cleanup(struct evdev *evdev)
891{
892 struct input_handle *handle = &evdev->handle;
893
894 evdev_mark_dead(evdev);
895 evdev_hangup(evdev);
896 evdev_remove_chrdev(evdev);
897
898 /* evdev is marked dead so no one else accesses evdev->open */
899 if (evdev->open) {
900 input_flush_device(handle, NULL);
901 input_close_device(handle);
902 }
903}
904
905/*
906 * Create new evdev device. Note that input core serializes calls
907 * to connect and disconnect so we don't need to lock evdev_table here.
908 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400909static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
910 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct evdev *evdev;
913 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400914 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400916 for (minor = 0; minor < EVDEV_MINORS; minor++)
917 if (!evdev_table[minor])
918 break;
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -0800921 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400922 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400925 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
926 if (!evdev)
927 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400929 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400930 spin_lock_init(&evdev->client_lock);
931 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 init_waitqueue_head(&evdev->wait);
933
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700934 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700935 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400937
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400938 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700939 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 evdev->handle.handler = handler;
941 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400942
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400943 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400944 evdev->dev.class = &input_class;
945 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400946 evdev->dev.release = evdev_free;
947 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400949 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400950 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400951 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400953 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400954 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400955 goto err_unregister_handle;
956
957 error = device_add(&evdev->dev);
958 if (error)
959 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400960
961 return 0;
962
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400963 err_cleanup_evdev:
964 evdev_cleanup(evdev);
965 err_unregister_handle:
966 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400967 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400968 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400969 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972static void evdev_disconnect(struct input_handle *handle)
973{
974 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400976 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400977 evdev_cleanup(evdev);
978 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400979 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400982static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 { .driver_info = 1 }, /* Matches all devices */
984 { }, /* Terminating zero entry */
985};
986
987MODULE_DEVICE_TABLE(input, evdev_ids);
988
989static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400990 .event = evdev_event,
991 .connect = evdev_connect,
992 .disconnect = evdev_disconnect,
993 .fops = &evdev_fops,
994 .minor = EVDEV_MINOR_BASE,
995 .name = "evdev",
996 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997};
998
999static int __init evdev_init(void)
1000{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001001 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004static void __exit evdev_exit(void)
1005{
1006 input_unregister_handler(&evdev_handler);
1007}
1008
1009module_init(evdev_init);
1010module_exit(evdev_exit);
1011
1012MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1013MODULE_DESCRIPTION("Input driver event char devices");
1014MODULE_LICENSE("GPL");