blob: 3e8fb8136452e3fc19c4530189b559facd10c600 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 1999-2001 Vojtech Pavlik
3 */
4
5/*
6 * Serial mouse driver for Linux
7 */
8
9/*
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.
14 *
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.
19 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
25#include <linux/delay.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/interrupt.h>
29#include <linux/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/serio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define DRIVER_DESC "Serial mouse driver"
33
34MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
35MODULE_DESCRIPTION(DRIVER_DESC);
36MODULE_LICENSE("GPL");
37
Helge Dellere38de672006-09-10 21:54:39 -040038static const char *sermouse_protocols[] = { "None", "Mouse Systems Mouse", "Sun Mouse", "Microsoft Mouse",
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 "Logitech M+ Mouse", "Microsoft MZ Mouse", "Logitech MZ+ Mouse",
40 "Logitech MZ++ Mouse"};
41
42struct sermouse {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -050043 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 signed char buf[8];
45 unsigned char count;
46 unsigned char type;
47 unsigned long last;
48 char phys[32];
49};
50
51/*
52 * sermouse_process_msc() analyzes the incoming MSC/Sun bytestream and
53 * applies some prediction to the data, resulting in 96 updates per
54 * second, which is as good as a PS/2 or USB mouse.
55 */
56
David Howells7d12e782006-10-05 14:55:46 +010057static void sermouse_process_msc(struct sermouse *sermouse, signed char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -050059 struct input_dev *dev = sermouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 signed char *buf = sermouse->buf;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 switch (sermouse->count) {
63
64 case 0:
Peter Osterlund867d2682007-03-16 00:58:37 -040065 if ((data & 0xf8) != 0x80)
66 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 input_report_key(dev, BTN_LEFT, !(data & 4));
68 input_report_key(dev, BTN_RIGHT, !(data & 1));
69 input_report_key(dev, BTN_MIDDLE, !(data & 2));
70 break;
71
72 case 1:
73 case 3:
74 input_report_rel(dev, REL_X, data / 2);
75 input_report_rel(dev, REL_Y, -buf[1]);
76 buf[0] = data - data / 2;
77 break;
78
79 case 2:
80 case 4:
81 input_report_rel(dev, REL_X, buf[0]);
82 input_report_rel(dev, REL_Y, buf[1] - data);
83 buf[1] = data / 2;
84 break;
85 }
86
87 input_sync(dev);
88
David S. Miller68ca2432005-12-28 13:27:04 -080089 if (++sermouse->count == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 sermouse->count = 0;
91}
92
93/*
94 * sermouse_process_ms() anlyzes the incoming MS(Z/+/++) bytestream and
95 * generates events. With prediction it gets 80 updates/sec, assuming
96 * standard 3-byte packets and 1200 bps.
97 */
98
David Howells7d12e782006-10-05 14:55:46 +010099static void sermouse_process_ms(struct sermouse *sermouse, signed char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500101 struct input_dev *dev = sermouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 signed char *buf = sermouse->buf;
103
Peter Osterlund867d2682007-03-16 00:58:37 -0400104 if (data & 0x40)
105 sermouse->count = 0;
106 else if (sermouse->count == 0)
107 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 switch (sermouse->count) {
110
111 case 0:
112 buf[1] = data;
113 input_report_key(dev, BTN_LEFT, (data >> 5) & 1);
114 input_report_key(dev, BTN_RIGHT, (data >> 4) & 1);
115 break;
116
117 case 1:
118 buf[2] = data;
119 data = (signed char) (((buf[1] << 6) & 0xc0) | (data & 0x3f));
120 input_report_rel(dev, REL_X, data / 2);
121 input_report_rel(dev, REL_Y, buf[4]);
122 buf[3] = data - data / 2;
123 break;
124
125 case 2:
126 /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */
127 if ((sermouse->type == SERIO_MS) && !data && !buf[2] && !((buf[0] & 0xf0) ^ buf[1]))
128 input_report_key(dev, BTN_MIDDLE, !test_bit(BTN_MIDDLE, dev->key));
129 buf[0] = buf[1];
130
131 data = (signed char) (((buf[1] << 4) & 0xc0) | (data & 0x3f));
132 input_report_rel(dev, REL_X, buf[3]);
133 input_report_rel(dev, REL_Y, data - buf[4]);
134 buf[4] = data / 2;
135 break;
136
137 case 3:
138
139 switch (sermouse->type) {
140
141 case SERIO_MS:
Gustavo A. R. Silva17a4ed52018-08-06 15:31:02 -0700142 sermouse->type = SERIO_MP;
143 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 case SERIO_MP:
146 if ((data >> 2) & 3) break; /* M++ Wireless Extension packet. */
147 input_report_key(dev, BTN_MIDDLE, (data >> 5) & 1);
148 input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
149 break;
150
151 case SERIO_MZP:
152 case SERIO_MZPP:
153 input_report_key(dev, BTN_SIDE, (data >> 5) & 1);
Gustavo A. R. Silva17a4ed52018-08-06 15:31:02 -0700154 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 case SERIO_MZ:
157 input_report_key(dev, BTN_MIDDLE, (data >> 4) & 1);
158 input_report_rel(dev, REL_WHEEL, (data & 8) - (data & 7));
159 break;
160 }
161
162 break;
163
164 case 4:
165 case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */
166 buf[1] = (data >> 2) & 0x0f;
167 break;
168
169 case 5:
170 case 7: /* Ignore anything besides MZ++ */
Peter Osterlund867d2682007-03-16 00:58:37 -0400171 if (sermouse->type != SERIO_MZPP)
172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
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
Peter Osterlund867d2682007-03-16 00:58:37 -0400209 if (time_after(jiffies, sermouse->last + HZ/10))
210 sermouse->count = 0;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 sermouse->last = jiffies;
213
214 if (sermouse->type > SERIO_SUN)
David Howells7d12e782006-10-05 14:55:46 +0100215 sermouse_process_ms(sermouse, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 else
David Howells7d12e782006-10-05 14:55:46 +0100217 sermouse_process_msc(sermouse, data);
Peter Osterlund867d2682007-03-16 00:58:37 -0400218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return IRQ_HANDLED;
220}
221
222/*
223 * sermouse_disconnect() cleans up after we don't want talk
224 * to the mouse anymore.
225 */
226
227static void sermouse_disconnect(struct serio *serio)
228{
229 struct sermouse *sermouse = serio_get_drvdata(serio);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 serio_close(serio);
232 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500233 input_unregister_device(sermouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 kfree(sermouse);
235}
236
237/*
238 * sermouse_connect() is a callback form the serio module when
239 * an unhandled serio port is found.
240 */
241
242static int sermouse_connect(struct serio *serio, struct serio_driver *drv)
243{
244 struct sermouse *sermouse;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500245 struct input_dev *input_dev;
246 unsigned char c = serio->id.extra;
247 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500249 sermouse = kzalloc(sizeof(struct sermouse), GFP_KERNEL);
250 input_dev = input_allocate_device();
251 if (!sermouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -0500252 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500254 sermouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -0400255 snprintf(sermouse->phys, sizeof(sermouse->phys), "%s/input0", serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500256 sermouse->type = serio->id.proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500258 input_dev->name = sermouse_protocols[sermouse->type];
259 input_dev->phys = sermouse->phys;
260 input_dev->id.bustype = BUS_RS232;
261 input_dev->id.vendor = sermouse->type;
262 input_dev->id.product = c;
263 input_dev->id.version = 0x0100;
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -0400264 input_dev->dev.parent = &serio->dev;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500265
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700266 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
267 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
268 BIT_MASK(BTN_RIGHT);
269 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500270
271 if (c & 0x01) set_bit(BTN_MIDDLE, input_dev->keybit);
272 if (c & 0x02) set_bit(BTN_SIDE, input_dev->keybit);
273 if (c & 0x04) set_bit(BTN_EXTRA, input_dev->keybit);
274 if (c & 0x10) set_bit(REL_WHEEL, input_dev->relbit);
275 if (c & 0x20) set_bit(REL_HWHEEL, input_dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 serio_set_drvdata(serio, sermouse);
278
279 err = serio_open(serio, drv);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500280 if (err)
Dmitry Torokhov72155612006-11-05 22:40:19 -0500281 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Dmitry Torokhov72155612006-11-05 22:40:19 -0500283 err = input_register_device(sermouse->dev);
284 if (err)
285 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 return 0;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500288
Dmitry Torokhov72155612006-11-05 22:40:19 -0500289 fail3: serio_close(serio);
290 fail2: serio_set_drvdata(serio, NULL);
291 fail1: input_free_device(input_dev);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500292 kfree(sermouse);
293 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296static struct serio_device_id sermouse_serio_ids[] = {
297 {
298 .type = SERIO_RS232,
299 .proto = SERIO_MSC,
300 .id = SERIO_ANY,
301 .extra = SERIO_ANY,
302 },
303 {
304 .type = SERIO_RS232,
305 .proto = SERIO_SUN,
306 .id = SERIO_ANY,
307 .extra = SERIO_ANY,
308 },
309 {
310 .type = SERIO_RS232,
311 .proto = SERIO_MS,
312 .id = SERIO_ANY,
313 .extra = SERIO_ANY,
314 },
315 {
316 .type = SERIO_RS232,
317 .proto = SERIO_MP,
318 .id = SERIO_ANY,
319 .extra = SERIO_ANY,
320 },
321 {
322 .type = SERIO_RS232,
323 .proto = SERIO_MZ,
324 .id = SERIO_ANY,
325 .extra = SERIO_ANY,
326 },
327 {
328 .type = SERIO_RS232,
329 .proto = SERIO_MZP,
330 .id = SERIO_ANY,
331 .extra = SERIO_ANY,
332 },
333 {
334 .type = SERIO_RS232,
335 .proto = SERIO_MZPP,
336 .id = SERIO_ANY,
337 .extra = SERIO_ANY,
338 },
339 { 0 }
340};
341
342MODULE_DEVICE_TABLE(serio, sermouse_serio_ids);
343
344static struct serio_driver sermouse_drv = {
345 .driver = {
346 .name = "sermouse",
347 },
348 .description = DRIVER_DESC,
349 .id_table = sermouse_serio_ids,
350 .interrupt = sermouse_interrupt,
351 .connect = sermouse_connect,
352 .disconnect = sermouse_disconnect,
353};
354
Axel Lin65ac9f72012-04-03 23:50:17 -0700355module_serio_driver(sermouse_drv);