2 * Input driver to ExplorerPS/2 device driver module.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2004 Dmitry Torokhov
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.
12 #define MOUSEDEV_MINOR_BASE 32
13 #define MOUSEDEV_MINORS 32
14 #define MOUSEDEV_MIX 31
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>
32 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
33 MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
34 MODULE_LICENSE("GPL");
36 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
37 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
39 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
40 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
43 static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
44 module_param(xres, uint, 0);
45 MODULE_PARM_DESC(xres, "Horizontal screen resolution");
47 static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
48 module_param(yres, uint, 0);
49 MODULE_PARM_DESC(yres, "Vertical screen resolution");
51 struct mousedev_motion {
60 wait_queue_head_t wait;
61 struct list_head list;
62 struct input_handle handle;
64 struct mousedev_motion packet;
65 unsigned long buttons;
66 unsigned int pkt_count;
67 int old_x[4], old_y[4];
71 struct mousedev_list {
72 struct fasync_struct *fasync;
73 struct mousedev *mousedev;
74 struct list_head node;
76 unsigned long buttons;
78 unsigned char ready, buffer, bufsiz;
79 unsigned char mode, imexseq, impsseq;
82 #define MOUSEDEV_SEQ_LEN 6
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 };
87 static struct input_handler mousedev_handler;
89 static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
90 static struct mousedev mousedev_mix;
92 #define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
93 #define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
95 static void mousedev_touchpad_event(struct mousedev *mousedev, unsigned int code, int value)
97 if (mousedev->touch) {
101 if (mousedev->pkt_count >= 2)
102 mousedev->packet.dx = ((fx(0) - fx(1)) / 2 + (fx(1) - fx(2)) / 2) / 8;
107 if (mousedev->pkt_count >= 2)
108 mousedev->packet.dy = -((fy(0) - fy(1)) / 2 + (fy(1) - fy(2)) / 2) / 8;
114 static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
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;
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;
135 static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
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;
144 static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
152 case BTN_LEFT: index = 0; break;
155 case BTN_RIGHT: index = 1; break;
158 case BTN_MIDDLE: index = 2; break;
161 case BTN_SIDE: index = 3; break;
163 case BTN_EXTRA: index = 4; break;
168 set_bit(index, &mousedev->buttons);
169 set_bit(index, &mousedev_mix.buttons);
171 clear_bit(index, &mousedev->buttons);
172 clear_bit(index, &mousedev_mix.buttons);
176 static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_motion *packet)
178 struct mousedev_list *list;
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;
186 kill_fasync(&list->fasync, SIGIO, POLL_IN);
189 wake_up_interruptible(&mousedev->wait);
192 static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
194 struct mousedev *mousedev = handle->private;
198 /* Ignore joysticks */
199 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
202 if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
203 mousedev_touchpad_event(mousedev, code, value);
205 mousedev_abs_event(handle->dev, mousedev, code, value);
210 mousedev_rel_event(mousedev, code, value);
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;
222 mousedev_key_event(mousedev, code, value);
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
237 mousedev_notify_readers(mousedev, &mousedev->packet);
238 mousedev_notify_readers(&mousedev_mix, &mousedev->packet);
240 memset(&mousedev->packet, 0, sizeof(struct mousedev_motion));
246 static int mousedev_fasync(int fd, struct file *file, int on)
249 struct mousedev_list *list = file->private_data;
250 retval = fasync_helper(fd, file, on, &list->fasync);
251 return retval < 0 ? retval : 0;
254 static void mousedev_free(struct mousedev *mousedev)
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;
262 static int mixdev_release(void)
264 struct input_handle *handle;
266 list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
267 struct mousedev *mousedev = handle->private;
269 if (!mousedev->open) {
271 input_close_device(&mousedev->handle);
273 mousedev_free(mousedev);
280 static int mousedev_release(struct inode * inode, struct file * file)
282 struct mousedev_list *list = file->private_data;
284 mousedev_fasync(-1, file, 0);
286 list_del(&list->node);
288 if (!--list->mousedev->open) {
289 if (list->mousedev->minor == MOUSEDEV_MIX)
290 return mixdev_release();
292 if (!mousedev_mix.open) {
293 if (list->mousedev->exist)
294 input_close_device(&list->mousedev->handle);
296 mousedev_free(list->mousedev);
304 static int mousedev_open(struct inode * inode, struct file * file)
306 struct mousedev_list *list;
307 struct input_handle *handle;
308 struct mousedev *mousedev;
311 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
312 if (imajor(inode) == MISC_MAJOR)
316 i = iminor(inode) - MOUSEDEV_MINOR_BASE;
318 if (i >= MOUSEDEV_MINORS || !mousedev_table[i])
321 if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
323 memset(list, 0, sizeof(struct mousedev_list));
325 list->mousedev = mousedev_table[i];
326 list_add_tail(&list->node, &mousedev_table[i]->list);
327 file->private_data = list;
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);
337 if (!mousedev_mix.open && list->mousedev->exist)
338 input_open_device(&list->mousedev->handle);
344 static void mousedev_packet(struct mousedev_list *list, unsigned char off)
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;
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);
359 list->ps2[off] |= ((list->buttons & 0x10) >> 3) | ((list->buttons & 0x08) >> 1);
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];
368 if (!list->dx && !list->dy && (!list->mode || !list->dz)) list->ready = 0;
369 list->buffer = list->bufsiz;
373 static ssize_t mousedev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
375 struct mousedev_list *list = file->private_data;
379 for (i = 0; i < count; i++) {
381 if (get_user(c, buffer + i))
384 if (c == mousedev_imex_seq[list->imexseq]) {
385 if (++list->imexseq == MOUSEDEV_SEQ_LEN) {
389 } else list->imexseq = 0;
391 if (c == mousedev_imps_seq[list->impsseq]) {
392 if (++list->impsseq == MOUSEDEV_SEQ_LEN) {
396 } else list->impsseq = 0;
403 case 0xeb: /* Poll */
404 mousedev_packet(list, 1);
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;
416 case 0xe9: /* Get info */
417 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
421 case 0xff: /* Reset */
431 list->buffer = list->bufsiz;
434 kill_fasync(&list->fasync, SIGIO, POLL_IN);
436 wake_up_interruptible(&list->mousedev->wait);
441 static ssize_t mousedev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
443 struct mousedev_list *list = file->private_data;
446 if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK))
449 retval = wait_event_interruptible(list->mousedev->wait, list->ready || list->buffer);
454 if (!list->buffer && list->ready)
455 mousedev_packet(list, 0);
457 if (count > list->buffer)
458 count = list->buffer;
460 list->buffer -= count;
462 if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count))
468 /* No kernel lock - fine */
469 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
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;
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,
488 static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
490 struct mousedev *mousedev;
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");
499 if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
501 memset(mousedev, 0, sizeof(struct mousedev));
503 INIT_LIST_HEAD(&mousedev->list);
504 init_waitqueue_head(&mousedev->wait);
506 mousedev->minor = minor;
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);
514 if (mousedev_mix.open)
515 input_open_device(&mousedev->handle);
517 mousedev_table[minor] = mousedev;
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);
525 return &mousedev->handle;
528 static void mousedev_disconnect(struct input_handle *handle)
530 struct mousedev *mousedev = handle->private;
534 if (mousedev->open) {
535 input_close_device(handle);
537 if (mousedev_mix.open)
538 input_close_device(handle);
539 mousedev_free(mousedev);
543 static struct input_device_id mousedev_ids[] = {
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 */
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 */
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 */
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) },
568 { }, /* Terminating entry */
571 MODULE_DEVICE_TABLE(input, mousedev_ids);
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,
580 .id_table = mousedev_ids,
583 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
584 static struct miscdevice psaux_mouse = {
585 PSMOUSE_MINOR, "psaux", &mousedev_fops
587 static int psaux_registered;
590 static int __init mousedev_init(void)
592 input_register_handler(&mousedev_handler);
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;
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),
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");
611 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
616 static void __exit mousedev_exit(void)
618 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
619 if (psaux_registered)
620 misc_deregister(&psaux_mouse);
622 devfs_remove("input/mice");
623 class_simple_device_remove(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX));
624 input_unregister_handler(&mousedev_handler);
627 module_init(mousedev_init);
628 module_exit(mousedev_exit);