blob: 2a272c5daf08b2284f4a6f24f10e70813c38c822 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * $Id: sermouse.c,v 1.17 2002/03/13 10:03:43 vojtech Exp $
3 *
4 * Copyright (c) 1999-2001 Vojtech Pavlik
5 */
6
7/*
8 * Serial mouse driver for Linux
9 */
10
11/*
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.
16 *
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.
21 *
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
25 *
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
29 */
30
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/serio.h>
37#include <linux/init.h>
38
39#define DRIVER_DESC "Serial mouse driver"
40
41MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42MODULE_DESCRIPTION(DRIVER_DESC);
43MODULE_LICENSE("GPL");
44
Helge Dellere38de672006-09-10 21:54:39 -040045static const char *sermouse_protocols[] = { "None", "Mouse Systems Mouse", "Sun Mouse", "Microsoft Mouse",
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 "Logitech M+ Mouse", "Microsoft MZ Mouse", "Logitech MZ+ Mouse",
47 "Logitech MZ++ Mouse"};
48
49struct sermouse {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -050050 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 signed char buf[8];
52 unsigned char count;
53 unsigned char type;
54 unsigned long last;
55 char phys[32];
56};
57
58/*
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.
62 */
63
David Howells7d12e782006-10-05 14:55:46 +010064static void sermouse_process_msc(struct sermouse *sermouse, signed char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -050066 struct input_dev *dev = sermouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 signed char *buf = sermouse->buf;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 switch (sermouse->count) {
70
71 case 0:
72 if ((data & 0xf8) != 0x80) return;
73 input_report_key(dev, BTN_LEFT, !(data & 4));
74 input_report_key(dev, BTN_RIGHT, !(data & 1));
75 input_report_key(dev, BTN_MIDDLE, !(data & 2));
76 break;
77
78 case 1:
79 case 3:
80 input_report_rel(dev, REL_X, data / 2);
81 input_report_rel(dev, REL_Y, -buf[1]);
82 buf[0] = data - data / 2;
83 break;
84
85 case 2:
86 case 4:
87 input_report_rel(dev, REL_X, buf[0]);
88 input_report_rel(dev, REL_Y, buf[1] - data);
89 buf[1] = data / 2;
90 break;
91 }
92
93 input_sync(dev);
94
David S. Miller68ca2432005-12-28 13:27:04 -080095 if (++sermouse->count == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 sermouse->count = 0;
97}
98
99/*
100 * sermouse_process_ms() anlyzes the incoming MS(Z/+/++) bytestream and
101 * generates events. With prediction it gets 80 updates/sec, assuming
102 * standard 3-byte packets and 1200 bps.
103 */
104
David Howells7d12e782006-10-05 14:55:46 +0100105static void sermouse_process_ms(struct sermouse *sermouse, signed char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500107 struct input_dev *dev = sermouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 signed char *buf = sermouse->buf;
109
110 if (data & 0x40) sermouse->count = 0;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 switch (sermouse->count) {
113
114 case 0:
115 buf[1] = data;
116 input_report_key(dev, BTN_LEFT, (data >> 5) & 1);
117 input_report_key(dev, BTN_RIGHT, (data >> 4) & 1);
118 break;
119
120 case 1:
121 buf[2] = data;
122 data = (signed char) (((buf[1] << 6) & 0xc0) | (data & 0x3f));
123 input_report_rel(dev, REL_X, data / 2);
124 input_report_rel(dev, REL_Y, buf[4]);
125 buf[3] = data - data / 2;
126 break;
127
128 case 2:
129 /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */
130 if ((sermouse->type == SERIO_MS) && !data && !buf[2] && !((buf[0] & 0xf0) ^ buf[1]))
131 input_report_key(dev, BTN_MIDDLE, !test_bit(BTN_MIDDLE, dev->key));
132 buf[0] = buf[1];
133
134 data = (signed char) (((buf[1] << 4) & 0xc0) | (data & 0x3f));
135 input_report_rel(dev, REL_X, buf[3]);
136 input_report_rel(dev, REL_Y, data - buf[4]);
137 buf[4] = data / 2;
138 break;
139
140 case 3:
141
142 switch (sermouse->type) {
143
144 case SERIO_MS:
145 sermouse->type = SERIO_MP;
146
147 case SERIO_MP:
148 if ((data >> 2) & 3) break; /* M++ Wireless Extension packet. */
149 input_report_key(dev, BTN_MIDDLE, (data >> 5) & 1);
150 input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
151 break;
152
153 case SERIO_MZP:
154 case SERIO_MZPP:
155 input_report_key(dev, BTN_SIDE, (data >> 5) & 1);
156
157 case SERIO_MZ:
158 input_report_key(dev, BTN_MIDDLE, (data >> 4) & 1);
159 input_report_rel(dev, REL_WHEEL, (data & 8) - (data & 7));
160 break;
161 }
162
163 break;
164
165 case 4:
166 case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */
167 buf[1] = (data >> 2) & 0x0f;
168 break;
169
170 case 5:
171 case 7: /* Ignore anything besides MZ++ */
172 if (sermouse->type != SERIO_MZPP) break;
173
174 switch (buf[1]) {
175
176 case 1: /* Extra mouse info */
177
178 input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
179 input_report_key(dev, BTN_EXTRA, (data >> 5) & 1);
180 input_report_rel(dev, data & 0x80 ? REL_HWHEEL : REL_WHEEL, (data & 7) - (data & 8));
181
182 break;
183
184 default: /* We don't decode anything else yet. */
185
186 printk(KERN_WARNING
187 "sermouse.c: Received MZ++ packet %x, don't know how to handle.\n", buf[1]);
188 break;
189 }
190
191 break;
192 }
193
194 input_sync(dev);
195
196 sermouse->count++;
197}
198
199/*
200 * sermouse_interrupt() handles incoming characters, either gathering them into
201 * packets or passing them to the command routine as command output.
202 */
203
204static irqreturn_t sermouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100205 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 struct sermouse *sermouse = serio_get_drvdata(serio);
208
209 if (time_after(jiffies, sermouse->last + HZ/10)) sermouse->count = 0;
210 sermouse->last = jiffies;
211
212 if (sermouse->type > SERIO_SUN)
David Howells7d12e782006-10-05 14:55:46 +0100213 sermouse_process_ms(sermouse, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 else
David Howells7d12e782006-10-05 14:55:46 +0100215 sermouse_process_msc(sermouse, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return IRQ_HANDLED;
217}
218
219/*
220 * sermouse_disconnect() cleans up after we don't want talk
221 * to the mouse anymore.
222 */
223
224static void sermouse_disconnect(struct serio *serio)
225{
226 struct sermouse *sermouse = serio_get_drvdata(serio);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 serio_close(serio);
229 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500230 input_unregister_device(sermouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 kfree(sermouse);
232}
233
234/*
235 * sermouse_connect() is a callback form the serio module when
236 * an unhandled serio port is found.
237 */
238
239static int sermouse_connect(struct serio *serio, struct serio_driver *drv)
240{
241 struct sermouse *sermouse;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500242 struct input_dev *input_dev;
243 unsigned char c = serio->id.extra;
244 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500246 sermouse = kzalloc(sizeof(struct sermouse), GFP_KERNEL);
247 input_dev = input_allocate_device();
248 if (!sermouse || !input_dev)
249 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500251 sermouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -0400252 snprintf(sermouse->phys, sizeof(sermouse->phys), "%s/input0", serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500253 sermouse->type = serio->id.proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500255 input_dev->name = sermouse_protocols[sermouse->type];
256 input_dev->phys = sermouse->phys;
257 input_dev->id.bustype = BUS_RS232;
258 input_dev->id.vendor = sermouse->type;
259 input_dev->id.product = c;
260 input_dev->id.version = 0x0100;
261 input_dev->cdev.dev = &serio->dev;
262
263 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
264 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
265 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
266 input_dev->private = sermouse;
267
268 if (c & 0x01) set_bit(BTN_MIDDLE, input_dev->keybit);
269 if (c & 0x02) set_bit(BTN_SIDE, input_dev->keybit);
270 if (c & 0x04) set_bit(BTN_EXTRA, input_dev->keybit);
271 if (c & 0x10) set_bit(REL_WHEEL, input_dev->relbit);
272 if (c & 0x20) set_bit(REL_HWHEEL, input_dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 serio_set_drvdata(serio, sermouse);
275
276 err = serio_open(serio, drv);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500277 if (err)
278 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500280 input_register_device(sermouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 return 0;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500283
284 fail: serio_set_drvdata(serio, NULL);
285 input_free_device(input_dev);
286 kfree(sermouse);
287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static struct serio_device_id sermouse_serio_ids[] = {
291 {
292 .type = SERIO_RS232,
293 .proto = SERIO_MSC,
294 .id = SERIO_ANY,
295 .extra = SERIO_ANY,
296 },
297 {
298 .type = SERIO_RS232,
299 .proto = SERIO_SUN,
300 .id = SERIO_ANY,
301 .extra = SERIO_ANY,
302 },
303 {
304 .type = SERIO_RS232,
305 .proto = SERIO_MS,
306 .id = SERIO_ANY,
307 .extra = SERIO_ANY,
308 },
309 {
310 .type = SERIO_RS232,
311 .proto = SERIO_MP,
312 .id = SERIO_ANY,
313 .extra = SERIO_ANY,
314 },
315 {
316 .type = SERIO_RS232,
317 .proto = SERIO_MZ,
318 .id = SERIO_ANY,
319 .extra = SERIO_ANY,
320 },
321 {
322 .type = SERIO_RS232,
323 .proto = SERIO_MZP,
324 .id = SERIO_ANY,
325 .extra = SERIO_ANY,
326 },
327 {
328 .type = SERIO_RS232,
329 .proto = SERIO_MZPP,
330 .id = SERIO_ANY,
331 .extra = SERIO_ANY,
332 },
333 { 0 }
334};
335
336MODULE_DEVICE_TABLE(serio, sermouse_serio_ids);
337
338static struct serio_driver sermouse_drv = {
339 .driver = {
340 .name = "sermouse",
341 },
342 .description = DRIVER_DESC,
343 .id_table = sermouse_serio_ids,
344 .interrupt = sermouse_interrupt,
345 .connect = sermouse_connect,
346 .disconnect = sermouse_disconnect,
347};
348
349static int __init sermouse_init(void)
350{
351 serio_register_driver(&sermouse_drv);
352 return 0;
353}
354
355static void __exit sermouse_exit(void)
356{
357 serio_unregister_driver(&sermouse_drv);
358}
359
360module_init(sermouse_init);
361module_exit(sermouse_exit);