2 * Copyright (c) 1998-2001 Vojtech Pavlik
6 * Genius Flight 2000 joystick driver for Linux
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.
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.
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
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 #include <linux/delay.h>
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/input.h>
34 #include <linux/gameport.h>
35 #include <linux/jiffies.h>
37 #define DRIVER_DESC "Genius Flight 2000 joystick driver"
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION(DRIVER_DESC);
41 MODULE_LICENSE("GPL");
43 #define GF2K_START 400 /* The time we wait for the first bit [400 us] */
44 #define GF2K_STROBE 40 /* The time we wait for the first bit [40 us] */
45 #define GF2K_TIMEOUT 4 /* Wait for everything to settle [4 ms] */
46 #define GF2K_LENGTH 80 /* Max number of triplets in a packet */
49 * Genius joystick ids ...
53 #define GF2K_ID_F30D 2
55 #define GF2K_ID_F31D 4
56 #define GF2K_ID_F305 5
57 #define GF2K_ID_F23P 6
61 static char gf2k_length[] = { 40, 40, 40, 40, 40, 40, 40, 40 };
62 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}};
64 static char *gf2k_names[] = {"", "Genius G-09D", "Genius F-30D", "Genius F-30", "Genius MaxFighter F-31D",
65 "Genius F-30-5", "Genius Flight2000 F-23", "Genius F-31"};
66 static unsigned char gf2k_hats[] = { 0, 2, 0, 0, 2, 0, 2, 0 };
67 static unsigned char gf2k_axes[] = { 0, 2, 0, 0, 4, 0, 4, 0 };
68 static unsigned char gf2k_joys[] = { 0, 0, 0, 0,10, 0, 8, 0 };
69 static unsigned char gf2k_pads[] = { 0, 6, 0, 0, 0, 0, 0, 0 };
70 static unsigned char gf2k_lens[] = { 0,18, 0, 0,18, 0,18, 0 };
72 static unsigned char gf2k_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, ABS_GAS, ABS_BRAKE };
73 static short gf2k_btn_joy[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 };
74 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 };
77 static short gf2k_seq_reset[] = { 240, 340, 0 };
78 static short gf2k_seq_digital[] = { 590, 320, 860, 0 };
81 struct gameport *gameport;
82 struct input_dev *dev;
91 * gf2k_read_packet() reads a Genius Flight2000 packet.
94 static int gf2k_read_packet(struct gameport *gameport, int length, char *data)
101 t = gameport_time(gameport, GF2K_START);
102 p = gameport_time(gameport, GF2K_STROBE);
106 local_irq_save(flags);
108 gameport_trigger(gameport);
109 v = gameport_read(gameport);
111 while (t > 0 && i < length) {
113 v = gameport_read(gameport);
120 local_irq_restore(flags);
126 * gf2k_trigger_seq() initializes a Genius Flight2000 joystick
130 static void gf2k_trigger_seq(struct gameport *gameport, short *seq)
136 local_irq_save(flags);
140 gameport_trigger(gameport);
141 t = gameport_time(gameport, GF2K_TIMEOUT * 1000);
142 while ((gameport_read(gameport) & 1) && t) t--;
146 gameport_trigger(gameport);
148 local_irq_restore(flags);
152 * js_sw_get_bits() composes bits from the triplet buffer into a __u64.
153 * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
154 * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
155 * is number of bits per triplet.
158 #define GB(p,n,s) gf2k_get_bits(data, p, n, s)
160 static int gf2k_get_bits(unsigned char *buf, int pos, int num, int shift)
165 for (i = 0; i < num / 3 + 2; i++)
166 data |= buf[pos / 3 + i] << (i * 3);
168 data &= (1 << num) - 1;
174 static void gf2k_read(struct gf2k *gf2k, unsigned char *data)
176 struct input_dev *dev = gf2k->dev;
179 for (i = 0; i < 4 && i < gf2k_axes[gf2k->id]; i++)
180 input_report_abs(dev, gf2k_abs[i], GB(i<<3,8,0) | GB(i+46,1,8) | GB(i+50,1,9));
182 for (i = 0; i < 2 && i < gf2k_axes[gf2k->id] - 4; i++)
183 input_report_abs(dev, gf2k_abs[i], GB(i*9+60,8,0) | GB(i+54,1,9));
187 for (i = 0; i < gf2k_hats[gf2k->id]; i++)
188 input_report_abs(dev, ABS_HAT0X + i, gf2k_hat_to_axis[t][i]);
190 t = GB(44,2,0) | GB(32,8,2) | GB(78,2,10);
192 for (i = 0; i < gf2k_joys[gf2k->id]; i++)
193 input_report_key(dev, gf2k_btn_joy[i], (t >> i) & 1);
195 for (i = 0; i < gf2k_pads[gf2k->id]; i++)
196 input_report_key(dev, gf2k_btn_pad[i], (t >> i) & 1);
202 * gf2k_poll() reads and analyzes Genius joystick data.
205 static void gf2k_poll(struct gameport *gameport)
207 struct gf2k *gf2k = gameport_get_drvdata(gameport);
208 unsigned char data[GF2K_LENGTH];
212 if (gf2k_read_packet(gf2k->gameport, gf2k_length[gf2k->id], data) < gf2k_length[gf2k->id])
215 gf2k_read(gf2k, data);
218 static int gf2k_open(struct input_dev *dev)
220 struct gf2k *gf2k = input_get_drvdata(dev);
222 gameport_start_polling(gf2k->gameport);
226 static void gf2k_close(struct input_dev *dev)
228 struct gf2k *gf2k = input_get_drvdata(dev);
230 gameport_stop_polling(gf2k->gameport);
234 * gf2k_connect() probes for Genius id joysticks.
237 static int gf2k_connect(struct gameport *gameport, struct gameport_driver *drv)
240 struct input_dev *input_dev;
241 unsigned char data[GF2K_LENGTH];
244 gf2k = kzalloc(sizeof(struct gf2k), GFP_KERNEL);
245 input_dev = input_allocate_device();
246 if (!gf2k || !input_dev) {
251 gf2k->gameport = gameport;
252 gf2k->dev = input_dev;
254 gameport_set_drvdata(gameport, gf2k);
256 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
260 gf2k_trigger_seq(gameport, gf2k_seq_reset);
262 msleep(GF2K_TIMEOUT);
264 gf2k_trigger_seq(gameport, gf2k_seq_digital);
266 msleep(GF2K_TIMEOUT);
268 if (gf2k_read_packet(gameport, GF2K_LENGTH, data) < 12) {
273 if (!(gf2k->id = GB(7,2,0) | GB(3,3,2) | GB(0,3,5))) {
279 if ((gf2k->id != (GB(19,2,0) | GB(15,3,2) | GB(12,3,5))) &&
280 (gf2k->id != (GB(31,2,0) | GB(27,3,2) | GB(24,3,5)))) {
288 if (gf2k->id > GF2K_ID_MAX || !gf2k_axes[gf2k->id]) {
289 printk(KERN_WARNING "gf2k.c: Not yet supported joystick on %s. [id: %d type:%s]\n",
290 gameport->phys, gf2k->id, gf2k->id > GF2K_ID_MAX ? "Unknown" : gf2k_names[gf2k->id]);
295 gameport_set_poll_handler(gameport, gf2k_poll);
296 gameport_set_poll_interval(gameport, 20);
298 snprintf(gf2k->phys, sizeof(gf2k->phys), "%s/input0", gameport->phys);
300 gf2k->length = gf2k_lens[gf2k->id];
302 input_dev->name = gf2k_names[gf2k->id];
303 input_dev->phys = gf2k->phys;
304 input_dev->id.bustype = BUS_GAMEPORT;
305 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GENIUS;
306 input_dev->id.product = gf2k->id;
307 input_dev->id.version = 0x0100;
308 input_dev->dev.parent = &gameport->dev;
310 input_set_drvdata(input_dev, gf2k);
312 input_dev->open = gf2k_open;
313 input_dev->close = gf2k_close;
315 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
317 for (i = 0; i < gf2k_axes[gf2k->id]; i++)
318 set_bit(gf2k_abs[i], input_dev->absbit);
320 for (i = 0; i < gf2k_hats[gf2k->id]; i++)
321 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
323 for (i = 0; i < gf2k_joys[gf2k->id]; i++)
324 set_bit(gf2k_btn_joy[i], input_dev->keybit);
326 for (i = 0; i < gf2k_pads[gf2k->id]; i++)
327 set_bit(gf2k_btn_pad[i], input_dev->keybit);
329 gf2k_read_packet(gameport, gf2k->length, data);
330 gf2k_read(gf2k, data);
332 for (i = 0; i < gf2k_axes[gf2k->id]; i++) {
334 input_abs_get_val(input_dev, gf2k_abs[i]) * 2 :
335 input_abs_get_val(input_dev, gf2k_abs[0]) +
336 input_abs_get_val(input_dev, gf2k_abs[1]);
337 int flat = i < 2 ? 24 : 0;
339 input_set_abs_params(input_dev, gf2k_abs[i],
340 32, max - 32, 8, flat);
343 err = input_register_device(gf2k->dev);
349 fail2: gameport_close(gameport);
350 fail1: gameport_set_drvdata(gameport, NULL);
351 input_free_device(input_dev);
356 static void gf2k_disconnect(struct gameport *gameport)
358 struct gf2k *gf2k = gameport_get_drvdata(gameport);
360 input_unregister_device(gf2k->dev);
361 gameport_close(gameport);
362 gameport_set_drvdata(gameport, NULL);
366 static struct gameport_driver gf2k_drv = {
370 .description = DRIVER_DESC,
371 .connect = gf2k_connect,
372 .disconnect = gf2k_disconnect,
375 module_gameport_driver(gf2k_drv);