2 * $Id: ns558.c,v 1.43 2002/01/24 19:23:21 vojtech Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
5 * Copyright (c) 1999 Brian Gerst
9 * NS558 based standard IBM game port driver for Linux
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.
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.
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
27 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
34 #include <linux/module.h>
35 #include <linux/ioport.h>
36 #include <linux/config.h>
37 #include <linux/init.h>
38 #include <linux/delay.h>
39 #include <linux/gameport.h>
40 #include <linux/slab.h>
41 #include <linux/pnp.h>
43 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44 MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver");
45 MODULE_LICENSE("GPL");
47 static int ns558_isa_portlist
[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
48 0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
55 struct gameport
*gameport
;
56 struct list_head node
;
59 static LIST_HEAD(ns558_list
);
62 * ns558_isa_probe() tries to find an isa gameport at the
63 * specified address, and also checks for mirrors.
64 * A joystick must be attached for this to work.
67 static int ns558_isa_probe(int io
)
70 unsigned char c
, u
, v
;
72 struct gameport
*port
;
75 * No one should be using this address.
78 if (!request_region(io
, 1, "ns558-isa"))
82 * We must not be able to write arbitrary values to the port.
83 * The lower two axis bits must be 1 after a write.
88 if (~(u
= v
= inb(io
)) & 3) {
90 release_region(io
, 1);
94 * After a trigger, there must be at least some bits changing.
97 for (i
= 0; i
< 1000; i
++) v
&= inb(io
);
101 release_region(io
, 1);
106 * After some time (4ms) the axes shouldn't change anymore.
110 for (i
= 0; i
< 1000; i
++)
111 if ((u
^ inb(io
)) & 0xf) {
113 release_region(io
, 1);
117 * And now find the number of mirrors of the port.
120 for (i
= 1; i
< 5; i
++) {
122 release_region(io
& (-1 << (i
- 1)), (1 << (i
- 1)));
124 if (!request_region(io
& (-1 << i
), (1 << i
), "ns558-isa"))
125 break; /* Don't disturb anyone */
127 outb(0xff, io
& (-1 << i
));
128 for (j
= b
= 0; j
< 1000; j
++)
129 if (inb(io
& (-1 << i
)) != inb((io
& (-1 << i
)) + (1 << i
) - 1)) b
++;
132 if (b
> 300) { /* We allow 30% difference */
133 release_region(io
& (-1 << i
), (1 << i
));
141 if (!request_region(io
& (-1 << i
), (1 << i
), "ns558-isa"))
145 ns558
= kcalloc(1, sizeof(struct ns558
), GFP_KERNEL
);
146 port
= gameport_allocate_port();
147 if (!ns558
|| !port
) {
148 printk(KERN_ERR
"ns558: Memory allocation failed.\n");
149 release_region(io
& (-1 << i
), (1 << i
));
151 gameport_free_port(port
);
155 memset(ns558
, 0, sizeof(struct ns558
));
157 ns558
->size
= 1 << i
;
158 ns558
->gameport
= port
;
161 gameport_set_name(port
, "NS558 ISA Gameport");
162 gameport_set_phys(port
, "isa%04x/gameport0", io
& (-1 << i
));
164 gameport_register_port(port
);
166 list_add(&ns558
->node
, &ns558_list
);
173 static struct pnp_device_id pnp_devids
[] = {
174 { .id
= "@P@0001", .driver_data
= 0 }, /* ALS 100 */
175 { .id
= "@P@0020", .driver_data
= 0 }, /* ALS 200 */
176 { .id
= "@P@1001", .driver_data
= 0 }, /* ALS 100+ */
177 { .id
= "@P@2001", .driver_data
= 0 }, /* ALS 120 */
178 { .id
= "ASB16fd", .driver_data
= 0 }, /* AdLib NSC16 */
179 { .id
= "AZT3001", .driver_data
= 0 }, /* AZT1008 */
180 { .id
= "CDC0001", .driver_data
= 0 }, /* Opl3-SAx */
181 { .id
= "CSC0001", .driver_data
= 0 }, /* CS4232 */
182 { .id
= "CSC000f", .driver_data
= 0 }, /* CS4236 */
183 { .id
= "CSC0101", .driver_data
= 0 }, /* CS4327 */
184 { .id
= "CTL7001", .driver_data
= 0 }, /* SB16 */
185 { .id
= "CTL7002", .driver_data
= 0 }, /* AWE64 */
186 { .id
= "CTL7005", .driver_data
= 0 }, /* Vibra16 */
187 { .id
= "ENS2020", .driver_data
= 0 }, /* SoundscapeVIVO */
188 { .id
= "ESS0001", .driver_data
= 0 }, /* ES1869 */
189 { .id
= "ESS0005", .driver_data
= 0 }, /* ES1878 */
190 { .id
= "ESS6880", .driver_data
= 0 }, /* ES688 */
191 { .id
= "IBM0012", .driver_data
= 0 }, /* CS4232 */
192 { .id
= "OPT0001", .driver_data
= 0 }, /* OPTi Audio16 */
193 { .id
= "YMH0006", .driver_data
= 0 }, /* Opl3-SA */
194 { .id
= "YMH0022", .driver_data
= 0 }, /* Opl3-SAx */
195 { .id
= "PNPb02f", .driver_data
= 0 }, /* Generic */
199 MODULE_DEVICE_TABLE(pnp
, pnp_devids
);
201 static int ns558_pnp_probe(struct pnp_dev
*dev
, const struct pnp_device_id
*did
)
205 struct gameport
*port
;
207 if (!pnp_port_valid(dev
, 0)) {
208 printk(KERN_WARNING
"ns558: No i/o ports on a gameport? Weird\n");
212 ioport
= pnp_port_start(dev
, 0);
213 iolen
= pnp_port_len(dev
, 0);
215 if (!request_region(ioport
, iolen
, "ns558-pnp"))
218 ns558
= kcalloc(1, sizeof(struct ns558
), GFP_KERNEL
);
219 port
= gameport_allocate_port();
220 if (!ns558
|| !port
) {
221 printk(KERN_ERR
"ns558: Memory allocation failed\n");
223 gameport_free_port(port
);
230 ns558
->gameport
= port
;
232 gameport_set_name(port
, "NS558 PnP Gameport");
233 gameport_set_phys(port
, "pnp%s/gameport0", dev
->dev
.bus_id
);
234 port
->dev
.parent
= &dev
->dev
;
237 gameport_register_port(port
);
239 list_add_tail(&ns558
->node
, &ns558_list
);
243 static struct pnp_driver ns558_pnp_driver
= {
245 .id_table
= pnp_devids
,
246 .probe
= ns558_pnp_probe
,
251 static struct pnp_driver ns558_pnp_driver
;
255 static int pnp_registered
= 0;
257 static int __init
ns558_init(void)
262 * Probe ISA ports first so that PnP gets to choose free port addresses
263 * not occupied by the ISA ports.
266 while (ns558_isa_portlist
[i
])
267 ns558_isa_probe(ns558_isa_portlist
[i
++]);
269 if (pnp_register_driver(&ns558_pnp_driver
) >= 0)
273 return (list_empty(&ns558_list
) && !pnp_registered
) ? -ENODEV
: 0;
276 static void __exit
ns558_exit(void)
280 list_for_each_entry(ns558
, &ns558_list
, node
) {
281 gameport_unregister_port(ns558
->gameport
);
282 release_region(ns558
->io
& ~(ns558
->size
- 1), ns558
->size
);
287 pnp_unregister_driver(&ns558_pnp_driver
);
290 module_init(ns558_init
);
291 module_exit(ns558_exit
);