Linux-2.4.0-test2
[davej-history.git] / drivers / usb / mousedev.c
blob5c871a88d94b519a758a18ecae115e2b72ec68a3
1 /*
2 * $Id: mousedev.c,v 1.10 2000/06/23 09:23:00 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>
42 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
43 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
44 #endif
45 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
46 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
47 #endif
49 struct mousedev {
50 int exist;
51 int open;
52 int minor;
53 wait_queue_head_t wait;
54 struct mousedev_list *list;
55 struct input_handle handle;
56 devfs_handle_t devfs;
59 struct mousedev_list {
60 struct fasync_struct *fasync;
61 struct mousedev *mousedev;
62 struct mousedev_list *next;
63 int dx, dy, dz, oldx, oldy;
64 char ps2[6];
65 unsigned long buttons;
66 unsigned char ready, buffer, bufsiz;
67 unsigned char mode, genseq, impseq;
70 #define MOUSEDEV_GENIUS_LEN 5
71 #define MOUSEDEV_IMPS_LEN 6
73 static unsigned char mousedev_genius_seq[] = { 0xe8, 3, 0xe6, 0xe6, 0xe6 };
74 static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
76 static struct input_handler mousedev_handler;
78 static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
79 static struct mousedev mousedev_mix;
81 static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
83 struct mousedev *mousedevs[3] = { handle->private, &mousedev_mix, NULL };
84 struct mousedev **mousedev = mousedevs;
85 struct mousedev_list *list;
86 int index, size;
88 while (*mousedev) {
89 list = (*mousedev)->list;
90 while (list) {
91 switch (type) {
92 case EV_ABS:
93 switch (code) {
94 case ABS_X:
95 size = handle->dev->absmax[ABS_X] - handle->dev->absmin[ABS_X];
96 list->dx += (value * CONFIG_INPUT_MOUSEDEV_SCREEN_X - list->oldx) / size;
97 list->oldx += list->dx * size;
98 break;
99 case ABS_Y:
100 size = handle->dev->absmax[ABS_Y] - handle->dev->absmin[ABS_Y];
101 list->dy -= (value * CONFIG_INPUT_MOUSEDEV_SCREEN_Y - list->oldy) / size;
102 list->oldy -= list->dy * size;
103 break;
105 break;
106 case EV_REL:
107 switch (code) {
108 case REL_X: list->dx += value; break;
109 case REL_Y: list->dy -= value; break;
110 case REL_WHEEL: if (list->mode) list->dz -= value; break;
112 break;
114 case EV_KEY:
115 switch (code) {
116 case BTN_0:
117 case BTN_TOUCH:
118 case BTN_LEFT: index = 0; break;
119 case BTN_4:
120 case BTN_EXTRA: if (list->mode > 1) { index = 4; break; }
121 case BTN_STYLUS:
122 case BTN_1:
123 case BTN_RIGHT: index = 1; break;
124 case BTN_3:
125 case BTN_SIDE: if (list->mode > 1) { index = 3; break; }
126 case BTN_2:
127 case BTN_STYLUS2:
128 case BTN_MIDDLE: index = 2; break;
129 default: return;
131 switch (value) {
132 case 0: clear_bit(index, &list->buttons); break;
133 case 1: set_bit(index, &list->buttons); break;
134 case 2: return;
136 break;
139 list->ready = 1;
141 kill_fasync(&list->fasync, SIGIO, POLL_IN);
143 list = list->next;
146 wake_up_interruptible(&((*mousedev)->wait));
147 mousedev++;
151 static int mousedev_fasync(int fd, struct file *file, int on)
153 int retval;
154 struct mousedev_list *list = file->private_data;
155 retval = fasync_helper(fd, file, on, &list->fasync);
156 return retval < 0 ? retval : 0;
159 static int mousedev_release(struct inode * inode, struct file * file)
161 struct mousedev_list *list = file->private_data;
162 struct mousedev_list **listptr = &list->mousedev->list;
164 mousedev_fasync(-1, file, 0);
166 while (*listptr && (*listptr != list))
167 listptr = &((*listptr)->next);
168 *listptr = (*listptr)->next;
170 if (!--list->mousedev->open) {
171 if (list->mousedev->minor == MOUSEDEV_MIX) {
172 struct input_handle *handle = mousedev_handler.handle;
173 while (handle) {
174 struct mousedev *mousedev = handle->private;
175 if (!mousedev->open) {
176 if (mousedev->exist) {
177 input_close_device(&mousedev->handle);
178 } else {
179 input_unregister_minor(mousedev->devfs);
180 mousedev_table[mousedev->minor] = NULL;
181 kfree(mousedev);
184 handle = handle->hnext;
186 } else {
187 if (!mousedev_mix.open) {
188 if (list->mousedev->exist) {
189 input_close_device(&list->mousedev->handle);
190 } else {
191 input_unregister_minor(list->mousedev->devfs);
192 mousedev_table[list->mousedev->minor] = NULL;
193 kfree(list->mousedev);
199 kfree(list);
201 return 0;
204 static int mousedev_open(struct inode * inode, struct file * file)
206 struct mousedev_list *list;
207 int i = MINOR(inode->i_rdev) - MOUSEDEV_MINOR_BASE;
209 if (i > MOUSEDEV_MINORS || !mousedev_table[i])
210 return -ENODEV;
212 if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
213 return -ENOMEM;
214 memset(list, 0, sizeof(struct mousedev_list));
216 list->mousedev = mousedev_table[i];
217 list->next = mousedev_table[i]->list;
218 mousedev_table[i]->list = list;
219 file->private_data = list;
221 if (!list->mousedev->open++) {
222 if (list->mousedev->minor == MOUSEDEV_MIX) {
223 struct input_handle *handle = mousedev_handler.handle;
224 while (handle) {
225 struct mousedev *mousedev = handle->private;
226 if (!mousedev->open)
227 if (mousedev->exist)
228 input_open_device(handle);
229 handle = handle->hnext;
231 } else {
232 if (!mousedev_mix.open)
233 if (list->mousedev->exist)
234 input_open_device(&list->mousedev->handle);
238 return 0;
241 static void mousedev_packet(struct mousedev_list *list, unsigned char off)
243 list->ps2[off] = 0x08 | ((list->dx < 0) << 4) | ((list->dy < 0) << 5) | (list->buttons & 0x07);
244 list->ps2[off + 1] = (list->dx > 127 ? 127 : (list->dx < -127 ? -127 : list->dx));
245 list->ps2[off + 2] = (list->dy > 127 ? 127 : (list->dy < -127 ? -127 : list->dy));
246 list->dx -= list->ps2[off + 1];
247 list->dy -= list->ps2[off + 2];
248 list->bufsiz = off + 3;
250 if (list->mode > 1)
251 list->ps2[off] |= ((list->buttons & 0x30) << 2);
253 if (list->mode) {
254 list->ps2[off + 3] = (list->dz > 127 ? 127 : (list->dz < -127 ? -127 : list->dz));
255 list->bufsiz++;
256 list->dz -= list->ps2[off + 3];
258 if (!list->dx && !list->dy && (!list->mode || !list->dz)) list->ready = 0;
259 list->buffer = list->bufsiz;
263 static ssize_t mousedev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
265 struct mousedev_list *list = file->private_data;
266 unsigned char c;
267 int i;
269 for (i = 0; i < count; i++) {
271 c = buffer[i];
273 if (c == mousedev_genius_seq[list->genseq]) {
274 if (++list->genseq == MOUSEDEV_GENIUS_LEN) {
275 list->genseq = 0;
276 list->ready = 1;
277 list->mode = 2;
279 } else list->genseq = 0;
281 if (c == mousedev_imps_seq[list->impseq]) {
282 if (++list->impseq == MOUSEDEV_IMPS_LEN) {
283 list->impseq = 0;
284 list->ready = 1;
285 list->mode = 1;
287 } else list->impseq = 0;
289 list->ps2[0] = 0xfa;
290 list->bufsiz = 1;
292 switch (c) {
294 case 0xeb: /* Poll */
295 mousedev_packet(list, 1);
296 break;
298 case 0xf2: /* Get ID */
299 list->ps2[1] = (list->mode == 1) ? 3 : 0;
300 list->bufsiz = 2;
301 break;
303 case 0xe9: /* Get info */
304 if (list->mode == 2) {
305 list->ps2[1] = 0x00; list->ps2[2] = 0x33; list->ps2[3] = 0x55;
306 } else {
307 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
309 list->bufsiz = 4;
310 break;
313 list->buffer = list->bufsiz;
316 kill_fasync(&list->fasync, SIGIO, POLL_IN);
318 wake_up_interruptible(&list->mousedev->wait);
320 return count;
323 static ssize_t mousedev_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
325 DECLARE_WAITQUEUE(wait, current);
326 struct mousedev_list *list = file->private_data;
327 int retval = 0;
329 if (!list->ready && !list->buffer) {
331 add_wait_queue(&list->mousedev->wait, &wait);
332 current->state = TASK_INTERRUPTIBLE;
334 while (!list->ready) {
336 if (file->f_flags & O_NONBLOCK) {
337 retval = -EAGAIN;
338 break;
340 if (signal_pending(current)) {
341 retval = -ERESTARTSYS;
342 break;
345 schedule();
348 current->state = TASK_RUNNING;
349 remove_wait_queue(&list->mousedev->wait, &wait);
352 if (retval)
353 return retval;
355 if (!list->buffer)
356 mousedev_packet(list, 0);
358 if (count > list->buffer)
359 count = list->buffer;
361 if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer, count))
362 return -EFAULT;
364 list->buffer -= count;
366 return count;
369 /* No kernel lock - fine */
370 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
372 struct mousedev_list *list = file->private_data;
373 poll_wait(file, &list->mousedev->wait, wait);
374 if (list->ready || list->buffer)
375 return POLLIN | POLLRDNORM;
376 return 0;
379 struct file_operations mousedev_fops = {
380 owner: THIS_MODULE,
381 read: mousedev_read,
382 write: mousedev_write,
383 poll: mousedev_poll,
384 open: mousedev_open,
385 release: mousedev_release,
386 fasync: mousedev_fasync,
389 static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev)
391 struct mousedev *mousedev;
392 int minor = 0;
394 if (!test_bit(EV_KEY, dev->evbit) ||
395 (!test_bit(BTN_LEFT, dev->keybit) && !test_bit(BTN_TOUCH, dev->keybit)))
396 return NULL;
398 if ((!test_bit(EV_REL, dev->evbit) || !test_bit(REL_X, dev->relbit)) &&
399 (!test_bit(EV_ABS, dev->evbit) || !test_bit(ABS_X, dev->absbit)))
400 return NULL;
402 for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
403 if (mousedev_table[minor]) {
404 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
405 return NULL;
408 if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
409 return NULL;
410 memset(mousedev, 0, sizeof(struct mousedev));
411 init_waitqueue_head(&mousedev->wait);
413 mousedev->exist = 1;
414 mousedev->minor = minor;
415 mousedev_table[minor] = mousedev;
417 mousedev->handle.dev = dev;
418 mousedev->handle.handler = handler;
419 mousedev->handle.private = mousedev;
421 mousedev->devfs = input_register_minor("mouse%d", minor, MOUSEDEV_MINOR_BASE);
423 if (mousedev_mix.open)
424 input_open_device(&mousedev->handle);
426 printk(KERN_INFO "mouse%d: PS/2 mouse device for input%d\n", minor, dev->number);
428 return &mousedev->handle;
431 static void mousedev_disconnect(struct input_handle *handle)
433 struct mousedev *mousedev = handle->private;
435 mousedev->exist = 0;
437 if (mousedev->open) {
438 input_close_device(handle);
439 } else {
440 if (mousedev_mix.open)
441 input_close_device(handle);
442 input_unregister_minor(mousedev->devfs);
443 mousedev_table[mousedev->minor] = NULL;
444 kfree(mousedev);
448 static struct input_handler mousedev_handler = {
449 event: mousedev_event,
450 connect: mousedev_connect,
451 disconnect: mousedev_disconnect,
452 fops: &mousedev_fops,
453 minor: MOUSEDEV_MINOR_BASE,
456 static int __init mousedev_init(void)
458 input_register_handler(&mousedev_handler);
460 memset(&mousedev_mix, 0, sizeof(struct mousedev));
461 init_waitqueue_head(&mousedev_mix.wait);
462 mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
463 mousedev_mix.exist = 1;
464 mousedev_mix.minor = MOUSEDEV_MIX;
465 mousedev_mix.devfs = input_register_minor("mice", MOUSEDEV_MIX, MOUSEDEV_MINOR_BASE);
467 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
469 return 0;
472 static void __exit mousedev_exit(void)
474 input_unregister_minor(mousedev_mix.devfs);
475 input_unregister_handler(&mousedev_handler);
478 module_init(mousedev_init);
479 module_exit(mousedev_exit);