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