2 * $Id: interact.c,v 1.16 2002/01/22 20:28:25 vojtech Exp $
4 * Copyright (c) 2001 Vojtech Pavlik
6 * Based on the work of:
11 * InterAct digital gamepad/joystick driver for Linux
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.
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.
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
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
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>
42 #define DRIVER_DESC "InterAct digital joystick driver"
44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45 MODULE_DESCRIPTION(DRIVER_DESC
);
46 MODULE_LICENSE("GPL");
48 #define INTERACT_MAX_START 600 /* 400 us */
49 #define INTERACT_MAX_STROBE 60 /* 40 us */
50 #define INTERACT_MAX_LENGTH 32 /* 32 bits */
52 #define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
53 #define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
56 struct gameport
*gameport
;
65 static short interact_abs_hhfx
[] =
66 { ABS_RX
, ABS_RY
, ABS_X
, ABS_Y
, ABS_HAT0X
, ABS_HAT0Y
, -1 };
67 static short interact_abs_pp8d
[] =
70 static 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 };
72 static short interact_btn_pp8d
[] =
73 { BTN_C
, BTN_TL
, BTN_TR
, BTN_A
, BTN_B
, BTN_Y
, BTN_Z
, BTN_X
, -1 };
75 struct interact_type
{
84 static 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 },
90 * interact_read_packet() reads and InterAct joystick data.
93 static int interact_read_packet(struct gameport
*gameport
, int length
, u32
*data
)
101 data
[0] = data
[1] = data
[2] = 0;
102 t
= gameport_time(gameport
, INTERACT_MAX_START
);
103 s
= gameport_time(gameport
, INTERACT_MAX_STROBE
);
105 local_irq_save(flags
);
106 gameport_trigger(gameport
);
107 v
= gameport_read(gameport
);
109 while (t
> 0 && i
< length
) {
111 u
= v
; v
= gameport_read(gameport
);
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);
121 local_irq_restore(flags
);
127 * interact_poll() reads and analyzes InterAct joystick data.
130 static void interact_poll(struct gameport
*gameport
)
132 struct interact
*interact
= gameport_get_drvdata(gameport
);
133 struct input_dev
*dev
= &interact
->dev
;
139 if (interact_read_packet(interact
->gameport
, interact
->length
, data
) < interact
->length
) {
143 for (i
= 0; i
< 3; i
++)
144 data
[i
] <<= INTERACT_MAX_LENGTH
- interact
->length
;
146 switch (interact
->type
) {
148 case INTERACT_TYPE_HHFX
:
150 for (i
= 0; i
< 4; i
++)
151 input_report_abs(dev
, interact_abs_hhfx
[i
], (data
[i
& 1] >> ((i
>> 1) << 3)) & 0xff);
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));
157 for (i
= 0; i
< 8; i
++)
158 input_report_key(dev
, interact_btn_hhfx
[i
], (data
[0] >> (i
+ 16)) & 1);
160 for (i
= 0; i
< 4; i
++)
161 input_report_key(dev
, interact_btn_hhfx
[i
+ 8], (data
[1] >> (i
+ 20)) & 1);
165 case INTERACT_TYPE_PP8D
:
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));
171 for (i
= 0; i
< 8; i
++)
172 input_report_key(dev
, interact_btn_pp8d
[i
], (data
[1] >> (i
+ 16)) & 1);
182 * interact_open() is a callback from the input open routine.
185 static int interact_open(struct input_dev
*dev
)
187 struct interact
*interact
= dev
->private;
189 gameport_start_polling(interact
->gameport
);
194 * interact_close() is a callback from the input close routine.
197 static void interact_close(struct input_dev
*dev
)
199 struct interact
*interact
= dev
->private;
201 gameport_stop_polling(interact
->gameport
);
205 * interact_connect() probes for InterAct joysticks.
208 static int interact_connect(struct gameport
*gameport
, struct gameport_driver
*drv
)
210 struct interact
*interact
;
215 if (!(interact
= kcalloc(1, sizeof(struct interact
), GFP_KERNEL
)))
218 interact
->gameport
= gameport
;
220 gameport_set_drvdata(gameport
, interact
);
222 err
= gameport_open(gameport
, drv
, GAMEPORT_MODE_RAW
);
226 i
= interact_read_packet(gameport
, INTERACT_MAX_LENGTH
* 2, data
);
228 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 %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
239 gameport
->phys
, i
, data
[0], data
[1], data
[2]);
244 gameport_set_poll_handler(gameport
, interact_poll
);
245 gameport_set_poll_interval(gameport
, 20);
247 sprintf(interact
->phys
, "%s/input0", gameport
->phys
);
250 interact
->length
= interact_type
[i
].length
;
252 interact
->dev
.private = interact
;
253 interact
->dev
.open
= interact_open
;
254 interact
->dev
.close
= interact_close
;
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;
263 interact
->dev
.evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
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;
271 interact
->dev
.absmin
[t
] = -1;
272 interact
->dev
.absmax
[t
] = 1;
276 for (i
= 0; (t
= interact_type
[interact
->type
].btn
[i
]) >= 0; i
++)
277 set_bit(t
, interact
->dev
.keybit
);
279 input_register_device(&interact
->dev
);
280 printk(KERN_INFO
"input: %s on %s\n",
281 interact_type
[interact
->type
].name
, gameport
->phys
);
285 fail2
: gameport_close(gameport
);
286 fail1
: gameport_set_drvdata(gameport
, NULL
);
291 static void interact_disconnect(struct gameport
*gameport
)
293 struct interact
*interact
= gameport_get_drvdata(gameport
);
295 input_unregister_device(&interact
->dev
);
296 gameport_close(gameport
);
297 gameport_set_drvdata(gameport
, NULL
);
301 static struct gameport_driver interact_drv
= {
305 .description
= DRIVER_DESC
,
306 .connect
= interact_connect
,
307 .disconnect
= interact_disconnect
,
310 static int __init
interact_init(void)
312 gameport_register_driver(&interact_drv
);
316 static void __exit
interact_exit(void)
318 gameport_unregister_driver(&interact_drv
);
321 module_init(interact_init
);
322 module_exit(interact_exit
);