patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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 struct mousedev_motion {
52         int dx, dy, dz;
53 };
54
55 struct mousedev {
56         int exist;
57         int open;
58         int minor;
59         char name[16];
60         wait_queue_head_t wait;
61         struct list_head list;
62         struct input_handle handle;
63
64         struct mousedev_motion packet;
65         unsigned long buttons;
66         unsigned int pkt_count;
67         int old_x[4], old_y[4];
68         unsigned int touch;
69 };
70
71 struct mousedev_list {
72         struct fasync_struct *fasync;
73         struct mousedev *mousedev;
74         struct list_head node;
75         int dx, dy, dz;
76         unsigned long buttons;
77         signed char ps2[6];
78         unsigned char ready, buffer, bufsiz;
79         unsigned char mode, imexseq, impsseq;
80 };
81
82 #define MOUSEDEV_SEQ_LEN        6
83
84 static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
85 static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
86
87 static struct input_handler mousedev_handler;
88
89 static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
90 static struct mousedev mousedev_mix;
91
92 #define fx(i)  (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
93 #define fy(i)  (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
94
95 static void mousedev_touchpad_event(struct mousedev *mousedev, unsigned int code, int value)
96 {
97         if (mousedev->touch) {
98                 switch (code) {
99                         case ABS_X:
100                                 fx(0) = value;
101                                 if (mousedev->pkt_count >= 2)
102                                         mousedev->packet.dx = ((fx(0) - fx(1)) / 2 + (fx(1) - fx(2)) / 2) / 8;
103                                 break;
104
105                         case ABS_Y:
106                                 fy(0) = value;
107                                 if (mousedev->pkt_count >= 2)
108                                         mousedev->packet.dy = -((fy(0) - fy(1)) / 2 + (fy(1) - fy(2)) / 2) / 8;
109                                 break;
110                 }
111         }
112 }
113
114 static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
115 {
116         int size;
117
118         switch (code) {
119                 case ABS_X:
120                         size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
121                         if (size == 0) size = xres;
122                         mousedev->packet.dx = (value * xres - mousedev->old_x[0]) / size;
123                         mousedev->old_x[0] = mousedev->packet.dx * size;
124                         break;
125
126                 case ABS_Y:
127                         size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
128                         if (size == 0) size = yres;
129                         mousedev->packet.dy = (value * yres - mousedev->old_y[0]) / size;
130                         mousedev->old_y[0] = mousedev->packet.dy * size;
131                         break;
132         }
133 }
134
135 static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
136 {
137         switch (code) {
138                 case REL_X:     mousedev->packet.dx += value; break;
139                 case REL_Y:     mousedev->packet.dy -= value; break;
140                 case REL_WHEEL: mousedev->packet.dz -= value; break;
141         }
142 }
143
144 static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
145 {
146         int index;
147
148         switch (code) {
149                 case BTN_TOUCH:
150                 case BTN_0:
151                 case BTN_FORWARD:
152                 case BTN_LEFT:          index = 0; break;
153                 case BTN_STYLUS:
154                 case BTN_1:
155                 case BTN_RIGHT:         index = 1; break;
156                 case BTN_2:
157                 case BTN_STYLUS2:
158                 case BTN_MIDDLE:        index = 2; break;
159                 case BTN_3:
160                 case BTN_BACK:
161                 case BTN_SIDE:          index = 3; break;
162                 case BTN_4:
163                 case BTN_EXTRA:         index = 4; break;
164                 default:                return;
165         }
166
167         if (value) {
168                 set_bit(index, &mousedev->buttons);
169                 set_bit(index, &mousedev_mix.buttons);
170         } else {
171                 clear_bit(index, &mousedev->buttons);
172                 clear_bit(index, &mousedev_mix.buttons);
173         }
174 }
175
176 static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_motion *packet)
177 {
178         struct mousedev_list *list;
179
180         list_for_each_entry(list, &mousedev->list, node) {
181                 list->dx += packet->dx;
182                 list->dy += packet->dy;
183                 list->dz += packet->dz;
184                 list->buttons = mousedev->buttons;
185                 list->ready = 1;
186                 kill_fasync(&list->fasync, SIGIO, POLL_IN);
187         }
188
189         wake_up_interruptible(&mousedev->wait);
190 }
191
192 static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
193 {
194         struct mousedev *mousedev = handle->private;
195
196         switch (type) {
197                 case EV_ABS:
198                         /* Ignore joysticks */
199                         if (test_bit(BTN_TRIGGER, handle->dev->keybit))
200                                 return;
201
202                         if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
203                                 mousedev_touchpad_event(mousedev, code, value);
204                         else
205                                 mousedev_abs_event(handle->dev, mousedev, code, value);
206
207                         break;
208
209                 case EV_REL:
210                         mousedev_rel_event(mousedev, code, value);
211                         break;
212
213                 case EV_KEY:
214                         if (value != 2) {
215                                 if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit)) {
216                                         /* Handle touchpad data */
217                                         mousedev->touch = value;
218                                         if (!mousedev->touch)
219                                                 mousedev->pkt_count = 0;
220                                 }
221                                 else
222                                         mousedev_key_event(mousedev, code, value);
223                         }
224                         break;
225
226                 case EV_SYN:
227                         if (code == SYN_REPORT) {
228                                 if (mousedev->touch) {
229                                         mousedev->pkt_count++;
230                                         /* Input system eats duplicate events, but we need all of them
231                                          * to do correct averaging so apply present one forward
232                                          */
233                                         fx(0) = fx(1);
234                                         fy(0) = fy(1);
235                                 }
236
237                                 mousedev_notify_readers(mousedev, &mousedev->packet);
238                                 mousedev_notify_readers(&mousedev_mix, &mousedev->packet);
239
240                                 memset(&mousedev->packet, 0, sizeof(struct mousedev_motion));
241                         }
242                         break;
243         }
244 }
245
246 static int mousedev_fasync(int fd, struct file *file, int on)
247 {
248         int retval;
249         struct mousedev_list *list = file->private_data;
250         retval = fasync_helper(fd, file, on, &list->fasync);
251         return retval < 0 ? retval : 0;
252 }
253
254 static void mousedev_free(struct mousedev *mousedev)
255 {
256         devfs_remove("input/mouse%d", mousedev->minor);
257         class_simple_device_remove(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor));
258         mousedev_table[mousedev->minor] = NULL;
259         kfree(mousedev);
260 }
261
262 static int mixdev_release(void)
263 {
264         struct input_handle *handle;
265
266         list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
267                 struct mousedev *mousedev = handle->private;
268
269                 if (!mousedev->open) {
270                         if (mousedev->exist)
271                                 input_close_device(&mousedev->handle);
272                         else
273                                 mousedev_free(mousedev);
274                 }
275         }
276
277         return 0;
278 }
279
280 static int mousedev_release(struct inode * inode, struct file * file)
281 {
282         struct mousedev_list *list = file->private_data;
283
284         mousedev_fasync(-1, file, 0);
285
286         list_del(&list->node);
287
288         if (!--list->mousedev->open) {
289                 if (list->mousedev->minor == MOUSEDEV_MIX)
290                         return mixdev_release();
291
292                 if (!mousedev_mix.open) {
293                         if (list->mousedev->exist)
294                                 input_close_device(&list->mousedev->handle);
295                         else
296                                 mousedev_free(list->mousedev);
297                 }
298         }
299
300         kfree(list);
301         return 0;
302 }
303
304 static int mousedev_open(struct inode * inode, struct file * file)
305 {
306         struct mousedev_list *list;
307         struct input_handle *handle;
308         struct mousedev *mousedev;
309         int i;
310
311 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
312         if (imajor(inode) == MISC_MAJOR)
313                 i = MOUSEDEV_MIX;
314         else
315 #endif
316                 i = iminor(inode) - MOUSEDEV_MINOR_BASE;
317
318         if (i >= MOUSEDEV_MINORS || !mousedev_table[i])
319                 return -ENODEV;
320
321         if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
322                 return -ENOMEM;
323         memset(list, 0, sizeof(struct mousedev_list));
324
325         list->mousedev = mousedev_table[i];
326         list_add_tail(&list->node, &mousedev_table[i]->list);
327         file->private_data = list;
328
329         if (!list->mousedev->open++) {
330                 if (list->mousedev->minor == MOUSEDEV_MIX) {
331                         list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
332                                 mousedev = handle->private;
333                                 if (!mousedev->open && mousedev->exist)
334                                         input_open_device(handle);
335                         }
336                 } else
337                         if (!mousedev_mix.open && list->mousedev->exist)
338                                 input_open_device(&list->mousedev->handle);
339         }
340
341         return 0;
342 }
343
344 static void mousedev_packet(struct mousedev_list *list, unsigned char off)
345 {
346         list->ps2[off] = 0x08 | ((list->dx < 0) << 4) | ((list->dy < 0) << 5) | (list->buttons & 0x07);
347         list->ps2[off + 1] = (list->dx > 127 ? 127 : (list->dx < -127 ? -127 : list->dx));
348         list->ps2[off + 2] = (list->dy > 127 ? 127 : (list->dy < -127 ? -127 : list->dy));
349         list->dx -= list->ps2[off + 1];
350         list->dy -= list->ps2[off + 2];
351         list->bufsiz = off + 3;
352
353         if (list->mode == 2) {
354                 list->ps2[off + 3] = (list->dz > 7 ? 7 : (list->dz < -7 ? -7 : list->dz));
355                 list->dz -= list->ps2[off + 3];
356                 list->ps2[off + 3] = (list->ps2[off + 3] & 0x0f) | ((list->buttons & 0x18) << 1);
357                 list->bufsiz++;
358         } else {
359                 list->ps2[off] |= ((list->buttons & 0x10) >> 3) | ((list->buttons & 0x08) >> 1);
360         }
361
362         if (list->mode == 1) {
363                 list->ps2[off + 3] = (list->dz > 127 ? 127 : (list->dz < -127 ? -127 : list->dz));
364                 list->dz -= list->ps2[off + 3];
365                 list->bufsiz++;
366         }
367
368         if (!list->dx && !list->dy && (!list->mode || !list->dz)) list->ready = 0;
369         list->buffer = list->bufsiz;
370 }
371
372
373 static ssize_t mousedev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
374 {
375         struct mousedev_list *list = file->private_data;
376         unsigned char c;
377         unsigned int i;
378
379         for (i = 0; i < count; i++) {
380
381                 if (get_user(c, buffer + i))
382                         return -EFAULT;
383
384                 if (c == mousedev_imex_seq[list->imexseq]) {
385                         if (++list->imexseq == MOUSEDEV_SEQ_LEN) {
386                                 list->imexseq = 0;
387                                 list->mode = 2;
388                         }
389                 } else list->imexseq = 0;
390
391                 if (c == mousedev_imps_seq[list->impsseq]) {
392                         if (++list->impsseq == MOUSEDEV_SEQ_LEN) {
393                                 list->impsseq = 0;
394                                 list->mode = 1;
395                         }
396                 } else list->impsseq = 0;
397
398                 list->ps2[0] = 0xfa;
399                 list->bufsiz = 1;
400
401                 switch (c) {
402
403                         case 0xeb: /* Poll */
404                                 mousedev_packet(list, 1);
405                                 break;
406
407                         case 0xf2: /* Get ID */
408                                 switch (list->mode) {
409                                         case 0: list->ps2[1] = 0; break;
410                                         case 1: list->ps2[1] = 3; break;
411                                         case 2: list->ps2[1] = 4; break;
412                                 }
413                                 list->bufsiz = 2;
414                                 break;
415
416                         case 0xe9: /* Get info */
417                                 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
418                                 list->bufsiz = 4;
419                                 break;
420
421                         case 0xff: /* Reset */
422                                 list->impsseq = 0;
423                                 list->imexseq = 0;
424                                 list->mode = 0;
425                                 list->ps2[1] = 0xaa;
426                                 list->ps2[2] = 0x00;
427                                 list->bufsiz = 3;
428                                 break;
429                 }
430
431                 list->buffer = list->bufsiz;
432         }
433
434         kill_fasync(&list->fasync, SIGIO, POLL_IN);
435
436         wake_up_interruptible(&list->mousedev->wait);
437
438         return count;
439 }
440
441 static ssize_t mousedev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
442 {
443         struct mousedev_list *list = file->private_data;
444         int retval = 0;
445
446         if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK))
447                 return -EAGAIN;
448
449         retval = wait_event_interruptible(list->mousedev->wait, list->ready || list->buffer);
450
451         if (retval)
452                 return retval;
453
454         if (!list->buffer && list->ready)
455                 mousedev_packet(list, 0);
456
457         if (count > list->buffer)
458                 count = list->buffer;
459
460         list->buffer -= count;
461
462         if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count))
463                 return -EFAULT;
464
465         return count;
466 }
467
468 /* No kernel lock - fine */
469 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
470 {
471         struct mousedev_list *list = file->private_data;
472         poll_wait(file, &list->mousedev->wait, wait);
473         if (list->ready || list->buffer)
474                 return POLLIN | POLLRDNORM;
475         return 0;
476 }
477
478 struct file_operations mousedev_fops = {
479         .owner =        THIS_MODULE,
480         .read =         mousedev_read,
481         .write =        mousedev_write,
482         .poll =         mousedev_poll,
483         .open =         mousedev_open,
484         .release =      mousedev_release,
485         .fasync =       mousedev_fasync,
486 };
487
488 static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
489 {
490         struct mousedev *mousedev;
491         int minor = 0;
492
493         for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
494         if (minor == MOUSEDEV_MINORS) {
495                 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
496                 return NULL;
497         }
498
499         if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
500                 return NULL;
501         memset(mousedev, 0, sizeof(struct mousedev));
502
503         INIT_LIST_HEAD(&mousedev->list);
504         init_waitqueue_head(&mousedev->wait);
505
506         mousedev->minor = minor;
507         mousedev->exist = 1;
508         mousedev->handle.dev = dev;
509         mousedev->handle.name = mousedev->name;
510         mousedev->handle.handler = handler;
511         mousedev->handle.private = mousedev;
512         sprintf(mousedev->name, "mouse%d", minor);
513
514         if (mousedev_mix.open)
515                 input_open_device(&mousedev->handle);
516
517         mousedev_table[minor] = mousedev;
518
519         devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
520                         S_IFCHR|S_IRUGO|S_IWUSR, "input/mouse%d", minor);
521         class_simple_device_add(input_class,
522                                 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
523                                 dev->dev, "mouse%d", minor);
524
525         return &mousedev->handle;
526 }
527
528 static void mousedev_disconnect(struct input_handle *handle)
529 {
530         struct mousedev *mousedev = handle->private;
531
532         mousedev->exist = 0;
533
534         if (mousedev->open) {
535                 input_close_device(handle);
536         } else {
537                 if (mousedev_mix.open)
538                         input_close_device(handle);
539                 mousedev_free(mousedev);
540         }
541 }
542
543 static struct input_device_id mousedev_ids[] = {
544         {
545                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
546                 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
547                 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
548                 .relbit = { BIT(REL_X) | BIT(REL_Y) },
549         },      /* A mouse like device, at least one button, two relative axes */
550         {
551                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
552                 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
553                 .relbit = { BIT(REL_WHEEL) },
554         },      /* A separate scrollwheel */
555         {
556                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
557                 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
558                 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
559                 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
560         },      /* A tablet like device, at least touch detection, two absolute axes */
561         {
562                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
563                 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
564                 .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
565                 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
566         },      /* A touchpad */
567
568         { },    /* Terminating entry */
569 };
570
571 MODULE_DEVICE_TABLE(input, mousedev_ids);
572
573 static struct input_handler mousedev_handler = {
574         .event =        mousedev_event,
575         .connect =      mousedev_connect,
576         .disconnect =   mousedev_disconnect,
577         .fops =         &mousedev_fops,
578         .minor =        MOUSEDEV_MINOR_BASE,
579         .name =         "mousedev",
580         .id_table =     mousedev_ids,
581 };
582
583 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
584 static struct miscdevice psaux_mouse = {
585         PSMOUSE_MINOR, "psaux", &mousedev_fops
586 };
587 static int psaux_registered;
588 #endif
589
590 static int __init mousedev_init(void)
591 {
592         input_register_handler(&mousedev_handler);
593
594         memset(&mousedev_mix, 0, sizeof(struct mousedev));
595         INIT_LIST_HEAD(&mousedev_mix.list);
596         init_waitqueue_head(&mousedev_mix.wait);
597         mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
598         mousedev_mix.exist = 1;
599         mousedev_mix.minor = MOUSEDEV_MIX;
600
601         devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX),
602                         S_IFCHR|S_IRUGO|S_IWUSR, "input/mice");
603         class_simple_device_add(input_class, MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX),
604                                 NULL, "mice");
605
606 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
607         if (!(psaux_registered = !misc_register(&psaux_mouse)))
608                 printk(KERN_WARNING "mice: could not misc_register the device\n");
609 #endif
610
611         printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
612
613         return 0;
614 }
615
616 static void __exit mousedev_exit(void)
617 {
618 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
619         if (psaux_registered)
620                 misc_deregister(&psaux_mouse);
621 #endif
622         devfs_remove("input/mice");
623         class_simple_device_remove(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX));
624         input_unregister_handler(&mousedev_handler);
625 }
626
627 module_init(mousedev_init);
628 module_exit(mousedev_exit);