blob: 3f4ec73c9553a2debeb52f2789b3e016a555b7cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 2001 Arndt Schoenewald
3 * Copyright (c) 2000-2001 Vojtech Pavlik
4 * Copyright (c) 2000 Mark Fletcher
5 *
6 * Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany
7 */
8
9/*
10 * Driver to use Handykey's Twiddler (the first edition, i.e. the one with
11 * the RS232 interface) as a joystick under Linux
12 *
13 * The Twiddler is a one-handed chording keyboard featuring twelve buttons on
14 * the front, six buttons on the top, and a built-in tilt sensor. The buttons
15 * on the front, which are grouped as four rows of three buttons, are pressed
16 * by the four fingers (this implies only one button per row can be held down
17 * at the same time) and the buttons on the top are for the thumb. The tilt
18 * sensor delivers X and Y axis data depending on how the Twiddler is held.
19 * Additional information can be found at http://www.handykey.com.
20 *
21 * This driver does not use the Twiddler for its intended purpose, i.e. as
22 * a chording keyboard, but as a joystick: pressing and releasing a button
23 * immediately sends a corresponding button event, and tilting it generates
24 * corresponding ABS_X and ABS_Y events. This turns the Twiddler into a game
25 * controller with amazing 18 buttons :-)
26 *
27 * Note: The Twiddler2 (the successor of the Twiddler that connects directly
28 * to the PS/2 keyboard and mouse ports) is NOT supported by this driver!
29 *
30 * For questions or feedback regarding this driver module please contact:
31 * Arndt Schoenewald <arndt@quelltext.com>
32 */
33
34/*
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48 */
49
50#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/slab.h>
53#include <linux/input.h>
54#include <linux/serio.h>
55#include <linux/init.h>
56
57#define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver"
58
59MODULE_DESCRIPTION(DRIVER_DESC);
60MODULE_LICENSE("GPL");
61
62/*
63 * Constants.
64 */
65
66#define TWIDJOY_MAX_LENGTH 5
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static struct twidjoy_button_spec {
69 int bitshift;
70 int bitmask;
71 int buttons[3];
72}
73twidjoy_buttons[] = {
74 { 0, 3, { BTN_A, BTN_B, BTN_C } },
75 { 2, 3, { BTN_X, BTN_Y, BTN_Z } },
76 { 4, 3, { BTN_TL, BTN_TR, BTN_TR2 } },
77 { 6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
78 { 8, 1, { BTN_BASE5 } },
79 { 9, 1, { BTN_BASE } },
80 { 10, 1, { BTN_BASE3 } },
81 { 11, 1, { BTN_BASE4 } },
82 { 12, 1, { BTN_BASE2 } },
83 { 13, 1, { BTN_BASE6 } },
84 { 0, 0, { 0 } }
85};
86
87/*
88 * Per-Twiddler data.
89 */
90
91struct twidjoy {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050092 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int idx;
94 unsigned char data[TWIDJOY_MAX_LENGTH];
95 char phys[32];
96};
97
98/*
99 * twidjoy_process_packet() decodes packets the driver receives from the
100 * Twiddler. It updates the data accordingly.
101 */
102
David Howells7d12e782006-10-05 14:55:46 +0100103static void twidjoy_process_packet(struct twidjoy *twidjoy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500105 struct input_dev *dev = twidjoy->dev;
106 unsigned char *data = twidjoy->data;
107 struct twidjoy_button_spec *bp;
108 int button_bits, abs_x, abs_y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500110 button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500112 for (bp = twidjoy_buttons; bp->bitmask; bp++) {
113 int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
114 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500116 for (i = 0; i < bp->bitmask; i++)
117 input_report_key(dev, bp->buttons[i], i+1 == value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500120 abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
121 if (data[4] & 0x08) abs_x -= 256;
122
123 abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
124 if (data[3] & 0x02) abs_y -= 256;
125
126 input_report_abs(dev, ABS_X, -abs_x);
127 input_report_abs(dev, ABS_Y, +abs_y);
128
129 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/*
133 * twidjoy_interrupt() is called by the low level driver when characters
134 * are ready for us. We then buffer them for further processing, or call the
135 * packet processing routine.
136 */
137
David Howells7d12e782006-10-05 14:55:46 +0100138static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct twidjoy *twidjoy = serio_get_drvdata(serio);
141
142 /* All Twiddler packets are 5 bytes. The fact that the first byte
143 * has a MSB of 0 and all other bytes have a MSB of 1 can be used
144 * to check and regain sync. */
145
146 if ((data & 0x80) == 0)
147 twidjoy->idx = 0; /* this byte starts a new packet */
148 else if (twidjoy->idx == 0)
149 return IRQ_HANDLED; /* wrong MSB -- ignore this byte */
150
151 if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
152 twidjoy->data[twidjoy->idx++] = data;
153
154 if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
David Howells7d12e782006-10-05 14:55:46 +0100155 twidjoy_process_packet(twidjoy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 twidjoy->idx = 0;
157 }
158
159 return IRQ_HANDLED;
160}
161
162/*
163 * twidjoy_disconnect() is the opposite of twidjoy_connect()
164 */
165
166static void twidjoy_disconnect(struct serio *serio)
167{
168 struct twidjoy *twidjoy = serio_get_drvdata(serio);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 serio_close(serio);
171 serio_set_drvdata(serio, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500172 input_unregister_device(twidjoy->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kfree(twidjoy);
174}
175
176/*
177 * twidjoy_connect() is the routine that is called when someone adds a
178 * new serio device. It looks for the Twiddler, and if found, registers
179 * it as an input device.
180 */
181
182static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
183{
184 struct twidjoy_button_spec *bp;
185 struct twidjoy *twidjoy;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500186 struct input_dev *input_dev;
187 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500190 twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);
191 input_dev = input_allocate_device();
192 if (!twidjoy || !input_dev)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500193 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500195 twidjoy->dev = input_dev;
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400196 snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500198 input_dev->name = "Handykey Twiddler";
199 input_dev->phys = twidjoy->phys;
200 input_dev->id.bustype = BUS_RS232;
201 input_dev->id.vendor = SERIO_TWIDJOY;
202 input_dev->id.product = 0x0001;
203 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400204 input_dev->dev.parent = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700206 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500207 input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);
208 input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500210 for (bp = twidjoy_buttons; bp->bitmask; bp++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 for (i = 0; i < bp->bitmask; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500212 set_bit(bp->buttons[i], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 serio_set_drvdata(serio, twidjoy);
215
216 err = serio_open(serio, drv);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500217 if (err)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500218 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500220 err = input_register_device(twidjoy->dev);
221 if (err)
222 goto fail3;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500225
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500226 fail3: serio_close(serio);
227 fail2: serio_set_drvdata(serio, NULL);
228 fail1: input_free_device(input_dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500229 kfree(twidjoy);
230 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233/*
234 * The serio driver structure.
235 */
236
237static struct serio_device_id twidjoy_serio_ids[] = {
238 {
239 .type = SERIO_RS232,
240 .proto = SERIO_TWIDJOY,
241 .id = SERIO_ANY,
242 .extra = SERIO_ANY,
243 },
244 { 0 }
245};
246
247MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
248
249static struct serio_driver twidjoy_drv = {
250 .driver = {
251 .name = "twidjoy",
252 },
253 .description = DRIVER_DESC,
254 .id_table = twidjoy_serio_ids,
255 .interrupt = twidjoy_interrupt,
256 .connect = twidjoy_connect,
257 .disconnect = twidjoy_disconnect,
258};
259
260/*
261 * The functions for inserting/removing us as a module.
262 */
263
Adrian Bunkffc6b5292006-01-29 21:51:07 -0500264static int __init twidjoy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Akinobu Mita153a9df02006-11-23 23:35:10 -0500266 return serio_register_driver(&twidjoy_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Adrian Bunkffc6b5292006-01-29 21:51:07 -0500269static void __exit twidjoy_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 serio_unregister_driver(&twidjoy_drv);
272}
273
274module_init(twidjoy_init);
275module_exit(twidjoy_exit);