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