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.
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
46 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
47 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
54 wait_queue_head_t wait
;
55 struct mousedev_list
*list
;
56 struct input_handle handle
;
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
;
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
;
90 list
= (*mousedev
)->list
;
94 if (test_bit(BTN_TRIGGER
, handle
->dev
->keybit
))
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
;
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
;
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;
122 case BTN_LEFT
: index
= 0; break;
124 case BTN_EXTRA
: if (list
->mode
> 1) { index
= 4; break; }
127 case BTN_RIGHT
: index
= 1; break;
129 case BTN_SIDE
: if (list
->mode
> 1) { index
= 3; break; }
132 case BTN_MIDDLE
: index
= 2; break;
136 case 0: clear_bit(index
, &list
->buttons
); break;
137 case 1: set_bit(index
, &list
->buttons
); break;
145 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
150 wake_up_interruptible(&((*mousedev
)->wait
));
155 static int mousedev_fasync(int fd
, struct file
*file
, int on
)
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
;
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
;
180 struct mousedev
*mousedev
= handle
->private;
181 if (!mousedev
->open
) {
182 if (mousedev
->exist
) {
183 input_close_device(&mousedev
->handle
);
185 input_unregister_minor(mousedev
->devfs
);
186 mousedev_table
[mousedev
->minor
] = NULL
;
190 handle
= handle
->hnext
;
193 if (!mousedev_mix
.open
) {
194 if (list
->mousedev
->exist
) {
195 input_close_device(&list
->mousedev
->handle
);
197 input_unregister_minor(list
->mousedev
->devfs
);
198 mousedev_table
[list
->mousedev
->minor
] = NULL
;
199 kfree(list
->mousedev
);
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
])
219 if (!(list
= kmalloc(sizeof(struct mousedev_list
), GFP_KERNEL
)))
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
;
232 struct mousedev
*mousedev
= handle
->private;
235 input_open_device(handle
);
236 handle
= handle
->hnext
;
239 if (!mousedev_mix
.open
)
240 if (list
->mousedev
->exist
)
241 input_open_device(&list
->mousedev
->handle
);
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;
258 list
->ps2
[off
] |= ((list
->buttons
& 0x18) << 3);
261 list
->ps2
[off
+ 3] = (list
->dz
> 127 ? 127 : (list
->dz
< -127 ? -127 : list
->dz
));
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
;
276 for (i
= 0; i
< count
; i
++) {
280 if (c
== mousedev_genius_seq
[list
->genseq
]) {
281 if (++list
->genseq
== MOUSEDEV_GENIUS_LEN
) {
286 } else list
->genseq
= 0;
288 if (c
== mousedev_imps_seq
[list
->impseq
]) {
289 if (++list
->impseq
== MOUSEDEV_IMPS_LEN
) {
294 } else list
->impseq
= 0;
301 case 0xeb: /* Poll */
302 mousedev_packet(list
, 1);
305 case 0xf2: /* Get ID */
306 list
->ps2
[1] = (list
->mode
== 1) ? 3 : 0;
310 case 0xe9: /* Get info */
311 if (list
->mode
== 2) {
312 list
->ps2
[1] = 0x00; list
->ps2
[2] = 0x33; list
->ps2
[3] = 0x55;
314 list
->ps2
[1] = 0x60; list
->ps2
[2] = 3; list
->ps2
[3] = 200;
320 list
->buffer
= list
->bufsiz
;
323 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
325 wake_up_interruptible(&list
->mousedev
->wait
);
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
;
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
) {
347 if (signal_pending(current
)) {
348 retval
= -ERESTARTSYS
;
355 current
->state
= TASK_RUNNING
;
356 remove_wait_queue(&list
->mousedev
->wait
, &wait
);
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
))
371 list
->buffer
-= 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
;
386 struct file_operations mousedev_fops
= {
389 write
: mousedev_write
,
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
;
401 if (!test_bit(EV_KEY
, dev
->evbit
) ||
402 (!test_bit(BTN_LEFT
, dev
->keybit
) && !test_bit(BTN_TOUCH
, dev
->keybit
)))
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
)))
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");
415 if (!(mousedev
= kmalloc(sizeof(struct mousedev
), GFP_KERNEL
)))
417 memset(mousedev
, 0, sizeof(struct mousedev
));
418 init_waitqueue_head(&mousedev
->wait
);
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;
444 if (mousedev
->open
) {
445 input_close_device(handle
);
447 if (mousedev_mix
.open
)
448 input_close_device(handle
);
449 input_unregister_minor(mousedev
->devfs
);
450 mousedev_table
[mousedev
->minor
] = NULL
;
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");
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");