2 * $Id: interact.c,v 1.8 2000/05/29 11:19:51 vojtech Exp $
4 * Copyright (c) 2000 Vojtech Pavlik
6 * Based on the work of:
13 * InterAct digital gamepad/joystick driver for Linux
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.
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.
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
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
36 #include <linux/kernel.h>
37 #include <linux/malloc.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>
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 */
49 #define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
50 #define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
53 struct gameport
*gameport
;
55 struct timer_list timer
;
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
[] =
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 };
73 struct interact_type
{
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 },
88 * interact_read_packet() reads and InterAct joystick data.
91 static int interact_read_packet(struct gameport
*gameport
, int length
, u32
*data
)
99 data
[0] = data
[1] = data
[2] = 0;
100 t
= gameport_time(gameport
, INTERACT_MAX_START
);
101 s
= gameport_time(gameport
, INTERACT_MAX_STROBE
);
105 gameport_trigger(gameport
);
106 v
= gameport_read(gameport
);
108 while (t
> 0 && i
< length
) {
110 u
= v
; v
= gameport_read(gameport
);
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);
120 __restore_flags(flags
);
126 * interact_timer() reads and analyzes InterAct joystick data.
129 static void interact_timer(unsigned long private)
131 struct interact
*interact
= (struct interact
*) private;
132 struct input_dev
*dev
= &interact
->dev
;
138 if (interact_read_packet(interact
->gameport
, interact
->length
, data
) < interact
->length
) {
142 for (i
= 0; i
< 3; i
++)
143 data
[i
] <<= INTERACT_MAX_LENGTH
- interact
->length
;
145 switch (interact
->type
) {
147 case INTERACT_TYPE_HHFX
:
149 for (i
= 0; i
< 4; i
++)
150 input_report_abs(dev
, interact_abs_hhfx
[i
], (data
[i
& 1] >> ((i
>> 1) << 3)) & 0xff);
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));
156 for (i
= 0; i
< 8; i
++)
157 input_report_key(dev
, interact_btn_hhfx
[i
], (data
[0] >> (i
+ 16)) & 1);
159 for (i
= 0; i
< 4; i
++)
160 input_report_key(dev
, interact_btn_hhfx
[i
+ 8], (data
[1] >> (i
+ 20)) & 1);
164 case INTERACT_TYPE_PP8D
:
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));
170 for (i
= 0; i
< 8; i
++)
171 input_report_key(dev
, interact_btn_pp8d
[i
], (data
[1] >> (i
+ 16)) & 1);
176 mod_timer(&interact
->timer
, jiffies
+ INTERACT_REFRESH_TIME
);
181 * interact_open() is a callback from the input open routine.
184 static int interact_open(struct input_dev
*dev
)
186 struct interact
*interact
= dev
->private;
187 if (!interact
->used
++)
188 mod_timer(&interact
->timer
, jiffies
+ INTERACT_REFRESH_TIME
);
193 * interact_close() is a callback from the input close routine.
196 static void interact_close(struct input_dev
*dev
)
198 struct interact
*interact
= dev
->private;
199 if (!--interact
->used
)
200 del_timer(&interact
->timer
);
204 * interact_connect() probes for InterAct joysticks.
207 static void interact_connect(struct gameport
*gameport
, struct gameport_dev
*dev
)
209 struct interact
*interact
;
213 if (!(interact
= kmalloc(sizeof(struct interact
), GFP_KERNEL
)))
215 memset(interact
, 0, sizeof(struct interact
));
217 gameport
->private = interact
;
219 interact
->gameport
= gameport
;
220 init_timer(&interact
->timer
);
221 interact
->timer
.data
= (long) interact
;
222 interact
->timer
.function
= interact_timer
;
224 if (gameport_open(gameport
, dev
, GAMEPORT_MODE_RAW
))
227 i
= interact_read_packet(gameport
, INTERACT_MAX_LENGTH
* 2, data
);
229 if (i
!= 32 || (data
[0] >> 24) != 0x0c || (data
[1] >> 24) != 0x02) {
233 for (i
= 0; interact_type
[i
].length
; i
++)
234 if (interact_type
[i
].id
== (data
[2] >> 16))
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]);
244 interact
->length
= interact_type
[i
].length
;
246 interact
->dev
.private = interact
;
247 interact
->dev
.open
= interact_open
;
248 interact
->dev
.close
= interact_close
;
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;
256 interact
->dev
.evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
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;
264 interact
->dev
.absmin
[t
] = -1;
265 interact
->dev
.absmax
[t
] = 1;
269 for (i
= 0; (t
= interact_type
[interact
->type
].btn
[i
]) >= 0; i
++)
270 set_bit(t
, interact
->dev
.keybit
);
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
);
277 fail2
: gameport_close(gameport
);
278 fail1
: kfree(interact
);
281 static void interact_disconnect(struct gameport
*gameport
)
283 struct interact
*interact
= gameport
->private;
284 input_unregister_device(&interact
->dev
);
285 gameport_close(gameport
);
289 static struct gameport_dev interact_dev
= {
290 connect
: interact_connect
,
291 disconnect
: interact_disconnect
,
294 int __init
interact_init(void)
296 gameport_register_device(&interact_dev
);
300 void __exit
interact_exit(void)
302 gameport_unregister_device(&interact_dev
);
305 module_init(interact_init
);
306 module_exit(interact_exit
);