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");
50 static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
51 0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
57 struct list_head node;
58 struct gameport gameport;
63 static LIST_HEAD(ns558_list);
66 * ns558_isa_probe() tries to find an isa gameport at the
67 * specified address, and also checks for mirrors.
68 * A joystick must be attached for this to work.
71 static void ns558_isa_probe(int io)
74 unsigned char c, u, v;
78 * No one should be using this address.
81 if (!request_region(io, 1, "ns558-isa"))
85 * We must not be able to write arbitrary values to the port.
86 * The lower two axis bits must be 1 after a write.
91 if (~(u = v = inb(io)) & 3) {
97 * After a trigger, there must be at least some bits changing.
100 for (i = 0; i < 1000; i++) v &= inb(io);
109 * After some time (4ms) the axes shouldn't change anymore.
113 for (i = 0; i < 1000; i++)
114 if ((u ^ inb(io)) & 0xf) {
120 * And now find the number of mirrors of the port.
123 for (i = 1; i < 5; i++) {
125 release_region(io & (-1 << (i-1)), (1 << (i-1)));
127 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa")) /* Don't disturb anyone */
130 outb(0xff, io & (-1 << i));
131 for (j = b = 0; j < 1000; j++)
132 if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++;
135 if (b > 300) { /* We allow 30% difference */
136 release_region(io & (-1 << i), (1 << i));
144 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
148 if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
149 printk(KERN_ERR "ns558: Memory allocation failed.\n");
152 memset(port, 0, sizeof(struct ns558));
154 port->type = NS558_ISA;
155 port->size = (1 << i);
156 port->gameport.io = io;
157 port->gameport.phys = port->phys;
158 port->gameport.name = port->name;
159 port->gameport.id.bustype = BUS_ISA;
161 sprintf(port->phys, "isa%04x/gameport0", io & (-1 << i));
162 sprintf(port->name, "NS558 ISA");
164 gameport_register_port(&port->gameport);
166 printk(KERN_INFO "gameport: NS558 ISA at %#x", port->gameport.io);
167 if (port->size > 1) printk(" size %d", port->size);
168 printk(" speed %d kHz\n", port->gameport.speed);
170 list_add(&port->node, &ns558_list);
173 release_region(io & (-1 << i), (1 << i));
178 static struct pnp_device_id pnp_devids[] = {
179 { .id = "@P@0001", .driver_data = 0 }, /* ALS 100 */
180 { .id = "@P@0020", .driver_data = 0 }, /* ALS 200 */
181 { .id = "@P@1001", .driver_data = 0 }, /* ALS 100+ */
182 { .id = "@P@2001", .driver_data = 0 }, /* ALS 120 */
183 { .id = "ASB16fd", .driver_data = 0 }, /* AdLib NSC16 */
184 { .id = "AZT3001", .driver_data = 0 }, /* AZT1008 */
185 { .id = "CDC0001", .driver_data = 0 }, /* Opl3-SAx */
186 { .id = "CSC0001", .driver_data = 0 }, /* CS4232 */
187 { .id = "CSC000f", .driver_data = 0 }, /* CS4236 */
188 { .id = "CSC0101", .driver_data = 0 }, /* CS4327 */
189 { .id = "CTL7001", .driver_data = 0 }, /* SB16 */
190 { .id = "CTL7002", .driver_data = 0 }, /* AWE64 */
191 { .id = "CTL7005", .driver_data = 0 }, /* Vibra16 */
192 { .id = "ENS2020", .driver_data = 0 }, /* SoundscapeVIVO */
193 { .id = "ESS0001", .driver_data = 0 }, /* ES1869 */
194 { .id = "ESS0005", .driver_data = 0 }, /* ES1878 */
195 { .id = "ESS6880", .driver_data = 0 }, /* ES688 */
196 { .id = "IBM0012", .driver_data = 0 }, /* CS4232 */
197 { .id = "OPT0001", .driver_data = 0 }, /* OPTi Audio16 */
198 { .id = "YMH0006", .driver_data = 0 }, /* Opl3-SA */
199 { .id = "YMH0022", .driver_data = 0 }, /* Opl3-SAx */
200 { .id = "PNPb02f", .driver_data = 0 }, /* Generic */
204 MODULE_DEVICE_TABLE(pnp, pnp_devids);
206 static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
211 if (!pnp_port_valid(dev, 0)) {
212 printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
216 ioport = pnp_port_start(dev,0);
217 iolen = pnp_port_len(dev,0);
219 if (!request_region(ioport, iolen, "ns558-pnp"))
222 if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
223 printk(KERN_ERR "ns558: Memory allocation failed.\n");
226 memset(port, 0, sizeof(struct ns558));
228 port->type = NS558_PNP;
232 port->gameport.io = ioport;
233 port->gameport.phys = port->phys;
234 port->gameport.name = port->name;
235 port->gameport.id.bustype = BUS_ISAPNP;
236 port->gameport.id.version = 0x100;
238 sprintf(port->phys, "pnp%s/gameport0", dev->dev.bus_id);
239 sprintf(port->name, "%s", "NS558 PnP Gameport");
241 gameport_register_port(&port->gameport);
243 printk(KERN_INFO "gameport: NS558 PnP at pnp%s io %#x",
244 dev->dev.bus_id, port->gameport.io);
245 if (iolen > 1) printk(" size %d", iolen);
246 printk(" speed %d kHz\n", port->gameport.speed);
248 list_add_tail(&port->node, &ns558_list);
252 static struct pnp_driver ns558_pnp_driver = {
254 .id_table = pnp_devids,
255 .probe = ns558_pnp_probe,
260 static struct pnp_driver ns558_pnp_driver;
264 int __init ns558_init(void)
269 * Probe for ISA ports.
272 while (ns558_isa_portlist[i])
273 ns558_isa_probe(ns558_isa_portlist[i++]);
275 pnp_register_driver(&ns558_pnp_driver);
276 return list_empty(&ns558_list) ? -ENODEV : 0;
279 void __exit ns558_exit(void)
283 list_for_each_entry(port, &ns558_list, node) {
284 gameport_unregister_port(&port->gameport);
285 switch (port->type) {
292 release_region(port->gameport.io & ~(port->size - 1), port->size);
300 pnp_unregister_driver(&ns558_pnp_driver);
303 module_init(ns558_init);
304 module_exit(ns558_exit);