blob: dcffc34f30c3c130b7a61d5e8b30cf3c453874a8 [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 */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500278static int db9_saturn_report(unsigned char id, unsigned char data[60], struct input_dev *devs[], int n, int max_pads)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500280 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int tmp, i, j;
282
283 tmp = (id == 0x41) ? 60 : 10;
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500284 for (j = 0; j < tmp && n < max_pads; j += 10, n++) {
285 dev = devs[n];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 switch (data[j]) {
287 case 0x16: /* multi controller (analog 4 axis) */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500288 input_report_abs(dev, db9_abs[5], data[j + 6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 case 0x15: /* mission stick (analog 3 axis) */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500290 input_report_abs(dev, db9_abs[3], data[j + 4]);
291 input_report_abs(dev, db9_abs[4], data[j + 5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case 0x13: /* racing controller (analog 1 axis) */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500293 input_report_abs(dev, db9_abs[2], data[j + 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 case 0x34: /* saturn keyboard (udlr ZXC ASD QE Esc) */
295 case 0x02: /* digital pad (digital 2 axis + buttons) */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500296 input_report_abs(dev, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
297 input_report_abs(dev, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 for (i = 0; i < 9; i++)
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500299 input_report_key(dev, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 case 0x19: /* mission stick x2 (analog 6 axis + buttons) */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500302 input_report_abs(dev, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
303 input_report_abs(dev, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 for (i = 0; i < 9; i++)
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500305 input_report_key(dev, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
306 input_report_abs(dev, db9_abs[2], data[j + 3]);
307 input_report_abs(dev, db9_abs[3], data[j + 4]);
308 input_report_abs(dev, db9_abs[4], data[j + 5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /*
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500310 input_report_abs(dev, db9_abs[8], (data[j + 6] & 128 ? 0 : 1) - (data[j + 6] & 64 ? 0 : 1));
311 input_report_abs(dev, db9_abs[9], (data[j + 6] & 32 ? 0 : 1) - (data[j + 6] & 16 ? 0 : 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500313 input_report_abs(dev, db9_abs[6], data[j + 7]);
314 input_report_abs(dev, db9_abs[7], data[j + 8]);
315 input_report_abs(dev, db9_abs[5], data[j + 9]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317 case 0xd3: /* sankyo ff (analog 1 axis + stop btn) */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500318 input_report_key(dev, BTN_A, data[j + 3] & 0x80);
319 input_report_abs(dev, db9_abs[2], data[j + 3] & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 break;
321 case 0xe3: /* shuttle mouse (analog 2 axis + buttons. signed value) */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500322 input_report_key(dev, BTN_START, data[j + 1] & 0x08);
323 input_report_key(dev, BTN_A, data[j + 1] & 0x04);
324 input_report_key(dev, BTN_C, data[j + 1] & 0x02);
325 input_report_key(dev, BTN_B, data[j + 1] & 0x01);
326 input_report_abs(dev, db9_abs[2], data[j + 2] ^ 0x80);
327 input_report_abs(dev, db9_abs[3], (0xff-(data[j + 3] ^ 0x80))+1); /* */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 break;
329 case 0xff:
330 default: /* no pad */
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500331 input_report_abs(dev, db9_abs[0], 0);
332 input_report_abs(dev, db9_abs[1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 for (i = 0; i < 9; i++)
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500334 input_report_key(dev, db9_cd32_btn[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 break;
336 }
337 }
338 return n;
339}
340
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500341static int db9_saturn(int mode, struct parport *port, struct input_dev *devs[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 unsigned char id, data[60];
344 int type, n, max_pads;
345 int tmp, i;
346
347 switch (mode) {
348 case DB9_SATURN_PAD:
349 type = 0;
350 n = 1;
351 break;
352 case DB9_SATURN_DPP:
353 type = 1;
354 n = 1;
355 break;
356 case DB9_SATURN_DPP_2:
357 type = 1;
358 n = 2;
359 break;
360 default:
361 return -1;
362 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500363 max_pads = min(db9_modes[mode].n_pads, DB9_MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 for (tmp = 0, i = 0; i < n; i++) {
365 id = db9_saturn_read_packet(port, data, type + i, 1);
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500366 tmp = db9_saturn_report(id, data, devs, tmp, max_pads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 return 0;
369}
370
371static void db9_timer(unsigned long private)
372{
373 struct db9 *db9 = (void *) private;
374 struct parport *port = db9->pd->port;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500375 struct input_dev *dev = db9->dev[0];
376 struct input_dev *dev2 = db9->dev[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int data, i;
378
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500379 switch (db9->mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 case DB9_MULTI_0802_2:
381
382 data = parport_read_data(port) >> 3;
383
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500384 input_report_abs(dev2, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
385 input_report_abs(dev2, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
386 input_report_key(dev2, BTN_TRIGGER, ~data & DB9_FIRE1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 case DB9_MULTI_0802:
389
390 data = parport_read_status(port) >> 3;
391
392 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
393 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
394 input_report_key(dev, BTN_TRIGGER, data & DB9_FIRE1);
395 break;
396
397 case DB9_MULTI_STICK:
398
399 data = parport_read_data(port);
400
401 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
402 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
403 input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
404 break;
405
406 case DB9_MULTI2_STICK:
407
408 data = parport_read_data(port);
409
410 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
411 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
412 input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
413 input_report_key(dev, BTN_THUMB, ~data & DB9_FIRE2);
414 break;
415
416 case DB9_GENESIS_PAD:
417
418 parport_write_control(port, DB9_NOSELECT);
419 data = parport_read_data(port);
420
421 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
422 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
423 input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
424 input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
425
426 parport_write_control(port, DB9_NORMAL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500427 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
430 input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
431 break;
432
433 case DB9_GENESIS5_PAD:
434
435 parport_write_control(port, DB9_NOSELECT);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500436 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
439 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
440 input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
441 input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
442
443 parport_write_control(port, DB9_NORMAL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500444 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
447 input_report_key(dev, BTN_X, ~data & DB9_FIRE2);
448 input_report_key(dev, BTN_Y, ~data & DB9_LEFT);
449 input_report_key(dev, BTN_START, ~data & DB9_RIGHT);
450 break;
451
452 case DB9_GENESIS6_PAD:
453
454 parport_write_control(port, DB9_NOSELECT); /* 1 */
455 udelay(DB9_GENESIS6_DELAY);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500456 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
459 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
460 input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
461 input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
462
463 parport_write_control(port, DB9_NORMAL);
464 udelay(DB9_GENESIS6_DELAY);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500465 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
468 input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
469
470 parport_write_control(port, DB9_NOSELECT); /* 2 */
471 udelay(DB9_GENESIS6_DELAY);
472 parport_write_control(port, DB9_NORMAL);
473 udelay(DB9_GENESIS6_DELAY);
474 parport_write_control(port, DB9_NOSELECT); /* 3 */
475 udelay(DB9_GENESIS6_DELAY);
476 data=parport_read_data(port);
477
478 input_report_key(dev, BTN_X, ~data & DB9_LEFT);
479 input_report_key(dev, BTN_Y, ~data & DB9_DOWN);
480 input_report_key(dev, BTN_Z, ~data & DB9_UP);
481 input_report_key(dev, BTN_MODE, ~data & DB9_RIGHT);
482
483 parport_write_control(port, DB9_NORMAL);
484 udelay(DB9_GENESIS6_DELAY);
485 parport_write_control(port, DB9_NOSELECT); /* 4 */
486 udelay(DB9_GENESIS6_DELAY);
487 parport_write_control(port, DB9_NORMAL);
488 break;
489
490 case DB9_SATURN_PAD:
491 case DB9_SATURN_DPP:
492 case DB9_SATURN_DPP_2:
493
Dmitry Torokhov84c61892006-01-29 21:51:31 -0500494 db9_saturn(db9->mode, port, db9->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496
497 case DB9_CD32_PAD:
498
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500499 data = parport_read_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
502 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
503
504 parport_write_control(port, 0x0a);
505
506 for (i = 0; i < 7; i++) {
507 data = parport_read_data(port);
508 parport_write_control(port, 0x02);
509 parport_write_control(port, 0x0a);
510 input_report_key(dev, db9_cd32_btn[i], ~data & DB9_FIRE2);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 parport_write_control(port, 0x00);
514 break;
515 }
516
517 input_sync(dev);
518
519 mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
520}
521
522static int db9_open(struct input_dev *dev)
523{
524 struct db9 *db9 = dev->private;
525 struct parport *port = db9->pd->port;
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500526 int err;
527
528 err = down_interruptible(&db9->sem);
529 if (err)
530 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (!db9->used++) {
533 parport_claim(db9->pd);
534 parport_write_data(port, 0xff);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500535 if (db9_modes[db9->mode].reverse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 parport_data_reverse(port);
537 parport_write_control(port, DB9_NORMAL);
538 }
539 mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
540 }
541
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500542 up(&db9->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return 0;
544}
545
546static void db9_close(struct input_dev *dev)
547{
548 struct db9 *db9 = dev->private;
549 struct parport *port = db9->pd->port;
550
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500551 down(&db9->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (!--db9->used) {
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500553 del_timer_sync(&db9->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 parport_write_control(port, 0x00);
555 parport_data_forward(port);
556 parport_release(db9->pd);
557 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500558 up(&db9->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500561static struct db9 __init *db9_probe(int parport, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct db9 *db9;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500564 const struct db9_mode_data *db9_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct parport *pp;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500566 struct pardevice *pd;
567 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int i, j;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500569 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500571 if (mode < 1 || mode >= DB9_MAX_PAD || !db9_modes[mode].n_buttons) {
572 printk(KERN_ERR "db9.c: Bad device type %d\n", mode);
573 err = -EINVAL;
574 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500577 db9_mode = &db9_modes[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500579 pp = parport_find_number(parport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!pp) {
581 printk(KERN_ERR "db9.c: no such parport\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500582 err = -ENODEV;
583 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500586 if (db9_mode[mode].bidirectional && !(pp->modes & PARPORT_MODE_TRISTATE)) {
587 printk(KERN_ERR "db9.c: specified parport is not bidirectional\n");
588 err = -EINVAL;
589 goto err_put_pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500592 pd = parport_register_device(pp, "db9", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
593 if (!pd) {
594 printk(KERN_ERR "db9.c: parport busy already - lp.o loaded?\n");
595 err = -EBUSY;
596 goto err_put_pp;
597 }
598
599 db9 = kzalloc(sizeof(struct db9), GFP_KERNEL);
600 if (!db9) {
601 printk(KERN_ERR "db9.c: Not enough memory\n");
602 err = -ENOMEM;
603 goto err_unreg_pardev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500606 init_MUTEX(&db9->sem);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500607 db9->pd = pd;
608 db9->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 init_timer(&db9->timer);
610 db9->timer.data = (long) db9;
611 db9->timer.function = db9_timer;
612
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500613 for (i = 0; i < (min(db9_mode->n_pads, DB9_MAX_DEVICES)); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500615 db9->dev[i] = input_dev = input_allocate_device();
616 if (!input_dev) {
617 printk(KERN_ERR "db9.c: Not enough memory for input device\n");
618 err = -ENOMEM;
Dmitry Torokhov2e9d6752006-01-29 21:51:36 -0500619 goto err_unreg_devs;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 sprintf(db9->phys[i], "%s/input%d", db9->pd->port->name, i);
623
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500624 input_dev->name = db9_mode->name;
625 input_dev->phys = db9->phys[i];
626 input_dev->id.bustype = BUS_PARPORT;
627 input_dev->id.vendor = 0x0002;
628 input_dev->id.product = mode;
629 input_dev->id.version = 0x0100;
630 input_dev->private = db9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500632 input_dev->open = db9_open;
633 input_dev->close = db9_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500635 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
636 for (j = 0; j < db9_mode->n_buttons; j++)
637 set_bit(db9_mode->buttons[j], input_dev->keybit);
638 for (j = 0; j < db9_mode->n_axis; j++) {
639 if (j < 2)
640 input_set_abs_params(input_dev, db9_abs[j], -1, 1, 0, 0);
641 else
642 input_set_abs_params(input_dev, db9_abs[j], 1, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500644
Dmitry Torokhov2e9d6752006-01-29 21:51:36 -0500645 err = input_register_device(input_dev);
646 if (err)
647 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500650 parport_put_port(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return db9;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500652
Dmitry Torokhov2e9d6752006-01-29 21:51:36 -0500653 err_free_dev:
654 input_free_device(db9->dev[i]);
655 err_unreg_devs:
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500656 while (--i >= 0)
657 input_unregister_device(db9->dev[i]);
658 kfree(db9);
659 err_unreg_pardev:
660 parport_unregister_device(pd);
661 err_put_pp:
662 parport_put_port(pp);
663 err_out:
664 return ERR_PTR(err);
665}
666
Dmitry Torokhov2e9d6752006-01-29 21:51:36 -0500667static void db9_remove(struct db9 *db9)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500668{
669 int i;
670
671 for (i = 0; i < min(db9_modes[db9->mode].n_pads, DB9_MAX_DEVICES); i++)
672 input_unregister_device(db9->dev[i]);
673 parport_unregister_device(db9->pd);
674 kfree(db9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677static int __init db9_init(void)
678{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500679 int i;
680 int have_dev = 0;
681 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500683 for (i = 0; i < DB9_MAX_PORTS; i++) {
684 if (db9[i].nargs == 0 || db9[i].args[DB9_ARG_PARPORT] < 0)
685 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500687 if (db9[i].nargs < 2) {
688 printk(KERN_ERR "db9.c: Device type must be specified.\n");
689 err = -EINVAL;
690 break;
691 }
692
693 db9_base[i] = db9_probe(db9[i].args[DB9_ARG_PARPORT],
694 db9[i].args[DB9_ARG_MODE]);
695 if (IS_ERR(db9_base[i])) {
696 err = PTR_ERR(db9_base[i]);
697 break;
698 }
699
700 have_dev = 1;
701 }
702
703 if (err) {
704 while (--i >= 0)
Dmitry Torokhov2e9d6752006-01-29 21:51:36 -0500705 if (db9_base[i])
706 db9_remove(db9_base[i]);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500707 return err;
708 }
709
710 return have_dev ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713static void __exit db9_exit(void)
714{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500715 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500717 for (i = 0; i < DB9_MAX_PORTS; i++)
718 if (db9_base[i])
719 db9_remove(db9_base[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722module_init(db9_init);
723module_exit(db9_exit);