2
* Input driver to ExplorerPS/2 device driver module.
4
* Copyright (c) 1999-2002 Vojtech Pavlik
5
* Copyright (c) 2004 Dmitry Torokhov
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License version 2 as published by
9
* the Free Software Foundation.
12
#define MOUSEDEV_MINOR_BASE 32
13
#define MOUSEDEV_MINORS 32
14
#define MOUSEDEV_MIX 31
16
#include <linux/slab.h>
17
#include <linux/poll.h>
18
#include <linux/module.h>
19
#include <linux/moduleparam.h>
20
#include <linux/init.h>
21
#include <linux/input.h>
22
#include <linux/config.h>
23
#include <linux/smp_lock.h>
24
#include <linux/random.h>
25
#include <linux/major.h>
26
#include <linux/device.h>
27
#include <linux/devfs_fs_kernel.h>
28
#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
29
#include <linux/miscdevice.h>
32
MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
33
MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces - 2.6.9-pc-0.1");
34
MODULE_LICENSE("GPL");
36
#ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
37
#define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
39
#ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
40
#define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
43
static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
44
module_param(xres, uint, 0);
45
MODULE_PARM_DESC(xres, "Horizontal screen resolution");
47
static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
48
module_param(yres, uint, 0);
49
MODULE_PARM_DESC(yres, "Vertical screen resolution");
51
static unsigned tap_time = 200;
52
module_param(tap_time, uint, 0);
53
MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)");
55
struct mousedev_hw_data {
59
unsigned long buttons;
67
wait_queue_head_t wait;
68
struct list_head list;
69
struct input_handle handle;
71
struct mousedev_hw_data packet;
72
unsigned int pkt_count;
73
int old_x[4], old_y[4];
83
struct mousedev_motion {
85
unsigned long buttons;
88
#define PACKET_QUEUE_LEN 16
89
struct mousedev_list {
90
struct fasync_struct *fasync;
91
struct mousedev *mousedev;
92
struct list_head node;
94
struct mousedev_motion packets[PACKET_QUEUE_LEN];
95
unsigned int head, tail;
96
spinlock_t packet_lock;
100
unsigned char ready, buffer, bufsiz;
101
unsigned char imexseq, impsseq;
102
enum mousedev_emul mode;
105
#define MOUSEDEV_SEQ_LEN 6
107
static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
108
static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
110
static struct input_handler mousedev_handler;
112
static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
113
static struct mousedev mousedev_mix;
115
#define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
116
#define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
118
static void mousedev_touchpad_event(struct mousedev *mousedev, unsigned int code, int value)
120
if (mousedev->touch) {
124
if (mousedev->pkt_count >= 2)
125
mousedev->packet.dx = ((fx(0) - fx(1)) / 2 + (fx(1) - fx(2)) / 2) / 8;
130
if (mousedev->pkt_count >= 2)
131
mousedev->packet.dy = -((fy(0) - fy(1)) / 2 + (fy(1) - fy(2)) / 2) / 8;
137
static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
143
size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
144
if (size == 0) size = xres;
145
if (value > dev->absmax[ABS_X]) value = dev->absmax[ABS_X];
146
if (value < dev->absmin[ABS_X]) value = dev->absmin[ABS_X];
147
mousedev->packet.x = ((value - dev->absmin[ABS_X]) * xres) / size;
148
mousedev->packet.abs_event = 1;
152
size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
153
if (size == 0) size = yres;
154
if (value > dev->absmax[ABS_Y]) value = dev->absmax[ABS_Y];
155
if (value < dev->absmin[ABS_Y]) value = dev->absmin[ABS_Y];
156
mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size;
157
mousedev->packet.abs_event = 1;
162
static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
165
case REL_X: mousedev->packet.dx += value; break;
166
case REL_Y: mousedev->packet.dy -= value; break;
167
case REL_WHEEL: mousedev->packet.dz -= value; break;
171
static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
179
case BTN_LEFT: index = 0; break;
182
case BTN_RIGHT: index = 1; break;
185
case BTN_MIDDLE: index = 2; break;
188
case BTN_SIDE: index = 3; break;
190
case BTN_EXTRA: index = 4; break;
195
set_bit(index, &mousedev->packet.buttons);
196
set_bit(index, &mousedev_mix.packet.buttons);
198
clear_bit(index, &mousedev->packet.buttons);
199
clear_bit(index, &mousedev_mix.packet.buttons);
203
static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet)
205
struct mousedev_list *list;
206
struct mousedev_motion *p;
209
list_for_each_entry(list, &mousedev->list, node) {
210
spin_lock_irqsave(&list->packet_lock, flags);
212
p = &list->packets[list->head];
213
if (list->ready && p->buttons != packet->buttons) {
214
unsigned int new_head = (list->head + 1) % PACKET_QUEUE_LEN;
215
if (new_head != list->tail) {
216
p = &list->packets[list->head = new_head];
217
memset(p, 0, sizeof(struct mousedev_motion));
221
if (packet->abs_event) {
222
p->dx += packet->x - list->pos_x;
223
p->dy += packet->y - list->pos_y;
224
list->pos_x = packet->x;
225
list->pos_y = packet->y;
228
list->pos_x += packet->dx;
229
list->pos_x = list->pos_x < 0 ? 0 : (list->pos_x >= xres ? xres : list->pos_x);
230
list->pos_y += packet->dy;
231
list->pos_y = list->pos_y < 0 ? 0 : (list->pos_y >= yres ? yres : list->pos_y);
236
p->buttons = mousedev->packet.buttons;
240
spin_unlock_irqrestore(&list->packet_lock, flags);
241
kill_fasync(&list->fasync, SIGIO, POLL_IN);
244
wake_up_interruptible(&mousedev->wait);
247
static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
250
if (mousedev->touch &&
251
time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) {
253
* Toggle left button to emulate tap.
254
* We rely on the fact that mousedev_mix always has 0
255
* motion packet so we won't mess current position.
257
set_bit(0, &mousedev->packet.buttons);
258
set_bit(0, &mousedev_mix.packet.buttons);
259
mousedev_notify_readers(mousedev, &mousedev_mix.packet);
260
mousedev_notify_readers(&mousedev_mix, &mousedev_mix.packet);
261
clear_bit(0, &mousedev->packet.buttons);
262
clear_bit(0, &mousedev_mix.packet.buttons);
264
mousedev->touch = mousedev->pkt_count = 0;
267
if (!mousedev->touch)
268
mousedev->touch = jiffies;
271
static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
273
struct mousedev *mousedev = handle->private;
277
/* Ignore joysticks */
278
if (test_bit(BTN_TRIGGER, handle->dev->keybit))
281
if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
282
mousedev_touchpad_event(mousedev, code, value);
284
mousedev_abs_event(handle->dev, mousedev, code, value);
289
mousedev_rel_event(mousedev, code, value);
294
if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
295
mousedev_touchpad_touch(mousedev, value);
297
mousedev_key_event(mousedev, code, value);
302
if (code == SYN_REPORT) {
303
if (mousedev->touch) {
304
mousedev->pkt_count++;
305
/* Input system eats duplicate events, but we need all of them
306
* to do correct averaging so apply present one forward
312
mousedev_notify_readers(mousedev, &mousedev->packet);
313
mousedev_notify_readers(&mousedev_mix, &mousedev->packet);
315
mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0;
316
mousedev->packet.abs_event = 0;
322
static int mousedev_fasync(int fd, struct file *file, int on)
325
struct mousedev_list *list = file->private_data;
326
retval = fasync_helper(fd, file, on, &list->fasync);
327
return retval < 0 ? retval : 0;
330
static void mousedev_free(struct mousedev *mousedev)
332
devfs_remove("input/mouse%d", mousedev->minor);
333
class_simple_device_remove(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor));
334
mousedev_table[mousedev->minor] = NULL;
338
static int mixdev_release(void)
340
struct input_handle *handle;
342
list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
343
struct mousedev *mousedev = handle->private;
345
if (!mousedev->open) {
347
input_close_device(&mousedev->handle);
349
mousedev_free(mousedev);
356
static int mousedev_release(struct inode * inode, struct file * file)
358
struct mousedev_list *list = file->private_data;
360
mousedev_fasync(-1, file, 0);
362
list_del(&list->node);
364
if (!--list->mousedev->open) {
365
if (list->mousedev->minor == MOUSEDEV_MIX)
366
return mixdev_release();
368
if (!mousedev_mix.open) {
369
if (list->mousedev->exist)
370
input_close_device(&list->mousedev->handle);
372
mousedev_free(list->mousedev);
380
static int mousedev_open(struct inode * inode, struct file * file)
382
struct mousedev_list *list;
383
struct input_handle *handle;
384
struct mousedev *mousedev;
387
#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
388
if (imajor(inode) == MISC_MAJOR)
392
i = iminor(inode) - MOUSEDEV_MINOR_BASE;
394
if (i >= MOUSEDEV_MINORS || !mousedev_table[i])
397
if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
399
memset(list, 0, sizeof(struct mousedev_list));
401
spin_lock_init(&list->packet_lock);
402
list->pos_x = xres / 2;
403
list->pos_y = yres / 2;
404
list->mousedev = mousedev_table[i];
405
list_add_tail(&list->node, &mousedev_table[i]->list);
406
file->private_data = list;
408
if (!list->mousedev->open++) {
409
if (list->mousedev->minor == MOUSEDEV_MIX) {
410
list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
411
mousedev = handle->private;
412
if (!mousedev->open && mousedev->exist)
413
input_open_device(handle);
416
if (!mousedev_mix.open && list->mousedev->exist)
417
input_open_device(&list->mousedev->handle);
423
static inline int mousedev_limit_delta(int delta, int limit)
425
return delta > limit ? limit : (delta < -limit ? -limit : delta);
428
static void mousedev_packet(struct mousedev_list *list, signed char *ps2_data)
430
struct mousedev_motion *p;
433
spin_lock_irqsave(&list->packet_lock, flags);
434
p = &list->packets[list->tail];
436
ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
437
ps2_data[1] = mousedev_limit_delta(p->dx, 127);
438
ps2_data[2] = mousedev_limit_delta(p->dy, 127);
439
p->dx -= ps2_data[1];
440
p->dy -= ps2_data[2];
442
switch (list->mode) {
443
case MOUSEDEV_EMUL_EXPS:
444
ps2_data[3] = mousedev_limit_delta(p->dz, 127);
445
p->dz -= ps2_data[3];
446
ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
450
case MOUSEDEV_EMUL_IMPS:
451
ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
452
ps2_data[3] = mousedev_limit_delta(p->dz, 127);
453
p->dz -= ps2_data[3];
457
case MOUSEDEV_EMUL_PS2:
459
ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
465
if (!p->dx && !p->dy && !p->dz) {
466
if (list->tail != list->head)
467
list->tail = (list->tail + 1) % PACKET_QUEUE_LEN;
468
if (list->tail == list->head)
472
spin_unlock_irqrestore(&list->packet_lock, flags);
476
static ssize_t mousedev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
478
struct mousedev_list *list = file->private_data;
482
for (i = 0; i < count; i++) {
484
if (get_user(c, buffer + i))
487
if (c == mousedev_imex_seq[list->imexseq]) {
488
if (++list->imexseq == MOUSEDEV_SEQ_LEN) {
490
list->mode = MOUSEDEV_EMUL_EXPS;
492
} else list->imexseq = 0;
494
if (c == mousedev_imps_seq[list->impsseq]) {
495
if (++list->impsseq == MOUSEDEV_SEQ_LEN) {
497
list->mode = MOUSEDEV_EMUL_IMPS;
499
} else list->impsseq = 0;
505
case 0xeb: /* Poll */
506
mousedev_packet(list, &list->ps2[1]);
507
list->bufsiz++; /* account for leading ACK */
510
case 0xf2: /* Get ID */
511
switch (list->mode) {
512
case MOUSEDEV_EMUL_PS2: list->ps2[1] = 0; break;
513
case MOUSEDEV_EMUL_IMPS: list->ps2[1] = 3; break;
514
case MOUSEDEV_EMUL_EXPS: list->ps2[1] = 4; break;
519
case 0xe9: /* Get info */
520
list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
524
case 0xff: /* Reset */
525
list->impsseq = list->imexseq = 0;
526
list->mode = MOUSEDEV_EMUL_PS2;
527
list->ps2[1] = 0xaa; list->ps2[2] = 0x00;
536
list->buffer = list->bufsiz;
539
kill_fasync(&list->fasync, SIGIO, POLL_IN);
541
wake_up_interruptible(&list->mousedev->wait);
546
static ssize_t mousedev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
548
struct mousedev_list *list = file->private_data;
551
if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK))
554
retval = wait_event_interruptible(list->mousedev->wait,
555
!list->mousedev->exist || list->ready || list->buffer);
560
if (!list->mousedev->exist)
563
if (!list->buffer && list->ready) {
564
mousedev_packet(list, list->ps2);
565
list->buffer = list->bufsiz;
568
if (count > list->buffer)
569
count = list->buffer;
571
list->buffer -= count;
573
if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count))
579
/* No kernel lock - fine */
580
static unsigned int mousedev_poll(struct file *file, poll_table *wait)
582
struct mousedev_list *list = file->private_data;
583
poll_wait(file, &list->mousedev->wait, wait);
584
if (list->ready || list->buffer)
585
return POLLIN | POLLRDNORM;
589
struct file_operations mousedev_fops = {
590
.owner = THIS_MODULE,
591
.read = mousedev_read,
592
.write = mousedev_write,
593
.poll = mousedev_poll,
594
.open = mousedev_open,
595
.release = mousedev_release,
596
.fasync = mousedev_fasync,
599
static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
601
struct mousedev *mousedev;
604
/* Ignore all wacom tablets */
605
if (dev->id.vendor == 0x56a) return NULL;
607
for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
608
if (minor == MOUSEDEV_MINORS) {
609
printk(KERN_ERR "mousedev: no more free mousedev devices\n");
613
if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
615
memset(mousedev, 0, sizeof(struct mousedev));
617
INIT_LIST_HEAD(&mousedev->list);
618
init_waitqueue_head(&mousedev->wait);
620
mousedev->minor = minor;
622
mousedev->handle.dev = dev;
623
mousedev->handle.name = mousedev->name;
624
mousedev->handle.handler = handler;
625
mousedev->handle.private = mousedev;
626
sprintf(mousedev->name, "mouse%d", minor);
628
if (mousedev_mix.open)
629
input_open_device(&mousedev->handle);
631
mousedev_table[minor] = mousedev;
633
devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
634
S_IFCHR|S_IRUGO|S_IWUSR, "input/mouse%d", minor);
635
class_simple_device_add(input_class,
636
MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
637
dev->dev, "mouse%d", minor);
639
return &mousedev->handle;
642
static void mousedev_disconnect(struct input_handle *handle)
644
struct mousedev *mousedev = handle->private;
648
if (mousedev->open) {
649
input_close_device(handle);
651
if (mousedev_mix.open)
652
input_close_device(handle);
653
mousedev_free(mousedev);
657
static struct input_device_id mousedev_ids[] = {
659
.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
660
.evbit = { BIT(EV_KEY) | BIT(EV_REL) },
661
.keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
662
.relbit = { BIT(REL_X) | BIT(REL_Y) },
663
}, /* A mouse like device, at least one button, two relative axes */
665
.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
666
.evbit = { BIT(EV_KEY) | BIT(EV_REL) },
667
.relbit = { BIT(REL_WHEEL) },
668
}, /* A separate scrollwheel */
670
.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
671
.evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
672
.keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
673
.absbit = { BIT(ABS_X) | BIT(ABS_Y) },
674
}, /* A tablet like device, at least touch detection, two absolute axes */
676
.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
677
.evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
678
.keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
679
.absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
682
{ }, /* Terminating entry */
685
MODULE_DEVICE_TABLE(input, mousedev_ids);
687
static struct input_handler mousedev_handler = {
688
.event = mousedev_event,
689
.connect = mousedev_connect,
690
.disconnect = mousedev_disconnect,
691
.fops = &mousedev_fops,
692
.minor = MOUSEDEV_MINOR_BASE,
694
.id_table = mousedev_ids,
697
#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
698
static struct miscdevice psaux_mouse = {
699
PSMOUSE_MINOR, "psaux", &mousedev_fops
701
static int psaux_registered;
704
static int __init mousedev_init(void)
706
input_register_handler(&mousedev_handler);
708
memset(&mousedev_mix, 0, sizeof(struct mousedev));
709
INIT_LIST_HEAD(&mousedev_mix.list);
710
init_waitqueue_head(&mousedev_mix.wait);
711
mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
712
mousedev_mix.exist = 1;
713
mousedev_mix.minor = MOUSEDEV_MIX;
715
devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX),
716
S_IFCHR|S_IRUGO|S_IWUSR, "input/mice");
717
class_simple_device_add(input_class, MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX),
720
#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
721
if (!(psaux_registered = !misc_register(&psaux_mouse)))
722
printk(KERN_WARNING "mice: could not misc_register the device\n");
725
printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
730
static void __exit mousedev_exit(void)
732
#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
733
if (psaux_registered)
734
misc_deregister(&psaux_mouse);
736
devfs_remove("input/mice");
737
class_simple_device_remove(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX));
738
input_unregister_handler(&mousedev_handler);
741
module_init(mousedev_init);
742
module_exit(mousedev_exit);