2 * Copyright (c) 2001 Arndt Schoenewald
3 * Copyright (c) 2000-2001 Vojtech Pavlik
4 * Copyright (c) 2000 Mark Fletcher
6 * Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany
10 * Driver to use Handykey's Twiddler (the first edition, i.e. the one with
11 * the RS232 interface) as a joystick under Linux
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.
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 :-)
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!
30 * For questions or feedback regarding this driver module please contact:
31 * Arndt Schoenewald <arndt@quelltext.com>
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.
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.
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
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>
56 #define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver"
58 MODULE_DESCRIPTION(DRIVER_DESC
);
59 MODULE_LICENSE("GPL");
65 #define TWIDJOY_MAX_LENGTH 5
67 static struct twidjoy_button_spec
{
73 { 0, 3, { BTN_A
, BTN_B
, BTN_C
} },
74 { 2, 3, { BTN_X
, BTN_Y
, BTN_Z
} },
75 { 4, 3, { BTN_TL
, BTN_TR
, BTN_TR2
} },
76 { 6, 3, { BTN_SELECT
, BTN_START
, BTN_MODE
} },
77 { 8, 1, { BTN_BASE5
} },
78 { 9, 1, { BTN_BASE
} },
79 { 10, 1, { BTN_BASE3
} },
80 { 11, 1, { BTN_BASE4
} },
81 { 12, 1, { BTN_BASE2
} },
82 { 13, 1, { BTN_BASE6
} },
91 struct input_dev
*dev
;
93 unsigned char data
[TWIDJOY_MAX_LENGTH
];
98 * twidjoy_process_packet() decodes packets the driver receives from the
99 * Twiddler. It updates the data accordingly.
102 static void twidjoy_process_packet(struct twidjoy
*twidjoy
)
104 struct input_dev
*dev
= twidjoy
->dev
;
105 unsigned char *data
= twidjoy
->data
;
106 struct twidjoy_button_spec
*bp
;
107 int button_bits
, abs_x
, abs_y
;
109 button_bits
= ((data
[1] & 0x7f) << 7) | (data
[0] & 0x7f);
111 for (bp
= twidjoy_buttons
; bp
->bitmask
; bp
++) {
112 int value
= (button_bits
& (bp
->bitmask
<< bp
->bitshift
)) >> bp
->bitshift
;
115 for (i
= 0; i
< bp
->bitmask
; i
++)
116 input_report_key(dev
, bp
->buttons
[i
], i
+1 == value
);
119 abs_x
= ((data
[4] & 0x07) << 5) | ((data
[3] & 0x7C) >> 2);
120 if (data
[4] & 0x08) abs_x
-= 256;
122 abs_y
= ((data
[3] & 0x01) << 7) | ((data
[2] & 0x7F) >> 0);
123 if (data
[3] & 0x02) abs_y
-= 256;
125 input_report_abs(dev
, ABS_X
, -abs_x
);
126 input_report_abs(dev
, ABS_Y
, +abs_y
);
132 * twidjoy_interrupt() is called by the low level driver when characters
133 * are ready for us. We then buffer them for further processing, or call the
134 * packet processing routine.
137 static irqreturn_t
twidjoy_interrupt(struct serio
*serio
, unsigned char data
, unsigned int flags
)
139 struct twidjoy
*twidjoy
= serio_get_drvdata(serio
);
141 /* All Twiddler packets are 5 bytes. The fact that the first byte
142 * has a MSB of 0 and all other bytes have a MSB of 1 can be used
143 * to check and regain sync. */
145 if ((data
& 0x80) == 0)
146 twidjoy
->idx
= 0; /* this byte starts a new packet */
147 else if (twidjoy
->idx
== 0)
148 return IRQ_HANDLED
; /* wrong MSB -- ignore this byte */
150 if (twidjoy
->idx
< TWIDJOY_MAX_LENGTH
)
151 twidjoy
->data
[twidjoy
->idx
++] = data
;
153 if (twidjoy
->idx
== TWIDJOY_MAX_LENGTH
) {
154 twidjoy_process_packet(twidjoy
);
162 * twidjoy_disconnect() is the opposite of twidjoy_connect()
165 static void twidjoy_disconnect(struct serio
*serio
)
167 struct twidjoy
*twidjoy
= serio_get_drvdata(serio
);
170 serio_set_drvdata(serio
, NULL
);
171 input_unregister_device(twidjoy
->dev
);
176 * twidjoy_connect() is the routine that is called when someone adds a
177 * new serio device. It looks for the Twiddler, and if found, registers
178 * it as an input device.
181 static int twidjoy_connect(struct serio
*serio
, struct serio_driver
*drv
)
183 struct twidjoy_button_spec
*bp
;
184 struct twidjoy
*twidjoy
;
185 struct input_dev
*input_dev
;
189 twidjoy
= kzalloc(sizeof(struct twidjoy
), GFP_KERNEL
);
190 input_dev
= input_allocate_device();
191 if (!twidjoy
|| !input_dev
)
194 twidjoy
->dev
= input_dev
;
195 snprintf(twidjoy
->phys
, sizeof(twidjoy
->phys
), "%s/input0", serio
->phys
);
197 input_dev
->name
= "Handykey Twiddler";
198 input_dev
->phys
= twidjoy
->phys
;
199 input_dev
->id
.bustype
= BUS_RS232
;
200 input_dev
->id
.vendor
= SERIO_TWIDJOY
;
201 input_dev
->id
.product
= 0x0001;
202 input_dev
->id
.version
= 0x0100;
203 input_dev
->dev
.parent
= &serio
->dev
;
205 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
206 input_set_abs_params(input_dev
, ABS_X
, -50, 50, 4, 4);
207 input_set_abs_params(input_dev
, ABS_Y
, -50, 50, 4, 4);
209 for (bp
= twidjoy_buttons
; bp
->bitmask
; bp
++)
210 for (i
= 0; i
< bp
->bitmask
; i
++)
211 set_bit(bp
->buttons
[i
], input_dev
->keybit
);
213 serio_set_drvdata(serio
, twidjoy
);
215 err
= serio_open(serio
, drv
);
219 err
= input_register_device(twidjoy
->dev
);
225 fail3
: serio_close(serio
);
226 fail2
: serio_set_drvdata(serio
, NULL
);
227 fail1
: input_free_device(input_dev
);
233 * The serio driver structure.
236 static struct serio_device_id twidjoy_serio_ids
[] = {
239 .proto
= SERIO_TWIDJOY
,
246 MODULE_DEVICE_TABLE(serio
, twidjoy_serio_ids
);
248 static struct serio_driver twidjoy_drv
= {
252 .description
= DRIVER_DESC
,
253 .id_table
= twidjoy_serio_ids
,
254 .interrupt
= twidjoy_interrupt
,
255 .connect
= twidjoy_connect
,
256 .disconnect
= twidjoy_disconnect
,
259 module_serio_driver(twidjoy_drv
);