blob: 1f571d73d9f2f2281992a4455893f0c8a08ee077 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Event char devices, giving access to raw input device events.
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
10
Joe Perchesda0c4902010-11-29 23:33:07 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#define EVDEV_MINOR_BASE 64
14#define EVDEV_MINORS 32
Henrik Rydberg63a64042010-06-10 12:05:24 -070015#define EVDEV_MIN_BUFFER_SIZE 64U
16#define EVDEV_BUF_PACKETS 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/poll.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/input.h>
24#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/device.h>
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -070026#include <linux/wakelock.h>
Philip Langdale2d56f3a2008-10-16 22:31:42 -040027#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29struct evdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 int open;
31 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 struct input_handle handle;
33 wait_queue_head_t wait;
Arnd Bergmann2be85272010-03-04 15:50:28 +010034 struct evdev_client __rcu *grab;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040035 struct list_head client_list;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040036 spinlock_t client_lock; /* protects client_list */
37 struct mutex mutex;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040038 struct device dev;
Dmitry Torokhov20da92d2010-07-15 23:27:36 -070039 bool exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040042struct evdev_client {
Jeff Brown9fb0f142011-04-12 23:29:38 -070043 unsigned int head;
44 unsigned int tail;
Jeff Browncdda9112011-04-26 22:16:11 -070045 unsigned int packet_head; /* [future] position of the first element of next packet */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040046 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -070047 struct wake_lock wake_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct fasync_struct *fasync;
49 struct evdev *evdev;
50 struct list_head node;
Jeff Brown9fb0f142011-04-12 23:29:38 -070051 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070052 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040056static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040058static void evdev_pass_event(struct evdev_client *client,
59 struct input_event *event)
60{
Jeff Brown9fb0f142011-04-12 23:29:38 -070061 /* Interrupts are disabled, just acquire the lock. */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040062 spin_lock(&client->buffer_lock);
Jeff Brown9fb0f142011-04-12 23:29:38 -070063
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -070064 wake_lock_timeout(&client->wake_lock, 5 * HZ);
Jeff Brown9fb0f142011-04-12 23:29:38 -070065 client->buffer[client->head++] = *event;
66 client->head &= client->bufsize - 1;
67
68 if (unlikely(client->head == client->tail)) {
69 /*
70 * This effectively "drops" all unconsumed events, leaving
71 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
72 */
73 client->tail = (client->head - 2) & (client->bufsize - 1);
74
75 client->buffer[client->tail].time = event->time;
76 client->buffer[client->tail].type = EV_SYN;
77 client->buffer[client->tail].code = SYN_DROPPED;
78 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -070079
80 client->packet_head = client->tail;
81 }
82
83 if (event->type == EV_SYN && event->code == SYN_REPORT) {
84 client->packet_head = client->head;
85 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -070086 }
87
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040088 spin_unlock(&client->buffer_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040089}
90
91/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040092 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040093 */
94static void evdev_event(struct input_handle *handle,
95 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040098 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040099 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400101 do_gettimeofday(&event.time);
102 event.type = type;
103 event.code = code;
104 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400106 rcu_read_lock();
107
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400108 client = rcu_dereference(evdev->grab);
109 if (client)
110 evdev_pass_event(client, &event);
111 else
112 list_for_each_entry_rcu(client, &evdev->client_list, node)
113 evdev_pass_event(client, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400115 rcu_read_unlock();
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 wake_up_interruptible(&evdev->wait);
118}
119
120static int evdev_fasync(int fd, struct file *file, int on)
121{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400122 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400123
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700124 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400127static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400129 struct evdev_client *client = file->private_data;
130 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400131 int retval;
132
133 retval = mutex_lock_interruptible(&evdev->mutex);
134 if (retval)
135 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400136
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400137 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400138 retval = -ENODEV;
139 else
140 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400141
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400142 mutex_unlock(&evdev->mutex);
143 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400146static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400148 struct evdev *evdev = container_of(dev, struct evdev, dev);
149
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400150 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 kfree(evdev);
152}
153
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400154/*
155 * Grabs an event device (along with underlying input device).
156 * This function is called with evdev->mutex taken.
157 */
158static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
159{
160 int error;
161
162 if (evdev->grab)
163 return -EBUSY;
164
165 error = input_grab_device(&evdev->handle);
166 if (error)
167 return error;
168
169 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400170
171 return 0;
172}
173
174static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
175{
176 if (evdev->grab != client)
177 return -EINVAL;
178
179 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400180 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400181 input_release_device(&evdev->handle);
182
183 return 0;
184}
185
186static void evdev_attach_client(struct evdev *evdev,
187 struct evdev_client *client)
188{
189 spin_lock(&evdev->client_lock);
190 list_add_tail_rcu(&client->node, &evdev->client_list);
191 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400192}
193
194static void evdev_detach_client(struct evdev *evdev,
195 struct evdev_client *client)
196{
197 spin_lock(&evdev->client_lock);
198 list_del_rcu(&client->node);
199 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400200 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400201}
202
203static int evdev_open_device(struct evdev *evdev)
204{
205 int retval;
206
207 retval = mutex_lock_interruptible(&evdev->mutex);
208 if (retval)
209 return retval;
210
211 if (!evdev->exist)
212 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400213 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400214 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400215 if (retval)
216 evdev->open--;
217 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400218
219 mutex_unlock(&evdev->mutex);
220 return retval;
221}
222
223static void evdev_close_device(struct evdev *evdev)
224{
225 mutex_lock(&evdev->mutex);
226
227 if (evdev->exist && !--evdev->open)
228 input_close_device(&evdev->handle);
229
230 mutex_unlock(&evdev->mutex);
231}
232
233/*
234 * Wake up users waiting for IO so they can disconnect from
235 * dead device.
236 */
237static void evdev_hangup(struct evdev *evdev)
238{
239 struct evdev_client *client;
240
241 spin_lock(&evdev->client_lock);
242 list_for_each_entry(client, &evdev->client_list, node)
243 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
244 spin_unlock(&evdev->client_lock);
245
246 wake_up_interruptible(&evdev->wait);
247}
248
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400249static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400251 struct evdev_client *client = file->private_data;
252 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400254 mutex_lock(&evdev->mutex);
255 if (evdev->grab == client)
256 evdev_ungrab(evdev, client);
257 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400259 evdev_detach_client(evdev, client);
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700260 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400261 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400263 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400264 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267}
268
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700269static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
270{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700271 unsigned int n_events =
272 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
273 EVDEV_MIN_BUFFER_SIZE);
274
275 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700276}
277
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400278static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400280 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400281 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700283 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400284 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400286 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -ENODEV;
288
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400289 error = mutex_lock_interruptible(&evdev_table_mutex);
290 if (error)
291 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400292 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400293 if (evdev)
294 get_device(&evdev->dev);
295 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400296
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400297 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400298 return -ENODEV;
299
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700300 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
301
302 client = kzalloc(sizeof(struct evdev_client) +
303 bufsize * sizeof(struct input_event),
304 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400305 if (!client) {
306 error = -ENOMEM;
307 goto err_put_evdev;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700310 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400311 spin_lock_init(&client->buffer_lock);
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700312 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, "evdev");
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400313 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400314 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400316 error = evdev_open_device(evdev);
317 if (error)
318 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400320 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800321 nonseekable_open(inode, file);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400324
325 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400326 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400327 kfree(client);
328 err_put_evdev:
329 put_device(&evdev->dev);
330 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400333static ssize_t evdev_write(struct file *file, const char __user *buffer,
334 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400336 struct evdev_client *client = file->private_data;
337 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct input_event event;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400339 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Peter Korsgaard439581e2011-02-25 09:30:46 -0800341 if (count < input_event_size())
342 return -EINVAL;
343
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400344 retval = mutex_lock_interruptible(&evdev->mutex);
345 if (retval)
346 return retval;
347
348 if (!evdev->exist) {
349 retval = -ENODEV;
350 goto out;
351 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500352
Peter Korsgaard439581e2011-02-25 09:30:46 -0800353 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400354 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400355 retval = -EFAULT;
356 goto out;
357 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800358 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400359
360 input_inject_event(&evdev->handle,
361 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800362 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500363
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400364 out:
365 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500366 return retval;
367}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500368
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400369static int evdev_fetch_next_event(struct evdev_client *client,
370 struct input_event *event)
371{
372 int have_event;
373
374 spin_lock_irq(&client->buffer_lock);
375
376 have_event = client->head != client->tail;
377 if (have_event) {
378 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700379 client->tail &= client->bufsize - 1;
Arve Hjønnevågca5d75f2008-10-17 15:20:55 -0700380 if (client->head == client->tail)
381 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400382 }
383
384 spin_unlock_irq(&client->buffer_lock);
385
386 return have_event;
387}
388
389static ssize_t evdev_read(struct file *file, char __user *buffer,
390 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400392 struct evdev_client *client = file->private_data;
393 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400394 struct input_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int retval;
396
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400397 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -EINVAL;
399
Jeff Browncdda9112011-04-26 22:16:11 -0700400 if (client->packet_head == client->tail && evdev->exist &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400401 (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -EAGAIN;
403
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400404 retval = wait_event_interruptible(evdev->wait,
Jeff Browncdda9112011-04-26 22:16:11 -0700405 client->packet_head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (retval)
407 return retval;
408
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400409 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -ENODEV;
411
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400412 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400413 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500414
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400415 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500416 return -EFAULT;
417
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400418 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
421 return retval;
422}
423
424/* No kernel lock - fine */
425static unsigned int evdev_poll(struct file *file, poll_table *wait)
426{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400427 struct evdev_client *client = file->private_data;
428 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700429 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400430
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400431 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700432
433 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700434 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700435 mask |= POLLIN | POLLRDNORM;
436
437 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500440#ifdef CONFIG_COMPAT
441
442#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700443#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500444
445#ifdef __BIG_ENDIAN
446static int bits_to_user(unsigned long *bits, unsigned int maxbit,
447 unsigned int maxlen, void __user *p, int compat)
448{
449 int len, i;
450
451 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700452 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400453 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500454 len = maxlen;
455
456 for (i = 0; i < len / sizeof(compat_long_t); i++)
457 if (copy_to_user((compat_long_t __user *) p + i,
458 (compat_long_t *) bits +
459 i + 1 - ((i % 2) << 1),
460 sizeof(compat_long_t)))
461 return -EFAULT;
462 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700463 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500464 if (len > maxlen)
465 len = maxlen;
466
467 if (copy_to_user(p, bits, len))
468 return -EFAULT;
469 }
470
471 return len;
472}
473#else
474static int bits_to_user(unsigned long *bits, unsigned int maxbit,
475 unsigned int maxlen, void __user *p, int compat)
476{
477 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700478 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
479 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500480
481 if (len > maxlen)
482 len = maxlen;
483
484 return copy_to_user(p, bits, len) ? -EFAULT : len;
485}
486#endif /* __BIG_ENDIAN */
487
488#else
489
490static int bits_to_user(unsigned long *bits, unsigned int maxbit,
491 unsigned int maxlen, void __user *p, int compat)
492{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700493 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500494
495 if (len > maxlen)
496 len = maxlen;
497
498 return copy_to_user(p, bits, len) ? -EFAULT : len;
499}
500
501#endif /* CONFIG_COMPAT */
502
503static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
504{
505 int len;
506
507 if (!str)
508 return -ENOENT;
509
510 len = strlen(str) + 1;
511 if (len > maxlen)
512 len = maxlen;
513
514 return copy_to_user(p, str, len) ? -EFAULT : len;
515}
516
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400517#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700518static int handle_eviocgbit(struct input_dev *dev,
519 unsigned int type, unsigned int size,
520 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400521{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400522 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400523 unsigned long *bits;
524 int len;
525
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700526 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400527
528 case 0: bits = dev->evbit; len = EV_MAX; break;
529 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
530 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
531 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
532 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
533 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
534 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
535 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
536 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
537 default: return -EINVAL;
538 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400539
540 /*
541 * Work around bugs in userspace programs that like to do
542 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
543 * should be in bytes, not in bits.
544 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700545 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400546 len = OLD_KEY_MAX;
547 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800548 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
549 "limiting output to %zu bytes. See "
550 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
551 OLD_KEY_MAX,
552 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400553 }
554
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700555 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400556}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400557#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400558
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800559static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
560{
561 struct input_keymap_entry ke = {
562 .len = sizeof(unsigned int),
563 .flags = 0,
564 };
565 int __user *ip = (int __user *)p;
566 int error;
567
568 /* legacy case */
569 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
570 return -EFAULT;
571
572 error = input_get_keycode(dev, &ke);
573 if (error)
574 return error;
575
576 if (put_user(ke.keycode, ip + 1))
577 return -EFAULT;
578
579 return 0;
580}
581
582static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700583{
584 struct input_keymap_entry ke;
585 int error;
586
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800587 if (copy_from_user(&ke, p, sizeof(ke)))
588 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700589
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800590 error = input_get_keycode(dev, &ke);
591 if (error)
592 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700593
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800594 if (copy_to_user(p, &ke, sizeof(ke)))
595 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700596
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700597 return 0;
598}
599
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800600static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
601{
602 struct input_keymap_entry ke = {
603 .len = sizeof(unsigned int),
604 .flags = 0,
605 };
606 int __user *ip = (int __user *)p;
607
608 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
609 return -EFAULT;
610
611 if (get_user(ke.keycode, ip + 1))
612 return -EFAULT;
613
614 return input_set_keycode(dev, &ke);
615}
616
617static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700618{
619 struct input_keymap_entry ke;
620
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800621 if (copy_from_user(&ke, p, sizeof(ke)))
622 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700623
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800624 if (ke.len > sizeof(ke.scancode))
625 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700626
627 return input_set_keycode(dev, &ke);
628}
629
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400630static long evdev_do_ioctl(struct file *file, unsigned int cmd,
631 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400633 struct evdev_client *client = file->private_data;
634 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct input_dev *dev = evdev->handle.dev;
636 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400637 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500638 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800639 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700640 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400641 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700643 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 switch (cmd) {
645
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400646 case EVIOCGVERSION:
647 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400649 case EVIOCGID:
650 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
651 return -EFAULT;
652 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400653
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400654 case EVIOCGREP:
655 if (!test_bit(EV_REP, dev->evbit))
656 return -ENOSYS;
657 if (put_user(dev->rep[REP_DELAY], ip))
658 return -EFAULT;
659 if (put_user(dev->rep[REP_PERIOD], ip + 1))
660 return -EFAULT;
661 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400662
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400663 case EVIOCSREP:
664 if (!test_bit(EV_REP, dev->evbit))
665 return -ENOSYS;
666 if (get_user(u, ip))
667 return -EFAULT;
668 if (get_user(v, ip + 1))
669 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400670
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400671 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
672 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500673
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400676 case EVIOCRMFF:
677 return input_ff_erase(dev, (int)(unsigned long) p, file);
678
679 case EVIOCGEFFECTS:
680 i = test_bit(EV_FF, dev->evbit) ?
681 dev->ff->max_effects : 0;
682 if (put_user(i, ip))
683 return -EFAULT;
684 return 0;
685
686 case EVIOCGRAB:
687 if (p)
688 return evdev_grab(evdev, client);
689 else
690 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800691
692 case EVIOCGKEYCODE:
693 return evdev_handle_get_keycode(dev, p);
694
695 case EVIOCSKEYCODE:
696 return evdev_handle_set_keycode(dev, p);
697
698 case EVIOCGKEYCODE_V2:
699 return evdev_handle_get_keycode_v2(dev, p);
700
701 case EVIOCSKEYCODE_V2:
702 return evdev_handle_set_keycode_v2(dev, p);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700703 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400704
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700705 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400706
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700707 /* Now check variable-length commands */
708#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700709 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400710
Henrik Rydberg85b77202010-12-18 20:51:13 +0100711 case EVIOCGPROP(0):
712 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
713 size, p, compat_mode);
714
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700715 case EVIOCGKEY(0):
716 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400717
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700718 case EVIOCGLED(0):
719 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400720
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700721 case EVIOCGSND(0):
722 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400723
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700724 case EVIOCGSW(0):
725 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400726
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700727 case EVIOCGNAME(0):
728 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400729
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700730 case EVIOCGPHYS(0):
731 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400732
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700733 case EVIOCGUNIQ(0):
734 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400735
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700736 case EVIOC_MASK_SIZE(EVIOCSFF):
737 if (input_ff_effect_from_user(p, size, &effect))
738 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400739
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700740 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400741
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700742 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
743 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400744
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700745 return error;
746 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400747
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700748 /* Multi-number variable-length handlers */
749 if (_IOC_TYPE(cmd) != 'E')
750 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700752 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700754 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
755 return handle_eviocgbit(dev,
756 _IOC_NR(cmd) & EV_MAX, size,
757 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700759 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400760
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700761 if (!dev->absinfo)
762 return -EINVAL;
763
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700764 t = _IOC_NR(cmd) & ABS_MAX;
765 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400766
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700767 if (copy_to_user(p, &abs, min_t(size_t,
768 size, sizeof(struct input_absinfo))))
769 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400770
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700771 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700774
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700775 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700776
777 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
778
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700779 if (!dev->absinfo)
780 return -EINVAL;
781
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700782 t = _IOC_NR(cmd) & ABS_MAX;
783
784 if (copy_from_user(&abs, p, min_t(size_t,
785 size, sizeof(struct input_absinfo))))
786 return -EFAULT;
787
788 if (size < sizeof(struct input_absinfo))
789 abs.resolution = 0;
790
791 /* We can't change number of reserved MT slots */
792 if (t == ABS_MT_SLOT)
793 return -EINVAL;
794
795 /*
796 * Take event lock to ensure that we are not
797 * changing device parameters in the middle
798 * of event.
799 */
800 spin_lock_irq(&dev->event_lock);
801 dev->absinfo[t] = abs;
802 spin_unlock_irq(&dev->event_lock);
803
804 return 0;
805 }
806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -EINVAL;
809}
810
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400811static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
812 void __user *p, int compat_mode)
813{
814 struct evdev_client *client = file->private_data;
815 struct evdev *evdev = client->evdev;
816 int retval;
817
818 retval = mutex_lock_interruptible(&evdev->mutex);
819 if (retval)
820 return retval;
821
822 if (!evdev->exist) {
823 retval = -ENODEV;
824 goto out;
825 }
826
827 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
828
829 out:
830 mutex_unlock(&evdev->mutex);
831 return retval;
832}
833
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500834static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
835{
836 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
837}
838
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500839#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400840static long evdev_ioctl_compat(struct file *file,
841 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500842{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500843 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500844}
845#endif
846
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400847static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400848 .owner = THIS_MODULE,
849 .read = evdev_read,
850 .write = evdev_write,
851 .poll = evdev_poll,
852 .open = evdev_open,
853 .release = evdev_release,
854 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500855#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400856 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500857#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400858 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200859 .flush = evdev_flush,
860 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861};
862
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400863static int evdev_install_chrdev(struct evdev *evdev)
864{
865 /*
866 * No need to do any locking here as calls to connect and
867 * disconnect are serialized by the input core
868 */
869 evdev_table[evdev->minor] = evdev;
870 return 0;
871}
872
873static void evdev_remove_chrdev(struct evdev *evdev)
874{
875 /*
876 * Lock evdev table to prevent race with evdev_open()
877 */
878 mutex_lock(&evdev_table_mutex);
879 evdev_table[evdev->minor] = NULL;
880 mutex_unlock(&evdev_table_mutex);
881}
882
883/*
884 * Mark device non-existent. This disables writes, ioctls and
885 * prevents new users from opening the device. Already posted
886 * blocking reads will stay, however new ones will fail.
887 */
888static void evdev_mark_dead(struct evdev *evdev)
889{
890 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700891 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400892 mutex_unlock(&evdev->mutex);
893}
894
895static void evdev_cleanup(struct evdev *evdev)
896{
897 struct input_handle *handle = &evdev->handle;
898
899 evdev_mark_dead(evdev);
900 evdev_hangup(evdev);
901 evdev_remove_chrdev(evdev);
902
903 /* evdev is marked dead so no one else accesses evdev->open */
904 if (evdev->open) {
905 input_flush_device(handle, NULL);
906 input_close_device(handle);
907 }
908}
909
910/*
911 * Create new evdev device. Note that input core serializes calls
912 * to connect and disconnect so we don't need to lock evdev_table here.
913 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400914static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
915 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct evdev *evdev;
918 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400919 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400921 for (minor = 0; minor < EVDEV_MINORS; minor++)
922 if (!evdev_table[minor])
923 break;
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -0800926 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400927 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400930 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
931 if (!evdev)
932 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400934 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400935 spin_lock_init(&evdev->client_lock);
936 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 init_waitqueue_head(&evdev->wait);
938
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700939 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -0700940 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400942
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400943 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -0700944 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 evdev->handle.handler = handler;
946 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400947
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400948 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400949 evdev->dev.class = &input_class;
950 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400951 evdev->dev.release = evdev_free;
952 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400954 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400955 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400956 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400958 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400959 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400960 goto err_unregister_handle;
961
962 error = device_add(&evdev->dev);
963 if (error)
964 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400965
966 return 0;
967
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400968 err_cleanup_evdev:
969 evdev_cleanup(evdev);
970 err_unregister_handle:
971 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400972 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400973 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400974 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977static void evdev_disconnect(struct input_handle *handle)
978{
979 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400981 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400982 evdev_cleanup(evdev);
983 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400984 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400987static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 { .driver_info = 1 }, /* Matches all devices */
989 { }, /* Terminating zero entry */
990};
991
992MODULE_DEVICE_TABLE(input, evdev_ids);
993
994static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400995 .event = evdev_event,
996 .connect = evdev_connect,
997 .disconnect = evdev_disconnect,
998 .fops = &evdev_fops,
999 .minor = EVDEV_MINOR_BASE,
1000 .name = "evdev",
1001 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002};
1003
1004static int __init evdev_init(void)
1005{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001006 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009static void __exit evdev_exit(void)
1010{
1011 input_unregister_handler(&evdev_handler);
1012}
1013
1014module_init(evdev_init);
1015module_exit(evdev_exit);
1016
1017MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1018MODULE_DESCRIPTION("Input driver event char devices");
1019MODULE_LICENSE("GPL");