Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * $Id: analog.c,v 1.68 2002/01/22 20:18:32 vojtech Exp $ |
| 3 | * |
| 4 | * Copyright (c) 1996-2001 Vojtech Pavlik |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Analog joystick and gamepad driver for Linux |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | * Should you need to contact me, the author, you can do so either by |
| 27 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
| 28 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 29 | */ |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/delay.h> |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/moduleparam.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/bitops.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <linux/input.h> |
| 39 | #include <linux/gameport.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 40 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/timex.h> |
| 42 | |
| 43 | #define DRIVER_DESC "Analog joystick and gamepad driver" |
| 44 | |
| 45 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 46 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 47 | MODULE_LICENSE("GPL"); |
| 48 | |
| 49 | /* |
| 50 | * Option parsing. |
| 51 | */ |
| 52 | |
| 53 | #define ANALOG_PORTS 16 |
| 54 | |
| 55 | static char *js[ANALOG_PORTS]; |
Dmitry Torokhov | 7816723 | 2007-05-03 00:52:51 -0400 | [diff] [blame^] | 56 | static unsigned int js_nargs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static int analog_options[ANALOG_PORTS]; |
| 58 | module_param_array_named(map, js, charp, &js_nargs, 0); |
| 59 | MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities"); |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Times, feature definitions. |
| 63 | */ |
| 64 | |
| 65 | #define ANALOG_RUDDER 0x00004 |
| 66 | #define ANALOG_THROTTLE 0x00008 |
| 67 | #define ANALOG_AXES_STD 0x0000f |
| 68 | #define ANALOG_BTNS_STD 0x000f0 |
| 69 | |
| 70 | #define ANALOG_BTNS_CHF 0x00100 |
| 71 | #define ANALOG_HAT1_CHF 0x00200 |
| 72 | #define ANALOG_HAT2_CHF 0x00400 |
| 73 | #define ANALOG_HAT_FCS 0x00800 |
| 74 | #define ANALOG_HATS_ALL 0x00e00 |
| 75 | #define ANALOG_BTN_TL 0x01000 |
| 76 | #define ANALOG_BTN_TR 0x02000 |
| 77 | #define ANALOG_BTN_TL2 0x04000 |
| 78 | #define ANALOG_BTN_TR2 0x08000 |
| 79 | #define ANALOG_BTNS_TLR 0x03000 |
| 80 | #define ANALOG_BTNS_TLR2 0x0c000 |
| 81 | #define ANALOG_BTNS_GAMEPAD 0x0f000 |
| 82 | |
| 83 | #define ANALOG_HBTN_CHF 0x10000 |
| 84 | #define ANALOG_ANY_CHF 0x10700 |
| 85 | #define ANALOG_SAITEK 0x20000 |
| 86 | #define ANALOG_EXTENSIONS 0x7ff00 |
| 87 | #define ANALOG_GAMEPAD 0x80000 |
| 88 | |
| 89 | #define ANALOG_MAX_TIME 3 /* 3 ms */ |
| 90 | #define ANALOG_LOOP_TIME 2000 /* 2 * loop */ |
| 91 | #define ANALOG_SAITEK_DELAY 200 /* 200 us */ |
| 92 | #define ANALOG_SAITEK_TIME 2000 /* 2000 us */ |
| 93 | #define ANALOG_AXIS_TIME 2 /* 2 * refresh */ |
| 94 | #define ANALOG_INIT_RETRIES 8 /* 8 times */ |
| 95 | #define ANALOG_FUZZ_BITS 2 /* 2 bit more */ |
| 96 | #define ANALOG_FUZZ_MAGIC 36 /* 36 u*ms/loop */ |
| 97 | |
| 98 | #define ANALOG_MAX_NAME_LENGTH 128 |
| 99 | #define ANALOG_MAX_PHYS_LENGTH 32 |
| 100 | |
| 101 | static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE }; |
| 102 | static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y }; |
| 103 | static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR }; |
| 104 | static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS }; |
| 105 | static short analog_pad_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_TL2, BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_BASE }; |
| 106 | static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, |
| 107 | BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 }; |
| 108 | |
| 109 | static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 }; |
| 110 | |
| 111 | struct analog { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 112 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | int mask; |
| 114 | short *buttons; |
| 115 | char name[ANALOG_MAX_NAME_LENGTH]; |
| 116 | char phys[ANALOG_MAX_PHYS_LENGTH]; |
| 117 | }; |
| 118 | |
| 119 | struct analog_port { |
| 120 | struct gameport *gameport; |
| 121 | struct analog analog[2]; |
| 122 | unsigned char mask; |
| 123 | char saitek; |
| 124 | char cooked; |
| 125 | int bads; |
| 126 | int reads; |
| 127 | int speed; |
| 128 | int loop; |
| 129 | int fuzz; |
| 130 | int axes[4]; |
| 131 | int buttons; |
| 132 | int initial[4]; |
| 133 | int axtime; |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * Time macros. |
| 138 | */ |
| 139 | |
| 140 | #ifdef __i386__ |
Ingo Molnar | 306e440 | 2005-06-30 02:58:55 -0700 | [diff] [blame] | 141 | |
| 142 | #include <asm/i8253.h> |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | #define GET_TIME(x) do { if (cpu_has_tsc) rdtscl(x); else x = get_time_pit(); } while (0) |
| 145 | #define DELTA(x,y) (cpu_has_tsc ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? CLOCK_TICK_RATE / HZ : 0))) |
| 146 | #define TIME_NAME (cpu_has_tsc?"TSC":"PIT") |
| 147 | static unsigned int get_time_pit(void) |
| 148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | unsigned long flags; |
| 150 | unsigned int count; |
| 151 | |
| 152 | spin_lock_irqsave(&i8253_lock, flags); |
| 153 | outb_p(0x00, 0x43); |
| 154 | count = inb_p(0x40); |
| 155 | count |= inb_p(0x40) << 8; |
| 156 | spin_unlock_irqrestore(&i8253_lock, flags); |
| 157 | |
| 158 | return count; |
| 159 | } |
| 160 | #elif defined(__x86_64__) |
| 161 | #define GET_TIME(x) rdtscl(x) |
| 162 | #define DELTA(x,y) ((y)-(x)) |
| 163 | #define TIME_NAME "TSC" |
| 164 | #elif defined(__alpha__) |
| 165 | #define GET_TIME(x) do { x = get_cycles(); } while (0) |
| 166 | #define DELTA(x,y) ((y)-(x)) |
| 167 | #define TIME_NAME "PCC" |
| 168 | #else |
| 169 | #define FAKE_TIME |
| 170 | static unsigned long analog_faketime = 0; |
| 171 | #define GET_TIME(x) do { x = analog_faketime++; } while(0) |
| 172 | #define DELTA(x,y) ((y)-(x)) |
| 173 | #define TIME_NAME "Unreliable" |
| 174 | #warning Precise timer not defined for this architecture. |
| 175 | #endif |
| 176 | |
| 177 | /* |
| 178 | * analog_decode() decodes analog joystick data and reports input events. |
| 179 | */ |
| 180 | |
| 181 | static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons) |
| 182 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 183 | struct input_dev *dev = analog->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int i, j; |
| 185 | |
| 186 | if (analog->mask & ANALOG_HAT_FCS) |
| 187 | for (i = 0; i < 4; i++) |
| 188 | if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) { |
| 189 | buttons |= 1 << (i + 14); |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | for (i = j = 0; i < 6; i++) |
| 194 | if (analog->mask & (0x10 << i)) |
| 195 | input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1); |
| 196 | |
| 197 | if (analog->mask & ANALOG_HBTN_CHF) |
| 198 | for (i = 0; i < 4; i++) |
| 199 | input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1); |
| 200 | |
| 201 | if (analog->mask & ANALOG_BTN_TL) |
| 202 | input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1)); |
| 203 | if (analog->mask & ANALOG_BTN_TR) |
| 204 | input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1)); |
| 205 | if (analog->mask & ANALOG_BTN_TL2) |
| 206 | input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1))); |
| 207 | if (analog->mask & ANALOG_BTN_TR2) |
| 208 | input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1))); |
| 209 | |
| 210 | for (i = j = 0; i < 4; i++) |
| 211 | if (analog->mask & (1 << i)) |
| 212 | input_report_abs(dev, analog_axes[j++], axes[i]); |
| 213 | |
| 214 | for (i = j = 0; i < 3; i++) |
| 215 | if (analog->mask & analog_exts[i]) { |
| 216 | input_report_abs(dev, analog_hats[j++], |
| 217 | ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1)); |
| 218 | input_report_abs(dev, analog_hats[j++], |
| 219 | ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1)); |
| 220 | } |
| 221 | |
| 222 | input_sync(dev); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * analog_cooked_read() reads analog joystick data. |
| 227 | */ |
| 228 | |
| 229 | static int analog_cooked_read(struct analog_port *port) |
| 230 | { |
| 231 | struct gameport *gameport = port->gameport; |
| 232 | unsigned int time[4], start, loop, now, loopout, timeout; |
| 233 | unsigned char data[4], this, last; |
| 234 | unsigned long flags; |
| 235 | int i, j; |
| 236 | |
| 237 | loopout = (ANALOG_LOOP_TIME * port->loop) / 1000; |
| 238 | timeout = ANALOG_MAX_TIME * port->speed; |
| 239 | |
| 240 | local_irq_save(flags); |
| 241 | gameport_trigger(gameport); |
| 242 | GET_TIME(now); |
| 243 | local_irq_restore(flags); |
| 244 | |
| 245 | start = now; |
| 246 | this = port->mask; |
| 247 | i = 0; |
| 248 | |
| 249 | do { |
| 250 | loop = now; |
| 251 | last = this; |
| 252 | |
| 253 | local_irq_disable(); |
| 254 | this = gameport_read(gameport) & port->mask; |
| 255 | GET_TIME(now); |
| 256 | local_irq_restore(flags); |
| 257 | |
| 258 | if ((last ^ this) && (DELTA(loop, now) < loopout)) { |
| 259 | data[i] = last ^ this; |
| 260 | time[i] = now; |
| 261 | i++; |
| 262 | } |
| 263 | |
| 264 | } while (this && (i < 4) && (DELTA(start, now) < timeout)); |
| 265 | |
| 266 | this <<= 4; |
| 267 | |
| 268 | for (--i; i >= 0; i--) { |
| 269 | this |= data[i]; |
| 270 | for (j = 0; j < 4; j++) |
| 271 | if (data[i] & (1 << j)) |
| 272 | port->axes[j] = (DELTA(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop; |
| 273 | } |
| 274 | |
| 275 | return -(this != port->mask); |
| 276 | } |
| 277 | |
| 278 | static int analog_button_read(struct analog_port *port, char saitek, char chf) |
| 279 | { |
| 280 | unsigned char u; |
| 281 | int t = 1, i = 0; |
| 282 | int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME); |
| 283 | |
| 284 | u = gameport_read(port->gameport); |
| 285 | |
| 286 | if (!chf) { |
| 287 | port->buttons = (~u >> 4) & 0xf; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | port->buttons = 0; |
| 292 | |
| 293 | while ((~u & 0xf0) && (i < 16) && t) { |
| 294 | port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf]; |
| 295 | if (!saitek) return 0; |
| 296 | udelay(ANALOG_SAITEK_DELAY); |
| 297 | t = strobe; |
| 298 | gameport_trigger(port->gameport); |
| 299 | while (((u = gameport_read(port->gameport)) & port->mask) && t) t--; |
| 300 | i++; |
| 301 | } |
| 302 | |
| 303 | return -(!t || (i == 16)); |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * analog_poll() repeatedly polls the Analog joysticks. |
| 308 | */ |
| 309 | |
| 310 | static void analog_poll(struct gameport *gameport) |
| 311 | { |
| 312 | struct analog_port *port = gameport_get_drvdata(gameport); |
| 313 | int i; |
| 314 | |
| 315 | char saitek = !!(port->analog[0].mask & ANALOG_SAITEK); |
| 316 | char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF); |
| 317 | |
| 318 | if (port->cooked) { |
| 319 | port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons); |
| 320 | if (chf) |
| 321 | port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0; |
| 322 | port->reads++; |
| 323 | } else { |
| 324 | if (!port->axtime--) { |
| 325 | port->bads -= analog_cooked_read(port); |
| 326 | port->bads -= analog_button_read(port, saitek, chf); |
| 327 | port->reads++; |
| 328 | port->axtime = ANALOG_AXIS_TIME - 1; |
| 329 | } else { |
| 330 | if (!saitek) |
| 331 | analog_button_read(port, saitek, chf); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | for (i = 0; i < 2; i++) |
| 336 | if (port->analog[i].mask) |
| 337 | analog_decode(port->analog + i, port->axes, port->initial, port->buttons); |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * analog_open() is a callback from the input open routine. |
| 342 | */ |
| 343 | |
| 344 | static int analog_open(struct input_dev *dev) |
| 345 | { |
Dmitry Torokhov | 8715c1c | 2007-04-12 01:34:14 -0400 | [diff] [blame] | 346 | struct analog_port *port = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | gameport_start_polling(port->gameport); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * analog_close() is a callback from the input close routine. |
| 354 | */ |
| 355 | |
| 356 | static void analog_close(struct input_dev *dev) |
| 357 | { |
Dmitry Torokhov | 8715c1c | 2007-04-12 01:34:14 -0400 | [diff] [blame] | 358 | struct analog_port *port = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | gameport_stop_polling(port->gameport); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * analog_calibrate_timer() calibrates the timer and computes loop |
| 365 | * and timeout values for a joystick port. |
| 366 | */ |
| 367 | |
| 368 | static void analog_calibrate_timer(struct analog_port *port) |
| 369 | { |
| 370 | struct gameport *gameport = port->gameport; |
| 371 | unsigned int i, t, tx, t1, t2, t3; |
| 372 | unsigned long flags; |
| 373 | |
| 374 | local_irq_save(flags); |
| 375 | GET_TIME(t1); |
| 376 | #ifdef FAKE_TIME |
| 377 | analog_faketime += 830; |
| 378 | #endif |
| 379 | mdelay(1); |
| 380 | GET_TIME(t2); |
| 381 | GET_TIME(t3); |
| 382 | local_irq_restore(flags); |
| 383 | |
| 384 | port->speed = DELTA(t1, t2) - DELTA(t2, t3); |
| 385 | |
| 386 | tx = ~0; |
| 387 | |
| 388 | for (i = 0; i < 50; i++) { |
| 389 | local_irq_save(flags); |
| 390 | GET_TIME(t1); |
| 391 | for (t = 0; t < 50; t++) { gameport_read(gameport); GET_TIME(t2); } |
| 392 | GET_TIME(t3); |
| 393 | local_irq_restore(flags); |
| 394 | udelay(i); |
| 395 | t = DELTA(t1, t2) - DELTA(t2, t3); |
| 396 | if (t < tx) tx = t; |
| 397 | } |
| 398 | |
| 399 | port->loop = tx / 50; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * analog_name() constructs a name for an analog joystick. |
| 404 | */ |
| 405 | |
| 406 | static void analog_name(struct analog *analog) |
| 407 | { |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 408 | snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button", |
| 409 | hweight8(analog->mask & ANALOG_AXES_STD), |
| 410 | hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 + |
| 411 | hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | if (analog->mask & ANALOG_HATS_ALL) |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 414 | snprintf(analog->name, sizeof(analog->name), "%s %d-hat", |
| 415 | analog->name, hweight16(analog->mask & ANALOG_HATS_ALL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | if (analog->mask & ANALOG_HAT_FCS) |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 418 | strlcat(analog->name, " FCS", sizeof(analog->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | if (analog->mask & ANALOG_ANY_CHF) |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 420 | strlcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF", |
| 421 | sizeof(analog->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 423 | strlcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick", |
| 424 | sizeof(analog->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* |
| 428 | * analog_init_device() |
| 429 | */ |
| 430 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 431 | static int analog_init_device(struct analog_port *port, struct analog *analog, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 433 | struct input_dev *input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | int i, j, t, v, w, x, y, z; |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 435 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
| 437 | analog_name(analog); |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 438 | snprintf(analog->phys, sizeof(analog->phys), |
| 439 | "%s/input%d", port->gameport->phys, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn; |
| 441 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 442 | analog->dev = input_dev = input_allocate_device(); |
| 443 | if (!input_dev) |
| 444 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 446 | input_dev->name = analog->name; |
| 447 | input_dev->phys = analog->phys; |
| 448 | input_dev->id.bustype = BUS_GAMEPORT; |
| 449 | input_dev->id.vendor = GAMEPORT_ID_VENDOR_ANALOG; |
| 450 | input_dev->id.product = analog->mask >> 4; |
| 451 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 935e658 | 2007-04-12 01:35:26 -0400 | [diff] [blame] | 452 | input_dev->dev.parent = &port->gameport->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Dmitry Torokhov | 8715c1c | 2007-04-12 01:34:14 -0400 | [diff] [blame] | 454 | input_set_drvdata(input_dev, port); |
| 455 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 456 | input_dev->open = analog_open; |
| 457 | input_dev->close = analog_close; |
Dmitry Torokhov | 8715c1c | 2007-04-12 01:34:14 -0400 | [diff] [blame] | 458 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 459 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | for (i = j = 0; i < 4; i++) |
| 462 | if (analog->mask & (1 << i)) { |
| 463 | |
| 464 | t = analog_axes[j]; |
| 465 | x = port->axes[i]; |
| 466 | y = (port->axes[0] + port->axes[1]) >> 1; |
| 467 | z = y - port->axes[i]; |
| 468 | z = z > 0 ? z : -z; |
| 469 | v = (x >> 3); |
| 470 | w = (x >> 3); |
| 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3))) |
| 473 | x = y; |
| 474 | |
| 475 | if (analog->mask & ANALOG_SAITEK) { |
| 476 | if (i == 2) x = port->axes[i]; |
| 477 | v = x - (x >> 2); |
| 478 | w = (x >> 4); |
| 479 | } |
| 480 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 481 | input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | j++; |
| 483 | } |
| 484 | |
| 485 | for (i = j = 0; i < 3; i++) |
| 486 | if (analog->mask & analog_exts[i]) |
| 487 | for (x = 0; x < 2; x++) { |
| 488 | t = analog_hats[j++]; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 489 | input_set_abs_params(input_dev, t, -1, 1, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | for (i = j = 0; i < 4; i++) |
| 493 | if (analog->mask & (0x10 << i)) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 494 | set_bit(analog->buttons[j++], input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | if (analog->mask & ANALOG_BTNS_CHF) |
| 497 | for (i = 0; i < 2; i++) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 498 | set_bit(analog->buttons[j++], input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
| 500 | if (analog->mask & ANALOG_HBTN_CHF) |
| 501 | for (i = 0; i < 4; i++) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 502 | set_bit(analog->buttons[j++], input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | for (i = 0; i < 4; i++) |
| 505 | if (analog->mask & (ANALOG_BTN_TL << i)) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 506 | set_bit(analog_pads[i], input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | analog_decode(analog, port->axes, port->initial, port->buttons); |
| 509 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 510 | error = input_register_device(analog->dev); |
| 511 | if (error) { |
| 512 | input_free_device(analog->dev); |
| 513 | return error; |
| 514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 516 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* |
| 520 | * analog_init_devices() sets up device-specific values and registers the input devices. |
| 521 | */ |
| 522 | |
| 523 | static int analog_init_masks(struct analog_port *port) |
| 524 | { |
| 525 | int i; |
| 526 | struct analog *analog = port->analog; |
| 527 | int max[4]; |
| 528 | |
| 529 | if (!port->mask) |
| 530 | return -1; |
| 531 | |
| 532 | if ((port->mask & 3) != 3 && port->mask != 0xc) { |
| 533 | printk(KERN_WARNING "analog.c: Unknown joystick device found " |
| 534 | "(data=%#x, %s), probably not analog joystick.\n", |
| 535 | port->mask, port->gameport->phys); |
| 536 | return -1; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */ |
| 541 | |
| 542 | analog[0].mask = i & 0xfffff; |
| 543 | |
| 544 | analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD) |
| 545 | | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS) |
| 546 | | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2); |
| 547 | |
| 548 | analog[0].mask &= ~(ANALOG_HAT2_CHF) |
| 549 | | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF); |
| 550 | |
| 551 | analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2) |
| 552 | | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8) |
| 553 | | ((~analog[0].mask & ANALOG_HAT_FCS) << 2) |
| 554 | | ((~analog[0].mask & ANALOG_HAT_FCS) << 4); |
| 555 | |
| 556 | analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER) |
| 557 | | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10) |
| 558 | & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12)); |
| 559 | |
| 560 | analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000); |
| 561 | |
| 562 | analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD |
| 563 | : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD); |
| 564 | |
| 565 | if (port->cooked) { |
| 566 | |
| 567 | for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1; |
| 568 | |
| 569 | if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1; |
| 570 | if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1; |
| 571 | if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1; |
| 572 | if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1; |
| 573 | if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1; |
| 574 | |
| 575 | gameport_calibrate(port->gameport, port->axes, max); |
| 576 | } |
| 577 | |
| 578 | for (i = 0; i < 4; i++) |
| 579 | port->initial[i] = port->axes[i]; |
| 580 | |
| 581 | return -!(analog[0].mask || analog[1].mask); |
| 582 | } |
| 583 | |
| 584 | static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port) |
| 585 | { |
| 586 | int i, t, u, v; |
| 587 | |
| 588 | port->gameport = gameport; |
| 589 | |
| 590 | gameport_set_drvdata(gameport, port); |
| 591 | |
| 592 | if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) { |
| 593 | |
| 594 | analog_calibrate_timer(port); |
| 595 | |
| 596 | gameport_trigger(gameport); |
| 597 | t = gameport_read(gameport); |
| 598 | msleep(ANALOG_MAX_TIME); |
| 599 | port->mask = (gameport_read(gameport) ^ t) & t & 0xf; |
| 600 | port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS; |
| 601 | |
| 602 | for (i = 0; i < ANALOG_INIT_RETRIES; i++) { |
| 603 | if (!analog_cooked_read(port)) |
| 604 | break; |
| 605 | msleep(ANALOG_MAX_TIME); |
| 606 | } |
| 607 | |
| 608 | u = v = 0; |
| 609 | |
| 610 | msleep(ANALOG_MAX_TIME); |
| 611 | t = gameport_time(gameport, ANALOG_MAX_TIME * 1000); |
| 612 | gameport_trigger(gameport); |
| 613 | while ((gameport_read(port->gameport) & port->mask) && (u < t)) |
| 614 | u++; |
| 615 | udelay(ANALOG_SAITEK_DELAY); |
| 616 | t = gameport_time(gameport, ANALOG_SAITEK_TIME); |
| 617 | gameport_trigger(gameport); |
| 618 | while ((gameport_read(port->gameport) & port->mask) && (v < t)) |
| 619 | v++; |
| 620 | |
| 621 | if (v < (u >> 1)) { /* FIXME - more than one port */ |
| 622 | analog_options[0] |= /* FIXME - more than one port */ |
| 623 | ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF; |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | gameport_close(gameport); |
| 628 | } |
| 629 | |
| 630 | if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) { |
| 631 | |
| 632 | for (i = 0; i < ANALOG_INIT_RETRIES; i++) |
| 633 | if (!gameport_cooked_read(gameport, port->axes, &port->buttons)) |
| 634 | break; |
| 635 | for (i = 0; i < 4; i++) |
| 636 | if (port->axes[i] != -1) |
| 637 | port->mask |= 1 << i; |
| 638 | |
| 639 | port->fuzz = gameport->fuzz; |
| 640 | port->cooked = 1; |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | return gameport_open(gameport, drv, GAMEPORT_MODE_RAW); |
| 645 | } |
| 646 | |
| 647 | static int analog_connect(struct gameport *gameport, struct gameport_driver *drv) |
| 648 | { |
| 649 | struct analog_port *port; |
| 650 | int i; |
| 651 | int err; |
| 652 | |
Pekka Enberg | a97e148 | 2005-09-06 15:18:33 -0700 | [diff] [blame] | 653 | if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return - ENOMEM; |
| 655 | |
| 656 | err = analog_init_port(gameport, drv, port); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 657 | if (err) |
| 658 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | err = analog_init_masks(port); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 661 | if (err) |
| 662 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | gameport_set_poll_handler(gameport, analog_poll); |
| 665 | gameport_set_poll_interval(gameport, 10); |
| 666 | |
| 667 | for (i = 0; i < 2; i++) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 668 | if (port->analog[i].mask) { |
| 669 | err = analog_init_device(port, port->analog + i, i); |
| 670 | if (err) |
| 671 | goto fail3; |
| 672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | return 0; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 675 | |
| 676 | fail3: while (--i >= 0) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 677 | if (port->analog[i].mask) |
| 678 | input_unregister_device(port->analog[i].dev); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 679 | fail2: gameport_close(gameport); |
| 680 | fail1: gameport_set_drvdata(gameport, NULL); |
| 681 | kfree(port); |
| 682 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static void analog_disconnect(struct gameport *gameport) |
| 686 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | struct analog_port *port = gameport_get_drvdata(gameport); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 688 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
| 690 | for (i = 0; i < 2; i++) |
| 691 | if (port->analog[i].mask) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 692 | input_unregister_device(port->analog[i].dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | gameport_close(gameport); |
| 694 | gameport_set_drvdata(gameport, NULL); |
| 695 | printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n", |
| 696 | port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0, |
| 697 | port->gameport->phys); |
| 698 | kfree(port); |
| 699 | } |
| 700 | |
| 701 | struct analog_types { |
| 702 | char *name; |
| 703 | int value; |
| 704 | }; |
| 705 | |
| 706 | static struct analog_types analog_types[] = { |
| 707 | { "none", 0x00000000 }, |
| 708 | { "auto", 0x000000ff }, |
| 709 | { "2btn", 0x0000003f }, |
| 710 | { "y-joy", 0x0cc00033 }, |
| 711 | { "y-pad", 0x8cc80033 }, |
| 712 | { "fcs", 0x000008f7 }, |
| 713 | { "chf", 0x000002ff }, |
| 714 | { "fullchf", 0x000007ff }, |
| 715 | { "gamepad", 0x000830f3 }, |
| 716 | { "gamepad8", 0x0008f0f3 }, |
| 717 | { NULL, 0 } |
| 718 | }; |
| 719 | |
| 720 | static void analog_parse_options(void) |
| 721 | { |
| 722 | int i, j; |
| 723 | char *end; |
| 724 | |
| 725 | for (i = 0; i < js_nargs; i++) { |
| 726 | |
| 727 | for (j = 0; analog_types[j].name; j++) |
| 728 | if (!strcmp(analog_types[j].name, js[i])) { |
| 729 | analog_options[i] = analog_types[j].value; |
| 730 | break; |
| 731 | } |
| 732 | if (analog_types[j].name) continue; |
| 733 | |
| 734 | analog_options[i] = simple_strtoul(js[i], &end, 0); |
| 735 | if (end != js[i]) continue; |
| 736 | |
| 737 | analog_options[i] = 0xff; |
| 738 | if (!strlen(js[i])) continue; |
| 739 | |
| 740 | printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]); |
| 741 | } |
| 742 | |
| 743 | for (; i < ANALOG_PORTS; i++) |
| 744 | analog_options[i] = 0xff; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * The gameport device structure. |
| 749 | */ |
| 750 | |
| 751 | static struct gameport_driver analog_drv = { |
| 752 | .driver = { |
| 753 | .name = "analog", |
| 754 | }, |
| 755 | .description = DRIVER_DESC, |
| 756 | .connect = analog_connect, |
| 757 | .disconnect = analog_disconnect, |
| 758 | }; |
| 759 | |
| 760 | static int __init analog_init(void) |
| 761 | { |
| 762 | analog_parse_options(); |
| 763 | gameport_register_driver(&analog_drv); |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | static void __exit analog_exit(void) |
| 769 | { |
| 770 | gameport_unregister_driver(&analog_drv); |
| 771 | } |
| 772 | |
| 773 | module_init(analog_init); |
| 774 | module_exit(analog_exit); |