vserver 1.9.3
[linux-2.6.git] / drivers / input / mouse / vsxxxaa.c
1 /*
2  * DEC VSXXX-AA and VSXXX-GA mouse driver.
3  *
4  * Copyright (C) 2003-2004 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
5  *
6  * The packet format was taken from a patch to GPM which is (C) 2001
7  * by   Karsten Merker <merker@linuxtag.org>
8  * and  Maciej W. Rozycki <macro@ds2.pg.gda.pl>
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
27 /*
28  * Building an adaptor to DB9 / DB25 RS232
29  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30  *
31  * DISCLAIMER: Use this description AT YOUR OWN RISK! I'll not pay for
32  * anything if you break your mouse, your computer or whatever!
33  *
34  * In theory, this mouse is a simple RS232 device. In practice, it has got
35  * a quite uncommon plug and the requirement to additionally get a power
36  * supply at +5V and -12V.
37  *
38  * If you look at the socket/jack (_not_ at the plug), we use this pin
39  * numbering:
40  *    _______
41  *   / 7 6 5 \
42  *  | 4 --- 3 |
43  *   \  2 1  /
44  *    -------
45  * 
46  *      DEC socket      DB9     DB25    Note
47  *      1 (GND)         5       7       -
48  *      2 (RxD)         2       3       -
49  *      3 (TxD)         3       2       -
50  *      4 (-12V)        -       -       Somewhere from the PSU. At ATX, it's
51  *                                      the thin blue wire at pin 12 of the
52  *                                      ATX power connector. Only required for
53  *                                      VSXXX-AA/-GA mice.
54  *      5 (+5V)         -       -       PSU (red wires of ATX power connector
55  *                                      on pin 4, 6, 19 or 20) or HDD power
56  *                                      connector (also red wire).
57  *      6 (+12V)        -       -       HDD power connector, yellow wire. Only
58  *                                      required for VSXXX-AB digitizer.
59  *      7 (dev. avail.) -       -       The mouse shorts this one to pin 1.
60  *                                      This way, the host computer can detect
61  *                                      the mouse. To use it with the adaptor,
62  *                                      simply don't connect this pin.
63  *
64  * So to get a working adaptor, you need to connect the mouse with three
65  * wires to a RS232 port and two or three additional wires for +5V, +12V and
66  * -12V to the PSU.
67  *
68  * Flow specification for the link is 4800, 8o1.
69  *
70  * The mice and tablet are described in "VCB02 Video Subsystem - Technical
71  * Manual", DEC EK-104AA-TM-001. You'll find it at MANX, a search engine
72  * specific for DEC documentation. Try
73  * http://www.vt100.net/manx/details?pn=EK-104AA-TM-001;id=21;cp=1
74  */
75
76 #include <linux/delay.h>
77 #include <linux/module.h>
78 #include <linux/slab.h>
79 #include <linux/interrupt.h>
80 #include <linux/input.h>
81 #include <linux/config.h>
82 #include <linux/serio.h>
83 #include <linux/init.h>
84
85 #define DRIVER_DESC     "Serial DEC VSXXX-AA/GA mouse / DEC tablet driver"
86
87 MODULE_AUTHOR ("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
88 MODULE_DESCRIPTION (DRIVER_DESC);
89 MODULE_LICENSE ("GPL");
90
91 #undef VSXXXAA_DEBUG
92 #ifdef VSXXXAA_DEBUG
93 #define DBG(x...) printk (x)
94 #else
95 #define DBG(x...) do {} while (0)
96 #endif
97
98 #define VSXXXAA_INTRO_MASK      0x80
99 #define VSXXXAA_INTRO_HEAD      0x80
100 #define IS_HDR_BYTE(x)          (((x) & VSXXXAA_INTRO_MASK)     \
101                                         == VSXXXAA_INTRO_HEAD)
102
103 #define VSXXXAA_PACKET_MASK     0xe0
104 #define VSXXXAA_PACKET_REL      0x80
105 #define VSXXXAA_PACKET_ABS      0xc0
106 #define VSXXXAA_PACKET_POR      0xa0
107 #define MATCH_PACKET_TYPE(data, type)   (((data) & VSXXXAA_PACKET_MASK) == type)
108
109
110
111 struct vsxxxaa {
112         struct input_dev dev;
113         struct serio *serio;
114 #define BUFLEN 15 /* At least 5 is needed for a full tablet packet */
115         unsigned char buf[BUFLEN];
116         unsigned char count;
117         unsigned char version;
118         unsigned char country;
119         unsigned char type;
120         char name[64];
121         char phys[32];
122 };
123
124 static void
125 vsxxxaa_drop_bytes (struct vsxxxaa *mouse, int num)
126 {
127         if (num >= mouse->count)
128                 mouse->count = 0;
129         else {
130                 memmove (mouse->buf, mouse->buf + num - 1, BUFLEN - num);
131                 mouse->count -= num;
132         }
133 }
134
135 static void
136 vsxxxaa_queue_byte (struct vsxxxaa *mouse, unsigned char byte)
137 {
138         if (mouse->count == BUFLEN) {
139                 printk (KERN_ERR "%s on %s: Dropping a byte of full buffer.\n",
140                                 mouse->name, mouse->phys);
141                 vsxxxaa_drop_bytes (mouse, 1);
142         }
143         DBG (KERN_INFO "Queueing byte 0x%02x\n", byte);
144
145         mouse->buf[mouse->count++] = byte;
146 }
147
148 static void
149 vsxxxaa_detection_done (struct vsxxxaa *mouse)
150 {
151         switch (mouse->type) {
152                 case 0x02:
153                         sprintf (mouse->name, "DEC VSXXX-AA/GA mouse");
154                         break;
155
156                 case 0x04:
157                         sprintf (mouse->name, "DEC VSXXX-AB digitizer");
158                         break;
159
160                 default:
161                         sprintf (mouse->name, "unknown DEC pointer device");
162                         break;
163         }
164
165         printk (KERN_INFO "Found %s version 0x%02x from country 0x%02x "
166                         "on port %s\n", mouse->name, mouse->version,
167                         mouse->country, mouse->phys);
168 }
169
170 /*
171  * Returns number of bytes to be dropped, 0 if packet is okay.
172  */
173 static int
174 vsxxxaa_check_packet (struct vsxxxaa *mouse, int packet_len)
175 {
176         int i;
177
178         /* First byte must be a header byte */
179         if (!IS_HDR_BYTE (mouse->buf[0])) {
180                 DBG ("vsck: len=%d, 1st=0x%02x\n", packet_len, mouse->buf[0]);
181                 return 1;
182         }
183
184         /* Check all following bytes */
185         if (packet_len > 1) {
186                 for (i = 1; i < packet_len; i++) {
187                         if (IS_HDR_BYTE (mouse->buf[i])) {
188                                 printk (KERN_ERR "Need to drop %d bytes "
189                                                 "of a broken packet.\n",
190                                                 i - 1);
191                                 DBG (KERN_INFO "check: len=%d, b[%d]=0x%02x\n",
192                                                 packet_len, i, mouse->buf[i]);
193                                 return i - 1;
194                         }
195                 }
196         }
197
198         return 0;
199 }
200
201 static __inline__ int
202 vsxxxaa_smells_like_packet (struct vsxxxaa *mouse, unsigned char type, size_t len)
203 {
204         return (mouse->count >= len) && MATCH_PACKET_TYPE (mouse->buf[0], type);
205 }
206
207 static void
208 vsxxxaa_handle_REL_packet (struct vsxxxaa *mouse, struct pt_regs *regs)
209 {
210         struct input_dev *dev = &mouse->dev;
211         unsigned char *buf = mouse->buf;
212         int left, middle, right;
213         int dx, dy;
214
215         /*
216          * Check for normal stream packets. This is three bytes,
217          * with the first byte's 3 MSB set to 100.
218          *
219          * [0]: 1       0       0       SignX   SignY   Left    Middle  Right
220          * [1]: 0       dx      dx      dx      dx      dx      dx      dx
221          * [2]: 0       dy      dy      dy      dy      dy      dy      dy
222          */
223
224         /*
225          * Low 7 bit of byte 1 are abs(dx), bit 7 is
226          * 0, bit 4 of byte 0 is direction.
227          */
228         dx = buf[1] & 0x7f;
229         dx *= ((buf[0] >> 4) & 0x01)? 1: -1;
230
231         /*
232          * Low 7 bit of byte 2 are abs(dy), bit 7 is
233          * 0, bit 3 of byte 0 is direction.
234          */
235         dy = buf[2] & 0x7f;
236         dy *= ((buf[0] >> 3) & 0x01)? -1: 1;
237
238         /*
239          * Get button state. It's the low three bits
240          * (for three buttons) of byte 0.
241          */
242         left    = (buf[0] & 0x04)? 1: 0;
243         middle  = (buf[0] & 0x02)? 1: 0;
244         right   = (buf[0] & 0x01)? 1: 0;
245
246         vsxxxaa_drop_bytes (mouse, 3);
247
248         DBG (KERN_INFO "%s on %s: dx=%d, dy=%d, buttons=%s%s%s\n",
249                         mouse->name, mouse->phys, dx, dy,
250                         left? "L": "l", middle? "M": "m", right? "R": "r");
251
252         /*
253          * Report what we've found so far...
254          */
255         input_regs (dev, regs);
256         input_report_key (dev, BTN_LEFT, left);
257         input_report_key (dev, BTN_MIDDLE, middle);
258         input_report_key (dev, BTN_RIGHT, right);
259         input_report_key (dev, BTN_TOUCH, 0);
260         input_report_rel (dev, REL_X, dx);
261         input_report_rel (dev, REL_Y, dy);
262         input_sync (dev);
263 }
264
265 static void
266 vsxxxaa_handle_ABS_packet (struct vsxxxaa *mouse, struct pt_regs *regs)
267 {
268         struct input_dev *dev = &mouse->dev;
269         unsigned char *buf = mouse->buf;
270         int left, middle, right, touch;
271         int x, y;
272
273         /*
274          * Tablet position / button packet
275          *
276          * [0]: 1       1       0       B4      B3      B2      B1      Pr
277          * [1]: 0       0       X5      X4      X3      X2      X1      X0
278          * [2]: 0       0       X11     X10     X9      X8      X7      X6
279          * [3]: 0       0       Y5      Y4      Y3      Y2      Y1      Y0
280          * [4]: 0       0       Y11     Y10     Y9      Y8      Y7      Y6
281          */
282
283         /*
284          * Get X/Y position. Y axis needs to be inverted since VSXXX-AB
285          * counts down->top while monitor counts top->bottom.
286          */
287         x = ((buf[2] & 0x3f) << 6) | (buf[1] & 0x3f);
288         y = ((buf[4] & 0x3f) << 6) | (buf[3] & 0x3f);
289         y = 1023 - y;
290
291         /*
292          * Get button state. It's bits <4..1> of byte 0.
293          */
294         left    = (buf[0] & 0x02)? 1: 0;
295         middle  = (buf[0] & 0x04)? 1: 0;
296         right   = (buf[0] & 0x08)? 1: 0;
297         touch   = (buf[0] & 0x10)? 1: 0;
298
299         vsxxxaa_drop_bytes (mouse, 5);
300
301         DBG (KERN_INFO "%s on %s: x=%d, y=%d, buttons=%s%s%s%s\n",
302                         mouse->name, mouse->phys, x, y,
303                         left? "L": "l", middle? "M": "m",
304                         right? "R": "r", touch? "T": "t");
305
306         /*
307          * Report what we've found so far...
308          */
309         input_regs (dev, regs);
310         input_report_key (dev, BTN_LEFT, left);
311         input_report_key (dev, BTN_MIDDLE, middle);
312         input_report_key (dev, BTN_RIGHT, right);
313         input_report_key (dev, BTN_TOUCH, touch);
314         input_report_abs (dev, ABS_X, x);
315         input_report_abs (dev, ABS_Y, y);
316         input_sync (dev);
317 }
318
319 static void
320 vsxxxaa_handle_POR_packet (struct vsxxxaa *mouse, struct pt_regs *regs)
321 {
322         struct input_dev *dev = &mouse->dev;
323         unsigned char *buf = mouse->buf;
324         int left, middle, right;
325         unsigned char error;
326
327         /*
328          * Check for Power-On-Reset packets. These are sent out
329          * after plugging the mouse in, or when explicitely
330          * requested by sending 'T'.
331          *
332          * [0]: 1       0       1       0       R3      R2      R1      R0
333          * [1]: 0       M2      M1      M0      D3      D2      D1      D0
334          * [2]: 0       E6      E5      E4      E3      E2      E1      E0
335          * [3]: 0       0       0       0       0       Left    Middle  Right
336          *
337          * M: manufacturer location code
338          * R: revision code
339          * E: Error code. I'm not sure about these, but gpm's sources,
340          *    which support this mouse, too, tell about them:
341          *      E = [0x00 .. 0x1f]: no error, byte #3 is button state
342          *      E = 0x3d: button error, byte #3 tells which one.
343          *      E = <else>: other error
344          * D: <0010> == mouse, <0100> == tablet
345          *
346          */
347
348         mouse->version = buf[0] & 0x0f;
349         mouse->country = (buf[1] >> 4) & 0x07;
350         mouse->type = buf[1] & 0x0f;
351         error = buf[2] & 0x7f;
352
353         /*
354          * Get button state. It's the low three bits
355          * (for three buttons) of byte 0. Maybe even the bit <3>
356          * has some meaning if a tablet is attached.
357          */
358         left    = (buf[0] & 0x04)? 1: 0;
359         middle  = (buf[0] & 0x02)? 1: 0;
360         right   = (buf[0] & 0x01)? 1: 0;
361
362         vsxxxaa_drop_bytes (mouse, 4);
363         vsxxxaa_detection_done (mouse);
364
365         if (error <= 0x1f) {
366                 /* No error. Report buttons */
367                 input_regs (dev, regs);
368                 input_report_key (dev, BTN_LEFT, left);
369                 input_report_key (dev, BTN_MIDDLE, middle);
370                 input_report_key (dev, BTN_RIGHT, right);
371                 input_report_key (dev, BTN_TOUCH, 0);
372                 input_sync (dev);
373         } else {
374                 printk (KERN_ERR "Your %s on %s reports an undefined error, "
375                                 "please check it...\n", mouse->name,
376                                 mouse->phys);
377         }
378
379         /*
380          * If the mouse was hot-plugged, we need to force differential mode
381          * now... However, give it a second to recover from it's reset.
382          */
383         printk (KERN_NOTICE "%s on %s: Forceing standard packet format and "
384                         "streaming mode\n", mouse->name, mouse->phys);
385         mouse->serio->write (mouse->serio, 'S');
386         mdelay (50);
387         mouse->serio->write (mouse->serio, 'R');
388 }
389
390 static void
391 vsxxxaa_parse_buffer (struct vsxxxaa *mouse, struct pt_regs *regs)
392 {
393         unsigned char *buf = mouse->buf;
394         int stray_bytes;
395
396         /*
397          * Parse buffer to death...
398          */
399         do {
400                 /*
401                  * Out of sync? Throw away what we don't understand. Each
402                  * packet starts with a byte whose bit 7 is set. Unhandled
403                  * packets (ie. which we don't know about or simply b0rk3d
404                  * data...) will get shifted out of the buffer after some
405                  * activity on the mouse.
406                  */
407                 while (mouse->count > 0 && !IS_HDR_BYTE(buf[0])) {
408                         printk (KERN_ERR "%s on %s: Dropping a byte to regain "
409                                         "sync with mouse data stream...\n",
410                                         mouse->name, mouse->phys);
411                         vsxxxaa_drop_bytes (mouse, 1);
412                 }
413
414                 /*
415                  * Check for packets we know about.
416                  */
417
418                 if (vsxxxaa_smells_like_packet (mouse, VSXXXAA_PACKET_REL, 3)) {
419                         /* Check for broken packet */
420                         stray_bytes = vsxxxaa_check_packet (mouse, 3);
421                         if (stray_bytes > 0) {
422                                 printk (KERN_ERR "Dropping %d bytes now...\n",
423                                                 stray_bytes);
424                                 vsxxxaa_drop_bytes (mouse, stray_bytes);
425                                 continue;
426                         }
427
428                         vsxxxaa_handle_REL_packet (mouse, regs);
429                         continue; /* More to parse? */
430                 }
431
432                 if (vsxxxaa_smells_like_packet (mouse, VSXXXAA_PACKET_ABS, 5)) {
433                         /* Check for broken packet */
434                         stray_bytes = vsxxxaa_check_packet (mouse, 5);
435                         if (stray_bytes > 0) {
436                                 printk (KERN_ERR "Dropping %d bytes now...\n",
437                                                 stray_bytes);
438                                 vsxxxaa_drop_bytes (mouse, stray_bytes);
439                                 continue;
440                         }
441
442                         vsxxxaa_handle_ABS_packet (mouse, regs);
443                         continue; /* More to parse? */
444                 }
445
446                 if (vsxxxaa_smells_like_packet (mouse, VSXXXAA_PACKET_POR, 4)) {
447                         /* Check for broken packet */
448                         stray_bytes = vsxxxaa_check_packet (mouse, 4);
449                         if (stray_bytes > 0) {
450                                 printk (KERN_ERR "Dropping %d bytes now...\n",
451                                                 stray_bytes);
452                                 vsxxxaa_drop_bytes (mouse, stray_bytes);
453                                 continue;
454                         }
455
456                         vsxxxaa_handle_POR_packet (mouse, regs);
457                         continue; /* More to parse? */
458                 }
459
460                 break; /* No REL, ABS or POR packet found */
461         } while (1);
462 }
463
464 static irqreturn_t
465 vsxxxaa_interrupt (struct serio *serio, unsigned char data, unsigned int flags,
466                 struct pt_regs *regs)
467 {
468         struct vsxxxaa *mouse = serio->private;
469
470         vsxxxaa_queue_byte (mouse, data);
471         vsxxxaa_parse_buffer (mouse, regs);
472
473         return IRQ_HANDLED;
474 }
475
476 static void
477 vsxxxaa_disconnect (struct serio *serio)
478 {
479         struct vsxxxaa *mouse = serio->private;
480
481         input_unregister_device (&mouse->dev);
482         serio_close (serio);
483         kfree (mouse);
484 }
485
486 static void
487 vsxxxaa_connect (struct serio *serio, struct serio_driver *drv)
488 {
489         struct vsxxxaa *mouse;
490
491         if ((serio->type & SERIO_TYPE) != SERIO_RS232)
492                 return;
493         if ((serio->type & SERIO_PROTO) != SERIO_VSXXXAA)
494                 return;
495
496         if (!(mouse = kmalloc (sizeof (struct vsxxxaa), GFP_KERNEL)))
497                 return;
498
499         memset (mouse, 0, sizeof (struct vsxxxaa));
500
501         init_input_dev (&mouse->dev);
502         set_bit (EV_KEY, mouse->dev.evbit);             /* We have buttons */
503         set_bit (EV_REL, mouse->dev.evbit);
504         set_bit (EV_ABS, mouse->dev.evbit);
505         set_bit (BTN_LEFT, mouse->dev.keybit);          /* We have 3 buttons */
506         set_bit (BTN_MIDDLE, mouse->dev.keybit);
507         set_bit (BTN_RIGHT, mouse->dev.keybit);
508         set_bit (BTN_TOUCH, mouse->dev.keybit);         /* ...and Tablet */
509         set_bit (REL_X, mouse->dev.relbit);
510         set_bit (REL_Y, mouse->dev.relbit);
511         set_bit (ABS_X, mouse->dev.absbit);
512         set_bit (ABS_Y, mouse->dev.absbit);
513
514         mouse->dev.absmin[ABS_X] = 0;
515         mouse->dev.absmax[ABS_X] = 1023;
516         mouse->dev.absmin[ABS_Y] = 0;
517         mouse->dev.absmax[ABS_Y] = 1023;
518
519         mouse->dev.private = mouse;
520         serio->private = mouse;
521
522         sprintf (mouse->name, "DEC VSXXX-AA/GA mouse or VSXXX-AB digitizer");
523         sprintf (mouse->phys, "%s/input0", serio->phys);
524         mouse->dev.name = mouse->name;
525         mouse->dev.phys = mouse->phys;
526         mouse->dev.id.bustype = BUS_RS232;
527         mouse->serio = serio;
528
529         if (serio_open (serio, drv)) {
530                 kfree (mouse);
531                 return;
532         }
533
534         /*
535          * Request selftest. Standard packet format and differential
536          * mode will be requested after the device ID'ed successfully.
537          */
538         mouse->serio->write (mouse->serio, 'T'); /* Test */
539
540         input_register_device (&mouse->dev);
541
542         printk (KERN_INFO "input: %s on %s\n", mouse->name, mouse->phys);
543 }
544
545 static struct serio_driver vsxxxaa_drv = {
546         .driver         = {
547                 .name   = "vsxxxaa",
548         },
549         .description    = DRIVER_DESC,
550         .connect        = vsxxxaa_connect,
551         .interrupt      = vsxxxaa_interrupt,
552         .disconnect     = vsxxxaa_disconnect,
553 };
554
555 int __init
556 vsxxxaa_init (void)
557 {
558         serio_register_driver(&vsxxxaa_drv);
559         return 0;
560 }
561
562 void __exit
563 vsxxxaa_exit (void)
564 {
565         serio_unregister_driver(&vsxxxaa_drv);
566 }
567
568 module_init (vsxxxaa_init);
569 module_exit (vsxxxaa_exit);
570