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