ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / input / joystick / twidjoy.c
1 /*
2  * $Id: twidjoy.c,v 1.5 2002/01/22 20:31:53 vojtech Exp $
3  *
4  *  derived from CVS-ID "stinger.c,v 1.5 2001/05/29 12:57:18 vojtech Exp"
5  *
6  *  Copyright (c) 2001 Arndt Schoenewald
7  *  Copyright (c) 2000-2001 Vojtech Pavlik
8  *  Copyright (c) 2000 Mark Fletcher
9  *
10  *  Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany
11  */
12
13 /*
14  * Driver to use Handykey's Twiddler (the first edition, i.e. the one with
15  * the RS232 interface) as a joystick under Linux
16  *
17  * The Twiddler is a one-handed chording keyboard featuring twelve buttons on
18  * the front, six buttons on the top, and a built-in tilt sensor. The buttons
19  * on the front, which are grouped as four rows of three buttons, are pressed
20  * by the four fingers (this implies only one button per row can be held down
21  * at the same time) and the buttons on the top are for the thumb. The tilt
22  * sensor delivers X and Y axis data depending on how the Twiddler is held.
23  * Additional information can be found at http://www.handykey.com.
24  *
25  * This driver does not use the Twiddler for its intended purpose, i.e. as
26  * a chording keyboard, but as a joystick: pressing and releasing a button
27  * immediately sends a corresponding button event, and tilting it generates
28  * corresponding ABS_X and ABS_Y events. This turns the Twiddler into a game
29  * controller with amazing 18 buttons :-)
30  *
31  * Note: The Twiddler2 (the successor of the Twiddler that connects directly
32  * to the PS/2 keyboard and mouse ports) is NOT supported by this driver!
33  *
34  * For questions or feedback regarding this driver module please contact:
35  * Arndt Schoenewald <arndt@quelltext.com>
36  */
37
38 /*
39  * This program is free software; you can redistribute it and/or modify
40  * it under the terms of the GNU General Public License as published by
41  * the Free Software Foundation; either version 2 of the License, or
42  * (at your option) any later version.
43  *
44  * This program is distributed in the hope that it will be useful,
45  * but WITHOUT ANY WARRANTY; without even the implied warranty of
46  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47  * GNU General Public License for more details.
48  *
49  * You should have received a copy of the GNU General Public License
50  * along with this program; if not, write to the Free Software
51  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
52  */
53
54 #include <linux/kernel.h>
55 #include <linux/module.h>
56 #include <linux/slab.h>
57 #include <linux/input.h>
58 #include <linux/serio.h>
59 #include <linux/init.h>
60
61 /*
62  * Constants.
63  */
64
65 #define TWIDJOY_MAX_LENGTH 5
66
67 static char *twidjoy_name = "Handykey Twiddler";
68
69 static struct twidjoy_button_spec {
70         int bitshift;
71         int bitmask;
72         int buttons[3];
73 }
74 twidjoy_buttons[] = {
75         {  0, 3, { BTN_A,      BTN_B,     BTN_C    } },
76         {  2, 3, { BTN_X,      BTN_Y,     BTN_Z    } },
77         {  4, 3, { BTN_TL,     BTN_TR,    BTN_TR2  } },
78         {  6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
79         {  8, 1, { BTN_BASE5                       } },
80         {  9, 1, { BTN_BASE                        } },
81         { 10, 1, { BTN_BASE3                       } },
82         { 11, 1, { BTN_BASE4                       } },
83         { 12, 1, { BTN_BASE2                       } },
84         { 13, 1, { BTN_BASE6                       } },
85         { 0,  0, { 0                               } }
86 };
87
88 /*
89  * Per-Twiddler data.
90  */
91
92 struct twidjoy {
93         struct input_dev dev;
94         int idx;
95         unsigned char data[TWIDJOY_MAX_LENGTH];
96         char phys[32];
97 };
98
99 /*
100  * twidjoy_process_packet() decodes packets the driver receives from the
101  * Twiddler. It updates the data accordingly.
102  */
103
104 static void twidjoy_process_packet(struct twidjoy *twidjoy, struct pt_regs *regs)
105 {
106         if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
107                 struct input_dev *dev = &twidjoy->dev;
108                 unsigned char *data = twidjoy->data;
109                 struct twidjoy_button_spec *bp;
110                 int button_bits, abs_x, abs_y;
111
112                 button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
113
114                 input_regs(dev, regs);
115
116                 for (bp = twidjoy_buttons; bp->bitmask; bp++) {
117                         int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
118                         int i;
119
120                         for (i = 0; i < bp->bitmask; i++)
121                                 input_report_key(dev, bp->buttons[i], i+1 == value);
122                 }
123
124                 abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
125                 if (data[4] & 0x08) abs_x -= 256;
126
127                 abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
128                 if (data[3] & 0x02) abs_y -= 256;
129
130                 input_report_abs(dev, ABS_X, -abs_x);
131                 input_report_abs(dev, ABS_Y, +abs_y);
132
133                 input_sync(dev);
134         }
135
136         return;
137 }
138
139 /*
140  * twidjoy_interrupt() is called by the low level driver when characters
141  * are ready for us. We then buffer them for further processing, or call the
142  * packet processing routine.
143  */
144
145 static void twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags, struc pt_regs *regs)
146 {
147         struct twidjoy *twidjoy = serio->private;
148
149         /* All Twiddler packets are 5 bytes. The fact that the first byte
150          * has a MSB of 0 and all other bytes have a MSB of 1 can be used
151          * to check and regain sync. */
152
153         if ((data & 0x80) == 0)
154                 twidjoy->idx = 0;       /* this byte starts a new packet */
155         else if (twidjoy->idx == 0)
156                 return;                 /* wrong MSB -- ignore this byte */
157
158         if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
159                 twidjoy->data[twidjoy->idx++] = data;
160
161         if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
162                 twidjoy_process_packet(twidjoy, regs);
163                 twidjoy->idx = 0;
164         }
165
166         return;
167 }
168
169 /*
170  * twidjoy_disconnect() is the opposite of twidjoy_connect()
171  */
172
173 static void twidjoy_disconnect(struct serio *serio)
174 {
175         struct twidjoy *twidjoy = serio->private;
176         input_unregister_device(&twidjoy->dev);
177         serio_close(serio);
178         kfree(twidjoy);
179 }
180
181 /*
182  * twidjoy_connect() is the routine that is called when someone adds a
183  * new serio device. It looks for the Twiddler, and if found, registers
184  * it as an input device.
185  */
186
187 static void twidjoy_connect(struct serio *serio, struct serio_dev *dev)
188 {
189         struct twidjoy_button_spec *bp;
190         struct twidjoy *twidjoy;
191         int i;
192
193         if (serio->type != (SERIO_RS232 | SERIO_TWIDJOY))
194                 return;
195
196         if (!(twidjoy = kmalloc(sizeof(struct twidjoy), GFP_KERNEL)))
197                 return;
198
199         memset(twidjoy, 0, sizeof(struct twidjoy));
200
201         sprintf(twidjoy->phys, "%s/input0", serio->phys);
202
203         init_input_dev(&twidjoy->dev);
204         twidjoy->dev.name = twidjoy_name;
205         twidjoy->dev.phys = twidjoy->phys;
206         twidjoy->dev.id.bustype = BUS_RS232;
207         twidjoy->dev.id.vendor = SERIO_TWIDJOY;
208         twidjoy->dev.id.product = 0x0001;
209         twidjoy->dev.id.version = 0x0100;
210
211         twidjoy->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);      
212
213         for (bp = twidjoy_buttons; bp->bitmask; bp++) {
214                 for (i = 0; i < bp->bitmask; i++)
215                         set_bit(bp->buttons[i], twidjoy->dev.keybit);
216         }
217
218         twidjoy->dev.absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
219
220         for (i = 0; i < 2; i++) {
221                 twidjoy->dev.absmax[ABS_X+i] =  50;     
222                 twidjoy->dev.absmin[ABS_X+i] = -50;     
223
224                 /* TODO: arndt 20010708: Are these values appropriate? */
225                 twidjoy->dev.absfuzz[ABS_X+i] = 4;
226                 twidjoy->dev.absflat[ABS_X+i] = 4;
227         }
228
229         twidjoy->dev.private = twidjoy;
230         serio->private = twidjoy;
231
232         if (serio_open(serio, dev)) {
233                 kfree(twidjoy);
234                 return;
235         }
236
237         input_register_device(&twidjoy->dev);
238
239         printk(KERN_INFO "input: %s on %s\n", twidjoy_name, serio->phys);
240 }
241
242 /*
243  * The serio device structure.
244  */
245
246 static struct serio_dev twidjoy_dev = {
247         .interrupt =    twidjoy_interrupt,
248         .connect =      twidjoy_connect,
249         .disconnect =   twidjoy_disconnect,
250 };
251
252 /*
253  * The functions for inserting/removing us as a module.
254  */
255
256 int __init twidjoy_init(void)
257 {
258         serio_register_device(&twidjoy_dev);
259         return 0;
260 }
261
262 void __exit twidjoy_exit(void)
263 {
264         serio_unregister_device(&twidjoy_dev);
265 }
266
267 module_init(twidjoy_init);
268 module_exit(twidjoy_exit);