xref: /openbmc/linux/drivers/input/joystick/gf2k.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
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>
38 
39 #define DRIVER_DESC	"Genius Flight 2000 joystick driver"
40 
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION(DRIVER_DESC);
43 MODULE_LICENSE("GPL");
44 
45 #define GF2K_START		400	/* The time we wait for the first bit [400 us] */
46 #define GF2K_STROBE		40	/* The time we wait for the first bit [40 us] */
47 #define GF2K_TIMEOUT		4	/* Wait for everything to settle [4 ms] */
48 #define GF2K_LENGTH		80	/* Max number of triplets in a packet */
49 
50 /*
51  * Genius joystick ids ...
52  */
53 
54 #define GF2K_ID_G09		1
55 #define GF2K_ID_F30D		2
56 #define GF2K_ID_F30		3
57 #define GF2K_ID_F31D		4
58 #define GF2K_ID_F305		5
59 #define GF2K_ID_F23P		6
60 #define GF2K_ID_F31		7
61 #define GF2K_ID_MAX		7
62 
63 static char gf2k_length[] = { 40, 40, 40, 40, 40, 40, 40, 40 };
64 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 
66 static char *gf2k_names[] = {"", "Genius G-09D", "Genius F-30D", "Genius F-30", "Genius MaxFighter F-31D",
67 				"Genius F-30-5", "Genius Flight2000 F-23", "Genius F-31"};
68 static unsigned char gf2k_hats[] = { 0, 2, 0, 0, 2, 0, 2, 0 };
69 static unsigned char gf2k_axes[] = { 0, 2, 0, 0, 4, 0, 4, 0 };
70 static unsigned char gf2k_joys[] = { 0, 0, 0, 0,10, 0, 8, 0 };
71 static unsigned char gf2k_pads[] = { 0, 6, 0, 0, 0, 0, 0, 0 };
72 static unsigned char gf2k_lens[] = { 0,18, 0, 0,18, 0,18, 0 };
73 
74 static unsigned char gf2k_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, ABS_GAS, ABS_BRAKE };
75 static short gf2k_btn_joy[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 };
76 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 
78 
79 static short gf2k_seq_reset[] = { 240, 340, 0 };
80 static short gf2k_seq_digital[] = { 590, 320, 860, 0 };
81 
82 struct gf2k {
83 	struct gameport *gameport;
84 	struct input_dev dev;
85 	int reads;
86 	int bads;
87 	unsigned char id;
88 	unsigned char length;
89 	char phys[32];
90 };
91 
92 /*
93  * gf2k_read_packet() reads a Genius Flight2000 packet.
94  */
95 
96 static int gf2k_read_packet(struct gameport *gameport, int length, char *data)
97 {
98 	unsigned char u, v;
99 	int i;
100 	unsigned int t, p;
101 	unsigned long flags;
102 
103 	t = gameport_time(gameport, GF2K_START);
104 	p = gameport_time(gameport, GF2K_STROBE);
105 
106 	i = 0;
107 
108 	local_irq_save(flags);
109 
110 	gameport_trigger(gameport);
111 	v = gameport_read(gameport);
112 
113 	while (t > 0 && i < length) {
114 		t--; u = v;
115 		v = gameport_read(gameport);
116 		if (v & ~u & 0x10) {
117 			data[i++] = v >> 5;
118 			t = p;
119 		}
120 	}
121 
122 	local_irq_restore(flags);
123 
124 	return i;
125 }
126 
127 /*
128  * gf2k_trigger_seq() initializes a Genius Flight2000 joystick
129  * into digital mode.
130  */
131 
132 static void gf2k_trigger_seq(struct gameport *gameport, short *seq)
133 {
134 
135 	unsigned long flags;
136 	int i, t;
137 
138         local_irq_save(flags);
139 
140 	i = 0;
141         do {
142 		gameport_trigger(gameport);
143 		t = gameport_time(gameport, GF2K_TIMEOUT * 1000);
144 		while ((gameport_read(gameport) & 1) && t) t--;
145                 udelay(seq[i]);
146         } while (seq[++i]);
147 
148 	gameport_trigger(gameport);
149 
150 	local_irq_restore(flags);
151 }
152 
153 /*
154  * js_sw_get_bits() composes bits from the triplet buffer into a __u64.
155  * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
156  * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
157  * is number of bits per triplet.
158  */
159 
160 #define GB(p,n,s)	gf2k_get_bits(data, p, n, s)
161 
162 static int gf2k_get_bits(unsigned char *buf, int pos, int num, int shift)
163 {
164 	__u64 data = 0;
165 	int i;
166 
167 	for (i = 0; i < num / 3 + 2; i++)
168 		data |= buf[pos / 3 + i] << (i * 3);
169 	data >>= pos % 3;
170 	data &= (1 << num) - 1;
171 	data <<= shift;
172 
173 	return data;
174 }
175 
176 static void gf2k_read(struct gf2k *gf2k, unsigned char *data)
177 {
178 	struct input_dev *dev = &gf2k->dev;
179 	int i, t;
180 
181 	for (i = 0; i < 4 && i < gf2k_axes[gf2k->id]; i++)
182 		input_report_abs(dev, gf2k_abs[i], GB(i<<3,8,0) | GB(i+46,1,8) | GB(i+50,1,9));
183 
184 	for (i = 0; i < 2 && i < gf2k_axes[gf2k->id] - 4; i++)
185 		input_report_abs(dev, gf2k_abs[i], GB(i*9+60,8,0) | GB(i+54,1,9));
186 
187 	t = GB(40,4,0);
188 
189 	for (i = 0; i < gf2k_hats[gf2k->id]; i++)
190 		input_report_abs(dev, ABS_HAT0X + i, gf2k_hat_to_axis[t][i]);
191 
192 	t = GB(44,2,0) | GB(32,8,2) | GB(78,2,10);
193 
194 	for (i = 0; i < gf2k_joys[gf2k->id]; i++)
195 		input_report_key(dev, gf2k_btn_joy[i], (t >> i) & 1);
196 
197 	for (i = 0; i < gf2k_pads[gf2k->id]; i++)
198 		input_report_key(dev, gf2k_btn_pad[i], (t >> i) & 1);
199 
200 	input_sync(dev);
201 }
202 
203 /*
204  * gf2k_poll() reads and analyzes Genius joystick data.
205  */
206 
207 static void gf2k_poll(struct gameport *gameport)
208 {
209 	struct gf2k *gf2k = gameport_get_drvdata(gameport);
210 	unsigned char data[GF2K_LENGTH];
211 
212 	gf2k->reads++;
213 
214 	if (gf2k_read_packet(gf2k->gameport, gf2k_length[gf2k->id], data) < gf2k_length[gf2k->id])
215 		gf2k->bads++;
216 	else
217 		gf2k_read(gf2k, data);
218 }
219 
220 static int gf2k_open(struct input_dev *dev)
221 {
222 	struct gf2k *gf2k = dev->private;
223 
224 	gameport_start_polling(gf2k->gameport);
225 	return 0;
226 }
227 
228 static void gf2k_close(struct input_dev *dev)
229 {
230 	struct gf2k *gf2k = dev->private;
231 
232 	gameport_stop_polling(gf2k->gameport);
233 }
234 
235 /*
236  * gf2k_connect() probes for Genius id joysticks.
237  */
238 
239 static int gf2k_connect(struct gameport *gameport, struct gameport_driver *drv)
240 {
241 	struct gf2k *gf2k;
242 	unsigned char data[GF2K_LENGTH];
243 	int i, err;
244 
245 	if (!(gf2k = kcalloc(1, sizeof(struct gf2k), GFP_KERNEL)))
246 		return -ENOMEM;
247 
248 	gf2k->gameport = gameport;
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 	sprintf(gf2k->phys, "%s/input0", gameport->phys);
295 
296 	gf2k->length = gf2k_lens[gf2k->id];
297 
298 	init_input_dev(&gf2k->dev);
299 
300 	gf2k->dev.private = gf2k;
301 	gf2k->dev.open = gf2k_open;
302 	gf2k->dev.close = gf2k_close;
303 	gf2k->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
304 
305 	gf2k->dev.name = gf2k_names[gf2k->id];
306 	gf2k->dev.phys = gf2k->phys;
307 	gf2k->dev.id.bustype = BUS_GAMEPORT;
308 	gf2k->dev.id.vendor = GAMEPORT_ID_VENDOR_GENIUS;
309 	gf2k->dev.id.product = gf2k->id;
310 	gf2k->dev.id.version = 0x0100;
311 
312 	for (i = 0; i < gf2k_axes[gf2k->id]; i++)
313 		set_bit(gf2k_abs[i], gf2k->dev.absbit);
314 
315 	for (i = 0; i < gf2k_hats[gf2k->id]; i++) {
316 		set_bit(ABS_HAT0X + i, gf2k->dev.absbit);
317 		gf2k->dev.absmin[ABS_HAT0X + i] = -1;
318 		gf2k->dev.absmax[ABS_HAT0X + i] = 1;
319 	}
320 
321 	for (i = 0; i < gf2k_joys[gf2k->id]; i++)
322 		set_bit(gf2k_btn_joy[i], gf2k->dev.keybit);
323 
324 	for (i = 0; i < gf2k_pads[gf2k->id]; i++)
325 		set_bit(gf2k_btn_pad[i], gf2k->dev.keybit);
326 
327 	gf2k_read_packet(gameport, gf2k->length, data);
328 	gf2k_read(gf2k, data);
329 
330 	for (i = 0; i < gf2k_axes[gf2k->id]; i++) {
331 		gf2k->dev.absmax[gf2k_abs[i]] = (i < 2) ? gf2k->dev.abs[gf2k_abs[i]] * 2 - 32 :
332 	      		  gf2k->dev.abs[gf2k_abs[0]] + gf2k->dev.abs[gf2k_abs[1]] - 32;
333 		gf2k->dev.absmin[gf2k_abs[i]] = 32;
334 		gf2k->dev.absfuzz[gf2k_abs[i]] = 8;
335 		gf2k->dev.absflat[gf2k_abs[i]] = (i < 2) ? 24 : 0;
336 	}
337 
338 	input_register_device(&gf2k->dev);
339 	printk(KERN_INFO "input: %s on %s\n", gf2k_names[gf2k->id], gameport->phys);
340 
341 	return 0;
342 
343 fail2:	gameport_close(gameport);
344 fail1:	gameport_set_drvdata(gameport, NULL);
345 	kfree(gf2k);
346 	return err;
347 }
348 
349 static void gf2k_disconnect(struct gameport *gameport)
350 {
351 	struct gf2k *gf2k = gameport_get_drvdata(gameport);
352 
353 	input_unregister_device(&gf2k->dev);
354 	gameport_close(gameport);
355 	gameport_set_drvdata(gameport, NULL);
356 	kfree(gf2k);
357 }
358 
359 static struct gameport_driver gf2k_drv = {
360 	.driver		= {
361 		.name	= "gf2k",
362 	},
363 	.description	= DRIVER_DESC,
364 	.connect	= gf2k_connect,
365 	.disconnect	= gf2k_disconnect,
366 };
367 
368 static int __init gf2k_init(void)
369 {
370 	gameport_register_driver(&gf2k_drv);
371 	return 0;
372 }
373 
374 static void __exit gf2k_exit(void)
375 {
376 	gameport_unregister_driver(&gf2k_drv);
377 }
378 
379 module_init(gf2k_init);
380 module_exit(gf2k_exit);
381