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.
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/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>
31 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
32 MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
33 MODULE_LICENSE("GPL");
35 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
36 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
38 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
39 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
42 static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
43 module_param(xres, uint, 0);
44 MODULE_PARM_DESC(xres, "Horizontal screen resolution");
46 static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
47 module_param(yres, uint, 0);
48 MODULE_PARM_DESC(yres, "Vertical screen resolution");
56 wait_queue_head_t wait;
57 struct list_head list;
58 struct input_handle handle;
61 struct mousedev_list {
62 struct fasync_struct *fasync;
63 struct mousedev *mousedev;
64 struct list_head node;
66 int old_x[4], old_y[4];
67 unsigned long buttons;
69 unsigned char ready, buffer, bufsiz;
70 unsigned char mode, imexseq, impsseq;
71 unsigned int pkt_count;
75 #define MOUSEDEV_SEQ_LEN 6
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 };
80 static struct input_handler mousedev_handler;
82 static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
83 static struct mousedev mousedev_mix;
85 #define fx(i) (list->old_x[(list->pkt_count - (i)) & 03])
86 #define fy(i) (list->old_y[(list->pkt_count - (i)) & 03])
88 static void mousedev_abs_event(struct input_handle *handle, struct mousedev_list *list, unsigned int code, int value)
93 /* Ignore joysticks */
94 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
97 touchpad = test_bit(BTN_TOOL_FINGER, handle->dev->keybit);
104 if (list->pkt_count >= 2)
105 list->dx = ((fx(0) - fx(1)) / 2 + (fx(1) - fx(2)) / 2) / 8;
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;
118 if (list->pkt_count >= 2)
119 list->dy = -((fy(0) - fy(1)) / 2 + (fy(1) - fy(2)) / 2) / 8;
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;
131 static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
133 struct mousedev *mousedevs[3] = { handle->private, &mousedev_mix, NULL };
134 struct mousedev **mousedev = mousedevs;
135 struct mousedev_list *list;
142 list_for_each_entry(list, &(*mousedev)->list, node)
145 mousedev_abs_event(handle, list, code, value);
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;
157 if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit)) {
158 /* Handle touchpad data */
169 case BTN_LEFT: index = 0; break;
171 case BTN_EXTRA: if (list->mode == 2) { index = 4; break; }
174 case BTN_RIGHT: index = 1; break;
177 case BTN_SIDE: if (list->mode == 2) { index = 3; break; }
180 case BTN_MIDDLE: index = 2; break;
184 case 0: clear_bit(index, &list->buttons); break;
185 case 1: set_bit(index, &list->buttons); break;
195 /* Input system eats duplicate events,
196 * but we need all of them to do correct
197 * averaging so apply present one forward
204 kill_fasync(&list->fasync, SIGIO, POLL_IN);
211 wake_up_interruptible(&((*mousedev)->wait));
217 static int mousedev_fasync(int fd, struct file *file, int on)
220 struct mousedev_list *list = file->private_data;
221 retval = fasync_helper(fd, file, on, &list->fasync);
222 return retval < 0 ? retval : 0;
225 static void mousedev_free(struct mousedev *mousedev)
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;
233 static int mixdev_release(void)
235 struct input_handle *handle;
237 list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
238 struct mousedev *mousedev = handle->private;
240 if (!mousedev->open) {
242 input_close_device(&mousedev->handle);
244 mousedev_free(mousedev);
251 static int mousedev_release(struct inode * inode, struct file * file)
253 struct mousedev_list *list = file->private_data;
255 mousedev_fasync(-1, file, 0);
257 list_del(&list->node);
259 if (!--list->mousedev->open) {
260 if (list->mousedev->minor == MOUSEDEV_MIX)
261 return mixdev_release();
263 if (!mousedev_mix.open) {
264 if (list->mousedev->exist)
265 input_close_device(&list->mousedev->handle);
267 mousedev_free(list->mousedev);
275 static int mousedev_open(struct inode * inode, struct file * file)
277 struct mousedev_list *list;
278 struct input_handle *handle;
279 struct mousedev *mousedev;
282 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
283 if (imajor(inode) == MISC_MAJOR)
287 i = iminor(inode) - MOUSEDEV_MINOR_BASE;
289 if (i >= MOUSEDEV_MINORS || !mousedev_table[i])
292 if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
294 memset(list, 0, sizeof(struct mousedev_list));
296 list->mousedev = mousedev_table[i];
297 list_add_tail(&list->node, &mousedev_table[i]->list);
298 file->private_data = list;
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);
308 if (!mousedev_mix.open && list->mousedev->exist)
309 input_open_device(&list->mousedev->handle);
315 static void mousedev_packet(struct mousedev_list *list, unsigned char off)
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;
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);
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];
337 if (!list->dx && !list->dy && (!list->mode || !list->dz)) list->ready = 0;
338 list->buffer = list->bufsiz;
342 static ssize_t mousedev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
344 struct mousedev_list *list = file->private_data;
348 for (i = 0; i < count; i++) {
350 if (get_user(c, buffer + i))
353 if (c == mousedev_imex_seq[list->imexseq]) {
354 if (++list->imexseq == MOUSEDEV_SEQ_LEN) {
358 } else list->imexseq = 0;
360 if (c == mousedev_imps_seq[list->impsseq]) {
361 if (++list->impsseq == MOUSEDEV_SEQ_LEN) {
365 } else list->impsseq = 0;
372 case 0xeb: /* Poll */
373 mousedev_packet(list, 1);
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;
385 case 0xe9: /* Get info */
386 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
390 case 0xff: /* Reset */
400 list->buffer = list->bufsiz;
403 kill_fasync(&list->fasync, SIGIO, POLL_IN);
405 wake_up_interruptible(&list->mousedev->wait);
410 static ssize_t mousedev_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
412 struct mousedev_list *list = file->private_data;
415 if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK))
418 retval = wait_event_interruptible(list->mousedev->wait, list->ready || list->buffer);
423 if (!list->buffer && list->ready)
424 mousedev_packet(list, 0);
426 if (count > list->buffer)
427 count = list->buffer;
429 list->buffer -= count;
431 if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count))
437 /* No kernel lock - fine */
438 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
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;
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,
457 static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
459 struct mousedev *mousedev;
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");
468 if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
470 memset(mousedev, 0, sizeof(struct mousedev));
472 INIT_LIST_HEAD(&mousedev->list);
473 init_waitqueue_head(&mousedev->wait);
475 mousedev->minor = minor;
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);
483 if (mousedev_mix.open)
484 input_open_device(&mousedev->handle);
486 mousedev_table[minor] = mousedev;
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);
494 return &mousedev->handle;
497 static void mousedev_disconnect(struct input_handle *handle)
499 struct mousedev *mousedev = handle->private;
503 if (mousedev->open) {
504 input_close_device(handle);
506 if (mousedev_mix.open)
507 input_close_device(handle);
508 mousedev_free(mousedev);
512 static struct input_device_id mousedev_ids[] = {
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 */
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 */
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 */
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) },
537 { }, /* Terminating entry */
540 MODULE_DEVICE_TABLE(input, mousedev_ids);
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,
549 .id_table = mousedev_ids,
552 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
553 static struct miscdevice psaux_mouse = {
554 PSMOUSE_MINOR, "psaux", &mousedev_fops
558 static int __init mousedev_init(void)
560 input_register_handler(&mousedev_handler);
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;
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),
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");
579 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
584 static void __exit mousedev_exit(void)
586 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
587 if (mousedev_mix.misc)
588 misc_deregister(&psaux_mouse);
590 devfs_remove("input/mice");
591 class_simple_device_remove(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX));
592 input_unregister_handler(&mousedev_handler);
595 module_init(mousedev_init);
596 module_exit(mousedev_exit);