1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 1998-2001 Vojtech Pavlik 4 */ 5 6 /* 7 * Gravis/Kensington GrIP protocol joystick and gamepad 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 "Gravis GrIP protocol joystick driver" 18 19 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 20 MODULE_DESCRIPTION(DRIVER_DESC); 21 MODULE_LICENSE("GPL"); 22 23 #define GRIP_MODE_GPP 1 24 #define GRIP_MODE_BD 2 25 #define GRIP_MODE_XT 3 26 #define GRIP_MODE_DC 4 27 28 #define GRIP_LENGTH_GPP 24 29 #define GRIP_STROBE_GPP 200 /* 200 us */ 30 #define GRIP_LENGTH_XT 4 31 #define GRIP_STROBE_XT 64 /* 64 us */ 32 #define GRIP_MAX_CHUNKS_XT 10 33 #define GRIP_MAX_BITS_XT 30 34 35 struct grip { 36 struct gameport *gameport; 37 struct input_dev *dev[2]; 38 unsigned char mode[2]; 39 int reads; 40 int bads; 41 char phys[2][32]; 42 }; 43 44 static int grip_btn_gpp[] = { BTN_START, BTN_SELECT, BTN_TR2, BTN_Y, 0, BTN_TL2, BTN_A, BTN_B, BTN_X, 0, BTN_TL, BTN_TR, -1 }; 45 static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 }; 46 static int grip_btn_xt[] = { BTN_TRIGGER, BTN_THUMB, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_SELECT, BTN_START, BTN_MODE, -1 }; 47 static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 }; 48 49 static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 }; 50 static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 }; 51 static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 }; 52 static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 }; 53 54 static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital", 55 "Gravis Xterminator Digital", "Gravis Xterminator DualControl" }; 56 static int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc }; 57 static int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc }; 58 static char grip_anx[] = { 0, 0, 3, 5, 5 }; 59 static char grip_cen[] = { 0, 0, 2, 2, 4 }; 60 61 /* 62 * grip_gpp_read_packet() reads a Gravis GamePad Pro packet. 63 */ 64 65 static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data) 66 { 67 unsigned long flags; 68 unsigned char u, v; 69 unsigned int t; 70 int i; 71 72 int strobe = gameport_time(gameport, GRIP_STROBE_GPP); 73 74 data[0] = 0; 75 t = strobe; 76 i = 0; 77 78 local_irq_save(flags); 79 80 v = gameport_read(gameport) >> shift; 81 82 do { 83 t--; 84 u = v; v = (gameport_read(gameport) >> shift) & 3; 85 if (~v & u & 1) { 86 data[0] |= (v >> 1) << i++; 87 t = strobe; 88 } 89 } while (i < GRIP_LENGTH_GPP && t > 0); 90 91 local_irq_restore(flags); 92 93 if (i < GRIP_LENGTH_GPP) return -1; 94 95 for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++) 96 data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1); 97 98 return -(i == GRIP_LENGTH_GPP); 99 } 100 101 /* 102 * grip_xt_read_packet() reads a Gravis Xterminator packet. 103 */ 104 105 static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data) 106 { 107 unsigned int i, j, buf, crc; 108 unsigned char u, v, w; 109 unsigned long flags; 110 unsigned int t; 111 char status; 112 113 int strobe = gameport_time(gameport, GRIP_STROBE_XT); 114 115 data[0] = data[1] = data[2] = data[3] = 0; 116 status = buf = i = j = 0; 117 t = strobe; 118 119 local_irq_save(flags); 120 121 v = w = (gameport_read(gameport) >> shift) & 3; 122 123 do { 124 t--; 125 u = (gameport_read(gameport) >> shift) & 3; 126 127 if (u ^ v) { 128 129 if ((u ^ v) & 1) { 130 buf = (buf << 1) | (u >> 1); 131 t = strobe; 132 i++; 133 } else 134 135 if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) { 136 if (i == 20) { 137 crc = buf ^ (buf >> 7) ^ (buf >> 14); 138 if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) { 139 data[buf >> 18] = buf >> 4; 140 status |= 1 << (buf >> 18); 141 } 142 j++; 143 } 144 t = strobe; 145 buf = 0; 146 i = 0; 147 } 148 w = v; 149 v = u; 150 } 151 152 } while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0); 153 154 local_irq_restore(flags); 155 156 return -(status != 0xf); 157 } 158 159 /* 160 * grip_timer() repeatedly polls the joysticks and generates events. 161 */ 162 163 static void grip_poll(struct gameport *gameport) 164 { 165 struct grip *grip = gameport_get_drvdata(gameport); 166 unsigned int data[GRIP_LENGTH_XT]; 167 struct input_dev *dev; 168 int i, j; 169 170 for (i = 0; i < 2; i++) { 171 172 dev = grip->dev[i]; 173 if (!dev) 174 continue; 175 176 grip->reads++; 177 178 switch (grip->mode[i]) { 179 180 case GRIP_MODE_GPP: 181 182 if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) { 183 grip->bads++; 184 break; 185 } 186 187 input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1)); 188 input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1)); 189 190 for (j = 0; j < 12; j++) 191 if (grip_btn_gpp[j]) 192 input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1); 193 194 break; 195 196 case GRIP_MODE_BD: 197 198 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) { 199 grip->bads++; 200 break; 201 } 202 203 input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f); 204 input_report_abs(dev, ABS_Y, 63 - ((data[0] >> 8) & 0x3f)); 205 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f); 206 207 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1)); 208 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1)); 209 210 for (j = 0; j < 5; j++) 211 input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1); 212 213 break; 214 215 case GRIP_MODE_XT: 216 217 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) { 218 grip->bads++; 219 break; 220 } 221 222 input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f); 223 input_report_abs(dev, ABS_Y, 63 - ((data[0] >> 8) & 0x3f)); 224 input_report_abs(dev, ABS_BRAKE, (data[1] >> 2) & 0x3f); 225 input_report_abs(dev, ABS_GAS, (data[1] >> 8) & 0x3f); 226 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f); 227 228 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1)); 229 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1)); 230 input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1)); 231 input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1)); 232 233 for (j = 0; j < 11; j++) 234 input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1); 235 break; 236 237 case GRIP_MODE_DC: 238 239 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) { 240 grip->bads++; 241 break; 242 } 243 244 input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f); 245 input_report_abs(dev, ABS_Y, (data[0] >> 8) & 0x3f); 246 input_report_abs(dev, ABS_RX, (data[1] >> 2) & 0x3f); 247 input_report_abs(dev, ABS_RY, (data[1] >> 8) & 0x3f); 248 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f); 249 250 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1)); 251 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1)); 252 253 for (j = 0; j < 9; j++) 254 input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1); 255 break; 256 257 258 } 259 260 input_sync(dev); 261 } 262 } 263 264 static int grip_open(struct input_dev *dev) 265 { 266 struct grip *grip = input_get_drvdata(dev); 267 268 gameport_start_polling(grip->gameport); 269 return 0; 270 } 271 272 static void grip_close(struct input_dev *dev) 273 { 274 struct grip *grip = input_get_drvdata(dev); 275 276 gameport_stop_polling(grip->gameport); 277 } 278 279 static int grip_connect(struct gameport *gameport, struct gameport_driver *drv) 280 { 281 struct grip *grip; 282 struct input_dev *input_dev; 283 unsigned int data[GRIP_LENGTH_XT]; 284 int i, j, t; 285 int err; 286 287 grip = kzalloc(sizeof(*grip), GFP_KERNEL); 288 if (!grip) 289 return -ENOMEM; 290 291 grip->gameport = gameport; 292 293 gameport_set_drvdata(gameport, grip); 294 295 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW); 296 if (err) 297 goto fail1; 298 299 for (i = 0; i < 2; i++) { 300 if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) { 301 grip->mode[i] = GRIP_MODE_GPP; 302 continue; 303 } 304 if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) { 305 if (!(data[3] & 7)) { 306 grip->mode[i] = GRIP_MODE_BD; 307 continue; 308 } 309 if (!(data[2] & 0xf0)) { 310 grip->mode[i] = GRIP_MODE_XT; 311 continue; 312 } 313 grip->mode[i] = GRIP_MODE_DC; 314 continue; 315 } 316 } 317 318 if (!grip->mode[0] && !grip->mode[1]) { 319 err = -ENODEV; 320 goto fail2; 321 } 322 323 gameport_set_poll_handler(gameport, grip_poll); 324 gameport_set_poll_interval(gameport, 20); 325 326 for (i = 0; i < 2; i++) { 327 if (!grip->mode[i]) 328 continue; 329 330 grip->dev[i] = input_dev = input_allocate_device(); 331 if (!input_dev) { 332 err = -ENOMEM; 333 goto fail3; 334 } 335 336 snprintf(grip->phys[i], sizeof(grip->phys[i]), 337 "%s/input%d", gameport->phys, i); 338 339 input_dev->name = grip_name[grip->mode[i]]; 340 input_dev->phys = grip->phys[i]; 341 input_dev->id.bustype = BUS_GAMEPORT; 342 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS; 343 input_dev->id.product = grip->mode[i]; 344 input_dev->id.version = 0x0100; 345 input_dev->dev.parent = &gameport->dev; 346 347 input_set_drvdata(input_dev, grip); 348 349 input_dev->open = grip_open; 350 input_dev->close = grip_close; 351 352 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 353 354 for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) { 355 356 if (j < grip_cen[grip->mode[i]]) 357 input_set_abs_params(input_dev, t, 14, 52, 1, 2); 358 else if (j < grip_anx[grip->mode[i]]) 359 input_set_abs_params(input_dev, t, 3, 57, 1, 0); 360 else 361 input_set_abs_params(input_dev, t, -1, 1, 0, 0); 362 } 363 364 for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++) 365 if (t > 0) 366 set_bit(t, input_dev->keybit); 367 368 err = input_register_device(grip->dev[i]); 369 if (err) 370 goto fail4; 371 } 372 373 return 0; 374 375 fail4: input_free_device(grip->dev[i]); 376 fail3: while (--i >= 0) 377 if (grip->dev[i]) 378 input_unregister_device(grip->dev[i]); 379 fail2: gameport_close(gameport); 380 fail1: gameport_set_drvdata(gameport, NULL); 381 kfree(grip); 382 return err; 383 } 384 385 static void grip_disconnect(struct gameport *gameport) 386 { 387 struct grip *grip = gameport_get_drvdata(gameport); 388 int i; 389 390 for (i = 0; i < 2; i++) 391 if (grip->dev[i]) 392 input_unregister_device(grip->dev[i]); 393 gameport_close(gameport); 394 gameport_set_drvdata(gameport, NULL); 395 kfree(grip); 396 } 397 398 static struct gameport_driver grip_drv = { 399 .driver = { 400 .name = "grip", 401 .owner = THIS_MODULE, 402 }, 403 .description = DRIVER_DESC, 404 .connect = grip_connect, 405 .disconnect = grip_disconnect, 406 }; 407 408 module_gameport_driver(grip_drv); 409