Linux 2.4.0-test7-pre7
[davej-history.git] / drivers / input / mousedev.c
blob1aa85e2273999a9c1fcf35cd9858476b9a08a92c
1 /*
2 * $Id: mousedev.c,v 1.15 2000/08/14 21:05:26 vojtech Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
6 * Input driver to PS/2 or ImPS/2 device driver module.
8 * Sponsored by SuSE
9 */
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/malloc.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>
43 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
44 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
45 #endif
46 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
47 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
48 #endif
50 struct mousedev {
51 int exist;
52 int open;
53 int minor;
54 wait_queue_head_t wait;
55 struct mousedev_list *list;
56 struct input_handle handle;
57 devfs_handle_t devfs;
60 struct mousedev_list {
61 struct fasync_struct *fasync;
62 struct mousedev *mousedev;
63 struct mousedev_list *next;
64 int dx, dy, dz, oldx, oldy;
65 signed char ps2[6];
66 unsigned long buttons;
67 unsigned char ready, buffer, bufsiz;
68 unsigned char mode, genseq, impseq;
71 #define MOUSEDEV_GENIUS_LEN 5
72 #define MOUSEDEV_IMPS_LEN 6
74 static unsigned char mousedev_genius_seq[] = { 0xe8, 3, 0xe6, 0xe6, 0xe6 };
75 static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 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 void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
84 struct mousedev *mousedevs[3] = { handle->private, &mousedev_mix, NULL };
85 struct mousedev **mousedev = mousedevs;
86 struct mousedev_list *list;
87 int index, size;
89 while (*mousedev) {
90 list = (*mousedev)->list;
91 while (list) {
92 switch (type) {
93 case EV_ABS:
94 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
95 break;
96 switch (code) {
97 case ABS_X:
98 size = handle->dev->absmax[ABS_X] - handle->dev->absmin[ABS_X];
99 list->dx += (value * CONFIG_INPUT_MOUSEDEV_SCREEN_X - list->oldx) / size;
100 list->oldx += list->dx * size;
101 break;
102 case ABS_Y:
103 size = handle->dev->absmax[ABS_Y] - handle->dev->absmin[ABS_Y];
104 list->dy -= (value * CONFIG_INPUT_MOUSEDEV_SCREEN_Y - list->oldy) / size;
105 list->oldy -= list->dy * size;
106 break;
108 break;
110 case EV_REL:
111 switch (code) {
112 case REL_X: list->dx += value; break;
113 case REL_Y: list->dy -= value; break;
114 case REL_WHEEL: if (list->mode) list->dz -= value; break;
116 break;
118 case EV_KEY:
119 switch (code) {
120 case BTN_0:
121 case BTN_TOUCH:
122 case BTN_LEFT: index = 0; break;
123 case BTN_4:
124 case BTN_EXTRA: if (list->mode > 1) { index = 4; break; }
125 case BTN_STYLUS:
126 case BTN_1:
127 case BTN_RIGHT: index = 1; break;
128 case BTN_3:
129 case BTN_SIDE: if (list->mode > 1) { index = 3; break; }
130 case BTN_2:
131 case BTN_STYLUS2:
132 case BTN_MIDDLE: index = 2; break;
133 default: return;
135 switch (value) {
136 case 0: clear_bit(index, &list->buttons); break;
137 case 1: set_bit(index, &list->buttons); break;
138 case 2: return;
140 break;
143 list->ready = 1;
145 kill_fasync(&list->fasync, SIGIO, POLL_IN);
147 list = list->next;
150 wake_up_interruptible(&((*mousedev)->wait));
151 mousedev++;
155 static int mousedev_fasync(int fd, struct file *file, int on)
157 int retval;
158 struct mousedev_list *list = file->private_data;
159 retval = fasync_helper(fd, file, on, &list->fasync);
160 return retval < 0 ? retval : 0;
163 static int mousedev_release(struct inode * inode, struct file * file)
165 struct mousedev_list *list = file->private_data;
166 struct mousedev_list **listptr;
168 lock_kernel();
169 listptr = &list->mousedev->list;
170 mousedev_fasync(-1, file, 0);
172 while (*listptr && (*listptr != list))
173 listptr = &((*listptr)->next);
174 *listptr = (*listptr)->next;
176 if (!--list->mousedev->open) {
177 if (list->mousedev->minor == MOUSEDEV_MIX) {
178 struct input_handle *handle = mousedev_handler.handle;
179 while (handle) {
180 struct mousedev *mousedev = handle->private;
181 if (!mousedev->open) {
182 if (mousedev->exist) {
183 input_close_device(&mousedev->handle);
184 } else {
185 input_unregister_minor(mousedev->devfs);
186 mousedev_table[mousedev->minor] = NULL;
187 kfree(mousedev);
190 handle = handle->hnext;
192 } else {
193 if (!mousedev_mix.open) {
194 if (list->mousedev->exist) {
195 input_close_device(&list->mousedev->handle);
196 } else {
197 input_unregister_minor(list->mousedev->devfs);
198 mousedev_table[list->mousedev->minor] = NULL;
199 kfree(list->mousedev);
205 kfree(list);
206 unlock_kernel();
208 return 0;
211 static int mousedev_open(struct inode * inode, struct file * file)
213 struct mousedev_list *list;
214 int i = MINOR(inode->i_rdev) - MOUSEDEV_MINOR_BASE;
216 if (i > MOUSEDEV_MINORS || !mousedev_table[i])
217 return -ENODEV;
219 if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
220 return -ENOMEM;
221 memset(list, 0, sizeof(struct mousedev_list));
223 list->mousedev = mousedev_table[i];
224 list->next = mousedev_table[i]->list;
225 mousedev_table[i]->list = list;
226 file->private_data = list;
228 if (!list->mousedev->open++) {
229 if (list->mousedev->minor == MOUSEDEV_MIX) {
230 struct input_handle *handle = mousedev_handler.handle;
231 while (handle) {
232 struct mousedev *mousedev = handle->private;
233 if (!mousedev->open)
234 if (mousedev->exist)
235 input_open_device(handle);
236 handle = handle->hnext;
238 } else {
239 if (!mousedev_mix.open)
240 if (list->mousedev->exist)
241 input_open_device(&list->mousedev->handle);
245 return 0;
248 static void mousedev_packet(struct mousedev_list *list, unsigned char off)
250 list->ps2[off] = 0x08 | ((list->dx < 0) << 4) | ((list->dy < 0) << 5) | (list->buttons & 0x07);
251 list->ps2[off + 1] = (list->dx > 127 ? 127 : (list->dx < -127 ? -127 : list->dx));
252 list->ps2[off + 2] = (list->dy > 127 ? 127 : (list->dy < -127 ? -127 : list->dy));
253 list->dx -= list->ps2[off + 1];
254 list->dy -= list->ps2[off + 2];
255 list->bufsiz = off + 3;
257 if (list->mode > 1)
258 list->ps2[off] |= ((list->buttons & 0x18) << 3);
260 if (list->mode) {
261 list->ps2[off + 3] = (list->dz > 127 ? 127 : (list->dz < -127 ? -127 : list->dz));
262 list->bufsiz++;
263 list->dz -= list->ps2[off + 3];
265 if (!list->dx && !list->dy && (!list->mode || !list->dz)) list->ready = 0;
266 list->buffer = list->bufsiz;
270 static ssize_t mousedev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
272 struct mousedev_list *list = file->private_data;
273 unsigned char c;
274 int i;
276 for (i = 0; i < count; i++) {
278 c = buffer[i];
280 if (c == mousedev_genius_seq[list->genseq]) {
281 if (++list->genseq == MOUSEDEV_GENIUS_LEN) {
282 list->genseq = 0;
283 list->ready = 1;
284 list->mode = 2;
286 } else list->genseq = 0;
288 if (c == mousedev_imps_seq[list->impseq]) {
289 if (++list->impseq == MOUSEDEV_IMPS_LEN) {
290 list->impseq = 0;
291 list->ready = 1;
292 list->mode = 1;
294 } else list->impseq = 0;
296 list->ps2[0] = 0xfa;
297 list->bufsiz = 1;
299 switch (c) {
301 case 0xeb: /* Poll */
302 mousedev_packet(list, 1);
303 break;
305 case 0xf2: /* Get ID */
306 list->ps2[1] = (list->mode == 1) ? 3 : 0;
307 list->bufsiz = 2;
308 break;
310 case 0xe9: /* Get info */
311 if (list->mode == 2) {
312 list->ps2[1] = 0x00; list->ps2[2] = 0x33; list->ps2[3] = 0x55;
313 } else {
314 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
316 list->bufsiz = 4;
317 break;
320 list->buffer = list->bufsiz;
323 kill_fasync(&list->fasync, SIGIO, POLL_IN);
325 wake_up_interruptible(&list->mousedev->wait);
327 return count;
330 static ssize_t mousedev_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
332 DECLARE_WAITQUEUE(wait, current);
333 struct mousedev_list *list = file->private_data;
334 int retval = 0;
336 if (!list->ready && !list->buffer) {
338 add_wait_queue(&list->mousedev->wait, &wait);
339 current->state = TASK_INTERRUPTIBLE;
341 while (!list->ready) {
343 if (file->f_flags & O_NONBLOCK) {
344 retval = -EAGAIN;
345 break;
347 if (signal_pending(current)) {
348 retval = -ERESTARTSYS;
349 break;
352 schedule();
355 current->state = TASK_RUNNING;
356 remove_wait_queue(&list->mousedev->wait, &wait);
359 if (retval)
360 return retval;
362 if (!list->buffer)
363 mousedev_packet(list, 0);
365 if (count > list->buffer)
366 count = list->buffer;
368 if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer, count))
369 return -EFAULT;
371 list->buffer -= count;
373 return count;
376 /* No kernel lock - fine */
377 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
379 struct mousedev_list *list = file->private_data;
380 poll_wait(file, &list->mousedev->wait, wait);
381 if (list->ready || list->buffer)
382 return POLLIN | POLLRDNORM;
383 return 0;
386 struct file_operations mousedev_fops = {
387 owner: THIS_MODULE,
388 read: mousedev_read,
389 write: mousedev_write,
390 poll: mousedev_poll,
391 open: mousedev_open,
392 release: mousedev_release,
393 fasync: mousedev_fasync,
396 static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev)
398 struct mousedev *mousedev;
399 int minor = 0;
401 if (!test_bit(EV_KEY, dev->evbit) ||
402 (!test_bit(BTN_LEFT, dev->keybit) && !test_bit(BTN_TOUCH, dev->keybit)))
403 return NULL;
405 if ((!test_bit(EV_REL, dev->evbit) || !test_bit(REL_X, dev->relbit)) &&
406 (!test_bit(EV_ABS, dev->evbit) || !test_bit(ABS_X, dev->absbit)))
407 return NULL;
409 for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
410 if (mousedev_table[minor]) {
411 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
412 return NULL;
415 if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
416 return NULL;
417 memset(mousedev, 0, sizeof(struct mousedev));
418 init_waitqueue_head(&mousedev->wait);
420 mousedev->exist = 1;
421 mousedev->minor = minor;
422 mousedev_table[minor] = mousedev;
424 mousedev->handle.dev = dev;
425 mousedev->handle.handler = handler;
426 mousedev->handle.private = mousedev;
428 mousedev->devfs = input_register_minor("mouse%d", minor, MOUSEDEV_MINOR_BASE);
430 if (mousedev_mix.open)
431 input_open_device(&mousedev->handle);
433 printk(KERN_INFO "mouse%d: PS/2 mouse device for input%d\n", minor, dev->number);
435 return &mousedev->handle;
438 static void mousedev_disconnect(struct input_handle *handle)
440 struct mousedev *mousedev = handle->private;
442 mousedev->exist = 0;
444 if (mousedev->open) {
445 input_close_device(handle);
446 } else {
447 if (mousedev_mix.open)
448 input_close_device(handle);
449 input_unregister_minor(mousedev->devfs);
450 mousedev_table[mousedev->minor] = NULL;
451 kfree(mousedev);
455 static struct input_handler mousedev_handler = {
456 event: mousedev_event,
457 connect: mousedev_connect,
458 disconnect: mousedev_disconnect,
459 fops: &mousedev_fops,
460 minor: MOUSEDEV_MINOR_BASE,
463 static int __init mousedev_init(void)
465 input_register_handler(&mousedev_handler);
467 memset(&mousedev_mix, 0, sizeof(struct mousedev));
468 init_waitqueue_head(&mousedev_mix.wait);
469 mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
470 mousedev_mix.exist = 1;
471 mousedev_mix.minor = MOUSEDEV_MIX;
472 mousedev_mix.devfs = input_register_minor("mice", MOUSEDEV_MIX, MOUSEDEV_MINOR_BASE);
474 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
476 return 0;
479 static void __exit mousedev_exit(void)
481 input_unregister_minor(mousedev_mix.devfs);
482 input_unregister_handler(&mousedev_handler);
485 module_init(mousedev_init);
486 module_exit(mousedev_exit);
488 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
489 MODULE_DESCRIPTION("Input driver to PS/2 or ImPS/2 device driver");