1/*
2 * Copyright (c) 2001 Vojtech Pavlik
3 *
4 * Based on the work of:
5 * Toby Deshane
6 */
7
8/*
9 * InterAct digital gamepad/joystick driver for Linux
10 */
11
12/*
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.
17 *
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.
22 *
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
26 */
27
28#include <linux/kernel.h>
29#include <linux/slab.h>
30#include <linux/module.h>
31#include <linux/delay.h>
32#include <linux/gameport.h>
33#include <linux/input.h>
34#include <linux/jiffies.h>
35
36#define DRIVER_DESC "InterAct digital joystick driver"
37
38MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39MODULE_DESCRIPTION(DRIVER_DESC);
40MODULE_LICENSE("GPL");
41
42#define INTERACT_MAX_START 600 /* 400 us */
43#define INTERACT_MAX_STROBE 60 /* 40 us */
44#define INTERACT_MAX_LENGTH 32 /* 32 bits */
45
46#define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
47#define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
48
49struct interact {
50 struct gameport *gameport;
51 struct input_dev *dev;
52 int bads;
53 int reads;
54 unsigned char type;
55 unsigned char length;
56 char phys[32];
57};
58
59static short interact_abs_hhfx[] =
60 { ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
61static short interact_abs_pp8d[] =
62 { ABS_X, ABS_Y, -1 };
63
64static short interact_btn_hhfx[] =
65 { 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 };
66static short interact_btn_pp8d[] =
67 { BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
68
69struct interact_type {
70 int id;
71 short *abs;
72 short *btn;
73 char *name;
74 unsigned char length;
75 unsigned char b8;
76};
77
78static struct interact_type interact_type[] = {
79 { 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX", 32, 4 },
80 { 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
81 { 0 }};
82
83/*
84 * interact_read_packet() reads and InterAct joystick data.
85 */
86
87static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
88{
89 unsigned long flags;
90 unsigned char u, v;
91 unsigned int t, s;
92 int i;
93
94 i = 0;
95 data[0] = data[1] = data[2] = 0;
96 t = gameport_time(gameport, INTERACT_MAX_START);
97 s = gameport_time(gameport, INTERACT_MAX_STROBE);
98
99 local_irq_save(flags);
100 gameport_trigger(gameport);
101 v = gameport_read(gameport);
102
103 while (t > 0 && i < length) {
104 t--;
105 u = v; v = gameport_read(gameport);
106 if (v & ~u & 0x40) {
107 data[0] = (data[0] << 1) | ((v >> 4) & 1);
108 data[1] = (data[1] << 1) | ((v >> 5) & 1);
109 data[2] = (data[2] << 1) | ((v >> 7) & 1);
110 i++;
111 t = s;
112 }
113 }
114
115 local_irq_restore(flags);
116
117 return i;
118}
119
120/*
121 * interact_poll() reads and analyzes InterAct joystick data.
122 */
123
124static void interact_poll(struct gameport *gameport)
125{
126 struct interact *interact = gameport_get_drvdata(gameport);
127 struct input_dev *dev = interact->dev;
128 u32 data[3];
129 int i;
130
131 interact->reads++;
132
133 if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
134 interact->bads++;
135 } else {
136
137 for (i = 0; i < 3; i++)
138 data[i] <<= INTERACT_MAX_LENGTH - interact->length;
139
140 switch (interact->type) {
141
142 case INTERACT_TYPE_HHFX:
143
144 for (i = 0; i < 4; i++)
145 input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
146
147 for (i = 0; i < 2; i++)
148 input_report_abs(dev, ABS_HAT0Y - i,
149 ((data[1] >> ((i << 1) + 17)) & 1) - ((data[1] >> ((i << 1) + 16)) & 1));
150
151 for (i = 0; i < 8; i++)
152 input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
153
154 for (i = 0; i < 4; i++)
155 input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
156
157 break;
158
159 case INTERACT_TYPE_PP8D:
160
161 for (i = 0; i < 2; i++)
162 input_report_abs(dev, interact_abs_pp8d[i],
163 ((data[0] >> ((i << 1) + 20)) & 1) - ((data[0] >> ((i << 1) + 21)) & 1));
164
165 for (i = 0; i < 8; i++)
166 input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
167
168 break;
169 }
170 }
171
172 input_sync(dev);
173}
174
175/*
176 * interact_open() is a callback from the input open routine.
177 */
178
179static int interact_open(struct input_dev *dev)
180{
181 struct interact *interact = input_get_drvdata(dev);
182
183 gameport_start_polling(interact->gameport);
184 return 0;
185}
186
187/*
188 * interact_close() is a callback from the input close routine.
189 */
190
191static void interact_close(struct input_dev *dev)
192{
193 struct interact *interact = input_get_drvdata(dev);
194
195 gameport_stop_polling(interact->gameport);
196}
197
198/*
199 * interact_connect() probes for InterAct joysticks.
200 */
201
202static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
203{
204 struct interact *interact;
205 struct input_dev *input_dev;
206 __u32 data[3];
207 int i, t;
208 int err;
209
210 interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
211 input_dev = input_allocate_device();
212 if (!interact || !input_dev) {
213 err = -ENOMEM;
214 goto fail1;
215 }
216
217 interact->gameport = gameport;
218 interact->dev = input_dev;
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 snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
248
249 interact->type = i;
250 interact->length = interact_type[i].length;
251
252 input_dev->name = interact_type[i].name;
253 input_dev->phys = interact->phys;
254 input_dev->id.bustype = BUS_GAMEPORT;
255 input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
256 input_dev->id.product = interact_type[i].id;
257 input_dev->id.version = 0x0100;
258 input_dev->dev.parent = &gameport->dev;
259
260 input_set_drvdata(input_dev, interact);
261
262 input_dev->open = interact_open;
263 input_dev->close = interact_close;
264
265 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
266
267 for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
268 if (i < interact_type[interact->type].b8)
269 input_set_abs_params(input_dev, t, 0, 255, 0, 0);
270 else
271 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
272 }
273
274 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
275 __set_bit(t, input_dev->keybit);
276
277 err = input_register_device(interact->dev);
278 if (err)
279 goto fail2;
280
281 return 0;
282
283fail2: gameport_close(gameport);
284fail1: gameport_set_drvdata(gameport, NULL);
285 input_free_device(input_dev);
286 kfree(interact);
287 return err;
288}
289
290static void interact_disconnect(struct gameport *gameport)
291{
292 struct interact *interact = gameport_get_drvdata(gameport);
293
294 input_unregister_device(interact->dev);
295 gameport_close(gameport);
296 gameport_set_drvdata(gameport, NULL);
297 kfree(interact);
298}
299
300static struct gameport_driver interact_drv = {
301 .driver = {
302 .name = "interact",
303 },
304 .description = DRIVER_DESC,
305 .connect = interact_connect,
306 .disconnect = interact_disconnect,
307};
308
309module_gameport_driver(interact_drv);
310