2 * Copyright (c) 1999-2001 Vojtech Pavlik
6 * Serial mouse driver for Linux
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 #include <linux/delay.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/input.h>
34 #include <linux/serio.h>
36 #define DRIVER_DESC "Serial mouse driver"
38 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39 MODULE_DESCRIPTION(DRIVER_DESC
);
40 MODULE_LICENSE("GPL");
42 static const char *sermouse_protocols
[] = { "None", "Mouse Systems Mouse", "Sun Mouse", "Microsoft Mouse",
43 "Logitech M+ Mouse", "Microsoft MZ Mouse", "Logitech MZ+ Mouse",
44 "Logitech MZ++ Mouse"};
47 struct input_dev
*dev
;
56 * sermouse_process_msc() analyzes the incoming MSC/Sun bytestream and
57 * applies some prediction to the data, resulting in 96 updates per
58 * second, which is as good as a PS/2 or USB mouse.
61 static void sermouse_process_msc(struct sermouse
*sermouse
, signed char data
)
63 struct input_dev
*dev
= sermouse
->dev
;
64 signed char *buf
= sermouse
->buf
;
66 switch (sermouse
->count
) {
69 if ((data
& 0xf8) != 0x80)
71 input_report_key(dev
, BTN_LEFT
, !(data
& 4));
72 input_report_key(dev
, BTN_RIGHT
, !(data
& 1));
73 input_report_key(dev
, BTN_MIDDLE
, !(data
& 2));
78 input_report_rel(dev
, REL_X
, data
/ 2);
79 input_report_rel(dev
, REL_Y
, -buf
[1]);
80 buf
[0] = data
- data
/ 2;
85 input_report_rel(dev
, REL_X
, buf
[0]);
86 input_report_rel(dev
, REL_Y
, buf
[1] - data
);
93 if (++sermouse
->count
== 5)
98 * sermouse_process_ms() anlyzes the incoming MS(Z/+/++) bytestream and
99 * generates events. With prediction it gets 80 updates/sec, assuming
100 * standard 3-byte packets and 1200 bps.
103 static void sermouse_process_ms(struct sermouse
*sermouse
, signed char data
)
105 struct input_dev
*dev
= sermouse
->dev
;
106 signed char *buf
= sermouse
->buf
;
110 else if (sermouse
->count
== 0)
113 switch (sermouse
->count
) {
117 input_report_key(dev
, BTN_LEFT
, (data
>> 5) & 1);
118 input_report_key(dev
, BTN_RIGHT
, (data
>> 4) & 1);
123 data
= (signed char) (((buf
[1] << 6) & 0xc0) | (data
& 0x3f));
124 input_report_rel(dev
, REL_X
, data
/ 2);
125 input_report_rel(dev
, REL_Y
, buf
[4]);
126 buf
[3] = data
- data
/ 2;
130 /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */
131 if ((sermouse
->type
== SERIO_MS
) && !data
&& !buf
[2] && !((buf
[0] & 0xf0) ^ buf
[1]))
132 input_report_key(dev
, BTN_MIDDLE
, !test_bit(BTN_MIDDLE
, dev
->key
));
135 data
= (signed char) (((buf
[1] << 4) & 0xc0) | (data
& 0x3f));
136 input_report_rel(dev
, REL_X
, buf
[3]);
137 input_report_rel(dev
, REL_Y
, data
- buf
[4]);
143 switch (sermouse
->type
) {
146 sermouse
->type
= SERIO_MP
;
149 if ((data
>> 2) & 3) break; /* M++ Wireless Extension packet. */
150 input_report_key(dev
, BTN_MIDDLE
, (data
>> 5) & 1);
151 input_report_key(dev
, BTN_SIDE
, (data
>> 4) & 1);
156 input_report_key(dev
, BTN_SIDE
, (data
>> 5) & 1);
159 input_report_key(dev
, BTN_MIDDLE
, (data
>> 4) & 1);
160 input_report_rel(dev
, REL_WHEEL
, (data
& 8) - (data
& 7));
167 case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */
168 buf
[1] = (data
>> 2) & 0x0f;
172 case 7: /* Ignore anything besides MZ++ */
173 if (sermouse
->type
!= SERIO_MZPP
)
178 case 1: /* Extra mouse info */
180 input_report_key(dev
, BTN_SIDE
, (data
>> 4) & 1);
181 input_report_key(dev
, BTN_EXTRA
, (data
>> 5) & 1);
182 input_report_rel(dev
, data
& 0x80 ? REL_HWHEEL
: REL_WHEEL
, (data
& 7) - (data
& 8));
186 default: /* We don't decode anything else yet. */
189 "sermouse.c: Received MZ++ packet %x, don't know how to handle.\n", buf
[1]);
202 * sermouse_interrupt() handles incoming characters, either gathering them into
203 * packets or passing them to the command routine as command output.
206 static irqreturn_t
sermouse_interrupt(struct serio
*serio
,
207 unsigned char data
, unsigned int flags
)
209 struct sermouse
*sermouse
= serio_get_drvdata(serio
);
211 if (time_after(jiffies
, sermouse
->last
+ HZ
/10))
214 sermouse
->last
= jiffies
;
216 if (sermouse
->type
> SERIO_SUN
)
217 sermouse_process_ms(sermouse
, data
);
219 sermouse_process_msc(sermouse
, data
);
225 * sermouse_disconnect() cleans up after we don't want talk
226 * to the mouse anymore.
229 static void sermouse_disconnect(struct serio
*serio
)
231 struct sermouse
*sermouse
= serio_get_drvdata(serio
);
234 serio_set_drvdata(serio
, NULL
);
235 input_unregister_device(sermouse
->dev
);
240 * sermouse_connect() is a callback form the serio module when
241 * an unhandled serio port is found.
244 static int sermouse_connect(struct serio
*serio
, struct serio_driver
*drv
)
246 struct sermouse
*sermouse
;
247 struct input_dev
*input_dev
;
248 unsigned char c
= serio
->id
.extra
;
251 sermouse
= kzalloc(sizeof(struct sermouse
), GFP_KERNEL
);
252 input_dev
= input_allocate_device();
253 if (!sermouse
|| !input_dev
)
256 sermouse
->dev
= input_dev
;
257 snprintf(sermouse
->phys
, sizeof(sermouse
->phys
), "%s/input0", serio
->phys
);
258 sermouse
->type
= serio
->id
.proto
;
260 input_dev
->name
= sermouse_protocols
[sermouse
->type
];
261 input_dev
->phys
= sermouse
->phys
;
262 input_dev
->id
.bustype
= BUS_RS232
;
263 input_dev
->id
.vendor
= sermouse
->type
;
264 input_dev
->id
.product
= c
;
265 input_dev
->id
.version
= 0x0100;
266 input_dev
->dev
.parent
= &serio
->dev
;
268 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
269 input_dev
->keybit
[BIT_WORD(BTN_MOUSE
)] = BIT_MASK(BTN_LEFT
) |
271 input_dev
->relbit
[0] = BIT_MASK(REL_X
) | BIT_MASK(REL_Y
);
273 if (c
& 0x01) set_bit(BTN_MIDDLE
, input_dev
->keybit
);
274 if (c
& 0x02) set_bit(BTN_SIDE
, input_dev
->keybit
);
275 if (c
& 0x04) set_bit(BTN_EXTRA
, input_dev
->keybit
);
276 if (c
& 0x10) set_bit(REL_WHEEL
, input_dev
->relbit
);
277 if (c
& 0x20) set_bit(REL_HWHEEL
, input_dev
->relbit
);
279 serio_set_drvdata(serio
, sermouse
);
281 err
= serio_open(serio
, drv
);
285 err
= input_register_device(sermouse
->dev
);
291 fail3
: serio_close(serio
);
292 fail2
: serio_set_drvdata(serio
, NULL
);
293 fail1
: input_free_device(input_dev
);
298 static struct serio_device_id sermouse_serio_ids
[] = {
344 MODULE_DEVICE_TABLE(serio
, sermouse_serio_ids
);
346 static struct serio_driver sermouse_drv
= {
350 .description
= DRIVER_DESC
,
351 .id_table
= sermouse_serio_ids
,
352 .interrupt
= sermouse_interrupt
,
353 .connect
= sermouse_connect
,
354 .disconnect
= sermouse_disconnect
,
357 module_serio_driver(sermouse_drv
);