2 * $Id: interact.c,v 1.16 2002/01/22 20:28:25 vojtech Exp $
4 * Copyright (c) 2001 Vojtech Pavlik
6 * Based on the work of:
11 * InterAct digital gamepad/joystick driver for Linux
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Should you need to contact me, the author, you can do so either by
30 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
31 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <linux/delay.h>
38 #include <linux/init.h>
39 #include <linux/gameport.h>
40 #include <linux/input.h>
41 #include <linux/jiffies.h>
43 #define DRIVER_DESC "InterAct digital joystick driver"
45 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
46 MODULE_DESCRIPTION(DRIVER_DESC);
47 MODULE_LICENSE("GPL");
49 #define INTERACT_MAX_START 600 /* 400 us */
50 #define INTERACT_MAX_STROBE 60 /* 40 us */
51 #define INTERACT_MAX_LENGTH 32 /* 32 bits */
53 #define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
54 #define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
57 struct gameport *gameport;
58 struct input_dev *dev;
66 static short interact_abs_hhfx[] =
67 { ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
68 static short interact_abs_pp8d[] =
71 static short interact_btn_hhfx[] =
72 { 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 };
73 static short interact_btn_pp8d[] =
74 { BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
76 struct interact_type {
85 static struct interact_type interact_type[] = {
86 { 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX", 32, 4 },
87 { 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
91 * interact_read_packet() reads and InterAct joystick data.
94 static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
102 data[0] = data[1] = data[2] = 0;
103 t = gameport_time(gameport, INTERACT_MAX_START);
104 s = gameport_time(gameport, INTERACT_MAX_STROBE);
106 local_irq_save(flags);
107 gameport_trigger(gameport);
108 v = gameport_read(gameport);
110 while (t > 0 && i < length) {
112 u = v; v = gameport_read(gameport);
114 data[0] = (data[0] << 1) | ((v >> 4) & 1);
115 data[1] = (data[1] << 1) | ((v >> 5) & 1);
116 data[2] = (data[2] << 1) | ((v >> 7) & 1);
122 local_irq_restore(flags);
128 * interact_poll() reads and analyzes InterAct joystick data.
131 static void interact_poll(struct gameport *gameport)
133 struct interact *interact = gameport_get_drvdata(gameport);
134 struct input_dev *dev = interact->dev;
140 if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
144 for (i = 0; i < 3; i++)
145 data[i] <<= INTERACT_MAX_LENGTH - interact->length;
147 switch (interact->type) {
149 case INTERACT_TYPE_HHFX:
151 for (i = 0; i < 4; i++)
152 input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
154 for (i = 0; i < 2; i++)
155 input_report_abs(dev, ABS_HAT0Y - i,
156 ((data[1] >> ((i << 1) + 17)) & 1) - ((data[1] >> ((i << 1) + 16)) & 1));
158 for (i = 0; i < 8; i++)
159 input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
161 for (i = 0; i < 4; i++)
162 input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
166 case INTERACT_TYPE_PP8D:
168 for (i = 0; i < 2; i++)
169 input_report_abs(dev, interact_abs_pp8d[i],
170 ((data[0] >> ((i << 1) + 20)) & 1) - ((data[0] >> ((i << 1) + 21)) & 1));
172 for (i = 0; i < 8; i++)
173 input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
183 * interact_open() is a callback from the input open routine.
186 static int interact_open(struct input_dev *dev)
188 struct interact *interact = input_get_drvdata(dev);
190 gameport_start_polling(interact->gameport);
195 * interact_close() is a callback from the input close routine.
198 static void interact_close(struct input_dev *dev)
200 struct interact *interact = input_get_drvdata(dev);
202 gameport_stop_polling(interact->gameport);
206 * interact_connect() probes for InterAct joysticks.
209 static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
211 struct interact *interact;
212 struct input_dev *input_dev;
217 interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
218 input_dev = input_allocate_device();
219 if (!interact || !input_dev) {
224 interact->gameport = gameport;
225 interact->dev = input_dev;
227 gameport_set_drvdata(gameport, interact);
229 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
233 i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
235 if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
240 for (i = 0; interact_type[i].length; i++)
241 if (interact_type[i].id == (data[2] >> 16))
244 if (!interact_type[i].length) {
245 printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
246 gameport->phys, i, data[0], data[1], data[2]);
251 gameport_set_poll_handler(gameport, interact_poll);
252 gameport_set_poll_interval(gameport, 20);
254 snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
257 interact->length = interact_type[i].length;
259 input_dev->name = interact_type[i].name;
260 input_dev->phys = interact->phys;
261 input_dev->id.bustype = BUS_GAMEPORT;
262 input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
263 input_dev->id.product = interact_type[i].id;
264 input_dev->id.version = 0x0100;
265 input_dev->dev.parent = &gameport->dev;
267 input_set_drvdata(input_dev, interact);
269 input_dev->open = interact_open;
270 input_dev->close = interact_close;
272 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
274 for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
275 set_bit(t, input_dev->absbit);
276 if (i < interact_type[interact->type].b8) {
277 input_dev->absmin[t] = 0;
278 input_dev->absmax[t] = 255;
280 input_dev->absmin[t] = -1;
281 input_dev->absmax[t] = 1;
285 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
286 set_bit(t, input_dev->keybit);
288 err = input_register_device(interact->dev);
294 fail2: gameport_close(gameport);
295 fail1: gameport_set_drvdata(gameport, NULL);
296 input_free_device(input_dev);
301 static void interact_disconnect(struct gameport *gameport)
303 struct interact *interact = gameport_get_drvdata(gameport);
305 input_unregister_device(interact->dev);
306 gameport_close(gameport);
307 gameport_set_drvdata(gameport, NULL);
311 static struct gameport_driver interact_drv = {
315 .description = DRIVER_DESC,
316 .connect = interact_connect,
317 .disconnect = interact_disconnect,
320 static int __init interact_init(void)
322 gameport_register_driver(&interact_drv);
326 static void __exit interact_exit(void)
328 gameport_unregister_driver(&interact_drv);
331 module_init(interact_init);
332 module_exit(interact_exit);