ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / input / mouse / sermouse.c
1 /*
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>
36 #include <linux/config.h>
37 #include <linux/serio.h>
38 #include <linux/init.h>
39
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION("Serial mouse driver");
42 MODULE_LICENSE("GPL");
43
44 static char *sermouse_protocols[] = { "None", "Mouse Systems Mouse", "Sun Mouse", "Microsoft Mouse",
45                                         "Logitech M+ Mouse", "Microsoft MZ Mouse", "Logitech MZ+ Mouse",
46                                         "Logitech MZ++ Mouse"};
47
48 struct sermouse {
49         struct input_dev dev;
50         signed char buf[8];
51         unsigned char count;
52         unsigned char type;
53         unsigned long last;
54         char phys[32];
55 };
56
57 /*
58  * sermouse_process_msc() analyzes the incoming MSC/Sun bytestream and
59  * applies some prediction to the data, resulting in 96 updates per
60  * second, which is as good as a PS/2 or USB mouse.
61  */
62
63 static void sermouse_process_msc(struct sermouse *sermouse, signed char data, struct pt_regs *regs)
64 {
65         struct input_dev *dev = &sermouse->dev;
66         signed char *buf = sermouse->buf;
67
68         input_regs(dev, regs);
69
70         switch (sermouse->count) {
71
72                 case 0:
73                         if ((data & 0xf8) != 0x80) return;
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));
77                         break;
78
79                 case 1: 
80                 case 3: 
81                         input_report_rel(dev, REL_X, data / 2);
82                         input_report_rel(dev, REL_Y, -buf[1]);
83                         buf[0] = data - data / 2;
84                         break;
85
86                 case 2: 
87                 case 4:
88                         input_report_rel(dev, REL_X, buf[0]);
89                         input_report_rel(dev, REL_Y, buf[1] - data);
90                         buf[1] = data / 2;
91                         break;
92         }
93
94         input_sync(dev);
95
96         if (++sermouse->count == (5 - ((sermouse->type == SERIO_SUN) << 1)))
97                 sermouse->count = 0;
98 }
99
100 /*
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.
104  */
105
106 static void sermouse_process_ms(struct sermouse *sermouse, signed char data, struct pt_regs *regs)
107 {
108         struct input_dev *dev = &sermouse->dev;
109         signed char *buf = sermouse->buf;
110
111         if (data & 0x40) sermouse->count = 0;
112
113         input_regs(dev, regs);
114
115         switch (sermouse->count) {
116
117                 case 0:
118                         buf[1] = data;
119                         input_report_key(dev, BTN_LEFT,   (data >> 5) & 1);
120                         input_report_key(dev, BTN_RIGHT,  (data >> 4) & 1);
121                         break;
122
123                 case 1:
124                         buf[2] = data;
125                         data = (signed char) (((buf[1] << 6) & 0xc0) | (data & 0x3f));
126                         input_report_rel(dev, REL_X, data / 2);
127                         input_report_rel(dev, REL_Y, buf[4]);
128                         buf[3] = data - data / 2;
129                         break;
130
131                 case 2:
132                         /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */
133                         if ((sermouse->type == SERIO_MS) && !data && !buf[2] && !((buf[0] & 0xf0) ^ buf[1]))
134                                 input_report_key(dev, BTN_MIDDLE, !test_bit(BTN_MIDDLE, dev->key));
135                         buf[0] = buf[1];
136
137                         data = (signed char) (((buf[1] << 4) & 0xc0) | (data & 0x3f));
138                         input_report_rel(dev, REL_X, buf[3]);
139                         input_report_rel(dev, REL_Y, data - buf[4]);
140                         buf[4] = data / 2;
141                         break;
142
143                 case 3:
144
145                         switch (sermouse->type) {
146                         
147                                 case SERIO_MS:
148                                          sermouse->type = SERIO_MP;
149
150                                 case SERIO_MP:
151                                         if ((data >> 2) & 3) break;     /* M++ Wireless Extension packet. */
152                                         input_report_key(dev, BTN_MIDDLE, (data >> 5) & 1);
153                                         input_report_key(dev, BTN_SIDE,   (data >> 4) & 1);
154                                         break;
155
156                                 case SERIO_MZP:
157                                 case SERIO_MZPP:
158                                         input_report_key(dev, BTN_SIDE,   (data >> 5) & 1);
159
160                                 case SERIO_MZ:
161                                         input_report_key(dev, BTN_MIDDLE, (data >> 4) & 1);
162                                         input_report_rel(dev, REL_WHEEL,  (data & 8) - (data & 7));
163                                         break;
164                         }
165                                         
166                         break;
167
168                 case 4:
169                 case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */
170                         buf[1] = (data >> 2) & 0x0f;
171                         break;
172
173                 case 5:
174                 case 7: /* Ignore anything besides MZ++ */
175                         if (sermouse->type != SERIO_MZPP) break;
176
177                         switch (buf[1]) {
178
179                                 case 1: /* Extra mouse info */
180
181                                         input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
182                                         input_report_key(dev, BTN_EXTRA, (data >> 5) & 1);
183                                         input_report_rel(dev, data & 0x80 ? REL_HWHEEL : REL_WHEEL, (data & 7) - (data & 8));
184
185                                         break;
186
187                                 default: /* We don't decode anything else yet. */
188
189                                         printk(KERN_WARNING
190                                                 "sermouse.c: Received MZ++ packet %x, don't know how to handle.\n", buf[1]);
191                                         break;
192                         }
193
194                         break;
195         }
196
197         input_sync(dev);
198
199         sermouse->count++;
200 }
201
202 /*
203  * sermouse_interrupt() handles incoming characters, either gathering them into
204  * packets or passing them to the command routine as command output.
205  */
206
207 static irqreturn_t sermouse_interrupt(struct serio *serio,
208                 unsigned char data, unsigned int flags, struct pt_regs *regs)
209 {
210         struct sermouse *sermouse = serio->private;
211
212         if (time_after(jiffies, sermouse->last + HZ/10)) sermouse->count = 0;
213         sermouse->last = jiffies;
214
215         if (sermouse->type > SERIO_SUN)
216                 sermouse_process_ms(sermouse, data, regs);
217         else
218                 sermouse_process_msc(sermouse, data, regs);
219         return IRQ_HANDLED;
220 }
221
222 /*
223  * sermouse_disconnect() cleans up after we don't want talk
224  * to the mouse anymore.
225  */
226
227 static void sermouse_disconnect(struct serio *serio)
228 {
229         struct sermouse *sermouse = serio->private;
230         input_unregister_device(&sermouse->dev);
231         serio_close(serio);
232         kfree(sermouse);
233 }
234
235 /*
236  * sermouse_connect() is a callback form the serio module when
237  * an unhandled serio port is found.
238  */
239
240 static void sermouse_connect(struct serio *serio, struct serio_dev *dev)
241 {
242         struct sermouse *sermouse;
243         unsigned char c;
244         
245         if ((serio->type & SERIO_TYPE) != SERIO_RS232)
246                 return;
247
248         if (!(serio->type & SERIO_PROTO) || ((serio->type & SERIO_PROTO) > SERIO_MZPP))
249                 return;
250
251         if (!(sermouse = kmalloc(sizeof(struct sermouse), GFP_KERNEL)))
252                 return;
253
254         memset(sermouse, 0, sizeof(struct sermouse));
255
256         init_input_dev(&sermouse->dev);
257         sermouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
258         sermouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
259         sermouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
260         sermouse->dev.private = sermouse;
261
262         serio->private = sermouse;
263
264         sermouse->type = serio->type & SERIO_PROTO;
265         c = (serio->type & SERIO_EXTRA) >> 16;
266
267         if (c & 0x01) set_bit(BTN_MIDDLE, sermouse->dev.keybit);
268         if (c & 0x02) set_bit(BTN_SIDE, sermouse->dev.keybit);
269         if (c & 0x04) set_bit(BTN_EXTRA, sermouse->dev.keybit);
270         if (c & 0x10) set_bit(REL_WHEEL, sermouse->dev.relbit);
271         if (c & 0x20) set_bit(REL_HWHEEL, sermouse->dev.relbit);
272
273         sprintf(sermouse->phys, "%s/input0", serio->phys);
274
275         sermouse->dev.name = sermouse_protocols[sermouse->type];
276         sermouse->dev.phys = sermouse->phys;
277         sermouse->dev.id.bustype = BUS_RS232;
278         sermouse->dev.id.vendor = sermouse->type;
279         sermouse->dev.id.product = c;
280         sermouse->dev.id.version = 0x0100;
281
282         if (serio_open(serio, dev)) {
283                 kfree(sermouse);
284                 return;
285         }
286
287         input_register_device(&sermouse->dev);
288         
289         printk(KERN_INFO "input: %s on %s\n", sermouse_protocols[sermouse->type], serio->phys);
290 }
291
292 static struct serio_dev sermouse_dev = {
293         .interrupt =    sermouse_interrupt,
294         .connect =      sermouse_connect,
295         .disconnect =   sermouse_disconnect
296 };
297
298 int __init sermouse_init(void)
299 {
300         serio_register_device(&sermouse_dev);
301         return 0;
302 }
303
304 void __exit sermouse_exit(void)
305 {
306         serio_unregister_device(&sermouse_dev);
307 }
308
309 module_init(sermouse_init);
310 module_exit(sermouse_exit);