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