root/drivers/input/joystick/turbografx.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. tgfx_timer
  2. tgfx_open
  3. tgfx_close
  4. tgfx_attach
  5. tgfx_detach
  6. tgfx_init
  7. tgfx_exit

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  *  Copyright (c) 1998-2001 Vojtech Pavlik
   4  *
   5  *  Based on the work of:
   6  *      Steffen Schwenke
   7  */
   8 
   9 /*
  10  * TurboGraFX parallel port interface driver for Linux.
  11  */
  12 
  13 /*
  14  */
  15 
  16 #include <linux/kernel.h>
  17 #include <linux/parport.h>
  18 #include <linux/input.h>
  19 #include <linux/module.h>
  20 #include <linux/init.h>
  21 #include <linux/mutex.h>
  22 #include <linux/slab.h>
  23 
  24 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  25 MODULE_DESCRIPTION("TurboGraFX parallel port interface driver");
  26 MODULE_LICENSE("GPL");
  27 
  28 #define TGFX_MAX_PORTS          3
  29 #define TGFX_MAX_DEVICES        7
  30 
  31 struct tgfx_config {
  32         int args[TGFX_MAX_DEVICES + 1];
  33         unsigned int nargs;
  34 };
  35 
  36 static struct tgfx_config tgfx_cfg[TGFX_MAX_PORTS];
  37 
  38 module_param_array_named(map, tgfx_cfg[0].args, int, &tgfx_cfg[0].nargs, 0);
  39 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<js1>,<js2>,..<js7>");
  40 module_param_array_named(map2, tgfx_cfg[1].args, int, &tgfx_cfg[1].nargs, 0);
  41 MODULE_PARM_DESC(map2, "Describes second set of devices");
  42 module_param_array_named(map3, tgfx_cfg[2].args, int, &tgfx_cfg[2].nargs, 0);
  43 MODULE_PARM_DESC(map3, "Describes third set of devices");
  44 
  45 #define TGFX_REFRESH_TIME       HZ/100  /* 10 ms */
  46 
  47 #define TGFX_TRIGGER            0x08
  48 #define TGFX_UP                 0x10
  49 #define TGFX_DOWN               0x20
  50 #define TGFX_LEFT               0x40
  51 #define TGFX_RIGHT              0x80
  52 
  53 #define TGFX_THUMB              0x02
  54 #define TGFX_THUMB2             0x04
  55 #define TGFX_TOP                0x01
  56 #define TGFX_TOP2               0x08
  57 
  58 static int tgfx_buttons[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2 };
  59 
  60 static struct tgfx {
  61         struct pardevice *pd;
  62         struct timer_list timer;
  63         struct input_dev *dev[TGFX_MAX_DEVICES];
  64         char name[TGFX_MAX_DEVICES][64];
  65         char phys[TGFX_MAX_DEVICES][32];
  66         int sticks;
  67         int used;
  68         int parportno;
  69         struct mutex sem;
  70 } *tgfx_base[TGFX_MAX_PORTS];
  71 
  72 /*
  73  * tgfx_timer() reads and analyzes TurboGraFX joystick data.
  74  */
  75 
  76 static void tgfx_timer(struct timer_list *t)
  77 {
  78         struct tgfx *tgfx = from_timer(tgfx, t, timer);
  79         struct input_dev *dev;
  80         int data1, data2, i;
  81 
  82         for (i = 0; i < 7; i++)
  83                 if (tgfx->sticks & (1 << i)) {
  84 
  85                         dev = tgfx->dev[i];
  86 
  87                         parport_write_data(tgfx->pd->port, ~(1 << i));
  88                         data1 = parport_read_status(tgfx->pd->port) ^ 0x7f;
  89                         data2 = parport_read_control(tgfx->pd->port) ^ 0x04;    /* CAVEAT parport */
  90 
  91                         input_report_abs(dev, ABS_X, !!(data1 & TGFX_RIGHT) - !!(data1 & TGFX_LEFT));
  92                         input_report_abs(dev, ABS_Y, !!(data1 & TGFX_DOWN ) - !!(data1 & TGFX_UP  ));
  93 
  94                         input_report_key(dev, BTN_TRIGGER, (data1 & TGFX_TRIGGER));
  95                         input_report_key(dev, BTN_THUMB,   (data2 & TGFX_THUMB  ));
  96                         input_report_key(dev, BTN_THUMB2,  (data2 & TGFX_THUMB2 ));
  97                         input_report_key(dev, BTN_TOP,     (data2 & TGFX_TOP    ));
  98                         input_report_key(dev, BTN_TOP2,    (data2 & TGFX_TOP2   ));
  99 
 100                         input_sync(dev);
 101                 }
 102 
 103         mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
 104 }
 105 
 106 static int tgfx_open(struct input_dev *dev)
 107 {
 108         struct tgfx *tgfx = input_get_drvdata(dev);
 109         int err;
 110 
 111         err = mutex_lock_interruptible(&tgfx->sem);
 112         if (err)
 113                 return err;
 114 
 115         if (!tgfx->used++) {
 116                 parport_claim(tgfx->pd);
 117                 parport_write_control(tgfx->pd->port, 0x04);
 118                 mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
 119         }
 120 
 121         mutex_unlock(&tgfx->sem);
 122         return 0;
 123 }
 124 
 125 static void tgfx_close(struct input_dev *dev)
 126 {
 127         struct tgfx *tgfx = input_get_drvdata(dev);
 128 
 129         mutex_lock(&tgfx->sem);
 130         if (!--tgfx->used) {
 131                 del_timer_sync(&tgfx->timer);
 132                 parport_write_control(tgfx->pd->port, 0x00);
 133                 parport_release(tgfx->pd);
 134         }
 135         mutex_unlock(&tgfx->sem);
 136 }
 137 
 138 
 139 
 140 /*
 141  * tgfx_probe() probes for tg gamepads.
 142  */
 143 
 144 static void tgfx_attach(struct parport *pp)
 145 {
 146         struct tgfx *tgfx;
 147         struct input_dev *input_dev;
 148         struct pardevice *pd;
 149         int i, j, port_idx;
 150         int *n_buttons, n_devs;
 151         struct pardev_cb tgfx_parport_cb;
 152 
 153         for (port_idx = 0; port_idx < TGFX_MAX_PORTS; port_idx++) {
 154                 if (tgfx_cfg[port_idx].nargs == 0 ||
 155                     tgfx_cfg[port_idx].args[0] < 0)
 156                         continue;
 157                 if (tgfx_cfg[port_idx].args[0] == pp->number)
 158                         break;
 159         }
 160 
 161         if (port_idx == TGFX_MAX_PORTS) {
 162                 pr_debug("Not using parport%d.\n", pp->number);
 163                 return;
 164         }
 165         n_buttons = tgfx_cfg[port_idx].args + 1;
 166         n_devs = tgfx_cfg[port_idx].nargs - 1;
 167 
 168         memset(&tgfx_parport_cb, 0, sizeof(tgfx_parport_cb));
 169         tgfx_parport_cb.flags = PARPORT_FLAG_EXCL;
 170 
 171         pd = parport_register_dev_model(pp, "turbografx", &tgfx_parport_cb,
 172                                         port_idx);
 173         if (!pd) {
 174                 pr_err("parport busy already - lp.o loaded?\n");
 175                 return;
 176         }
 177 
 178         tgfx = kzalloc(sizeof(struct tgfx), GFP_KERNEL);
 179         if (!tgfx) {
 180                 printk(KERN_ERR "turbografx.c: Not enough memory\n");
 181                 goto err_unreg_pardev;
 182         }
 183 
 184         mutex_init(&tgfx->sem);
 185         tgfx->pd = pd;
 186         tgfx->parportno = pp->number;
 187         timer_setup(&tgfx->timer, tgfx_timer, 0);
 188 
 189         for (i = 0; i < n_devs; i++) {
 190                 if (n_buttons[i] < 1)
 191                         continue;
 192 
 193                 if (n_buttons[i] > ARRAY_SIZE(tgfx_buttons)) {
 194                         printk(KERN_ERR "turbografx.c: Invalid number of buttons %d\n", n_buttons[i]);
 195                         goto err_unreg_devs;
 196                 }
 197 
 198                 tgfx->dev[i] = input_dev = input_allocate_device();
 199                 if (!input_dev) {
 200                         printk(KERN_ERR "turbografx.c: Not enough memory for input device\n");
 201                         goto err_unreg_devs;
 202                 }
 203 
 204                 tgfx->sticks |= (1 << i);
 205                 snprintf(tgfx->name[i], sizeof(tgfx->name[i]),
 206                          "TurboGraFX %d-button Multisystem joystick", n_buttons[i]);
 207                 snprintf(tgfx->phys[i], sizeof(tgfx->phys[i]),
 208                          "%s/input%d", tgfx->pd->port->name, i);
 209 
 210                 input_dev->name = tgfx->name[i];
 211                 input_dev->phys = tgfx->phys[i];
 212                 input_dev->id.bustype = BUS_PARPORT;
 213                 input_dev->id.vendor = 0x0003;
 214                 input_dev->id.product = n_buttons[i];
 215                 input_dev->id.version = 0x0100;
 216 
 217                 input_set_drvdata(input_dev, tgfx);
 218 
 219                 input_dev->open = tgfx_open;
 220                 input_dev->close = tgfx_close;
 221 
 222                 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
 223                 input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0);
 224                 input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0);
 225 
 226                 for (j = 0; j < n_buttons[i]; j++)
 227                         set_bit(tgfx_buttons[j], input_dev->keybit);
 228 
 229                 if (input_register_device(tgfx->dev[i]))
 230                         goto err_free_dev;
 231         }
 232 
 233         if (!tgfx->sticks) {
 234                 printk(KERN_ERR "turbografx.c: No valid devices specified\n");
 235                 goto err_free_tgfx;
 236         }
 237 
 238         tgfx_base[port_idx] = tgfx;
 239         return;
 240 
 241  err_free_dev:
 242         input_free_device(tgfx->dev[i]);
 243  err_unreg_devs:
 244         while (--i >= 0)
 245                 if (tgfx->dev[i])
 246                         input_unregister_device(tgfx->dev[i]);
 247  err_free_tgfx:
 248         kfree(tgfx);
 249  err_unreg_pardev:
 250         parport_unregister_device(pd);
 251 }
 252 
 253 static void tgfx_detach(struct parport *port)
 254 {
 255         int i;
 256         struct tgfx *tgfx;
 257 
 258         for (i = 0; i < TGFX_MAX_PORTS; i++) {
 259                 if (tgfx_base[i] && tgfx_base[i]->parportno == port->number)
 260                         break;
 261         }
 262 
 263         if (i == TGFX_MAX_PORTS)
 264                 return;
 265 
 266         tgfx = tgfx_base[i];
 267         tgfx_base[i] = NULL;
 268 
 269         for (i = 0; i < TGFX_MAX_DEVICES; i++)
 270                 if (tgfx->dev[i])
 271                         input_unregister_device(tgfx->dev[i]);
 272         parport_unregister_device(tgfx->pd);
 273         kfree(tgfx);
 274 }
 275 
 276 static struct parport_driver tgfx_parport_driver = {
 277         .name = "turbografx",
 278         .match_port = tgfx_attach,
 279         .detach = tgfx_detach,
 280         .devmodel = true,
 281 };
 282 
 283 static int __init tgfx_init(void)
 284 {
 285         int i;
 286         int have_dev = 0;
 287 
 288         for (i = 0; i < TGFX_MAX_PORTS; i++) {
 289                 if (tgfx_cfg[i].nargs == 0 || tgfx_cfg[i].args[0] < 0)
 290                         continue;
 291 
 292                 if (tgfx_cfg[i].nargs < 2) {
 293                         printk(KERN_ERR "turbografx.c: at least one joystick must be specified\n");
 294                         return -EINVAL;
 295                 }
 296 
 297                 have_dev = 1;
 298         }
 299 
 300         if (!have_dev)
 301                 return -ENODEV;
 302 
 303         return parport_register_driver(&tgfx_parport_driver);
 304 }
 305 
 306 static void __exit tgfx_exit(void)
 307 {
 308         parport_unregister_driver(&tgfx_parport_driver);
 309 }
 310 
 311 module_init(tgfx_init);
 312 module_exit(tgfx_exit);

/* [<][>][^][v][top][bottom][index][help] */