xref: /openbmc/linux/drivers/input/joystick/twidjoy.c (revision fbb6b31a)
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 /*
36  */
37 
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/slab.h>
41 #include <linux/input.h>
42 #include <linux/serio.h>
43 
44 #define DRIVER_DESC	"Handykey Twiddler keyboard as a joystick driver"
45 
46 MODULE_DESCRIPTION(DRIVER_DESC);
47 MODULE_LICENSE("GPL");
48 
49 /*
50  * Constants.
51  */
52 
53 #define TWIDJOY_MAX_LENGTH 5
54 
55 static struct twidjoy_button_spec {
56 	int bitshift;
57 	int bitmask;
58 	int buttons[3];
59 }
60 twidjoy_buttons[] = {
61 	{  0, 3, { BTN_A,      BTN_B,     BTN_C    } },
62 	{  2, 3, { BTN_X,      BTN_Y,     BTN_Z    } },
63 	{  4, 3, { BTN_TL,     BTN_TR,    BTN_TR2  } },
64 	{  6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
65 	{  8, 1, { BTN_BASE5                       } },
66 	{  9, 1, { BTN_BASE                        } },
67 	{ 10, 1, { BTN_BASE3                       } },
68 	{ 11, 1, { BTN_BASE4                       } },
69 	{ 12, 1, { BTN_BASE2                       } },
70 	{ 13, 1, { BTN_BASE6                       } },
71 	{ 0,  0, { 0                               } }
72 };
73 
74 /*
75  * Per-Twiddler data.
76  */
77 
78 struct twidjoy {
79 	struct input_dev *dev;
80 	int idx;
81 	unsigned char data[TWIDJOY_MAX_LENGTH];
82 	char phys[32];
83 };
84 
85 /*
86  * twidjoy_process_packet() decodes packets the driver receives from the
87  * Twiddler. It updates the data accordingly.
88  */
89 
90 static void twidjoy_process_packet(struct twidjoy *twidjoy)
91 {
92 	struct input_dev *dev = twidjoy->dev;
93 	unsigned char *data = twidjoy->data;
94 	struct twidjoy_button_spec *bp;
95 	int button_bits, abs_x, abs_y;
96 
97 	button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
98 
99 	for (bp = twidjoy_buttons; bp->bitmask; bp++) {
100 		int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
101 		int i;
102 
103 		for (i = 0; i < bp->bitmask; i++)
104 			input_report_key(dev, bp->buttons[i], i+1 == value);
105 	}
106 
107 	abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
108 	if (data[4] & 0x08) abs_x -= 256;
109 
110 	abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
111 	if (data[3] & 0x02) abs_y -= 256;
112 
113 	input_report_abs(dev, ABS_X, -abs_x);
114 	input_report_abs(dev, ABS_Y, +abs_y);
115 
116 	input_sync(dev);
117 }
118 
119 /*
120  * twidjoy_interrupt() is called by the low level driver when characters
121  * are ready for us. We then buffer them for further processing, or call the
122  * packet processing routine.
123  */
124 
125 static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
126 {
127 	struct twidjoy *twidjoy = serio_get_drvdata(serio);
128 
129 	/* All Twiddler packets are 5 bytes. The fact that the first byte
130 	 * has a MSB of 0 and all other bytes have a MSB of 1 can be used
131 	 * to check and regain sync. */
132 
133 	if ((data & 0x80) == 0)
134 		twidjoy->idx = 0;	/* this byte starts a new packet */
135 	else if (twidjoy->idx == 0)
136 		return IRQ_HANDLED;	/* wrong MSB -- ignore this byte */
137 
138 	if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
139 		twidjoy->data[twidjoy->idx++] = data;
140 
141 	if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
142 		twidjoy_process_packet(twidjoy);
143 		twidjoy->idx = 0;
144 	}
145 
146 	return IRQ_HANDLED;
147 }
148 
149 /*
150  * twidjoy_disconnect() is the opposite of twidjoy_connect()
151  */
152 
153 static void twidjoy_disconnect(struct serio *serio)
154 {
155 	struct twidjoy *twidjoy = serio_get_drvdata(serio);
156 
157 	serio_close(serio);
158 	serio_set_drvdata(serio, NULL);
159 	input_unregister_device(twidjoy->dev);
160 	kfree(twidjoy);
161 }
162 
163 /*
164  * twidjoy_connect() is the routine that is called when someone adds a
165  * new serio device. It looks for the Twiddler, and if found, registers
166  * it as an input device.
167  */
168 
169 static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
170 {
171 	struct twidjoy_button_spec *bp;
172 	struct twidjoy *twidjoy;
173 	struct input_dev *input_dev;
174 	int err = -ENOMEM;
175 	int i;
176 
177 	twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);
178 	input_dev = input_allocate_device();
179 	if (!twidjoy || !input_dev)
180 		goto fail1;
181 
182 	twidjoy->dev = input_dev;
183 	snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
184 
185 	input_dev->name = "Handykey Twiddler";
186 	input_dev->phys = twidjoy->phys;
187 	input_dev->id.bustype = BUS_RS232;
188 	input_dev->id.vendor = SERIO_TWIDJOY;
189 	input_dev->id.product = 0x0001;
190 	input_dev->id.version = 0x0100;
191 	input_dev->dev.parent = &serio->dev;
192 
193 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
194 	input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);
195 	input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);
196 
197 	for (bp = twidjoy_buttons; bp->bitmask; bp++)
198 		for (i = 0; i < bp->bitmask; i++)
199 			set_bit(bp->buttons[i], input_dev->keybit);
200 
201 	serio_set_drvdata(serio, twidjoy);
202 
203 	err = serio_open(serio, drv);
204 	if (err)
205 		goto fail2;
206 
207 	err = input_register_device(twidjoy->dev);
208 	if (err)
209 		goto fail3;
210 
211 	return 0;
212 
213  fail3:	serio_close(serio);
214  fail2:	serio_set_drvdata(serio, NULL);
215  fail1:	input_free_device(input_dev);
216 	kfree(twidjoy);
217 	return err;
218 }
219 
220 /*
221  * The serio driver structure.
222  */
223 
224 static const struct serio_device_id twidjoy_serio_ids[] = {
225 	{
226 		.type	= SERIO_RS232,
227 		.proto	= SERIO_TWIDJOY,
228 		.id	= SERIO_ANY,
229 		.extra	= SERIO_ANY,
230 	},
231 	{ 0 }
232 };
233 
234 MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
235 
236 static struct serio_driver twidjoy_drv = {
237 	.driver		= {
238 		.name	= "twidjoy",
239 	},
240 	.description	= DRIVER_DESC,
241 	.id_table	= twidjoy_serio_ids,
242 	.interrupt	= twidjoy_interrupt,
243 	.connect	= twidjoy_connect,
244 	.disconnect	= twidjoy_disconnect,
245 };
246 
247 module_serio_driver(twidjoy_drv);
248