2 * mousedev.c Version 0.1
4 * Copyright (c) 1999 Vojtech Pavlik
6 * Input driver to PS/2 or ImPS/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
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
44 #ifndef CONFIG_MOUSEDEV_SCREEN_Y
45 #define CONFIG_MOUSEDEV_SCREEN_Y 768
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
;
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
;
77 static unsigned long mousedev_miscbits
= 0;
78 static struct mousedev
*mousedev_base
[BITS_PER_LONG
];
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
;
90 if (test_bit(EV_REL
, handle
->dev
->evbit
) && test_bit(REL_X
, handle
->dev
->relbit
))
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
;
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
;
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;
117 case BTN_LEFT
: index
= 0; break;
119 case BTN_EXTRA
: if (list
->mode
> 1) { index
= 4; break; }
122 case BTN_RIGHT
: index
= 1; break;
124 case BTN_SIDE
: if (list
->mode
> 1) { index
= 3; break; }
127 case BTN_MIDDLE
: index
= 2; break;
131 case 0: clear_bit(index
, &list
->buttons
); break;
132 case 1: set_bit(index
, &list
->buttons
); break;
141 kill_fasync(list
->fasync
, SIGIO
, POLL_IN
);
146 wake_up_interruptible(&mousedev
->wait
);
149 static int mousedev_fasync(int fd
, struct file
*file
, int on
)
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
);
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
))
192 if (!(list
= kmalloc(sizeof(struct mousedev_list
), GFP_KERNEL
)))
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
;
203 list
->mousedev
= mousedev_base
[i
];
204 list
->next
= mousedev_base
[i
]->list
;
205 mousedev_base
[i
]->list
= list
;
206 list
->mousedev
->used
++;
209 file
->private_data
= list
;
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;
225 list
->ps2
[off
] |= ((list
->buttons
& 0x30) << 2);
228 list
->ps2
[off
+ 3] = (list
->dz
> 127 ? 127 : (list
->dz
< -127 ? -127 : list
->dz
));
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
;
243 for (i
= 0; i
< count
; i
++) {
247 if (c
== mousedev_genius_seq
[list
->genseq
]) {
248 if (++list
->genseq
== MOUSEDEV_GENIUS_LEN
) {
253 } else list
->genseq
= 0;
255 if (c
== mousedev_imps_seq
[list
->impseq
]) {
256 if (++list
->impseq
== MOUSEDEV_IMPS_LEN
) {
261 } else list
->impseq
= 0;
268 case 0xeb: /* Poll */
269 mousedev_packet(list
, 1);
272 case 0xf2: /* Get ID */
273 list
->ps2
[1] = (list
->mode
== 1) ? 3 : 0;
277 case 0xe9: /* Get info */
278 if (list
->mode
== 2) {
279 list
->ps2
[1] = 0x00; list
->ps2
[2] = 0x33; list
->ps2
[3] = 0x55;
281 list
->ps2
[1] = 0x60; list
->ps2
[2] = 3; list
->ps2
[3] = 200;
287 list
->buffer
= list
->bufsiz
;
291 kill_fasync(list
->fasync
, SIGIO
, POLL_IN
);
293 wake_up_interruptible(&list
->mousedev
->wait
);
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
;
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
) {
315 if (signal_pending(current
)) {
316 retval
= -ERESTARTSYS
;
323 current
->state
= TASK_RUNNING
;
324 remove_wait_queue(&list
->mousedev
->wait
, &wait
);
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
))
339 list
->buffer
-= 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
;
353 struct file_operations mousedev_fops
= {
355 write
: mousedev_write
,
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
)))
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
)))
373 #ifdef CONFIG_INPUT_MOUSEDEV_MIX
375 struct input_handle
*handle
;
377 if (!(handle
= kmalloc(sizeof(struct input_handle
), GFP_KERNEL
)))
380 memset(handle
, 0, sizeof(struct input_handle
));
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
);
392 struct mousedev
*mousedev
;
394 if (!(mousedev
= kmalloc(sizeof(struct mousedev
), GFP_KERNEL
)))
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
);
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
);
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
);
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
);
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
);
470 static void __exit
mousedev_exit(void)
472 #ifdef CONFIG_INPUT_MOUSEDEV_MIX
473 misc_deregister(&mousedev_single
.misc
);
475 input_unregister_handler(&mousedev_handler
);
478 module_init(mousedev_init
);
479 module_exit(mousedev_exit
);