2 * $Id: gf2k.c,v 1.19 2002/01/22 20:27:43 vojtech Exp $
4 * Copyright (c) 1998-2001 Vojtech Pavlik
8 * Genius Flight 2000 joystick driver for Linux
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.
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.
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
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
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>
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION("Genius Flight 2000 joystick driver");
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 */
47 #define GF2K_REFRESH HZ/50 /* Time between joystick polls [20 ms] */
50 * Genius joystick ids ...
54 #define GF2K_ID_F30D 2
56 #define GF2K_ID_F31D 4
57 #define GF2K_ID_F305 5
58 #define GF2K_ID_F23P 6
62 static char gf2k_length
[] = { 40, 40, 40, 40, 40, 40, 40, 40 };
63 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}};
65 static char *gf2k_names
[] = {"", "Genius G-09D", "Genius F-30D", "Genius F-30", "Genius MaxFighter F-31D",
66 "Genius F-30-5", "Genius Flight2000 F-23", "Genius F-31"};
67 static unsigned char gf2k_hats
[] = { 0, 2, 0, 0, 2, 0, 2, 0 };
68 static unsigned char gf2k_axes
[] = { 0, 2, 0, 0, 4, 0, 4, 0 };
69 static unsigned char gf2k_joys
[] = { 0, 0, 0, 0,10, 0, 8, 0 };
70 static unsigned char gf2k_pads
[] = { 0, 6, 0, 0, 0, 0, 0, 0 };
71 static unsigned char gf2k_lens
[] = { 0,18, 0, 0,18, 0,18, 0 };
73 static unsigned char gf2k_abs
[] = { ABS_X
, ABS_Y
, ABS_THROTTLE
, ABS_RUDDER
, ABS_GAS
, ABS_BRAKE
};
74 static short gf2k_btn_joy
[] = { BTN_TRIGGER
, BTN_THUMB
, BTN_TOP
, BTN_TOP2
, BTN_BASE
, BTN_BASE2
, BTN_BASE3
, BTN_BASE4
};
75 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 static short gf2k_seq_reset
[] = { 240, 340, 0 };
79 static short gf2k_seq_digital
[] = { 590, 320, 860, 0 };
82 struct gameport
*gameport
;
83 struct timer_list timer
;
94 * gf2k_read_packet() reads a Genius Flight2000 packet.
97 static int gf2k_read_packet(struct gameport
*gameport
, int length
, char *data
)
104 t
= gameport_time(gameport
, GF2K_START
);
105 p
= gameport_time(gameport
, GF2K_STROBE
);
109 local_irq_save(flags
);
111 gameport_trigger(gameport
);
112 v
= gameport_read(gameport
);
114 while (t
> 0 && i
< length
) {
116 v
= gameport_read(gameport
);
123 local_irq_restore(flags
);
129 * gf2k_trigger_seq() initializes a Genius Flight2000 joystick
133 static void gf2k_trigger_seq(struct gameport
*gameport
, short *seq
)
139 local_irq_save(flags
);
143 gameport_trigger(gameport
);
144 t
= gameport_time(gameport
, GF2K_TIMEOUT
* 1000);
145 while ((gameport_read(gameport
) & 1) && t
) t
--;
149 gameport_trigger(gameport
);
151 local_irq_restore(flags
);
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.
161 #define GB(p,n,s) gf2k_get_bits(data, p, n, s)
163 static int gf2k_get_bits(unsigned char *buf
, int pos
, int num
, int shift
)
168 for (i
= 0; i
< num
/ 3 + 2; i
++)
169 data
|= buf
[pos
/ 3 + i
] << (i
* 3);
171 data
&= (1 << num
) - 1;
177 static void gf2k_read(struct gf2k
*gf2k
, unsigned char *data
)
179 struct input_dev
*dev
= &gf2k
->dev
;
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));
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));
190 for (i
= 0; i
< gf2k_hats
[gf2k
->id
]; i
++)
191 input_report_abs(dev
, ABS_HAT0X
+ i
, gf2k_hat_to_axis
[t
][i
]);
193 t
= GB(44,2,0) | GB(32,8,2) | GB(78,2,10);
195 for (i
= 0; i
< gf2k_joys
[gf2k
->id
]; i
++)
196 input_report_key(dev
, gf2k_btn_joy
[i
], (t
>> i
) & 1);
198 for (i
= 0; i
< gf2k_pads
[gf2k
->id
]; i
++)
199 input_report_key(dev
, gf2k_btn_pad
[i
], (t
>> i
) & 1);
205 * gf2k_timer() reads and analyzes Genius joystick data.
208 static void gf2k_timer(unsigned long private)
210 struct gf2k
*gf2k
= (void *) private;
211 unsigned char data
[GF2K_LENGTH
];
215 if (gf2k_read_packet(gf2k
->gameport
, gf2k_length
[gf2k
->id
], data
) < gf2k_length
[gf2k
->id
]) {
217 } else gf2k_read(gf2k
, data
);
219 mod_timer(&gf2k
->timer
, jiffies
+ GF2K_REFRESH
);
222 static int gf2k_open(struct input_dev
*dev
)
224 struct gf2k
*gf2k
= dev
->private;
226 mod_timer(&gf2k
->timer
, jiffies
+ GF2K_REFRESH
);
230 static void gf2k_close(struct input_dev
*dev
)
232 struct gf2k
*gf2k
= dev
->private;
234 del_timer(&gf2k
->timer
);
238 * gf2k_connect() probes for Genius id joysticks.
241 static void gf2k_connect(struct gameport
*gameport
, struct gameport_dev
*dev
)
244 unsigned char data
[GF2K_LENGTH
];
247 if (!(gf2k
= kmalloc(sizeof(struct gf2k
), GFP_KERNEL
)))
249 memset(gf2k
, 0, sizeof(struct gf2k
));
251 gameport
->private = gf2k
;
253 gf2k
->gameport
= gameport
;
254 init_timer(&gf2k
->timer
);
255 gf2k
->timer
.data
= (long) gf2k
;
256 gf2k
->timer
.function
= gf2k_timer
;
258 if (gameport_open(gameport
, dev
, GAMEPORT_MODE_RAW
))
261 gf2k_trigger_seq(gameport
, gf2k_seq_reset
);
263 msleep(GF2K_TIMEOUT
);
265 gf2k_trigger_seq(gameport
, gf2k_seq_digital
);
267 msleep(GF2K_TIMEOUT
);
269 if (gf2k_read_packet(gameport
, GF2K_LENGTH
, data
) < 12)
272 if (!(gf2k
->id
= GB(7,2,0) | GB(3,3,2) | GB(0,3,5)))
276 if ((gf2k
->id
!= (GB(19,2,0) | GB(15,3,2) | GB(12,3,5))) ||
277 (gf2k
->id
!= (GB(31,2,0) | GB(27,3,2) | GB(24,3,5))))
283 if (gf2k
->id
> GF2K_ID_MAX
|| !gf2k_axes
[gf2k
->id
]) {
284 printk(KERN_WARNING
"gf2k.c: Not yet supported joystick on %s. [id: %d type:%s]\n",
285 gameport
->phys
, gf2k
->id
, gf2k
->id
> GF2K_ID_MAX
? "Unknown" : gf2k_names
[gf2k
->id
]);
289 sprintf(gf2k
->phys
, "%s/input0", gameport
->phys
);
291 gf2k
->length
= gf2k_lens
[gf2k
->id
];
293 init_input_dev(&gf2k
->dev
);
295 gf2k
->dev
.private = gf2k
;
296 gf2k
->dev
.open
= gf2k_open
;
297 gf2k
->dev
.close
= gf2k_close
;
298 gf2k
->dev
.evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
300 gf2k
->dev
.name
= gf2k_names
[gf2k
->id
];
301 gf2k
->dev
.phys
= gf2k
->phys
;
302 gf2k
->dev
.id
.bustype
= BUS_GAMEPORT
;
303 gf2k
->dev
.id
.vendor
= GAMEPORT_ID_VENDOR_GENIUS
;
304 gf2k
->dev
.id
.product
= gf2k
->id
;
305 gf2k
->dev
.id
.version
= 0x0100;
307 for (i
= 0; i
< gf2k_axes
[gf2k
->id
]; i
++)
308 set_bit(gf2k_abs
[i
], gf2k
->dev
.absbit
);
310 for (i
= 0; i
< gf2k_hats
[gf2k
->id
]; i
++) {
311 set_bit(ABS_HAT0X
+ i
, gf2k
->dev
.absbit
);
312 gf2k
->dev
.absmin
[ABS_HAT0X
+ i
] = -1;
313 gf2k
->dev
.absmax
[ABS_HAT0X
+ i
] = 1;
316 for (i
= 0; i
< gf2k_joys
[gf2k
->id
]; i
++)
317 set_bit(gf2k_btn_joy
[i
], gf2k
->dev
.keybit
);
319 for (i
= 0; i
< gf2k_pads
[gf2k
->id
]; i
++)
320 set_bit(gf2k_btn_pad
[i
], gf2k
->dev
.keybit
);
322 gf2k_read_packet(gameport
, gf2k
->length
, data
);
323 gf2k_read(gf2k
, data
);
325 for (i
= 0; i
< gf2k_axes
[gf2k
->id
]; i
++) {
326 gf2k
->dev
.absmax
[gf2k_abs
[i
]] = (i
< 2) ? gf2k
->dev
.abs
[gf2k_abs
[i
]] * 2 - 32 :
327 gf2k
->dev
.abs
[gf2k_abs
[0]] + gf2k
->dev
.abs
[gf2k_abs
[1]] - 32;
328 gf2k
->dev
.absmin
[gf2k_abs
[i
]] = 32;
329 gf2k
->dev
.absfuzz
[gf2k_abs
[i
]] = 8;
330 gf2k
->dev
.absflat
[gf2k_abs
[i
]] = (i
< 2) ? 24 : 0;
333 input_register_device(&gf2k
->dev
);
334 printk(KERN_INFO
"input: %s on %s\n", gf2k_names
[gf2k
->id
], gameport
->phys
);
337 fail2
: gameport_close(gameport
);
341 static void gf2k_disconnect(struct gameport
*gameport
)
343 struct gf2k
*gf2k
= gameport
->private;
344 input_unregister_device(&gf2k
->dev
);
345 gameport_close(gameport
);
349 static struct gameport_dev gf2k_dev
= {
350 .connect
= gf2k_connect
,
351 .disconnect
= gf2k_disconnect
,
354 int __init
gf2k_init(void)
356 gameport_register_device(&gf2k_dev
);
360 void __exit
gf2k_exit(void)
362 gameport_unregister_device(&gf2k_dev
);
365 module_init(gf2k_init
);
366 module_exit(gf2k_exit
);