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