Import 2.4.0-test2pre7
[davej-history.git] / drivers / char / joystick / interact.c
blob7104e5d49e8d492c0b2a901f233f71ce818e1a5d
1 /*
2 * $Id: interact.c,v 1.8 2000/05/29 11:19:51 vojtech Exp $
4 * Copyright (c) 2000 Vojtech Pavlik
6 * Based on the work of:
7 * Toby Deshane
9 * Sponsored by SuSE
13 * InterAct digital gamepad/joystick driver for Linux
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * Should you need to contact me, the author, you can do so either by
32 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
33 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
36 #include <linux/kernel.h>
37 #include <linux/malloc.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/init.h>
41 #include <linux/gameport.h>
42 #include <linux/input.h>
44 #define INTERACT_MAX_START 400 /* 400 us */
45 #define INTERACT_MAX_STROBE 40 /* 40 us */
46 #define INTERACT_MAX_LENGTH 32 /* 32 bits */
47 #define INTERACT_REFRESH_TIME HZ/50 /* 20 ms */
49 #define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
50 #define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
52 struct interact {
53 struct gameport *gameport;
54 struct input_dev dev;
55 struct timer_list timer;
56 int used;
57 int bads;
58 int reads;
59 unsigned char type;
60 unsigned char length;
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 };
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 };
73 struct interact_type {
74 int id;
75 short *abs;
76 short *btn;
77 char *name;
78 unsigned char length;
79 unsigned char b8;
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 }};
88 * interact_read_packet() reads and InterAct joystick data.
91 static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
93 unsigned long flags;
94 unsigned char u, v;
95 unsigned int t, s;
96 int i;
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);
103 __save_flags(flags);
104 __cli();
105 gameport_trigger(gameport);
106 v = gameport_read(gameport);
108 while (t > 0 && i < length) {
109 t--;
110 u = v; v = gameport_read(gameport);
111 if (v & ~u & 0x40) {
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);
115 i++;
116 t = s;
120 __restore_flags(flags);
122 return i;
126 * interact_timer() reads and analyzes InterAct joystick data.
129 static void interact_timer(unsigned long private)
131 struct interact *interact = (struct interact *) private;
132 struct input_dev *dev = &interact->dev;
133 u32 data[3];
134 int i;
136 interact->reads++;
138 if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
139 interact->bads++;
140 } else
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);
162 break;
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);
173 break;
176 mod_timer(&interact->timer, jiffies + INTERACT_REFRESH_TIME);
181 * interact_open() is a callback from the input open routine.
184 static int interact_open(struct input_dev *dev)
186 struct interact *interact = dev->private;
187 if (!interact->used++)
188 mod_timer(&interact->timer, jiffies + INTERACT_REFRESH_TIME);
189 return 0;
193 * interact_close() is a callback from the input close routine.
196 static void interact_close(struct input_dev *dev)
198 struct interact *interact = dev->private;
199 if (!--interact->used)
200 del_timer(&interact->timer);
204 * interact_connect() probes for InterAct joysticks.
207 static void interact_connect(struct gameport *gameport, struct gameport_dev *dev)
209 struct interact *interact;
210 __u32 data[3];
211 int i, t;
213 if (!(interact = kmalloc(sizeof(struct interact), GFP_KERNEL)))
214 return;
215 memset(interact, 0, sizeof(struct interact));
217 gameport->private = interact;
219 interact->gameport = gameport;
220 init_timer(&interact->timer);
221 interact->timer.data = (long) interact;
222 interact->timer.function = interact_timer;
224 if (gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
225 goto fail1;
227 i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
229 if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
230 goto fail2;
233 for (i = 0; interact_type[i].length; i++)
234 if (interact_type[i].id == (data[2] >> 16))
235 break;
237 if (!interact_type[i].length) {
238 printk(KERN_WARNING "interact.c: Unknown joystick on gameport%d. [len %d d0 %08x d1 %08x i2 %08x]\n",
239 gameport->number, i, data[0], data[1], data[2]);
240 goto fail2;
243 interact->type = i;
244 interact->length = interact_type[i].length;
246 interact->dev.private = interact;
247 interact->dev.open = interact_open;
248 interact->dev.close = interact_close;
250 interact->dev.name = interact_type[i].name;
251 interact->dev.idbus = BUS_GAMEPORT;
252 interact->dev.idvendor = GAMEPORT_ID_VENDOR_INTERACT;
253 interact->dev.idproduct = interact_type[i].id;
254 interact->dev.idversion = 0x0100;
256 interact->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
258 for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
259 set_bit(t, interact->dev.absbit);
260 if (i < interact_type[interact->type].b8) {
261 interact->dev.absmin[t] = 0;
262 interact->dev.absmax[t] = 255;
263 } else {
264 interact->dev.absmin[t] = -1;
265 interact->dev.absmax[t] = 1;
269 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
270 set_bit(t, interact->dev.keybit);
272 input_register_device(&interact->dev);
273 printk(KERN_INFO "input%d: %s on gameport%d.0\n",
274 interact->dev.number, interact_type[interact->type].name, gameport->number);
276 return;
277 fail2: gameport_close(gameport);
278 fail1: kfree(interact);
281 static void interact_disconnect(struct gameport *gameport)
283 struct interact *interact = gameport->private;
284 input_unregister_device(&interact->dev);
285 gameport_close(gameport);
286 kfree(interact);
289 static struct gameport_dev interact_dev = {
290 connect: interact_connect,
291 disconnect: interact_disconnect,
294 int __init interact_init(void)
296 gameport_register_device(&interact_dev);
297 return 0;
300 void __exit interact_exit(void)
302 gameport_unregister_device(&interact_dev);
305 module_init(interact_init);
306 module_exit(interact_exit);