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