Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * $Id: gf2k.c,v 1.19 2002/01/22 20:27:43 vojtech Exp $ |
| 3 | * |
| 4 | * Copyright (c) 1998-2001 Vojtech Pavlik |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Genius Flight 2000 joystick 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 | |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/input.h> |
| 37 | #include <linux/gameport.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 38 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #define DRIVER_DESC "Genius Flight 2000 joystick driver" |
| 41 | |
| 42 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 43 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 44 | MODULE_LICENSE("GPL"); |
| 45 | |
| 46 | #define GF2K_START 400 /* The time we wait for the first bit [400 us] */ |
| 47 | #define GF2K_STROBE 40 /* The time we wait for the first bit [40 us] */ |
| 48 | #define GF2K_TIMEOUT 4 /* Wait for everything to settle [4 ms] */ |
| 49 | #define GF2K_LENGTH 80 /* Max number of triplets in a packet */ |
| 50 | |
| 51 | /* |
| 52 | * Genius joystick ids ... |
| 53 | */ |
| 54 | |
| 55 | #define GF2K_ID_G09 1 |
| 56 | #define GF2K_ID_F30D 2 |
| 57 | #define GF2K_ID_F30 3 |
| 58 | #define GF2K_ID_F31D 4 |
| 59 | #define GF2K_ID_F305 5 |
| 60 | #define GF2K_ID_F23P 6 |
| 61 | #define GF2K_ID_F31 7 |
| 62 | #define GF2K_ID_MAX 7 |
| 63 | |
| 64 | static char gf2k_length[] = { 40, 40, 40, 40, 40, 40, 40, 40 }; |
| 65 | static char gf2k_hat_to_axis[][2] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; |
| 66 | |
| 67 | static char *gf2k_names[] = {"", "Genius G-09D", "Genius F-30D", "Genius F-30", "Genius MaxFighter F-31D", |
| 68 | "Genius F-30-5", "Genius Flight2000 F-23", "Genius F-31"}; |
| 69 | static unsigned char gf2k_hats[] = { 0, 2, 0, 0, 2, 0, 2, 0 }; |
| 70 | static unsigned char gf2k_axes[] = { 0, 2, 0, 0, 4, 0, 4, 0 }; |
| 71 | static unsigned char gf2k_joys[] = { 0, 0, 0, 0,10, 0, 8, 0 }; |
| 72 | static unsigned char gf2k_pads[] = { 0, 6, 0, 0, 0, 0, 0, 0 }; |
| 73 | static unsigned char gf2k_lens[] = { 0,18, 0, 0,18, 0,18, 0 }; |
| 74 | |
| 75 | static unsigned char gf2k_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, ABS_GAS, ABS_BRAKE }; |
| 76 | static short gf2k_btn_joy[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 }; |
| 77 | static short gf2k_btn_pad[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_START, BTN_SELECT }; |
| 78 | |
| 79 | |
| 80 | static short gf2k_seq_reset[] = { 240, 340, 0 }; |
| 81 | static short gf2k_seq_digital[] = { 590, 320, 860, 0 }; |
| 82 | |
| 83 | struct gf2k { |
| 84 | struct gameport *gameport; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 85 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | int reads; |
| 87 | int bads; |
| 88 | unsigned char id; |
| 89 | unsigned char length; |
| 90 | char phys[32]; |
| 91 | }; |
| 92 | |
| 93 | /* |
| 94 | * gf2k_read_packet() reads a Genius Flight2000 packet. |
| 95 | */ |
| 96 | |
| 97 | static int gf2k_read_packet(struct gameport *gameport, int length, char *data) |
| 98 | { |
| 99 | unsigned char u, v; |
| 100 | int i; |
| 101 | unsigned int t, p; |
| 102 | unsigned long flags; |
| 103 | |
| 104 | t = gameport_time(gameport, GF2K_START); |
| 105 | p = gameport_time(gameport, GF2K_STROBE); |
| 106 | |
| 107 | i = 0; |
| 108 | |
| 109 | local_irq_save(flags); |
| 110 | |
| 111 | gameport_trigger(gameport); |
| 112 | v = gameport_read(gameport); |
| 113 | |
| 114 | while (t > 0 && i < length) { |
| 115 | t--; u = v; |
| 116 | v = gameport_read(gameport); |
| 117 | if (v & ~u & 0x10) { |
| 118 | data[i++] = v >> 5; |
| 119 | t = p; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | local_irq_restore(flags); |
| 124 | |
| 125 | return i; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * gf2k_trigger_seq() initializes a Genius Flight2000 joystick |
| 130 | * into digital mode. |
| 131 | */ |
| 132 | |
| 133 | static void gf2k_trigger_seq(struct gameport *gameport, short *seq) |
| 134 | { |
| 135 | |
| 136 | unsigned long flags; |
| 137 | int i, t; |
| 138 | |
| 139 | local_irq_save(flags); |
| 140 | |
| 141 | i = 0; |
| 142 | do { |
| 143 | gameport_trigger(gameport); |
| 144 | t = gameport_time(gameport, GF2K_TIMEOUT * 1000); |
| 145 | while ((gameport_read(gameport) & 1) && t) t--; |
| 146 | udelay(seq[i]); |
| 147 | } while (seq[++i]); |
| 148 | |
| 149 | gameport_trigger(gameport); |
| 150 | |
| 151 | local_irq_restore(flags); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * js_sw_get_bits() composes bits from the triplet buffer into a __u64. |
| 156 | * Parameter 'pos' is bit number inside packet where to start at, 'num' is number |
| 157 | * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits |
| 158 | * is number of bits per triplet. |
| 159 | */ |
| 160 | |
| 161 | #define GB(p,n,s) gf2k_get_bits(data, p, n, s) |
| 162 | |
| 163 | static int gf2k_get_bits(unsigned char *buf, int pos, int num, int shift) |
| 164 | { |
| 165 | __u64 data = 0; |
| 166 | int i; |
| 167 | |
| 168 | for (i = 0; i < num / 3 + 2; i++) |
| 169 | data |= buf[pos / 3 + i] << (i * 3); |
| 170 | data >>= pos % 3; |
| 171 | data &= (1 << num) - 1; |
| 172 | data <<= shift; |
| 173 | |
| 174 | return data; |
| 175 | } |
| 176 | |
| 177 | static void gf2k_read(struct gf2k *gf2k, unsigned char *data) |
| 178 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 179 | struct input_dev *dev = gf2k->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | int i, t; |
| 181 | |
| 182 | for (i = 0; i < 4 && i < gf2k_axes[gf2k->id]; i++) |
| 183 | input_report_abs(dev, gf2k_abs[i], GB(i<<3,8,0) | GB(i+46,1,8) | GB(i+50,1,9)); |
| 184 | |
| 185 | for (i = 0; i < 2 && i < gf2k_axes[gf2k->id] - 4; i++) |
| 186 | input_report_abs(dev, gf2k_abs[i], GB(i*9+60,8,0) | GB(i+54,1,9)); |
| 187 | |
| 188 | t = GB(40,4,0); |
| 189 | |
| 190 | for (i = 0; i < gf2k_hats[gf2k->id]; i++) |
| 191 | input_report_abs(dev, ABS_HAT0X + i, gf2k_hat_to_axis[t][i]); |
| 192 | |
| 193 | t = GB(44,2,0) | GB(32,8,2) | GB(78,2,10); |
| 194 | |
| 195 | for (i = 0; i < gf2k_joys[gf2k->id]; i++) |
| 196 | input_report_key(dev, gf2k_btn_joy[i], (t >> i) & 1); |
| 197 | |
| 198 | for (i = 0; i < gf2k_pads[gf2k->id]; i++) |
| 199 | input_report_key(dev, gf2k_btn_pad[i], (t >> i) & 1); |
| 200 | |
| 201 | input_sync(dev); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * gf2k_poll() reads and analyzes Genius joystick data. |
| 206 | */ |
| 207 | |
| 208 | static void gf2k_poll(struct gameport *gameport) |
| 209 | { |
| 210 | struct gf2k *gf2k = gameport_get_drvdata(gameport); |
| 211 | unsigned char data[GF2K_LENGTH]; |
| 212 | |
| 213 | gf2k->reads++; |
| 214 | |
| 215 | if (gf2k_read_packet(gf2k->gameport, gf2k_length[gf2k->id], data) < gf2k_length[gf2k->id]) |
| 216 | gf2k->bads++; |
| 217 | else |
| 218 | gf2k_read(gf2k, data); |
| 219 | } |
| 220 | |
| 221 | static int gf2k_open(struct input_dev *dev) |
| 222 | { |
| 223 | struct gf2k *gf2k = dev->private; |
| 224 | |
| 225 | gameport_start_polling(gf2k->gameport); |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static void gf2k_close(struct input_dev *dev) |
| 230 | { |
| 231 | struct gf2k *gf2k = dev->private; |
| 232 | |
| 233 | gameport_stop_polling(gf2k->gameport); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * gf2k_connect() probes for Genius id joysticks. |
| 238 | */ |
| 239 | |
| 240 | static int gf2k_connect(struct gameport *gameport, struct gameport_driver *drv) |
| 241 | { |
| 242 | struct gf2k *gf2k; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 243 | struct input_dev *input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | unsigned char data[GF2K_LENGTH]; |
| 245 | int i, err; |
| 246 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 247 | gf2k = kzalloc(sizeof(struct gf2k), GFP_KERNEL); |
| 248 | input_dev = input_allocate_device(); |
| 249 | if (!gf2k || !input_dev) { |
| 250 | err = -ENOMEM; |
| 251 | goto fail1; |
| 252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | gf2k->gameport = gameport; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 255 | gf2k->dev = input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | gameport_set_drvdata(gameport, gf2k); |
| 258 | |
| 259 | err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW); |
| 260 | if (err) |
| 261 | goto fail1; |
| 262 | |
| 263 | gf2k_trigger_seq(gameport, gf2k_seq_reset); |
| 264 | |
| 265 | msleep(GF2K_TIMEOUT); |
| 266 | |
| 267 | gf2k_trigger_seq(gameport, gf2k_seq_digital); |
| 268 | |
| 269 | msleep(GF2K_TIMEOUT); |
| 270 | |
| 271 | if (gf2k_read_packet(gameport, GF2K_LENGTH, data) < 12) { |
| 272 | err = -ENODEV; |
| 273 | goto fail2; |
| 274 | } |
| 275 | |
| 276 | if (!(gf2k->id = GB(7,2,0) | GB(3,3,2) | GB(0,3,5))) { |
| 277 | err = -ENODEV; |
| 278 | goto fail2; |
| 279 | } |
| 280 | |
| 281 | #ifdef RESET_WORKS |
| 282 | if ((gf2k->id != (GB(19,2,0) | GB(15,3,2) | GB(12,3,5))) || |
| 283 | (gf2k->id != (GB(31,2,0) | GB(27,3,2) | GB(24,3,5)))) { |
| 284 | err = -ENODEV; |
| 285 | goto fail2; |
| 286 | } |
| 287 | #else |
| 288 | gf2k->id = 6; |
| 289 | #endif |
| 290 | |
| 291 | if (gf2k->id > GF2K_ID_MAX || !gf2k_axes[gf2k->id]) { |
| 292 | printk(KERN_WARNING "gf2k.c: Not yet supported joystick on %s. [id: %d type:%s]\n", |
| 293 | gameport->phys, gf2k->id, gf2k->id > GF2K_ID_MAX ? "Unknown" : gf2k_names[gf2k->id]); |
| 294 | err = -ENODEV; |
| 295 | goto fail2; |
| 296 | } |
| 297 | |
| 298 | gameport_set_poll_handler(gameport, gf2k_poll); |
| 299 | gameport_set_poll_interval(gameport, 20); |
| 300 | |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 301 | snprintf(gf2k->phys, sizeof(gf2k->phys), "%s/input0", gameport->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | gf2k->length = gf2k_lens[gf2k->id]; |
| 304 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 305 | input_dev->name = gf2k_names[gf2k->id]; |
| 306 | input_dev->phys = gf2k->phys; |
| 307 | input_dev->id.bustype = BUS_GAMEPORT; |
| 308 | input_dev->id.vendor = GAMEPORT_ID_VENDOR_GENIUS; |
| 309 | input_dev->id.product = gf2k->id; |
| 310 | input_dev->id.version = 0x0100; |
| 311 | input_dev->cdev.dev = &gameport->dev; |
| 312 | input_dev->private = gf2k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 314 | input_dev->open = gf2k_open; |
| 315 | input_dev->close = gf2k_close; |
| 316 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | for (i = 0; i < gf2k_axes[gf2k->id]; i++) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 319 | set_bit(gf2k_abs[i], input_dev->absbit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | for (i = 0; i < gf2k_hats[gf2k->id]; i++) { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 322 | set_bit(ABS_HAT0X + i, input_dev->absbit); |
| 323 | input_dev->absmin[ABS_HAT0X + i] = -1; |
| 324 | input_dev->absmax[ABS_HAT0X + i] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | for (i = 0; i < gf2k_joys[gf2k->id]; i++) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 328 | set_bit(gf2k_btn_joy[i], input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 330 | for (i = 0; i < gf2k_pads[gf2k->id]; i++) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 331 | set_bit(gf2k_btn_pad[i], input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | gf2k_read_packet(gameport, gf2k->length, data); |
| 334 | gf2k_read(gf2k, data); |
| 335 | |
| 336 | for (i = 0; i < gf2k_axes[gf2k->id]; i++) { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 337 | input_dev->absmax[gf2k_abs[i]] = (i < 2) ? input_dev->abs[gf2k_abs[i]] * 2 - 32 : |
| 338 | input_dev->abs[gf2k_abs[0]] + input_dev->abs[gf2k_abs[1]] - 32; |
| 339 | input_dev->absmin[gf2k_abs[i]] = 32; |
| 340 | input_dev->absfuzz[gf2k_abs[i]] = 8; |
| 341 | input_dev->absflat[gf2k_abs[i]] = (i < 2) ? 24 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 344 | err = input_register_device(gf2k->dev); |
| 345 | if (err) |
| 346 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | return 0; |
| 349 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 350 | fail2: gameport_close(gameport); |
| 351 | fail1: gameport_set_drvdata(gameport, NULL); |
| 352 | input_free_device(input_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | kfree(gf2k); |
| 354 | return err; |
| 355 | } |
| 356 | |
| 357 | static void gf2k_disconnect(struct gameport *gameport) |
| 358 | { |
| 359 | struct gf2k *gf2k = gameport_get_drvdata(gameport); |
| 360 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 361 | input_unregister_device(gf2k->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | gameport_close(gameport); |
| 363 | gameport_set_drvdata(gameport, NULL); |
| 364 | kfree(gf2k); |
| 365 | } |
| 366 | |
| 367 | static struct gameport_driver gf2k_drv = { |
| 368 | .driver = { |
| 369 | .name = "gf2k", |
| 370 | }, |
| 371 | .description = DRIVER_DESC, |
| 372 | .connect = gf2k_connect, |
| 373 | .disconnect = gf2k_disconnect, |
| 374 | }; |
| 375 | |
| 376 | static int __init gf2k_init(void) |
| 377 | { |
| 378 | gameport_register_driver(&gf2k_drv); |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static void __exit gf2k_exit(void) |
| 383 | { |
| 384 | gameport_unregister_driver(&gf2k_drv); |
| 385 | } |
| 386 | |
| 387 | module_init(gf2k_init); |
| 388 | module_exit(gf2k_exit); |