Linux v2.6.15-rc7
[pohmelfs.git] / drivers / input / joystick / db9.c
blob499344c72756dcbb6301f4c57101b4c0aa09b54c
1 /*
2 * $Id: db9.c,v 1.13 2002/04/07 20:13:37 vojtech Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
6 * Based on the work of:
7 * Andree Borrmann Mats Sjövall
8 */
11 * Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver for Linux
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.
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.
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
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
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>
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION("Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver");
44 MODULE_LICENSE("GPL");
46 struct db9_config {
47 int args[2];
48 int nargs;
51 #define DB9_MAX_PORTS 3
52 static struct db9_config db9[DB9_MAX_PORTS] __initdata;
54 module_param_array_named(dev, db9[0].args, int, &db9[0].nargs, 0);
55 MODULE_PARM_DESC(dev, "Describes first attached device (<parport#>,<type>)");
56 module_param_array_named(dev2, db9[1].args, int, &db9[0].nargs, 0);
57 MODULE_PARM_DESC(dev2, "Describes second attached device (<parport#>,<type>)");
58 module_param_array_named(dev3, db9[2].args, int, &db9[2].nargs, 0);
59 MODULE_PARM_DESC(dev3, "Describes third attached device (<parport#>,<type>)");
61 __obsolete_setup("db9=");
62 __obsolete_setup("db9_2=");
63 __obsolete_setup("db9_3=");
65 #define DB9_ARG_PARPORT 0
66 #define DB9_ARG_MODE 1
68 #define DB9_MULTI_STICK 0x01
69 #define DB9_MULTI2_STICK 0x02
70 #define DB9_GENESIS_PAD 0x03
71 #define DB9_GENESIS5_PAD 0x05
72 #define DB9_GENESIS6_PAD 0x06
73 #define DB9_SATURN_PAD 0x07
74 #define DB9_MULTI_0802 0x08
75 #define DB9_MULTI_0802_2 0x09
76 #define DB9_CD32_PAD 0x0A
77 #define DB9_SATURN_DPP 0x0B
78 #define DB9_SATURN_DPP_2 0x0C
79 #define DB9_MAX_PAD 0x0D
81 #define DB9_UP 0x01
82 #define DB9_DOWN 0x02
83 #define DB9_LEFT 0x04
84 #define DB9_RIGHT 0x08
85 #define DB9_FIRE1 0x10
86 #define DB9_FIRE2 0x20
87 #define DB9_FIRE3 0x40
88 #define DB9_FIRE4 0x80
90 #define DB9_NORMAL 0x0a
91 #define DB9_NOSELECT 0x08
93 #define DB9_GENESIS6_DELAY 14
94 #define DB9_REFRESH_TIME HZ/100
96 #define DB9_MAX_DEVICES 2
98 struct db9_mode_data {
99 const char *name;
100 const short *buttons;
101 int n_buttons;
102 int n_pads;
103 int n_axis;
104 int bidirectional;
105 int reverse;
108 struct db9 {
109 struct input_dev *dev[DB9_MAX_DEVICES];
110 struct timer_list timer;
111 struct pardevice *pd;
112 int mode;
113 int used;
114 struct semaphore sem;
115 char phys[DB9_MAX_DEVICES][32];
118 static struct db9 *db9_base[3];
120 static const short db9_multi_btn[] = { BTN_TRIGGER, BTN_THUMB };
121 static const short db9_genesis_btn[] = { BTN_START, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_MODE };
122 static const short db9_cd32_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START };
123 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 };
125 static const struct db9_mode_data db9_modes[] = {
126 { NULL, NULL, 0, 0, 0, 0, 0 },
127 { "Multisystem joystick", db9_multi_btn, 1, 1, 2, 1, 1 },
128 { "Multisystem joystick (2 fire)", db9_multi_btn, 2, 1, 2, 1, 1 },
129 { "Genesis pad", db9_genesis_btn, 4, 1, 2, 1, 1 },
130 { NULL, NULL, 0, 0, 0, 0, 0 },
131 { "Genesis 5 pad", db9_genesis_btn, 6, 1, 2, 1, 1 },
132 { "Genesis 6 pad", db9_genesis_btn, 8, 1, 2, 1, 1 },
133 { "Saturn pad", db9_cd32_btn, 9, 6, 7, 0, 1 },
134 { "Multisystem (0.8.0.2) joystick", db9_multi_btn, 1, 1, 2, 1, 1 },
135 { "Multisystem (0.8.0.2-dual) joystick", db9_multi_btn, 1, 2, 2, 1, 1 },
136 { "Amiga CD-32 pad", db9_cd32_btn, 7, 1, 2, 1, 1 },
137 { "Saturn dpp", db9_cd32_btn, 9, 6, 7, 0, 0 },
138 { "Saturn dpp dual", db9_cd32_btn, 9, 12, 7, 0, 0 },
142 * Saturn controllers
144 #define DB9_SATURN_DELAY 300
145 static const int db9_saturn_byte[] = { 1, 1, 1, 2, 2, 2, 2, 2, 1 };
146 static const unsigned char db9_saturn_mask[] = { 0x04, 0x01, 0x02, 0x40, 0x20, 0x10, 0x08, 0x80, 0x08 };
149 * db9_saturn_write_sub() writes 2 bit data.
151 static void db9_saturn_write_sub(struct parport *port, int type, unsigned char data, int powered, int pwr_sub)
153 unsigned char c;
155 switch (type) {
156 case 1: /* DPP1 */
157 c = 0x80 | 0x30 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | data;
158 parport_write_data(port, c);
159 break;
160 case 2: /* DPP2 */
161 c = 0x40 | data << 4 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | 0x03;
162 parport_write_data(port, c);
163 break;
164 case 0: /* DB9 */
165 c = ((((data & 2) ? 2 : 0) | ((data & 1) ? 4 : 0)) ^ 0x02) | !powered;
166 parport_write_control(port, c);
167 break;
172 * gc_saturn_read_sub() reads 4 bit data.
174 static unsigned char db9_saturn_read_sub(struct parport *port, int type)
176 unsigned char data;
178 if (type) {
179 /* DPP */
180 data = parport_read_status(port) ^ 0x80;
181 return (data & 0x80 ? 1 : 0) | (data & 0x40 ? 2 : 0)
182 | (data & 0x20 ? 4 : 0) | (data & 0x10 ? 8 : 0);
183 } else {
184 /* DB9 */
185 data = parport_read_data(port) & 0x0f;
186 return (data & 0x8 ? 1 : 0) | (data & 0x4 ? 2 : 0)
187 | (data & 0x2 ? 4 : 0) | (data & 0x1 ? 8 : 0);
192 * db9_saturn_read_analog() sends clock and reads 8 bit data.
194 static unsigned char db9_saturn_read_analog(struct parport *port, int type, int powered)
196 unsigned char data;
198 db9_saturn_write_sub(port, type, 0, powered, 0);
199 udelay(DB9_SATURN_DELAY);
200 data = db9_saturn_read_sub(port, type) << 4;
201 db9_saturn_write_sub(port, type, 2, powered, 0);
202 udelay(DB9_SATURN_DELAY);
203 data |= db9_saturn_read_sub(port, type);
204 return data;
208 * db9_saturn_read_packet() reads whole saturn packet at connector
209 * and returns device identifier code.
211 static unsigned char db9_saturn_read_packet(struct parport *port, unsigned char *data, int type, int powered)
213 int i, j;
214 unsigned char tmp;
216 db9_saturn_write_sub(port, type, 3, powered, 0);
217 data[0] = db9_saturn_read_sub(port, type);
218 switch (data[0] & 0x0f) {
219 case 0xf:
220 /* 1111 no pad */
221 return data[0] = 0xff;
222 case 0x4: case 0x4 | 0x8:
223 /* ?100 : digital controller */
224 db9_saturn_write_sub(port, type, 0, powered, 1);
225 data[2] = db9_saturn_read_sub(port, type) << 4;
226 db9_saturn_write_sub(port, type, 2, powered, 1);
227 data[1] = db9_saturn_read_sub(port, type) << 4;
228 db9_saturn_write_sub(port, type, 1, powered, 1);
229 data[1] |= db9_saturn_read_sub(port, type);
230 db9_saturn_write_sub(port, type, 3, powered, 1);
231 /* data[2] |= db9_saturn_read_sub(port, type); */
232 data[2] |= data[0];
233 return data[0] = 0x02;
234 case 0x1:
235 /* 0001 : analog controller or multitap */
236 db9_saturn_write_sub(port, type, 2, powered, 0);
237 udelay(DB9_SATURN_DELAY);
238 data[0] = db9_saturn_read_analog(port, type, powered);
239 if (data[0] != 0x41) {
240 /* read analog controller */
241 for (i = 0; i < (data[0] & 0x0f); i++)
242 data[i + 1] = db9_saturn_read_analog(port, type, powered);
243 db9_saturn_write_sub(port, type, 3, powered, 0);
244 return data[0];
245 } else {
246 /* read multitap */
247 if (db9_saturn_read_analog(port, type, powered) != 0x60)
248 return data[0] = 0xff;
249 for (i = 0; i < 60; i += 10) {
250 data[i] = db9_saturn_read_analog(port, type, powered);
251 if (data[i] != 0xff)
252 /* read each pad */
253 for (j = 0; j < (data[i] & 0x0f); j++)
254 data[i + j + 1] = db9_saturn_read_analog(port, type, powered);
256 db9_saturn_write_sub(port, type, 3, powered, 0);
257 return 0x41;
259 case 0x0:
260 /* 0000 : mouse */
261 db9_saturn_write_sub(port, type, 2, powered, 0);
262 udelay(DB9_SATURN_DELAY);
263 tmp = db9_saturn_read_analog(port, type, powered);
264 if (tmp == 0xff) {
265 for (i = 0; i < 3; i++)
266 data[i + 1] = db9_saturn_read_analog(port, type, powered);
267 db9_saturn_write_sub(port, type, 3, powered, 0);
268 return data[0] = 0xe3;
270 default:
271 return data[0];
276 * db9_saturn_report() analyzes packet and reports.
278 static int db9_saturn_report(unsigned char id, unsigned char data[60], struct input_dev *dev, int n, int max_pads)
280 int tmp, i, j;
282 tmp = (id == 0x41) ? 60 : 10;
283 for (j = 0; (j < tmp) && (n < max_pads); j += 10, n++) {
284 switch (data[j]) {
285 case 0x16: /* multi controller (analog 4 axis) */
286 input_report_abs(dev + n, db9_abs[5], data[j + 6]);
287 case 0x15: /* mission stick (analog 3 axis) */
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 case 0x13: /* racing controller (analog 1 axis) */
291 input_report_abs(dev + n, db9_abs[2], data[j + 3]);
292 case 0x34: /* saturn keyboard (udlr ZXC ASD QE Esc) */
293 case 0x02: /* digital pad (digital 2 axis + buttons) */
294 input_report_abs(dev + n, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
295 input_report_abs(dev + n, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
296 for (i = 0; i < 9; i++)
297 input_report_key(dev + n, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
298 break;
299 case 0x19: /* mission stick x2 (analog 6 axis + buttons) */
300 input_report_abs(dev + n, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
301 input_report_abs(dev + n, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
302 for (i = 0; i < 9; i++)
303 input_report_key(dev + n, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
304 input_report_abs(dev + n, db9_abs[2], data[j + 3]);
305 input_report_abs(dev + n, db9_abs[3], data[j + 4]);
306 input_report_abs(dev + n, db9_abs[4], data[j + 5]);
308 input_report_abs(dev + n, db9_abs[8], (data[j + 6] & 128 ? 0 : 1) - (data[j + 6] & 64 ? 0 : 1));
309 input_report_abs(dev + n, db9_abs[9], (data[j + 6] & 32 ? 0 : 1) - (data[j + 6] & 16 ? 0 : 1));
311 input_report_abs(dev + n, db9_abs[6], data[j + 7]);
312 input_report_abs(dev + n, db9_abs[7], data[j + 8]);
313 input_report_abs(dev + n, db9_abs[5], data[j + 9]);
314 break;
315 case 0xd3: /* sankyo ff (analog 1 axis + stop btn) */
316 input_report_key(dev + n, BTN_A, data[j + 3] & 0x80);
317 input_report_abs(dev + n, db9_abs[2], data[j + 3] & 0x7f);
318 break;
319 case 0xe3: /* shuttle mouse (analog 2 axis + buttons. signed value) */
320 input_report_key(dev + n, BTN_START, data[j + 1] & 0x08);
321 input_report_key(dev + n, BTN_A, data[j + 1] & 0x04);
322 input_report_key(dev + n, BTN_C, data[j + 1] & 0x02);
323 input_report_key(dev + n, BTN_B, data[j + 1] & 0x01);
324 input_report_abs(dev + n, db9_abs[2], data[j + 2] ^ 0x80);
325 input_report_abs(dev + n, db9_abs[3], (0xff-(data[j + 3] ^ 0x80))+1); /* */
326 break;
327 case 0xff:
328 default: /* no pad */
329 input_report_abs(dev + n, db9_abs[0], 0);
330 input_report_abs(dev + n, db9_abs[1], 0);
331 for (i = 0; i < 9; i++)
332 input_report_key(dev + n, db9_cd32_btn[i], 0);
333 break;
336 return n;
339 static int db9_saturn(int mode, struct parport *port, struct input_dev *dev)
341 unsigned char id, data[60];
342 int type, n, max_pads;
343 int tmp, i;
345 switch (mode) {
346 case DB9_SATURN_PAD:
347 type = 0;
348 n = 1;
349 break;
350 case DB9_SATURN_DPP:
351 type = 1;
352 n = 1;
353 break;
354 case DB9_SATURN_DPP_2:
355 type = 1;
356 n = 2;
357 break;
358 default:
359 return -1;
361 max_pads = min(db9_modes[mode].n_pads, DB9_MAX_DEVICES);
362 for (tmp = 0, i = 0; i < n; i++) {
363 id = db9_saturn_read_packet(port, data, type + i, 1);
364 tmp = db9_saturn_report(id, data, dev, tmp, max_pads);
366 return 0;
369 static void db9_timer(unsigned long private)
371 struct db9 *db9 = (void *) private;
372 struct parport *port = db9->pd->port;
373 struct input_dev *dev = db9->dev[0];
374 struct input_dev *dev2 = db9->dev[1];
375 int data, i;
377 switch (db9->mode) {
378 case DB9_MULTI_0802_2:
380 data = parport_read_data(port) >> 3;
382 input_report_abs(dev2, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
383 input_report_abs(dev2, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
384 input_report_key(dev2, BTN_TRIGGER, ~data & DB9_FIRE1);
386 case DB9_MULTI_0802:
388 data = parport_read_status(port) >> 3;
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 break;
395 case DB9_MULTI_STICK:
397 data = parport_read_data(port);
399 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
400 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
401 input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
402 break;
404 case DB9_MULTI2_STICK:
406 data = parport_read_data(port);
408 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
409 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
410 input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
411 input_report_key(dev, BTN_THUMB, ~data & DB9_FIRE2);
412 break;
414 case DB9_GENESIS_PAD:
416 parport_write_control(port, DB9_NOSELECT);
417 data = parport_read_data(port);
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);
424 parport_write_control(port, DB9_NORMAL);
425 data = parport_read_data(port);
427 input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
428 input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
429 break;
431 case DB9_GENESIS5_PAD:
433 parport_write_control(port, DB9_NOSELECT);
434 data = parport_read_data(port);
436 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
437 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
438 input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
439 input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
441 parport_write_control(port, DB9_NORMAL);
442 data = parport_read_data(port);
444 input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
445 input_report_key(dev, BTN_X, ~data & DB9_FIRE2);
446 input_report_key(dev, BTN_Y, ~data & DB9_LEFT);
447 input_report_key(dev, BTN_START, ~data & DB9_RIGHT);
448 break;
450 case DB9_GENESIS6_PAD:
452 parport_write_control(port, DB9_NOSELECT); /* 1 */
453 udelay(DB9_GENESIS6_DELAY);
454 data = parport_read_data(port);
456 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
457 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
458 input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
459 input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
461 parport_write_control(port, DB9_NORMAL);
462 udelay(DB9_GENESIS6_DELAY);
463 data = parport_read_data(port);
465 input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
466 input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
468 parport_write_control(port, DB9_NOSELECT); /* 2 */
469 udelay(DB9_GENESIS6_DELAY);
470 parport_write_control(port, DB9_NORMAL);
471 udelay(DB9_GENESIS6_DELAY);
472 parport_write_control(port, DB9_NOSELECT); /* 3 */
473 udelay(DB9_GENESIS6_DELAY);
474 data=parport_read_data(port);
476 input_report_key(dev, BTN_X, ~data & DB9_LEFT);
477 input_report_key(dev, BTN_Y, ~data & DB9_DOWN);
478 input_report_key(dev, BTN_Z, ~data & DB9_UP);
479 input_report_key(dev, BTN_MODE, ~data & DB9_RIGHT);
481 parport_write_control(port, DB9_NORMAL);
482 udelay(DB9_GENESIS6_DELAY);
483 parport_write_control(port, DB9_NOSELECT); /* 4 */
484 udelay(DB9_GENESIS6_DELAY);
485 parport_write_control(port, DB9_NORMAL);
486 break;
488 case DB9_SATURN_PAD:
489 case DB9_SATURN_DPP:
490 case DB9_SATURN_DPP_2:
492 db9_saturn(db9->mode, port, dev);
493 break;
495 case DB9_CD32_PAD:
497 data = parport_read_data(port);
499 input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
500 input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
502 parport_write_control(port, 0x0a);
504 for (i = 0; i < 7; i++) {
505 data = parport_read_data(port);
506 parport_write_control(port, 0x02);
507 parport_write_control(port, 0x0a);
508 input_report_key(dev, db9_cd32_btn[i], ~data & DB9_FIRE2);
511 parport_write_control(port, 0x00);
512 break;
515 input_sync(dev);
517 mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
520 static int db9_open(struct input_dev *dev)
522 struct db9 *db9 = dev->private;
523 struct parport *port = db9->pd->port;
524 int err;
526 err = down_interruptible(&db9->sem);
527 if (err)
528 return err;
530 if (!db9->used++) {
531 parport_claim(db9->pd);
532 parport_write_data(port, 0xff);
533 if (db9_modes[db9->mode].reverse) {
534 parport_data_reverse(port);
535 parport_write_control(port, DB9_NORMAL);
537 mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
540 up(&db9->sem);
541 return 0;
544 static void db9_close(struct input_dev *dev)
546 struct db9 *db9 = dev->private;
547 struct parport *port = db9->pd->port;
549 down(&db9->sem);
550 if (!--db9->used) {
551 del_timer_sync(&db9->timer);
552 parport_write_control(port, 0x00);
553 parport_data_forward(port);
554 parport_release(db9->pd);
556 up(&db9->sem);
559 static struct db9 __init *db9_probe(int parport, int mode)
561 struct db9 *db9;
562 const struct db9_mode_data *db9_mode;
563 struct parport *pp;
564 struct pardevice *pd;
565 struct input_dev *input_dev;
566 int i, j;
567 int err;
569 if (mode < 1 || mode >= DB9_MAX_PAD || !db9_modes[mode].n_buttons) {
570 printk(KERN_ERR "db9.c: Bad device type %d\n", mode);
571 err = -EINVAL;
572 goto err_out;
575 db9_mode = &db9_modes[mode];
577 pp = parport_find_number(parport);
578 if (!pp) {
579 printk(KERN_ERR "db9.c: no such parport\n");
580 err = -ENODEV;
581 goto err_out;
584 if (db9_mode[mode].bidirectional && !(pp->modes & PARPORT_MODE_TRISTATE)) {
585 printk(KERN_ERR "db9.c: specified parport is not bidirectional\n");
586 err = -EINVAL;
587 goto err_put_pp;
590 pd = parport_register_device(pp, "db9", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
591 if (!pd) {
592 printk(KERN_ERR "db9.c: parport busy already - lp.o loaded?\n");
593 err = -EBUSY;
594 goto err_put_pp;
597 db9 = kzalloc(sizeof(struct db9), GFP_KERNEL);
598 if (!db9) {
599 printk(KERN_ERR "db9.c: Not enough memory\n");
600 err = -ENOMEM;
601 goto err_unreg_pardev;
604 init_MUTEX(&db9->sem);
605 db9->pd = pd;
606 db9->mode = mode;
607 init_timer(&db9->timer);
608 db9->timer.data = (long) db9;
609 db9->timer.function = db9_timer;
611 for (i = 0; i < (min(db9_mode->n_pads, DB9_MAX_DEVICES)); i++) {
613 db9->dev[i] = input_dev = input_allocate_device();
614 if (!input_dev) {
615 printk(KERN_ERR "db9.c: Not enough memory for input device\n");
616 err = -ENOMEM;
617 goto err_free_devs;
620 sprintf(db9->phys[i], "%s/input%d", db9->pd->port->name, i);
622 input_dev->name = db9_mode->name;
623 input_dev->phys = db9->phys[i];
624 input_dev->id.bustype = BUS_PARPORT;
625 input_dev->id.vendor = 0x0002;
626 input_dev->id.product = mode;
627 input_dev->id.version = 0x0100;
628 input_dev->private = db9;
630 input_dev->open = db9_open;
631 input_dev->close = db9_close;
633 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
634 for (j = 0; j < db9_mode->n_buttons; j++)
635 set_bit(db9_mode->buttons[j], input_dev->keybit);
636 for (j = 0; j < db9_mode->n_axis; j++) {
637 if (j < 2)
638 input_set_abs_params(input_dev, db9_abs[j], -1, 1, 0, 0);
639 else
640 input_set_abs_params(input_dev, db9_abs[j], 1, 255, 0, 0);
643 input_register_device(input_dev);
646 parport_put_port(pp);
647 return db9;
649 err_free_devs:
650 while (--i >= 0)
651 input_unregister_device(db9->dev[i]);
652 kfree(db9);
653 err_unreg_pardev:
654 parport_unregister_device(pd);
655 err_put_pp:
656 parport_put_port(pp);
657 err_out:
658 return ERR_PTR(err);
661 static void __exit db9_remove(struct db9 *db9)
663 int i;
665 for (i = 0; i < min(db9_modes[db9->mode].n_pads, DB9_MAX_DEVICES); i++)
666 input_unregister_device(db9->dev[i]);
667 parport_unregister_device(db9->pd);
668 kfree(db9);
671 static int __init db9_init(void)
673 int i;
674 int have_dev = 0;
675 int err = 0;
677 for (i = 0; i < DB9_MAX_PORTS; i++) {
678 if (db9[i].nargs == 0 || db9[i].args[DB9_ARG_PARPORT] < 0)
679 continue;
681 if (db9[i].nargs < 2) {
682 printk(KERN_ERR "db9.c: Device type must be specified.\n");
683 err = -EINVAL;
684 break;
687 db9_base[i] = db9_probe(db9[i].args[DB9_ARG_PARPORT],
688 db9[i].args[DB9_ARG_MODE]);
689 if (IS_ERR(db9_base[i])) {
690 err = PTR_ERR(db9_base[i]);
691 break;
694 have_dev = 1;
697 if (err) {
698 while (--i >= 0)
699 db9_remove(db9_base[i]);
700 return err;
703 return have_dev ? 0 : -ENODEV;
706 static void __exit db9_exit(void)
708 int i;
710 for (i = 0; i < DB9_MAX_PORTS; i++)
711 if (db9_base[i])
712 db9_remove(db9_base[i]);
715 module_init(db9_init);
716 module_exit(db9_exit);