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