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