2 * Input driver to ExplorerPS/2 device driver module.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
11 #define MOUSEDEV_MINOR_BASE 32
12 #define MOUSEDEV_MINORS 32
13 #define MOUSEDEV_MIX 31
15 #include <linux/slab.h>
16 #include <linux/poll.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
20 #include <linux/config.h>
21 #include <linux/smp_lock.h>
22 #include <linux/random.h>
23 #include <linux/major.h>
24 #include <linux/device.h>
25 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
26 #include <linux/miscdevice.h>
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30 MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
31 MODULE_LICENSE("GPL");
33 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
34 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
36 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
37 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
46 wait_queue_head_t wait
;
47 struct list_head list
;
48 struct input_handle handle
;
52 struct mousedev_list
{
53 struct fasync_struct
*fasync
;
54 struct mousedev
*mousedev
;
55 struct list_head node
;
56 int dx
, dy
, dz
, oldx
, oldy
;
58 unsigned long buttons
;
59 unsigned char ready
, buffer
, bufsiz
;
60 unsigned char mode
, imexseq
, impsseq
;
63 #define MOUSEDEV_SEQ_LEN 6
65 static unsigned char mousedev_imps_seq
[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
66 static unsigned char mousedev_imex_seq
[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
68 static struct input_handler mousedev_handler
;
70 static struct mousedev
*mousedev_table
[MOUSEDEV_MINORS
];
71 static struct mousedev mousedev_mix
;
73 static int xres
= CONFIG_INPUT_MOUSEDEV_SCREEN_X
;
74 static int yres
= CONFIG_INPUT_MOUSEDEV_SCREEN_Y
;
76 static void mousedev_event(struct input_handle
*handle
, unsigned int type
, unsigned int code
, int value
)
78 struct mousedev
*mousedevs
[3] = { handle
->private, &mousedev_mix
, NULL
};
79 struct mousedev
**mousedev
= mousedevs
;
80 struct mousedev_list
*list
;
81 int index
, size
, wake
;
87 list_for_each_entry(list
, &(*mousedev
)->list
, node
)
90 if (test_bit(BTN_TRIGGER
, handle
->dev
->keybit
))
94 size
= handle
->dev
->absmax
[ABS_X
] - handle
->dev
->absmin
[ABS_X
];
96 list
->dx
+= (value
* xres
- list
->oldx
) / size
;
97 list
->oldx
+= list
->dx
* size
;
99 list
->dx
+= value
- list
->oldx
;
100 list
->oldx
+= list
->dx
;
105 size
= handle
->dev
->absmax
[ABS_Y
] - handle
->dev
->absmin
[ABS_Y
];
107 list
->dy
-= (value
* yres
- list
->oldy
) / size
;
108 list
->oldy
-= list
->dy
* size
;
110 list
->dy
-= value
- list
->oldy
;
111 list
->oldy
-= list
->dy
;
119 case REL_X
: list
->dx
+= value
; break;
120 case REL_Y
: list
->dy
-= value
; break;
121 case REL_WHEEL
: if (list
->mode
) list
->dz
-= value
; break;
129 case BTN_LEFT
: index
= 0; break;
131 case BTN_EXTRA
: if (list
->mode
== 2) { index
= 4; break; }
134 case BTN_RIGHT
: index
= 1; break;
136 case BTN_SIDE
: if (list
->mode
== 2) { index
= 3; break; }
139 case BTN_MIDDLE
: index
= 2; break;
143 case 0: clear_bit(index
, &list
->buttons
); break;
144 case 1: set_bit(index
, &list
->buttons
); break;
153 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
160 wake_up_interruptible(&((*mousedev
)->wait
));
166 static int mousedev_fasync(int fd
, struct file
*file
, int on
)
169 struct mousedev_list
*list
= file
->private_data
;
170 retval
= fasync_helper(fd
, file
, on
, &list
->fasync
);
171 return retval
< 0 ? retval
: 0;
174 static int mousedev_release(struct inode
* inode
, struct file
* file
)
176 struct mousedev_list
*list
= file
->private_data
;
177 struct input_handle
*handle
;
178 struct mousedev
*mousedev
;
180 mousedev_fasync(-1, file
, 0);
182 list_del(&list
->node
);
184 if (!--list
->mousedev
->open
) {
185 if (list
->mousedev
->minor
== MOUSEDEV_MIX
) {
186 list_for_each_entry(handle
, &mousedev_handler
.h_list
, h_node
) {
187 mousedev
= handle
->private;
188 if (!mousedev
->open
) {
189 if (mousedev
->exist
) {
190 input_close_device(&mousedev
->handle
);
192 input_unregister_minor(mousedev
->devfs
);
193 mousedev_table
[mousedev
->minor
] = NULL
;
199 if (!mousedev_mix
.open
) {
200 if (list
->mousedev
->exist
) {
201 input_close_device(&list
->mousedev
->handle
);
203 input_unregister_minor(list
->mousedev
->devfs
);
204 mousedev_table
[list
->mousedev
->minor
] = NULL
;
205 kfree(list
->mousedev
);
216 static int mousedev_open(struct inode
* inode
, struct file
* file
)
218 struct mousedev_list
*list
;
219 struct input_handle
*handle
;
220 struct mousedev
*mousedev
;
223 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
224 if (major(inode
->i_rdev
) == MISC_MAJOR
)
228 i
= minor(inode
->i_rdev
) - MOUSEDEV_MINOR_BASE
;
230 if (i
>= MOUSEDEV_MINORS
|| !mousedev_table
[i
])
233 if (!(list
= kmalloc(sizeof(struct mousedev_list
), GFP_KERNEL
)))
235 memset(list
, 0, sizeof(struct mousedev_list
));
237 list
->mousedev
= mousedev_table
[i
];
238 list_add_tail(&list
->node
, &mousedev_table
[i
]->list
);
239 file
->private_data
= list
;
241 if (!list
->mousedev
->open
++) {
242 if (list
->mousedev
->minor
== MOUSEDEV_MIX
) {
243 list_for_each_entry(handle
, &mousedev_handler
.h_list
, h_node
) {
244 mousedev
= handle
->private;
245 if (!mousedev
->open
&& mousedev
->exist
)
246 input_open_device(handle
);
249 if (!mousedev_mix
.open
&& list
->mousedev
->exist
)
250 input_open_device(&list
->mousedev
->handle
);
256 static void mousedev_packet(struct mousedev_list
*list
, unsigned char off
)
258 list
->ps2
[off
] = 0x08 | ((list
->dx
< 0) << 4) | ((list
->dy
< 0) << 5) | (list
->buttons
& 0x07);
259 list
->ps2
[off
+ 1] = (list
->dx
> 127 ? 127 : (list
->dx
< -127 ? -127 : list
->dx
));
260 list
->ps2
[off
+ 2] = (list
->dy
> 127 ? 127 : (list
->dy
< -127 ? -127 : list
->dy
));
261 list
->dx
-= list
->ps2
[off
+ 1];
262 list
->dy
-= list
->ps2
[off
+ 2];
263 list
->bufsiz
= off
+ 3;
265 if (list
->mode
== 2) {
266 list
->ps2
[off
+ 3] = (list
->dz
> 7 ? 7 : (list
->dz
< -7 ? -7 : list
->dz
));
267 list
->dz
-= list
->ps2
[off
+ 3];
268 list
->ps2
[off
+ 3] = (list
->ps2
[off
+ 3] & 0x0f) | ((list
->buttons
& 0x18) << 1);
272 if (list
->mode
== 1) {
273 list
->ps2
[off
+ 3] = (list
->dz
> 127 ? 127 : (list
->dz
< -127 ? -127 : list
->dz
));
274 list
->dz
-= list
->ps2
[off
+ 3];
278 if (!list
->dx
&& !list
->dy
&& (!list
->mode
|| !list
->dz
)) list
->ready
= 0;
279 list
->buffer
= list
->bufsiz
;
283 static ssize_t
mousedev_write(struct file
* file
, const char * buffer
, size_t count
, loff_t
*ppos
)
285 struct mousedev_list
*list
= file
->private_data
;
289 for (i
= 0; i
< count
; i
++) {
291 if (get_user(c
, buffer
+ i
))
294 if (c
== mousedev_imex_seq
[list
->imexseq
]) {
295 if (++list
->imexseq
== MOUSEDEV_SEQ_LEN
) {
299 } else list
->imexseq
= 0;
301 if (c
== mousedev_imps_seq
[list
->impsseq
]) {
302 if (++list
->impsseq
== MOUSEDEV_SEQ_LEN
) {
306 } else list
->impsseq
= 0;
313 case 0xeb: /* Poll */
314 mousedev_packet(list
, 1);
317 case 0xf2: /* Get ID */
318 switch (list
->mode
) {
319 case 0: list
->ps2
[1] = 0; break;
320 case 1: list
->ps2
[1] = 3; break;
321 case 2: list
->ps2
[1] = 4; break;
326 case 0xe9: /* Get info */
327 list
->ps2
[1] = 0x60; list
->ps2
[2] = 3; list
->ps2
[3] = 200;
331 case 0xff: /* Reset */
341 list
->buffer
= list
->bufsiz
;
344 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
346 wake_up_interruptible(&list
->mousedev
->wait
);
351 static ssize_t
mousedev_read(struct file
* file
, char * buffer
, size_t count
, loff_t
*ppos
)
353 struct mousedev_list
*list
= file
->private_data
;
356 if (!list
->ready
&& !list
->buffer
&& (file
->f_flags
& O_NONBLOCK
))
359 retval
= wait_event_interruptible(list
->mousedev
->wait
, list
->ready
|| list
->buffer
);
364 if (!list
->buffer
&& list
->ready
)
365 mousedev_packet(list
, 0);
367 if (count
> list
->buffer
)
368 count
= list
->buffer
;
370 list
->buffer
-= count
;
372 if (copy_to_user(buffer
, list
->ps2
+ list
->bufsiz
- list
->buffer
- count
, count
))
378 /* No kernel lock - fine */
379 static unsigned int mousedev_poll(struct file
*file
, poll_table
*wait
)
381 struct mousedev_list
*list
= file
->private_data
;
382 poll_wait(file
, &list
->mousedev
->wait
, wait
);
383 if (list
->ready
|| list
->buffer
)
384 return POLLIN
| POLLRDNORM
;
388 struct file_operations mousedev_fops
= {
389 .owner
= THIS_MODULE
,
390 .read
= mousedev_read
,
391 .write
= mousedev_write
,
392 .poll
= mousedev_poll
,
393 .open
= mousedev_open
,
394 .release
= mousedev_release
,
395 .fasync
= mousedev_fasync
,
398 static struct input_handle
*mousedev_connect(struct input_handler
*handler
, struct input_dev
*dev
, struct input_device_id
*id
)
400 struct mousedev
*mousedev
;
403 for (minor
= 0; minor
< MOUSEDEV_MINORS
&& mousedev_table
[minor
]; minor
++);
404 if (minor
== MOUSEDEV_MINORS
) {
405 printk(KERN_ERR
"mousedev: no more free mousedev devices\n");
409 if (!(mousedev
= kmalloc(sizeof(struct mousedev
), GFP_KERNEL
)))
411 memset(mousedev
, 0, sizeof(struct mousedev
));
413 INIT_LIST_HEAD(&mousedev
->list
);
414 init_waitqueue_head(&mousedev
->wait
);
416 mousedev
->minor
= minor
;
418 mousedev
->handle
.dev
= dev
;
419 mousedev
->handle
.name
= mousedev
->name
;
420 mousedev
->handle
.handler
= handler
;
421 mousedev
->handle
.private = mousedev
;
422 sprintf(mousedev
->name
, "mouse%d", minor
);
424 if (mousedev_mix
.open
)
425 input_open_device(&mousedev
->handle
);
427 mousedev_table
[minor
] = mousedev
;
428 mousedev
->devfs
= input_register_minor("mouse%d", minor
, MOUSEDEV_MINOR_BASE
);
430 return &mousedev
->handle
;
433 static void mousedev_disconnect(struct input_handle
*handle
)
435 struct mousedev
*mousedev
= handle
->private;
439 if (mousedev
->open
) {
440 input_close_device(handle
);
442 if (mousedev_mix
.open
)
443 input_close_device(handle
);
444 input_unregister_minor(mousedev
->devfs
);
445 mousedev_table
[mousedev
->minor
] = NULL
;
450 static struct input_device_id mousedev_ids
[] = {
452 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_KEYBIT
| INPUT_DEVICE_ID_MATCH_RELBIT
,
453 .evbit
= { BIT(EV_KEY
) | BIT(EV_REL
) },
454 .keybit
= { [LONG(BTN_LEFT
)] = BIT(BTN_LEFT
) },
455 .relbit
= { BIT(REL_X
) | BIT(REL_Y
) },
456 }, /* A mouse like device, at least one button, two relative axes */
459 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_KEYBIT
| INPUT_DEVICE_ID_MATCH_ABSBIT
,
460 .evbit
= { BIT(EV_KEY
) | BIT(EV_ABS
) },
461 .keybit
= { [LONG(BTN_TOUCH
)] = BIT(BTN_TOUCH
) },
462 .absbit
= { BIT(ABS_X
) | BIT(ABS_Y
) },
463 }, /* A tablet like device, at least touch detection, two absolute axes */
465 { }, /* Terminating entry */
468 MODULE_DEVICE_TABLE(input
, mousedev_ids
);
470 static struct input_handler mousedev_handler
= {
471 .event
= mousedev_event
,
472 .connect
= mousedev_connect
,
473 .disconnect
= mousedev_disconnect
,
474 .fops
= &mousedev_fops
,
475 .minor
= MOUSEDEV_MINOR_BASE
,
477 .id_table
= mousedev_ids
,
480 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
481 static struct miscdevice psaux_mouse
= {
482 PSMOUSE_MINOR
, "psaux", &mousedev_fops
486 static struct device_interface mousedev_intf
= {
488 .devclass
= &input_devclass
,
491 static int __init
mousedev_init(void)
495 if((retval
= interface_register(&mousedev_intf
)) < 0)
498 input_register_handler(&mousedev_handler
);
500 memset(&mousedev_mix
, 0, sizeof(struct mousedev
));
501 INIT_LIST_HEAD(&mousedev_mix
.list
);
502 init_waitqueue_head(&mousedev_mix
.wait
);
503 mousedev_table
[MOUSEDEV_MIX
] = &mousedev_mix
;
504 mousedev_mix
.exist
= 1;
505 mousedev_mix
.minor
= MOUSEDEV_MIX
;
506 mousedev_mix
.devfs
= input_register_minor("mice", MOUSEDEV_MIX
, MOUSEDEV_MINOR_BASE
);
508 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
509 if (!(mousedev_mix
.misc
= !misc_register(&psaux_mouse
)))
510 printk(KERN_WARNING
"mice: could not misc_register the device\n");
513 printk(KERN_INFO
"mice: PS/2 mouse device common for all mice\n");
518 static void __exit
mousedev_exit(void)
520 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
521 if (mousedev_mix
.misc
)
522 misc_deregister(&psaux_mouse
);
524 input_unregister_minor(mousedev_mix
.devfs
);
525 input_unregister_handler(&mousedev_handler
);
526 interface_unregister(&mousedev_intf
);
529 module_init(mousedev_init
);
530 module_exit(mousedev_exit
);
532 MODULE_PARM(xres
, "i");
533 MODULE_PARM_DESC(xres
, "Horizontal screen resolution");
534 MODULE_PARM(yres
, "i");
535 MODULE_PARM_DESC(yres
, "Vertical screen resolution");