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