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