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