1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 1998-2001 Vojtech Pavlik 4 */ 5 6 /* 7 * FP-Gaming Assassin 3D joystick driver for Linux 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/slab.h> 13 #include <linux/gameport.h> 14 #include <linux/input.h> 15 #include <linux/jiffies.h> 16 17 #define DRIVER_DESC "FP-Gaming Assassin 3D joystick driver" 18 19 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 20 MODULE_DESCRIPTION(DRIVER_DESC); 21 MODULE_LICENSE("GPL"); 22 23 #define A3D_MAX_START 600 /* 600 us */ 24 #define A3D_MAX_STROBE 80 /* 80 us */ 25 #define A3D_MAX_LENGTH 40 /* 40*3 bits */ 26 27 #define A3D_MODE_A3D 1 /* Assassin 3D */ 28 #define A3D_MODE_PAN 2 /* Panther */ 29 #define A3D_MODE_OEM 3 /* Panther OEM version */ 30 #define A3D_MODE_PXL 4 /* Panther XL */ 31 32 static char *a3d_names[] = { NULL, "FP-Gaming Assassin 3D", "MadCatz Panther", "OEM Panther", 33 "MadCatz Panther XL", "MadCatz Panther XL w/ rudder" }; 34 35 struct a3d { 36 struct gameport *gameport; 37 struct gameport *adc; 38 struct input_dev *dev; 39 int axes[4]; 40 int buttons; 41 int mode; 42 int length; 43 int reads; 44 int bads; 45 char phys[32]; 46 }; 47 48 /* 49 * a3d_read_packet() reads an Assassin 3D packet. 50 */ 51 52 static int a3d_read_packet(struct gameport *gameport, int length, char *data) 53 { 54 unsigned long flags; 55 unsigned char u, v; 56 unsigned int t, s; 57 int i; 58 59 i = 0; 60 t = gameport_time(gameport, A3D_MAX_START); 61 s = gameport_time(gameport, A3D_MAX_STROBE); 62 63 local_irq_save(flags); 64 gameport_trigger(gameport); 65 v = gameport_read(gameport); 66 67 while (t > 0 && i < length) { 68 t--; 69 u = v; v = gameport_read(gameport); 70 if (~v & u & 0x10) { 71 data[i++] = v >> 5; 72 t = s; 73 } 74 } 75 76 local_irq_restore(flags); 77 78 return i; 79 } 80 81 /* 82 * a3d_csum() computes checksum of triplet packet 83 */ 84 85 static int a3d_csum(char *data, int count) 86 { 87 int i, csum = 0; 88 89 for (i = 0; i < count - 2; i++) 90 csum += data[i]; 91 return (csum & 0x3f) != ((data[count - 2] << 3) | data[count - 1]); 92 } 93 94 static void a3d_read(struct a3d *a3d, unsigned char *data) 95 { 96 struct input_dev *dev = a3d->dev; 97 98 switch (a3d->mode) { 99 100 case A3D_MODE_A3D: 101 case A3D_MODE_OEM: 102 case A3D_MODE_PAN: 103 104 input_report_rel(dev, REL_X, ((data[5] << 6) | (data[6] << 3) | data[ 7]) - ((data[5] & 4) << 7)); 105 input_report_rel(dev, REL_Y, ((data[8] << 6) | (data[9] << 3) | data[10]) - ((data[8] & 4) << 7)); 106 107 input_report_key(dev, BTN_RIGHT, data[2] & 1); 108 input_report_key(dev, BTN_LEFT, data[3] & 2); 109 input_report_key(dev, BTN_MIDDLE, data[3] & 4); 110 111 input_sync(dev); 112 113 a3d->axes[0] = ((signed char)((data[11] << 6) | (data[12] << 3) | (data[13]))) + 128; 114 a3d->axes[1] = ((signed char)((data[14] << 6) | (data[15] << 3) | (data[16]))) + 128; 115 a3d->axes[2] = ((signed char)((data[17] << 6) | (data[18] << 3) | (data[19]))) + 128; 116 a3d->axes[3] = ((signed char)((data[20] << 6) | (data[21] << 3) | (data[22]))) + 128; 117 118 a3d->buttons = ((data[3] << 3) | data[4]) & 0xf; 119 120 break; 121 122 case A3D_MODE_PXL: 123 124 input_report_rel(dev, REL_X, ((data[ 9] << 6) | (data[10] << 3) | data[11]) - ((data[ 9] & 4) << 7)); 125 input_report_rel(dev, REL_Y, ((data[12] << 6) | (data[13] << 3) | data[14]) - ((data[12] & 4) << 7)); 126 127 input_report_key(dev, BTN_RIGHT, data[2] & 1); 128 input_report_key(dev, BTN_LEFT, data[3] & 2); 129 input_report_key(dev, BTN_MIDDLE, data[3] & 4); 130 input_report_key(dev, BTN_SIDE, data[7] & 2); 131 input_report_key(dev, BTN_EXTRA, data[7] & 4); 132 133 input_report_abs(dev, ABS_X, ((signed char)((data[15] << 6) | (data[16] << 3) | (data[17]))) + 128); 134 input_report_abs(dev, ABS_Y, ((signed char)((data[18] << 6) | (data[19] << 3) | (data[20]))) + 128); 135 input_report_abs(dev, ABS_RUDDER, ((signed char)((data[21] << 6) | (data[22] << 3) | (data[23]))) + 128); 136 input_report_abs(dev, ABS_THROTTLE, ((signed char)((data[24] << 6) | (data[25] << 3) | (data[26]))) + 128); 137 138 input_report_abs(dev, ABS_HAT0X, ( data[5] & 1) - ((data[5] >> 2) & 1)); 139 input_report_abs(dev, ABS_HAT0Y, ((data[5] >> 1) & 1) - ((data[6] >> 2) & 1)); 140 input_report_abs(dev, ABS_HAT1X, ((data[4] >> 1) & 1) - ( data[3] & 1)); 141 input_report_abs(dev, ABS_HAT1Y, ((data[4] >> 2) & 1) - ( data[4] & 1)); 142 143 input_report_key(dev, BTN_TRIGGER, data[8] & 1); 144 input_report_key(dev, BTN_THUMB, data[8] & 2); 145 input_report_key(dev, BTN_TOP, data[8] & 4); 146 input_report_key(dev, BTN_PINKIE, data[7] & 1); 147 148 input_sync(dev); 149 150 break; 151 } 152 } 153 154 155 /* 156 * a3d_poll() reads and analyzes A3D joystick data. 157 */ 158 159 static void a3d_poll(struct gameport *gameport) 160 { 161 struct a3d *a3d = gameport_get_drvdata(gameport); 162 unsigned char data[A3D_MAX_LENGTH]; 163 164 a3d->reads++; 165 if (a3d_read_packet(a3d->gameport, a3d->length, data) != a3d->length || 166 data[0] != a3d->mode || a3d_csum(data, a3d->length)) 167 a3d->bads++; 168 else 169 a3d_read(a3d, data); 170 } 171 172 /* 173 * a3d_adc_cooked_read() copies the acis and button data to the 174 * callers arrays. It could do the read itself, but the caller could 175 * call this more than 50 times a second, which would use too much CPU. 176 */ 177 178 static int a3d_adc_cooked_read(struct gameport *gameport, int *axes, int *buttons) 179 { 180 struct a3d *a3d = gameport->port_data; 181 int i; 182 183 for (i = 0; i < 4; i++) 184 axes[i] = (a3d->axes[i] < 254) ? a3d->axes[i] : -1; 185 *buttons = a3d->buttons; 186 return 0; 187 } 188 189 /* 190 * a3d_adc_open() is the gameport open routine. It refuses to serve 191 * any but cooked data. 192 */ 193 194 static int a3d_adc_open(struct gameport *gameport, int mode) 195 { 196 struct a3d *a3d = gameport->port_data; 197 198 if (mode != GAMEPORT_MODE_COOKED) 199 return -1; 200 201 gameport_start_polling(a3d->gameport); 202 return 0; 203 } 204 205 /* 206 * a3d_adc_close() is a callback from the input close routine. 207 */ 208 209 static void a3d_adc_close(struct gameport *gameport) 210 { 211 struct a3d *a3d = gameport->port_data; 212 213 gameport_stop_polling(a3d->gameport); 214 } 215 216 /* 217 * a3d_open() is a callback from the input open routine. 218 */ 219 220 static int a3d_open(struct input_dev *dev) 221 { 222 struct a3d *a3d = input_get_drvdata(dev); 223 224 gameport_start_polling(a3d->gameport); 225 return 0; 226 } 227 228 /* 229 * a3d_close() is a callback from the input close routine. 230 */ 231 232 static void a3d_close(struct input_dev *dev) 233 { 234 struct a3d *a3d = input_get_drvdata(dev); 235 236 gameport_stop_polling(a3d->gameport); 237 } 238 239 /* 240 * a3d_connect() probes for A3D joysticks. 241 */ 242 243 static int a3d_connect(struct gameport *gameport, struct gameport_driver *drv) 244 { 245 struct a3d *a3d; 246 struct input_dev *input_dev; 247 struct gameport *adc; 248 unsigned char data[A3D_MAX_LENGTH]; 249 int i; 250 int err; 251 252 a3d = kzalloc(sizeof(*a3d), GFP_KERNEL); 253 input_dev = input_allocate_device(); 254 if (!a3d || !input_dev) { 255 err = -ENOMEM; 256 goto fail1; 257 } 258 259 a3d->dev = input_dev; 260 a3d->gameport = gameport; 261 262 gameport_set_drvdata(gameport, a3d); 263 264 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW); 265 if (err) 266 goto fail1; 267 268 i = a3d_read_packet(gameport, A3D_MAX_LENGTH, data); 269 270 if (!i || a3d_csum(data, i)) { 271 err = -ENODEV; 272 goto fail2; 273 } 274 275 a3d->mode = data[0]; 276 277 if (!a3d->mode || a3d->mode > 5) { 278 printk(KERN_WARNING "a3d.c: Unknown A3D device detected " 279 "(%s, id=%d), contact <vojtech@ucw.cz>\n", gameport->phys, a3d->mode); 280 err = -ENODEV; 281 goto fail2; 282 } 283 284 gameport_set_poll_handler(gameport, a3d_poll); 285 gameport_set_poll_interval(gameport, 20); 286 287 snprintf(a3d->phys, sizeof(a3d->phys), "%s/input0", gameport->phys); 288 289 input_dev->name = a3d_names[a3d->mode]; 290 input_dev->phys = a3d->phys; 291 input_dev->id.bustype = BUS_GAMEPORT; 292 input_dev->id.vendor = GAMEPORT_ID_VENDOR_MADCATZ; 293 input_dev->id.product = a3d->mode; 294 input_dev->id.version = 0x0100; 295 input_dev->dev.parent = &gameport->dev; 296 input_dev->open = a3d_open; 297 input_dev->close = a3d_close; 298 299 input_set_drvdata(input_dev, a3d); 300 301 if (a3d->mode == A3D_MODE_PXL) { 302 303 int axes[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER }; 304 305 a3d->length = 33; 306 307 input_dev->evbit[0] |= BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY) | 308 BIT_MASK(EV_REL); 309 input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y); 310 input_dev->absbit[0] |= BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) | 311 BIT_MASK(ABS_THROTTLE) | BIT_MASK(ABS_RUDDER) | 312 BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) | 313 BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y); 314 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) | 315 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | 316 BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA); 317 input_dev->keybit[BIT_WORD(BTN_JOYSTICK)] |= 318 BIT_MASK(BTN_TRIGGER) | BIT_MASK(BTN_THUMB) | 319 BIT_MASK(BTN_TOP) | BIT_MASK(BTN_PINKIE); 320 321 a3d_read(a3d, data); 322 323 for (i = 0; i < 4; i++) { 324 if (i < 2) 325 input_set_abs_params(input_dev, axes[i], 326 48, input_abs_get_val(input_dev, axes[i]) * 2 - 48, 0, 8); 327 else 328 input_set_abs_params(input_dev, axes[i], 2, 253, 0, 0); 329 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0); 330 } 331 332 } else { 333 a3d->length = 29; 334 335 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); 336 input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y); 337 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) | 338 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE); 339 340 a3d_read(a3d, data); 341 342 if (!(a3d->adc = adc = gameport_allocate_port())) 343 printk(KERN_ERR "a3d: Not enough memory for ADC port\n"); 344 else { 345 adc->port_data = a3d; 346 adc->open = a3d_adc_open; 347 adc->close = a3d_adc_close; 348 adc->cooked_read = a3d_adc_cooked_read; 349 adc->fuzz = 1; 350 351 gameport_set_name(adc, a3d_names[a3d->mode]); 352 gameport_set_phys(adc, "%s/gameport0", gameport->phys); 353 adc->dev.parent = &gameport->dev; 354 355 gameport_register_port(adc); 356 } 357 } 358 359 err = input_register_device(a3d->dev); 360 if (err) 361 goto fail3; 362 363 return 0; 364 365 fail3: if (a3d->adc) 366 gameport_unregister_port(a3d->adc); 367 fail2: gameport_close(gameport); 368 fail1: gameport_set_drvdata(gameport, NULL); 369 input_free_device(input_dev); 370 kfree(a3d); 371 return err; 372 } 373 374 static void a3d_disconnect(struct gameport *gameport) 375 { 376 struct a3d *a3d = gameport_get_drvdata(gameport); 377 378 input_unregister_device(a3d->dev); 379 if (a3d->adc) 380 gameport_unregister_port(a3d->adc); 381 gameport_close(gameport); 382 gameport_set_drvdata(gameport, NULL); 383 kfree(a3d); 384 } 385 386 static struct gameport_driver a3d_drv = { 387 .driver = { 388 .name = "adc", 389 .owner = THIS_MODULE, 390 }, 391 .description = DRIVER_DESC, 392 .connect = a3d_connect, 393 .disconnect = a3d_disconnect, 394 }; 395 396 module_gameport_driver(a3d_drv); 397