xref: /openbmc/linux/drivers/input/joystick/db9.c (revision 1da177e4)
1 /*
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 
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION("Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver");
44 MODULE_LICENSE("GPL");
45 
46 static int db9[] __initdata = { -1, 0 };
47 static int db9_nargs __initdata = 0;
48 module_param_array_named(dev, db9, int, &db9_nargs, 0);
49 MODULE_PARM_DESC(dev, "Describes first attached device (<parport#>,<type>)");
50 
51 static int db9_2[] __initdata = { -1, 0 };
52 static int db9_nargs_2 __initdata = 0;
53 module_param_array_named(dev2, db9_2, int, &db9_nargs_2, 0);
54 MODULE_PARM_DESC(dev2, "Describes second attached device (<parport#>,<type>)");
55 
56 static int db9_3[] __initdata = { -1, 0 };
57 static int db9_nargs_3 __initdata = 0;
58 module_param_array_named(dev3, db9_3, int, &db9_nargs_3, 0);
59 MODULE_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 
65 #define DB9_MULTI_STICK		0x01
66 #define DB9_MULTI2_STICK	0x02
67 #define DB9_GENESIS_PAD		0x03
68 #define DB9_GENESIS5_PAD	0x05
69 #define DB9_GENESIS6_PAD	0x06
70 #define DB9_SATURN_PAD		0x07
71 #define DB9_MULTI_0802		0x08
72 #define DB9_MULTI_0802_2	0x09
73 #define DB9_CD32_PAD		0x0A
74 #define DB9_SATURN_DPP		0x0B
75 #define DB9_SATURN_DPP_2	0x0C
76 #define DB9_MAX_PAD		0x0D
77 
78 #define DB9_UP			0x01
79 #define DB9_DOWN		0x02
80 #define DB9_LEFT		0x04
81 #define DB9_RIGHT		0x08
82 #define DB9_FIRE1		0x10
83 #define DB9_FIRE2		0x20
84 #define DB9_FIRE3		0x40
85 #define DB9_FIRE4		0x80
86 
87 #define DB9_NORMAL		0x0a
88 #define DB9_NOSELECT		0x08
89 
90 #define DB9_MAX_DEVICES 2
91 
92 #define DB9_GENESIS6_DELAY	14
93 #define DB9_REFRESH_TIME	HZ/100
94 
95 struct db9 {
96 	struct input_dev dev[DB9_MAX_DEVICES];
97 	struct timer_list timer;
98 	struct pardevice *pd;
99 	int mode;
100 	int used;
101 	char phys[2][32];
102 };
103 
104 static struct db9 *db9_base[3];
105 
106 static short db9_multi_btn[] = { BTN_TRIGGER, BTN_THUMB };
107 static short db9_genesis_btn[] = { BTN_START, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_MODE };
108 static short db9_cd32_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START };
109 
110 static char db9_buttons[DB9_MAX_PAD] = { 0, 1, 2, 4, 0, 6, 8, 9, 1, 1, 7, 9, 9 };
111 static short *db9_btn[DB9_MAX_PAD] = { NULL, db9_multi_btn, db9_multi_btn, db9_genesis_btn, NULL, db9_genesis_btn,
112 					db9_genesis_btn, db9_cd32_btn, db9_multi_btn, db9_multi_btn, db9_cd32_btn,
113 					db9_cd32_btn, db9_cd32_btn };
114 static char *db9_name[DB9_MAX_PAD] = { NULL, "Multisystem joystick", "Multisystem joystick (2 fire)", "Genesis pad",
115 				      NULL, "Genesis 5 pad", "Genesis 6 pad", "Saturn pad", "Multisystem (0.8.0.2) joystick",
116 				     "Multisystem (0.8.0.2-dual) joystick", "Amiga CD-32 pad", "Saturn dpp", "Saturn dpp dual" };
117 
118 static const int db9_max_pads[DB9_MAX_PAD] = { 0, 1, 1, 1, 0, 1, 1, 6, 1, 2, 1, 6, 12 };
119 static const int db9_num_axis[DB9_MAX_PAD] = { 0, 2, 2, 2, 0, 2, 2, 7, 2, 2, 2 ,7, 7 };
120 static const short db9_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_RZ, ABS_Z, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
121 static const int db9_bidirectional[DB9_MAX_PAD] = { 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0 };
122 static const int db9_reverse[DB9_MAX_PAD] = { 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0 };
123 
124 /*
125  * Saturn controllers
126  */
127 #define DB9_SATURN_DELAY 300
128 static const int db9_saturn_byte[] = { 1, 1, 1, 2, 2, 2, 2, 2, 1 };
129 static const unsigned char db9_saturn_mask[] = { 0x04, 0x01, 0x02, 0x40, 0x20, 0x10, 0x08, 0x80, 0x08 };
130 
131 /*
132  * db9_saturn_write_sub() writes 2 bit data.
133  */
134 static void db9_saturn_write_sub(struct parport *port, int type, unsigned char data, int powered, int pwr_sub)
135 {
136 	unsigned char c;
137 
138 	switch (type) {
139 	case 1: /* DPP1 */
140 		c = 0x80 | 0x30 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | data;
141 		parport_write_data(port, c);
142 		break;
143 	case 2: /* DPP2 */
144 		c = 0x40 | data << 4 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | 0x03;
145 		parport_write_data(port, c);
146 		break;
147 	case 0:	/* DB9 */
148 		c = ((((data & 2) ? 2 : 0) | ((data & 1) ? 4 : 0)) ^ 0x02) | !powered;
149 		parport_write_control(port, c);
150 		break;
151 	}
152 }
153 
154 /*
155  * gc_saturn_read_sub() reads 4 bit data.
156  */
157 static unsigned char db9_saturn_read_sub(struct parport *port, int type)
158 {
159 	unsigned char data;
160 
161 	if (type) {
162 		/* DPP */
163 		data = parport_read_status(port) ^ 0x80;
164 		return (data & 0x80 ? 1 : 0) | (data & 0x40 ? 2 : 0)
165 		     | (data & 0x20 ? 4 : 0) | (data & 0x10 ? 8 : 0);
166 	} else {
167 		/* DB9 */
168 		data = parport_read_data(port) & 0x0f;
169 		return (data & 0x8 ? 1 : 0) | (data & 0x4 ? 2 : 0)
170 		     | (data & 0x2 ? 4 : 0) | (data & 0x1 ? 8 : 0);
171 	}
172 }
173 
174 /*
175  * db9_saturn_read_analog() sends clock and reads 8 bit data.
176  */
177 static unsigned char db9_saturn_read_analog(struct parport *port, int type, int powered)
178 {
179 	unsigned char data;
180 
181 	db9_saturn_write_sub(port, type, 0, powered, 0);
182 	udelay(DB9_SATURN_DELAY);
183 	data = db9_saturn_read_sub(port, type) << 4;
184 	db9_saturn_write_sub(port, type, 2, powered, 0);
185 	udelay(DB9_SATURN_DELAY);
186 	data |= db9_saturn_read_sub(port, type);
187 	return data;
188 }
189 
190 /*
191  * db9_saturn_read_packet() reads whole saturn packet at connector
192  * and returns device identifier code.
193  */
194 static unsigned char db9_saturn_read_packet(struct parport *port, unsigned char *data, int type, int powered)
195 {
196 	int i, j;
197 	unsigned char tmp;
198 
199 	db9_saturn_write_sub(port, type, 3, powered, 0);
200 	data[0] = db9_saturn_read_sub(port, type);
201 	switch (data[0] & 0x0f) {
202 	case 0xf:
203 		/* 1111  no pad */
204 		return data[0] = 0xff;
205 	case 0x4: case 0x4 | 0x8:
206 		/* ?100 : digital controller */
207 		db9_saturn_write_sub(port, type, 0, powered, 1);
208 		data[2] = db9_saturn_read_sub(port, type) << 4;
209 		db9_saturn_write_sub(port, type, 2, powered, 1);
210 		data[1] = db9_saturn_read_sub(port, type) << 4;
211 		db9_saturn_write_sub(port, type, 1, powered, 1);
212 		data[1] |= db9_saturn_read_sub(port, type);
213 		db9_saturn_write_sub(port, type, 3, powered, 1);
214 		/* data[2] |= db9_saturn_read_sub(port, type); */
215 		data[2] |= data[0];
216 		return data[0] = 0x02;
217 	case 0x1:
218 		/* 0001 : analog controller or multitap */
219 		db9_saturn_write_sub(port, type, 2, powered, 0);
220 		udelay(DB9_SATURN_DELAY);
221 		data[0] = db9_saturn_read_analog(port, type, powered);
222 		if (data[0] != 0x41) {
223 			/* read analog controller */
224 			for (i = 0; i < (data[0] & 0x0f); i++)
225 				data[i + 1] = db9_saturn_read_analog(port, type, powered);
226 			db9_saturn_write_sub(port, type, 3, powered, 0);
227 			return data[0];
228 		} else {
229 			/* read multitap */
230 			if (db9_saturn_read_analog(port, type, powered) != 0x60)
231 				return data[0] = 0xff;
232 			for (i = 0; i < 60; i += 10) {
233 				data[i] = db9_saturn_read_analog(port, type, powered);
234 				if (data[i] != 0xff)
235 					/* read each pad */
236 					for (j = 0; j < (data[i] & 0x0f); j++)
237 						data[i + j + 1] = db9_saturn_read_analog(port, type, powered);
238 			}
239 			db9_saturn_write_sub(port, type, 3, powered, 0);
240 			return 0x41;
241 		}
242 	case 0x0:
243 		/* 0000 : mouse */
244 		db9_saturn_write_sub(port, type, 2, powered, 0);
245 		udelay(DB9_SATURN_DELAY);
246 		tmp = db9_saturn_read_analog(port, type, powered);
247 		if (tmp == 0xff) {
248 			for (i = 0; i < 3; i++)
249 				data[i + 1] = db9_saturn_read_analog(port, type, powered);
250 			db9_saturn_write_sub(port, type, 3, powered, 0);
251 			return data[0] = 0xe3;
252 		}
253 	default:
254 		return data[0];
255 	}
256 }
257 
258 /*
259  * db9_saturn_report() analyzes packet and reports.
260  */
261 static int db9_saturn_report(unsigned char id, unsigned char data[60], struct input_dev *dev, int n, int max_pads)
262 {
263 	int tmp, i, j;
264 
265 	tmp = (id == 0x41) ? 60 : 10;
266 	for (j = 0; (j < tmp) && (n < max_pads); j += 10, n++) {
267 		switch (data[j]) {
268 		case 0x16: /* multi controller (analog 4 axis) */
269 			input_report_abs(dev + n, db9_abs[5], data[j + 6]);
270 		case 0x15: /* mission stick (analog 3 axis) */
271 			input_report_abs(dev + n, db9_abs[3], data[j + 4]);
272 			input_report_abs(dev + n, db9_abs[4], data[j + 5]);
273 		case 0x13: /* racing controller (analog 1 axis) */
274 			input_report_abs(dev + n, db9_abs[2], data[j + 3]);
275 		case 0x34: /* saturn keyboard (udlr ZXC ASD QE Esc) */
276 		case 0x02: /* digital pad (digital 2 axis + buttons) */
277 			input_report_abs(dev + n, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
278 			input_report_abs(dev + n, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
279 			for (i = 0; i < 9; i++)
280 				input_report_key(dev + n, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
281 			break;
282 		case 0x19: /* mission stick x2 (analog 6 axis + buttons) */
283 			input_report_abs(dev + n, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
284 			input_report_abs(dev + n, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
285 			for (i = 0; i < 9; i++)
286 				input_report_key(dev + n, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
287 			input_report_abs(dev + n, db9_abs[2], data[j + 3]);
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 			/*
291 			input_report_abs(dev + n, db9_abs[8], (data[j + 6] & 128 ? 0 : 1) - (data[j + 6] & 64 ? 0 : 1));
292 			input_report_abs(dev + n, db9_abs[9], (data[j + 6] & 32 ? 0 : 1) - (data[j + 6] & 16 ? 0 : 1));
293 			*/
294 			input_report_abs(dev + n, db9_abs[6], data[j + 7]);
295 			input_report_abs(dev + n, db9_abs[7], data[j + 8]);
296 			input_report_abs(dev + n, db9_abs[5], data[j + 9]);
297 			break;
298 		case 0xd3: /* sankyo ff (analog 1 axis + stop btn) */
299 			input_report_key(dev + n, BTN_A, data[j + 3] & 0x80);
300 			input_report_abs(dev + n, db9_abs[2], data[j + 3] & 0x7f);
301 			break;
302 		case 0xe3: /* shuttle mouse (analog 2 axis + buttons. signed value) */
303 			input_report_key(dev + n, BTN_START, data[j + 1] & 0x08);
304 			input_report_key(dev + n, BTN_A, data[j + 1] & 0x04);
305 			input_report_key(dev + n, BTN_C, data[j + 1] & 0x02);
306 			input_report_key(dev + n, BTN_B, data[j + 1] & 0x01);
307 			input_report_abs(dev + n, db9_abs[2], data[j + 2] ^ 0x80);
308 			input_report_abs(dev + n, db9_abs[3], (0xff-(data[j + 3] ^ 0x80))+1); /* */
309 			break;
310 		case 0xff:
311 		default: /* no pad */
312 			input_report_abs(dev + n, db9_abs[0], 0);
313 			input_report_abs(dev + n, db9_abs[1], 0);
314 			for (i = 0; i < 9; i++)
315 				input_report_key(dev + n, db9_cd32_btn[i], 0);
316 			break;
317 		}
318 	}
319 	return n;
320 }
321 
322 static int db9_saturn(int mode, struct parport *port, struct input_dev *dev)
323 {
324 	unsigned char id, data[60];
325 	int type, n, max_pads;
326 	int tmp, i;
327 
328 	switch (mode) {
329 	case DB9_SATURN_PAD:
330 		type = 0;
331 		n = 1;
332 		break;
333 	case DB9_SATURN_DPP:
334 		type = 1;
335 		n = 1;
336 		break;
337 	case DB9_SATURN_DPP_2:
338 		type = 1;
339 		n = 2;
340 		break;
341 	default:
342 		return -1;
343 	}
344 	max_pads = min(db9_max_pads[mode], DB9_MAX_DEVICES);
345 	for (tmp = 0, i = 0; i < n; i++) {
346 		id = db9_saturn_read_packet(port, data, type + i, 1);
347 		tmp = db9_saturn_report(id, data, dev, tmp, max_pads);
348 	}
349 	return 0;
350 }
351 
352 static void db9_timer(unsigned long private)
353 {
354 	struct db9 *db9 = (void *) private;
355 	struct parport *port = db9->pd->port;
356 	struct input_dev *dev = db9->dev;
357 	int data, i;
358 
359 	switch(db9->mode) {
360 		case DB9_MULTI_0802_2:
361 
362 			data = parport_read_data(port) >> 3;
363 
364 			input_report_abs(dev + 1, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
365 			input_report_abs(dev + 1, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
366 			input_report_key(dev + 1, BTN_TRIGGER, ~data & DB9_FIRE1);
367 
368 		case DB9_MULTI_0802:
369 
370 			data = parport_read_status(port) >> 3;
371 
372 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
373 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
374 			input_report_key(dev, BTN_TRIGGER, data & DB9_FIRE1);
375 			break;
376 
377 		case DB9_MULTI_STICK:
378 
379 			data = parport_read_data(port);
380 
381 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
382 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
383 			input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
384 			break;
385 
386 		case DB9_MULTI2_STICK:
387 
388 			data = parport_read_data(port);
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 			input_report_key(dev, BTN_THUMB,   ~data & DB9_FIRE2);
394 			break;
395 
396 		case DB9_GENESIS_PAD:
397 
398 			parport_write_control(port, DB9_NOSELECT);
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_B, ~data & DB9_FIRE1);
404 			input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
405 
406 			parport_write_control(port, DB9_NORMAL);
407 			data=parport_read_data(port);
408 
409 			input_report_key(dev, BTN_A,     ~data & DB9_FIRE1);
410 			input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
411 			break;
412 
413 		case DB9_GENESIS5_PAD:
414 
415 			parport_write_control(port, DB9_NOSELECT);
416 			data=parport_read_data(port);
417 
418 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
419 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
420 			input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
421 			input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
422 
423 			parport_write_control(port, DB9_NORMAL);
424 			data=parport_read_data(port);
425 
426 			input_report_key(dev, BTN_A,     ~data & DB9_FIRE1);
427 			input_report_key(dev, BTN_X,     ~data & DB9_FIRE2);
428 			input_report_key(dev, BTN_Y,     ~data & DB9_LEFT);
429 			input_report_key(dev, BTN_START, ~data & DB9_RIGHT);
430 			break;
431 
432 		case DB9_GENESIS6_PAD:
433 
434 			parport_write_control(port, DB9_NOSELECT); /* 1 */
435 			udelay(DB9_GENESIS6_DELAY);
436 			data=parport_read_data(port);
437 
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);
444 			udelay(DB9_GENESIS6_DELAY);
445 			data=parport_read_data(port);
446 
447 			input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
448 			input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
449 
450 			parport_write_control(port, DB9_NOSELECT); /* 2 */
451 			udelay(DB9_GENESIS6_DELAY);
452 			parport_write_control(port, DB9_NORMAL);
453 			udelay(DB9_GENESIS6_DELAY);
454 			parport_write_control(port, DB9_NOSELECT); /* 3 */
455 			udelay(DB9_GENESIS6_DELAY);
456 			data=parport_read_data(port);
457 
458 			input_report_key(dev, BTN_X,    ~data & DB9_LEFT);
459 			input_report_key(dev, BTN_Y,    ~data & DB9_DOWN);
460 			input_report_key(dev, BTN_Z,    ~data & DB9_UP);
461 			input_report_key(dev, BTN_MODE, ~data & DB9_RIGHT);
462 
463 			parport_write_control(port, DB9_NORMAL);
464 			udelay(DB9_GENESIS6_DELAY);
465 			parport_write_control(port, DB9_NOSELECT); /* 4 */
466 			udelay(DB9_GENESIS6_DELAY);
467 			parport_write_control(port, DB9_NORMAL);
468 			break;
469 
470 		case DB9_SATURN_PAD:
471 		case DB9_SATURN_DPP:
472 		case DB9_SATURN_DPP_2:
473 
474 			db9_saturn(db9->mode, port, dev);
475 			break;
476 
477 		case DB9_CD32_PAD:
478 
479 			data=parport_read_data(port);
480 
481 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
482 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
483 
484 			parport_write_control(port, 0x0a);
485 
486 			for (i = 0; i < 7; i++) {
487 				data = parport_read_data(port);
488 				parport_write_control(port, 0x02);
489 				parport_write_control(port, 0x0a);
490 				input_report_key(dev, db9_cd32_btn[i], ~data & DB9_FIRE2);
491 				}
492 
493 			parport_write_control(port, 0x00);
494 			break;
495 		}
496 
497 	input_sync(dev);
498 
499 	mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
500 }
501 
502 static int db9_open(struct input_dev *dev)
503 {
504 	struct db9 *db9 = dev->private;
505 	struct parport *port = db9->pd->port;
506 
507 	if (!db9->used++) {
508 		parport_claim(db9->pd);
509 		parport_write_data(port, 0xff);
510 		if (db9_reverse[db9->mode]) {
511 			parport_data_reverse(port);
512 			parport_write_control(port, DB9_NORMAL);
513 		}
514 		mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
515 	}
516 
517 	return 0;
518 }
519 
520 static void db9_close(struct input_dev *dev)
521 {
522 	struct db9 *db9 = dev->private;
523 	struct parport *port = db9->pd->port;
524 
525 	if (!--db9->used) {
526 		del_timer(&db9->timer);
527 		parport_write_control(port, 0x00);
528 		parport_data_forward(port);
529 		parport_release(db9->pd);
530 	}
531 }
532 
533 static struct db9 __init *db9_probe(int *config, int nargs)
534 {
535 	struct db9 *db9;
536 	struct parport *pp;
537 	int i, j;
538 
539 	if (config[0] < 0)
540 		return NULL;
541 
542 	if (nargs < 2) {
543 		printk(KERN_ERR "db9.c: Device type must be specified.\n");
544 		return NULL;
545 	}
546 
547 	if (config[1] < 1 || config[1] >= DB9_MAX_PAD || !db9_buttons[config[1]]) {
548 		printk(KERN_ERR "db9.c: bad config\n");
549 		return NULL;
550 	}
551 
552 	pp = parport_find_number(config[0]);
553 	if (!pp) {
554 		printk(KERN_ERR "db9.c: no such parport\n");
555 		return NULL;
556 	}
557 
558 	if (db9_bidirectional[config[1]]) {
559 		if (!(pp->modes & PARPORT_MODE_TRISTATE)) {
560 			printk(KERN_ERR "db9.c: specified parport is not bidirectional\n");
561 			parport_put_port(pp);
562 			return NULL;
563 		}
564 	}
565 
566 	if (!(db9 = kmalloc(sizeof(struct db9), GFP_KERNEL))) {
567 		parport_put_port(pp);
568 		return NULL;
569 	}
570 	memset(db9, 0, sizeof(struct db9));
571 
572 	db9->mode = config[1];
573 	init_timer(&db9->timer);
574 	db9->timer.data = (long) db9;
575 	db9->timer.function = db9_timer;
576 
577 	db9->pd = parport_register_device(pp, "db9", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
578 	parport_put_port(pp);
579 
580 	if (!db9->pd) {
581 		printk(KERN_ERR "db9.c: parport busy already - lp.o loaded?\n");
582 		kfree(db9);
583 		return NULL;
584 	}
585 
586 	for (i = 0; i < (min(db9_max_pads[db9->mode], DB9_MAX_DEVICES)); i++) {
587 
588 		sprintf(db9->phys[i], "%s/input%d", db9->pd->port->name, i);
589 
590 		db9->dev[i].private = db9;
591 		db9->dev[i].open = db9_open;
592 		db9->dev[i].close = db9_close;
593 
594 		db9->dev[i].name = db9_name[db9->mode];
595 		db9->dev[i].phys = db9->phys[i];
596 		db9->dev[i].id.bustype = BUS_PARPORT;
597 		db9->dev[i].id.vendor = 0x0002;
598 		db9->dev[i].id.product = config[1];
599 		db9->dev[i].id.version = 0x0100;
600 
601 		db9->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
602 		for (j = 0; j < db9_buttons[db9->mode]; j++)
603 			set_bit(db9_btn[db9->mode][j], db9->dev[i].keybit);
604 		for (j = 0; j < db9_num_axis[db9->mode]; j++) {
605 			set_bit(db9_abs[j], db9->dev[i].absbit);
606 			if (j < 2) {
607 				db9->dev[i].absmin[db9_abs[j]] = -1;
608 				db9->dev[i].absmax[db9_abs[j]] = 1;
609 			} else {
610 				db9->dev[i].absmin[db9_abs[j]] = 1;
611 				db9->dev[i].absmax[db9_abs[j]] = 255;
612 				db9->dev[i].absflat[db9_abs[j]] = 0;
613 			}
614 		}
615 		input_register_device(db9->dev + i);
616 		printk(KERN_INFO "input: %s on %s\n", db9->dev[i].name, db9->pd->port->name);
617 	}
618 
619 	return db9;
620 }
621 
622 static int __init db9_init(void)
623 {
624 	db9_base[0] = db9_probe(db9, db9_nargs);
625 	db9_base[1] = db9_probe(db9_2, db9_nargs_2);
626 	db9_base[2] = db9_probe(db9_3, db9_nargs_3);
627 
628 	if (db9_base[0] || db9_base[1] || db9_base[2])
629 		return 0;
630 
631 	return -ENODEV;
632 }
633 
634 static void __exit db9_exit(void)
635 {
636 	int i, j;
637 
638 	for (i = 0; i < 3; i++)
639 		if (db9_base[i]) {
640 			for (j = 0; j < min(db9_max_pads[db9_base[i]->mode], DB9_MAX_DEVICES); j++)
641 				input_unregister_device(db9_base[i]->dev + j);
642 		parport_unregister_device(db9_base[i]->pd);
643 	}
644 }
645 
646 module_init(db9_init);
647 module_exit(db9_exit);
648