Path: blob/master/drivers/input/joystick/twidjoy.c
15111 views
/*1* Copyright (c) 2001 Arndt Schoenewald2* Copyright (c) 2000-2001 Vojtech Pavlik3* Copyright (c) 2000 Mark Fletcher4*5* Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany6*/78/*9* Driver to use Handykey's Twiddler (the first edition, i.e. the one with10* the RS232 interface) as a joystick under Linux11*12* The Twiddler is a one-handed chording keyboard featuring twelve buttons on13* the front, six buttons on the top, and a built-in tilt sensor. The buttons14* on the front, which are grouped as four rows of three buttons, are pressed15* by the four fingers (this implies only one button per row can be held down16* at the same time) and the buttons on the top are for the thumb. The tilt17* sensor delivers X and Y axis data depending on how the Twiddler is held.18* Additional information can be found at http://www.handykey.com.19*20* This driver does not use the Twiddler for its intended purpose, i.e. as21* a chording keyboard, but as a joystick: pressing and releasing a button22* immediately sends a corresponding button event, and tilting it generates23* corresponding ABS_X and ABS_Y events. This turns the Twiddler into a game24* controller with amazing 18 buttons :-)25*26* Note: The Twiddler2 (the successor of the Twiddler that connects directly27* to the PS/2 keyboard and mouse ports) is NOT supported by this driver!28*29* For questions or feedback regarding this driver module please contact:30* Arndt Schoenewald <[email protected]>31*/3233/*34* This program is free software; you can redistribute it and/or modify35* it under the terms of the GNU General Public License as published by36* the Free Software Foundation; either version 2 of the License, or37* (at your option) any later version.38*39* This program is distributed in the hope that it will be useful,40* but WITHOUT ANY WARRANTY; without even the implied warranty of41* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the42* GNU General Public License for more details.43*44* You should have received a copy of the GNU General Public License45* along with this program; if not, write to the Free Software46* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA47*/4849#include <linux/kernel.h>50#include <linux/module.h>51#include <linux/slab.h>52#include <linux/input.h>53#include <linux/serio.h>54#include <linux/init.h>5556#define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver"5758MODULE_DESCRIPTION(DRIVER_DESC);59MODULE_LICENSE("GPL");6061/*62* Constants.63*/6465#define TWIDJOY_MAX_LENGTH 56667static struct twidjoy_button_spec {68int bitshift;69int bitmask;70int buttons[3];71}72twidjoy_buttons[] = {73{ 0, 3, { BTN_A, BTN_B, BTN_C } },74{ 2, 3, { BTN_X, BTN_Y, BTN_Z } },75{ 4, 3, { BTN_TL, BTN_TR, BTN_TR2 } },76{ 6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },77{ 8, 1, { BTN_BASE5 } },78{ 9, 1, { BTN_BASE } },79{ 10, 1, { BTN_BASE3 } },80{ 11, 1, { BTN_BASE4 } },81{ 12, 1, { BTN_BASE2 } },82{ 13, 1, { BTN_BASE6 } },83{ 0, 0, { 0 } }84};8586/*87* Per-Twiddler data.88*/8990struct twidjoy {91struct input_dev *dev;92int idx;93unsigned char data[TWIDJOY_MAX_LENGTH];94char phys[32];95};9697/*98* twidjoy_process_packet() decodes packets the driver receives from the99* Twiddler. It updates the data accordingly.100*/101102static void twidjoy_process_packet(struct twidjoy *twidjoy)103{104struct input_dev *dev = twidjoy->dev;105unsigned char *data = twidjoy->data;106struct twidjoy_button_spec *bp;107int button_bits, abs_x, abs_y;108109button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);110111for (bp = twidjoy_buttons; bp->bitmask; bp++) {112int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;113int i;114115for (i = 0; i < bp->bitmask; i++)116input_report_key(dev, bp->buttons[i], i+1 == value);117}118119abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);120if (data[4] & 0x08) abs_x -= 256;121122abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);123if (data[3] & 0x02) abs_y -= 256;124125input_report_abs(dev, ABS_X, -abs_x);126input_report_abs(dev, ABS_Y, +abs_y);127128input_sync(dev);129}130131/*132* twidjoy_interrupt() is called by the low level driver when characters133* are ready for us. We then buffer them for further processing, or call the134* packet processing routine.135*/136137static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)138{139struct twidjoy *twidjoy = serio_get_drvdata(serio);140141/* All Twiddler packets are 5 bytes. The fact that the first byte142* has a MSB of 0 and all other bytes have a MSB of 1 can be used143* to check and regain sync. */144145if ((data & 0x80) == 0)146twidjoy->idx = 0; /* this byte starts a new packet */147else if (twidjoy->idx == 0)148return IRQ_HANDLED; /* wrong MSB -- ignore this byte */149150if (twidjoy->idx < TWIDJOY_MAX_LENGTH)151twidjoy->data[twidjoy->idx++] = data;152153if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {154twidjoy_process_packet(twidjoy);155twidjoy->idx = 0;156}157158return IRQ_HANDLED;159}160161/*162* twidjoy_disconnect() is the opposite of twidjoy_connect()163*/164165static void twidjoy_disconnect(struct serio *serio)166{167struct twidjoy *twidjoy = serio_get_drvdata(serio);168169serio_close(serio);170serio_set_drvdata(serio, NULL);171input_unregister_device(twidjoy->dev);172kfree(twidjoy);173}174175/*176* twidjoy_connect() is the routine that is called when someone adds a177* new serio device. It looks for the Twiddler, and if found, registers178* it as an input device.179*/180181static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)182{183struct twidjoy_button_spec *bp;184struct twidjoy *twidjoy;185struct input_dev *input_dev;186int err = -ENOMEM;187int i;188189twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);190input_dev = input_allocate_device();191if (!twidjoy || !input_dev)192goto fail1;193194twidjoy->dev = input_dev;195snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);196197input_dev->name = "Handykey Twiddler";198input_dev->phys = twidjoy->phys;199input_dev->id.bustype = BUS_RS232;200input_dev->id.vendor = SERIO_TWIDJOY;201input_dev->id.product = 0x0001;202input_dev->id.version = 0x0100;203input_dev->dev.parent = &serio->dev;204205input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);206input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);207input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);208209for (bp = twidjoy_buttons; bp->bitmask; bp++)210for (i = 0; i < bp->bitmask; i++)211set_bit(bp->buttons[i], input_dev->keybit);212213serio_set_drvdata(serio, twidjoy);214215err = serio_open(serio, drv);216if (err)217goto fail2;218219err = input_register_device(twidjoy->dev);220if (err)221goto fail3;222223return 0;224225fail3: serio_close(serio);226fail2: serio_set_drvdata(serio, NULL);227fail1: input_free_device(input_dev);228kfree(twidjoy);229return err;230}231232/*233* The serio driver structure.234*/235236static struct serio_device_id twidjoy_serio_ids[] = {237{238.type = SERIO_RS232,239.proto = SERIO_TWIDJOY,240.id = SERIO_ANY,241.extra = SERIO_ANY,242},243{ 0 }244};245246MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);247248static struct serio_driver twidjoy_drv = {249.driver = {250.name = "twidjoy",251},252.description = DRIVER_DESC,253.id_table = twidjoy_serio_ids,254.interrupt = twidjoy_interrupt,255.connect = twidjoy_connect,256.disconnect = twidjoy_disconnect,257};258259/*260* The functions for inserting/removing us as a module.261*/262263static int __init twidjoy_init(void)264{265return serio_register_driver(&twidjoy_drv);266}267268static void __exit twidjoy_exit(void)269{270serio_unregister_driver(&twidjoy_drv);271}272273module_init(twidjoy_init);274module_exit(twidjoy_exit);275276277