xref: /linux/drivers/input/joystick/twidjoy.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 2001 Arndt Schoenewald
4  *  Copyright (c) 2000-2001 Vojtech Pavlik
5  *  Copyright (c) 2000 Mark Fletcher
6  *
7  *  Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany
8  */
9 
10 /*
11  * Driver to use Handykey's Twiddler (the first edition, i.e. the one with
12  * the RS232 interface) as a joystick under Linux
13  *
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.
21  *
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 :-)
27  *
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!
30  *
31  * For questions or feedback regarding this driver module please contact:
32  * Arndt Schoenewald <arndt@quelltext.com>
33  */
34 
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>
40 
41 #define DRIVER_DESC	"Handykey Twiddler keyboard as a joystick driver"
42 
43 MODULE_DESCRIPTION(DRIVER_DESC);
44 MODULE_LICENSE("GPL");
45 
46 /*
47  * Constants.
48  */
49 
50 #define TWIDJOY_MAX_LENGTH 5
51 
52 static struct twidjoy_button_spec {
53 	int bitshift;
54 	int bitmask;
55 	int buttons[3];
56 }
57 twidjoy_buttons[] = {
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                       } },
68 	{ 0,  0, { 0                               } }
69 };
70 
71 /*
72  * Per-Twiddler data.
73  */
74 
75 struct twidjoy {
76 	struct input_dev *dev;
77 	int idx;
78 	unsigned char data[TWIDJOY_MAX_LENGTH];
79 	char phys[32];
80 };
81 
82 /*
83  * twidjoy_process_packet() decodes packets the driver receives from the
84  * Twiddler. It updates the data accordingly.
85  */
86 
87 static void twidjoy_process_packet(struct twidjoy *twidjoy)
88 {
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;
93 
94 	button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
95 
96 	for (bp = twidjoy_buttons; bp->bitmask; bp++) {
97 		int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
98 		int i;
99 
100 		for (i = 0; i < bp->bitmask; i++)
101 			input_report_key(dev, bp->buttons[i], i+1 == value);
102 	}
103 
104 	abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
105 	if (data[4] & 0x08) abs_x -= 256;
106 
107 	abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
108 	if (data[3] & 0x02) abs_y -= 256;
109 
110 	input_report_abs(dev, ABS_X, -abs_x);
111 	input_report_abs(dev, ABS_Y, +abs_y);
112 
113 	input_sync(dev);
114 }
115 
116 /*
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.
120  */
121 
122 static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
123 {
124 	struct twidjoy *twidjoy = serio_get_drvdata(serio);
125 
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. */
129 
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 */
134 
135 	if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
136 		twidjoy->data[twidjoy->idx++] = data;
137 
138 	if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
139 		twidjoy_process_packet(twidjoy);
140 		twidjoy->idx = 0;
141 	}
142 
143 	return IRQ_HANDLED;
144 }
145 
146 /*
147  * twidjoy_disconnect() is the opposite of twidjoy_connect()
148  */
149 
150 static void twidjoy_disconnect(struct serio *serio)
151 {
152 	struct twidjoy *twidjoy = serio_get_drvdata(serio);
153 
154 	serio_close(serio);
155 	serio_set_drvdata(serio, NULL);
156 	input_unregister_device(twidjoy->dev);
157 	kfree(twidjoy);
158 }
159 
160 /*
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.
164  */
165 
166 static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
167 {
168 	struct twidjoy_button_spec *bp;
169 	struct twidjoy *twidjoy;
170 	struct input_dev *input_dev;
171 	int err = -ENOMEM;
172 	int i;
173 
174 	twidjoy = kzalloc(sizeof(*twidjoy), GFP_KERNEL);
175 	input_dev = input_allocate_device();
176 	if (!twidjoy || !input_dev)
177 		goto fail1;
178 
179 	twidjoy->dev = input_dev;
180 	snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
181 
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;
189 
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);
193 
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);
197 
198 	serio_set_drvdata(serio, twidjoy);
199 
200 	err = serio_open(serio, drv);
201 	if (err)
202 		goto fail2;
203 
204 	err = input_register_device(twidjoy->dev);
205 	if (err)
206 		goto fail3;
207 
208 	return 0;
209 
210  fail3:	serio_close(serio);
211  fail2:	serio_set_drvdata(serio, NULL);
212  fail1:	input_free_device(input_dev);
213 	kfree(twidjoy);
214 	return err;
215 }
216 
217 /*
218  * The serio driver structure.
219  */
220 
221 static const struct serio_device_id twidjoy_serio_ids[] = {
222 	{
223 		.type	= SERIO_RS232,
224 		.proto	= SERIO_TWIDJOY,
225 		.id	= SERIO_ANY,
226 		.extra	= SERIO_ANY,
227 	},
228 	{ 0 }
229 };
230 
231 MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
232 
233 static struct serio_driver twidjoy_drv = {
234 	.driver		= {
235 		.name	= "twidjoy",
236 	},
237 	.description	= DRIVER_DESC,
238 	.id_table	= twidjoy_serio_ids,
239 	.interrupt	= twidjoy_interrupt,
240 	.connect	= twidjoy_connect,
241 	.disconnect	= twidjoy_disconnect,
242 };
243 
244 module_serio_driver(twidjoy_drv);
245