Path: blob/master/drivers/input/joystick/stinger.c
15109 views
/*1* Copyright (c) 2000-2001 Vojtech Pavlik2* Copyright (c) 2000 Mark Fletcher3*/45/*6* Gravis Stinger gamepad driver for Linux7*/89/*10* This program is free warftware; you can redistribute it and/or modify11* it under the terms of the GNU General Public License as published by12* the Free Software Foundation; either version 2 of the License, or13* (at your option) any later version.14*15* This program is distributed in the hope that it will be useful,16* but WITHOUT ANY WARRANTY; without even the implied warranty of17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the18* GNU General Public License for more details.19*20* You should have received a copy of the GNU General Public License21* along with this program; if not, write to the Free Software22* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA23*24* Should you need to contact me, the author, you can do so either by25* e-mail - mail your message to <[email protected]>, or by paper mail:26* Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic27*/2829#include <linux/kernel.h>30#include <linux/module.h>31#include <linux/slab.h>32#include <linux/input.h>33#include <linux/serio.h>34#include <linux/init.h>3536#define DRIVER_DESC "Gravis Stinger gamepad driver"3738MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");39MODULE_DESCRIPTION(DRIVER_DESC);40MODULE_LICENSE("GPL");4142/*43* Constants.44*/4546#define STINGER_MAX_LENGTH 84748/*49* Per-Stinger data.50*/5152struct stinger {53struct input_dev *dev;54int idx;55unsigned char data[STINGER_MAX_LENGTH];56char phys[32];57};5859/*60* stinger_process_packet() decodes packets the driver receives from the61* Stinger. It updates the data accordingly.62*/6364static void stinger_process_packet(struct stinger *stinger)65{66struct input_dev *dev = stinger->dev;67unsigned char *data = stinger->data;6869if (!stinger->idx) return;7071input_report_key(dev, BTN_A, ((data[0] & 0x20) >> 5));72input_report_key(dev, BTN_B, ((data[0] & 0x10) >> 4));73input_report_key(dev, BTN_C, ((data[0] & 0x08) >> 3));74input_report_key(dev, BTN_X, ((data[0] & 0x04) >> 2));75input_report_key(dev, BTN_Y, ((data[3] & 0x20) >> 5));76input_report_key(dev, BTN_Z, ((data[3] & 0x10) >> 4));77input_report_key(dev, BTN_TL, ((data[3] & 0x08) >> 3));78input_report_key(dev, BTN_TR, ((data[3] & 0x04) >> 2));79input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));80input_report_key(dev, BTN_START, (data[3] & 0x01));8182input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));83input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));8485input_sync(dev);8687return;88}8990/*91* stinger_interrupt() is called by the low level driver when characters92* are ready for us. We then buffer them for further processing, or call the93* packet processing routine.94*/9596static irqreturn_t stinger_interrupt(struct serio *serio,97unsigned char data, unsigned int flags)98{99struct stinger *stinger = serio_get_drvdata(serio);100101/* All Stinger packets are 4 bytes */102103if (stinger->idx < STINGER_MAX_LENGTH)104stinger->data[stinger->idx++] = data;105106if (stinger->idx == 4) {107stinger_process_packet(stinger);108stinger->idx = 0;109}110111return IRQ_HANDLED;112}113114/*115* stinger_disconnect() is the opposite of stinger_connect()116*/117118static void stinger_disconnect(struct serio *serio)119{120struct stinger *stinger = serio_get_drvdata(serio);121122serio_close(serio);123serio_set_drvdata(serio, NULL);124input_unregister_device(stinger->dev);125kfree(stinger);126}127128/*129* stinger_connect() is the routine that is called when someone adds a130* new serio device that supports Stinger protocol and registers it as131* an input device.132*/133134static int stinger_connect(struct serio *serio, struct serio_driver *drv)135{136struct stinger *stinger;137struct input_dev *input_dev;138int err = -ENOMEM;139140stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);141input_dev = input_allocate_device();142if (!stinger || !input_dev)143goto fail1;144145stinger->dev = input_dev;146snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);147148input_dev->name = "Gravis Stinger";149input_dev->phys = stinger->phys;150input_dev->id.bustype = BUS_RS232;151input_dev->id.vendor = SERIO_STINGER;152input_dev->id.product = 0x0001;153input_dev->id.version = 0x0100;154input_dev->dev.parent = &serio->dev;155156input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);157input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |158BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |159BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |160BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);161input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);162input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);163164serio_set_drvdata(serio, stinger);165166err = serio_open(serio, drv);167if (err)168goto fail2;169170err = input_register_device(stinger->dev);171if (err)172goto fail3;173174return 0;175176fail3: serio_close(serio);177fail2: serio_set_drvdata(serio, NULL);178fail1: input_free_device(input_dev);179kfree(stinger);180return err;181}182183/*184* The serio driver structure.185*/186187static struct serio_device_id stinger_serio_ids[] = {188{189.type = SERIO_RS232,190.proto = SERIO_STINGER,191.id = SERIO_ANY,192.extra = SERIO_ANY,193},194{ 0 }195};196197MODULE_DEVICE_TABLE(serio, stinger_serio_ids);198199static struct serio_driver stinger_drv = {200.driver = {201.name = "stinger",202},203.description = DRIVER_DESC,204.id_table = stinger_serio_ids,205.interrupt = stinger_interrupt,206.connect = stinger_connect,207.disconnect = stinger_disconnect,208};209210/*211* The functions for inserting/removing us as a module.212*/213214static int __init stinger_init(void)215{216return serio_register_driver(&stinger_drv);217}218219static void __exit stinger_exit(void)220{221serio_unregister_driver(&stinger_drv);222}223224module_init(stinger_init);225module_exit(stinger_exit);226227228