xref: /linux/drivers/input/joystick/interact.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  *  Copyright (c) 2001 Vojtech Pavlik
3  *
4  *  Based on the work of:
5  *	Toby Deshane
6  */
7 
8 /*
9  * InterAct digital gamepad/joystick driver for Linux
10  */
11 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * Should you need to contact me, the author, you can do so either by
28  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
30  */
31 
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/delay.h>
36 #include <linux/gameport.h>
37 #include <linux/input.h>
38 #include <linux/jiffies.h>
39 
40 #define DRIVER_DESC	"InterAct digital joystick driver"
41 
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION(DRIVER_DESC);
44 MODULE_LICENSE("GPL");
45 
46 #define INTERACT_MAX_START	600	/* 400 us */
47 #define INTERACT_MAX_STROBE	60	/* 40 us */
48 #define INTERACT_MAX_LENGTH	32	/* 32 bits */
49 
50 #define INTERACT_TYPE_HHFX	0	/* HammerHead/FX */
51 #define INTERACT_TYPE_PP8D	1	/* ProPad 8 */
52 
53 struct interact {
54 	struct gameport *gameport;
55 	struct input_dev *dev;
56 	int bads;
57 	int reads;
58 	unsigned char type;
59 	unsigned char length;
60 	char phys[32];
61 };
62 
63 static short interact_abs_hhfx[] =
64 	{ ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
65 static short interact_abs_pp8d[] =
66 	{ ABS_X, ABS_Y, -1 };
67 
68 static short interact_btn_hhfx[] =
69 	{ BTN_TR, BTN_X, BTN_Y, BTN_Z, BTN_A, BTN_B, BTN_C, BTN_TL, BTN_TL2, BTN_TR2, BTN_MODE, BTN_SELECT, -1 };
70 static short interact_btn_pp8d[] =
71 	{ BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
72 
73 struct interact_type {
74 	int id;
75 	short *abs;
76 	short *btn;
77 	char *name;
78 	unsigned char length;
79 	unsigned char b8;
80 };
81 
82 static struct interact_type interact_type[] = {
83 	{ 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX",    32, 4 },
84 	{ 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
85 	{ 0 }};
86 
87 /*
88  * interact_read_packet() reads and InterAct joystick data.
89  */
90 
91 static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
92 {
93 	unsigned long flags;
94 	unsigned char u, v;
95 	unsigned int t, s;
96 	int i;
97 
98 	i = 0;
99 	data[0] = data[1] = data[2] = 0;
100 	t = gameport_time(gameport, INTERACT_MAX_START);
101 	s = gameport_time(gameport, INTERACT_MAX_STROBE);
102 
103 	local_irq_save(flags);
104 	gameport_trigger(gameport);
105 	v = gameport_read(gameport);
106 
107 	while (t > 0 && i < length) {
108 		t--;
109 		u = v; v = gameport_read(gameport);
110 		if (v & ~u & 0x40) {
111 			data[0] = (data[0] << 1) | ((v >> 4) & 1);
112 			data[1] = (data[1] << 1) | ((v >> 5) & 1);
113 			data[2] = (data[2] << 1) | ((v >> 7) & 1);
114 			i++;
115 			t = s;
116 		}
117 	}
118 
119 	local_irq_restore(flags);
120 
121 	return i;
122 }
123 
124 /*
125  * interact_poll() reads and analyzes InterAct joystick data.
126  */
127 
128 static void interact_poll(struct gameport *gameport)
129 {
130 	struct interact *interact = gameport_get_drvdata(gameport);
131 	struct input_dev *dev = interact->dev;
132 	u32 data[3];
133 	int i;
134 
135 	interact->reads++;
136 
137 	if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
138 		interact->bads++;
139 	} else {
140 
141 		for (i = 0; i < 3; i++)
142 			data[i] <<= INTERACT_MAX_LENGTH - interact->length;
143 
144 		switch (interact->type) {
145 
146 			case INTERACT_TYPE_HHFX:
147 
148 				for (i = 0; i < 4; i++)
149 					input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
150 
151 				for (i = 0; i < 2; i++)
152 					input_report_abs(dev, ABS_HAT0Y - i,
153 						((data[1] >> ((i << 1) + 17)) & 1)  - ((data[1] >> ((i << 1) + 16)) & 1));
154 
155 				for (i = 0; i < 8; i++)
156 					input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
157 
158 				for (i = 0; i < 4; i++)
159 					input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
160 
161 				break;
162 
163 			case INTERACT_TYPE_PP8D:
164 
165 				for (i = 0; i < 2; i++)
166 					input_report_abs(dev, interact_abs_pp8d[i],
167 						((data[0] >> ((i << 1) + 20)) & 1)  - ((data[0] >> ((i << 1) + 21)) & 1));
168 
169 				for (i = 0; i < 8; i++)
170 					input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
171 
172 				break;
173 		}
174 	}
175 
176 	input_sync(dev);
177 }
178 
179 /*
180  * interact_open() is a callback from the input open routine.
181  */
182 
183 static int interact_open(struct input_dev *dev)
184 {
185 	struct interact *interact = input_get_drvdata(dev);
186 
187 	gameport_start_polling(interact->gameport);
188 	return 0;
189 }
190 
191 /*
192  * interact_close() is a callback from the input close routine.
193  */
194 
195 static void interact_close(struct input_dev *dev)
196 {
197 	struct interact *interact = input_get_drvdata(dev);
198 
199 	gameport_stop_polling(interact->gameport);
200 }
201 
202 /*
203  * interact_connect() probes for InterAct joysticks.
204  */
205 
206 static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
207 {
208 	struct interact *interact;
209 	struct input_dev *input_dev;
210 	__u32 data[3];
211 	int i, t;
212 	int err;
213 
214 	interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
215 	input_dev = input_allocate_device();
216 	if (!interact || !input_dev) {
217 		err = -ENOMEM;
218 		goto fail1;
219 	}
220 
221 	interact->gameport = gameport;
222 	interact->dev = input_dev;
223 
224 	gameport_set_drvdata(gameport, interact);
225 
226 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
227 	if (err)
228 		goto fail1;
229 
230 	i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
231 
232 	if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
233 		err = -ENODEV;
234 		goto fail2;
235 	}
236 
237 	for (i = 0; interact_type[i].length; i++)
238 		if (interact_type[i].id == (data[2] >> 16))
239 			break;
240 
241 	if (!interact_type[i].length) {
242 		printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
243 			gameport->phys, i, data[0], data[1], data[2]);
244 		err = -ENODEV;
245 		goto fail2;
246 	}
247 
248 	gameport_set_poll_handler(gameport, interact_poll);
249 	gameport_set_poll_interval(gameport, 20);
250 
251 	snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
252 
253 	interact->type = i;
254 	interact->length = interact_type[i].length;
255 
256 	input_dev->name = interact_type[i].name;
257 	input_dev->phys = interact->phys;
258 	input_dev->id.bustype = BUS_GAMEPORT;
259 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
260 	input_dev->id.product = interact_type[i].id;
261 	input_dev->id.version = 0x0100;
262 	input_dev->dev.parent = &gameport->dev;
263 
264 	input_set_drvdata(input_dev, interact);
265 
266 	input_dev->open = interact_open;
267 	input_dev->close = interact_close;
268 
269 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
270 
271 	for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
272 		if (i < interact_type[interact->type].b8)
273 			input_set_abs_params(input_dev, t, 0, 255, 0, 0);
274 		else
275 			input_set_abs_params(input_dev, t, -1, 1, 0, 0);
276 	}
277 
278 	for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
279 		__set_bit(t, input_dev->keybit);
280 
281 	err = input_register_device(interact->dev);
282 	if (err)
283 		goto fail2;
284 
285 	return 0;
286 
287 fail2:	gameport_close(gameport);
288 fail1:  gameport_set_drvdata(gameport, NULL);
289 	input_free_device(input_dev);
290 	kfree(interact);
291 	return err;
292 }
293 
294 static void interact_disconnect(struct gameport *gameport)
295 {
296 	struct interact *interact = gameport_get_drvdata(gameport);
297 
298 	input_unregister_device(interact->dev);
299 	gameport_close(gameport);
300 	gameport_set_drvdata(gameport, NULL);
301 	kfree(interact);
302 }
303 
304 static struct gameport_driver interact_drv = {
305 	.driver		= {
306 		.name	= "interact",
307 	},
308 	.description	= DRIVER_DESC,
309 	.connect	= interact_connect,
310 	.disconnect	= interact_disconnect,
311 };
312 
313 module_gameport_driver(interact_drv);
314