2 * $Id: sermouse.c,v 1.17 2002/03/13 10:03:43 vojtech Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
8 * Serial mouse driver for Linux
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@ucw.cz>, or by paper mail:
28 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
31 #include <linux/delay.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/input.h>
36 #include <linux/serio.h>
37 #include <linux/init.h>
39 #define DRIVER_DESC "Serial mouse driver"
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION(DRIVER_DESC);
43 MODULE_LICENSE("GPL");
45 static const char *sermouse_protocols[] = { "None", "Mouse Systems Mouse", "Sun Mouse", "Microsoft Mouse",
46 "Logitech M+ Mouse", "Microsoft MZ Mouse", "Logitech MZ+ Mouse",
47 "Logitech MZ++ Mouse"};
50 struct input_dev *dev;
59 * sermouse_process_msc() analyzes the incoming MSC/Sun bytestream and
60 * applies some prediction to the data, resulting in 96 updates per
61 * second, which is as good as a PS/2 or USB mouse.
64 static void sermouse_process_msc(struct sermouse *sermouse, signed char data)
66 struct input_dev *dev = sermouse->dev;
67 signed char *buf = sermouse->buf;
69 switch (sermouse->count) {
72 if ((data & 0xf8) != 0x80)
74 input_report_key(dev, BTN_LEFT, !(data & 4));
75 input_report_key(dev, BTN_RIGHT, !(data & 1));
76 input_report_key(dev, BTN_MIDDLE, !(data & 2));
81 input_report_rel(dev, REL_X, data / 2);
82 input_report_rel(dev, REL_Y, -buf[1]);
83 buf[0] = data - data / 2;
88 input_report_rel(dev, REL_X, buf[0]);
89 input_report_rel(dev, REL_Y, buf[1] - data);
96 if (++sermouse->count == 5)
101 * sermouse_process_ms() anlyzes the incoming MS(Z/+/++) bytestream and
102 * generates events. With prediction it gets 80 updates/sec, assuming
103 * standard 3-byte packets and 1200 bps.
106 static void sermouse_process_ms(struct sermouse *sermouse, signed char data)
108 struct input_dev *dev = sermouse->dev;
109 signed char *buf = sermouse->buf;
113 else if (sermouse->count == 0)
116 switch (sermouse->count) {
120 input_report_key(dev, BTN_LEFT, (data >> 5) & 1);
121 input_report_key(dev, BTN_RIGHT, (data >> 4) & 1);
126 data = (signed char) (((buf[1] << 6) & 0xc0) | (data & 0x3f));
127 input_report_rel(dev, REL_X, data / 2);
128 input_report_rel(dev, REL_Y, buf[4]);
129 buf[3] = data - data / 2;
133 /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */
134 if ((sermouse->type == SERIO_MS) && !data && !buf[2] && !((buf[0] & 0xf0) ^ buf[1]))
135 input_report_key(dev, BTN_MIDDLE, !test_bit(BTN_MIDDLE, dev->key));
138 data = (signed char) (((buf[1] << 4) & 0xc0) | (data & 0x3f));
139 input_report_rel(dev, REL_X, buf[3]);
140 input_report_rel(dev, REL_Y, data - buf[4]);
146 switch (sermouse->type) {
149 sermouse->type = SERIO_MP;
152 if ((data >> 2) & 3) break; /* M++ Wireless Extension packet. */
153 input_report_key(dev, BTN_MIDDLE, (data >> 5) & 1);
154 input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
159 input_report_key(dev, BTN_SIDE, (data >> 5) & 1);
162 input_report_key(dev, BTN_MIDDLE, (data >> 4) & 1);
163 input_report_rel(dev, REL_WHEEL, (data & 8) - (data & 7));
170 case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */
171 buf[1] = (data >> 2) & 0x0f;
175 case 7: /* Ignore anything besides MZ++ */
176 if (sermouse->type != SERIO_MZPP)
181 case 1: /* Extra mouse info */
183 input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
184 input_report_key(dev, BTN_EXTRA, (data >> 5) & 1);
185 input_report_rel(dev, data & 0x80 ? REL_HWHEEL : REL_WHEEL, (data & 7) - (data & 8));
189 default: /* We don't decode anything else yet. */
192 "sermouse.c: Received MZ++ packet %x, don't know how to handle.\n", buf[1]);
205 * sermouse_interrupt() handles incoming characters, either gathering them into
206 * packets or passing them to the command routine as command output.
209 static irqreturn_t sermouse_interrupt(struct serio *serio,
210 unsigned char data, unsigned int flags)
212 struct sermouse *sermouse = serio_get_drvdata(serio);
214 if (time_after(jiffies, sermouse->last + HZ/10))
217 sermouse->last = jiffies;
219 if (sermouse->type > SERIO_SUN)
220 sermouse_process_ms(sermouse, data);
222 sermouse_process_msc(sermouse, data);
228 * sermouse_disconnect() cleans up after we don't want talk
229 * to the mouse anymore.
232 static void sermouse_disconnect(struct serio *serio)
234 struct sermouse *sermouse = serio_get_drvdata(serio);
237 serio_set_drvdata(serio, NULL);
238 input_unregister_device(sermouse->dev);
243 * sermouse_connect() is a callback form the serio module when
244 * an unhandled serio port is found.
247 static int sermouse_connect(struct serio *serio, struct serio_driver *drv)
249 struct sermouse *sermouse;
250 struct input_dev *input_dev;
251 unsigned char c = serio->id.extra;
254 sermouse = kzalloc(sizeof(struct sermouse), GFP_KERNEL);
255 input_dev = input_allocate_device();
256 if (!sermouse || !input_dev)
259 sermouse->dev = input_dev;
260 snprintf(sermouse->phys, sizeof(sermouse->phys), "%s/input0", serio->phys);
261 sermouse->type = serio->id.proto;
263 input_dev->name = sermouse_protocols[sermouse->type];
264 input_dev->phys = sermouse->phys;
265 input_dev->id.bustype = BUS_RS232;
266 input_dev->id.vendor = sermouse->type;
267 input_dev->id.product = c;
268 input_dev->id.version = 0x0100;
269 input_dev->dev.parent = &serio->dev;
271 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
272 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
274 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
276 if (c & 0x01) set_bit(BTN_MIDDLE, input_dev->keybit);
277 if (c & 0x02) set_bit(BTN_SIDE, input_dev->keybit);
278 if (c & 0x04) set_bit(BTN_EXTRA, input_dev->keybit);
279 if (c & 0x10) set_bit(REL_WHEEL, input_dev->relbit);
280 if (c & 0x20) set_bit(REL_HWHEEL, input_dev->relbit);
282 serio_set_drvdata(serio, sermouse);
284 err = serio_open(serio, drv);
288 err = input_register_device(sermouse->dev);
294 fail3: serio_close(serio);
295 fail2: serio_set_drvdata(serio, NULL);
296 fail1: input_free_device(input_dev);
301 static struct serio_device_id sermouse_serio_ids[] = {
347 MODULE_DEVICE_TABLE(serio, sermouse_serio_ids);
349 static struct serio_driver sermouse_drv = {
353 .description = DRIVER_DESC,
354 .id_table = sermouse_serio_ids,
355 .interrupt = sermouse_interrupt,
356 .connect = sermouse_connect,
357 .disconnect = sermouse_disconnect,
360 static int __init sermouse_init(void)
362 return serio_register_driver(&sermouse_drv);
365 static void __exit sermouse_exit(void)
367 serio_unregister_driver(&sermouse_drv);
370 module_init(sermouse_init);
371 module_exit(sermouse_exit);