Merge with 2.3.48.
[linux-2.6/linux-mips.git] / drivers / usb / mousedev.c
blob0984e48fd26ecfb0ef79a319018001eaf16e3210
1 /*
2 * mousedev.c Version 0.1
4 * Copyright (c) 1999 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
33 #include <linux/miscdevice.h>
34 #include <linux/malloc.h>
35 #include <linux/poll.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/input.h>
39 #include <linux/config.h>
41 #ifndef CONFIG_MOUSEDEV_SCREEN_X
42 #define CONFIG_MOUSEDEV_SCREEN_X 1024
43 #endif
44 #ifndef CONFIG_MOUSEDEV_SCREEN_Y
45 #define CONFIG_MOUSEDEV_SCREEN_Y 768
46 #endif
48 struct mousedev {
49 char name[32];
50 int used;
51 struct input_handle handle;
52 struct miscdevice misc;
53 wait_queue_head_t wait;
54 struct mousedev_list *list;
57 struct mousedev_list {
58 struct fasync_struct *fasync;
59 struct mousedev *mousedev;
60 struct mousedev_list *next;
61 int dx, dy, dz, oldx, oldy;
62 char ps2[6];
63 unsigned long buttons;
64 unsigned char ready, buffer, bufsiz;
65 unsigned char mode, genseq, impseq;
68 #define MOUSEDEV_GENIUS_LEN 5
69 #define MOUSEDEV_IMPS_LEN 6
71 static unsigned char mousedev_genius_seq[] = { 0xe8, 3, 0xe6, 0xe6, 0xe6 };
72 static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
74 #ifdef CONFIG_INPUT_MOUSEDEV_MIX
75 static struct mousedev mousedev_single;
76 #else
77 static unsigned long mousedev_miscbits = 0;
78 static struct mousedev *mousedev_base[BITS_PER_LONG];
79 #endif
81 static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
83 struct mousedev *mousedev = handle->private;
84 struct mousedev_list *list = mousedev->list;
85 int index, size;
87 while (list) {
88 switch (type) {
89 case EV_ABS:
90 if (test_bit(EV_REL, handle->dev->evbit) && test_bit(REL_X, handle->dev->relbit))
91 return;
92 switch (code) {
93 case ABS_X:
94 size = handle->dev->absmax[ABS_X] - handle->dev->absmin[ABS_X];
95 list->dx += (value * CONFIG_MOUSEDEV_SCREEN_X - list->oldx) / size;
96 list->oldx += list->dx * size;
97 break;
98 case ABS_Y:
99 size = handle->dev->absmax[ABS_Y] - handle->dev->absmin[ABS_Y];
100 list->dy += (value * CONFIG_MOUSEDEV_SCREEN_Y - list->oldy) / size;
101 list->oldy += list->dy * size;
102 break;
104 break;
105 case EV_REL:
106 switch (code) {
107 case REL_X: list->dx += value; break;
108 case REL_Y: list->dy -= value; break;
109 case REL_WHEEL: if (list->mode) list->dz -= value; break;
111 break;
113 case EV_KEY:
114 switch (code) {
115 case BTN_0:
116 case BTN_TOUCH:
117 case BTN_LEFT: index = 0; break;
118 case BTN_4:
119 case BTN_EXTRA: if (list->mode > 1) { index = 4; break; }
120 case BTN_STYLUS:
121 case BTN_1:
122 case BTN_RIGHT: index = 1; break;
123 case BTN_3:
124 case BTN_SIDE: if (list->mode > 1) { index = 3; break; }
125 case BTN_2:
126 case BTN_STYLUS2:
127 case BTN_MIDDLE: index = 2; break;
128 default: return;
130 switch (value) {
131 case 0: clear_bit(index, &list->buttons); break;
132 case 1: set_bit(index, &list->buttons); break;
133 case 2: return;
135 break;
138 list->ready = 1;
140 if (list->fasync)
141 kill_fasync(list->fasync, SIGIO, POLL_IN);
143 list = list->next;
146 wake_up_interruptible(&mousedev->wait);
149 static int mousedev_fasync(int fd, struct file *file, int on)
151 int retval;
152 struct mousedev_list *list = file->private_data;
153 retval = fasync_helper(fd, file, on, &list->fasync);
154 return retval < 0 ? retval : 0;
157 static int mousedev_release(struct inode * inode, struct file * file)
159 struct mousedev_list *list = file->private_data;
160 struct mousedev_list **listptr = &list->mousedev->list;
162 mousedev_fasync(-1, file, 0);
164 while (*listptr && (*listptr != list))
165 listptr = &((*listptr)->next);
166 *listptr = (*listptr)->next;
168 #ifndef CONFIG_INPUT_MOUSEDEV_MIX
169 if (!--list->mousedev->used) {
170 clear_bit(list->mousedev->misc.minor - MOUSEDEV_MINOR_BASE, &mousedev_miscbits);
171 misc_deregister(&list->mousedev->misc);
172 kfree(list->mousedev);
174 #endif
176 kfree(list);
178 MOD_DEC_USE_COUNT;
179 return 0;
182 static int mousedev_open(struct inode * inode, struct file * file)
184 struct mousedev_list *list;
186 #ifndef CONFIG_INPUT_MOUSEDEV_MIX
187 int i = MINOR(inode->i_rdev) - MOUSEDEV_MINOR_BASE;
188 if (i > BITS_PER_LONG || !test_bit(i, &mousedev_miscbits))
189 return -ENODEV;
190 #endif
192 if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
193 return -ENOMEM;
195 memset(list, 0, sizeof(struct mousedev_list));
198 #ifdef CONFIG_INPUT_MOUSEDEV_MIX
199 list->mousedev = &mousedev_single;
200 list->next = mousedev_single.list;
201 mousedev_single.list = list;
202 #else
203 list->mousedev = mousedev_base[i];
204 list->next = mousedev_base[i]->list;
205 mousedev_base[i]->list = list;
206 list->mousedev->used++;
207 #endif
209 file->private_data = list;
211 MOD_INC_USE_COUNT;
212 return 0;
215 static void mousedev_packet(struct mousedev_list *list, unsigned char off)
217 list->ps2[off] = 0x08 | ((list->dx < 0) << 4) | ((list->dy < 0) << 5) | (list->buttons & 0x07);
218 list->ps2[off + 1] = (list->dx > 127 ? 127 : (list->dx < -127 ? -127 : list->dx));
219 list->ps2[off + 2] = (list->dy > 127 ? 127 : (list->dy < -127 ? -127 : list->dy));
220 list->dx -= list->ps2[off + 1];
221 list->dy -= list->ps2[off + 2];
222 list->bufsiz = off + 3;
224 if (list->mode > 1)
225 list->ps2[off] |= ((list->buttons & 0x30) << 2);
227 if (list->mode) {
228 list->ps2[off + 3] = (list->dz > 127 ? 127 : (list->dz < -127 ? -127 : list->dz));
229 list->bufsiz++;
230 list->dz -= list->ps2[off + 3];
232 if (!list->dx && !list->dy && (!list->mode || !list->dz)) list->ready = 0;
233 list->buffer = list->bufsiz;
237 static ssize_t mousedev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
239 struct mousedev_list *list = file->private_data;
240 unsigned char c;
241 int i;
243 for (i = 0; i < count; i++) {
245 c = buffer[i];
247 if (c == mousedev_genius_seq[list->genseq]) {
248 if (++list->genseq == MOUSEDEV_GENIUS_LEN) {
249 list->genseq = 0;
250 list->ready = 1;
251 list->mode = 2;
253 } else list->genseq = 0;
255 if (c == mousedev_imps_seq[list->impseq]) {
256 if (++list->impseq == MOUSEDEV_IMPS_LEN) {
257 list->impseq = 0;
258 list->ready = 1;
259 list->mode = 1;
261 } else list->impseq = 0;
263 list->ps2[0] = 0xfa;
264 list->bufsiz = 1;
266 switch (c) {
268 case 0xeb: /* Poll */
269 mousedev_packet(list, 1);
270 break;
272 case 0xf2: /* Get ID */
273 list->ps2[1] = (list->mode == 1) ? 3 : 0;
274 list->bufsiz = 2;
275 break;
277 case 0xe9: /* Get info */
278 if (list->mode == 2) {
279 list->ps2[1] = 0x00; list->ps2[2] = 0x33; list->ps2[3] = 0x55;
280 } else {
281 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
283 list->bufsiz = 4;
284 break;
287 list->buffer = list->bufsiz;
290 if (list->fasync)
291 kill_fasync(list->fasync, SIGIO, POLL_IN);
293 wake_up_interruptible(&list->mousedev->wait);
295 return count;
298 static ssize_t mousedev_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
300 DECLARE_WAITQUEUE(wait, current);
301 struct mousedev_list *list = file->private_data;
302 int retval = 0;
304 if (!list->ready && !list->buffer) {
306 add_wait_queue(&list->mousedev->wait, &wait);
307 current->state = TASK_INTERRUPTIBLE;
309 while (!list->ready) {
311 if (file->f_flags & O_NONBLOCK) {
312 retval = -EAGAIN;
313 break;
315 if (signal_pending(current)) {
316 retval = -ERESTARTSYS;
317 break;
320 schedule();
323 current->state = TASK_RUNNING;
324 remove_wait_queue(&list->mousedev->wait, &wait);
327 if (retval)
328 return retval;
330 if (!list->buffer)
331 mousedev_packet(list, 0);
333 if (count > list->buffer)
334 count = list->buffer;
336 if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer, count))
337 return -EFAULT;
339 list->buffer -= count;
341 return count;
344 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
346 struct mousedev_list *list = file->private_data;
347 poll_wait(file, &list->mousedev->wait, wait);
348 if (list->ready || list->buffer)
349 return POLLIN | POLLRDNORM;
350 return 0;
353 struct file_operations mousedev_fops = {
354 read: mousedev_read,
355 write: mousedev_write,
356 poll: mousedev_poll,
357 open: mousedev_open,
358 release: mousedev_release,
359 fasync: mousedev_fasync,
362 static int mousedev_connect(struct input_handler *handler, struct input_dev *dev)
365 if (!test_bit(EV_KEY, dev->evbit) ||
366 (!test_bit(BTN_LEFT, dev->keybit) && !test_bit(BTN_TOUCH, dev->keybit)))
367 return -1;
369 if ((!test_bit(EV_REL, dev->evbit) || !test_bit(REL_X, dev->relbit)) &&
370 (!test_bit(EV_ABS, dev->evbit) || !test_bit(ABS_X, dev->absbit)))
371 return -1;
373 #ifdef CONFIG_INPUT_MOUSEDEV_MIX
375 struct input_handle *handle;
377 if (!(handle = kmalloc(sizeof(struct input_handle), GFP_KERNEL)))
378 return -1;
380 memset(handle, 0, sizeof(struct input_handle));
382 handle->dev = dev;
383 handle->handler = handler;
384 handle->private = &mousedev_single;
386 input_open_device(handle);
388 printk("mousedev.c: Adding mouse: input%d\n", dev->number);
390 #else
392 struct mousedev *mousedev;
394 if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
395 return -1;
397 memset(mousedev, 0, sizeof(struct mousedev));
399 mousedev->misc.minor = ffz(mousedev_miscbits);
400 set_bit(mousedev->misc.minor, &mousedev_miscbits);
401 mousedev_base[mousedev->misc.minor] = mousedev;
403 sprintf(mousedev->name, "mousedev%d", mousedev->misc.minor);
404 mousedev->misc.name = mousedev->name;
405 mousedev->misc.minor += MOUSEDEV_MINOR_BASE;
406 mousedev->misc.fops = &mousedev_fops;
408 mousedev->handle.dev = dev;
409 mousedev->handle.handler = handler;
410 mousedev->handle.private = mousedev;
412 init_waitqueue_head(&mousedev->wait);
414 mousedev->used = 1;
416 misc_register(&mousedev->misc);
417 input_open_device(&mousedev->handle);
419 printk("%s: PS/2 mouse device for input%d on misc%d\n",
420 mousedev->name, dev->number, mousedev->misc.minor);
422 #endif
424 return 0;
427 static void mousedev_disconnect(struct input_handle *handle)
429 #ifdef CONFIG_INPUT_MOUSEDEV_MIX
430 printk("mousedev.c: Removing mouse: input%d\n", handle->dev->number);
431 input_close_device(handle);
432 kfree(handle);
433 #else
434 struct mousedev *mousedev = handle->private;
435 input_close_device(handle);
436 if (!--mousedev->used) {
437 clear_bit(mousedev->misc.minor - MOUSEDEV_MINOR_BASE, &mousedev_miscbits);
438 misc_deregister(&mousedev->misc);
439 kfree(mousedev);
441 #endif
444 static struct input_handler mousedev_handler = {
445 event: mousedev_event,
446 connect: mousedev_connect,
447 disconnect: mousedev_disconnect,
450 static int __init mousedev_init(void)
452 input_register_handler(&mousedev_handler);
454 #ifdef CONFIG_INPUT_MOUSEDEV_MIX
455 memset(&mousedev_single, 0, sizeof(struct mousedev));
457 init_waitqueue_head(&mousedev_single.wait);
458 mousedev_single.misc.minor = MOUSEDEV_MINOR_BASE;
459 mousedev_single.misc.name = "mousedev";
460 mousedev_single.misc.fops = &mousedev_fops;
462 misc_register(&mousedev_single.misc);
464 printk("mousedev: PS/2 mouse device on misc%d\n", mousedev_single.misc.minor);
465 #endif
467 return 0;
470 static void __exit mousedev_exit(void)
472 #ifdef CONFIG_INPUT_MOUSEDEV_MIX
473 misc_deregister(&mousedev_single.misc);
474 #endif
475 input_unregister_handler(&mousedev_handler);
478 module_init(mousedev_init);
479 module_exit(mousedev_exit);