blob: 9d3f8c38cb097391403f44846781a75d4c68d845 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * $Id: interact.c,v 1.16 2002/01/22 20:28:25 vojtech Exp $
3 *
4 * Copyright (c) 2001 Vojtech Pavlik
5 *
6 * Based on the work of:
7 * Toby Deshane
8 */
9
10/*
11 * InterAct digital gamepad/joystick driver for Linux
12 */
13
14/*
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.
19 *
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.
24 *
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
28 *
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
32 */
33
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
42#define DRIVER_DESC "InterAct digital joystick driver"
43
44MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45MODULE_DESCRIPTION(DRIVER_DESC);
46MODULE_LICENSE("GPL");
47
48#define INTERACT_MAX_START 600 /* 400 us */
49#define INTERACT_MAX_STROBE 60 /* 40 us */
50#define INTERACT_MAX_LENGTH 32 /* 32 bits */
51
52#define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
53#define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
54
55struct interact {
56 struct gameport *gameport;
57 struct input_dev dev;
58 int bads;
59 int reads;
60 unsigned char type;
61 unsigned char length;
62 char phys[32];
63};
64
65static short interact_abs_hhfx[] =
66 { ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
67static short interact_abs_pp8d[] =
68 { ABS_X, ABS_Y, -1 };
69
70static short interact_btn_hhfx[] =
71 { 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 };
72static short interact_btn_pp8d[] =
73 { BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
74
75struct interact_type {
76 int id;
77 short *abs;
78 short *btn;
79 char *name;
80 unsigned char length;
81 unsigned char b8;
82};
83
84static struct interact_type interact_type[] = {
85 { 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX", 32, 4 },
86 { 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
87 { 0 }};
88
89/*
90 * interact_read_packet() reads and InterAct joystick data.
91 */
92
93static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
94{
95 unsigned long flags;
96 unsigned char u, v;
97 unsigned int t, s;
98 int i;
99
100 i = 0;
101 data[0] = data[1] = data[2] = 0;
102 t = gameport_time(gameport, INTERACT_MAX_START);
103 s = gameport_time(gameport, INTERACT_MAX_STROBE);
104
105 local_irq_save(flags);
106 gameport_trigger(gameport);
107 v = gameport_read(gameport);
108
109 while (t > 0 && i < length) {
110 t--;
111 u = v; v = gameport_read(gameport);
112 if (v & ~u & 0x40) {
113 data[0] = (data[0] << 1) | ((v >> 4) & 1);
114 data[1] = (data[1] << 1) | ((v >> 5) & 1);
115 data[2] = (data[2] << 1) | ((v >> 7) & 1);
116 i++;
117 t = s;
118 }
119 }
120
121 local_irq_restore(flags);
122
123 return i;
124}
125
126/*
127 * interact_poll() reads and analyzes InterAct joystick data.
128 */
129
130static void interact_poll(struct gameport *gameport)
131{
132 struct interact *interact = gameport_get_drvdata(gameport);
133 struct input_dev *dev = &interact->dev;
134 u32 data[3];
135 int i;
136
137 interact->reads++;
138
139 if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
140 interact->bads++;
141 } else {
142
143 for (i = 0; i < 3; i++)
144 data[i] <<= INTERACT_MAX_LENGTH - interact->length;
145
146 switch (interact->type) {
147
148 case INTERACT_TYPE_HHFX:
149
150 for (i = 0; i < 4; i++)
151 input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
152
153 for (i = 0; i < 2; i++)
154 input_report_abs(dev, ABS_HAT0Y - i,
155 ((data[1] >> ((i << 1) + 17)) & 1) - ((data[1] >> ((i << 1) + 16)) & 1));
156
157 for (i = 0; i < 8; i++)
158 input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
159
160 for (i = 0; i < 4; i++)
161 input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
162
163 break;
164
165 case INTERACT_TYPE_PP8D:
166
167 for (i = 0; i < 2; i++)
168 input_report_abs(dev, interact_abs_pp8d[i],
169 ((data[0] >> ((i << 1) + 20)) & 1) - ((data[0] >> ((i << 1) + 21)) & 1));
170
171 for (i = 0; i < 8; i++)
172 input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
173
174 break;
175 }
176 }
177
178 input_sync(dev);
179}
180
181/*
182 * interact_open() is a callback from the input open routine.
183 */
184
185static int interact_open(struct input_dev *dev)
186{
187 struct interact *interact = dev->private;
188
189 gameport_start_polling(interact->gameport);
190 return 0;
191}
192
193/*
194 * interact_close() is a callback from the input close routine.
195 */
196
197static void interact_close(struct input_dev *dev)
198{
199 struct interact *interact = dev->private;
200
201 gameport_stop_polling(interact->gameport);
202}
203
204/*
205 * interact_connect() probes for InterAct joysticks.
206 */
207
208static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
209{
210 struct interact *interact;
211 __u32 data[3];
212 int i, t;
213 int err;
214
215 if (!(interact = kcalloc(1, sizeof(struct interact), GFP_KERNEL)))
216 return -ENOMEM;
217
218 interact->gameport = gameport;
219
220 gameport_set_drvdata(gameport, interact);
221
222 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
223 if (err)
224 goto fail1;
225
226 i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
227
228 if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
229 err = -ENODEV;
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 %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
239 gameport->phys, i, data[0], data[1], data[2]);
240 err = -ENODEV;
241 goto fail2;
242 }
243
244 gameport_set_poll_handler(gameport, interact_poll);
245 gameport_set_poll_interval(gameport, 20);
246
247 sprintf(interact->phys, "%s/input0", gameport->phys);
248
249 interact->type = i;
250 interact->length = interact_type[i].length;
251
252 interact->dev.private = interact;
253 interact->dev.open = interact_open;
254 interact->dev.close = interact_close;
255
256 interact->dev.name = interact_type[i].name;
257 interact->dev.phys = interact->phys;
258 interact->dev.id.bustype = BUS_GAMEPORT;
259 interact->dev.id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
260 interact->dev.id.product = interact_type[i].id;
261 interact->dev.id.version = 0x0100;
262
263 interact->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
264
265 for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
266 set_bit(t, interact->dev.absbit);
267 if (i < interact_type[interact->type].b8) {
268 interact->dev.absmin[t] = 0;
269 interact->dev.absmax[t] = 255;
270 } else {
271 interact->dev.absmin[t] = -1;
272 interact->dev.absmax[t] = 1;
273 }
274 }
275
276 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
277 set_bit(t, interact->dev.keybit);
278
279 input_register_device(&interact->dev);
280 printk(KERN_INFO "input: %s on %s\n",
281 interact_type[interact->type].name, gameport->phys);
282
283 return 0;
284
285fail2: gameport_close(gameport);
286fail1: gameport_set_drvdata(gameport, NULL);
287 kfree(interact);
288 return err;
289}
290
291static void interact_disconnect(struct gameport *gameport)
292{
293 struct interact *interact = gameport_get_drvdata(gameport);
294
295 input_unregister_device(&interact->dev);
296 gameport_close(gameport);
297 gameport_set_drvdata(gameport, NULL);
298 kfree(interact);
299}
300
301static struct gameport_driver interact_drv = {
302 .driver = {
303 .name = "interact",
304 },
305 .description = DRIVER_DESC,
306 .connect = interact_connect,
307 .disconnect = interact_disconnect,
308};
309
310static int __init interact_init(void)
311{
312 gameport_register_driver(&interact_drv);
313 return 0;
314}
315
316static void __exit interact_exit(void)
317{
318 gameport_unregister_driver(&interact_drv);
319}
320
321module_init(interact_init);
322module_exit(interact_exit);