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