2 * Copyright (c) 2001 Vojtech Pavlik
4 * Based on the work of:
9 * InterAct digital gamepad/joystick driver for Linux
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.
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.
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
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
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/delay.h>
36 #include <linux/init.h>
37 #include <linux/gameport.h>
38 #include <linux/input.h>
39 #include <linux/jiffies.h>
41 #define DRIVER_DESC "InterAct digital joystick driver"
43 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44 MODULE_DESCRIPTION(DRIVER_DESC);
45 MODULE_LICENSE("GPL");
47 #define INTERACT_MAX_START 600 /* 400 us */
48 #define INTERACT_MAX_STROBE 60 /* 40 us */
49 #define INTERACT_MAX_LENGTH 32 /* 32 bits */
51 #define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
52 #define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
55 struct gameport *gameport;
56 struct input_dev *dev;
64 static short interact_abs_hhfx[] =
65 { ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
66 static short interact_abs_pp8d[] =
69 static short interact_btn_hhfx[] =
70 { 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 };
71 static short interact_btn_pp8d[] =
72 { BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
74 struct interact_type {
83 static struct interact_type interact_type[] = {
84 { 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX", 32, 4 },
85 { 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
89 * interact_read_packet() reads and InterAct joystick data.
92 static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
100 data[0] = data[1] = data[2] = 0;
101 t = gameport_time(gameport, INTERACT_MAX_START);
102 s = gameport_time(gameport, INTERACT_MAX_STROBE);
104 local_irq_save(flags);
105 gameport_trigger(gameport);
106 v = gameport_read(gameport);
108 while (t > 0 && i < length) {
110 u = v; v = gameport_read(gameport);
112 data[0] = (data[0] << 1) | ((v >> 4) & 1);
113 data[1] = (data[1] << 1) | ((v >> 5) & 1);
114 data[2] = (data[2] << 1) | ((v >> 7) & 1);
120 local_irq_restore(flags);
126 * interact_poll() reads and analyzes InterAct joystick data.
129 static void interact_poll(struct gameport *gameport)
131 struct interact *interact = gameport_get_drvdata(gameport);
132 struct input_dev *dev = interact->dev;
138 if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
142 for (i = 0; i < 3; i++)
143 data[i] <<= INTERACT_MAX_LENGTH - interact->length;
145 switch (interact->type) {
147 case INTERACT_TYPE_HHFX:
149 for (i = 0; i < 4; i++)
150 input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
152 for (i = 0; i < 2; i++)
153 input_report_abs(dev, ABS_HAT0Y - i,
154 ((data[1] >> ((i << 1) + 17)) & 1) - ((data[1] >> ((i << 1) + 16)) & 1));
156 for (i = 0; i < 8; i++)
157 input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
159 for (i = 0; i < 4; i++)
160 input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
164 case INTERACT_TYPE_PP8D:
166 for (i = 0; i < 2; i++)
167 input_report_abs(dev, interact_abs_pp8d[i],
168 ((data[0] >> ((i << 1) + 20)) & 1) - ((data[0] >> ((i << 1) + 21)) & 1));
170 for (i = 0; i < 8; i++)
171 input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
181 * interact_open() is a callback from the input open routine.
184 static int interact_open(struct input_dev *dev)
186 struct interact *interact = input_get_drvdata(dev);
188 gameport_start_polling(interact->gameport);
193 * interact_close() is a callback from the input close routine.
196 static void interact_close(struct input_dev *dev)
198 struct interact *interact = input_get_drvdata(dev);
200 gameport_stop_polling(interact->gameport);
204 * interact_connect() probes for InterAct joysticks.
207 static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
209 struct interact *interact;
210 struct input_dev *input_dev;
215 interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
216 input_dev = input_allocate_device();
217 if (!interact || !input_dev) {
222 interact->gameport = gameport;
223 interact->dev = input_dev;
225 gameport_set_drvdata(gameport, interact);
227 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
231 i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
233 if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
238 for (i = 0; interact_type[i].length; i++)
239 if (interact_type[i].id == (data[2] >> 16))
242 if (!interact_type[i].length) {
243 printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
244 gameport->phys, i, data[0], data[1], data[2]);
249 gameport_set_poll_handler(gameport, interact_poll);
250 gameport_set_poll_interval(gameport, 20);
252 snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
255 interact->length = interact_type[i].length;
257 input_dev->name = interact_type[i].name;
258 input_dev->phys = interact->phys;
259 input_dev->id.bustype = BUS_GAMEPORT;
260 input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
261 input_dev->id.product = interact_type[i].id;
262 input_dev->id.version = 0x0100;
263 input_dev->dev.parent = &gameport->dev;
265 input_set_drvdata(input_dev, interact);
267 input_dev->open = interact_open;
268 input_dev->close = interact_close;
270 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
272 for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
273 set_bit(t, input_dev->absbit);
274 if (i < interact_type[interact->type].b8) {
275 input_dev->absmin[t] = 0;
276 input_dev->absmax[t] = 255;
278 input_dev->absmin[t] = -1;
279 input_dev->absmax[t] = 1;
283 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
284 set_bit(t, input_dev->keybit);
286 err = input_register_device(interact->dev);
292 fail2: gameport_close(gameport);
293 fail1: gameport_set_drvdata(gameport, NULL);
294 input_free_device(input_dev);
299 static void interact_disconnect(struct gameport *gameport)
301 struct interact *interact = gameport_get_drvdata(gameport);
303 input_unregister_device(interact->dev);
304 gameport_close(gameport);
305 gameport_set_drvdata(gameport, NULL);
309 static struct gameport_driver interact_drv = {
313 .description = DRIVER_DESC,
314 .connect = interact_connect,
315 .disconnect = interact_disconnect,
318 static int __init interact_init(void)
320 return gameport_register_driver(&interact_drv);
323 static void __exit interact_exit(void)
325 gameport_unregister_driver(&interact_drv);
328 module_init(interact_init);
329 module_exit(interact_exit);