blob: 499344c72756dcbb6301f4c57101b4c0aa09b54c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * $Id: db9.c,v 1.13 2002/04/07 20:13:37 vojtech Exp $
3 *
4 * Copyright (c) 1999-2001 Vojtech Pavlik
5 *
6 * Based on the work of:
7 * Andree Borrmann Mats Sjövall
8 */
9
10/*
11 * Atari, Amstrad, Commodore, Amiga, Sega, etc. 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/module.h>
36#include <linux/moduleparam.h>
37#include <linux/delay.h>
38#include <linux/init.h>
39#include <linux/parport.h>
40#include <linux/input.h>
41
42MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43MODULE_DESCRIPTION("Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver");
44MODULE_LICENSE("GPL");
45
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050046struct db9_config {
47 int args[2];
48 int nargs;
49};
50
51#define DB9_MAX_PORTS 3
52static struct db9_config db9[DB9_MAX_PORTS] __initdata;
53
54module_param_array_named(dev, db9[0].args, int, &db9[0].nargs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_PARM_DESC(dev, "Describes first attached device (<parport#>,<type>)");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050056module_param_array_named(dev2, db9[1].args, int, &db9[0].nargs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_PARM_DESC(dev2, "Describes second attached device (<parport#>,<type>)");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050058module_param_array_named(dev3, db9[2].args, int, &db9[2].nargs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_PARM_DESC(dev3, "Describes third attached device (<parport#>,<type>)");
60
61__obsolete_setup("db9=");
62__obsolete_setup("db9_2=");
63__obsolete_setup("db9_3=");
64
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050065#define DB9_ARG_PARPORT 0
66#define DB9_ARG_MODE 1
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define DB9_MULTI_STICK 0x01
69#define DB9_MULTI2_STICK 0x02
70#define DB9_GENESIS_PAD 0x03
71#define DB9_GENESIS5_PAD 0x05
72#define DB9_GENESIS6_PAD 0x06
73#define DB9_SATURN_PAD 0x07
74#define DB9_MULTI_0802 0x08
75#define DB9_MULTI_0802_2 0x09
76#define DB9_CD32_PAD 0x0A
77#define DB9_SATURN_DPP 0x0B
78#define DB9_SATURN_DPP_2 0x0C
79#define DB9_MAX_PAD 0x0D
80
81#define DB9_UP 0x01
82#define DB9_DOWN 0x02
83#define DB9_LEFT 0x04
84#define DB9_RIGHT 0x08
85#define DB9_FIRE1 0x10
86#define DB9_FIRE2 0x20
87#define DB9_FIRE3 0x40
88#define DB9_FIRE4 0x80
89
90#define DB9_NORMAL 0x0a
91#define DB9_NOSELECT 0x08
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define DB9_GENESIS6_DELAY 14
94#define DB9_REFRESH_TIME HZ/100
95
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050096#define DB9_MAX_DEVICES 2
97
98struct db9_mode_data {
99 const char *name;
100 const short *buttons;
101 int n_buttons;
102 int n_pads;
103 int n_axis;
104 int bidirectional;
105 int reverse;
106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108struct db9 {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500109 struct input_dev *dev[DB9_MAX_DEVICES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct timer_list timer;
111 struct pardevice *pd;
112 int mode;
113 int used;
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500114 struct semaphore sem;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500115 char phys[DB9_MAX_DEVICES][32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118static struct db9 *db9_base[3];
119
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500120static const short db9_multi_btn[] = { BTN_TRIGGER, BTN_THUMB };
121static const short db9_genesis_btn[] = { BTN_START, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_MODE };
122static const short db9_cd32_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static const short db9_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_RZ, ABS_Z, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500124
125static const struct db9_mode_data db9_modes[] = {
126 { NULL, NULL, 0, 0, 0, 0, 0 },
127 { "Multisystem joystick", db9_multi_btn, 1, 1, 2, 1, 1 },
128 { "Multisystem joystick (2 fire)", db9_multi_btn, 2, 1, 2, 1, 1 },
129 { "Genesis pad", db9_genesis_btn, 4, 1, 2, 1, 1 },
130 { NULL, NULL, 0, 0, 0, 0, 0 },
131 { "Genesis 5 pad", db9_genesis_btn, 6, 1, 2, 1, 1 },
132 { "Genesis 6 pad", db9_genesis_btn, 8, 1, 2, 1, 1 },
133 { "Saturn pad", db9_cd32_btn, 9, 6, 7, 0, 1 },
134 { "Multisystem (0.8.0.2) joystick", db9_multi_btn, 1, 1, 2, 1, 1 },
135 { "Multisystem (0.8.0.2-dual) joystick", db9_multi_btn, 1, 2, 2, 1, 1 },
136 { "Amiga CD-32 pad", db9_cd32_btn, 7, 1, 2, 1, 1 },
137 { "Saturn dpp", db9_cd32_btn, 9, 6, 7, 0, 0 },
138 { "Saturn dpp dual", db9_cd32_btn, 9, 12, 7, 0, 0 },
139};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141/*
142 * Saturn controllers
143 */
144#define DB9_SATURN_DELAY 300
145static const int db9_saturn_byte[] = { 1, 1, 1, 2, 2, 2, 2, 2, 1 };
146static const unsigned char db9_saturn_mask[] = { 0x04, 0x01, 0x02, 0x40, 0x20, 0x10, 0x08, 0x80, 0x08 };
147
148/*
149 * db9_saturn_write_sub() writes 2 bit data.
150 */
151static void db9_saturn_write_sub(struct parport *port, int type, unsigned char data, int powered, int pwr_sub)
152{
153 unsigned char c;
154
155 switch (type) {
156 case 1: /* DPP1 */
157 c = 0x80 | 0x30 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | data;
158 parport_write_data(port, c);
159 break;
160 case 2: /* DPP2 */
161 c = 0x40 | data << 4 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | 0x03;
162 parport_write_data(port, c);
163 break;
164 case 0: /* DB9 */
165 c = ((((data & 2) ? 2 : 0) | ((data & 1) ? 4 : 0)) ^ 0x02) | !powered;
166 parport_write_control(port, c);
167 break;
168 }
169}
170
171/*
172 * gc_saturn_read_sub() reads 4 bit data.
173 */
174static unsigned char db9_saturn_read_sub(struct parport *port, int type)
175{
176 unsigned char data;
177
178 if (type) {
179 /* DPP */
180 data = parport_read_status(port) ^ 0x80;
181 return (data & 0x80 ? 1 : 0) | (data & 0x40 ? 2 : 0)
182 | (data & 0x20 ? 4 : 0) | (data & 0x10 ? 8 : 0);
183 } else {
184 /* DB9 */
185 data = parport_read_data(port) & 0x0f;
186 return (data & 0x8 ? 1 : 0) | (data & 0x4 ? 2 : 0)
187 | (data & 0x2 ? 4 : 0) | (data & 0x1 ? 8 : 0);
188 }
189}
190
191/*
192 * db9_saturn_read_analog() sends clock and reads 8 bit data.
193 */
194static unsigned char db9_saturn_read_analog(struct parport *port, int type, int powered)
195{
196 unsigned char data;
197
198 db9_saturn_write_sub(port, type, 0, powered, 0);
199 udelay(DB9_SATURN_DELAY);
200 data = db9_saturn_read_sub(port, type) << 4;
201 db9_saturn_write_sub(port, type, 2, powered, 0);
202 udelay(DB9_SATURN_DELAY);
203 data |= db9_saturn_read_sub(port, type);
204 return data;
205}
206
207/*
208 * db9_saturn_read_packet() reads whole saturn packet at connector
209 * and returns device identifier code.
210 */
211static unsigned char db9_saturn_read_packet(struct parport *port, unsigned char *data, int type, int powered)
212{
213 int i, j;
214 unsigned char tmp;
215
216 db9_saturn_write_sub(port, type, 3, powered, 0);
217 data[0] = db9_saturn_read_sub(port, type);
218 switch (data[0] & 0x0f) {
219 case 0xf:
220 /* 1111 no pad */
221 return data[0] = 0xff;
222 case 0x4: case 0x4 | 0x8:
223 /* ?100 : digital controller */
224 db9_saturn_write_sub(port, type, 0, powered, 1);
225 data[2] = db9_saturn_read_sub(port, type) << 4;
226 db9_saturn_write_sub(port, type, 2, powered, 1);
227 data[1] = db9_saturn_read_sub(port, type) << 4;
228 db9_saturn_write_sub(port, type, 1, powered, 1);
229 data[1] |= db9_saturn_read_sub(port, type);
230 db9_saturn_write_sub(port, type, 3, powered, 1);
231 /* data[2] |= db9_saturn_read_sub(port, type); */
232 data[2] |= data[0];
233 return data[0] = 0x02;
234 case 0x1:
235 /* 0001 : analog controller or multitap */
236 db9_saturn_write_sub(port, type, 2, powered, 0);
237 udelay(DB9_SATURN_DELAY);
238 data[0] = db9_saturn_read_analog(port, type, powered);
239 if (data[0] != 0x41) {
240 /* read analog controller */
241 for (i = 0; i < (data[0] & 0x0f); i++)
242 data[i + 1] = db9_saturn_read_analog(port, type, powered);
243 db9_saturn_write_sub(port, type, 3, powered, 0);
244 return data[0];
245 } else {
246 /* read multitap */
247 if (db9_saturn_read_analog(port, type, powered) != 0x60)
248 return data[0] = 0xff;
249 for (i = 0; i < 60; i += 10) {
250 data[i] = db9_saturn_read_analog(port, type, powered);
251 if (data[i] != 0xff)
252 /* read each pad */
253 for (j = 0; j < (data[i] & 0x0f); j++)
254 data[i + j + 1] = db9_saturn_read_analog(port, type, powered);
255 }
256 db9_saturn_write_sub(port, type, 3, powered, 0);
257 return 0x41;
258 }
259 case 0x0:
260 /* 0000 : mouse */
261 db9_saturn_write_sub(port, type, 2, powered, 0);
262 udelay(DB9_SATURN_DELAY);
263 tmp = db9_saturn_read_analog(port, type, powered);
264 if (tmp == 0xff) {
265 for (i = 0; i < 3; i++)
266 data[i + 1] = db9_saturn_read_analog(port, type, powered);
267 db9_saturn_write_sub(port, type, 3, powered, 0);
268 return data[0] = 0xe3;
269 }
270 default:
271 return data[0];
272 }
273}
274
275/*
276 * db9_saturn_report() analyzes packet and reports.
277 */
278static int db9_saturn_report(unsigned char id, unsigned char data[60], struct input_dev *dev, int n, int max_pads)
279{
280 int tmp, i, j;
281
282 tmp = (id == 0x41) ? 60 : 10;
283 for (j = 0; (j < tmp) && (n < max_pads); j += 10, n++) {
284 switch (data[j]) {
285 case 0x16: /* multi controller (analog 4 axis) */
286 input_report_abs(dev + n, db9_abs[5], data[j + 6]);
287 case 0x15: /* mission stick (analog 3 axis) */
288 input_report_abs(dev + n, db9_abs[3], data[j + 4]);
289 input_report_abs(dev + n, db9_abs[4], data[j + 5]);
290 case 0x13: /* racing controller (analog 1 axis) */
291 input_report_abs(dev + n, db9_abs[2], data[j + 3]);
292 case 0x34: /* saturn keyboard (udlr ZXC ASD QE Esc) */
293 case 0x02: /* digital pad (digital 2 axis + buttons) */
294 input_report_abs(dev + n, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
295 input_report_abs(dev + n, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
296 for (i = 0; i < 9; i++)
297 input_report_key(dev + n, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
298 break;
299 case 0x19: /* mission stick x2 (analog 6 axis + buttons) */
300 input_report_abs(dev + n, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
301 input_report_abs(dev + n, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
302 for (i = 0; i < 9; i++)
303 input_report_key(dev + n, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
304 input_report_abs(dev + n, db9_abs[2], data[j + 3]);
305 input_report_abs(dev + n, db9_abs[3], data[j + 4]);
306 input_report_abs(dev + n, db9_abs[4], data[j + 5]);
307 /*
308 input_report_abs(dev + n, db9_abs[8], (data[j + 6] & 128 ? 0 : 1) - (data[j + 6] & 64 ? 0 : 1));
309 input_report_abs(dev + n, db9_abs[9], (data[j + 6] & 32 ? 0 : 1) - (data[j + 6] & 16 ? 0 : 1));
310 */
311 input_report_abs(dev + n, db9_abs[6], data[j + 7]);
312 input_report_abs(dev + n, db9_abs[7], data[j + 8]);
313 input_report_abs(dev + n, db9_abs[5], data[j + 9]);
314 break;
315 case 0xd3: /* sankyo ff (analog 1 axis + stop btn) */
316 input_report_key(dev + n, BTN_A, data[j + 3] & 0x80);
317 input_report_abs(dev + n, db9_abs[2], data[j + 3] & 0x7f);
318 break;
319 case 0xe3: /* shuttle mouse (analog 2 axis + buttons. signed value) */
320 input_report_key(dev + n, BTN_START, data[j + 1] & 0x08);
321 input_report_key(dev + n, BTN_A, data[j + 1] & 0x04);
322 input_report_key(dev + n, BTN_C, data[j + 1] & 0x02);
323 input_report_key(dev + n, BTN_B, data[j + 1] & 0x01);
324 input_report_abs(dev + n, db9_abs[2], data[j + 2] ^ 0x80);
325 input_report_abs(dev + n, db9_abs[3], (0xff-(data[j + 3] ^ 0x80))+1); /* */
326 break;
327 case 0xff:
328 default: /* no pad */
329 input_report_abs(dev + n, db9_abs[0], 0);
330 input_report_abs(dev + n, db9_abs[1], 0);
331 for (i = 0; i < 9; i++)
332 input_report_key(dev + n, db9_cd32_btn[i], 0);
333 break;
334 }
335 }
336 return n;
337}
338
339static int db9_saturn(int mode, struct parport *port, struct input_dev *dev)
340{
341 unsigned char id, data[60];
342 int type, n, max_pads;
343 int tmp, i;
344
345 switch (mode) {
346 case DB9_SATURN_PAD:
347 type = 0;
348 n = 1;
349 break;
350 case DB9_SATURN_DPP:
351 type = 1;
352 n = 1;
353 break;
354 case DB9_SATURN_DPP_2:
355 type = 1;
356 n = 2;
357 break;
358 default:
359 return -1;
360 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500361 max_pads = min(db9_modes[mode].n_pads, DB9_MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 for (tmp = 0, i = 0; i < n; i++) {
363 id = db9_saturn_read_packet(port, data, type + i, 1);
364 tmp = db9_saturn_report(id, data, dev, tmp, max_pads);
365 }
366 return 0;
367}
368
369static void db9_timer(unsigned long private)
370{
371 struct db9 *db9 = (void *) private;
372 struct parport *port = db9->pd->port;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500373 struct input_dev *dev = db9->dev[0];
374 struct input_dev *dev2 = db9->dev[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int data, i;
376
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500377 switch (db9->mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 case DB9_MULTI_0802_2:
379
380 data = parport_read_data(port) >> 3;
381
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500382 input_report_abs(dev2, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
383 input_report_abs(dev2, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
384 input_report_key(dev2, BTN_TRIGGER, ~data & DB9_FIRE1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 case DB9_MULTI_0802:
387
388 data = parport_read_status(port) >> 3;
389
390 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
391 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
392 input_report_key(dev, BTN_TRIGGER, data & DB9_FIRE1);
393 break;
394
395 case DB9_MULTI_STICK:
396
397 data = parport_read_data(port);
398
399 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
400 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
401 input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
402 break;
403
404 case DB9_MULTI2_STICK:
405
406 data = parport_read_data(port);
407
408 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
409 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
410 input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
411 input_report_key(dev, BTN_THUMB, ~data & DB9_FIRE2);
412 break;
413
414 case DB9_GENESIS_PAD:
415
416 parport_write_control(port, DB9_NOSELECT);
417 data = parport_read_data(port);
418
419 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
420 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
421 input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
422 input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
423
424 parport_write_control(port, DB9_NORMAL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500425 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
428 input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
429 break;
430
431 case DB9_GENESIS5_PAD:
432
433 parport_write_control(port, DB9_NOSELECT);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500434 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
437 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
438 input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
439 input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
440
441 parport_write_control(port, DB9_NORMAL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500442 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
445 input_report_key(dev, BTN_X, ~data & DB9_FIRE2);
446 input_report_key(dev, BTN_Y, ~data & DB9_LEFT);
447 input_report_key(dev, BTN_START, ~data & DB9_RIGHT);
448 break;
449
450 case DB9_GENESIS6_PAD:
451
452 parport_write_control(port, DB9_NOSELECT); /* 1 */
453 udelay(DB9_GENESIS6_DELAY);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500454 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
457 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
458 input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
459 input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
460
461 parport_write_control(port, DB9_NORMAL);
462 udelay(DB9_GENESIS6_DELAY);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500463 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
466 input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
467
468 parport_write_control(port, DB9_NOSELECT); /* 2 */
469 udelay(DB9_GENESIS6_DELAY);
470 parport_write_control(port, DB9_NORMAL);
471 udelay(DB9_GENESIS6_DELAY);
472 parport_write_control(port, DB9_NOSELECT); /* 3 */
473 udelay(DB9_GENESIS6_DELAY);
474 data=parport_read_data(port);
475
476 input_report_key(dev, BTN_X, ~data & DB9_LEFT);
477 input_report_key(dev, BTN_Y, ~data & DB9_DOWN);
478 input_report_key(dev, BTN_Z, ~data & DB9_UP);
479 input_report_key(dev, BTN_MODE, ~data & DB9_RIGHT);
480
481 parport_write_control(port, DB9_NORMAL);
482 udelay(DB9_GENESIS6_DELAY);
483 parport_write_control(port, DB9_NOSELECT); /* 4 */
484 udelay(DB9_GENESIS6_DELAY);
485 parport_write_control(port, DB9_NORMAL);
486 break;
487
488 case DB9_SATURN_PAD:
489 case DB9_SATURN_DPP:
490 case DB9_SATURN_DPP_2:
491
492 db9_saturn(db9->mode, port, dev);
493 break;
494
495 case DB9_CD32_PAD:
496
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500497 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
500 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
501
502 parport_write_control(port, 0x0a);
503
504 for (i = 0; i < 7; i++) {
505 data = parport_read_data(port);
506 parport_write_control(port, 0x02);
507 parport_write_control(port, 0x0a);
508 input_report_key(dev, db9_cd32_btn[i], ~data & DB9_FIRE2);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 parport_write_control(port, 0x00);
512 break;
513 }
514
515 input_sync(dev);
516
517 mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
518}
519
520static int db9_open(struct input_dev *dev)
521{
522 struct db9 *db9 = dev->private;
523 struct parport *port = db9->pd->port;
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500524 int err;
525
526 err = down_interruptible(&db9->sem);
527 if (err)
528 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (!db9->used++) {
531 parport_claim(db9->pd);
532 parport_write_data(port, 0xff);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500533 if (db9_modes[db9->mode].reverse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 parport_data_reverse(port);
535 parport_write_control(port, DB9_NORMAL);
536 }
537 mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
538 }
539
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500540 up(&db9->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 0;
542}
543
544static void db9_close(struct input_dev *dev)
545{
546 struct db9 *db9 = dev->private;
547 struct parport *port = db9->pd->port;
548
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500549 down(&db9->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!--db9->used) {
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500551 del_timer_sync(&db9->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 parport_write_control(port, 0x00);
553 parport_data_forward(port);
554 parport_release(db9->pd);
555 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500556 up(&db9->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500559static struct db9 __init *db9_probe(int parport, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct db9 *db9;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500562 const struct db9_mode_data *db9_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct parport *pp;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500564 struct pardevice *pd;
565 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int i, j;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500567 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500569 if (mode < 1 || mode >= DB9_MAX_PAD || !db9_modes[mode].n_buttons) {
570 printk(KERN_ERR "db9.c: Bad device type %d\n", mode);
571 err = -EINVAL;
572 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500575 db9_mode = &db9_modes[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500577 pp = parport_find_number(parport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!pp) {
579 printk(KERN_ERR "db9.c: no such parport\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500580 err = -ENODEV;
581 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500584 if (db9_mode[mode].bidirectional && !(pp->modes & PARPORT_MODE_TRISTATE)) {
585 printk(KERN_ERR "db9.c: specified parport is not bidirectional\n");
586 err = -EINVAL;
587 goto err_put_pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500590 pd = parport_register_device(pp, "db9", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
591 if (!pd) {
592 printk(KERN_ERR "db9.c: parport busy already - lp.o loaded?\n");
593 err = -EBUSY;
594 goto err_put_pp;
595 }
596
597 db9 = kzalloc(sizeof(struct db9), GFP_KERNEL);
598 if (!db9) {
599 printk(KERN_ERR "db9.c: Not enough memory\n");
600 err = -ENOMEM;
601 goto err_unreg_pardev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500604 init_MUTEX(&db9->sem);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500605 db9->pd = pd;
606 db9->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 init_timer(&db9->timer);
608 db9->timer.data = (long) db9;
609 db9->timer.function = db9_timer;
610
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500611 for (i = 0; i < (min(db9_mode->n_pads, DB9_MAX_DEVICES)); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500613 db9->dev[i] = input_dev = input_allocate_device();
614 if (!input_dev) {
615 printk(KERN_ERR "db9.c: Not enough memory for input device\n");
616 err = -ENOMEM;
617 goto err_free_devs;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 sprintf(db9->phys[i], "%s/input%d", db9->pd->port->name, i);
621
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500622 input_dev->name = db9_mode->name;
623 input_dev->phys = db9->phys[i];
624 input_dev->id.bustype = BUS_PARPORT;
625 input_dev->id.vendor = 0x0002;
626 input_dev->id.product = mode;
627 input_dev->id.version = 0x0100;
628 input_dev->private = db9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500630 input_dev->open = db9_open;
631 input_dev->close = db9_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500633 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
634 for (j = 0; j < db9_mode->n_buttons; j++)
635 set_bit(db9_mode->buttons[j], input_dev->keybit);
636 for (j = 0; j < db9_mode->n_axis; j++) {
637 if (j < 2)
638 input_set_abs_params(input_dev, db9_abs[j], -1, 1, 0, 0);
639 else
640 input_set_abs_params(input_dev, db9_abs[j], 1, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500642
643 input_register_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500646 parport_put_port(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return db9;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500648
649 err_free_devs:
650 while (--i >= 0)
651 input_unregister_device(db9->dev[i]);
652 kfree(db9);
653 err_unreg_pardev:
654 parport_unregister_device(pd);
655 err_put_pp:
656 parport_put_port(pp);
657 err_out:
658 return ERR_PTR(err);
659}
660
661static void __exit db9_remove(struct db9 *db9)
662{
663 int i;
664
665 for (i = 0; i < min(db9_modes[db9->mode].n_pads, DB9_MAX_DEVICES); i++)
666 input_unregister_device(db9->dev[i]);
667 parport_unregister_device(db9->pd);
668 kfree(db9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671static int __init db9_init(void)
672{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500673 int i;
674 int have_dev = 0;
675 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500677 for (i = 0; i < DB9_MAX_PORTS; i++) {
678 if (db9[i].nargs == 0 || db9[i].args[DB9_ARG_PARPORT] < 0)
679 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500681 if (db9[i].nargs < 2) {
682 printk(KERN_ERR "db9.c: Device type must be specified.\n");
683 err = -EINVAL;
684 break;
685 }
686
687 db9_base[i] = db9_probe(db9[i].args[DB9_ARG_PARPORT],
688 db9[i].args[DB9_ARG_MODE]);
689 if (IS_ERR(db9_base[i])) {
690 err = PTR_ERR(db9_base[i]);
691 break;
692 }
693
694 have_dev = 1;
695 }
696
697 if (err) {
698 while (--i >= 0)
699 db9_remove(db9_base[i]);
700 return err;
701 }
702
703 return have_dev ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static void __exit db9_exit(void)
707{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500708 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500710 for (i = 0; i < DB9_MAX_PORTS; i++)
711 if (db9_base[i])
712 db9_remove(db9_base[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715module_init(db9_init);
716module_exit(db9_exit);