]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
41 | ||
42 | #define DRIVER_DESC "InterAct digital joystick driver" | |
43 | ||
44 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); | |
45 | MODULE_DESCRIPTION(DRIVER_DESC); | |
46 | MODULE_LICENSE("GPL"); | |
47 | ||
48 | #define INTERACT_MAX_START 600 /* 400 us */ | |
49 | #define INTERACT_MAX_STROBE 60 /* 40 us */ | |
50 | #define INTERACT_MAX_LENGTH 32 /* 32 bits */ | |
51 | ||
52 | #define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */ | |
53 | #define INTERACT_TYPE_PP8D 1 /* ProPad 8 */ | |
54 | ||
55 | struct interact { | |
56 | struct gameport *gameport; | |
17dd3f0f | 57 | struct input_dev *dev; |
1da177e4 LT |
58 | int bads; |
59 | int reads; | |
60 | unsigned char type; | |
61 | unsigned char length; | |
62 | char phys[32]; | |
63 | }; | |
64 | ||
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[] = | |
68 | { ABS_X, ABS_Y, -1 }; | |
69 | ||
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 }; | |
74 | ||
75 | struct interact_type { | |
76 | int id; | |
77 | short *abs; | |
78 | short *btn; | |
79 | char *name; | |
80 | unsigned char length; | |
81 | unsigned char b8; | |
82 | }; | |
83 | ||
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 }, | |
87 | { 0 }}; | |
88 | ||
89 | /* | |
90 | * interact_read_packet() reads and InterAct joystick data. | |
91 | */ | |
92 | ||
93 | static int interact_read_packet(struct gameport *gameport, int length, u32 *data) | |
94 | { | |
95 | unsigned long flags; | |
96 | unsigned char u, v; | |
97 | unsigned int t, s; | |
98 | int i; | |
99 | ||
100 | i = 0; | |
101 | data[0] = data[1] = data[2] = 0; | |
102 | t = gameport_time(gameport, INTERACT_MAX_START); | |
103 | s = gameport_time(gameport, INTERACT_MAX_STROBE); | |
104 | ||
105 | local_irq_save(flags); | |
106 | gameport_trigger(gameport); | |
107 | v = gameport_read(gameport); | |
108 | ||
109 | while (t > 0 && i < length) { | |
110 | t--; | |
111 | u = v; v = gameport_read(gameport); | |
112 | if (v & ~u & 0x40) { | |
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); | |
116 | i++; | |
117 | t = s; | |
118 | } | |
119 | } | |
120 | ||
121 | local_irq_restore(flags); | |
122 | ||
123 | return i; | |
124 | } | |
125 | ||
126 | /* | |
127 | * interact_poll() reads and analyzes InterAct joystick data. | |
128 | */ | |
129 | ||
130 | static void interact_poll(struct gameport *gameport) | |
131 | { | |
132 | struct interact *interact = gameport_get_drvdata(gameport); | |
17dd3f0f | 133 | struct input_dev *dev = interact->dev; |
1da177e4 LT |
134 | u32 data[3]; |
135 | int i; | |
136 | ||
137 | interact->reads++; | |
138 | ||
139 | if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) { | |
140 | interact->bads++; | |
141 | } else { | |
142 | ||
143 | for (i = 0; i < 3; i++) | |
144 | data[i] <<= INTERACT_MAX_LENGTH - interact->length; | |
145 | ||
146 | switch (interact->type) { | |
147 | ||
148 | case INTERACT_TYPE_HHFX: | |
149 | ||
150 | for (i = 0; i < 4; i++) | |
151 | input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff); | |
152 | ||
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)); | |
156 | ||
157 | for (i = 0; i < 8; i++) | |
158 | input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1); | |
159 | ||
160 | for (i = 0; i < 4; i++) | |
161 | input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1); | |
162 | ||
163 | break; | |
164 | ||
165 | case INTERACT_TYPE_PP8D: | |
166 | ||
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)); | |
170 | ||
171 | for (i = 0; i < 8; i++) | |
172 | input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1); | |
173 | ||
174 | break; | |
175 | } | |
176 | } | |
177 | ||
178 | input_sync(dev); | |
179 | } | |
180 | ||
181 | /* | |
182 | * interact_open() is a callback from the input open routine. | |
183 | */ | |
184 | ||
185 | static int interact_open(struct input_dev *dev) | |
186 | { | |
187 | struct interact *interact = dev->private; | |
188 | ||
189 | gameport_start_polling(interact->gameport); | |
190 | return 0; | |
191 | } | |
192 | ||
193 | /* | |
194 | * interact_close() is a callback from the input close routine. | |
195 | */ | |
196 | ||
197 | static void interact_close(struct input_dev *dev) | |
198 | { | |
199 | struct interact *interact = dev->private; | |
200 | ||
201 | gameport_stop_polling(interact->gameport); | |
202 | } | |
203 | ||
204 | /* | |
205 | * interact_connect() probes for InterAct joysticks. | |
206 | */ | |
207 | ||
208 | static int interact_connect(struct gameport *gameport, struct gameport_driver *drv) | |
209 | { | |
210 | struct interact *interact; | |
17dd3f0f | 211 | struct input_dev *input_dev; |
1da177e4 LT |
212 | __u32 data[3]; |
213 | int i, t; | |
214 | int err; | |
215 | ||
17dd3f0f DT |
216 | interact = kzalloc(sizeof(struct interact), GFP_KERNEL); |
217 | input_dev = input_allocate_device(); | |
218 | if (!interact || !input_dev) { | |
219 | err = -ENOMEM; | |
220 | goto fail1; | |
221 | } | |
1da177e4 LT |
222 | |
223 | interact->gameport = gameport; | |
17dd3f0f | 224 | interact->dev = input_dev; |
1da177e4 LT |
225 | |
226 | gameport_set_drvdata(gameport, interact); | |
227 | ||
228 | err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW); | |
229 | if (err) | |
230 | goto fail1; | |
231 | ||
232 | i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data); | |
233 | ||
234 | if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) { | |
235 | err = -ENODEV; | |
236 | goto fail2; | |
237 | } | |
238 | ||
239 | for (i = 0; interact_type[i].length; i++) | |
240 | if (interact_type[i].id == (data[2] >> 16)) | |
241 | break; | |
242 | ||
243 | if (!interact_type[i].length) { | |
244 | printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n", | |
245 | gameport->phys, i, data[0], data[1], data[2]); | |
246 | err = -ENODEV; | |
247 | goto fail2; | |
248 | } | |
249 | ||
250 | gameport_set_poll_handler(gameport, interact_poll); | |
251 | gameport_set_poll_interval(gameport, 20); | |
252 | ||
253 | sprintf(interact->phys, "%s/input0", gameport->phys); | |
254 | ||
255 | interact->type = i; | |
256 | interact->length = interact_type[i].length; | |
257 | ||
17dd3f0f DT |
258 | input_dev->name = interact_type[i].name; |
259 | input_dev->phys = interact->phys; | |
260 | input_dev->id.bustype = BUS_GAMEPORT; | |
261 | input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT; | |
262 | input_dev->id.product = interact_type[i].id; | |
263 | input_dev->id.version = 0x0100; | |
264 | input_dev->private = interact; | |
1da177e4 | 265 | |
17dd3f0f DT |
266 | input_dev->open = interact_open; |
267 | input_dev->close = interact_close; | |
1da177e4 | 268 | |
17dd3f0f | 269 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
1da177e4 LT |
270 | |
271 | for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) { | |
17dd3f0f | 272 | set_bit(t, input_dev->absbit); |
1da177e4 | 273 | if (i < interact_type[interact->type].b8) { |
17dd3f0f DT |
274 | input_dev->absmin[t] = 0; |
275 | input_dev->absmax[t] = 255; | |
1da177e4 | 276 | } else { |
17dd3f0f DT |
277 | input_dev->absmin[t] = -1; |
278 | input_dev->absmax[t] = 1; | |
1da177e4 LT |
279 | } |
280 | } | |
281 | ||
282 | for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++) | |
17dd3f0f | 283 | set_bit(t, input_dev->keybit); |
1da177e4 | 284 | |
17dd3f0f | 285 | input_register_device(interact->dev); |
1da177e4 LT |
286 | |
287 | return 0; | |
288 | ||
289 | fail2: gameport_close(gameport); | |
290 | fail1: gameport_set_drvdata(gameport, NULL); | |
17dd3f0f | 291 | input_free_device(input_dev); |
1da177e4 LT |
292 | kfree(interact); |
293 | return err; | |
294 | } | |
295 | ||
296 | static void interact_disconnect(struct gameport *gameport) | |
297 | { | |
298 | struct interact *interact = gameport_get_drvdata(gameport); | |
299 | ||
17dd3f0f | 300 | input_unregister_device(interact->dev); |
1da177e4 LT |
301 | gameport_close(gameport); |
302 | gameport_set_drvdata(gameport, NULL); | |
303 | kfree(interact); | |
304 | } | |
305 | ||
306 | static struct gameport_driver interact_drv = { | |
307 | .driver = { | |
308 | .name = "interact", | |
309 | }, | |
310 | .description = DRIVER_DESC, | |
311 | .connect = interact_connect, | |
312 | .disconnect = interact_disconnect, | |
313 | }; | |
314 | ||
315 | static int __init interact_init(void) | |
316 | { | |
317 | gameport_register_driver(&interact_drv); | |
318 | return 0; | |
319 | } | |
320 | ||
321 | static void __exit interact_exit(void) | |
322 | { | |
323 | gameport_unregister_driver(&interact_drv); | |
324 | } | |
325 | ||
326 | module_init(interact_init); | |
327 | module_exit(interact_exit); |