1 /*
2 * $Id: interact.c,v 1.8 2000/05/29 11:19:51 vojtech Exp $
3 *
4 * Copyright (c) 2000 Vojtech Pavlik
5 *
6 * Based on the work of:
7 * Toby Deshane
8 *
9 * Sponsored by SuSE
10 */
11
12 /*
13 * InterAct digital gamepad/joystick driver for Linux
14 */
15
16 /*
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.
21 *
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.
26 *
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
30 *
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
34 */
35
36 #include <linux/kernel.h>
37 #include <linux/slab.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>
43
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 */
48
49 #define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
50 #define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
51
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;
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
interact_read_packet(struct gameport * gameport,int length,u32 * data)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 __save_flags(flags);
104 __cli();
105 gameport_trigger(gameport);
106 v = gameport_read(gameport);
107
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;
117 }
118 }
119
120 __restore_flags(flags);
121
122 return i;
123 }
124
125 /*
126 * interact_timer() reads and analyzes InterAct joystick data.
127 */
128
interact_timer(unsigned long private)129 static void interact_timer(unsigned long private)
130 {
131 struct interact *interact = (struct interact *) private;
132 struct input_dev *dev = &interact->dev;
133 u32 data[3];
134 int i;
135
136 interact->reads++;
137
138 if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
139 interact->bads++;
140 } else
141
142 for (i = 0; i < 3; i++)
143 data[i] <<= INTERACT_MAX_LENGTH - interact->length;
144
145 switch (interact->type) {
146
147 case INTERACT_TYPE_HHFX:
148
149 for (i = 0; i < 4; i++)
150 input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
151
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));
155
156 for (i = 0; i < 8; i++)
157 input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
158
159 for (i = 0; i < 4; i++)
160 input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
161
162 break;
163
164 case INTERACT_TYPE_PP8D:
165
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));
169
170 for (i = 0; i < 8; i++)
171 input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
172
173 break;
174 }
175
176 mod_timer(&interact->timer, jiffies + INTERACT_REFRESH_TIME);
177
178 }
179
180 /*
181 * interact_open() is a callback from the input open routine.
182 */
183
interact_open(struct input_dev * dev)184 static int interact_open(struct input_dev *dev)
185 {
186 struct interact *interact = dev->private;
187 if (!interact->used++)
188 mod_timer(&interact->timer, jiffies + INTERACT_REFRESH_TIME);
189 return 0;
190 }
191
192 /*
193 * interact_close() is a callback from the input close routine.
194 */
195
interact_close(struct input_dev * dev)196 static void interact_close(struct input_dev *dev)
197 {
198 struct interact *interact = dev->private;
199 if (!--interact->used)
200 del_timer(&interact->timer);
201 }
202
203 /*
204 * interact_connect() probes for InterAct joysticks.
205 */
206
interact_connect(struct gameport * gameport,struct gameport_dev * dev)207 static void interact_connect(struct gameport *gameport, struct gameport_dev *dev)
208 {
209 struct interact *interact;
210 __u32 data[3];
211 int i, t;
212
213 if (!(interact = kmalloc(sizeof(struct interact), GFP_KERNEL)))
214 return;
215 memset(interact, 0, sizeof(struct interact));
216
217 gameport->private = interact;
218
219 interact->gameport = gameport;
220 init_timer(&interact->timer);
221 interact->timer.data = (long) interact;
222 interact->timer.function = interact_timer;
223
224 if (gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
225 goto fail1;
226
227 i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
228
229 if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
230 goto fail2;
231 }
232
233 for (i = 0; interact_type[i].length; i++)
234 if (interact_type[i].id == (data[2] >> 16))
235 break;
236
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;
241 }
242
243 interact->type = i;
244 interact->length = interact_type[i].length;
245
246 interact->dev.private = interact;
247 interact->dev.open = interact_open;
248 interact->dev.close = interact_close;
249
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;
255
256 interact->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
257
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;
266 }
267 }
268
269 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
270 set_bit(t, interact->dev.keybit);
271
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);
275
276 return;
277 fail2: gameport_close(gameport);
278 fail1: kfree(interact);
279 }
280
interact_disconnect(struct gameport * gameport)281 static void interact_disconnect(struct gameport *gameport)
282 {
283 struct interact *interact = gameport->private;
284 input_unregister_device(&interact->dev);
285 gameport_close(gameport);
286 kfree(interact);
287 }
288
289 static struct gameport_dev interact_dev = {
290 connect: interact_connect,
291 disconnect: interact_disconnect,
292 };
293
interact_init(void)294 int __init interact_init(void)
295 {
296 gameport_register_device(&interact_dev);
297 return 0;
298 }
299
interact_exit(void)300 void __exit interact_exit(void)
301 {
302 gameport_unregister_device(&interact_dev);
303 }
304
305 module_init(interact_init);
306 module_exit(interact_exit);
307
308 MODULE_LICENSE("GPL");
309