2 * $Id: mousedev.c,v 1.24 2000/11/15 10:57:45 vojtech Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
6 * Input driver to ImExPS/2 device driver module.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Should you need to contact me, the author, you can do so either by
27 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
28 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
31 #define MOUSEDEV_MINOR_BASE 32
32 #define MOUSEDEV_MINORS 32
33 #define MOUSEDEV_MIX 31
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/input.h>
40 #include <linux/config.h>
41 #include <linux/smp_lock.h>
42 #include <linux/random.h>
44 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
45 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
47 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
48 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
55 wait_queue_head_t wait;
56 struct mousedev_list *list;
57 struct input_handle handle;
61 struct mousedev_list {
62 struct fasync_struct *fasync;
63 struct mousedev *mousedev;
64 struct mousedev_list *next;
65 int dx, dy, dz, oldx, oldy;
67 unsigned long buttons;
68 unsigned char ready, buffer, bufsiz;
69 unsigned char mode, imexseq, impsseq;
72 #define MOUSEDEV_SEQ_LEN 6
74 static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
75 static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
77 static struct input_handler mousedev_handler;
79 static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
80 static struct mousedev mousedev_mix;
82 static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
83 static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
85 static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
87 struct mousedev *mousedevs[3] = { handle->private, &mousedev_mix, NULL };
88 struct mousedev **mousedev = mousedevs;
89 struct mousedev_list *list;
92 add_mouse_randomness((type << 4) ^ code ^ (code >> 4) ^ value);
95 list = (*mousedev)->list;
99 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
103 size = handle->dev->absmax[ABS_X] - handle->dev->absmin[ABS_X];
104 list->dx += (value * xres - list->oldx) / size;
105 list->oldx += list->dx * size;
108 size = handle->dev->absmax[ABS_Y] - handle->dev->absmin[ABS_Y];
109 list->dy -= (value * yres - list->oldy) / size;
110 list->oldy -= list->dy * size;
117 case REL_X: list->dx += value; break;
118 case REL_Y: list->dy -= value; break;
119 case REL_WHEEL: if (list->mode) list->dz -= value; break;
127 case BTN_LEFT: index = 0; break;
129 case BTN_EXTRA: if (list->mode == 2) { index = 4; break; }
132 case BTN_RIGHT: index = 1; break;
134 case BTN_SIDE: if (list->mode == 2) { index = 3; break; }
137 case BTN_MIDDLE: index = 2; break;
141 case 0: clear_bit(index, &list->buttons); break;
142 case 1: set_bit(index, &list->buttons); break;
150 kill_fasync(&list->fasync, SIGIO, POLL_IN);
155 wake_up_interruptible(&((*mousedev)->wait));
160 static int mousedev_fasync(int fd, struct file *file, int on)
163 struct mousedev_list *list = file->private_data;
164 retval = fasync_helper(fd, file, on, &list->fasync);
165 return retval < 0 ? retval : 0;
168 static int mousedev_release(struct inode * inode, struct file * file)
170 struct mousedev_list *list = file->private_data;
171 struct mousedev_list **listptr;
174 listptr = &list->mousedev->list;
175 mousedev_fasync(-1, file, 0);
177 while (*listptr && (*listptr != list))
178 listptr = &((*listptr)->next);
179 *listptr = (*listptr)->next;
181 if (!--list->mousedev->open) {
182 if (list->mousedev->minor == MOUSEDEV_MIX) {
183 struct input_handle *handle = mousedev_handler.handle;
185 struct mousedev *mousedev = handle->private;
186 if (!mousedev->open) {
187 if (mousedev->exist) {
188 input_close_device(&mousedev->handle);
190 input_unregister_minor(mousedev->devfs);
191 mousedev_table[mousedev->minor] = NULL;
195 handle = handle->hnext;
198 if (!mousedev_mix.open) {
199 if (list->mousedev->exist) {
200 input_close_device(&list->mousedev->handle);
202 input_unregister_minor(list->mousedev->devfs);
203 mousedev_table[list->mousedev->minor] = NULL;
204 kfree(list->mousedev);
216 static int mousedev_open(struct inode * inode, struct file * file)
218 struct mousedev_list *list;
219 int i = MINOR(inode->i_rdev) - MOUSEDEV_MINOR_BASE;
221 if (i >= MOUSEDEV_MINORS || !mousedev_table[i])
224 if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
226 memset(list, 0, sizeof(struct mousedev_list));
228 list->mousedev = mousedev_table[i];
229 list->next = mousedev_table[i]->list;
230 mousedev_table[i]->list = list;
231 file->private_data = list;
233 if (!list->mousedev->open++) {
234 if (list->mousedev->minor == MOUSEDEV_MIX) {
235 struct input_handle *handle = mousedev_handler.handle;
237 struct mousedev *mousedev = handle->private;
240 input_open_device(handle);
241 handle = handle->hnext;
244 if (!mousedev_mix.open)
245 if (list->mousedev->exist)
246 input_open_device(&list->mousedev->handle);
253 static void mousedev_packet(struct mousedev_list *list, unsigned char off)
255 list->ps2[off] = 0x08 | ((list->dx < 0) << 4) | ((list->dy < 0) << 5) | (list->buttons & 0x07);
256 list->ps2[off + 1] = (list->dx > 127 ? 127 : (list->dx < -127 ? -127 : list->dx));
257 list->ps2[off + 2] = (list->dy > 127 ? 127 : (list->dy < -127 ? -127 : list->dy));
258 list->dx -= list->ps2[off + 1];
259 list->dy -= list->ps2[off + 2];
260 list->bufsiz = off + 3;
262 if (list->mode == 2) {
263 list->ps2[off + 3] = (list->dz > 7 ? 7 : (list->dz < -7 ? -7 : list->dz));
264 list->dz -= list->ps2[off + 3];
265 list->ps2[off + 3] = (list->ps2[off + 3] & 0x0f) | ((list->buttons & 0x18) << 1);
269 if (list->mode == 1) {
270 list->ps2[off + 3] = (list->dz > 127 ? 127 : (list->dz < -127 ? -127 : list->dz));
271 list->dz -= list->ps2[off + 3];
275 if (!list->dx && !list->dy && (!list->mode || !list->dz)) list->ready = 0;
276 list->buffer = list->bufsiz;
280 static ssize_t mousedev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
282 struct mousedev_list *list = file->private_data;
286 for (i = 0; i < count; i++) {
288 if (get_user(c, buffer + i))
291 if (c == mousedev_imex_seq[list->imexseq]) {
292 if (++list->imexseq == MOUSEDEV_SEQ_LEN) {
296 } else list->imexseq = 0;
298 if (c == mousedev_imps_seq[list->impsseq]) {
299 if (++list->impsseq == MOUSEDEV_SEQ_LEN) {
303 } else list->impsseq = 0;
311 case 0xeb: /* Poll */
312 mousedev_packet(list, 1);
315 case 0xf2: /* Get ID */
316 switch (list->mode) {
317 case 0: list->ps2[1] = 0; break;
318 case 1: list->ps2[1] = 3; break;
319 case 2: list->ps2[1] = 4; break;
324 case 0xe9: /* Get info */
325 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
330 list->buffer = list->bufsiz;
333 kill_fasync(&list->fasync, SIGIO, POLL_IN);
335 wake_up_interruptible(&list->mousedev->wait);
340 static ssize_t mousedev_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
342 DECLARE_WAITQUEUE(wait, current);
343 struct mousedev_list *list = file->private_data;
346 if (!list->ready && !list->buffer) {
348 add_wait_queue(&list->mousedev->wait, &wait);
349 current->state = TASK_INTERRUPTIBLE;
351 while (!list->ready) {
353 if (file->f_flags & O_NONBLOCK) {
357 if (signal_pending(current)) {
358 retval = -ERESTARTSYS;
365 current->state = TASK_RUNNING;
366 remove_wait_queue(&list->mousedev->wait, &wait);
373 mousedev_packet(list, 0);
375 if (count > list->buffer)
376 count = list->buffer;
378 if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer, count))
381 list->buffer -= count;
386 /* No kernel lock - fine */
387 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
389 struct mousedev_list *list = file->private_data;
390 poll_wait(file, &list->mousedev->wait, wait);
391 if (list->ready || list->buffer)
392 return POLLIN | POLLRDNORM;
396 struct file_operations mousedev_fops = {
399 write: mousedev_write,
402 release: mousedev_release,
403 fasync: mousedev_fasync,
406 static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev)
408 struct mousedev *mousedev;
411 if (!test_bit(EV_KEY, dev->evbit) ||
412 (!test_bit(BTN_LEFT, dev->keybit) &&
413 !test_bit(BTN_MIDDLE, dev->keybit) &&
414 !test_bit(BTN_TOUCH, dev->keybit)))
417 if ((!test_bit(EV_REL, dev->evbit) || !test_bit(REL_X, dev->relbit)) &&
418 (!test_bit(EV_REL, dev->evbit) || !test_bit(REL_WHEEL, dev->relbit)) &&
419 (!test_bit(EV_ABS, dev->evbit) || !test_bit(ABS_X, dev->absbit)))
422 for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
423 if (minor == MOUSEDEV_MINORS) {
424 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
428 if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
430 memset(mousedev, 0, sizeof(struct mousedev));
431 init_waitqueue_head(&mousedev->wait);
434 mousedev->minor = minor;
435 mousedev_table[minor] = mousedev;
437 mousedev->handle.dev = dev;
438 mousedev->handle.handler = handler;
439 mousedev->handle.private = mousedev;
441 mousedev->devfs = input_register_minor("mouse%d", minor, MOUSEDEV_MINOR_BASE);
443 if (mousedev_mix.open)
444 input_open_device(&mousedev->handle);
446 // printk(KERN_INFO "mouse%d: PS/2 mouse device for input%d\n", minor, dev->number);
448 return &mousedev->handle;
451 static void mousedev_disconnect(struct input_handle *handle)
453 struct mousedev *mousedev = handle->private;
457 if (mousedev->open) {
458 input_close_device(handle);
460 if (mousedev_mix.open)
461 input_close_device(handle);
462 input_unregister_minor(mousedev->devfs);
463 mousedev_table[mousedev->minor] = NULL;
468 static struct input_handler mousedev_handler = {
469 event: mousedev_event,
470 connect: mousedev_connect,
471 disconnect: mousedev_disconnect,
472 fops: &mousedev_fops,
473 minor: MOUSEDEV_MINOR_BASE,
476 static int __init mousedev_init(void)
478 input_register_handler(&mousedev_handler);
480 memset(&mousedev_mix, 0, sizeof(struct mousedev));
481 init_waitqueue_head(&mousedev_mix.wait);
482 mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
483 mousedev_mix.exist = 1;
484 mousedev_mix.minor = MOUSEDEV_MIX;
485 mousedev_mix.devfs = input_register_minor("mice", MOUSEDEV_MIX, MOUSEDEV_MINOR_BASE);
487 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
492 static void __exit mousedev_exit(void)
494 input_unregister_minor(mousedev_mix.devfs);
495 input_unregister_handler(&mousedev_handler);
498 module_init(mousedev_init);
499 module_exit(mousedev_exit);
501 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
502 MODULE_DESCRIPTION("Input driver to PS/2 or ImPS/2 device driver");
503 MODULE_LICENSE("GPL");
505 MODULE_PARM(xres, "i");
506 MODULE_PARM_DESC(xres, "Horizontal screen resolution");
507 MODULE_PARM(yres, "i");
508 MODULE_PARM_DESC(yres, "Vertical screen resolution");