2 * joy-db9.c Version 0.6V
4 * Copyright (c) 1998 Andree Borrmann
5 * Copyright (c) 1999 Vojtech Pavlik
11 * This is a module for the Linux joystick driver, supporting
12 * console (Atari, Amstrad, Commodore, Amiga, Sega) joysticks
13 * and gamepads connected to the parallel port.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * Should you need to contact me, the author, you can do so either by
32 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
33 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
37 #include <asm/system.h>
38 #include <linux/errno.h>
39 #include <linux/ioport.h>
40 #include <linux/joystick.h>
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/string.h>
44 #include <linux/delay.h>
45 #include <linux/init.h>
47 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
48 MODULE_PARM(js_db9
, "2i");
49 MODULE_PARM(js_db9_2
, "2i");
50 MODULE_PARM(js_db9_3
, "2i");
52 #define JS_MULTI_STICK 0x01
53 #define JS_MULTI2_STICK 0x02
54 #define JS_GENESIS_PAD 0x03
55 #define JS_GENESIS5_PAD 0x05
56 #define JS_GENESIS6_PAD 0x06
57 #define JS_SATURN_PAD 0x07
58 #define JS_MULTI_0802 0x08
59 #define JS_MULTI_0802_2 0x09
60 #define JS_MAX_PAD 0x0A
62 #define JS_DB9_UP 0x01
63 #define JS_DB9_DOWN 0x02
64 #define JS_DB9_LEFT 0x04
65 #define JS_DB9_RIGHT 0x08
66 #define JS_DB9_FIRE1 0x10
67 #define JS_DB9_FIRE2 0x20
68 #define JS_DB9_FIRE3 0x40
69 #define JS_DB9_FIRE4 0x80
71 #define JS_DB9_NORMAL 0x2a
72 #define JS_DB9_NOSELECT 0x28
74 #define JS_DB9_SATURN0 0x20
75 #define JS_DB9_SATURN1 0x22
76 #define JS_DB9_SATURN2 0x24
77 #define JS_DB9_SATURN3 0x26
79 #define JS_GENESIS6_DELAY 14
81 static struct js_port
* js_db9_port
= NULL
;
83 static int js_db9
[] __initdata
= { -1, 0 };
84 static int js_db9_2
[] __initdata
= { -1, 0 };
85 static int js_db9_3
[] __initdata
= { -1, 0 };
88 struct pardevice
*port
; /* parport device */
89 int mode
; /* pad mode */
93 * js_db9_read() reads and analyzes db9 joystick data.
96 static int js_db9_read(void *xinfo
, int **axes
, int **buttons
)
98 struct js_db9_info
*info
= xinfo
;
103 case JS_MULTI_0802_2
:
105 data
= JS_PAR_DATA_IN(info
->port
) >> 3;
107 axes
[1][1] = (data
&JS_DB9_DOWN
?0:1) - (data
&JS_DB9_UP
?0:1);
108 axes
[1][0] = (data
&JS_DB9_RIGHT
?0:1) - (data
&JS_DB9_LEFT
?0:1);
110 buttons
[1][0] = (data
&JS_DB9_FIRE1
?0:1);
114 data
= JS_PAR_STATUS(info
->port
) >> 3;
116 axes
[0][1] = (data
&JS_DB9_DOWN
?0:1) - (data
&JS_DB9_UP
?0:1);
117 axes
[0][0] = (data
&JS_DB9_RIGHT
?0:1) - (data
&JS_DB9_LEFT
?0:1);
119 buttons
[0][0] = (data
&JS_DB9_FIRE1
?1:0);
125 data
= JS_PAR_DATA_IN(info
->port
);
127 axes
[0][1] = (data
&JS_DB9_DOWN
?0:1) - (data
&JS_DB9_UP
?0:1);
128 axes
[0][0] = (data
&JS_DB9_RIGHT
?0:1) - (data
&JS_DB9_LEFT
?0:1);
130 buttons
[0][0] = (data
&JS_DB9_FIRE1
?0:1);
134 case JS_MULTI2_STICK
:
136 data
=JS_PAR_DATA_IN(info
->port
);
138 axes
[0][1] = (data
&JS_DB9_DOWN
?0:1) - (data
&JS_DB9_UP
?0:1);
139 axes
[0][0] = (data
&JS_DB9_RIGHT
?0:1) - (data
&JS_DB9_LEFT
?0:1);
141 buttons
[0][0] = (data
&JS_DB9_FIRE1
?0:1) | (data
&JS_DB9_FIRE2
?0:2);
147 JS_PAR_CTRL_OUT(JS_DB9_NOSELECT
, info
->port
);
148 data
= JS_PAR_DATA_IN(info
->port
);
150 axes
[0][1] = (data
&JS_DB9_DOWN
?0:1) - (data
&JS_DB9_UP
?0:1);
151 axes
[0][0] = (data
&JS_DB9_RIGHT
?0:1) - (data
&JS_DB9_LEFT
?0:1);
153 buttons
[0][0] = (data
&JS_DB9_FIRE1
?0:2) | (data
&JS_DB9_FIRE2
?0:4);
155 JS_PAR_CTRL_OUT(JS_DB9_NORMAL
,info
->port
);
156 data
=JS_PAR_DATA_IN(info
->port
);
158 buttons
[0][0] |= (data
&JS_DB9_FIRE1
?0:1) | (data
&JS_DB9_FIRE2
?0:8);
162 case JS_GENESIS5_PAD
:
164 JS_PAR_CTRL_OUT(JS_DB9_NOSELECT
,info
->port
);
165 data
=JS_PAR_DATA_IN(info
->port
);
167 axes
[0][1] = (data
&JS_DB9_DOWN
?0:1) - (data
&JS_DB9_UP
?0:1);
168 axes
[0][0] = (data
&JS_DB9_RIGHT
?0:1) - (data
&JS_DB9_LEFT
?0:1);
170 buttons
[0][0] = (data
&JS_DB9_FIRE1
?0:0x02) | (data
&JS_DB9_FIRE2
?0:0x04);
172 JS_PAR_CTRL_OUT(JS_DB9_NORMAL
, info
->port
);
173 data
=JS_PAR_DATA_IN(info
->port
);
175 buttons
[0][0] |= (data
&JS_DB9_FIRE1
?0:0x01) | (data
&JS_DB9_FIRE2
?0:0x08) |
176 (data
&JS_DB9_LEFT
?0:0x10) | (data
&JS_DB9_RIGHT
?0:0x20);
179 case JS_GENESIS6_PAD
:
181 JS_PAR_CTRL_OUT(JS_DB9_NOSELECT
,info
->port
); /* 1 */
182 udelay(JS_GENESIS6_DELAY
);
183 data
=JS_PAR_DATA_IN(info
->port
);
185 axes
[0][1] = (data
&JS_DB9_DOWN
?0:1) - (data
&JS_DB9_UP
?0:1);
186 axes
[0][0] = (data
&JS_DB9_RIGHT
?0:1) - (data
&JS_DB9_LEFT
?0:1);
188 buttons
[0][0] = (data
&JS_DB9_FIRE1
?0:0x02) | (data
&JS_DB9_FIRE2
?0:0x04);
190 JS_PAR_CTRL_OUT(JS_DB9_NORMAL
, info
->port
);
191 udelay(JS_GENESIS6_DELAY
);
192 data
=JS_PAR_DATA_IN(info
->port
);
194 buttons
[0][0] |= (data
&JS_DB9_FIRE1
?0:0x01) | (data
&JS_DB9_FIRE2
?0:0x08);
196 JS_PAR_CTRL_OUT(JS_DB9_NOSELECT
, info
->port
); /* 2 */
197 udelay(JS_GENESIS6_DELAY
);
198 JS_PAR_CTRL_OUT(JS_DB9_NORMAL
, info
->port
);
199 udelay(JS_GENESIS6_DELAY
);
200 JS_PAR_CTRL_OUT(JS_DB9_NOSELECT
, info
->port
); /* 3 */
201 udelay(JS_GENESIS6_DELAY
);
202 data
=JS_PAR_DATA_IN(info
->port
);
204 buttons
[0][0] |= (data
&JS_DB9_LEFT
?0:0x10) | (data
&JS_DB9_DOWN
?0:0x20) |
205 (data
&JS_DB9_UP
?0:0x40) | (data
&JS_DB9_RIGHT
?0:0x80);
207 JS_PAR_CTRL_OUT(JS_DB9_NORMAL
, info
->port
);
208 udelay(JS_GENESIS6_DELAY
);
209 JS_PAR_CTRL_OUT(JS_DB9_NOSELECT
, info
->port
); /* 4 */
210 udelay(JS_GENESIS6_DELAY
);
211 JS_PAR_CTRL_OUT(JS_DB9_NORMAL
, info
->port
);
217 JS_PAR_CTRL_OUT(JS_DB9_SATURN0
, info
->port
);
218 data
= JS_PAR_DATA_IN(info
->port
);
220 buttons
[0][0] = (data
&JS_DB9_UP
?0:0x20) | (data
&JS_DB9_DOWN
?0:0x10) |
221 (data
&JS_DB9_LEFT
?0:0x08) | (data
&JS_DB9_RIGHT
?0:0x40);
223 JS_PAR_CTRL_OUT(JS_DB9_SATURN2
, info
->port
);
224 data
= JS_PAR_DATA_IN(info
->port
);
226 axes
[0][1] = (data
&JS_DB9_DOWN
?0:1) - (data
&JS_DB9_UP
?0:1);
227 axes
[0][0] = (data
&JS_DB9_RIGHT
?0:1) - (data
&JS_DB9_LEFT
?0:1);
229 JS_PAR_CTRL_OUT(JS_DB9_NORMAL
, info
->port
);
230 data
= JS_PAR_DATA_IN(info
->port
);
232 buttons
[0][0] |= (data
&JS_DB9_UP
?0:0x02) | (data
&JS_DB9_DOWN
?0:0x04) |
233 (data
&JS_DB9_LEFT
?0:0x01) | (data
&JS_DB9_RIGHT
?0:0x80);
246 * open callback: claim parport.
247 * FIXME: race possible.
250 int js_db9_open(struct js_dev
*dev
)
252 struct js_db9_info
*info
= dev
->port
->info
;
255 if (parport_claim(info
->port
)) return -EBUSY
;
257 JS_PAR_DATA_OUT(0xff, info
->port
);
258 if (info
->mode
!= JS_MULTI_0802
)
259 JS_PAR_ECTRL_OUT(0x35,info
->port
); /* enable PS/2 mode: */
260 JS_PAR_CTRL_OUT(JS_DB9_NORMAL
,info
->port
); /* reverse direction, enable Select signal */
268 * close callback: release parport
271 int js_db9_close(struct js_dev
*dev
)
273 struct js_db9_info
*info
= dev
->port
->info
;
279 JS_PAR_CTRL_OUT(0x00,info
->port
); /* normal direction */
280 if (info
->mode
!= JS_MULTI_0802
)
281 JS_PAR_ECTRL_OUT(0x15,info
->port
); /* enable normal mode */
283 parport_release(info
->port
);
289 void cleanup_module(void)
291 struct js_db9_info
*info
;
294 while (js_db9_port
) {
295 info
= js_db9_port
->info
;
297 for (i
= 0; i
< js_db9_port
->ndevs
; i
++)
298 if (js_db9_port
->devs
[i
])
299 js_unregister_device(js_db9_port
->devs
[i
]);
300 parport_unregister_device(info
->port
);
301 js_db9_port
= js_unregister_port(js_db9_port
);
308 * js_db9_init_corr() initializes correction values of
312 static void __init
js_db9_init_corr(struct js_corr
*corr
)
316 for (i
= 0; i
< 2; i
++) {
317 corr
[i
].type
= JS_CORR_BROKEN
;
321 corr
[i
].coef
[2] = (1 << 29);
322 corr
[i
].coef
[3] = (1 << 29);
327 * js_db9_probe() probes for db9 gamepads.
330 static struct js_port __init
*js_db9_probe(int *config
, struct js_port
*port
)
332 struct js_db9_info info
;
335 char buttons
[JS_MAX_PAD
] = {0,1,2,4,0,6,8,8,1,1};
336 char *name
[JS_MAX_PAD
] = {NULL
, "Multisystem joystick", "Multisystem joystick (2 fire)", "Genesis pad",
337 NULL
, "Genesis 5 pad", "Genesis 6 pad", "Saturn pad", "Multisystem (0.8.0.2) joystick",
338 "Multisystem (0.8.0.2-dual) joystick"};
340 if (config
[0] < 0) return port
;
341 if (config
[1] < 0 || config
[1] >= JS_MAX_PAD
|| !name
[config
[1]]) return port
;
343 info
.mode
= config
[1];
345 if (config
[0] > 0x10)
346 for (pp
=parport_enumerate(); pp
&& (pp
->base
!=config
[0]); pp
=pp
->next
);
348 for (pp
=parport_enumerate(); pp
&& (config
[0]>0); pp
=pp
->next
) config
[0]--;
351 printk(KERN_ERR
"joy-db9: no such parport\n");
355 if (!(pp
->modes
& (PARPORT_MODE_PCPS2
| PARPORT_MODE_PCECPPS2
)) && info
.mode
!= JS_MULTI_0802
) {
356 printk(KERN_ERR
"js-db9: specified parport is not bidirectional\n");
360 info
.port
= parport_register_device(pp
, "joystick (db9)", NULL
, NULL
, NULL
, PARPORT_DEV_EXCL
, NULL
);
364 port
= js_register_port(port
, &info
, 1 + (info
.mode
== JS_MULTI_0802_2
), sizeof(struct js_db9_info
), js_db9_read
);
366 for (i
= 0; i
< 1 + (info
.mode
== JS_MULTI_0802_2
); i
++) {
367 printk(KERN_INFO
"js%d: %s on %s\n",
368 js_register_device(port
, i
, 2, buttons
[info
.mode
], name
[info
.mode
], NULL
, js_db9_open
, js_db9_close
),
369 name
[info
.mode
], info
.port
->port
->name
);
371 js_db9_init_corr(port
->corr
[i
]);
379 int __init
js_db9_setup(SETUP_PARAM
)
383 for (i
= 0; i
<= ints
[0] && i
< 2; i
++) js_db9
[i
] = ints
[i
+1];
386 int __init
js_db9_setup_2(SETUP_PARAM
)
390 for (i
= 0; i
<= ints
[0] && i
< 2; i
++) js_db9_2
[i
] = ints
[i
+1];
393 int __init
js_db9_setup_3(SETUP_PARAM
)
397 for (i
= 0; i
<= ints
[0] && i
< 2; i
++) js_db9_3
[i
] = ints
[i
+1];
400 __setup("js_db9=", js_db9_setup
);
401 __setup("js_db9_2=", js_db9_setup_2
);
402 __setup("js_db9_3=", js_db9_setup_3
);
406 int init_module(void)
408 int __init
js_db9_init(void)
411 js_db9_port
= js_db9_probe(js_db9
, js_db9_port
);
412 js_db9_port
= js_db9_probe(js_db9_2
, js_db9_port
);
413 js_db9_port
= js_db9_probe(js_db9_3
, js_db9_port
);
415 if (js_db9_port
) return 0;
418 printk(KERN_WARNING
"joy-db9: no joysticks specified\n");