blob: d037ae68a44d83ebe514ee3e20490ce95ab6cd02 [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>
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +010023#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/device.h>
Arve Hjønnevåg783820b2008-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
Aniroop Mathurf397b412014-12-17 15:33:06 -080029enum evdev_clock_type {
30 EV_CLK_REAL = 0,
31 EV_CLK_MONO,
32 EV_CLK_BOOT,
33 EV_CLK_MAX
34};
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036struct evdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 int open;
38 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct input_handle handle;
40 wait_queue_head_t wait;
Arnd Bergmann2be85272010-03-04 15:50:28 +010041 struct evdev_client __rcu *grab;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040042 struct list_head client_list;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040043 spinlock_t client_lock; /* protects client_list */
44 struct mutex mutex;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040045 struct device dev;
Dmitry Torokhov20da92d2010-07-15 23:27:36 -070046 bool exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040049struct evdev_client {
Jeff Brown9fb0f142011-04-12 23:29:38 -070050 unsigned int head;
51 unsigned int tail;
Jeff Browncdda9112011-04-26 22:16:11 -070052 unsigned int packet_head; /* [future] position of the first element of next packet */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -040053 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
Arve Hjønnevåg783820b2008-10-17 15:20:55 -070054 struct wake_lock wake_lock;
55 bool use_wake_lock;
56 char name[28];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 struct fasync_struct *fasync;
58 struct evdev *evdev;
59 struct list_head node;
Aniroop Mathurf397b412014-12-17 15:33:06 -080060 int clk_type;
Jeff Brown9fb0f142011-04-12 23:29:38 -070061 unsigned int bufsize;
Henrik Rydbergb58f7082010-06-23 09:17:56 -070062 struct input_event buffer[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Dimitri Unrau9f4d6d12016-04-21 03:23:06 +020065const char * const wants_boottime_dev_names[] = {
66#ifdef CONFIG_INPUT_CAPELLA_CM3629
67 "proximity",
68 "lightsensor-level",
69#endif
70#ifdef CONFIG_SENSORS_R3GD20
71 "r3gd20_gyr",
72#endif
73#ifdef CONFIG_SENSORS_PANASONIC_GYRO
74 "ewtzmu2_gyroscope",
75#endif
76#ifdef CONFIG_SENSORS_BMA250_BOSCH
Dimitri Unrau9f4d6d12016-04-21 03:23:06 +020077 "sig_motion",
78#endif
Jose Adolfo Galdamez332797a2016-05-13 00:02:28 -060079#if defined(CONFIG_SENSORS_BMA250_BOSCH) || defined(CONFIG_SENSORS_BMA250)
80 "bma250",
81#endif
Dimitri Unrau9f4d6d12016-04-21 03:23:06 +020082#if defined(CONFIG_SENSORS_BMA250_BOSCH) || defined(CONFIG_SENSORS_ONLY_BMA250)
83 "CIRSensor",
84#endif
85#if defined(CONFIG_MPU_SENSORS_AK8963) || defined(CONFIG_SENSORS_AKM8963_NST) || defined(CONFIG_SENSORS_AKM8975)
86 "compass",
87#endif
88};
89
Aniroop Mathurf397b412014-12-17 15:33:06 -080090static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
91{
92 switch (clkid) {
93
94 case CLOCK_REALTIME:
95 client->clk_type = EV_CLK_REAL;
96 break;
97 case CLOCK_MONOTONIC:
98 client->clk_type = EV_CLK_MONO;
99 break;
100 case CLOCK_BOOTTIME:
101 client->clk_type = EV_CLK_BOOT;
102 break;
103 default:
104 return -EINVAL;
105 }
106
107 return 0;
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static struct evdev *evdev_table[EVDEV_MINORS];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400111static DEFINE_MUTEX(evdev_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400113static void evdev_pass_event(struct evdev_client *client,
John Stultza80b83b2012-02-03 00:19:07 -0800114 struct input_event *event,
Aniroop Mathurf397b412014-12-17 15:33:06 -0800115 ktime_t *ev_time)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400116{
Aniroop Mathurf397b412014-12-17 15:33:06 -0800117 event->time = ktime_to_timeval(ev_time[client->clk_type]);
John Stultza80b83b2012-02-03 00:19:07 -0800118
Jeff Brown9fb0f142011-04-12 23:29:38 -0700119 /* Interrupts are disabled, just acquire the lock. */
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400120 spin_lock(&client->buffer_lock);
Jeff Brown9fb0f142011-04-12 23:29:38 -0700121
122 client->buffer[client->head++] = *event;
123 client->head &= client->bufsize - 1;
124
125 if (unlikely(client->head == client->tail)) {
126 /*
127 * This effectively "drops" all unconsumed events, leaving
128 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
129 */
130 client->tail = (client->head - 2) & (client->bufsize - 1);
131
132 client->buffer[client->tail].time = event->time;
133 client->buffer[client->tail].type = EV_SYN;
134 client->buffer[client->tail].code = SYN_DROPPED;
135 client->buffer[client->tail].value = 0;
Jeff Browncdda9112011-04-26 22:16:11 -0700136
137 client->packet_head = client->tail;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700138 if (client->use_wake_lock)
139 wake_unlock(&client->wake_lock);
Jeff Browncdda9112011-04-26 22:16:11 -0700140 }
141
142 if (event->type == EV_SYN && event->code == SYN_REPORT) {
143 client->packet_head = client->head;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700144 if (client->use_wake_lock)
145 wake_lock(&client->wake_lock);
Jeff Browncdda9112011-04-26 22:16:11 -0700146 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Jeff Brown9fb0f142011-04-12 23:29:38 -0700147 }
148
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400149 spin_unlock(&client->buffer_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400150}
151
152/*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400153 * Pass incoming event to all connected clients.
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400154 */
155static void evdev_event(struct input_handle *handle,
156 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400159 struct evdev_client *client;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400160 struct input_event event;
Aniroop Mathurf397b412014-12-17 15:33:06 -0800161 struct timespec xtim;
162 struct timespec wtom;
163 struct timespec sleep;
164 ktime_t ev_time[EV_CLK_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Aniroop Mathurf397b412014-12-17 15:33:06 -0800166 get_xtime_and_monotonic_and_sleep_offset(&xtim, &wtom, &sleep);
167 ev_time[EV_CLK_MONO] = ktime_get();
168 ev_time[EV_CLK_REAL] = ktime_sub(ev_time[EV_CLK_MONO],
169 timespec_to_ktime(wtom));
170 ev_time[EV_CLK_BOOT] = ktime_add(ev_time[EV_CLK_MONO],
171 timespec_to_ktime(sleep));
John Stultza80b83b2012-02-03 00:19:07 -0800172
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400173 event.type = type;
174 event.code = code;
175 event.value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400177 rcu_read_lock();
178
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400179 client = rcu_dereference(evdev->grab);
John Stultza80b83b2012-02-03 00:19:07 -0800180
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400181 if (client)
Aniroop Mathurf397b412014-12-17 15:33:06 -0800182 evdev_pass_event(client, &event, ev_time);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400183 else
184 list_for_each_entry_rcu(client, &evdev->client_list, node)
Aniroop Mathurf397b412014-12-17 15:33:06 -0800185 evdev_pass_event(client, &event, ev_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400187 rcu_read_unlock();
188
Dmitry Torokhovda40b0b2011-06-18 02:50:11 -0700189 if (type == EV_SYN && code == SYN_REPORT)
190 wake_up_interruptible(&evdev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193static int evdev_fasync(int fd, struct file *file, int on)
194{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400195 struct evdev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400196
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700197 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400200static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400202 struct evdev_client *client = file->private_data;
203 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400204 int retval;
205
206 retval = mutex_lock_interruptible(&evdev->mutex);
207 if (retval)
208 return retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400209
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400210 if (!evdev->exist)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400211 retval = -ENODEV;
212 else
213 retval = input_flush_device(&evdev->handle, file);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400214
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400215 mutex_unlock(&evdev->mutex);
216 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400219static void evdev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400221 struct evdev *evdev = container_of(dev, struct evdev, dev);
222
Dmitry Torokhova7097ff2008-04-01 00:22:53 -0400223 input_put_device(evdev->handle.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 kfree(evdev);
225}
226
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400227/*
228 * Grabs an event device (along with underlying input device).
229 * This function is called with evdev->mutex taken.
230 */
231static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
232{
233 int error;
234
235 if (evdev->grab)
236 return -EBUSY;
237
238 error = input_grab_device(&evdev->handle);
239 if (error)
240 return error;
241
242 rcu_assign_pointer(evdev->grab, client);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400243
244 return 0;
245}
246
247static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
248{
249 if (evdev->grab != client)
250 return -EINVAL;
251
252 rcu_assign_pointer(evdev->grab, NULL);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400253 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400254 input_release_device(&evdev->handle);
255
256 return 0;
257}
258
259static void evdev_attach_client(struct evdev *evdev,
260 struct evdev_client *client)
261{
262 spin_lock(&evdev->client_lock);
263 list_add_tail_rcu(&client->node, &evdev->client_list);
264 spin_unlock(&evdev->client_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400265}
266
267static void evdev_detach_client(struct evdev *evdev,
268 struct evdev_client *client)
269{
270 spin_lock(&evdev->client_lock);
271 list_del_rcu(&client->node);
272 spin_unlock(&evdev->client_lock);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400273 synchronize_rcu();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400274}
275
276static int evdev_open_device(struct evdev *evdev)
277{
278 int retval;
279
280 retval = mutex_lock_interruptible(&evdev->mutex);
281 if (retval)
282 return retval;
283
284 if (!evdev->exist)
285 retval = -ENODEV;
Oliver Neukum06445012007-10-12 14:18:40 -0400286 else if (!evdev->open++) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400287 retval = input_open_device(&evdev->handle);
Oliver Neukum06445012007-10-12 14:18:40 -0400288 if (retval)
289 evdev->open--;
290 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400291
292 mutex_unlock(&evdev->mutex);
293 return retval;
294}
295
296static void evdev_close_device(struct evdev *evdev)
297{
298 mutex_lock(&evdev->mutex);
299
300 if (evdev->exist && !--evdev->open)
301 input_close_device(&evdev->handle);
302
303 mutex_unlock(&evdev->mutex);
304}
305
306/*
307 * Wake up users waiting for IO so they can disconnect from
308 * dead device.
309 */
310static void evdev_hangup(struct evdev *evdev)
311{
312 struct evdev_client *client;
313
314 spin_lock(&evdev->client_lock);
315 list_for_each_entry(client, &evdev->client_list, node)
316 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
317 spin_unlock(&evdev->client_lock);
318
319 wake_up_interruptible(&evdev->wait);
320}
321
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400322static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400324 struct evdev_client *client = file->private_data;
325 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400327 mutex_lock(&evdev->mutex);
328 if (evdev->grab == client)
329 evdev_ungrab(evdev, client);
330 mutex_unlock(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400332 evdev_detach_client(evdev, client);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700333 if (client->use_wake_lock)
334 wake_lock_destroy(&client->wake_lock);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400335 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400337 evdev_close_device(evdev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400338 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341}
342
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700343static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
344{
Henrik Rydberg63a64042010-06-10 12:05:24 -0700345 unsigned int n_events =
346 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
347 EVDEV_MIN_BUFFER_SIZE);
348
349 return roundup_pow_of_two(n_events);
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700350}
351
Dimitri Unrau9f4d6d12016-04-21 03:23:06 +0200352static bool evdev_wants_boottime(const char *name)
353{
354 int i;
355
356 for (i = 0; i < ARRAY_SIZE(wants_boottime_dev_names); i++) {
357 if (!strcmp(name, wants_boottime_dev_names[i]))
358 return 1;
359 }
360
361 return 0;
362}
363
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400364static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400366 struct evdev *evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400367 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int i = iminor(inode) - EVDEV_MINOR_BASE;
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700369 unsigned int bufsize;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400370 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400372 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -ENODEV;
374
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400375 error = mutex_lock_interruptible(&evdev_table_mutex);
376 if (error)
377 return error;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400378 evdev = evdev_table[i];
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400379 if (evdev)
380 get_device(&evdev->dev);
381 mutex_unlock(&evdev_table_mutex);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400382
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400383 if (!evdev)
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400384 return -ENODEV;
385
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700386 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
387
388 client = kzalloc(sizeof(struct evdev_client) +
389 bufsize * sizeof(struct input_event),
390 GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400391 if (!client) {
392 error = -ENOMEM;
393 goto err_put_evdev;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Dimitri Unrau9f4d6d12016-04-21 03:23:06 +0200396 if (evdev_wants_boottime(evdev->handle.dev->name))
397 client->clk_type = EV_CLK_BOOT;
398 else
399 client->clk_type = EV_CLK_MONO;
400
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700401 client->bufsize = bufsize;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400402 spin_lock_init(&client->buffer_lock);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700403 snprintf(client->name, sizeof(client->name), "%s-%d",
404 dev_name(&evdev->dev), task_tgid_vnr(current));
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400405 client->evdev = evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400406 evdev_attach_client(evdev, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400408 error = evdev_open_device(evdev);
409 if (error)
410 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400412 file->private_data = client;
Dmitry Torokhov3d7bbd42010-02-04 00:30:42 -0800413 nonseekable_open(inode, file);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400416
417 err_free_client:
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400418 evdev_detach_client(evdev, client);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400419 kfree(client);
420 err_put_evdev:
421 put_device(&evdev->dev);
422 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400425static ssize_t evdev_write(struct file *file, const char __user *buffer,
426 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400428 struct evdev_client *client = file->private_data;
429 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct input_event event;
Heiko Stübner02dfc492012-02-08 23:08:48 -0800431 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Peter Korsgaard439581e2011-02-25 09:30:46 -0800433 if (count < input_event_size())
434 return -EINVAL;
435
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400436 retval = mutex_lock_interruptible(&evdev->mutex);
437 if (retval)
438 return retval;
439
440 if (!evdev->exist) {
441 retval = -ENODEV;
442 goto out;
443 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500444
Peter Korsgaard439581e2011-02-25 09:30:46 -0800445 do {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400446 if (input_event_from_user(buffer + retval, &event)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400447 retval = -EFAULT;
448 goto out;
449 }
Peter Korsgaard439581e2011-02-25 09:30:46 -0800450 retval += input_event_size();
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400451
452 input_inject_event(&evdev->handle,
453 event.type, event.code, event.value);
Peter Korsgaard439581e2011-02-25 09:30:46 -0800454 } while (retval + input_event_size() <= count);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500455
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400456 out:
457 mutex_unlock(&evdev->mutex);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500458 return retval;
459}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500460
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400461static int evdev_fetch_next_event(struct evdev_client *client,
462 struct input_event *event)
463{
464 int have_event;
465
466 spin_lock_irq(&client->buffer_lock);
467
Dima Zavin566cf5b2011-12-30 15:16:44 -0800468 have_event = client->packet_head != client->tail;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400469 if (have_event) {
470 *event = client->buffer[client->tail++];
Henrik Rydbergb58f7082010-06-23 09:17:56 -0700471 client->tail &= client->bufsize - 1;
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700472 if (client->use_wake_lock &&
473 client->packet_head == client->tail)
474 wake_unlock(&client->wake_lock);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400475 }
476
477 spin_unlock_irq(&client->buffer_lock);
478
479 return have_event;
480}
481
482static ssize_t evdev_read(struct file *file, char __user *buffer,
483 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400485 struct evdev_client *client = file->private_data;
486 struct evdev *evdev = client->evdev;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400487 struct input_event event;
Heiko Stübner42f57872012-02-01 09:12:23 -0800488 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400490 if (count < input_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EINVAL;
492
Dima Zavin509f87c2011-12-30 15:16:44 -0800493 if (!(file->f_flags & O_NONBLOCK)) {
494 retval = wait_event_interruptible(evdev->wait,
495 client->packet_head != client->tail ||
496 !evdev->exist);
497 if (retval)
498 return retval;
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400501 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -ENODEV;
503
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400504 while (retval + input_event_size() <= count &&
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400505 evdev_fetch_next_event(client, &event)) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500506
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400507 if (input_event_to_user(buffer + retval, &event))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500508 return -EFAULT;
509
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400510 retval += input_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
Dima Zavine90f8692011-12-30 15:16:44 -0800513 if (retval == 0 && (file->f_flags & O_NONBLOCK))
514 return -EAGAIN;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return retval;
517}
518
519/* No kernel lock - fine */
520static unsigned int evdev_poll(struct file *file, poll_table *wait)
521{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400522 struct evdev_client *client = file->private_data;
523 struct evdev *evdev = client->evdev;
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700524 unsigned int mask;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400525
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400526 poll_wait(file, &evdev->wait, wait);
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700527
528 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
Jeff Browncdda9112011-04-26 22:16:11 -0700529 if (client->packet_head != client->tail)
Dmitry Torokhovc18fb132010-07-15 23:28:42 -0700530 mask |= POLLIN | POLLRDNORM;
531
532 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500535#ifdef CONFIG_COMPAT
536
537#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700538#define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500539
540#ifdef __BIG_ENDIAN
541static int bits_to_user(unsigned long *bits, unsigned int maxbit,
542 unsigned int maxlen, void __user *p, int compat)
543{
544 int len, i;
545
546 if (compat) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700547 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400548 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500549 len = maxlen;
550
551 for (i = 0; i < len / sizeof(compat_long_t); i++)
552 if (copy_to_user((compat_long_t __user *) p + i,
553 (compat_long_t *) bits +
554 i + 1 - ((i % 2) << 1),
555 sizeof(compat_long_t)))
556 return -EFAULT;
557 } else {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700558 len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500559 if (len > maxlen)
560 len = maxlen;
561
562 if (copy_to_user(p, bits, len))
563 return -EFAULT;
564 }
565
566 return len;
567}
568#else
569static int bits_to_user(unsigned long *bits, unsigned int maxbit,
570 unsigned int maxlen, void __user *p, int compat)
571{
572 int len = compat ?
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700573 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
574 BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500575
576 if (len > maxlen)
577 len = maxlen;
578
579 return copy_to_user(p, bits, len) ? -EFAULT : len;
580}
581#endif /* __BIG_ENDIAN */
582
583#else
584
585static int bits_to_user(unsigned long *bits, unsigned int maxbit,
586 unsigned int maxlen, void __user *p, int compat)
587{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700588 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500589
590 if (len > maxlen)
591 len = maxlen;
592
593 return copy_to_user(p, bits, len) ? -EFAULT : len;
594}
595
596#endif /* CONFIG_COMPAT */
597
598static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
599{
600 int len;
601
602 if (!str)
603 return -ENOENT;
604
605 len = strlen(str) + 1;
606 if (len > maxlen)
607 len = maxlen;
608
609 return copy_to_user(p, str, len) ? -EFAULT : len;
610}
611
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400612#define OLD_KEY_MAX 0x1ff
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700613static int handle_eviocgbit(struct input_dev *dev,
614 unsigned int type, unsigned int size,
615 void __user *p, int compat_mode)
Linus Torvalds5402a732008-08-05 11:42:42 -0400616{
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400617 static unsigned long keymax_warn_time;
Linus Torvalds5402a732008-08-05 11:42:42 -0400618 unsigned long *bits;
619 int len;
620
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700621 switch (type) {
Linus Torvalds5402a732008-08-05 11:42:42 -0400622
623 case 0: bits = dev->evbit; len = EV_MAX; break;
624 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
625 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
626 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
627 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
628 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
629 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
630 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
631 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
632 default: return -EINVAL;
633 }
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400634
635 /*
636 * Work around bugs in userspace programs that like to do
637 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
638 * should be in bytes, not in bits.
639 */
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700640 if (type == EV_KEY && size == OLD_KEY_MAX) {
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400641 len = OLD_KEY_MAX;
642 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
Joe Perchesda0c4902010-11-29 23:33:07 -0800643 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
644 "limiting output to %zu bytes. See "
645 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
646 OLD_KEY_MAX,
647 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400648 }
649
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700650 return bits_to_user(bits, len, size, p, compat_mode);
Linus Torvalds5402a732008-08-05 11:42:42 -0400651}
Dmitry Torokhovf2afa772008-08-08 11:46:53 -0400652#undef OLD_KEY_MAX
Linus Torvalds5402a732008-08-05 11:42:42 -0400653
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800654static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
655{
656 struct input_keymap_entry ke = {
657 .len = sizeof(unsigned int),
658 .flags = 0,
659 };
660 int __user *ip = (int __user *)p;
661 int error;
662
663 /* legacy case */
664 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
665 return -EFAULT;
666
667 error = input_get_keycode(dev, &ke);
668 if (error)
669 return error;
670
671 if (put_user(ke.keycode, ip + 1))
672 return -EFAULT;
673
674 return 0;
675}
676
677static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700678{
679 struct input_keymap_entry ke;
680 int error;
681
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800682 if (copy_from_user(&ke, p, sizeof(ke)))
683 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700684
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800685 error = input_get_keycode(dev, &ke);
686 if (error)
687 return error;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700688
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800689 if (copy_to_user(p, &ke, sizeof(ke)))
690 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700691
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700692 return 0;
693}
694
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800695static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
696{
697 struct input_keymap_entry ke = {
698 .len = sizeof(unsigned int),
699 .flags = 0,
700 };
701 int __user *ip = (int __user *)p;
702
703 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
704 return -EFAULT;
705
706 if (get_user(ke.keycode, ip + 1))
707 return -EFAULT;
708
709 return input_set_keycode(dev, &ke);
710}
711
712static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700713{
714 struct input_keymap_entry ke;
715
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800716 if (copy_from_user(&ke, p, sizeof(ke)))
717 return -EFAULT;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700718
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800719 if (ke.len > sizeof(ke.scancode))
720 return -EINVAL;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700721
722 return input_set_keycode(dev, &ke);
723}
724
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100725static int evdev_handle_mt_request(struct input_dev *dev,
726 unsigned int size,
727 int __user *ip)
728{
729 const struct input_mt_slot *mt = dev->mt;
730 unsigned int code;
731 int max_slots;
732 int i;
733
734 if (get_user(code, &ip[0]))
735 return -EFAULT;
736 if (!input_is_mt_value(code))
737 return -EINVAL;
738
739 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
740 for (i = 0; i < dev->mtsize && i < max_slots; i++)
741 if (put_user(input_mt_get_value(&mt[i], code), &ip[1 + i]))
742 return -EFAULT;
743
744 return 0;
745}
746
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700747static int evdev_enable_suspend_block(struct evdev *evdev,
748 struct evdev_client *client)
749{
750 if (client->use_wake_lock)
751 return 0;
752
753 spin_lock_irq(&client->buffer_lock);
754 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
755 client->use_wake_lock = true;
756 if (client->packet_head != client->tail)
757 wake_lock(&client->wake_lock);
758 spin_unlock_irq(&client->buffer_lock);
759 return 0;
760}
761
762static int evdev_disable_suspend_block(struct evdev *evdev,
763 struct evdev_client *client)
764{
765 if (!client->use_wake_lock)
766 return 0;
767
768 spin_lock_irq(&client->buffer_lock);
769 client->use_wake_lock = false;
770 wake_lock_destroy(&client->wake_lock);
771 spin_unlock_irq(&client->buffer_lock);
772
773 return 0;
774}
775
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400776static long evdev_do_ioctl(struct file *file, unsigned int cmd,
777 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400779 struct evdev_client *client = file->private_data;
780 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct input_dev *dev = evdev->handle.dev;
782 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400783 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500784 int __user *ip = (int __user *)p;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800785 unsigned int i, t, u, v;
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700786 unsigned int size;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400787 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700789 /* First we check for fixed-length commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 switch (cmd) {
791
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400792 case EVIOCGVERSION:
793 return put_user(EV_VERSION, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400795 case EVIOCGID:
796 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
797 return -EFAULT;
798 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400799
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400800 case EVIOCGREP:
801 if (!test_bit(EV_REP, dev->evbit))
802 return -ENOSYS;
803 if (put_user(dev->rep[REP_DELAY], ip))
804 return -EFAULT;
805 if (put_user(dev->rep[REP_PERIOD], ip + 1))
806 return -EFAULT;
807 return 0;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400808
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400809 case EVIOCSREP:
810 if (!test_bit(EV_REP, dev->evbit))
811 return -ENOSYS;
812 if (get_user(u, ip))
813 return -EFAULT;
814 if (get_user(v, ip + 1))
815 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400816
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400817 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
818 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500819
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400822 case EVIOCRMFF:
823 return input_ff_erase(dev, (int)(unsigned long) p, file);
824
825 case EVIOCGEFFECTS:
826 i = test_bit(EV_FF, dev->evbit) ?
827 dev->ff->max_effects : 0;
828 if (put_user(i, ip))
829 return -EFAULT;
830 return 0;
831
832 case EVIOCGRAB:
833 if (p)
834 return evdev_grab(evdev, client);
835 else
836 return evdev_ungrab(evdev, client);
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800837
John Stultza80b83b2012-02-03 00:19:07 -0800838 case EVIOCSCLOCKID:
839 if (copy_from_user(&i, p, sizeof(unsigned int)))
840 return -EFAULT;
Aniroop Mathurf397b412014-12-17 15:33:06 -0800841
842 return evdev_set_clk_type(client, i);
John Stultza80b83b2012-02-03 00:19:07 -0800843
Dmitry Torokhovab4e0192010-12-14 23:53:21 -0800844 case EVIOCGKEYCODE:
845 return evdev_handle_get_keycode(dev, p);
846
847 case EVIOCSKEYCODE:
848 return evdev_handle_set_keycode(dev, p);
849
850 case EVIOCGKEYCODE_V2:
851 return evdev_handle_get_keycode_v2(dev, p);
852
853 case EVIOCSKEYCODE_V2:
854 return evdev_handle_set_keycode_v2(dev, p);
Arve Hjønnevåg783820b2008-10-17 15:20:55 -0700855
856 case EVIOCGSUSPENDBLOCK:
857 return put_user(client->use_wake_lock, ip);
858
859 case EVIOCSSUSPENDBLOCK:
860 if (p)
861 return evdev_enable_suspend_block(evdev, client);
862 else
863 return evdev_disable_suspend_block(evdev, client);
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700864 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400865
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700866 size = _IOC_SIZE(cmd);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400867
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700868 /* Now check variable-length commands */
869#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700870 switch (EVIOC_MASK_SIZE(cmd)) {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400871
Henrik Rydberg85b77202010-12-18 20:51:13 +0100872 case EVIOCGPROP(0):
873 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
874 size, p, compat_mode);
875
Henrik Rydberg1cf0c6e2012-02-06 08:49:25 +0100876 case EVIOCGMTSLOTS(0):
877 return evdev_handle_mt_request(dev, size, ip);
878
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700879 case EVIOCGKEY(0):
880 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400881
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700882 case EVIOCGLED(0):
883 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400884
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700885 case EVIOCGSND(0):
886 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400887
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700888 case EVIOCGSW(0):
889 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400890
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700891 case EVIOCGNAME(0):
892 return str_to_user(dev->name, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400893
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700894 case EVIOCGPHYS(0):
895 return str_to_user(dev->phys, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400896
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700897 case EVIOCGUNIQ(0):
898 return str_to_user(dev->uniq, size, p);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400899
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700900 case EVIOC_MASK_SIZE(EVIOCSFF):
901 if (input_ff_effect_from_user(p, size, &effect))
902 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400903
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700904 error = input_ff_upload(dev, &effect, file);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400905
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700906 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
907 return -EFAULT;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400908
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700909 return error;
910 }
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400911
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700912 /* Multi-number variable-length handlers */
913 if (_IOC_TYPE(cmd) != 'E')
914 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700916 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700918 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
919 return handle_eviocgbit(dev,
920 _IOC_NR(cmd) & EV_MAX, size,
921 p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700923 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400924
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700925 if (!dev->absinfo)
926 return -EINVAL;
927
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700928 t = _IOC_NR(cmd) & ABS_MAX;
929 abs = dev->absinfo[t];
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400930
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700931 if (copy_to_user(p, &abs, min_t(size_t,
932 size, sizeof(struct input_absinfo))))
933 return -EFAULT;
Adam Dawidowskif2278f32008-06-02 01:08:10 -0400934
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700935 return 0;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700938
Daniel Mackf9ce6eb2010-10-18 08:43:50 -0700939 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700940
941 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
942
Daniel Mack0a74a1d2010-10-18 08:43:30 -0700943 if (!dev->absinfo)
944 return -EINVAL;
945
Dmitry Torokhov448cd162010-08-02 20:29:10 -0700946 t = _IOC_NR(cmd) & ABS_MAX;
947
948 if (copy_from_user(&abs, p, min_t(size_t,
949 size, sizeof(struct input_absinfo))))
950 return -EFAULT;
951
952 if (size < sizeof(struct input_absinfo))
953 abs.resolution = 0;
954
955 /* We can't change number of reserved MT slots */
956 if (t == ABS_MT_SLOT)
957 return -EINVAL;
958
959 /*
960 * Take event lock to ensure that we are not
961 * changing device parameters in the middle
962 * of event.
963 */
964 spin_lock_irq(&dev->event_lock);
965 dev->absinfo[t] = abs;
966 spin_unlock_irq(&dev->event_lock);
967
968 return 0;
969 }
970 }
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return -EINVAL;
973}
974
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -0400975static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
976 void __user *p, int compat_mode)
977{
978 struct evdev_client *client = file->private_data;
979 struct evdev *evdev = client->evdev;
980 int retval;
981
982 retval = mutex_lock_interruptible(&evdev->mutex);
983 if (retval)
984 return retval;
985
986 if (!evdev->exist) {
987 retval = -ENODEV;
988 goto out;
989 }
990
991 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
992
993 out:
994 mutex_unlock(&evdev->mutex);
995 return retval;
996}
997
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500998static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
999{
1000 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
1001}
1002
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001003#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001004static long evdev_ioctl_compat(struct file *file,
1005 unsigned int cmd, unsigned long arg)
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001006{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -05001007 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001008}
1009#endif
1010
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001011static const struct file_operations evdev_fops = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001012 .owner = THIS_MODULE,
1013 .read = evdev_read,
1014 .write = evdev_write,
1015 .poll = evdev_poll,
1016 .open = evdev_open,
1017 .release = evdev_release,
1018 .unlocked_ioctl = evdev_ioctl,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001019#ifdef CONFIG_COMPAT
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001020 .compat_ioctl = evdev_ioctl_compat,
Juergen Kreileder52658bb2005-05-29 02:26:43 -05001021#endif
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001022 .fasync = evdev_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001023 .flush = evdev_flush,
1024 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025};
1026
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001027static int evdev_install_chrdev(struct evdev *evdev)
1028{
1029 /*
1030 * No need to do any locking here as calls to connect and
1031 * disconnect are serialized by the input core
1032 */
1033 evdev_table[evdev->minor] = evdev;
1034 return 0;
1035}
1036
1037static void evdev_remove_chrdev(struct evdev *evdev)
1038{
1039 /*
1040 * Lock evdev table to prevent race with evdev_open()
1041 */
1042 mutex_lock(&evdev_table_mutex);
1043 evdev_table[evdev->minor] = NULL;
1044 mutex_unlock(&evdev_table_mutex);
1045}
1046
1047/*
1048 * Mark device non-existent. This disables writes, ioctls and
1049 * prevents new users from opening the device. Already posted
1050 * blocking reads will stay, however new ones will fail.
1051 */
1052static void evdev_mark_dead(struct evdev *evdev)
1053{
1054 mutex_lock(&evdev->mutex);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001055 evdev->exist = false;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001056 mutex_unlock(&evdev->mutex);
1057}
1058
1059static void evdev_cleanup(struct evdev *evdev)
1060{
1061 struct input_handle *handle = &evdev->handle;
1062
1063 evdev_mark_dead(evdev);
1064 evdev_hangup(evdev);
1065 evdev_remove_chrdev(evdev);
1066
1067 /* evdev is marked dead so no one else accesses evdev->open */
1068 if (evdev->open) {
1069 input_flush_device(handle, NULL);
1070 input_close_device(handle);
1071 }
1072}
1073
1074/*
1075 * Create new evdev device. Note that input core serializes calls
1076 * to connect and disconnect so we don't need to lock evdev_table here.
1077 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001078static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1079 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct evdev *evdev;
1082 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001083 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001085 for (minor = 0; minor < EVDEV_MINORS; minor++)
1086 if (!evdev_table[minor])
1087 break;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (minor == EVDEV_MINORS) {
Joe Perchesda0c4902010-11-29 23:33:07 -08001090 pr_err("no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001091 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001094 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
1095 if (!evdev)
1096 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -04001098 INIT_LIST_HEAD(&evdev->client_list);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001099 spin_lock_init(&evdev->client_lock);
1100 mutex_init(&evdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 init_waitqueue_head(&evdev->wait);
1102
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001103 dev_set_name(&evdev->dev, "event%d", minor);
Dmitry Torokhov20da92d2010-07-15 23:27:36 -07001104 evdev->exist = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 evdev->minor = minor;
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001106
Dmitry Torokhova7097ff2008-04-01 00:22:53 -04001107 evdev->handle.dev = input_get_device(dev);
Thadeu Lima de Souza Cascardo3d5cb602009-05-09 16:08:04 -07001108 evdev->handle.name = dev_name(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 evdev->handle.handler = handler;
1110 evdev->handle.private = evdev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001111
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001112 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001113 evdev->dev.class = &input_class;
1114 evdev->dev.parent = &dev->dev;
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001115 evdev->dev.release = evdev_free;
1116 device_initialize(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001118 error = input_register_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001119 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001120 goto err_free_evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001122 error = evdev_install_chrdev(evdev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001123 if (error)
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001124 goto err_unregister_handle;
1125
1126 error = device_add(&evdev->dev);
1127 if (error)
1128 goto err_cleanup_evdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001129
1130 return 0;
1131
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001132 err_cleanup_evdev:
1133 evdev_cleanup(evdev);
1134 err_unregister_handle:
1135 input_unregister_handle(&evdev->handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001136 err_free_evdev:
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001137 put_device(&evdev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001138 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141static void evdev_disconnect(struct input_handle *handle)
1142{
1143 struct evdev *evdev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001145 device_del(&evdev->dev);
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001146 evdev_cleanup(evdev);
1147 input_unregister_handle(handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001148 put_device(&evdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001151static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 { .driver_info = 1 }, /* Matches all devices */
1153 { }, /* Terminating zero entry */
1154};
1155
1156MODULE_DEVICE_TABLE(input, evdev_ids);
1157
1158static struct input_handler evdev_handler = {
Dmitry Torokhov6addb1d2007-08-30 00:22:18 -04001159 .event = evdev_event,
1160 .connect = evdev_connect,
1161 .disconnect = evdev_disconnect,
1162 .fops = &evdev_fops,
1163 .minor = EVDEV_MINOR_BASE,
1164 .name = "evdev",
1165 .id_table = evdev_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166};
1167
1168static int __init evdev_init(void)
1169{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001170 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173static void __exit evdev_exit(void)
1174{
1175 input_unregister_handler(&evdev_handler);
1176}
1177
1178module_init(evdev_init);
1179module_exit(evdev_exit);
1180
1181MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1182MODULE_DESCRIPTION("Input driver event char devices");
1183MODULE_LICENSE("GPL");