xref: /linux/drivers/input/joystick/db9.c (revision 95298d63c67673c654c08952672d016212b26054)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 1999-2001 Vojtech Pavlik
4  *
5  *  Based on the work of:
6  *	Andree Borrmann		Mats Sjövall
7  */
8 
9 /*
10  * Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver for Linux
11  */
12 
13 /*
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/parport.h>
21 #include <linux/input.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
26 MODULE_DESCRIPTION("Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver");
27 MODULE_LICENSE("GPL");
28 
29 struct db9_config {
30 	int args[2];
31 	unsigned int nargs;
32 };
33 
34 #define DB9_MAX_PORTS		3
35 static struct db9_config db9_cfg[DB9_MAX_PORTS];
36 
37 module_param_array_named(dev, db9_cfg[0].args, int, &db9_cfg[0].nargs, 0);
38 MODULE_PARM_DESC(dev, "Describes first attached device (<parport#>,<type>)");
39 module_param_array_named(dev2, db9_cfg[1].args, int, &db9_cfg[1].nargs, 0);
40 MODULE_PARM_DESC(dev2, "Describes second attached device (<parport#>,<type>)");
41 module_param_array_named(dev3, db9_cfg[2].args, int, &db9_cfg[2].nargs, 0);
42 MODULE_PARM_DESC(dev3, "Describes third attached device (<parport#>,<type>)");
43 
44 #define DB9_ARG_PARPORT		0
45 #define DB9_ARG_MODE		1
46 
47 #define DB9_MULTI_STICK		0x01
48 #define DB9_MULTI2_STICK	0x02
49 #define DB9_GENESIS_PAD		0x03
50 #define DB9_GENESIS5_PAD	0x05
51 #define DB9_GENESIS6_PAD	0x06
52 #define DB9_SATURN_PAD		0x07
53 #define DB9_MULTI_0802		0x08
54 #define DB9_MULTI_0802_2	0x09
55 #define DB9_CD32_PAD		0x0A
56 #define DB9_SATURN_DPP		0x0B
57 #define DB9_SATURN_DPP_2	0x0C
58 #define DB9_MAX_PAD		0x0D
59 
60 #define DB9_UP			0x01
61 #define DB9_DOWN		0x02
62 #define DB9_LEFT		0x04
63 #define DB9_RIGHT		0x08
64 #define DB9_FIRE1		0x10
65 #define DB9_FIRE2		0x20
66 #define DB9_FIRE3		0x40
67 #define DB9_FIRE4		0x80
68 
69 #define DB9_NORMAL		0x0a
70 #define DB9_NOSELECT		0x08
71 
72 #define DB9_GENESIS6_DELAY	14
73 #define DB9_REFRESH_TIME	HZ/100
74 
75 #define DB9_MAX_DEVICES		2
76 
77 struct db9_mode_data {
78 	const char *name;
79 	const short *buttons;
80 	int n_buttons;
81 	int n_pads;
82 	int n_axis;
83 	int bidirectional;
84 	int reverse;
85 };
86 
87 struct db9 {
88 	struct input_dev *dev[DB9_MAX_DEVICES];
89 	struct timer_list timer;
90 	struct pardevice *pd;
91 	int mode;
92 	int used;
93 	int parportno;
94 	struct mutex mutex;
95 	char phys[DB9_MAX_DEVICES][32];
96 };
97 
98 static struct db9 *db9_base[3];
99 
100 static const short db9_multi_btn[] = { BTN_TRIGGER, BTN_THUMB };
101 static const short db9_genesis_btn[] = { BTN_START, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_MODE };
102 static const short db9_cd32_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START };
103 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 };
104 
105 static const struct db9_mode_data db9_modes[] = {
106 	{ NULL,					 NULL,		  0,  0,  0,  0,  0 },
107 	{ "Multisystem joystick",		 db9_multi_btn,	  1,  1,  2,  1,  1 },
108 	{ "Multisystem joystick (2 fire)",	 db9_multi_btn,	  2,  1,  2,  1,  1 },
109 	{ "Genesis pad",			 db9_genesis_btn, 4,  1,  2,  1,  1 },
110 	{ NULL,					 NULL,		  0,  0,  0,  0,  0 },
111 	{ "Genesis 5 pad",			 db9_genesis_btn, 6,  1,  2,  1,  1 },
112 	{ "Genesis 6 pad",			 db9_genesis_btn, 8,  1,  2,  1,  1 },
113 	{ "Saturn pad",				 db9_cd32_btn,	  9,  6,  7,  0,  1 },
114 	{ "Multisystem (0.8.0.2) joystick",	 db9_multi_btn,	  1,  1,  2,  1,  1 },
115 	{ "Multisystem (0.8.0.2-dual) joystick", db9_multi_btn,	  1,  2,  2,  1,  1 },
116 	{ "Amiga CD-32 pad",			 db9_cd32_btn,	  7,  1,  2,  1,  1 },
117 	{ "Saturn dpp",				 db9_cd32_btn,	  9,  6,  7,  0,  0 },
118 	{ "Saturn dpp dual",			 db9_cd32_btn,	  9,  12, 7,  0,  0 },
119 };
120 
121 /*
122  * Saturn controllers
123  */
124 #define DB9_SATURN_DELAY 300
125 static const int db9_saturn_byte[] = { 1, 1, 1, 2, 2, 2, 2, 2, 1 };
126 static const unsigned char db9_saturn_mask[] = { 0x04, 0x01, 0x02, 0x40, 0x20, 0x10, 0x08, 0x80, 0x08 };
127 
128 /*
129  * db9_saturn_write_sub() writes 2 bit data.
130  */
131 static void db9_saturn_write_sub(struct parport *port, int type, unsigned char data, int powered, int pwr_sub)
132 {
133 	unsigned char c;
134 
135 	switch (type) {
136 	case 1: /* DPP1 */
137 		c = 0x80 | 0x30 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | data;
138 		parport_write_data(port, c);
139 		break;
140 	case 2: /* DPP2 */
141 		c = 0x40 | data << 4 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | 0x03;
142 		parport_write_data(port, c);
143 		break;
144 	case 0:	/* DB9 */
145 		c = ((((data & 2) ? 2 : 0) | ((data & 1) ? 4 : 0)) ^ 0x02) | !powered;
146 		parport_write_control(port, c);
147 		break;
148 	}
149 }
150 
151 /*
152  * gc_saturn_read_sub() reads 4 bit data.
153  */
154 static unsigned char db9_saturn_read_sub(struct parport *port, int type)
155 {
156 	unsigned char data;
157 
158 	if (type) {
159 		/* DPP */
160 		data = parport_read_status(port) ^ 0x80;
161 		return (data & 0x80 ? 1 : 0) | (data & 0x40 ? 2 : 0)
162 		     | (data & 0x20 ? 4 : 0) | (data & 0x10 ? 8 : 0);
163 	} else {
164 		/* DB9 */
165 		data = parport_read_data(port) & 0x0f;
166 		return (data & 0x8 ? 1 : 0) | (data & 0x4 ? 2 : 0)
167 		     | (data & 0x2 ? 4 : 0) | (data & 0x1 ? 8 : 0);
168 	}
169 }
170 
171 /*
172  * db9_saturn_read_analog() sends clock and reads 8 bit data.
173  */
174 static unsigned char db9_saturn_read_analog(struct parport *port, int type, int powered)
175 {
176 	unsigned char data;
177 
178 	db9_saturn_write_sub(port, type, 0, powered, 0);
179 	udelay(DB9_SATURN_DELAY);
180 	data = db9_saturn_read_sub(port, type) << 4;
181 	db9_saturn_write_sub(port, type, 2, powered, 0);
182 	udelay(DB9_SATURN_DELAY);
183 	data |= db9_saturn_read_sub(port, type);
184 	return data;
185 }
186 
187 /*
188  * db9_saturn_read_packet() reads whole saturn packet at connector
189  * and returns device identifier code.
190  */
191 static unsigned char db9_saturn_read_packet(struct parport *port, unsigned char *data, int type, int powered)
192 {
193 	int i, j;
194 	unsigned char tmp;
195 
196 	db9_saturn_write_sub(port, type, 3, powered, 0);
197 	data[0] = db9_saturn_read_sub(port, type);
198 	switch (data[0] & 0x0f) {
199 	case 0xf:
200 		/* 1111  no pad */
201 		return data[0] = 0xff;
202 	case 0x4: case 0x4 | 0x8:
203 		/* ?100 : digital controller */
204 		db9_saturn_write_sub(port, type, 0, powered, 1);
205 		data[2] = db9_saturn_read_sub(port, type) << 4;
206 		db9_saturn_write_sub(port, type, 2, powered, 1);
207 		data[1] = db9_saturn_read_sub(port, type) << 4;
208 		db9_saturn_write_sub(port, type, 1, powered, 1);
209 		data[1] |= db9_saturn_read_sub(port, type);
210 		db9_saturn_write_sub(port, type, 3, powered, 1);
211 		/* data[2] |= db9_saturn_read_sub(port, type); */
212 		data[2] |= data[0];
213 		return data[0] = 0x02;
214 	case 0x1:
215 		/* 0001 : analog controller or multitap */
216 		db9_saturn_write_sub(port, type, 2, powered, 0);
217 		udelay(DB9_SATURN_DELAY);
218 		data[0] = db9_saturn_read_analog(port, type, powered);
219 		if (data[0] != 0x41) {
220 			/* read analog controller */
221 			for (i = 0; i < (data[0] & 0x0f); i++)
222 				data[i + 1] = db9_saturn_read_analog(port, type, powered);
223 			db9_saturn_write_sub(port, type, 3, powered, 0);
224 			return data[0];
225 		} else {
226 			/* read multitap */
227 			if (db9_saturn_read_analog(port, type, powered) != 0x60)
228 				return data[0] = 0xff;
229 			for (i = 0; i < 60; i += 10) {
230 				data[i] = db9_saturn_read_analog(port, type, powered);
231 				if (data[i] != 0xff)
232 					/* read each pad */
233 					for (j = 0; j < (data[i] & 0x0f); j++)
234 						data[i + j + 1] = db9_saturn_read_analog(port, type, powered);
235 			}
236 			db9_saturn_write_sub(port, type, 3, powered, 0);
237 			return 0x41;
238 		}
239 	case 0x0:
240 		/* 0000 : mouse */
241 		db9_saturn_write_sub(port, type, 2, powered, 0);
242 		udelay(DB9_SATURN_DELAY);
243 		tmp = db9_saturn_read_analog(port, type, powered);
244 		if (tmp == 0xff) {
245 			for (i = 0; i < 3; i++)
246 				data[i + 1] = db9_saturn_read_analog(port, type, powered);
247 			db9_saturn_write_sub(port, type, 3, powered, 0);
248 			return data[0] = 0xe3;
249 		}
250 		/* fall through */
251 	default:
252 		return data[0];
253 	}
254 }
255 
256 /*
257  * db9_saturn_report() analyzes packet and reports.
258  */
259 static int db9_saturn_report(unsigned char id, unsigned char data[60], struct input_dev *devs[], int n, int max_pads)
260 {
261 	struct input_dev *dev;
262 	int tmp, i, j;
263 
264 	tmp = (id == 0x41) ? 60 : 10;
265 	for (j = 0; j < tmp && n < max_pads; j += 10, n++) {
266 		dev = devs[n];
267 		switch (data[j]) {
268 		case 0x16: /* multi controller (analog 4 axis) */
269 			input_report_abs(dev, db9_abs[5], data[j + 6]);
270 			/* fall through */
271 		case 0x15: /* mission stick (analog 3 axis) */
272 			input_report_abs(dev, db9_abs[3], data[j + 4]);
273 			input_report_abs(dev, db9_abs[4], data[j + 5]);
274 			/* fall through */
275 		case 0x13: /* racing controller (analog 1 axis) */
276 			input_report_abs(dev, db9_abs[2], data[j + 3]);
277 			/* fall through */
278 		case 0x34: /* saturn keyboard (udlr ZXC ASD QE Esc) */
279 		case 0x02: /* digital pad (digital 2 axis + buttons) */
280 			input_report_abs(dev, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
281 			input_report_abs(dev, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
282 			for (i = 0; i < 9; i++)
283 				input_report_key(dev, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
284 			break;
285 		case 0x19: /* mission stick x2 (analog 6 axis + buttons) */
286 			input_report_abs(dev, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
287 			input_report_abs(dev, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
288 			for (i = 0; i < 9; i++)
289 				input_report_key(dev, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
290 			input_report_abs(dev, db9_abs[2], data[j + 3]);
291 			input_report_abs(dev, db9_abs[3], data[j + 4]);
292 			input_report_abs(dev, db9_abs[4], data[j + 5]);
293 			/*
294 			input_report_abs(dev, db9_abs[8], (data[j + 6] & 128 ? 0 : 1) - (data[j + 6] & 64 ? 0 : 1));
295 			input_report_abs(dev, db9_abs[9], (data[j + 6] & 32 ? 0 : 1) - (data[j + 6] & 16 ? 0 : 1));
296 			*/
297 			input_report_abs(dev, db9_abs[6], data[j + 7]);
298 			input_report_abs(dev, db9_abs[7], data[j + 8]);
299 			input_report_abs(dev, db9_abs[5], data[j + 9]);
300 			break;
301 		case 0xd3: /* sankyo ff (analog 1 axis + stop btn) */
302 			input_report_key(dev, BTN_A, data[j + 3] & 0x80);
303 			input_report_abs(dev, db9_abs[2], data[j + 3] & 0x7f);
304 			break;
305 		case 0xe3: /* shuttle mouse (analog 2 axis + buttons. signed value) */
306 			input_report_key(dev, BTN_START, data[j + 1] & 0x08);
307 			input_report_key(dev, BTN_A, data[j + 1] & 0x04);
308 			input_report_key(dev, BTN_C, data[j + 1] & 0x02);
309 			input_report_key(dev, BTN_B, data[j + 1] & 0x01);
310 			input_report_abs(dev, db9_abs[2], data[j + 2] ^ 0x80);
311 			input_report_abs(dev, db9_abs[3], (0xff-(data[j + 3] ^ 0x80))+1); /* */
312 			break;
313 		case 0xff:
314 		default: /* no pad */
315 			input_report_abs(dev, db9_abs[0], 0);
316 			input_report_abs(dev, db9_abs[1], 0);
317 			for (i = 0; i < 9; i++)
318 				input_report_key(dev, db9_cd32_btn[i], 0);
319 			break;
320 		}
321 	}
322 	return n;
323 }
324 
325 static int db9_saturn(int mode, struct parport *port, struct input_dev *devs[])
326 {
327 	unsigned char id, data[60];
328 	int type, n, max_pads;
329 	int tmp, i;
330 
331 	switch (mode) {
332 	case DB9_SATURN_PAD:
333 		type = 0;
334 		n = 1;
335 		break;
336 	case DB9_SATURN_DPP:
337 		type = 1;
338 		n = 1;
339 		break;
340 	case DB9_SATURN_DPP_2:
341 		type = 1;
342 		n = 2;
343 		break;
344 	default:
345 		return -1;
346 	}
347 	max_pads = min(db9_modes[mode].n_pads, DB9_MAX_DEVICES);
348 	for (tmp = 0, i = 0; i < n; i++) {
349 		id = db9_saturn_read_packet(port, data, type + i, 1);
350 		tmp = db9_saturn_report(id, data, devs, tmp, max_pads);
351 	}
352 	return 0;
353 }
354 
355 static void db9_timer(struct timer_list *t)
356 {
357 	struct db9 *db9 = from_timer(db9, t, timer);
358 	struct parport *port = db9->pd->port;
359 	struct input_dev *dev = db9->dev[0];
360 	struct input_dev *dev2 = db9->dev[1];
361 	int data, i;
362 
363 	switch (db9->mode) {
364 		case DB9_MULTI_0802_2:
365 
366 			data = parport_read_data(port) >> 3;
367 
368 			input_report_abs(dev2, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
369 			input_report_abs(dev2, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
370 			input_report_key(dev2, BTN_TRIGGER, ~data & DB9_FIRE1);
371 			/* fall through */
372 
373 		case DB9_MULTI_0802:
374 
375 			data = parport_read_status(port) >> 3;
376 
377 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
378 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
379 			input_report_key(dev, BTN_TRIGGER, data & DB9_FIRE1);
380 			break;
381 
382 		case DB9_MULTI_STICK:
383 
384 			data = parport_read_data(port);
385 
386 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
387 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
388 			input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
389 			break;
390 
391 		case DB9_MULTI2_STICK:
392 
393 			data = parport_read_data(port);
394 
395 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
396 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
397 			input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
398 			input_report_key(dev, BTN_THUMB,   ~data & DB9_FIRE2);
399 			break;
400 
401 		case DB9_GENESIS_PAD:
402 
403 			parport_write_control(port, DB9_NOSELECT);
404 			data = parport_read_data(port);
405 
406 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
407 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
408 			input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
409 			input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
410 
411 			parport_write_control(port, DB9_NORMAL);
412 			data = parport_read_data(port);
413 
414 			input_report_key(dev, BTN_A,     ~data & DB9_FIRE1);
415 			input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
416 			break;
417 
418 		case DB9_GENESIS5_PAD:
419 
420 			parport_write_control(port, DB9_NOSELECT);
421 			data = parport_read_data(port);
422 
423 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
424 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
425 			input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
426 			input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
427 
428 			parport_write_control(port, DB9_NORMAL);
429 			data = parport_read_data(port);
430 
431 			input_report_key(dev, BTN_A,     ~data & DB9_FIRE1);
432 			input_report_key(dev, BTN_X,     ~data & DB9_FIRE2);
433 			input_report_key(dev, BTN_Y,     ~data & DB9_LEFT);
434 			input_report_key(dev, BTN_START, ~data & DB9_RIGHT);
435 			break;
436 
437 		case DB9_GENESIS6_PAD:
438 
439 			parport_write_control(port, DB9_NOSELECT); /* 1 */
440 			udelay(DB9_GENESIS6_DELAY);
441 			data = parport_read_data(port);
442 
443 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
444 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
445 			input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
446 			input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
447 
448 			parport_write_control(port, DB9_NORMAL);
449 			udelay(DB9_GENESIS6_DELAY);
450 			data = parport_read_data(port);
451 
452 			input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
453 			input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
454 
455 			parport_write_control(port, DB9_NOSELECT); /* 2 */
456 			udelay(DB9_GENESIS6_DELAY);
457 			parport_write_control(port, DB9_NORMAL);
458 			udelay(DB9_GENESIS6_DELAY);
459 			parport_write_control(port, DB9_NOSELECT); /* 3 */
460 			udelay(DB9_GENESIS6_DELAY);
461 			data=parport_read_data(port);
462 
463 			input_report_key(dev, BTN_X,    ~data & DB9_LEFT);
464 			input_report_key(dev, BTN_Y,    ~data & DB9_DOWN);
465 			input_report_key(dev, BTN_Z,    ~data & DB9_UP);
466 			input_report_key(dev, BTN_MODE, ~data & DB9_RIGHT);
467 
468 			parport_write_control(port, DB9_NORMAL);
469 			udelay(DB9_GENESIS6_DELAY);
470 			parport_write_control(port, DB9_NOSELECT); /* 4 */
471 			udelay(DB9_GENESIS6_DELAY);
472 			parport_write_control(port, DB9_NORMAL);
473 			break;
474 
475 		case DB9_SATURN_PAD:
476 		case DB9_SATURN_DPP:
477 		case DB9_SATURN_DPP_2:
478 
479 			db9_saturn(db9->mode, port, db9->dev);
480 			break;
481 
482 		case DB9_CD32_PAD:
483 
484 			data = parport_read_data(port);
485 
486 			input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
487 			input_report_abs(dev, ABS_Y, (data & DB9_DOWN  ? 0 : 1) - (data & DB9_UP   ? 0 : 1));
488 
489 			parport_write_control(port, 0x0a);
490 
491 			for (i = 0; i < 7; i++) {
492 				data = parport_read_data(port);
493 				parport_write_control(port, 0x02);
494 				parport_write_control(port, 0x0a);
495 				input_report_key(dev, db9_cd32_btn[i], ~data & DB9_FIRE2);
496 			}
497 
498 			parport_write_control(port, 0x00);
499 			break;
500 		}
501 
502 	input_sync(dev);
503 
504 	mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
505 }
506 
507 static int db9_open(struct input_dev *dev)
508 {
509 	struct db9 *db9 = input_get_drvdata(dev);
510 	struct parport *port = db9->pd->port;
511 	int err;
512 
513 	err = mutex_lock_interruptible(&db9->mutex);
514 	if (err)
515 		return err;
516 
517 	if (!db9->used++) {
518 		parport_claim(db9->pd);
519 		parport_write_data(port, 0xff);
520 		if (db9_modes[db9->mode].reverse) {
521 			parport_data_reverse(port);
522 			parport_write_control(port, DB9_NORMAL);
523 		}
524 		mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
525 	}
526 
527 	mutex_unlock(&db9->mutex);
528 	return 0;
529 }
530 
531 static void db9_close(struct input_dev *dev)
532 {
533 	struct db9 *db9 = input_get_drvdata(dev);
534 	struct parport *port = db9->pd->port;
535 
536 	mutex_lock(&db9->mutex);
537 	if (!--db9->used) {
538 		del_timer_sync(&db9->timer);
539 		parport_write_control(port, 0x00);
540 		parport_data_forward(port);
541 		parport_release(db9->pd);
542 	}
543 	mutex_unlock(&db9->mutex);
544 }
545 
546 static void db9_attach(struct parport *pp)
547 {
548 	struct db9 *db9;
549 	const struct db9_mode_data *db9_mode;
550 	struct pardevice *pd;
551 	struct input_dev *input_dev;
552 	int i, j, port_idx;
553 	int mode;
554 	struct pardev_cb db9_parport_cb;
555 
556 	for (port_idx = 0; port_idx < DB9_MAX_PORTS; port_idx++) {
557 		if (db9_cfg[port_idx].nargs == 0 ||
558 		    db9_cfg[port_idx].args[DB9_ARG_PARPORT] < 0)
559 			continue;
560 
561 		if (db9_cfg[port_idx].args[DB9_ARG_PARPORT] == pp->number)
562 			break;
563 	}
564 
565 	if (port_idx == DB9_MAX_PORTS) {
566 		pr_debug("Not using parport%d.\n", pp->number);
567 		return;
568 	}
569 
570 	mode = db9_cfg[port_idx].args[DB9_ARG_MODE];
571 
572 	if (mode < 1 || mode >= DB9_MAX_PAD || !db9_modes[mode].n_buttons) {
573 		printk(KERN_ERR "db9.c: Bad device type %d\n", mode);
574 		return;
575 	}
576 
577 	db9_mode = &db9_modes[mode];
578 
579 	if (db9_mode->bidirectional && !(pp->modes & PARPORT_MODE_TRISTATE)) {
580 		printk(KERN_ERR "db9.c: specified parport is not bidirectional\n");
581 		return;
582 	}
583 
584 	memset(&db9_parport_cb, 0, sizeof(db9_parport_cb));
585 	db9_parport_cb.flags = PARPORT_FLAG_EXCL;
586 
587 	pd = parport_register_dev_model(pp, "db9", &db9_parport_cb, port_idx);
588 	if (!pd) {
589 		printk(KERN_ERR "db9.c: parport busy already - lp.o loaded?\n");
590 		return;
591 	}
592 
593 	db9 = kzalloc(sizeof(struct db9), GFP_KERNEL);
594 	if (!db9)
595 		goto err_unreg_pardev;
596 
597 	mutex_init(&db9->mutex);
598 	db9->pd = pd;
599 	db9->mode = mode;
600 	db9->parportno = pp->number;
601 	timer_setup(&db9->timer, db9_timer, 0);
602 
603 	for (i = 0; i < (min(db9_mode->n_pads, DB9_MAX_DEVICES)); i++) {
604 
605 		db9->dev[i] = input_dev = input_allocate_device();
606 		if (!input_dev) {
607 			printk(KERN_ERR "db9.c: Not enough memory for input device\n");
608 			goto err_unreg_devs;
609 		}
610 
611 		snprintf(db9->phys[i], sizeof(db9->phys[i]),
612 			 "%s/input%d", db9->pd->port->name, i);
613 
614 		input_dev->name = db9_mode->name;
615 		input_dev->phys = db9->phys[i];
616 		input_dev->id.bustype = BUS_PARPORT;
617 		input_dev->id.vendor = 0x0002;
618 		input_dev->id.product = mode;
619 		input_dev->id.version = 0x0100;
620 
621 		input_set_drvdata(input_dev, db9);
622 
623 		input_dev->open = db9_open;
624 		input_dev->close = db9_close;
625 
626 		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
627 		for (j = 0; j < db9_mode->n_buttons; j++)
628 			set_bit(db9_mode->buttons[j], input_dev->keybit);
629 		for (j = 0; j < db9_mode->n_axis; j++) {
630 			if (j < 2)
631 				input_set_abs_params(input_dev, db9_abs[j], -1, 1, 0, 0);
632 			else
633 				input_set_abs_params(input_dev, db9_abs[j], 1, 255, 0, 0);
634 		}
635 
636 		if (input_register_device(input_dev))
637 			goto err_free_dev;
638 	}
639 
640 	db9_base[port_idx] = db9;
641 	return;
642 
643  err_free_dev:
644 	input_free_device(db9->dev[i]);
645  err_unreg_devs:
646 	while (--i >= 0)
647 		input_unregister_device(db9->dev[i]);
648 	kfree(db9);
649  err_unreg_pardev:
650 	parport_unregister_device(pd);
651 }
652 
653 static void db9_detach(struct parport *port)
654 {
655 	int i;
656 	struct db9 *db9;
657 
658 	for (i = 0; i < DB9_MAX_PORTS; i++) {
659 		if (db9_base[i] && db9_base[i]->parportno == port->number)
660 			break;
661 	}
662 
663 	if (i == DB9_MAX_PORTS)
664 		return;
665 
666 	db9 = db9_base[i];
667 	db9_base[i] = NULL;
668 
669 	for (i = 0; i < min(db9_modes[db9->mode].n_pads, DB9_MAX_DEVICES); i++)
670 		input_unregister_device(db9->dev[i]);
671 	parport_unregister_device(db9->pd);
672 	kfree(db9);
673 }
674 
675 static struct parport_driver db9_parport_driver = {
676 	.name = "db9",
677 	.match_port = db9_attach,
678 	.detach = db9_detach,
679 	.devmodel = true,
680 };
681 
682 static int __init db9_init(void)
683 {
684 	int i;
685 	int have_dev = 0;
686 
687 	for (i = 0; i < DB9_MAX_PORTS; i++) {
688 		if (db9_cfg[i].nargs == 0 || db9_cfg[i].args[DB9_ARG_PARPORT] < 0)
689 			continue;
690 
691 		if (db9_cfg[i].nargs < 2) {
692 			printk(KERN_ERR "db9.c: Device type must be specified.\n");
693 			return -EINVAL;
694 		}
695 
696 		have_dev = 1;
697 	}
698 
699 	if (!have_dev)
700 		return -ENODEV;
701 
702 	return parport_register_driver(&db9_parport_driver);
703 }
704 
705 static void __exit db9_exit(void)
706 {
707 	parport_unregister_driver(&db9_parport_driver);
708 }
709 
710 module_init(db9_init);
711 module_exit(db9_exit);
712