blob: bbfeb9c59b874c763158a1e458ce35e8a4844ebc [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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080041#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define DRIVER_DESC "InterAct digital joystick driver"
44
45MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
46MODULE_DESCRIPTION(DRIVER_DESC);
47MODULE_LICENSE("GPL");
48
49#define INTERACT_MAX_START 600 /* 400 us */
50#define INTERACT_MAX_STROBE 60 /* 40 us */
51#define INTERACT_MAX_LENGTH 32 /* 32 bits */
52
53#define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
54#define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
55
56struct interact {
57 struct gameport *gameport;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050058 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int bads;
60 int reads;
61 unsigned char type;
62 unsigned char length;
63 char phys[32];
64};
65
66static short interact_abs_hhfx[] =
67 { ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
68static short interact_abs_pp8d[] =
69 { ABS_X, ABS_Y, -1 };
70
71static short interact_btn_hhfx[] =
72 { 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 };
73static short interact_btn_pp8d[] =
74 { BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
75
76struct interact_type {
77 int id;
78 short *abs;
79 short *btn;
80 char *name;
81 unsigned char length;
82 unsigned char b8;
83};
84
85static struct interact_type interact_type[] = {
86 { 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX", 32, 4 },
87 { 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
88 { 0 }};
89
90/*
91 * interact_read_packet() reads and InterAct joystick data.
92 */
93
94static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
95{
96 unsigned long flags;
97 unsigned char u, v;
98 unsigned int t, s;
99 int i;
100
101 i = 0;
102 data[0] = data[1] = data[2] = 0;
103 t = gameport_time(gameport, INTERACT_MAX_START);
104 s = gameport_time(gameport, INTERACT_MAX_STROBE);
105
106 local_irq_save(flags);
107 gameport_trigger(gameport);
108 v = gameport_read(gameport);
109
110 while (t > 0 && i < length) {
111 t--;
112 u = v; v = gameport_read(gameport);
113 if (v & ~u & 0x40) {
114 data[0] = (data[0] << 1) | ((v >> 4) & 1);
115 data[1] = (data[1] << 1) | ((v >> 5) & 1);
116 data[2] = (data[2] << 1) | ((v >> 7) & 1);
117 i++;
118 t = s;
119 }
120 }
121
122 local_irq_restore(flags);
123
124 return i;
125}
126
127/*
128 * interact_poll() reads and analyzes InterAct joystick data.
129 */
130
131static void interact_poll(struct gameport *gameport)
132{
133 struct interact *interact = gameport_get_drvdata(gameport);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500134 struct input_dev *dev = interact->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 u32 data[3];
136 int i;
137
138 interact->reads++;
139
140 if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
141 interact->bads++;
142 } else {
143
144 for (i = 0; i < 3; i++)
145 data[i] <<= INTERACT_MAX_LENGTH - interact->length;
146
147 switch (interact->type) {
148
149 case INTERACT_TYPE_HHFX:
150
151 for (i = 0; i < 4; i++)
152 input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
153
154 for (i = 0; i < 2; i++)
155 input_report_abs(dev, ABS_HAT0Y - i,
156 ((data[1] >> ((i << 1) + 17)) & 1) - ((data[1] >> ((i << 1) + 16)) & 1));
157
158 for (i = 0; i < 8; i++)
159 input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
160
161 for (i = 0; i < 4; i++)
162 input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
163
164 break;
165
166 case INTERACT_TYPE_PP8D:
167
168 for (i = 0; i < 2; i++)
169 input_report_abs(dev, interact_abs_pp8d[i],
170 ((data[0] >> ((i << 1) + 20)) & 1) - ((data[0] >> ((i << 1) + 21)) & 1));
171
172 for (i = 0; i < 8; i++)
173 input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
174
175 break;
176 }
177 }
178
179 input_sync(dev);
180}
181
182/*
183 * interact_open() is a callback from the input open routine.
184 */
185
186static int interact_open(struct input_dev *dev)
187{
188 struct interact *interact = dev->private;
189
190 gameport_start_polling(interact->gameport);
191 return 0;
192}
193
194/*
195 * interact_close() is a callback from the input close routine.
196 */
197
198static void interact_close(struct input_dev *dev)
199{
200 struct interact *interact = dev->private;
201
202 gameport_stop_polling(interact->gameport);
203}
204
205/*
206 * interact_connect() probes for InterAct joysticks.
207 */
208
209static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
210{
211 struct interact *interact;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500212 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 __u32 data[3];
214 int i, t;
215 int err;
216
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500217 interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
218 input_dev = input_allocate_device();
219 if (!interact || !input_dev) {
220 err = -ENOMEM;
221 goto fail1;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 interact->gameport = gameport;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500225 interact->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 gameport_set_drvdata(gameport, interact);
228
229 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
230 if (err)
231 goto fail1;
232
233 i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
234
235 if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
236 err = -ENODEV;
237 goto fail2;
238 }
239
240 for (i = 0; interact_type[i].length; i++)
241 if (interact_type[i].id == (data[2] >> 16))
242 break;
243
244 if (!interact_type[i].length) {
245 printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
246 gameport->phys, i, data[0], data[1], data[2]);
247 err = -ENODEV;
248 goto fail2;
249 }
250
251 gameport_set_poll_handler(gameport, interact_poll);
252 gameport_set_poll_interval(gameport, 20);
253
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400254 snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 interact->type = i;
257 interact->length = interact_type[i].length;
258
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500259 input_dev->name = interact_type[i].name;
260 input_dev->phys = interact->phys;
261 input_dev->id.bustype = BUS_GAMEPORT;
262 input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
263 input_dev->id.product = interact_type[i].id;
264 input_dev->id.version = 0x0100;
265 input_dev->private = interact;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500267 input_dev->open = interact_open;
268 input_dev->close = interact_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500270 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500273 set_bit(t, input_dev->absbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (i < interact_type[interact->type].b8) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500275 input_dev->absmin[t] = 0;
276 input_dev->absmax[t] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 } else {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500278 input_dev->absmin[t] = -1;
279 input_dev->absmax[t] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 }
282
283 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500284 set_bit(t, input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500286 input_register_device(interact->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 return 0;
289
290fail2: gameport_close(gameport);
291fail1: gameport_set_drvdata(gameport, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500292 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 kfree(interact);
294 return err;
295}
296
297static void interact_disconnect(struct gameport *gameport)
298{
299 struct interact *interact = gameport_get_drvdata(gameport);
300
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500301 input_unregister_device(interact->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 gameport_close(gameport);
303 gameport_set_drvdata(gameport, NULL);
304 kfree(interact);
305}
306
307static struct gameport_driver interact_drv = {
308 .driver = {
309 .name = "interact",
310 },
311 .description = DRIVER_DESC,
312 .connect = interact_connect,
313 .disconnect = interact_disconnect,
314};
315
316static int __init interact_init(void)
317{
318 gameport_register_driver(&interact_drv);
319 return 0;
320}
321
322static void __exit interact_exit(void)
323{
324 gameport_unregister_driver(&interact_drv);
325}
326
327module_init(interact_init);
328module_exit(interact_exit);