Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) 2001 Arndt Schoenewald
0004  *  Copyright (c) 2000-2001 Vojtech Pavlik
0005  *  Copyright (c) 2000 Mark Fletcher
0006  *
0007  *  Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany
0008  */
0009 
0010 /*
0011  * Driver to use Handykey's Twiddler (the first edition, i.e. the one with
0012  * the RS232 interface) as a joystick under Linux
0013  *
0014  * The Twiddler is a one-handed chording keyboard featuring twelve buttons on
0015  * the front, six buttons on the top, and a built-in tilt sensor. The buttons
0016  * on the front, which are grouped as four rows of three buttons, are pressed
0017  * by the four fingers (this implies only one button per row can be held down
0018  * at the same time) and the buttons on the top are for the thumb. The tilt
0019  * sensor delivers X and Y axis data depending on how the Twiddler is held.
0020  * Additional information can be found at http://www.handykey.com.
0021  *
0022  * This driver does not use the Twiddler for its intended purpose, i.e. as
0023  * a chording keyboard, but as a joystick: pressing and releasing a button
0024  * immediately sends a corresponding button event, and tilting it generates
0025  * corresponding ABS_X and ABS_Y events. This turns the Twiddler into a game
0026  * controller with amazing 18 buttons :-)
0027  *
0028  * Note: The Twiddler2 (the successor of the Twiddler that connects directly
0029  * to the PS/2 keyboard and mouse ports) is NOT supported by this driver!
0030  *
0031  * For questions or feedback regarding this driver module please contact:
0032  * Arndt Schoenewald <arndt@quelltext.com>
0033  */
0034 
0035 /*
0036  */
0037 
0038 #include <linux/kernel.h>
0039 #include <linux/module.h>
0040 #include <linux/slab.h>
0041 #include <linux/input.h>
0042 #include <linux/serio.h>
0043 
0044 #define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver"
0045 
0046 MODULE_DESCRIPTION(DRIVER_DESC);
0047 MODULE_LICENSE("GPL");
0048 
0049 /*
0050  * Constants.
0051  */
0052 
0053 #define TWIDJOY_MAX_LENGTH 5
0054 
0055 static struct twidjoy_button_spec {
0056     int bitshift;
0057     int bitmask;
0058     int buttons[3];
0059 }
0060 twidjoy_buttons[] = {
0061     {  0, 3, { BTN_A,      BTN_B,     BTN_C    } },
0062     {  2, 3, { BTN_X,      BTN_Y,     BTN_Z    } },
0063     {  4, 3, { BTN_TL,     BTN_TR,    BTN_TR2  } },
0064     {  6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
0065     {  8, 1, { BTN_BASE5                       } },
0066     {  9, 1, { BTN_BASE                        } },
0067     { 10, 1, { BTN_BASE3                       } },
0068     { 11, 1, { BTN_BASE4                       } },
0069     { 12, 1, { BTN_BASE2                       } },
0070     { 13, 1, { BTN_BASE6                       } },
0071     { 0,  0, { 0                               } }
0072 };
0073 
0074 /*
0075  * Per-Twiddler data.
0076  */
0077 
0078 struct twidjoy {
0079     struct input_dev *dev;
0080     int idx;
0081     unsigned char data[TWIDJOY_MAX_LENGTH];
0082     char phys[32];
0083 };
0084 
0085 /*
0086  * twidjoy_process_packet() decodes packets the driver receives from the
0087  * Twiddler. It updates the data accordingly.
0088  */
0089 
0090 static void twidjoy_process_packet(struct twidjoy *twidjoy)
0091 {
0092     struct input_dev *dev = twidjoy->dev;
0093     unsigned char *data = twidjoy->data;
0094     struct twidjoy_button_spec *bp;
0095     int button_bits, abs_x, abs_y;
0096 
0097     button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
0098 
0099     for (bp = twidjoy_buttons; bp->bitmask; bp++) {
0100         int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
0101         int i;
0102 
0103         for (i = 0; i < bp->bitmask; i++)
0104             input_report_key(dev, bp->buttons[i], i+1 == value);
0105     }
0106 
0107     abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
0108     if (data[4] & 0x08) abs_x -= 256;
0109 
0110     abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
0111     if (data[3] & 0x02) abs_y -= 256;
0112 
0113     input_report_abs(dev, ABS_X, -abs_x);
0114     input_report_abs(dev, ABS_Y, +abs_y);
0115 
0116     input_sync(dev);
0117 }
0118 
0119 /*
0120  * twidjoy_interrupt() is called by the low level driver when characters
0121  * are ready for us. We then buffer them for further processing, or call the
0122  * packet processing routine.
0123  */
0124 
0125 static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
0126 {
0127     struct twidjoy *twidjoy = serio_get_drvdata(serio);
0128 
0129     /* All Twiddler packets are 5 bytes. The fact that the first byte
0130      * has a MSB of 0 and all other bytes have a MSB of 1 can be used
0131      * to check and regain sync. */
0132 
0133     if ((data & 0x80) == 0)
0134         twidjoy->idx = 0;   /* this byte starts a new packet */
0135     else if (twidjoy->idx == 0)
0136         return IRQ_HANDLED; /* wrong MSB -- ignore this byte */
0137 
0138     if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
0139         twidjoy->data[twidjoy->idx++] = data;
0140 
0141     if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
0142         twidjoy_process_packet(twidjoy);
0143         twidjoy->idx = 0;
0144     }
0145 
0146     return IRQ_HANDLED;
0147 }
0148 
0149 /*
0150  * twidjoy_disconnect() is the opposite of twidjoy_connect()
0151  */
0152 
0153 static void twidjoy_disconnect(struct serio *serio)
0154 {
0155     struct twidjoy *twidjoy = serio_get_drvdata(serio);
0156 
0157     serio_close(serio);
0158     serio_set_drvdata(serio, NULL);
0159     input_unregister_device(twidjoy->dev);
0160     kfree(twidjoy);
0161 }
0162 
0163 /*
0164  * twidjoy_connect() is the routine that is called when someone adds a
0165  * new serio device. It looks for the Twiddler, and if found, registers
0166  * it as an input device.
0167  */
0168 
0169 static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
0170 {
0171     struct twidjoy_button_spec *bp;
0172     struct twidjoy *twidjoy;
0173     struct input_dev *input_dev;
0174     int err = -ENOMEM;
0175     int i;
0176 
0177     twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);
0178     input_dev = input_allocate_device();
0179     if (!twidjoy || !input_dev)
0180         goto fail1;
0181 
0182     twidjoy->dev = input_dev;
0183     snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
0184 
0185     input_dev->name = "Handykey Twiddler";
0186     input_dev->phys = twidjoy->phys;
0187     input_dev->id.bustype = BUS_RS232;
0188     input_dev->id.vendor = SERIO_TWIDJOY;
0189     input_dev->id.product = 0x0001;
0190     input_dev->id.version = 0x0100;
0191     input_dev->dev.parent = &serio->dev;
0192 
0193     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0194     input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);
0195     input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);
0196 
0197     for (bp = twidjoy_buttons; bp->bitmask; bp++)
0198         for (i = 0; i < bp->bitmask; i++)
0199             set_bit(bp->buttons[i], input_dev->keybit);
0200 
0201     serio_set_drvdata(serio, twidjoy);
0202 
0203     err = serio_open(serio, drv);
0204     if (err)
0205         goto fail2;
0206 
0207     err = input_register_device(twidjoy->dev);
0208     if (err)
0209         goto fail3;
0210 
0211     return 0;
0212 
0213  fail3: serio_close(serio);
0214  fail2: serio_set_drvdata(serio, NULL);
0215  fail1: input_free_device(input_dev);
0216     kfree(twidjoy);
0217     return err;
0218 }
0219 
0220 /*
0221  * The serio driver structure.
0222  */
0223 
0224 static const struct serio_device_id twidjoy_serio_ids[] = {
0225     {
0226         .type   = SERIO_RS232,
0227         .proto  = SERIO_TWIDJOY,
0228         .id = SERIO_ANY,
0229         .extra  = SERIO_ANY,
0230     },
0231     { 0 }
0232 };
0233 
0234 MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
0235 
0236 static struct serio_driver twidjoy_drv = {
0237     .driver     = {
0238         .name   = "twidjoy",
0239     },
0240     .description    = DRIVER_DESC,
0241     .id_table   = twidjoy_serio_ids,
0242     .interrupt  = twidjoy_interrupt,
0243     .connect    = twidjoy_connect,
0244     .disconnect = twidjoy_disconnect,
0245 };
0246 
0247 module_serio_driver(twidjoy_drv);