Path: blob/master/drivers/input/joystick/turbografx.c
15111 views
/*1* Copyright (c) 1998-2001 Vojtech Pavlik2*3* Based on the work of:4* Steffen Schwenke5*/67/*8* TurboGraFX parallel port interface driver for Linux.9*/1011/*12* This program is free software; you can redistribute it and/or modify13* it under the terms of the GNU General Public License as published by14* the Free Software Foundation; either version 2 of the License, or15* (at your option) any later version.16*17* This program is distributed in the hope that it will be useful,18* but WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20* GNU General Public License for more details.21*22* You should have received a copy of the GNU General Public License23* along with this program; if not, write to the Free Software24* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA25*26* Should you need to contact me, the author, you can do so either by27* e-mail - mail your message to <[email protected]>, or by paper mail:28* Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic29*/3031#include <linux/kernel.h>32#include <linux/parport.h>33#include <linux/input.h>34#include <linux/module.h>35#include <linux/init.h>36#include <linux/mutex.h>37#include <linux/slab.h>3839MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");40MODULE_DESCRIPTION("TurboGraFX parallel port interface driver");41MODULE_LICENSE("GPL");4243#define TGFX_MAX_PORTS 344#define TGFX_MAX_DEVICES 74546struct tgfx_config {47int args[TGFX_MAX_DEVICES + 1];48unsigned int nargs;49};5051static struct tgfx_config tgfx_cfg[TGFX_MAX_PORTS] __initdata;5253module_param_array_named(map, tgfx_cfg[0].args, int, &tgfx_cfg[0].nargs, 0);54MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<js1>,<js2>,..<js7>");55module_param_array_named(map2, tgfx_cfg[1].args, int, &tgfx_cfg[1].nargs, 0);56MODULE_PARM_DESC(map2, "Describes second set of devices");57module_param_array_named(map3, tgfx_cfg[2].args, int, &tgfx_cfg[2].nargs, 0);58MODULE_PARM_DESC(map3, "Describes third set of devices");5960#define TGFX_REFRESH_TIME HZ/100 /* 10 ms */6162#define TGFX_TRIGGER 0x0863#define TGFX_UP 0x1064#define TGFX_DOWN 0x2065#define TGFX_LEFT 0x4066#define TGFX_RIGHT 0x806768#define TGFX_THUMB 0x0269#define TGFX_THUMB2 0x0470#define TGFX_TOP 0x0171#define TGFX_TOP2 0x087273static int tgfx_buttons[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2 };7475static struct tgfx {76struct pardevice *pd;77struct timer_list timer;78struct input_dev *dev[TGFX_MAX_DEVICES];79char name[TGFX_MAX_DEVICES][64];80char phys[TGFX_MAX_DEVICES][32];81int sticks;82int used;83struct mutex sem;84} *tgfx_base[TGFX_MAX_PORTS];8586/*87* tgfx_timer() reads and analyzes TurboGraFX joystick data.88*/8990static void tgfx_timer(unsigned long private)91{92struct tgfx *tgfx = (void *) private;93struct input_dev *dev;94int data1, data2, i;9596for (i = 0; i < 7; i++)97if (tgfx->sticks & (1 << i)) {9899dev = tgfx->dev[i];100101parport_write_data(tgfx->pd->port, ~(1 << i));102data1 = parport_read_status(tgfx->pd->port) ^ 0x7f;103data2 = parport_read_control(tgfx->pd->port) ^ 0x04; /* CAVEAT parport */104105input_report_abs(dev, ABS_X, !!(data1 & TGFX_RIGHT) - !!(data1 & TGFX_LEFT));106input_report_abs(dev, ABS_Y, !!(data1 & TGFX_DOWN ) - !!(data1 & TGFX_UP ));107108input_report_key(dev, BTN_TRIGGER, (data1 & TGFX_TRIGGER));109input_report_key(dev, BTN_THUMB, (data2 & TGFX_THUMB ));110input_report_key(dev, BTN_THUMB2, (data2 & TGFX_THUMB2 ));111input_report_key(dev, BTN_TOP, (data2 & TGFX_TOP ));112input_report_key(dev, BTN_TOP2, (data2 & TGFX_TOP2 ));113114input_sync(dev);115}116117mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);118}119120static int tgfx_open(struct input_dev *dev)121{122struct tgfx *tgfx = input_get_drvdata(dev);123int err;124125err = mutex_lock_interruptible(&tgfx->sem);126if (err)127return err;128129if (!tgfx->used++) {130parport_claim(tgfx->pd);131parport_write_control(tgfx->pd->port, 0x04);132mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);133}134135mutex_unlock(&tgfx->sem);136return 0;137}138139static void tgfx_close(struct input_dev *dev)140{141struct tgfx *tgfx = input_get_drvdata(dev);142143mutex_lock(&tgfx->sem);144if (!--tgfx->used) {145del_timer_sync(&tgfx->timer);146parport_write_control(tgfx->pd->port, 0x00);147parport_release(tgfx->pd);148}149mutex_unlock(&tgfx->sem);150}151152153154/*155* tgfx_probe() probes for tg gamepads.156*/157158static struct tgfx __init *tgfx_probe(int parport, int *n_buttons, int n_devs)159{160struct tgfx *tgfx;161struct input_dev *input_dev;162struct parport *pp;163struct pardevice *pd;164int i, j;165int err;166167pp = parport_find_number(parport);168if (!pp) {169printk(KERN_ERR "turbografx.c: no such parport\n");170err = -EINVAL;171goto err_out;172}173174pd = parport_register_device(pp, "turbografx", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);175if (!pd) {176printk(KERN_ERR "turbografx.c: parport busy already - lp.o loaded?\n");177err = -EBUSY;178goto err_put_pp;179}180181tgfx = kzalloc(sizeof(struct tgfx), GFP_KERNEL);182if (!tgfx) {183printk(KERN_ERR "turbografx.c: Not enough memory\n");184err = -ENOMEM;185goto err_unreg_pardev;186}187188mutex_init(&tgfx->sem);189tgfx->pd = pd;190init_timer(&tgfx->timer);191tgfx->timer.data = (long) tgfx;192tgfx->timer.function = tgfx_timer;193194for (i = 0; i < n_devs; i++) {195if (n_buttons[i] < 1)196continue;197198if (n_buttons[i] > 6) {199printk(KERN_ERR "turbografx.c: Invalid number of buttons %d\n", n_buttons[i]);200err = -EINVAL;201goto err_unreg_devs;202}203204tgfx->dev[i] = input_dev = input_allocate_device();205if (!input_dev) {206printk(KERN_ERR "turbografx.c: Not enough memory for input device\n");207err = -ENOMEM;208goto err_unreg_devs;209}210211tgfx->sticks |= (1 << i);212snprintf(tgfx->name[i], sizeof(tgfx->name[i]),213"TurboGraFX %d-button Multisystem joystick", n_buttons[i]);214snprintf(tgfx->phys[i], sizeof(tgfx->phys[i]),215"%s/input%d", tgfx->pd->port->name, i);216217input_dev->name = tgfx->name[i];218input_dev->phys = tgfx->phys[i];219input_dev->id.bustype = BUS_PARPORT;220input_dev->id.vendor = 0x0003;221input_dev->id.product = n_buttons[i];222input_dev->id.version = 0x0100;223224input_set_drvdata(input_dev, tgfx);225226input_dev->open = tgfx_open;227input_dev->close = tgfx_close;228229input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);230input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0);231input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0);232233for (j = 0; j < n_buttons[i]; j++)234set_bit(tgfx_buttons[j], input_dev->keybit);235236err = input_register_device(tgfx->dev[i]);237if (err)238goto err_free_dev;239}240241if (!tgfx->sticks) {242printk(KERN_ERR "turbografx.c: No valid devices specified\n");243err = -EINVAL;244goto err_free_tgfx;245}246247parport_put_port(pp);248return tgfx;249250err_free_dev:251input_free_device(tgfx->dev[i]);252err_unreg_devs:253while (--i >= 0)254if (tgfx->dev[i])255input_unregister_device(tgfx->dev[i]);256err_free_tgfx:257kfree(tgfx);258err_unreg_pardev:259parport_unregister_device(pd);260err_put_pp:261parport_put_port(pp);262err_out:263return ERR_PTR(err);264}265266static void tgfx_remove(struct tgfx *tgfx)267{268int i;269270for (i = 0; i < TGFX_MAX_DEVICES; i++)271if (tgfx->dev[i])272input_unregister_device(tgfx->dev[i]);273parport_unregister_device(tgfx->pd);274kfree(tgfx);275}276277static int __init tgfx_init(void)278{279int i;280int have_dev = 0;281int err = 0;282283for (i = 0; i < TGFX_MAX_PORTS; i++) {284if (tgfx_cfg[i].nargs == 0 || tgfx_cfg[i].args[0] < 0)285continue;286287if (tgfx_cfg[i].nargs < 2) {288printk(KERN_ERR "turbografx.c: at least one joystick must be specified\n");289err = -EINVAL;290break;291}292293tgfx_base[i] = tgfx_probe(tgfx_cfg[i].args[0],294tgfx_cfg[i].args + 1,295tgfx_cfg[i].nargs - 1);296if (IS_ERR(tgfx_base[i])) {297err = PTR_ERR(tgfx_base[i]);298break;299}300301have_dev = 1;302}303304if (err) {305while (--i >= 0)306if (tgfx_base[i])307tgfx_remove(tgfx_base[i]);308return err;309}310311return have_dev ? 0 : -ENODEV;312}313314static void __exit tgfx_exit(void)315{316int i;317318for (i = 0; i < TGFX_MAX_PORTS; i++)319if (tgfx_base[i])320tgfx_remove(tgfx_base[i]);321}322323module_init(tgfx_init);324module_exit(tgfx_exit);325326327