blob: 88c22623a2e8cc01ec461d70f216f09486a25904 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
30 */
31
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/module.h>
35#include <linux/delay.h>
36#include <linux/init.h>
37#include <linux/gameport.h>
38#include <linux/input.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080039#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define DRIVER_DESC "InterAct digital joystick driver"
42
43MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44MODULE_DESCRIPTION(DRIVER_DESC);
45MODULE_LICENSE("GPL");
46
47#define INTERACT_MAX_START 600 /* 400 us */
48#define INTERACT_MAX_STROBE 60 /* 40 us */
49#define INTERACT_MAX_LENGTH 32 /* 32 bits */
50
51#define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
52#define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
53
54struct interact {
55 struct gameport *gameport;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050056 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int bads;
58 int reads;
59 unsigned char type;
60 unsigned char length;
61 char phys[32];
62};
63
64static short interact_abs_hhfx[] =
65 { ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
66static short interact_abs_pp8d[] =
67 { ABS_X, ABS_Y, -1 };
68
69static short interact_btn_hhfx[] =
70 { 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 };
71static short interact_btn_pp8d[] =
72 { BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
73
74struct interact_type {
75 int id;
76 short *abs;
77 short *btn;
78 char *name;
79 unsigned char length;
80 unsigned char b8;
81};
82
83static struct interact_type interact_type[] = {
84 { 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX", 32, 4 },
85 { 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
86 { 0 }};
87
88/*
89 * interact_read_packet() reads and InterAct joystick data.
90 */
91
92static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
93{
94 unsigned long flags;
95 unsigned char u, v;
96 unsigned int t, s;
97 int i;
98
99 i = 0;
100 data[0] = data[1] = data[2] = 0;
101 t = gameport_time(gameport, INTERACT_MAX_START);
102 s = gameport_time(gameport, INTERACT_MAX_STROBE);
103
104 local_irq_save(flags);
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 local_irq_restore(flags);
121
122 return i;
123}
124
125/*
126 * interact_poll() reads and analyzes InterAct joystick data.
127 */
128
129static void interact_poll(struct gameport *gameport)
130{
131 struct interact *interact = gameport_get_drvdata(gameport);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500132 struct input_dev *dev = interact->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 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
177 input_sync(dev);
178}
179
180/*
181 * interact_open() is a callback from the input open routine.
182 */
183
184static int interact_open(struct input_dev *dev)
185{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400186 struct interact *interact = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 gameport_start_polling(interact->gameport);
189 return 0;
190}
191
192/*
193 * interact_close() is a callback from the input close routine.
194 */
195
196static void interact_close(struct input_dev *dev)
197{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400198 struct interact *interact = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 gameport_stop_polling(interact->gameport);
201}
202
203/*
204 * interact_connect() probes for InterAct joysticks.
205 */
206
207static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
208{
209 struct interact *interact;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500210 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 __u32 data[3];
212 int i, t;
213 int err;
214
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500215 interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
216 input_dev = input_allocate_device();
217 if (!interact || !input_dev) {
218 err = -ENOMEM;
219 goto fail1;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 interact->gameport = gameport;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500223 interact->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 gameport_set_drvdata(gameport, interact);
226
227 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
228 if (err)
229 goto fail1;
230
231 i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
232
233 if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
234 err = -ENODEV;
235 goto fail2;
236 }
237
238 for (i = 0; interact_type[i].length; i++)
239 if (interact_type[i].id == (data[2] >> 16))
240 break;
241
242 if (!interact_type[i].length) {
243 printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
244 gameport->phys, i, data[0], data[1], data[2]);
245 err = -ENODEV;
246 goto fail2;
247 }
248
249 gameport_set_poll_handler(gameport, interact_poll);
250 gameport_set_poll_interval(gameport, 20);
251
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400252 snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 interact->type = i;
255 interact->length = interact_type[i].length;
256
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500257 input_dev->name = interact_type[i].name;
258 input_dev->phys = interact->phys;
259 input_dev->id.bustype = BUS_GAMEPORT;
260 input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
261 input_dev->id.product = interact_type[i].id;
262 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400263 input_dev->dev.parent = &gameport->dev;
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400264
265 input_set_drvdata(input_dev, 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
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700270 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
Daniel Mack987a6c02010-08-02 20:15:17 -0700273 if (i < interact_type[interact->type].b8)
274 input_set_abs_params(input_dev, t, 0, 255, 0, 0);
275 else
276 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
279 for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
Daniel Mack987a6c02010-08-02 20:15:17 -0700280 __set_bit(t, input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500282 err = input_register_device(interact->dev);
283 if (err)
284 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 return 0;
287
288fail2: gameport_close(gameport);
289fail1: gameport_set_drvdata(gameport, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500290 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 kfree(interact);
292 return err;
293}
294
295static void interact_disconnect(struct gameport *gameport)
296{
297 struct interact *interact = gameport_get_drvdata(gameport);
298
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500299 input_unregister_device(interact->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 gameport_close(gameport);
301 gameport_set_drvdata(gameport, NULL);
302 kfree(interact);
303}
304
305static struct gameport_driver interact_drv = {
306 .driver = {
307 .name = "interact",
308 },
309 .description = DRIVER_DESC,
310 .connect = interact_connect,
311 .disconnect = interact_disconnect,
312};
313
Axel Lin98a84132012-04-03 23:52:27 -0700314module_gameport_driver(interact_drv);