Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #define EVDEV_MINOR_BASE 64 |
| 14 | #define EVDEV_MINORS 32 |
Henrik Rydberg | 63a6404 | 2010-06-10 12:05:24 -0700 | [diff] [blame] | 15 | #define EVDEV_MIN_BUFFER_SIZE 64U |
| 16 | #define EVDEV_BUF_PACKETS 8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include <linux/poll.h> |
Alexey Dobriyan | a99bbaf | 2009-10-04 16:11:37 +0400 | [diff] [blame] | 19 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
Daniel Stone | 92eb77d | 2013-10-31 00:25:34 -0700 | [diff] [blame] | 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
Henrik Rydberg | 1cf0c6e | 2012-02-06 08:49:25 +0100 | [diff] [blame] | 25 | #include <linux/input/mt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/major.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/device.h> |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 28 | #include <linux/cdev.h> |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 29 | #include "input-compat.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 31 | enum evdev_clock_type { |
| 32 | EV_CLK_REAL = 0, |
| 33 | EV_CLK_MONO, |
| 34 | EV_CLK_BOOT, |
| 35 | EV_CLK_MAX |
| 36 | }; |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct evdev { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | int open; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct input_handle handle; |
| 41 | wait_queue_head_t wait; |
Arnd Bergmann | 2be8527 | 2010-03-04 15:50:28 +0100 | [diff] [blame] | 42 | struct evdev_client __rcu *grab; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 43 | struct list_head client_list; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 44 | spinlock_t client_lock; /* protects client_list */ |
| 45 | struct mutex mutex; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 46 | struct device dev; |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 47 | struct cdev cdev; |
Dmitry Torokhov | 20da92d | 2010-07-15 23:27:36 -0700 | [diff] [blame] | 48 | bool exist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 51 | struct evdev_client { |
Jeff Brown | 9fb0f14 | 2011-04-12 23:29:38 -0700 | [diff] [blame] | 52 | unsigned int head; |
| 53 | unsigned int tail; |
Jeff Brown | cdda911 | 2011-04-26 22:16:11 -0700 | [diff] [blame] | 54 | unsigned int packet_head; /* [future] position of the first element of next packet */ |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 55 | spinlock_t buffer_lock; /* protects access to buffer, head and tail */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | struct fasync_struct *fasync; |
| 57 | struct evdev *evdev; |
| 58 | struct list_head node; |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 59 | int clk_type; |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 60 | bool revoked; |
Jeff Brown | 9fb0f14 | 2011-04-12 23:29:38 -0700 | [diff] [blame] | 61 | unsigned int bufsize; |
Henrik Rydberg | b58f708 | 2010-06-23 09:17:56 -0700 | [diff] [blame] | 62 | struct input_event buffer[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 65 | /* flush queued events of type @type, caller must hold client->buffer_lock */ |
| 66 | static void __evdev_flush_queue(struct evdev_client *client, unsigned int type) |
| 67 | { |
| 68 | unsigned int i, head, num; |
| 69 | unsigned int mask = client->bufsize - 1; |
| 70 | bool is_report; |
| 71 | struct input_event *ev; |
| 72 | |
| 73 | BUG_ON(type == EV_SYN); |
| 74 | |
| 75 | head = client->tail; |
| 76 | client->packet_head = client->tail; |
| 77 | |
| 78 | /* init to 1 so a leading SYN_REPORT will not be dropped */ |
| 79 | num = 1; |
| 80 | |
| 81 | for (i = client->tail; i != client->head; i = (i + 1) & mask) { |
| 82 | ev = &client->buffer[i]; |
| 83 | is_report = ev->type == EV_SYN && ev->code == SYN_REPORT; |
| 84 | |
| 85 | if (ev->type == type) { |
| 86 | /* drop matched entry */ |
| 87 | continue; |
| 88 | } else if (is_report && !num) { |
| 89 | /* drop empty SYN_REPORT groups */ |
| 90 | continue; |
| 91 | } else if (head != i) { |
| 92 | /* move entry to fill the gap */ |
| 93 | client->buffer[head].time = ev->time; |
| 94 | client->buffer[head].type = ev->type; |
| 95 | client->buffer[head].code = ev->code; |
| 96 | client->buffer[head].value = ev->value; |
| 97 | } |
| 98 | |
| 99 | num++; |
| 100 | head = (head + 1) & mask; |
| 101 | |
| 102 | if (is_report) { |
| 103 | num = 0; |
| 104 | client->packet_head = head; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | client->head = head; |
| 109 | } |
| 110 | |
Anshul Garg | 0c3e994 | 2015-01-15 09:06:50 -0800 | [diff] [blame^] | 111 | /* queue SYN_DROPPED event and flush queue if flush parameter is true */ |
| 112 | static void evdev_queue_syn_dropped(struct evdev_client *client, bool flush) |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 113 | { |
| 114 | unsigned long flags; |
| 115 | struct input_event ev; |
| 116 | ktime_t time; |
| 117 | |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 118 | time = client->clk_type == EV_CLK_REAL ? |
| 119 | ktime_get_real() : |
| 120 | client->clk_type == EV_CLK_MONO ? |
| 121 | ktime_get() : |
| 122 | ktime_get_boottime(); |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 123 | |
| 124 | ev.time = ktime_to_timeval(time); |
| 125 | ev.type = EV_SYN; |
| 126 | ev.code = SYN_DROPPED; |
| 127 | ev.value = 0; |
| 128 | |
| 129 | spin_lock_irqsave(&client->buffer_lock, flags); |
| 130 | |
Anshul Garg | 0c3e994 | 2015-01-15 09:06:50 -0800 | [diff] [blame^] | 131 | if (flush) |
| 132 | client->packet_head = client->head = client->tail; |
| 133 | |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 134 | client->buffer[client->head++] = ev; |
| 135 | client->head &= client->bufsize - 1; |
| 136 | |
| 137 | if (unlikely(client->head == client->tail)) { |
| 138 | /* drop queue but keep our SYN_DROPPED event */ |
| 139 | client->tail = (client->head - 1) & (client->bufsize - 1); |
| 140 | client->packet_head = client->tail; |
| 141 | } |
| 142 | |
| 143 | spin_unlock_irqrestore(&client->buffer_lock, flags); |
| 144 | } |
| 145 | |
Anshul Garg | 0c3e994 | 2015-01-15 09:06:50 -0800 | [diff] [blame^] | 146 | static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid) |
| 147 | { |
| 148 | if (client->clk_type == clkid) |
| 149 | return 0; |
| 150 | |
| 151 | switch (clkid) { |
| 152 | |
| 153 | case CLOCK_REALTIME: |
| 154 | client->clk_type = EV_CLK_REAL; |
| 155 | break; |
| 156 | case CLOCK_MONOTONIC: |
| 157 | client->clk_type = EV_CLK_MONO; |
| 158 | break; |
| 159 | case CLOCK_BOOTTIME: |
| 160 | client->clk_type = EV_CLK_BOOT; |
| 161 | break; |
| 162 | default: |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | /* Flush pending events and queue SYN_DROPPED event.*/ |
| 167 | evdev_queue_syn_dropped(client, true); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 172 | static void __pass_event(struct evdev_client *client, |
| 173 | const struct input_event *event) |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 174 | { |
Jeff Brown | 9fb0f14 | 2011-04-12 23:29:38 -0700 | [diff] [blame] | 175 | client->buffer[client->head++] = *event; |
| 176 | client->head &= client->bufsize - 1; |
| 177 | |
| 178 | if (unlikely(client->head == client->tail)) { |
| 179 | /* |
| 180 | * This effectively "drops" all unconsumed events, leaving |
| 181 | * EV_SYN/SYN_DROPPED plus the newest event in the queue. |
| 182 | */ |
| 183 | client->tail = (client->head - 2) & (client->bufsize - 1); |
| 184 | |
| 185 | client->buffer[client->tail].time = event->time; |
| 186 | client->buffer[client->tail].type = EV_SYN; |
| 187 | client->buffer[client->tail].code = SYN_DROPPED; |
| 188 | client->buffer[client->tail].value = 0; |
Jeff Brown | cdda911 | 2011-04-26 22:16:11 -0700 | [diff] [blame] | 189 | |
| 190 | client->packet_head = client->tail; |
| 191 | } |
| 192 | |
| 193 | if (event->type == EV_SYN && event->code == SYN_REPORT) { |
| 194 | client->packet_head = client->head; |
| 195 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
Jeff Brown | 9fb0f14 | 2011-04-12 23:29:38 -0700 | [diff] [blame] | 196 | } |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static void evdev_pass_values(struct evdev_client *client, |
| 200 | const struct input_value *vals, unsigned int count, |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 201 | ktime_t *ev_time) |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 202 | { |
| 203 | struct evdev *evdev = client->evdev; |
| 204 | const struct input_value *v; |
| 205 | struct input_event event; |
| 206 | bool wakeup = false; |
| 207 | |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 208 | if (client->revoked) |
| 209 | return; |
| 210 | |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 211 | event.time = ktime_to_timeval(ev_time[client->clk_type]); |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 212 | |
| 213 | /* Interrupts are disabled, just acquire the lock. */ |
| 214 | spin_lock(&client->buffer_lock); |
| 215 | |
| 216 | for (v = vals; v != vals + count; v++) { |
| 217 | event.type = v->type; |
| 218 | event.code = v->code; |
| 219 | event.value = v->value; |
| 220 | __pass_event(client, &event); |
| 221 | if (v->type == EV_SYN && v->code == SYN_REPORT) |
| 222 | wakeup = true; |
| 223 | } |
Jeff Brown | 9fb0f14 | 2011-04-12 23:29:38 -0700 | [diff] [blame] | 224 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 225 | spin_unlock(&client->buffer_lock); |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 226 | |
| 227 | if (wakeup) |
| 228 | wake_up_interruptible(&evdev->wait); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Pass incoming events to all connected clients. |
| 233 | */ |
| 234 | static void evdev_events(struct input_handle *handle, |
| 235 | const struct input_value *vals, unsigned int count) |
| 236 | { |
| 237 | struct evdev *evdev = handle->private; |
| 238 | struct evdev_client *client; |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 239 | ktime_t ev_time[EV_CLK_MAX]; |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 240 | |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 241 | ev_time[EV_CLK_MONO] = ktime_get(); |
| 242 | ev_time[EV_CLK_REAL] = ktime_mono_to_real(ev_time[EV_CLK_MONO]); |
| 243 | ev_time[EV_CLK_BOOT] = ktime_mono_to_any(ev_time[EV_CLK_MONO], |
| 244 | TK_OFFS_BOOT); |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 245 | |
| 246 | rcu_read_lock(); |
| 247 | |
| 248 | client = rcu_dereference(evdev->grab); |
| 249 | |
| 250 | if (client) |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 251 | evdev_pass_values(client, vals, count, ev_time); |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 252 | else |
| 253 | list_for_each_entry_rcu(client, &evdev->client_list, node) |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 254 | evdev_pass_values(client, vals, count, ev_time); |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 255 | |
| 256 | rcu_read_unlock(); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
Dmitry Torokhov | 82ba56c | 2007-10-13 15:46:55 -0400 | [diff] [blame] | 260 | * Pass incoming event to all connected clients. |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 261 | */ |
| 262 | static void evdev_event(struct input_handle *handle, |
| 263 | unsigned int type, unsigned int code, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 265 | struct input_value vals[] = { { type, code, value } }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 267 | evdev_events(handle, vals, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static int evdev_fasync(int fd, struct file *file, int on) |
| 271 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 272 | struct evdev_client *client = file->private_data; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 273 | |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 274 | return fasync_helper(fd, file, on, &client->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 277 | static int evdev_flush(struct file *file, fl_owner_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 279 | struct evdev_client *client = file->private_data; |
| 280 | struct evdev *evdev = client->evdev; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 281 | int retval; |
| 282 | |
| 283 | retval = mutex_lock_interruptible(&evdev->mutex); |
| 284 | if (retval) |
| 285 | return retval; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 286 | |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 287 | if (!evdev->exist || client->revoked) |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 288 | retval = -ENODEV; |
| 289 | else |
| 290 | retval = input_flush_device(&evdev->handle, file); |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 291 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 292 | mutex_unlock(&evdev->mutex); |
| 293 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 296 | static void evdev_free(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 298 | struct evdev *evdev = container_of(dev, struct evdev, dev); |
| 299 | |
Dmitry Torokhov | a7097ff | 2008-04-01 00:22:53 -0400 | [diff] [blame] | 300 | input_put_device(evdev->handle.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | kfree(evdev); |
| 302 | } |
| 303 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 304 | /* |
| 305 | * Grabs an event device (along with underlying input device). |
| 306 | * This function is called with evdev->mutex taken. |
| 307 | */ |
| 308 | static int evdev_grab(struct evdev *evdev, struct evdev_client *client) |
| 309 | { |
| 310 | int error; |
| 311 | |
| 312 | if (evdev->grab) |
| 313 | return -EBUSY; |
| 314 | |
| 315 | error = input_grab_device(&evdev->handle); |
| 316 | if (error) |
| 317 | return error; |
| 318 | |
| 319 | rcu_assign_pointer(evdev->grab, client); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client) |
| 325 | { |
Dmitry Torokhov | dba4258 | 2012-05-02 00:13:36 -0700 | [diff] [blame] | 326 | struct evdev_client *grab = rcu_dereference_protected(evdev->grab, |
| 327 | lockdep_is_held(&evdev->mutex)); |
| 328 | |
| 329 | if (grab != client) |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 330 | return -EINVAL; |
| 331 | |
| 332 | rcu_assign_pointer(evdev->grab, NULL); |
Dmitry Torokhov | 82ba56c | 2007-10-13 15:46:55 -0400 | [diff] [blame] | 333 | synchronize_rcu(); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 334 | input_release_device(&evdev->handle); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static void evdev_attach_client(struct evdev *evdev, |
| 340 | struct evdev_client *client) |
| 341 | { |
| 342 | spin_lock(&evdev->client_lock); |
| 343 | list_add_tail_rcu(&client->node, &evdev->client_list); |
| 344 | spin_unlock(&evdev->client_lock); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static void evdev_detach_client(struct evdev *evdev, |
| 348 | struct evdev_client *client) |
| 349 | { |
| 350 | spin_lock(&evdev->client_lock); |
| 351 | list_del_rcu(&client->node); |
| 352 | spin_unlock(&evdev->client_lock); |
Dmitry Torokhov | 82ba56c | 2007-10-13 15:46:55 -0400 | [diff] [blame] | 353 | synchronize_rcu(); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static int evdev_open_device(struct evdev *evdev) |
| 357 | { |
| 358 | int retval; |
| 359 | |
| 360 | retval = mutex_lock_interruptible(&evdev->mutex); |
| 361 | if (retval) |
| 362 | return retval; |
| 363 | |
| 364 | if (!evdev->exist) |
| 365 | retval = -ENODEV; |
Oliver Neukum | 0644501 | 2007-10-12 14:18:40 -0400 | [diff] [blame] | 366 | else if (!evdev->open++) { |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 367 | retval = input_open_device(&evdev->handle); |
Oliver Neukum | 0644501 | 2007-10-12 14:18:40 -0400 | [diff] [blame] | 368 | if (retval) |
| 369 | evdev->open--; |
| 370 | } |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 371 | |
| 372 | mutex_unlock(&evdev->mutex); |
| 373 | return retval; |
| 374 | } |
| 375 | |
| 376 | static void evdev_close_device(struct evdev *evdev) |
| 377 | { |
| 378 | mutex_lock(&evdev->mutex); |
| 379 | |
| 380 | if (evdev->exist && !--evdev->open) |
| 381 | input_close_device(&evdev->handle); |
| 382 | |
| 383 | mutex_unlock(&evdev->mutex); |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Wake up users waiting for IO so they can disconnect from |
| 388 | * dead device. |
| 389 | */ |
| 390 | static void evdev_hangup(struct evdev *evdev) |
| 391 | { |
| 392 | struct evdev_client *client; |
| 393 | |
| 394 | spin_lock(&evdev->client_lock); |
| 395 | list_for_each_entry(client, &evdev->client_list, node) |
| 396 | kill_fasync(&client->fasync, SIGIO, POLL_HUP); |
| 397 | spin_unlock(&evdev->client_lock); |
| 398 | |
| 399 | wake_up_interruptible(&evdev->wait); |
| 400 | } |
| 401 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 402 | static int evdev_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 404 | struct evdev_client *client = file->private_data; |
| 405 | struct evdev *evdev = client->evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 407 | mutex_lock(&evdev->mutex); |
Dmitry Torokhov | dba4258 | 2012-05-02 00:13:36 -0700 | [diff] [blame] | 408 | evdev_ungrab(evdev, client); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 409 | mutex_unlock(&evdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 411 | evdev_detach_client(evdev, client); |
Daniel Stone | 92eb77d | 2013-10-31 00:25:34 -0700 | [diff] [blame] | 412 | |
| 413 | if (is_vmalloc_addr(client)) |
| 414 | vfree(client); |
| 415 | else |
| 416 | kfree(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 418 | evdev_close_device(evdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Henrik Rydberg | b58f708 | 2010-06-23 09:17:56 -0700 | [diff] [blame] | 423 | static unsigned int evdev_compute_buffer_size(struct input_dev *dev) |
| 424 | { |
Henrik Rydberg | 63a6404 | 2010-06-10 12:05:24 -0700 | [diff] [blame] | 425 | unsigned int n_events = |
| 426 | max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS, |
| 427 | EVDEV_MIN_BUFFER_SIZE); |
| 428 | |
| 429 | return roundup_pow_of_two(n_events); |
Henrik Rydberg | b58f708 | 2010-06-23 09:17:56 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 432 | static int evdev_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 434 | struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev); |
| 435 | unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev); |
Daniel Stone | 92eb77d | 2013-10-31 00:25:34 -0700 | [diff] [blame] | 436 | unsigned int size = sizeof(struct evdev_client) + |
| 437 | bufsize * sizeof(struct input_event); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 438 | struct evdev_client *client; |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 439 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Daniel Stone | 92eb77d | 2013-10-31 00:25:34 -0700 | [diff] [blame] | 441 | client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); |
| 442 | if (!client) |
| 443 | client = vzalloc(size); |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 444 | if (!client) |
| 445 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Henrik Rydberg | b58f708 | 2010-06-23 09:17:56 -0700 | [diff] [blame] | 447 | client->bufsize = bufsize; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 448 | spin_lock_init(&client->buffer_lock); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 449 | client->evdev = evdev; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 450 | evdev_attach_client(evdev, client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 452 | error = evdev_open_device(evdev); |
| 453 | if (error) |
| 454 | goto err_free_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 456 | file->private_data = client; |
Dmitry Torokhov | 3d7bbd4 | 2010-02-04 00:30:42 -0800 | [diff] [blame] | 457 | nonseekable_open(inode, file); |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | return 0; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 460 | |
| 461 | err_free_client: |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 462 | evdev_detach_client(evdev, client); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 463 | kfree(client); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 464 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 467 | static ssize_t evdev_write(struct file *file, const char __user *buffer, |
| 468 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 470 | struct evdev_client *client = file->private_data; |
| 471 | struct evdev *evdev = client->evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | struct input_event event; |
Heiko Stübner | 02dfc49 | 2012-02-08 23:08:48 -0800 | [diff] [blame] | 473 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Dmitry Torokhov | 2872a9b | 2012-05-02 00:13:37 -0700 | [diff] [blame] | 475 | if (count != 0 && count < input_event_size()) |
Peter Korsgaard | 439581e | 2011-02-25 09:30:46 -0800 | [diff] [blame] | 476 | return -EINVAL; |
| 477 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 478 | retval = mutex_lock_interruptible(&evdev->mutex); |
| 479 | if (retval) |
| 480 | return retval; |
| 481 | |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 482 | if (!evdev->exist || client->revoked) { |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 483 | retval = -ENODEV; |
| 484 | goto out; |
| 485 | } |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 486 | |
Dmitry Torokhov | 2872a9b | 2012-05-02 00:13:37 -0700 | [diff] [blame] | 487 | while (retval + input_event_size() <= count) { |
| 488 | |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 489 | if (input_event_from_user(buffer + retval, &event)) { |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 490 | retval = -EFAULT; |
| 491 | goto out; |
| 492 | } |
Peter Korsgaard | 439581e | 2011-02-25 09:30:46 -0800 | [diff] [blame] | 493 | retval += input_event_size(); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 494 | |
| 495 | input_inject_event(&evdev->handle, |
| 496 | event.type, event.code, event.value); |
Dmitry Torokhov | 2872a9b | 2012-05-02 00:13:37 -0700 | [diff] [blame] | 497 | } |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 498 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 499 | out: |
| 500 | mutex_unlock(&evdev->mutex); |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 501 | return retval; |
| 502 | } |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 503 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 504 | static int evdev_fetch_next_event(struct evdev_client *client, |
| 505 | struct input_event *event) |
| 506 | { |
| 507 | int have_event; |
| 508 | |
| 509 | spin_lock_irq(&client->buffer_lock); |
| 510 | |
Dima Zavin | 566cf5b | 2011-12-30 15:16:44 -0800 | [diff] [blame] | 511 | have_event = client->packet_head != client->tail; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 512 | if (have_event) { |
| 513 | *event = client->buffer[client->tail++]; |
Henrik Rydberg | b58f708 | 2010-06-23 09:17:56 -0700 | [diff] [blame] | 514 | client->tail &= client->bufsize - 1; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | spin_unlock_irq(&client->buffer_lock); |
| 518 | |
| 519 | return have_event; |
| 520 | } |
| 521 | |
| 522 | static ssize_t evdev_read(struct file *file, char __user *buffer, |
| 523 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 525 | struct evdev_client *client = file->private_data; |
| 526 | struct evdev *evdev = client->evdev; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 527 | struct input_event event; |
Dmitry Torokhov | 2872a9b | 2012-05-02 00:13:37 -0700 | [diff] [blame] | 528 | size_t read = 0; |
| 529 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
Dmitry Torokhov | 2872a9b | 2012-05-02 00:13:37 -0700 | [diff] [blame] | 531 | if (count != 0 && count < input_event_size()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return -EINVAL; |
| 533 | |
Dmitry Torokhov | 2872a9b | 2012-05-02 00:13:37 -0700 | [diff] [blame] | 534 | for (;;) { |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 535 | if (!evdev->exist || client->revoked) |
Dmitry Torokhov | 2872a9b | 2012-05-02 00:13:37 -0700 | [diff] [blame] | 536 | return -ENODEV; |
| 537 | |
| 538 | if (client->packet_head == client->tail && |
| 539 | (file->f_flags & O_NONBLOCK)) |
| 540 | return -EAGAIN; |
| 541 | |
| 542 | /* |
| 543 | * count == 0 is special - no IO is done but we check |
| 544 | * for error conditions (see above). |
| 545 | */ |
| 546 | if (count == 0) |
| 547 | break; |
| 548 | |
| 549 | while (read + input_event_size() <= count && |
| 550 | evdev_fetch_next_event(client, &event)) { |
| 551 | |
| 552 | if (input_event_to_user(buffer + read, &event)) |
| 553 | return -EFAULT; |
| 554 | |
| 555 | read += input_event_size(); |
| 556 | } |
| 557 | |
| 558 | if (read) |
| 559 | break; |
| 560 | |
| 561 | if (!(file->f_flags & O_NONBLOCK)) { |
| 562 | error = wait_event_interruptible(evdev->wait, |
| 563 | client->packet_head != client->tail || |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 564 | !evdev->exist || client->revoked); |
Dmitry Torokhov | 2872a9b | 2012-05-02 00:13:37 -0700 | [diff] [blame] | 565 | if (error) |
| 566 | return error; |
| 567 | } |
Dima Zavin | 509f87c | 2011-12-30 15:16:44 -0800 | [diff] [blame] | 568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Dmitry Torokhov | 2872a9b | 2012-05-02 00:13:37 -0700 | [diff] [blame] | 570 | return read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* No kernel lock - fine */ |
| 574 | static unsigned int evdev_poll(struct file *file, poll_table *wait) |
| 575 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 576 | struct evdev_client *client = file->private_data; |
| 577 | struct evdev *evdev = client->evdev; |
Dmitry Torokhov | c18fb13 | 2010-07-15 23:28:42 -0700 | [diff] [blame] | 578 | unsigned int mask; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 579 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 580 | poll_wait(file, &evdev->wait, wait); |
Dmitry Torokhov | c18fb13 | 2010-07-15 23:28:42 -0700 | [diff] [blame] | 581 | |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 582 | if (evdev->exist && !client->revoked) |
| 583 | mask = POLLOUT | POLLWRNORM; |
| 584 | else |
| 585 | mask = POLLHUP | POLLERR; |
| 586 | |
Jeff Brown | cdda911 | 2011-04-26 22:16:11 -0700 | [diff] [blame] | 587 | if (client->packet_head != client->tail) |
Dmitry Torokhov | c18fb13 | 2010-07-15 23:28:42 -0700 | [diff] [blame] | 588 | mask |= POLLIN | POLLRDNORM; |
| 589 | |
| 590 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 593 | #ifdef CONFIG_COMPAT |
| 594 | |
| 595 | #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8) |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 596 | #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1) |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 597 | |
| 598 | #ifdef __BIG_ENDIAN |
| 599 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 600 | unsigned int maxlen, void __user *p, int compat) |
| 601 | { |
| 602 | int len, i; |
| 603 | |
| 604 | if (compat) { |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 605 | len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t); |
Kenichi Nagai | bf61f8d | 2007-05-11 01:12:15 -0400 | [diff] [blame] | 606 | if (len > maxlen) |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 607 | len = maxlen; |
| 608 | |
| 609 | for (i = 0; i < len / sizeof(compat_long_t); i++) |
| 610 | if (copy_to_user((compat_long_t __user *) p + i, |
| 611 | (compat_long_t *) bits + |
| 612 | i + 1 - ((i % 2) << 1), |
| 613 | sizeof(compat_long_t))) |
| 614 | return -EFAULT; |
| 615 | } else { |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 616 | len = BITS_TO_LONGS(maxbit) * sizeof(long); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 617 | if (len > maxlen) |
| 618 | len = maxlen; |
| 619 | |
| 620 | if (copy_to_user(p, bits, len)) |
| 621 | return -EFAULT; |
| 622 | } |
| 623 | |
| 624 | return len; |
| 625 | } |
| 626 | #else |
| 627 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 628 | unsigned int maxlen, void __user *p, int compat) |
| 629 | { |
| 630 | int len = compat ? |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 631 | BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) : |
| 632 | BITS_TO_LONGS(maxbit) * sizeof(long); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 633 | |
| 634 | if (len > maxlen) |
| 635 | len = maxlen; |
| 636 | |
| 637 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 638 | } |
| 639 | #endif /* __BIG_ENDIAN */ |
| 640 | |
| 641 | #else |
| 642 | |
| 643 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 644 | unsigned int maxlen, void __user *p, int compat) |
| 645 | { |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 646 | int len = BITS_TO_LONGS(maxbit) * sizeof(long); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 647 | |
| 648 | if (len > maxlen) |
| 649 | len = maxlen; |
| 650 | |
| 651 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 652 | } |
| 653 | |
| 654 | #endif /* CONFIG_COMPAT */ |
| 655 | |
| 656 | static int str_to_user(const char *str, unsigned int maxlen, void __user *p) |
| 657 | { |
| 658 | int len; |
| 659 | |
| 660 | if (!str) |
| 661 | return -ENOENT; |
| 662 | |
| 663 | len = strlen(str) + 1; |
| 664 | if (len > maxlen) |
| 665 | len = maxlen; |
| 666 | |
| 667 | return copy_to_user(p, str, len) ? -EFAULT : len; |
| 668 | } |
| 669 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 670 | static int handle_eviocgbit(struct input_dev *dev, |
| 671 | unsigned int type, unsigned int size, |
| 672 | void __user *p, int compat_mode) |
Linus Torvalds | 5402a73 | 2008-08-05 11:42:42 -0400 | [diff] [blame] | 673 | { |
| 674 | unsigned long *bits; |
| 675 | int len; |
| 676 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 677 | switch (type) { |
Linus Torvalds | 5402a73 | 2008-08-05 11:42:42 -0400 | [diff] [blame] | 678 | |
| 679 | case 0: bits = dev->evbit; len = EV_MAX; break; |
| 680 | case EV_KEY: bits = dev->keybit; len = KEY_MAX; break; |
| 681 | case EV_REL: bits = dev->relbit; len = REL_MAX; break; |
| 682 | case EV_ABS: bits = dev->absbit; len = ABS_MAX; break; |
| 683 | case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break; |
| 684 | case EV_LED: bits = dev->ledbit; len = LED_MAX; break; |
| 685 | case EV_SND: bits = dev->sndbit; len = SND_MAX; break; |
| 686 | case EV_FF: bits = dev->ffbit; len = FF_MAX; break; |
| 687 | case EV_SW: bits = dev->swbit; len = SW_MAX; break; |
| 688 | default: return -EINVAL; |
| 689 | } |
Dmitry Torokhov | f2afa77 | 2008-08-08 11:46:53 -0400 | [diff] [blame] | 690 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 691 | return bits_to_user(bits, len, size, p, compat_mode); |
Linus Torvalds | 5402a73 | 2008-08-05 11:42:42 -0400 | [diff] [blame] | 692 | } |
Linus Torvalds | 5402a73 | 2008-08-05 11:42:42 -0400 | [diff] [blame] | 693 | |
Dmitry Torokhov | ab4e019 | 2010-12-14 23:53:21 -0800 | [diff] [blame] | 694 | static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p) |
| 695 | { |
| 696 | struct input_keymap_entry ke = { |
| 697 | .len = sizeof(unsigned int), |
| 698 | .flags = 0, |
| 699 | }; |
| 700 | int __user *ip = (int __user *)p; |
| 701 | int error; |
| 702 | |
| 703 | /* legacy case */ |
| 704 | if (copy_from_user(ke.scancode, p, sizeof(unsigned int))) |
| 705 | return -EFAULT; |
| 706 | |
| 707 | error = input_get_keycode(dev, &ke); |
| 708 | if (error) |
| 709 | return error; |
| 710 | |
| 711 | if (put_user(ke.keycode, ip + 1)) |
| 712 | return -EFAULT; |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p) |
Mauro Carvalho Chehab | 8613e4c | 2010-09-09 21:54:22 -0700 | [diff] [blame] | 718 | { |
| 719 | struct input_keymap_entry ke; |
| 720 | int error; |
| 721 | |
Dmitry Torokhov | ab4e019 | 2010-12-14 23:53:21 -0800 | [diff] [blame] | 722 | if (copy_from_user(&ke, p, sizeof(ke))) |
| 723 | return -EFAULT; |
Mauro Carvalho Chehab | 8613e4c | 2010-09-09 21:54:22 -0700 | [diff] [blame] | 724 | |
Dmitry Torokhov | ab4e019 | 2010-12-14 23:53:21 -0800 | [diff] [blame] | 725 | error = input_get_keycode(dev, &ke); |
| 726 | if (error) |
| 727 | return error; |
Mauro Carvalho Chehab | 8613e4c | 2010-09-09 21:54:22 -0700 | [diff] [blame] | 728 | |
Dmitry Torokhov | ab4e019 | 2010-12-14 23:53:21 -0800 | [diff] [blame] | 729 | if (copy_to_user(p, &ke, sizeof(ke))) |
| 730 | return -EFAULT; |
Mauro Carvalho Chehab | 8613e4c | 2010-09-09 21:54:22 -0700 | [diff] [blame] | 731 | |
Mauro Carvalho Chehab | 8613e4c | 2010-09-09 21:54:22 -0700 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
Dmitry Torokhov | ab4e019 | 2010-12-14 23:53:21 -0800 | [diff] [blame] | 735 | static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p) |
| 736 | { |
| 737 | struct input_keymap_entry ke = { |
| 738 | .len = sizeof(unsigned int), |
| 739 | .flags = 0, |
| 740 | }; |
| 741 | int __user *ip = (int __user *)p; |
| 742 | |
| 743 | if (copy_from_user(ke.scancode, p, sizeof(unsigned int))) |
| 744 | return -EFAULT; |
| 745 | |
| 746 | if (get_user(ke.keycode, ip + 1)) |
| 747 | return -EFAULT; |
| 748 | |
| 749 | return input_set_keycode(dev, &ke); |
| 750 | } |
| 751 | |
| 752 | static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p) |
Mauro Carvalho Chehab | 8613e4c | 2010-09-09 21:54:22 -0700 | [diff] [blame] | 753 | { |
| 754 | struct input_keymap_entry ke; |
| 755 | |
Dmitry Torokhov | ab4e019 | 2010-12-14 23:53:21 -0800 | [diff] [blame] | 756 | if (copy_from_user(&ke, p, sizeof(ke))) |
| 757 | return -EFAULT; |
Mauro Carvalho Chehab | 8613e4c | 2010-09-09 21:54:22 -0700 | [diff] [blame] | 758 | |
Dmitry Torokhov | ab4e019 | 2010-12-14 23:53:21 -0800 | [diff] [blame] | 759 | if (ke.len > sizeof(ke.scancode)) |
| 760 | return -EINVAL; |
Mauro Carvalho Chehab | 8613e4c | 2010-09-09 21:54:22 -0700 | [diff] [blame] | 761 | |
| 762 | return input_set_keycode(dev, &ke); |
| 763 | } |
| 764 | |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 765 | /* |
| 766 | * If we transfer state to the user, we should flush all pending events |
| 767 | * of the same type from the client's queue. Otherwise, they might end up |
| 768 | * with duplicate events, which can screw up client's state tracking. |
| 769 | * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED |
| 770 | * event so user-space will notice missing events. |
| 771 | * |
| 772 | * LOCKING: |
| 773 | * We need to take event_lock before buffer_lock to avoid dead-locks. But we |
| 774 | * need the even_lock only to guarantee consistent state. We can safely release |
| 775 | * it while flushing the queue. This allows input-core to handle filters while |
| 776 | * we flush the queue. |
| 777 | */ |
| 778 | static int evdev_handle_get_val(struct evdev_client *client, |
| 779 | struct input_dev *dev, unsigned int type, |
Dmitry Torokhov | 7c4f560 | 2014-10-06 10:55:49 -0700 | [diff] [blame] | 780 | unsigned long *bits, unsigned int maxbit, |
| 781 | unsigned int maxlen, void __user *p, |
| 782 | int compat) |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 783 | { |
| 784 | int ret; |
| 785 | unsigned long *mem; |
Dmitry Torokhov | 7c4f560 | 2014-10-06 10:55:49 -0700 | [diff] [blame] | 786 | size_t len; |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 787 | |
Dmitry Torokhov | 7c4f560 | 2014-10-06 10:55:49 -0700 | [diff] [blame] | 788 | len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long); |
| 789 | mem = kmalloc(len, GFP_KERNEL); |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 790 | if (!mem) |
| 791 | return -ENOMEM; |
| 792 | |
| 793 | spin_lock_irq(&dev->event_lock); |
| 794 | spin_lock(&client->buffer_lock); |
| 795 | |
Dmitry Torokhov | 7c4f560 | 2014-10-06 10:55:49 -0700 | [diff] [blame] | 796 | memcpy(mem, bits, len); |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 797 | |
| 798 | spin_unlock(&dev->event_lock); |
| 799 | |
| 800 | __evdev_flush_queue(client, type); |
| 801 | |
| 802 | spin_unlock_irq(&client->buffer_lock); |
| 803 | |
Dmitry Torokhov | 7c4f560 | 2014-10-06 10:55:49 -0700 | [diff] [blame] | 804 | ret = bits_to_user(mem, maxbit, maxlen, p, compat); |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 805 | if (ret < 0) |
Anshul Garg | 0c3e994 | 2015-01-15 09:06:50 -0800 | [diff] [blame^] | 806 | evdev_queue_syn_dropped(client, false); |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 807 | |
| 808 | kfree(mem); |
| 809 | |
| 810 | return ret; |
| 811 | } |
| 812 | |
Henrik Rydberg | 1cf0c6e | 2012-02-06 08:49:25 +0100 | [diff] [blame] | 813 | static int evdev_handle_mt_request(struct input_dev *dev, |
| 814 | unsigned int size, |
| 815 | int __user *ip) |
| 816 | { |
Henrik Rydberg | 8d18fba | 2012-09-15 15:15:58 +0200 | [diff] [blame] | 817 | const struct input_mt *mt = dev->mt; |
Henrik Rydberg | 1cf0c6e | 2012-02-06 08:49:25 +0100 | [diff] [blame] | 818 | unsigned int code; |
| 819 | int max_slots; |
| 820 | int i; |
| 821 | |
| 822 | if (get_user(code, &ip[0])) |
| 823 | return -EFAULT; |
Henrik Rydberg | 8d18fba | 2012-09-15 15:15:58 +0200 | [diff] [blame] | 824 | if (!mt || !input_is_mt_value(code)) |
Henrik Rydberg | 1cf0c6e | 2012-02-06 08:49:25 +0100 | [diff] [blame] | 825 | return -EINVAL; |
| 826 | |
| 827 | max_slots = (size - sizeof(__u32)) / sizeof(__s32); |
Henrik Rydberg | 8d18fba | 2012-09-15 15:15:58 +0200 | [diff] [blame] | 828 | for (i = 0; i < mt->num_slots && i < max_slots; i++) { |
| 829 | int value = input_mt_get_value(&mt->slots[i], code); |
| 830 | if (put_user(value, &ip[1 + i])) |
Henrik Rydberg | 1cf0c6e | 2012-02-06 08:49:25 +0100 | [diff] [blame] | 831 | return -EFAULT; |
Henrik Rydberg | 8d18fba | 2012-09-15 15:15:58 +0200 | [diff] [blame] | 832 | } |
Henrik Rydberg | 1cf0c6e | 2012-02-06 08:49:25 +0100 | [diff] [blame] | 833 | |
| 834 | return 0; |
| 835 | } |
| 836 | |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 837 | static int evdev_revoke(struct evdev *evdev, struct evdev_client *client, |
| 838 | struct file *file) |
| 839 | { |
| 840 | client->revoked = true; |
| 841 | evdev_ungrab(evdev, client); |
| 842 | input_flush_device(&evdev->handle, file); |
| 843 | wake_up_interruptible(&evdev->wait); |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 848 | static long evdev_do_ioctl(struct file *file, unsigned int cmd, |
| 849 | void __user *p, int compat_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 851 | struct evdev_client *client = file->private_data; |
| 852 | struct evdev *evdev = client->evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | struct input_dev *dev = evdev->handle.dev; |
| 854 | struct input_absinfo abs; |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 855 | struct ff_effect effect; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 856 | int __user *ip = (int __user *)p; |
Dmitry Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 857 | unsigned int i, t, u, v; |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 858 | unsigned int size; |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 859 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 861 | /* First we check for fixed-length commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | switch (cmd) { |
| 863 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 864 | case EVIOCGVERSION: |
| 865 | return put_user(EV_VERSION, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 867 | case EVIOCGID: |
| 868 | if (copy_to_user(p, &dev->id, sizeof(struct input_id))) |
| 869 | return -EFAULT; |
| 870 | return 0; |
Dmitry Torokhov | 08791e5 | 2006-04-29 01:13:21 -0400 | [diff] [blame] | 871 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 872 | case EVIOCGREP: |
| 873 | if (!test_bit(EV_REP, dev->evbit)) |
| 874 | return -ENOSYS; |
| 875 | if (put_user(dev->rep[REP_DELAY], ip)) |
| 876 | return -EFAULT; |
| 877 | if (put_user(dev->rep[REP_PERIOD], ip + 1)) |
| 878 | return -EFAULT; |
| 879 | return 0; |
Dmitry Torokhov | 08791e5 | 2006-04-29 01:13:21 -0400 | [diff] [blame] | 880 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 881 | case EVIOCSREP: |
| 882 | if (!test_bit(EV_REP, dev->evbit)) |
| 883 | return -ENOSYS; |
| 884 | if (get_user(u, ip)) |
| 885 | return -EFAULT; |
| 886 | if (get_user(v, ip + 1)) |
| 887 | return -EFAULT; |
Dmitry Torokhov | 08791e5 | 2006-04-29 01:13:21 -0400 | [diff] [blame] | 888 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 889 | input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u); |
| 890 | input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 891 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 892 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 894 | case EVIOCRMFF: |
| 895 | return input_ff_erase(dev, (int)(unsigned long) p, file); |
| 896 | |
| 897 | case EVIOCGEFFECTS: |
| 898 | i = test_bit(EV_FF, dev->evbit) ? |
| 899 | dev->ff->max_effects : 0; |
| 900 | if (put_user(i, ip)) |
| 901 | return -EFAULT; |
| 902 | return 0; |
| 903 | |
| 904 | case EVIOCGRAB: |
| 905 | if (p) |
| 906 | return evdev_grab(evdev, client); |
| 907 | else |
| 908 | return evdev_ungrab(evdev, client); |
Dmitry Torokhov | ab4e019 | 2010-12-14 23:53:21 -0800 | [diff] [blame] | 909 | |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 910 | case EVIOCREVOKE: |
| 911 | if (p) |
| 912 | return -EINVAL; |
| 913 | else |
| 914 | return evdev_revoke(evdev, client, file); |
| 915 | |
John Stultz | a80b83b | 2012-02-03 00:19:07 -0800 | [diff] [blame] | 916 | case EVIOCSCLOCKID: |
| 917 | if (copy_from_user(&i, p, sizeof(unsigned int))) |
| 918 | return -EFAULT; |
Aniroop Mathur | aac8bcf | 2014-12-17 15:33:06 -0800 | [diff] [blame] | 919 | |
| 920 | return evdev_set_clk_type(client, i); |
John Stultz | a80b83b | 2012-02-03 00:19:07 -0800 | [diff] [blame] | 921 | |
Dmitry Torokhov | ab4e019 | 2010-12-14 23:53:21 -0800 | [diff] [blame] | 922 | case EVIOCGKEYCODE: |
| 923 | return evdev_handle_get_keycode(dev, p); |
| 924 | |
| 925 | case EVIOCSKEYCODE: |
| 926 | return evdev_handle_set_keycode(dev, p); |
| 927 | |
| 928 | case EVIOCGKEYCODE_V2: |
| 929 | return evdev_handle_get_keycode_v2(dev, p); |
| 930 | |
| 931 | case EVIOCSKEYCODE_V2: |
| 932 | return evdev_handle_set_keycode_v2(dev, p); |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 933 | } |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 934 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 935 | size = _IOC_SIZE(cmd); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 936 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 937 | /* Now check variable-length commands */ |
| 938 | #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 939 | switch (EVIOC_MASK_SIZE(cmd)) { |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 940 | |
Henrik Rydberg | 85b7720 | 2010-12-18 20:51:13 +0100 | [diff] [blame] | 941 | case EVIOCGPROP(0): |
| 942 | return bits_to_user(dev->propbit, INPUT_PROP_MAX, |
| 943 | size, p, compat_mode); |
| 944 | |
Henrik Rydberg | 1cf0c6e | 2012-02-06 08:49:25 +0100 | [diff] [blame] | 945 | case EVIOCGMTSLOTS(0): |
| 946 | return evdev_handle_mt_request(dev, size, ip); |
| 947 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 948 | case EVIOCGKEY(0): |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 949 | return evdev_handle_get_val(client, dev, EV_KEY, dev->key, |
| 950 | KEY_MAX, size, p, compat_mode); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 951 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 952 | case EVIOCGLED(0): |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 953 | return evdev_handle_get_val(client, dev, EV_LED, dev->led, |
| 954 | LED_MAX, size, p, compat_mode); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 955 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 956 | case EVIOCGSND(0): |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 957 | return evdev_handle_get_val(client, dev, EV_SND, dev->snd, |
| 958 | SND_MAX, size, p, compat_mode); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 959 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 960 | case EVIOCGSW(0): |
David Herrmann | 4831802 | 2013-04-07 21:13:19 -0700 | [diff] [blame] | 961 | return evdev_handle_get_val(client, dev, EV_SW, dev->sw, |
| 962 | SW_MAX, size, p, compat_mode); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 963 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 964 | case EVIOCGNAME(0): |
| 965 | return str_to_user(dev->name, size, p); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 966 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 967 | case EVIOCGPHYS(0): |
| 968 | return str_to_user(dev->phys, size, p); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 969 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 970 | case EVIOCGUNIQ(0): |
| 971 | return str_to_user(dev->uniq, size, p); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 972 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 973 | case EVIOC_MASK_SIZE(EVIOCSFF): |
| 974 | if (input_ff_effect_from_user(p, size, &effect)) |
| 975 | return -EFAULT; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 976 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 977 | error = input_ff_upload(dev, &effect, file); |
Elias Vanderstuyft | fc7392a | 2014-03-29 12:08:45 -0700 | [diff] [blame] | 978 | if (error) |
| 979 | return error; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 980 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 981 | if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) |
| 982 | return -EFAULT; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 983 | |
Elias Vanderstuyft | fc7392a | 2014-03-29 12:08:45 -0700 | [diff] [blame] | 984 | return 0; |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 985 | } |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 986 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 987 | /* Multi-number variable-length handlers */ |
| 988 | if (_IOC_TYPE(cmd) != 'E') |
| 989 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 991 | if (_IOC_DIR(cmd) == _IOC_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 993 | if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0))) |
| 994 | return handle_eviocgbit(dev, |
| 995 | _IOC_NR(cmd) & EV_MAX, size, |
| 996 | p, compat_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 998 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { |
Adam Dawidowski | f2278f3 | 2008-06-02 01:08:10 -0400 | [diff] [blame] | 999 | |
Daniel Mack | 0a74a1d | 2010-10-18 08:43:30 -0700 | [diff] [blame] | 1000 | if (!dev->absinfo) |
| 1001 | return -EINVAL; |
| 1002 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 1003 | t = _IOC_NR(cmd) & ABS_MAX; |
| 1004 | abs = dev->absinfo[t]; |
Adam Dawidowski | f2278f3 | 2008-06-02 01:08:10 -0400 | [diff] [blame] | 1005 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 1006 | if (copy_to_user(p, &abs, min_t(size_t, |
| 1007 | size, sizeof(struct input_absinfo)))) |
| 1008 | return -EFAULT; |
Adam Dawidowski | f2278f3 | 2008-06-02 01:08:10 -0400 | [diff] [blame] | 1009 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 1010 | return 0; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1011 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | } |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 1013 | |
Daniel Mack | f9ce6eb5 | 2010-10-18 08:43:50 -0700 | [diff] [blame] | 1014 | if (_IOC_DIR(cmd) == _IOC_WRITE) { |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 1015 | |
| 1016 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { |
| 1017 | |
Daniel Mack | 0a74a1d | 2010-10-18 08:43:30 -0700 | [diff] [blame] | 1018 | if (!dev->absinfo) |
| 1019 | return -EINVAL; |
| 1020 | |
Dmitry Torokhov | 448cd16 | 2010-08-02 20:29:10 -0700 | [diff] [blame] | 1021 | t = _IOC_NR(cmd) & ABS_MAX; |
| 1022 | |
| 1023 | if (copy_from_user(&abs, p, min_t(size_t, |
| 1024 | size, sizeof(struct input_absinfo)))) |
| 1025 | return -EFAULT; |
| 1026 | |
| 1027 | if (size < sizeof(struct input_absinfo)) |
| 1028 | abs.resolution = 0; |
| 1029 | |
| 1030 | /* We can't change number of reserved MT slots */ |
| 1031 | if (t == ABS_MT_SLOT) |
| 1032 | return -EINVAL; |
| 1033 | |
| 1034 | /* |
| 1035 | * Take event lock to ensure that we are not |
| 1036 | * changing device parameters in the middle |
| 1037 | * of event. |
| 1038 | */ |
| 1039 | spin_lock_irq(&dev->event_lock); |
| 1040 | dev->absinfo[t] = abs; |
| 1041 | spin_unlock_irq(&dev->event_lock); |
| 1042 | |
| 1043 | return 0; |
| 1044 | } |
| 1045 | } |
| 1046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | return -EINVAL; |
| 1048 | } |
| 1049 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1050 | static long evdev_ioctl_handler(struct file *file, unsigned int cmd, |
| 1051 | void __user *p, int compat_mode) |
| 1052 | { |
| 1053 | struct evdev_client *client = file->private_data; |
| 1054 | struct evdev *evdev = client->evdev; |
| 1055 | int retval; |
| 1056 | |
| 1057 | retval = mutex_lock_interruptible(&evdev->mutex); |
| 1058 | if (retval) |
| 1059 | return retval; |
| 1060 | |
David Herrmann | c7dc657 | 2013-09-07 12:23:05 -0700 | [diff] [blame] | 1061 | if (!evdev->exist || client->revoked) { |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1062 | retval = -ENODEV; |
| 1063 | goto out; |
| 1064 | } |
| 1065 | |
| 1066 | retval = evdev_do_ioctl(file, cmd, p, compat_mode); |
| 1067 | |
| 1068 | out: |
| 1069 | mutex_unlock(&evdev->mutex); |
| 1070 | return retval; |
| 1071 | } |
| 1072 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 1073 | static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1074 | { |
| 1075 | return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0); |
| 1076 | } |
| 1077 | |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 1078 | #ifdef CONFIG_COMPAT |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1079 | static long evdev_ioctl_compat(struct file *file, |
| 1080 | unsigned int cmd, unsigned long arg) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 1081 | { |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 1082 | return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1); |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 1083 | } |
| 1084 | #endif |
| 1085 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 1086 | static const struct file_operations evdev_fops = { |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1087 | .owner = THIS_MODULE, |
| 1088 | .read = evdev_read, |
| 1089 | .write = evdev_write, |
| 1090 | .poll = evdev_poll, |
| 1091 | .open = evdev_open, |
| 1092 | .release = evdev_release, |
| 1093 | .unlocked_ioctl = evdev_ioctl, |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 1094 | #ifdef CONFIG_COMPAT |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1095 | .compat_ioctl = evdev_ioctl_compat, |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 1096 | #endif |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1097 | .fasync = evdev_fasync, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1098 | .flush = evdev_flush, |
| 1099 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | }; |
| 1101 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1102 | /* |
| 1103 | * Mark device non-existent. This disables writes, ioctls and |
| 1104 | * prevents new users from opening the device. Already posted |
| 1105 | * blocking reads will stay, however new ones will fail. |
| 1106 | */ |
| 1107 | static void evdev_mark_dead(struct evdev *evdev) |
| 1108 | { |
| 1109 | mutex_lock(&evdev->mutex); |
Dmitry Torokhov | 20da92d | 2010-07-15 23:27:36 -0700 | [diff] [blame] | 1110 | evdev->exist = false; |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1111 | mutex_unlock(&evdev->mutex); |
| 1112 | } |
| 1113 | |
| 1114 | static void evdev_cleanup(struct evdev *evdev) |
| 1115 | { |
| 1116 | struct input_handle *handle = &evdev->handle; |
| 1117 | |
| 1118 | evdev_mark_dead(evdev); |
| 1119 | evdev_hangup(evdev); |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1120 | |
| 1121 | cdev_del(&evdev->cdev); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1122 | |
| 1123 | /* evdev is marked dead so no one else accesses evdev->open */ |
| 1124 | if (evdev->open) { |
| 1125 | input_flush_device(handle, NULL); |
| 1126 | input_close_device(handle); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Create new evdev device. Note that input core serializes calls |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1132 | * to connect and disconnect. |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1133 | */ |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 1134 | static int evdev_connect(struct input_handler *handler, struct input_dev *dev, |
| 1135 | const struct input_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | { |
| 1137 | struct evdev *evdev; |
| 1138 | int minor; |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1139 | int dev_no; |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 1140 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1142 | minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true); |
| 1143 | if (minor < 0) { |
| 1144 | error = minor; |
| 1145 | pr_err("failed to reserve new minor: %d\n", error); |
| 1146 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 1149 | evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL); |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1150 | if (!evdev) { |
| 1151 | error = -ENOMEM; |
| 1152 | goto err_free_minor; |
| 1153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 1155 | INIT_LIST_HEAD(&evdev->client_list); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1156 | spin_lock_init(&evdev->client_lock); |
| 1157 | mutex_init(&evdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | init_waitqueue_head(&evdev->wait); |
Dmitry Torokhov | 20da92d | 2010-07-15 23:27:36 -0700 | [diff] [blame] | 1159 | evdev->exist = true; |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1160 | |
| 1161 | dev_no = minor; |
| 1162 | /* Normalize device number if it falls into legacy range */ |
| 1163 | if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS) |
| 1164 | dev_no -= EVDEV_MINOR_BASE; |
| 1165 | dev_set_name(&evdev->dev, "event%d", dev_no); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1166 | |
Dmitry Torokhov | a7097ff | 2008-04-01 00:22:53 -0400 | [diff] [blame] | 1167 | evdev->handle.dev = input_get_device(dev); |
Thadeu Lima de Souza Cascardo | 3d5cb60 | 2009-05-09 16:08:04 -0700 | [diff] [blame] | 1168 | evdev->handle.name = dev_name(&evdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | evdev->handle.handler = handler; |
| 1170 | evdev->handle.private = evdev; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 1171 | |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1172 | evdev->dev.devt = MKDEV(INPUT_MAJOR, minor); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 1173 | evdev->dev.class = &input_class; |
| 1174 | evdev->dev.parent = &dev->dev; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 1175 | evdev->dev.release = evdev_free; |
| 1176 | device_initialize(&evdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1178 | error = input_register_handle(&evdev->handle); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 1179 | if (error) |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 1180 | goto err_free_evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1182 | cdev_init(&evdev->cdev, &evdev_fops); |
Dmitry Torokhov | 4a215aa | 2012-10-21 17:57:20 -0700 | [diff] [blame] | 1183 | evdev->cdev.kobj.parent = &evdev->dev.kobj; |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1184 | error = cdev_add(&evdev->cdev, evdev->dev.devt, 1); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 1185 | if (error) |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1186 | goto err_unregister_handle; |
| 1187 | |
| 1188 | error = device_add(&evdev->dev); |
| 1189 | if (error) |
| 1190 | goto err_cleanup_evdev; |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 1191 | |
| 1192 | return 0; |
| 1193 | |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1194 | err_cleanup_evdev: |
| 1195 | evdev_cleanup(evdev); |
| 1196 | err_unregister_handle: |
| 1197 | input_unregister_handle(&evdev->handle); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 1198 | err_free_evdev: |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 1199 | put_device(&evdev->dev); |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1200 | err_free_minor: |
| 1201 | input_free_minor(minor); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 1202 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | static void evdev_disconnect(struct input_handle *handle) |
| 1206 | { |
| 1207 | struct evdev *evdev = handle->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 1209 | device_del(&evdev->dev); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1210 | evdev_cleanup(evdev); |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1211 | input_free_minor(MINOR(evdev->dev.devt)); |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1212 | input_unregister_handle(handle); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 1213 | put_device(&evdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 1216 | static const struct input_device_id evdev_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | { .driver_info = 1 }, /* Matches all devices */ |
| 1218 | { }, /* Terminating zero entry */ |
| 1219 | }; |
| 1220 | |
| 1221 | MODULE_DEVICE_TABLE(input, evdev_ids); |
| 1222 | |
| 1223 | static struct input_handler evdev_handler = { |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1224 | .event = evdev_event, |
Henrik Rydberg | a274ac1 | 2012-08-29 20:48:02 +0200 | [diff] [blame] | 1225 | .events = evdev_events, |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1226 | .connect = evdev_connect, |
| 1227 | .disconnect = evdev_disconnect, |
Dmitry Torokhov | 7f8d4ca | 2012-10-08 09:07:24 -0700 | [diff] [blame] | 1228 | .legacy_minors = true, |
Dmitry Torokhov | 6addb1d | 2007-08-30 00:22:18 -0400 | [diff] [blame] | 1229 | .minor = EVDEV_MINOR_BASE, |
| 1230 | .name = "evdev", |
| 1231 | .id_table = evdev_ids, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | }; |
| 1233 | |
| 1234 | static int __init evdev_init(void) |
| 1235 | { |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 1236 | return input_register_handler(&evdev_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | static void __exit evdev_exit(void) |
| 1240 | { |
| 1241 | input_unregister_handler(&evdev_handler); |
| 1242 | } |
| 1243 | |
| 1244 | module_init(evdev_init); |
| 1245 | module_exit(evdev_exit); |
| 1246 | |
| 1247 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 1248 | MODULE_DESCRIPTION("Input driver event char devices"); |
| 1249 | MODULE_LICENSE("GPL"); |