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