Merge with rsync://fileserver/linux
[linux-2.6] / drivers / input / mousedev.c
1 /*
2  * Input driver to ExplorerPS/2 device driver module.
3  *
4  * Copyright (c) 1999-2002 Vojtech Pavlik
5  * Copyright (c) 2004      Dmitry Torokhov
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  */
11
12 #define MOUSEDEV_MINOR_BASE     32
13 #define MOUSEDEV_MINORS         32
14 #define MOUSEDEV_MIX            31
15
16 #include <linux/slab.h>
17 #include <linux/poll.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/init.h>
21 #include <linux/input.h>
22 #include <linux/config.h>
23 #include <linux/smp_lock.h>
24 #include <linux/random.h>
25 #include <linux/major.h>
26 #include <linux/device.h>
27 #include <linux/devfs_fs_kernel.h>
28 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
29 #include <linux/miscdevice.h>
30 #endif
31
32 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
33 MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
34 MODULE_LICENSE("GPL");
35
36 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
37 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X  1024
38 #endif
39 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
40 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y  768
41 #endif
42
43 static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
44 module_param(xres, uint, 0);
45 MODULE_PARM_DESC(xres, "Horizontal screen resolution");
46
47 static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
48 module_param(yres, uint, 0);
49 MODULE_PARM_DESC(yres, "Vertical screen resolution");
50
51 static unsigned tap_time = 200;
52 module_param(tap_time, uint, 0);
53 MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)");
54
55 struct mousedev_hw_data {
56         int dx, dy, dz;
57         int x, y;
58         int abs_event;
59         unsigned long buttons;
60 };
61
62 struct mousedev {
63         int exist;
64         int open;
65         int minor;
66         char name[16];
67         wait_queue_head_t wait;
68         struct list_head list;
69         struct input_handle handle;
70
71         struct mousedev_hw_data packet;
72         unsigned int pkt_count;
73         int old_x[4], old_y[4];
74         int frac_dx, frac_dy;
75         unsigned long touch;
76 };
77
78 enum mousedev_emul {
79         MOUSEDEV_EMUL_PS2,
80         MOUSEDEV_EMUL_IMPS,
81         MOUSEDEV_EMUL_EXPS
82 };
83
84 struct mousedev_motion {
85         int dx, dy, dz;
86         unsigned long buttons;
87 };
88
89 #define PACKET_QUEUE_LEN        16
90 struct mousedev_list {
91         struct fasync_struct *fasync;
92         struct mousedev *mousedev;
93         struct list_head node;
94
95         struct mousedev_motion packets[PACKET_QUEUE_LEN];
96         unsigned int head, tail;
97         spinlock_t packet_lock;
98         int pos_x, pos_y;
99
100         signed char ps2[6];
101         unsigned char ready, buffer, bufsiz;
102         unsigned char imexseq, impsseq;
103         enum mousedev_emul mode;
104         unsigned long last_buttons;
105 };
106
107 #define MOUSEDEV_SEQ_LEN        6
108
109 static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
110 static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
111
112 static struct input_handler mousedev_handler;
113
114 static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
115 static struct mousedev mousedev_mix;
116
117 #define fx(i)  (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
118 #define fy(i)  (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
119
120 static void mousedev_touchpad_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
121 {
122         int size, tmp;
123         enum { FRACTION_DENOM = 128 };
124
125         if (mousedev->touch) {
126                 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
127                 if (size == 0) size = 256 * 2;
128                 switch (code) {
129                         case ABS_X:
130                                 fx(0) = value;
131                                 if (mousedev->pkt_count >= 2) {
132                                         tmp = ((value - fx(2)) * (256 * FRACTION_DENOM)) / size;
133                                         tmp += mousedev->frac_dx;
134                                         mousedev->packet.dx = tmp / FRACTION_DENOM;
135                                         mousedev->frac_dx = tmp - mousedev->packet.dx * FRACTION_DENOM;
136                                 }
137                                 break;
138
139                         case ABS_Y:
140                                 fy(0) = value;
141                                 if (mousedev->pkt_count >= 2) {
142                                         tmp = -((value - fy(2)) * (256 * FRACTION_DENOM)) / size;
143                                         tmp += mousedev->frac_dy;
144                                         mousedev->packet.dy = tmp / FRACTION_DENOM;
145                                         mousedev->frac_dy = tmp - mousedev->packet.dy * FRACTION_DENOM;
146                                 }
147                                 break;
148                 }
149         }
150 }
151
152 static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
153 {
154         int size;
155
156         switch (code) {
157                 case ABS_X:
158                         size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
159                         if (size == 0) size = xres;
160                         if (value > dev->absmax[ABS_X]) value = dev->absmax[ABS_X];
161                         if (value < dev->absmin[ABS_X]) value = dev->absmin[ABS_X];
162                         mousedev->packet.x = ((value - dev->absmin[ABS_X]) * xres) / size;
163                         mousedev->packet.abs_event = 1;
164                         break;
165
166                 case ABS_Y:
167                         size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
168                         if (size == 0) size = yres;
169                         if (value > dev->absmax[ABS_Y]) value = dev->absmax[ABS_Y];
170                         if (value < dev->absmin[ABS_Y]) value = dev->absmin[ABS_Y];
171                         mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size;
172                         mousedev->packet.abs_event = 1;
173                         break;
174         }
175 }
176
177 static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
178 {
179         switch (code) {
180                 case REL_X:     mousedev->packet.dx += value; break;
181                 case REL_Y:     mousedev->packet.dy -= value; break;
182                 case REL_WHEEL: mousedev->packet.dz -= value; break;
183         }
184 }
185
186 static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
187 {
188         int index;
189
190         switch (code) {
191                 case BTN_TOUCH:
192                 case BTN_0:
193                 case BTN_FORWARD:
194                 case BTN_LEFT:          index = 0; break;
195                 case BTN_STYLUS:
196                 case BTN_1:
197                 case BTN_RIGHT:         index = 1; break;
198                 case BTN_2:
199                 case BTN_STYLUS2:
200                 case BTN_MIDDLE:        index = 2; break;
201                 case BTN_3:
202                 case BTN_BACK:
203                 case BTN_SIDE:          index = 3; break;
204                 case BTN_4:
205                 case BTN_EXTRA:         index = 4; break;
206                 default:                return;
207         }
208
209         if (value) {
210                 set_bit(index, &mousedev->packet.buttons);
211                 set_bit(index, &mousedev_mix.packet.buttons);
212         } else {
213                 clear_bit(index, &mousedev->packet.buttons);
214                 clear_bit(index, &mousedev_mix.packet.buttons);
215         }
216 }
217
218 static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet)
219 {
220         struct mousedev_list *list;
221         struct mousedev_motion *p;
222         unsigned long flags;
223         int wake_readers = 0;
224
225         list_for_each_entry(list, &mousedev->list, node) {
226                 spin_lock_irqsave(&list->packet_lock, flags);
227
228                 p = &list->packets[list->head];
229                 if (list->ready && p->buttons != mousedev->packet.buttons) {
230                         unsigned int new_head = (list->head + 1) % PACKET_QUEUE_LEN;
231                         if (new_head != list->tail) {
232                                 p = &list->packets[list->head = new_head];
233                                 memset(p, 0, sizeof(struct mousedev_motion));
234                         }
235                 }
236
237                 if (packet->abs_event) {
238                         p->dx += packet->x - list->pos_x;
239                         p->dy += packet->y - list->pos_y;
240                         list->pos_x = packet->x;
241                         list->pos_y = packet->y;
242                 }
243
244                 list->pos_x += packet->dx;
245                 list->pos_x = list->pos_x < 0 ? 0 : (list->pos_x >= xres ? xres : list->pos_x);
246                 list->pos_y += packet->dy;
247                 list->pos_y = list->pos_y < 0 ? 0 : (list->pos_y >= yres ? yres : list->pos_y);
248
249                 p->dx += packet->dx;
250                 p->dy += packet->dy;
251                 p->dz += packet->dz;
252                 p->buttons = mousedev->packet.buttons;
253
254                 if (p->dx || p->dy || p->dz || p->buttons != list->last_buttons)
255                         list->ready = 1;
256
257                 spin_unlock_irqrestore(&list->packet_lock, flags);
258
259                 if (list->ready) {
260                         kill_fasync(&list->fasync, SIGIO, POLL_IN);
261                         wake_readers = 1;
262                 }
263         }
264
265         if (wake_readers)
266                 wake_up_interruptible(&mousedev->wait);
267 }
268
269 static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
270 {
271         if (!value) {
272                 if (mousedev->touch &&
273                     time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) {
274                         /*
275                          * Toggle left button to emulate tap.
276                          * We rely on the fact that mousedev_mix always has 0
277                          * motion packet so we won't mess current position.
278                          */
279                         set_bit(0, &mousedev->packet.buttons);
280                         set_bit(0, &mousedev_mix.packet.buttons);
281                         mousedev_notify_readers(mousedev, &mousedev_mix.packet);
282                         mousedev_notify_readers(&mousedev_mix, &mousedev_mix.packet);
283                         clear_bit(0, &mousedev->packet.buttons);
284                         clear_bit(0, &mousedev_mix.packet.buttons);
285                 }
286                 mousedev->touch = mousedev->pkt_count = 0;
287                 mousedev->frac_dx = 0;
288                 mousedev->frac_dy = 0;
289         }
290         else
291                 if (!mousedev->touch)
292                         mousedev->touch = jiffies;
293 }
294
295 static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
296 {
297         struct mousedev *mousedev = handle->private;
298
299         switch (type) {
300                 case EV_ABS:
301                         /* Ignore joysticks */
302                         if (test_bit(BTN_TRIGGER, handle->dev->keybit))
303                                 return;
304
305                         if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
306                                 mousedev_touchpad_event(handle->dev, mousedev, code, value);
307                         else
308                                 mousedev_abs_event(handle->dev, mousedev, code, value);
309
310                         break;
311
312                 case EV_REL:
313                         mousedev_rel_event(mousedev, code, value);
314                         break;
315
316                 case EV_KEY:
317                         if (value != 2) {
318                                 if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
319                                         mousedev_touchpad_touch(mousedev, value);
320                                 else
321                                         mousedev_key_event(mousedev, code, value);
322                         }
323                         break;
324
325                 case EV_SYN:
326                         if (code == SYN_REPORT) {
327                                 if (mousedev->touch) {
328                                         mousedev->pkt_count++;
329                                         /* Input system eats duplicate events, but we need all of them
330                                          * to do correct averaging so apply present one forward
331                                          */
332                                         fx(0) = fx(1);
333                                         fy(0) = fy(1);
334                                 }
335
336                                 mousedev_notify_readers(mousedev, &mousedev->packet);
337                                 mousedev_notify_readers(&mousedev_mix, &mousedev->packet);
338
339                                 mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0;
340                                 mousedev->packet.abs_event = 0;
341                         }
342                         break;
343         }
344 }
345
346 static int mousedev_fasync(int fd, struct file *file, int on)
347 {
348         int retval;
349         struct mousedev_list *list = file->private_data;
350         retval = fasync_helper(fd, file, on, &list->fasync);
351         return retval < 0 ? retval : 0;
352 }
353
354 static void mousedev_free(struct mousedev *mousedev)
355 {
356         mousedev_table[mousedev->minor] = NULL;
357         kfree(mousedev);
358 }
359
360 static int mixdev_release(void)
361 {
362         struct input_handle *handle;
363
364         list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
365                 struct mousedev *mousedev = handle->private;
366
367                 if (!mousedev->open) {
368                         if (mousedev->exist)
369                                 input_close_device(&mousedev->handle);
370                         else
371                                 mousedev_free(mousedev);
372                 }
373         }
374
375         return 0;
376 }
377
378 static int mousedev_release(struct inode * inode, struct file * file)
379 {
380         struct mousedev_list *list = file->private_data;
381
382         mousedev_fasync(-1, file, 0);
383
384         list_del(&list->node);
385
386         if (!--list->mousedev->open) {
387                 if (list->mousedev->minor == MOUSEDEV_MIX)
388                         return mixdev_release();
389
390                 if (!mousedev_mix.open) {
391                         if (list->mousedev->exist)
392                                 input_close_device(&list->mousedev->handle);
393                         else
394                                 mousedev_free(list->mousedev);
395                 }
396         }
397
398         kfree(list);
399         return 0;
400 }
401
402 static int mousedev_open(struct inode * inode, struct file * file)
403 {
404         struct mousedev_list *list;
405         struct input_handle *handle;
406         struct mousedev *mousedev;
407         int i;
408
409 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
410         if (imajor(inode) == MISC_MAJOR)
411                 i = MOUSEDEV_MIX;
412         else
413 #endif
414                 i = iminor(inode) - MOUSEDEV_MINOR_BASE;
415
416         if (i >= MOUSEDEV_MINORS || !mousedev_table[i])
417                 return -ENODEV;
418
419         if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
420                 return -ENOMEM;
421         memset(list, 0, sizeof(struct mousedev_list));
422
423         spin_lock_init(&list->packet_lock);
424         list->pos_x = xres / 2;
425         list->pos_y = yres / 2;
426         list->mousedev = mousedev_table[i];
427         list_add_tail(&list->node, &mousedev_table[i]->list);
428         file->private_data = list;
429
430         if (!list->mousedev->open++) {
431                 if (list->mousedev->minor == MOUSEDEV_MIX) {
432                         list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
433                                 mousedev = handle->private;
434                                 if (!mousedev->open && mousedev->exist)
435                                         input_open_device(handle);
436                         }
437                 } else
438                         if (!mousedev_mix.open && list->mousedev->exist)
439                                 input_open_device(&list->mousedev->handle);
440         }
441
442         return 0;
443 }
444
445 static inline int mousedev_limit_delta(int delta, int limit)
446 {
447         return delta > limit ? limit : (delta < -limit ? -limit : delta);
448 }
449
450 static void mousedev_packet(struct mousedev_list *list, signed char *ps2_data)
451 {
452         struct mousedev_motion *p;
453         unsigned long flags;
454
455         spin_lock_irqsave(&list->packet_lock, flags);
456         p = &list->packets[list->tail];
457
458         ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
459         ps2_data[1] = mousedev_limit_delta(p->dx, 127);
460         ps2_data[2] = mousedev_limit_delta(p->dy, 127);
461         p->dx -= ps2_data[1];
462         p->dy -= ps2_data[2];
463
464         switch (list->mode) {
465                 case MOUSEDEV_EMUL_EXPS:
466                         ps2_data[3] = mousedev_limit_delta(p->dz, 7);
467                         p->dz -= ps2_data[3];
468                         ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
469                         list->bufsiz = 4;
470                         break;
471
472                 case MOUSEDEV_EMUL_IMPS:
473                         ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
474                         ps2_data[3] = mousedev_limit_delta(p->dz, 127);
475                         p->dz -= ps2_data[3];
476                         list->bufsiz = 4;
477                         break;
478
479                 case MOUSEDEV_EMUL_PS2:
480                 default:
481                         ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
482                         p->dz = 0;
483                         list->bufsiz = 3;
484                         break;
485         }
486
487         if (!p->dx && !p->dy && !p->dz) {
488                 if (list->tail == list->head) {
489                         list->ready = 0;
490                         list->last_buttons = p->buttons;
491                 } else
492                         list->tail = (list->tail + 1) % PACKET_QUEUE_LEN;
493         }
494
495         spin_unlock_irqrestore(&list->packet_lock, flags);
496 }
497
498
499 static ssize_t mousedev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
500 {
501         struct mousedev_list *list = file->private_data;
502         unsigned char c;
503         unsigned int i;
504
505         for (i = 0; i < count; i++) {
506
507                 if (get_user(c, buffer + i))
508                         return -EFAULT;
509
510                 if (c == mousedev_imex_seq[list->imexseq]) {
511                         if (++list->imexseq == MOUSEDEV_SEQ_LEN) {
512                                 list->imexseq = 0;
513                                 list->mode = MOUSEDEV_EMUL_EXPS;
514                         }
515                 } else list->imexseq = 0;
516
517                 if (c == mousedev_imps_seq[list->impsseq]) {
518                         if (++list->impsseq == MOUSEDEV_SEQ_LEN) {
519                                 list->impsseq = 0;
520                                 list->mode = MOUSEDEV_EMUL_IMPS;
521                         }
522                 } else list->impsseq = 0;
523
524                 list->ps2[0] = 0xfa;
525
526                 switch (c) {
527
528                         case 0xeb: /* Poll */
529                                 mousedev_packet(list, &list->ps2[1]);
530                                 list->bufsiz++; /* account for leading ACK */
531                                 break;
532
533                         case 0xf2: /* Get ID */
534                                 switch (list->mode) {
535                                         case MOUSEDEV_EMUL_PS2:  list->ps2[1] = 0; break;
536                                         case MOUSEDEV_EMUL_IMPS: list->ps2[1] = 3; break;
537                                         case MOUSEDEV_EMUL_EXPS: list->ps2[1] = 4; break;
538                                 }
539                                 list->bufsiz = 2;
540                                 break;
541
542                         case 0xe9: /* Get info */
543                                 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
544                                 list->bufsiz = 4;
545                                 break;
546
547                         case 0xff: /* Reset */
548                                 list->impsseq = list->imexseq = 0;
549                                 list->mode = MOUSEDEV_EMUL_PS2;
550                                 list->ps2[1] = 0xaa; list->ps2[2] = 0x00;
551                                 list->bufsiz = 3;
552                                 break;
553
554                         default:
555                                 list->bufsiz = 1;
556                                 break;
557                 }
558
559                 list->buffer = list->bufsiz;
560         }
561
562         kill_fasync(&list->fasync, SIGIO, POLL_IN);
563
564         wake_up_interruptible(&list->mousedev->wait);
565
566         return count;
567 }
568
569 static ssize_t mousedev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
570 {
571         struct mousedev_list *list = file->private_data;
572         int retval = 0;
573
574         if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK))
575                 return -EAGAIN;
576
577         retval = wait_event_interruptible(list->mousedev->wait,
578                                           !list->mousedev->exist || list->ready || list->buffer);
579
580         if (retval)
581                 return retval;
582
583         if (!list->mousedev->exist)
584                 return -ENODEV;
585
586         if (!list->buffer && list->ready) {
587                 mousedev_packet(list, list->ps2);
588                 list->buffer = list->bufsiz;
589         }
590
591         if (count > list->buffer)
592                 count = list->buffer;
593
594         list->buffer -= count;
595
596         if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count))
597                 return -EFAULT;
598
599         return count;
600 }
601
602 /* No kernel lock - fine */
603 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
604 {
605         struct mousedev_list *list = file->private_data;
606         poll_wait(file, &list->mousedev->wait, wait);
607         return ((list->ready || list->buffer) ? (POLLIN | POLLRDNORM) : 0) |
608                 (list->mousedev->exist ? 0 : (POLLHUP | POLLERR));
609 }
610
611 static struct file_operations mousedev_fops = {
612         .owner =        THIS_MODULE,
613         .read =         mousedev_read,
614         .write =        mousedev_write,
615         .poll =         mousedev_poll,
616         .open =         mousedev_open,
617         .release =      mousedev_release,
618         .fasync =       mousedev_fasync,
619 };
620
621 static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
622 {
623         struct mousedev *mousedev;
624         int minor = 0;
625
626         for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
627         if (minor == MOUSEDEV_MINORS) {
628                 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
629                 return NULL;
630         }
631
632         if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
633                 return NULL;
634         memset(mousedev, 0, sizeof(struct mousedev));
635
636         INIT_LIST_HEAD(&mousedev->list);
637         init_waitqueue_head(&mousedev->wait);
638
639         mousedev->minor = minor;
640         mousedev->exist = 1;
641         mousedev->handle.dev = dev;
642         mousedev->handle.name = mousedev->name;
643         mousedev->handle.handler = handler;
644         mousedev->handle.private = mousedev;
645         sprintf(mousedev->name, "mouse%d", minor);
646
647         if (mousedev_mix.open)
648                 input_open_device(&mousedev->handle);
649
650         mousedev_table[minor] = mousedev;
651
652         devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
653                         S_IFCHR|S_IRUGO|S_IWUSR, "input/mouse%d", minor);
654         class_device_create(input_class,
655                         MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
656                         dev->dev, "mouse%d", minor);
657
658         return &mousedev->handle;
659 }
660
661 static void mousedev_disconnect(struct input_handle *handle)
662 {
663         struct mousedev *mousedev = handle->private;
664         struct mousedev_list *list;
665
666         class_device_destroy(input_class,
667                         MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor));
668         devfs_remove("input/mouse%d", mousedev->minor);
669         mousedev->exist = 0;
670
671         if (mousedev->open) {
672                 input_close_device(handle);
673                 wake_up_interruptible(&mousedev->wait);
674                 list_for_each_entry(list, &mousedev->list, node)
675                         kill_fasync(&list->fasync, SIGIO, POLL_HUP);
676         } else {
677                 if (mousedev_mix.open)
678                         input_close_device(handle);
679                 mousedev_free(mousedev);
680         }
681 }
682
683 static struct input_device_id mousedev_ids[] = {
684         {
685                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
686                 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
687                 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
688                 .relbit = { BIT(REL_X) | BIT(REL_Y) },
689         },      /* A mouse like device, at least one button, two relative axes */
690         {
691                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
692                 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
693                 .relbit = { BIT(REL_WHEEL) },
694         },      /* A separate scrollwheel */
695         {
696                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
697                 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
698                 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
699                 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
700         },      /* A tablet like device, at least touch detection, two absolute axes */
701         {
702                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
703                 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
704                 .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
705                 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
706         },      /* A touchpad */
707
708         { },    /* Terminating entry */
709 };
710
711 MODULE_DEVICE_TABLE(input, mousedev_ids);
712
713 static struct input_handler mousedev_handler = {
714         .event =        mousedev_event,
715         .connect =      mousedev_connect,
716         .disconnect =   mousedev_disconnect,
717         .fops =         &mousedev_fops,
718         .minor =        MOUSEDEV_MINOR_BASE,
719         .name =         "mousedev",
720         .id_table =     mousedev_ids,
721 };
722
723 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
724 static struct miscdevice psaux_mouse = {
725         PSMOUSE_MINOR, "psaux", &mousedev_fops
726 };
727 static int psaux_registered;
728 #endif
729
730 static int __init mousedev_init(void)
731 {
732         input_register_handler(&mousedev_handler);
733
734         memset(&mousedev_mix, 0, sizeof(struct mousedev));
735         INIT_LIST_HEAD(&mousedev_mix.list);
736         init_waitqueue_head(&mousedev_mix.wait);
737         mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
738         mousedev_mix.exist = 1;
739         mousedev_mix.minor = MOUSEDEV_MIX;
740
741         devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX),
742                         S_IFCHR|S_IRUGO|S_IWUSR, "input/mice");
743         class_device_create(input_class,
744                         MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), NULL, "mice");
745
746 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
747         if (!(psaux_registered = !misc_register(&psaux_mouse)))
748                 printk(KERN_WARNING "mice: could not misc_register the device\n");
749 #endif
750
751         printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
752
753         return 0;
754 }
755
756 static void __exit mousedev_exit(void)
757 {
758 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
759         if (psaux_registered)
760                 misc_deregister(&psaux_mouse);
761 #endif
762         devfs_remove("input/mice");
763         class_device_destroy(input_class,
764                         MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX));
765         input_unregister_handler(&mousedev_handler);
766 }
767
768 module_init(mousedev_init);
769 module_exit(mousedev_exit);