1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1999-2001 Vojtech Pavlik
7 * Serial mouse driver for Linux
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/input.h>
18 #include <linux/serio.h>
20 #define DRIVER_DESC "Serial mouse driver"
22 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
23 MODULE_DESCRIPTION(DRIVER_DESC
);
24 MODULE_LICENSE("GPL");
26 static const char *sermouse_protocols
[] = { "None", "Mouse Systems Mouse", "Sun Mouse", "Microsoft Mouse",
27 "Logitech M+ Mouse", "Microsoft MZ Mouse", "Logitech MZ+ Mouse",
28 "Logitech MZ++ Mouse"};
31 struct input_dev
*dev
;
40 * sermouse_process_msc() analyzes the incoming MSC/Sun bytestream and
41 * applies some prediction to the data, resulting in 96 updates per
42 * second, which is as good as a PS/2 or USB mouse.
45 static void sermouse_process_msc(struct sermouse
*sermouse
, signed char data
)
47 struct input_dev
*dev
= sermouse
->dev
;
48 signed char *buf
= sermouse
->buf
;
50 switch (sermouse
->count
) {
53 if ((data
& 0xf8) != 0x80)
55 input_report_key(dev
, BTN_LEFT
, !(data
& 4));
56 input_report_key(dev
, BTN_RIGHT
, !(data
& 1));
57 input_report_key(dev
, BTN_MIDDLE
, !(data
& 2));
62 input_report_rel(dev
, REL_X
, data
/ 2);
63 input_report_rel(dev
, REL_Y
, -buf
[1]);
64 buf
[0] = data
- data
/ 2;
69 input_report_rel(dev
, REL_X
, buf
[0]);
70 input_report_rel(dev
, REL_Y
, buf
[1] - data
);
77 if (++sermouse
->count
== 5)
82 * sermouse_process_ms() anlyzes the incoming MS(Z/+/++) bytestream and
83 * generates events. With prediction it gets 80 updates/sec, assuming
84 * standard 3-byte packets and 1200 bps.
87 static void sermouse_process_ms(struct sermouse
*sermouse
, signed char data
)
89 struct input_dev
*dev
= sermouse
->dev
;
90 signed char *buf
= sermouse
->buf
;
94 else if (sermouse
->count
== 0)
97 switch (sermouse
->count
) {
101 input_report_key(dev
, BTN_LEFT
, (data
>> 5) & 1);
102 input_report_key(dev
, BTN_RIGHT
, (data
>> 4) & 1);
107 data
= (signed char) (((buf
[1] << 6) & 0xc0) | (data
& 0x3f));
108 input_report_rel(dev
, REL_X
, data
/ 2);
109 input_report_rel(dev
, REL_Y
, buf
[4]);
110 buf
[3] = data
- data
/ 2;
114 /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */
115 if ((sermouse
->type
== SERIO_MS
) && !data
&& !buf
[2] && !((buf
[0] & 0xf0) ^ buf
[1]))
116 input_report_key(dev
, BTN_MIDDLE
, !test_bit(BTN_MIDDLE
, dev
->key
));
119 data
= (signed char) (((buf
[1] << 4) & 0xc0) | (data
& 0x3f));
120 input_report_rel(dev
, REL_X
, buf
[3]);
121 input_report_rel(dev
, REL_Y
, data
- buf
[4]);
127 switch (sermouse
->type
) {
130 sermouse
->type
= SERIO_MP
;
134 if ((data
>> 2) & 3) break; /* M++ Wireless Extension packet. */
135 input_report_key(dev
, BTN_MIDDLE
, (data
>> 5) & 1);
136 input_report_key(dev
, BTN_SIDE
, (data
>> 4) & 1);
141 input_report_key(dev
, BTN_SIDE
, (data
>> 5) & 1);
145 input_report_key(dev
, BTN_MIDDLE
, (data
>> 4) & 1);
146 input_report_rel(dev
, REL_WHEEL
, (data
& 8) - (data
& 7));
153 case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */
154 buf
[1] = (data
>> 2) & 0x0f;
158 case 7: /* Ignore anything besides MZ++ */
159 if (sermouse
->type
!= SERIO_MZPP
)
164 case 1: /* Extra mouse info */
166 input_report_key(dev
, BTN_SIDE
, (data
>> 4) & 1);
167 input_report_key(dev
, BTN_EXTRA
, (data
>> 5) & 1);
168 input_report_rel(dev
, data
& 0x80 ? REL_HWHEEL
: REL_WHEEL
, (data
& 7) - (data
& 8));
172 default: /* We don't decode anything else yet. */
175 "sermouse.c: Received MZ++ packet %x, don't know how to handle.\n", buf
[1]);
188 * sermouse_interrupt() handles incoming characters, either gathering them into
189 * packets or passing them to the command routine as command output.
192 static irqreturn_t
sermouse_interrupt(struct serio
*serio
,
193 unsigned char data
, unsigned int flags
)
195 struct sermouse
*sermouse
= serio_get_drvdata(serio
);
197 if (time_after(jiffies
, sermouse
->last
+ HZ
/10))
200 sermouse
->last
= jiffies
;
202 if (sermouse
->type
> SERIO_SUN
)
203 sermouse_process_ms(sermouse
, data
);
205 sermouse_process_msc(sermouse
, data
);
211 * sermouse_disconnect() cleans up after we don't want talk
212 * to the mouse anymore.
215 static void sermouse_disconnect(struct serio
*serio
)
217 struct sermouse
*sermouse
= serio_get_drvdata(serio
);
220 serio_set_drvdata(serio
, NULL
);
221 input_unregister_device(sermouse
->dev
);
226 * sermouse_connect() is a callback form the serio module when
227 * an unhandled serio port is found.
230 static int sermouse_connect(struct serio
*serio
, struct serio_driver
*drv
)
232 struct sermouse
*sermouse
;
233 struct input_dev
*input_dev
;
234 unsigned char c
= serio
->id
.extra
;
237 sermouse
= kzalloc(sizeof(struct sermouse
), GFP_KERNEL
);
238 input_dev
= input_allocate_device();
239 if (!sermouse
|| !input_dev
)
242 sermouse
->dev
= input_dev
;
243 snprintf(sermouse
->phys
, sizeof(sermouse
->phys
), "%s/input0", serio
->phys
);
244 sermouse
->type
= serio
->id
.proto
;
246 input_dev
->name
= sermouse_protocols
[sermouse
->type
];
247 input_dev
->phys
= sermouse
->phys
;
248 input_dev
->id
.bustype
= BUS_RS232
;
249 input_dev
->id
.vendor
= sermouse
->type
;
250 input_dev
->id
.product
= c
;
251 input_dev
->id
.version
= 0x0100;
252 input_dev
->dev
.parent
= &serio
->dev
;
254 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
255 input_dev
->keybit
[BIT_WORD(BTN_MOUSE
)] = BIT_MASK(BTN_LEFT
) |
257 input_dev
->relbit
[0] = BIT_MASK(REL_X
) | BIT_MASK(REL_Y
);
259 if (c
& 0x01) set_bit(BTN_MIDDLE
, input_dev
->keybit
);
260 if (c
& 0x02) set_bit(BTN_SIDE
, input_dev
->keybit
);
261 if (c
& 0x04) set_bit(BTN_EXTRA
, input_dev
->keybit
);
262 if (c
& 0x10) set_bit(REL_WHEEL
, input_dev
->relbit
);
263 if (c
& 0x20) set_bit(REL_HWHEEL
, input_dev
->relbit
);
265 serio_set_drvdata(serio
, sermouse
);
267 err
= serio_open(serio
, drv
);
271 err
= input_register_device(sermouse
->dev
);
277 fail3
: serio_close(serio
);
278 fail2
: serio_set_drvdata(serio
, NULL
);
279 fail1
: input_free_device(input_dev
);
284 static struct serio_device_id sermouse_serio_ids
[] = {
330 MODULE_DEVICE_TABLE(serio
, sermouse_serio_ids
);
332 static struct serio_driver sermouse_drv
= {
336 .description
= DRIVER_DESC
,
337 .id_table
= sermouse_serio_ids
,
338 .interrupt
= sermouse_interrupt
,
339 .connect
= sermouse_connect
,
340 .disconnect
= sermouse_disconnect
,
343 module_serio_driver(sermouse_drv
);