11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * $Id: stinger.c,v 1.10 2002/01/22 20:29:31 vojtech Exp $ 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (c) 2000-2001 Vojtech Pavlik 51da177e4SLinus Torvalds * Copyright (c) 2000 Mark Fletcher 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * Gravis Stinger gamepad driver for Linux 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * This program is free warftware; you can redistribute it and/or modify 141da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 151da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 161da177e4SLinus Torvalds * (at your option) any later version. 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 191da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 201da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 211da177e4SLinus Torvalds * GNU General Public License for more details. 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 241da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 251da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Should you need to contact me, the author, you can do so either by 281da177e4SLinus Torvalds * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: 291da177e4SLinus Torvalds * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #include <linux/kernel.h> 331da177e4SLinus Torvalds #include <linux/module.h> 341da177e4SLinus Torvalds #include <linux/slab.h> 351da177e4SLinus Torvalds #include <linux/input.h> 361da177e4SLinus Torvalds #include <linux/serio.h> 371da177e4SLinus Torvalds #include <linux/init.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #define DRIVER_DESC "Gravis Stinger gamepad driver" 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 421da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 431da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds /* 461da177e4SLinus Torvalds * Constants. 471da177e4SLinus Torvalds */ 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #define STINGER_MAX_LENGTH 8 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds /* 521da177e4SLinus Torvalds * Per-Stinger data. 531da177e4SLinus Torvalds */ 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds struct stinger { 5617dd3f0fSDmitry Torokhov struct input_dev *dev; 571da177e4SLinus Torvalds int idx; 581da177e4SLinus Torvalds unsigned char data[STINGER_MAX_LENGTH]; 591da177e4SLinus Torvalds char phys[32]; 601da177e4SLinus Torvalds }; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds /* 631da177e4SLinus Torvalds * stinger_process_packet() decodes packets the driver receives from the 641da177e4SLinus Torvalds * Stinger. It updates the data accordingly. 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds 677d12e780SDavid Howells static void stinger_process_packet(struct stinger *stinger) 681da177e4SLinus Torvalds { 6917dd3f0fSDmitry Torokhov struct input_dev *dev = stinger->dev; 701da177e4SLinus Torvalds unsigned char *data = stinger->data; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds if (!stinger->idx) return; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds input_report_key(dev, BTN_A, ((data[0] & 0x20) >> 5)); 751da177e4SLinus Torvalds input_report_key(dev, BTN_B, ((data[0] & 0x10) >> 4)); 761da177e4SLinus Torvalds input_report_key(dev, BTN_C, ((data[0] & 0x08) >> 3)); 771da177e4SLinus Torvalds input_report_key(dev, BTN_X, ((data[0] & 0x04) >> 2)); 781da177e4SLinus Torvalds input_report_key(dev, BTN_Y, ((data[3] & 0x20) >> 5)); 791da177e4SLinus Torvalds input_report_key(dev, BTN_Z, ((data[3] & 0x10) >> 4)); 801da177e4SLinus Torvalds input_report_key(dev, BTN_TL, ((data[3] & 0x08) >> 3)); 811da177e4SLinus Torvalds input_report_key(dev, BTN_TR, ((data[3] & 0x04) >> 2)); 821da177e4SLinus Torvalds input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1)); 831da177e4SLinus Torvalds input_report_key(dev, BTN_START, (data[3] & 0x01)); 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6)); 861da177e4SLinus Torvalds input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F)); 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds input_sync(dev); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds return; 911da177e4SLinus Torvalds } 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* 941da177e4SLinus Torvalds * stinger_interrupt() is called by the low level driver when characters 951da177e4SLinus Torvalds * are ready for us. We then buffer them for further processing, or call the 961da177e4SLinus Torvalds * packet processing routine. 971da177e4SLinus Torvalds */ 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds static irqreturn_t stinger_interrupt(struct serio *serio, 1007d12e780SDavid Howells unsigned char data, unsigned int flags) 1011da177e4SLinus Torvalds { 1021da177e4SLinus Torvalds struct stinger *stinger = serio_get_drvdata(serio); 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* All Stinger packets are 4 bytes */ 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds if (stinger->idx < STINGER_MAX_LENGTH) 1071da177e4SLinus Torvalds stinger->data[stinger->idx++] = data; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds if (stinger->idx == 4) { 1107d12e780SDavid Howells stinger_process_packet(stinger); 1111da177e4SLinus Torvalds stinger->idx = 0; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds return IRQ_HANDLED; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* 1181da177e4SLinus Torvalds * stinger_disconnect() is the opposite of stinger_connect() 1191da177e4SLinus Torvalds */ 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds static void stinger_disconnect(struct serio *serio) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds struct stinger *stinger = serio_get_drvdata(serio); 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds serio_close(serio); 1261da177e4SLinus Torvalds serio_set_drvdata(serio, NULL); 12717dd3f0fSDmitry Torokhov input_unregister_device(stinger->dev); 1281da177e4SLinus Torvalds kfree(stinger); 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * stinger_connect() is the routine that is called when someone adds a 1331da177e4SLinus Torvalds * new serio device that supports Stinger protocol and registers it as 1341da177e4SLinus Torvalds * an input device. 1351da177e4SLinus Torvalds */ 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds static int stinger_connect(struct serio *serio, struct serio_driver *drv) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds struct stinger *stinger; 14017dd3f0fSDmitry Torokhov struct input_dev *input_dev; 14117dd3f0fSDmitry Torokhov int err = -ENOMEM; 1421da177e4SLinus Torvalds 14317dd3f0fSDmitry Torokhov stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL); 14417dd3f0fSDmitry Torokhov input_dev = input_allocate_device(); 14517dd3f0fSDmitry Torokhov if (!stinger || !input_dev) 146*127278ceSDmitry Torokhov goto fail1; 1471da177e4SLinus Torvalds 14817dd3f0fSDmitry Torokhov stinger->dev = input_dev; 14910ca4c0aSDmitry Torokhov snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys); 1501da177e4SLinus Torvalds 15117dd3f0fSDmitry Torokhov input_dev->name = "Gravis Stinger"; 15217dd3f0fSDmitry Torokhov input_dev->phys = stinger->phys; 15317dd3f0fSDmitry Torokhov input_dev->id.bustype = BUS_RS232; 15417dd3f0fSDmitry Torokhov input_dev->id.vendor = SERIO_STINGER; 15517dd3f0fSDmitry Torokhov input_dev->id.product = 0x0001; 15617dd3f0fSDmitry Torokhov input_dev->id.version = 0x0100; 15717dd3f0fSDmitry Torokhov input_dev->cdev.dev = &serio->dev; 15817dd3f0fSDmitry Torokhov input_dev->private = stinger; 1591da177e4SLinus Torvalds 16017dd3f0fSDmitry Torokhov input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); 16117dd3f0fSDmitry Torokhov input_dev->keybit[LONG(BTN_A)] = BIT(BTN_A) | BIT(BTN_B) | BIT(BTN_C) | BIT(BTN_X) | 16217dd3f0fSDmitry Torokhov BIT(BTN_Y) | BIT(BTN_Z) | BIT(BTN_TL) | BIT(BTN_TR) | 16317dd3f0fSDmitry Torokhov BIT(BTN_START) | BIT(BTN_SELECT); 16417dd3f0fSDmitry Torokhov input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4); 16517dd3f0fSDmitry Torokhov input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds serio_set_drvdata(serio, stinger); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds err = serio_open(serio, drv); 17017dd3f0fSDmitry Torokhov if (err) 171*127278ceSDmitry Torokhov goto fail2; 17217dd3f0fSDmitry Torokhov 173*127278ceSDmitry Torokhov err = input_register_device(stinger->dev); 174*127278ceSDmitry Torokhov if (err) 175*127278ceSDmitry Torokhov goto fail3; 176*127278ceSDmitry Torokhov 17717dd3f0fSDmitry Torokhov return 0; 17817dd3f0fSDmitry Torokhov 179*127278ceSDmitry Torokhov fail3: serio_close(serio); 180*127278ceSDmitry Torokhov fail2: serio_set_drvdata(serio, NULL); 181*127278ceSDmitry Torokhov fail1: input_free_device(input_dev); 1821da177e4SLinus Torvalds kfree(stinger); 1831da177e4SLinus Torvalds return err; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds /* 1871da177e4SLinus Torvalds * The serio driver structure. 1881da177e4SLinus Torvalds */ 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds static struct serio_device_id stinger_serio_ids[] = { 1911da177e4SLinus Torvalds { 1921da177e4SLinus Torvalds .type = SERIO_RS232, 1931da177e4SLinus Torvalds .proto = SERIO_STINGER, 1941da177e4SLinus Torvalds .id = SERIO_ANY, 1951da177e4SLinus Torvalds .extra = SERIO_ANY, 1961da177e4SLinus Torvalds }, 1971da177e4SLinus Torvalds { 0 } 1981da177e4SLinus Torvalds }; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds MODULE_DEVICE_TABLE(serio, stinger_serio_ids); 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds static struct serio_driver stinger_drv = { 2031da177e4SLinus Torvalds .driver = { 2041da177e4SLinus Torvalds .name = "stinger", 2051da177e4SLinus Torvalds }, 2061da177e4SLinus Torvalds .description = DRIVER_DESC, 2071da177e4SLinus Torvalds .id_table = stinger_serio_ids, 2081da177e4SLinus Torvalds .interrupt = stinger_interrupt, 2091da177e4SLinus Torvalds .connect = stinger_connect, 2101da177e4SLinus Torvalds .disconnect = stinger_disconnect, 2111da177e4SLinus Torvalds }; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds /* 2141da177e4SLinus Torvalds * The functions for inserting/removing us as a module. 2151da177e4SLinus Torvalds */ 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds static int __init stinger_init(void) 2181da177e4SLinus Torvalds { 2191da177e4SLinus Torvalds serio_register_driver(&stinger_drv); 2201da177e4SLinus Torvalds return 0; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds static void __exit stinger_exit(void) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds serio_unregister_driver(&stinger_drv); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds module_init(stinger_init); 2291da177e4SLinus Torvalds module_exit(stinger_exit); 230