Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / drivers / input / mousedev.c
blob39c9ebaec4207c04bc076e79afbf254507f937e2
1 /*
2 * Input driver to ExplorerPS/2 device driver module.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
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 */
11 #define MOUSEDEV_MINOR_BASE 32
12 #define MOUSEDEV_MINORS 32
13 #define MOUSEDEV_MIX 31
15 #include <linux/slab.h>
16 #include <linux/poll.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
20 #include <linux/config.h>
21 #include <linux/smp_lock.h>
22 #include <linux/random.h>
23 #include <linux/major.h>
24 #include <linux/device.h>
25 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
26 #include <linux/miscdevice.h>
27 #endif
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30 MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
31 MODULE_LICENSE("GPL");
33 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
34 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
35 #endif
36 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
37 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
38 #endif
40 struct mousedev {
41 int exist;
42 int open;
43 int minor;
44 int misc;
45 char name[16];
46 wait_queue_head_t wait;
47 struct list_head list;
48 struct input_handle handle;
49 devfs_handle_t devfs;
52 struct mousedev_list {
53 struct fasync_struct *fasync;
54 struct mousedev *mousedev;
55 struct list_head node;
56 int dx, dy, dz, oldx, oldy;
57 signed char ps2[6];
58 unsigned long buttons;
59 unsigned char ready, buffer, bufsiz;
60 unsigned char mode, imexseq, impsseq;
63 #define MOUSEDEV_SEQ_LEN 6
65 static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
66 static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
68 static struct input_handler mousedev_handler;
70 static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
71 static struct mousedev mousedev_mix;
73 static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
74 static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
76 static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
78 struct mousedev *mousedevs[3] = { handle->private, &mousedev_mix, NULL };
79 struct mousedev **mousedev = mousedevs;
80 struct mousedev_list *list;
81 int index, size, wake;
83 while (*mousedev) {
85 wake = 0;
87 list_for_each_entry(list, &(*mousedev)->list, node)
88 switch (type) {
89 case EV_ABS:
90 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
91 break;
92 switch (code) {
93 case ABS_X:
94 size = handle->dev->absmax[ABS_X] - handle->dev->absmin[ABS_X];
95 if (size != 0) {
96 list->dx += (value * xres - list->oldx) / size;
97 list->oldx += list->dx * size;
98 } else {
99 list->dx += value - list->oldx;
100 list->oldx += list->dx;
102 break;
104 case ABS_Y:
105 size = handle->dev->absmax[ABS_Y] - handle->dev->absmin[ABS_Y];
106 if (size != 0) {
107 list->dy -= (value * yres - list->oldy) / size;
108 list->oldy -= list->dy * size;
109 } else {
110 list->dy -= value - list->oldy;
111 list->oldy -= list->dy;
113 break;
115 break;
117 case EV_REL:
118 switch (code) {
119 case REL_X: list->dx += value; break;
120 case REL_Y: list->dy -= value; break;
121 case REL_WHEEL: if (list->mode) list->dz -= value; break;
123 break;
125 case EV_KEY:
126 switch (code) {
127 case BTN_0:
128 case BTN_TOUCH:
129 case BTN_LEFT: index = 0; break;
130 case BTN_4:
131 case BTN_EXTRA: if (list->mode == 2) { index = 4; break; }
132 case BTN_STYLUS:
133 case BTN_1:
134 case BTN_RIGHT: index = 1; break;
135 case BTN_3:
136 case BTN_SIDE: if (list->mode == 2) { index = 3; break; }
137 case BTN_2:
138 case BTN_STYLUS2:
139 case BTN_MIDDLE: index = 2; break;
140 default: return;
142 switch (value) {
143 case 0: clear_bit(index, &list->buttons); break;
144 case 1: set_bit(index, &list->buttons); break;
145 case 2: return;
147 break;
149 case EV_SYN:
150 switch (code) {
151 case SYN_REPORT:
152 list->ready = 1;
153 kill_fasync(&list->fasync, SIGIO, POLL_IN);
154 wake = 1;
155 break;
159 if (wake)
160 wake_up_interruptible(&((*mousedev)->wait));
162 mousedev++;
166 static int mousedev_fasync(int fd, struct file *file, int on)
168 int retval;
169 struct mousedev_list *list = file->private_data;
170 retval = fasync_helper(fd, file, on, &list->fasync);
171 return retval < 0 ? retval : 0;
174 static int mousedev_release(struct inode * inode, struct file * file)
176 struct mousedev_list *list = file->private_data;
177 struct input_handle *handle;
178 struct mousedev *mousedev;
180 mousedev_fasync(-1, file, 0);
182 list_del(&list->node);
184 if (!--list->mousedev->open) {
185 if (list->mousedev->minor == MOUSEDEV_MIX) {
186 list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
187 mousedev = handle->private;
188 if (!mousedev->open) {
189 if (mousedev->exist) {
190 input_close_device(&mousedev->handle);
191 } else {
192 input_unregister_minor(mousedev->devfs);
193 mousedev_table[mousedev->minor] = NULL;
194 kfree(mousedev);
198 } else {
199 if (!mousedev_mix.open) {
200 if (list->mousedev->exist) {
201 input_close_device(&list->mousedev->handle);
202 } else {
203 input_unregister_minor(list->mousedev->devfs);
204 mousedev_table[list->mousedev->minor] = NULL;
205 kfree(list->mousedev);
211 kfree(list);
213 return 0;
216 static int mousedev_open(struct inode * inode, struct file * file)
218 struct mousedev_list *list;
219 struct input_handle *handle;
220 struct mousedev *mousedev;
221 int i;
223 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
224 if (major(inode->i_rdev) == MISC_MAJOR)
225 i = MOUSEDEV_MIX;
226 else
227 #endif
228 i = minor(inode->i_rdev) - MOUSEDEV_MINOR_BASE;
230 if (i >= MOUSEDEV_MINORS || !mousedev_table[i])
231 return -ENODEV;
233 if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
234 return -ENOMEM;
235 memset(list, 0, sizeof(struct mousedev_list));
237 list->mousedev = mousedev_table[i];
238 list_add_tail(&list->node, &mousedev_table[i]->list);
239 file->private_data = list;
241 if (!list->mousedev->open++) {
242 if (list->mousedev->minor == MOUSEDEV_MIX) {
243 list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
244 mousedev = handle->private;
245 if (!mousedev->open && mousedev->exist)
246 input_open_device(handle);
248 } else
249 if (!mousedev_mix.open && list->mousedev->exist)
250 input_open_device(&list->mousedev->handle);
253 return 0;
256 static void mousedev_packet(struct mousedev_list *list, unsigned char off)
258 list->ps2[off] = 0x08 | ((list->dx < 0) << 4) | ((list->dy < 0) << 5) | (list->buttons & 0x07);
259 list->ps2[off + 1] = (list->dx > 127 ? 127 : (list->dx < -127 ? -127 : list->dx));
260 list->ps2[off + 2] = (list->dy > 127 ? 127 : (list->dy < -127 ? -127 : list->dy));
261 list->dx -= list->ps2[off + 1];
262 list->dy -= list->ps2[off + 2];
263 list->bufsiz = off + 3;
265 if (list->mode == 2) {
266 list->ps2[off + 3] = (list->dz > 7 ? 7 : (list->dz < -7 ? -7 : list->dz));
267 list->dz -= list->ps2[off + 3];
268 list->ps2[off + 3] = (list->ps2[off + 3] & 0x0f) | ((list->buttons & 0x18) << 1);
269 list->bufsiz++;
272 if (list->mode == 1) {
273 list->ps2[off + 3] = (list->dz > 127 ? 127 : (list->dz < -127 ? -127 : list->dz));
274 list->dz -= list->ps2[off + 3];
275 list->bufsiz++;
278 if (!list->dx && !list->dy && (!list->mode || !list->dz)) list->ready = 0;
279 list->buffer = list->bufsiz;
283 static ssize_t mousedev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
285 struct mousedev_list *list = file->private_data;
286 unsigned char c;
287 int i;
289 for (i = 0; i < count; i++) {
291 if (get_user(c, buffer + i))
292 return -EFAULT;
294 if (c == mousedev_imex_seq[list->imexseq]) {
295 if (++list->imexseq == MOUSEDEV_SEQ_LEN) {
296 list->imexseq = 0;
297 list->mode = 2;
299 } else list->imexseq = 0;
301 if (c == mousedev_imps_seq[list->impsseq]) {
302 if (++list->impsseq == MOUSEDEV_SEQ_LEN) {
303 list->impsseq = 0;
304 list->mode = 1;
306 } else list->impsseq = 0;
308 list->ps2[0] = 0xfa;
309 list->bufsiz = 1;
311 switch (c) {
313 case 0xeb: /* Poll */
314 mousedev_packet(list, 1);
315 break;
317 case 0xf2: /* Get ID */
318 switch (list->mode) {
319 case 0: list->ps2[1] = 0; break;
320 case 1: list->ps2[1] = 3; break;
321 case 2: list->ps2[1] = 4; break;
323 list->bufsiz = 2;
324 break;
326 case 0xe9: /* Get info */
327 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
328 list->bufsiz = 4;
329 break;
331 case 0xff: /* Reset */
332 list->impsseq = 0;
333 list->imexseq = 0;
334 list->mode = 0;
335 list->ps2[0] = 0xaa;
336 list->ps2[1] = 0x00;
337 list->bufsiz = 2;
338 break;
341 list->buffer = list->bufsiz;
344 kill_fasync(&list->fasync, SIGIO, POLL_IN);
346 wake_up_interruptible(&list->mousedev->wait);
348 return count;
351 static ssize_t mousedev_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
353 struct mousedev_list *list = file->private_data;
354 int retval = 0;
356 if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK))
357 return -EAGAIN;
359 retval = wait_event_interruptible(list->mousedev->wait, list->ready || list->buffer);
361 if (retval)
362 return retval;
364 if (!list->buffer && list->ready)
365 mousedev_packet(list, 0);
367 if (count > list->buffer)
368 count = list->buffer;
370 list->buffer -= count;
372 if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count))
373 return -EFAULT;
375 return count;
378 /* No kernel lock - fine */
379 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
381 struct mousedev_list *list = file->private_data;
382 poll_wait(file, &list->mousedev->wait, wait);
383 if (list->ready || list->buffer)
384 return POLLIN | POLLRDNORM;
385 return 0;
388 struct file_operations mousedev_fops = {
389 .owner = THIS_MODULE,
390 .read = mousedev_read,
391 .write = mousedev_write,
392 .poll = mousedev_poll,
393 .open = mousedev_open,
394 .release = mousedev_release,
395 .fasync = mousedev_fasync,
398 static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
400 struct mousedev *mousedev;
401 int minor = 0;
403 for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
404 if (minor == MOUSEDEV_MINORS) {
405 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
406 return NULL;
409 if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
410 return NULL;
411 memset(mousedev, 0, sizeof(struct mousedev));
413 INIT_LIST_HEAD(&mousedev->list);
414 init_waitqueue_head(&mousedev->wait);
416 mousedev->minor = minor;
417 mousedev->exist = 1;
418 mousedev->handle.dev = dev;
419 mousedev->handle.name = mousedev->name;
420 mousedev->handle.handler = handler;
421 mousedev->handle.private = mousedev;
422 sprintf(mousedev->name, "mouse%d", minor);
424 if (mousedev_mix.open)
425 input_open_device(&mousedev->handle);
427 mousedev_table[minor] = mousedev;
428 mousedev->devfs = input_register_minor("mouse%d", minor, MOUSEDEV_MINOR_BASE);
430 return &mousedev->handle;
433 static void mousedev_disconnect(struct input_handle *handle)
435 struct mousedev *mousedev = handle->private;
437 mousedev->exist = 0;
439 if (mousedev->open) {
440 input_close_device(handle);
441 } else {
442 if (mousedev_mix.open)
443 input_close_device(handle);
444 input_unregister_minor(mousedev->devfs);
445 mousedev_table[mousedev->minor] = NULL;
446 kfree(mousedev);
450 static struct input_device_id mousedev_ids[] = {
452 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
453 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
454 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
455 .relbit = { BIT(REL_X) | BIT(REL_Y) },
456 }, /* A mouse like device, at least one button, two relative axes */
459 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
460 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
461 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
462 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
463 }, /* A tablet like device, at least touch detection, two absolute axes */
465 { }, /* Terminating entry */
468 MODULE_DEVICE_TABLE(input, mousedev_ids);
470 static struct input_handler mousedev_handler = {
471 .event = mousedev_event,
472 .connect = mousedev_connect,
473 .disconnect = mousedev_disconnect,
474 .fops = &mousedev_fops,
475 .minor = MOUSEDEV_MINOR_BASE,
476 .name = "mousedev",
477 .id_table = mousedev_ids,
480 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
481 static struct miscdevice psaux_mouse = {
482 PSMOUSE_MINOR, "psaux", &mousedev_fops
484 #endif
486 static struct device_interface mousedev_intf = {
487 .name = "mouse",
488 .devclass = &input_devclass,
491 static int __init mousedev_init(void)
493 int retval;
495 if((retval = interface_register(&mousedev_intf)) < 0)
496 return retval;
498 input_register_handler(&mousedev_handler);
500 memset(&mousedev_mix, 0, sizeof(struct mousedev));
501 INIT_LIST_HEAD(&mousedev_mix.list);
502 init_waitqueue_head(&mousedev_mix.wait);
503 mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
504 mousedev_mix.exist = 1;
505 mousedev_mix.minor = MOUSEDEV_MIX;
506 mousedev_mix.devfs = input_register_minor("mice", MOUSEDEV_MIX, MOUSEDEV_MINOR_BASE);
508 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
509 if (!(mousedev_mix.misc = !misc_register(&psaux_mouse)))
510 printk(KERN_WARNING "mice: could not misc_register the device\n");
511 #endif
513 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
515 return 0;
518 static void __exit mousedev_exit(void)
520 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
521 if (mousedev_mix.misc)
522 misc_deregister(&psaux_mouse);
523 #endif
524 input_unregister_minor(mousedev_mix.devfs);
525 input_unregister_handler(&mousedev_handler);
526 interface_unregister(&mousedev_intf);
529 module_init(mousedev_init);
530 module_exit(mousedev_exit);
532 MODULE_PARM(xres, "i");
533 MODULE_PARM_DESC(xres, "Horizontal screen resolution");
534 MODULE_PARM(yres, "i");
535 MODULE_PARM_DESC(yres, "Vertical screen resolution");