1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2001 Arndt Schoenewald
4 * Copyright (c) 2000-2001 Vojtech Pavlik
5 * Copyright (c) 2000 Mark Fletcher
7 * Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany
11 * Driver to use Handykey's Twiddler (the first edition, i.e. the one with
12 * the RS232 interface) as a joystick under Linux
14 * The Twiddler is a one-handed chording keyboard featuring twelve buttons on
15 * the front, six buttons on the top, and a built-in tilt sensor. The buttons
16 * on the front, which are grouped as four rows of three buttons, are pressed
17 * by the four fingers (this implies only one button per row can be held down
18 * at the same time) and the buttons on the top are for the thumb. The tilt
19 * sensor delivers X and Y axis data depending on how the Twiddler is held.
20 * Additional information can be found at http://www.handykey.com.
22 * This driver does not use the Twiddler for its intended purpose, i.e. as
23 * a chording keyboard, but as a joystick: pressing and releasing a button
24 * immediately sends a corresponding button event, and tilting it generates
25 * corresponding ABS_X and ABS_Y events. This turns the Twiddler into a game
26 * controller with amazing 18 buttons :-)
28 * Note: The Twiddler2 (the successor of the Twiddler that connects directly
29 * to the PS/2 keyboard and mouse ports) is NOT supported by this driver!
31 * For questions or feedback regarding this driver module please contact:
32 * Arndt Schoenewald <arndt@quelltext.com>
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/input.h>
39 #include <linux/serio.h>
41 #define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver"
43 MODULE_DESCRIPTION(DRIVER_DESC
);
44 MODULE_LICENSE("GPL");
50 #define TWIDJOY_MAX_LENGTH 5
52 static struct twidjoy_button_spec
{
58 { 0, 3, { BTN_A
, BTN_B
, BTN_C
} },
59 { 2, 3, { BTN_X
, BTN_Y
, BTN_Z
} },
60 { 4, 3, { BTN_TL
, BTN_TR
, BTN_TR2
} },
61 { 6, 3, { BTN_SELECT
, BTN_START
, BTN_MODE
} },
62 { 8, 1, { BTN_BASE5
} },
63 { 9, 1, { BTN_BASE
} },
64 { 10, 1, { BTN_BASE3
} },
65 { 11, 1, { BTN_BASE4
} },
66 { 12, 1, { BTN_BASE2
} },
67 { 13, 1, { BTN_BASE6
} },
76 struct input_dev
*dev
;
78 unsigned char data
[TWIDJOY_MAX_LENGTH
];
83 * twidjoy_process_packet() decodes packets the driver receives from the
84 * Twiddler. It updates the data accordingly.
87 static void twidjoy_process_packet(struct twidjoy
*twidjoy
)
89 struct input_dev
*dev
= twidjoy
->dev
;
90 unsigned char *data
= twidjoy
->data
;
91 struct twidjoy_button_spec
*bp
;
92 int button_bits
, abs_x
, abs_y
;
94 button_bits
= ((data
[1] & 0x7f) << 7) | (data
[0] & 0x7f);
96 for (bp
= twidjoy_buttons
; bp
->bitmask
; bp
++) {
97 int value
= (button_bits
& (bp
->bitmask
<< bp
->bitshift
)) >> bp
->bitshift
;
100 for (i
= 0; i
< bp
->bitmask
; i
++)
101 input_report_key(dev
, bp
->buttons
[i
], i
+1 == value
);
104 abs_x
= ((data
[4] & 0x07) << 5) | ((data
[3] & 0x7C) >> 2);
105 if (data
[4] & 0x08) abs_x
-= 256;
107 abs_y
= ((data
[3] & 0x01) << 7) | ((data
[2] & 0x7F) >> 0);
108 if (data
[3] & 0x02) abs_y
-= 256;
110 input_report_abs(dev
, ABS_X
, -abs_x
);
111 input_report_abs(dev
, ABS_Y
, +abs_y
);
117 * twidjoy_interrupt() is called by the low level driver when characters
118 * are ready for us. We then buffer them for further processing, or call the
119 * packet processing routine.
122 static irqreturn_t
twidjoy_interrupt(struct serio
*serio
, unsigned char data
, unsigned int flags
)
124 struct twidjoy
*twidjoy
= serio_get_drvdata(serio
);
126 /* All Twiddler packets are 5 bytes. The fact that the first byte
127 * has a MSB of 0 and all other bytes have a MSB of 1 can be used
128 * to check and regain sync. */
130 if ((data
& 0x80) == 0)
131 twidjoy
->idx
= 0; /* this byte starts a new packet */
132 else if (twidjoy
->idx
== 0)
133 return IRQ_HANDLED
; /* wrong MSB -- ignore this byte */
135 if (twidjoy
->idx
< TWIDJOY_MAX_LENGTH
)
136 twidjoy
->data
[twidjoy
->idx
++] = data
;
138 if (twidjoy
->idx
== TWIDJOY_MAX_LENGTH
) {
139 twidjoy_process_packet(twidjoy
);
147 * twidjoy_disconnect() is the opposite of twidjoy_connect()
150 static void twidjoy_disconnect(struct serio
*serio
)
152 struct twidjoy
*twidjoy
= serio_get_drvdata(serio
);
155 serio_set_drvdata(serio
, NULL
);
156 input_unregister_device(twidjoy
->dev
);
161 * twidjoy_connect() is the routine that is called when someone adds a
162 * new serio device. It looks for the Twiddler, and if found, registers
163 * it as an input device.
166 static int twidjoy_connect(struct serio
*serio
, struct serio_driver
*drv
)
168 struct twidjoy_button_spec
*bp
;
169 struct twidjoy
*twidjoy
;
170 struct input_dev
*input_dev
;
174 twidjoy
= kzalloc(sizeof(*twidjoy
), GFP_KERNEL
);
175 input_dev
= input_allocate_device();
176 if (!twidjoy
|| !input_dev
)
179 twidjoy
->dev
= input_dev
;
180 snprintf(twidjoy
->phys
, sizeof(twidjoy
->phys
), "%s/input0", serio
->phys
);
182 input_dev
->name
= "Handykey Twiddler";
183 input_dev
->phys
= twidjoy
->phys
;
184 input_dev
->id
.bustype
= BUS_RS232
;
185 input_dev
->id
.vendor
= SERIO_TWIDJOY
;
186 input_dev
->id
.product
= 0x0001;
187 input_dev
->id
.version
= 0x0100;
188 input_dev
->dev
.parent
= &serio
->dev
;
190 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
191 input_set_abs_params(input_dev
, ABS_X
, -50, 50, 4, 4);
192 input_set_abs_params(input_dev
, ABS_Y
, -50, 50, 4, 4);
194 for (bp
= twidjoy_buttons
; bp
->bitmask
; bp
++)
195 for (i
= 0; i
< bp
->bitmask
; i
++)
196 set_bit(bp
->buttons
[i
], input_dev
->keybit
);
198 serio_set_drvdata(serio
, twidjoy
);
200 err
= serio_open(serio
, drv
);
204 err
= input_register_device(twidjoy
->dev
);
210 fail3
: serio_close(serio
);
211 fail2
: serio_set_drvdata(serio
, NULL
);
212 fail1
: input_free_device(input_dev
);
218 * The serio driver structure.
221 static const struct serio_device_id twidjoy_serio_ids
[] = {
224 .proto
= SERIO_TWIDJOY
,
231 MODULE_DEVICE_TABLE(serio
, twidjoy_serio_ids
);
233 static struct serio_driver twidjoy_drv
= {
237 .description
= DRIVER_DESC
,
238 .id_table
= twidjoy_serio_ids
,
239 .interrupt
= twidjoy_interrupt
,
240 .connect
= twidjoy_connect
,
241 .disconnect
= twidjoy_disconnect
,
244 module_serio_driver(twidjoy_drv
);