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