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