30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
34 #include <linux/input.h>
35 #include <linux/gameport.h>
38 #define DRIVER_DESC "Genius Flight 2000 joystick driver"
44 #define GF2K_START 400
45 #define GF2K_STROBE 40
46 #define GF2K_TIMEOUT 4
47 #define GF2K_LENGTH 80
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 };
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 };
83 struct input_dev *
dev;
109 gameport_trigger(gameport);
110 v = gameport_read(gameport);
112 while (t > 0 && i < length) {
114 v = gameport_read(gameport);
141 gameport_trigger(gameport);
143 while ((gameport_read(gameport) & 1) && t) t--;
147 gameport_trigger(gameport);
159 #define GB(p,n,s) gf2k_get_bits(data, p, n, s)
161 static int gf2k_get_bits(
unsigned char *
buf,
int pos,
int num,
int shift)
166 for (i = 0; i < num / 3 + 2; i++)
167 data |= buf[pos / 3 + i] << (i * 3);
169 data &= (1 << num) - 1;
175 static void gf2k_read(
struct gf2k *
gf2k,
unsigned char *
data)
177 struct input_dev *
dev = gf2k->
dev;
180 for (i = 0; i < 4 && i < gf2k_axes[gf2k->
id]; i++)
181 input_report_abs(dev, gf2k_abs[i],
GB(i<<3,8,0) |
GB(i+46,1,8) |
GB(i+50,1,9));
183 for (i = 0; i < 2 && i < gf2k_axes[gf2k->
id] - 4; i++)
184 input_report_abs(dev, gf2k_abs[i],
GB(i*9+60,8,0) |
GB(i+54,1,9));
188 for (i = 0; i < gf2k_hats[gf2k->
id]; i++)
189 input_report_abs(dev,
ABS_HAT0X + i, gf2k_hat_to_axis[t][i]);
191 t =
GB(44,2,0) |
GB(32,8,2) |
GB(78,2,10);
193 for (i = 0; i < gf2k_joys[gf2k->
id]; i++)
194 input_report_key(dev, gf2k_btn_joy[i], (t >> i) & 1);
196 for (i = 0; i < gf2k_pads[gf2k->
id]; i++)
197 input_report_key(dev, gf2k_btn_pad[i], (t >> i) & 1);
206 static void gf2k_poll(
struct gameport *gameport)
208 struct gf2k *gf2k = gameport_get_drvdata(gameport);
213 if (gf2k_read_packet(gf2k->
gameport, gf2k_length[gf2k->
id], data) < gf2k_length[gf2k->
id])
216 gf2k_read(gf2k, data);
219 static int gf2k_open(
struct input_dev *dev)
221 struct gf2k *gf2k = input_get_drvdata(dev);
227 static void gf2k_close(
struct input_dev *dev)
229 struct gf2k *gf2k = input_get_drvdata(dev);
238 static int gf2k_connect(
struct gameport *gameport,
struct gameport_driver *drv)
241 struct input_dev *input_dev;
245 gf2k = kzalloc(
sizeof(
struct gf2k),
GFP_KERNEL);
246 input_dev = input_allocate_device();
247 if (!gf2k || !input_dev) {
253 gf2k->
dev = input_dev;
255 gameport_set_drvdata(gameport, gf2k);
261 gf2k_trigger_seq(gameport, gf2k_seq_reset);
265 gf2k_trigger_seq(gameport, gf2k_seq_digital);
269 if (gf2k_read_packet(gameport,
GF2K_LENGTH, data) < 12) {
274 if (!(gf2k->
id =
GB(7,2,0) |
GB(3,3,2) |
GB(0,3,5))) {
280 if ((gf2k->
id != (
GB(19,2,0) |
GB(15,3,2) |
GB(12,3,5))) &&
281 (gf2k->
id != (
GB(31,2,0) |
GB(27,3,2) |
GB(24,3,5)))) {
296 gameport_set_poll_handler(gameport, gf2k_poll);
297 gameport_set_poll_interval(gameport, 20);
303 input_dev->name = gf2k_names[gf2k->
id];
304 input_dev->phys = gf2k->
phys;
307 input_dev->id.product = gf2k->
id;
308 input_dev->id.version = 0x0100;
309 input_dev->dev.parent = &gameport->
dev;
311 input_set_drvdata(input_dev, gf2k);
313 input_dev->open = gf2k_open;
314 input_dev->close = gf2k_close;
318 for (i = 0; i < gf2k_axes[gf2k->
id]; i++)
319 set_bit(gf2k_abs[i], input_dev->absbit);
321 for (i = 0; i < gf2k_hats[gf2k->
id]; i++)
322 input_set_abs_params(input_dev,
ABS_HAT0X + i, -1, 1, 0, 0);
324 for (i = 0; i < gf2k_joys[gf2k->
id]; i++)
325 set_bit(gf2k_btn_joy[i], input_dev->keybit);
327 for (i = 0; i < gf2k_pads[gf2k->
id]; i++)
328 set_bit(gf2k_btn_pad[i], input_dev->keybit);
330 gf2k_read_packet(gameport, gf2k->
length, data);
331 gf2k_read(gf2k, data);
333 for (i = 0; i < gf2k_axes[gf2k->
id]; i++) {
335 input_abs_get_val(input_dev, gf2k_abs[i]) * 2 :
336 input_abs_get_val(input_dev, gf2k_abs[0]) +
337 input_abs_get_val(input_dev, gf2k_abs[1]);
338 int flat = i < 2 ? 24 : 0;
340 input_set_abs_params(input_dev, gf2k_abs[i],
341 32, max - 32, 8, flat);
344 err = input_register_device(gf2k->
dev);
351 fail1: gameport_set_drvdata(gameport,
NULL);
352 input_free_device(input_dev);
357 static void gf2k_disconnect(
struct gameport *gameport)
359 struct gf2k *gf2k = gameport_get_drvdata(gameport);
361 input_unregister_device(gf2k->
dev);
363 gameport_set_drvdata(gameport,
NULL);
372 .connect = gf2k_connect,
373 .disconnect = gf2k_disconnect,