18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) 2001 Arndt Schoenewald
48c2ecf20Sopenharmony_ci *  Copyright (c) 2000-2001 Vojtech Pavlik
58c2ecf20Sopenharmony_ci *  Copyright (c) 2000 Mark Fletcher
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/*
118c2ecf20Sopenharmony_ci * Driver to use Handykey's Twiddler (the first edition, i.e. the one with
128c2ecf20Sopenharmony_ci * the RS232 interface) as a joystick under Linux
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The Twiddler is a one-handed chording keyboard featuring twelve buttons on
158c2ecf20Sopenharmony_ci * the front, six buttons on the top, and a built-in tilt sensor. The buttons
168c2ecf20Sopenharmony_ci * on the front, which are grouped as four rows of three buttons, are pressed
178c2ecf20Sopenharmony_ci * by the four fingers (this implies only one button per row can be held down
188c2ecf20Sopenharmony_ci * at the same time) and the buttons on the top are for the thumb. The tilt
198c2ecf20Sopenharmony_ci * sensor delivers X and Y axis data depending on how the Twiddler is held.
208c2ecf20Sopenharmony_ci * Additional information can be found at http://www.handykey.com.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * This driver does not use the Twiddler for its intended purpose, i.e. as
238c2ecf20Sopenharmony_ci * a chording keyboard, but as a joystick: pressing and releasing a button
248c2ecf20Sopenharmony_ci * immediately sends a corresponding button event, and tilting it generates
258c2ecf20Sopenharmony_ci * corresponding ABS_X and ABS_Y events. This turns the Twiddler into a game
268c2ecf20Sopenharmony_ci * controller with amazing 18 buttons :-)
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * Note: The Twiddler2 (the successor of the Twiddler that connects directly
298c2ecf20Sopenharmony_ci * to the PS/2 keyboard and mouse ports) is NOT supported by this driver!
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * For questions or feedback regarding this driver module please contact:
328c2ecf20Sopenharmony_ci * Arndt Schoenewald <arndt@quelltext.com>
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/*
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include <linux/kernel.h>
398c2ecf20Sopenharmony_ci#include <linux/module.h>
408c2ecf20Sopenharmony_ci#include <linux/slab.h>
418c2ecf20Sopenharmony_ci#include <linux/input.h>
428c2ecf20Sopenharmony_ci#include <linux/serio.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define DRIVER_DESC	"Handykey Twiddler keyboard as a joystick driver"
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
478c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci * Constants.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define TWIDJOY_MAX_LENGTH 5
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic struct twidjoy_button_spec {
568c2ecf20Sopenharmony_ci	int bitshift;
578c2ecf20Sopenharmony_ci	int bitmask;
588c2ecf20Sopenharmony_ci	int buttons[3];
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_citwidjoy_buttons[] = {
618c2ecf20Sopenharmony_ci	{  0, 3, { BTN_A,      BTN_B,     BTN_C    } },
628c2ecf20Sopenharmony_ci	{  2, 3, { BTN_X,      BTN_Y,     BTN_Z    } },
638c2ecf20Sopenharmony_ci	{  4, 3, { BTN_TL,     BTN_TR,    BTN_TR2  } },
648c2ecf20Sopenharmony_ci	{  6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
658c2ecf20Sopenharmony_ci	{  8, 1, { BTN_BASE5                       } },
668c2ecf20Sopenharmony_ci	{  9, 1, { BTN_BASE                        } },
678c2ecf20Sopenharmony_ci	{ 10, 1, { BTN_BASE3                       } },
688c2ecf20Sopenharmony_ci	{ 11, 1, { BTN_BASE4                       } },
698c2ecf20Sopenharmony_ci	{ 12, 1, { BTN_BASE2                       } },
708c2ecf20Sopenharmony_ci	{ 13, 1, { BTN_BASE6                       } },
718c2ecf20Sopenharmony_ci	{ 0,  0, { 0                               } }
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/*
758c2ecf20Sopenharmony_ci * Per-Twiddler data.
768c2ecf20Sopenharmony_ci */
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistruct twidjoy {
798c2ecf20Sopenharmony_ci	struct input_dev *dev;
808c2ecf20Sopenharmony_ci	int idx;
818c2ecf20Sopenharmony_ci	unsigned char data[TWIDJOY_MAX_LENGTH];
828c2ecf20Sopenharmony_ci	char phys[32];
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * twidjoy_process_packet() decodes packets the driver receives from the
878c2ecf20Sopenharmony_ci * Twiddler. It updates the data accordingly.
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic void twidjoy_process_packet(struct twidjoy *twidjoy)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct input_dev *dev = twidjoy->dev;
938c2ecf20Sopenharmony_ci	unsigned char *data = twidjoy->data;
948c2ecf20Sopenharmony_ci	struct twidjoy_button_spec *bp;
958c2ecf20Sopenharmony_ci	int button_bits, abs_x, abs_y;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	for (bp = twidjoy_buttons; bp->bitmask; bp++) {
1008c2ecf20Sopenharmony_ci		int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
1018c2ecf20Sopenharmony_ci		int i;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		for (i = 0; i < bp->bitmask; i++)
1048c2ecf20Sopenharmony_ci			input_report_key(dev, bp->buttons[i], i+1 == value);
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
1088c2ecf20Sopenharmony_ci	if (data[4] & 0x08) abs_x -= 256;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
1118c2ecf20Sopenharmony_ci	if (data[3] & 0x02) abs_y -= 256;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	input_report_abs(dev, ABS_X, -abs_x);
1148c2ecf20Sopenharmony_ci	input_report_abs(dev, ABS_Y, +abs_y);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	input_sync(dev);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/*
1208c2ecf20Sopenharmony_ci * twidjoy_interrupt() is called by the low level driver when characters
1218c2ecf20Sopenharmony_ci * are ready for us. We then buffer them for further processing, or call the
1228c2ecf20Sopenharmony_ci * packet processing routine.
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	struct twidjoy *twidjoy = serio_get_drvdata(serio);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	/* All Twiddler packets are 5 bytes. The fact that the first byte
1308c2ecf20Sopenharmony_ci	 * has a MSB of 0 and all other bytes have a MSB of 1 can be used
1318c2ecf20Sopenharmony_ci	 * to check and regain sync. */
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if ((data & 0x80) == 0)
1348c2ecf20Sopenharmony_ci		twidjoy->idx = 0;	/* this byte starts a new packet */
1358c2ecf20Sopenharmony_ci	else if (twidjoy->idx == 0)
1368c2ecf20Sopenharmony_ci		return IRQ_HANDLED;	/* wrong MSB -- ignore this byte */
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
1398c2ecf20Sopenharmony_ci		twidjoy->data[twidjoy->idx++] = data;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
1428c2ecf20Sopenharmony_ci		twidjoy_process_packet(twidjoy);
1438c2ecf20Sopenharmony_ci		twidjoy->idx = 0;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/*
1508c2ecf20Sopenharmony_ci * twidjoy_disconnect() is the opposite of twidjoy_connect()
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic void twidjoy_disconnect(struct serio *serio)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	struct twidjoy *twidjoy = serio_get_drvdata(serio);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	serio_close(serio);
1588c2ecf20Sopenharmony_ci	serio_set_drvdata(serio, NULL);
1598c2ecf20Sopenharmony_ci	input_unregister_device(twidjoy->dev);
1608c2ecf20Sopenharmony_ci	kfree(twidjoy);
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/*
1648c2ecf20Sopenharmony_ci * twidjoy_connect() is the routine that is called when someone adds a
1658c2ecf20Sopenharmony_ci * new serio device. It looks for the Twiddler, and if found, registers
1668c2ecf20Sopenharmony_ci * it as an input device.
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	struct twidjoy_button_spec *bp;
1728c2ecf20Sopenharmony_ci	struct twidjoy *twidjoy;
1738c2ecf20Sopenharmony_ci	struct input_dev *input_dev;
1748c2ecf20Sopenharmony_ci	int err = -ENOMEM;
1758c2ecf20Sopenharmony_ci	int i;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);
1788c2ecf20Sopenharmony_ci	input_dev = input_allocate_device();
1798c2ecf20Sopenharmony_ci	if (!twidjoy || !input_dev)
1808c2ecf20Sopenharmony_ci		goto fail1;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	twidjoy->dev = input_dev;
1838c2ecf20Sopenharmony_ci	snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	input_dev->name = "Handykey Twiddler";
1868c2ecf20Sopenharmony_ci	input_dev->phys = twidjoy->phys;
1878c2ecf20Sopenharmony_ci	input_dev->id.bustype = BUS_RS232;
1888c2ecf20Sopenharmony_ci	input_dev->id.vendor = SERIO_TWIDJOY;
1898c2ecf20Sopenharmony_ci	input_dev->id.product = 0x0001;
1908c2ecf20Sopenharmony_ci	input_dev->id.version = 0x0100;
1918c2ecf20Sopenharmony_ci	input_dev->dev.parent = &serio->dev;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1948c2ecf20Sopenharmony_ci	input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);
1958c2ecf20Sopenharmony_ci	input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	for (bp = twidjoy_buttons; bp->bitmask; bp++)
1988c2ecf20Sopenharmony_ci		for (i = 0; i < bp->bitmask; i++)
1998c2ecf20Sopenharmony_ci			set_bit(bp->buttons[i], input_dev->keybit);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	serio_set_drvdata(serio, twidjoy);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	err = serio_open(serio, drv);
2048c2ecf20Sopenharmony_ci	if (err)
2058c2ecf20Sopenharmony_ci		goto fail2;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	err = input_register_device(twidjoy->dev);
2088c2ecf20Sopenharmony_ci	if (err)
2098c2ecf20Sopenharmony_ci		goto fail3;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	return 0;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci fail3:	serio_close(serio);
2148c2ecf20Sopenharmony_ci fail2:	serio_set_drvdata(serio, NULL);
2158c2ecf20Sopenharmony_ci fail1:	input_free_device(input_dev);
2168c2ecf20Sopenharmony_ci	kfree(twidjoy);
2178c2ecf20Sopenharmony_ci	return err;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/*
2218c2ecf20Sopenharmony_ci * The serio driver structure.
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic const struct serio_device_id twidjoy_serio_ids[] = {
2258c2ecf20Sopenharmony_ci	{
2268c2ecf20Sopenharmony_ci		.type	= SERIO_RS232,
2278c2ecf20Sopenharmony_ci		.proto	= SERIO_TWIDJOY,
2288c2ecf20Sopenharmony_ci		.id	= SERIO_ANY,
2298c2ecf20Sopenharmony_ci		.extra	= SERIO_ANY,
2308c2ecf20Sopenharmony_ci	},
2318c2ecf20Sopenharmony_ci	{ 0 }
2328c2ecf20Sopenharmony_ci};
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic struct serio_driver twidjoy_drv = {
2378c2ecf20Sopenharmony_ci	.driver		= {
2388c2ecf20Sopenharmony_ci		.name	= "twidjoy",
2398c2ecf20Sopenharmony_ci	},
2408c2ecf20Sopenharmony_ci	.description	= DRIVER_DESC,
2418c2ecf20Sopenharmony_ci	.id_table	= twidjoy_serio_ids,
2428c2ecf20Sopenharmony_ci	.interrupt	= twidjoy_interrupt,
2438c2ecf20Sopenharmony_ci	.connect	= twidjoy_connect,
2448c2ecf20Sopenharmony_ci	.disconnect	= twidjoy_disconnect,
2458c2ecf20Sopenharmony_ci};
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cimodule_serio_driver(twidjoy_drv);
248