Linux 2.6.19-rc1
[linux-2.6/next.git] / drivers / input / gameport / ns558.c
blobf68dbe6f7f04634ca7bf63688555de65be85f7f0
1 /*
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
6 */
8 /*
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
32 #include <asm/io.h>
34 #include <linux/module.h>
35 #include <linux/ioport.h>
36 #include <linux/init.h>
37 #include <linux/delay.h>
38 #include <linux/gameport.h>
39 #include <linux/slab.h>
40 #include <linux/pnp.h>
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver");
44 MODULE_LICENSE("GPL");
46 static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
47 0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
49 struct ns558 {
50 int type;
51 int io;
52 int size;
53 struct pnp_dev *dev;
54 struct gameport *gameport;
55 struct list_head node;
58 static LIST_HEAD(ns558_list);
61 * ns558_isa_probe() tries to find an isa gameport at the
62 * specified address, and also checks for mirrors.
63 * A joystick must be attached for this to work.
66 static int ns558_isa_probe(int io)
68 int i, j, b;
69 unsigned char c, u, v;
70 struct ns558 *ns558;
71 struct gameport *port;
74 * No one should be using this address.
77 if (!request_region(io, 1, "ns558-isa"))
78 return -EBUSY;
81 * We must not be able to write arbitrary values to the port.
82 * The lower two axis bits must be 1 after a write.
85 c = inb(io);
86 outb(~c & ~3, io);
87 if (~(u = v = inb(io)) & 3) {
88 outb(c, io);
89 release_region(io, 1);
90 return -ENODEV;
93 * After a trigger, there must be at least some bits changing.
96 for (i = 0; i < 1000; i++) v &= inb(io);
98 if (u == v) {
99 outb(c, io);
100 release_region(io, 1);
101 return -ENODEV;
103 msleep(3);
105 * After some time (4ms) the axes shouldn't change anymore.
108 u = inb(io);
109 for (i = 0; i < 1000; i++)
110 if ((u ^ inb(io)) & 0xf) {
111 outb(c, io);
112 release_region(io, 1);
113 return -ENODEV;
116 * And now find the number of mirrors of the port.
119 for (i = 1; i < 5; i++) {
121 release_region(io & (-1 << (i - 1)), (1 << (i - 1)));
123 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
124 break; /* Don't disturb anyone */
126 outb(0xff, io & (-1 << i));
127 for (j = b = 0; j < 1000; j++)
128 if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++;
129 msleep(3);
131 if (b > 300) { /* We allow 30% difference */
132 release_region(io & (-1 << i), (1 << i));
133 break;
137 i--;
139 if (i != 4) {
140 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
141 return -EBUSY;
144 ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
145 port = gameport_allocate_port();
146 if (!ns558 || !port) {
147 printk(KERN_ERR "ns558: Memory allocation failed.\n");
148 release_region(io & (-1 << i), (1 << i));
149 kfree(ns558);
150 gameport_free_port(port);
151 return -ENOMEM;
154 memset(ns558, 0, sizeof(struct ns558));
155 ns558->io = io;
156 ns558->size = 1 << i;
157 ns558->gameport = port;
159 port->io = io;
160 gameport_set_name(port, "NS558 ISA Gameport");
161 gameport_set_phys(port, "isa%04x/gameport0", io & (-1 << i));
163 gameport_register_port(port);
165 list_add(&ns558->node, &ns558_list);
167 return 0;
170 #ifdef CONFIG_PNP
172 static struct pnp_device_id pnp_devids[] = {
173 { .id = "@P@0001", .driver_data = 0 }, /* ALS 100 */
174 { .id = "@P@0020", .driver_data = 0 }, /* ALS 200 */
175 { .id = "@P@1001", .driver_data = 0 }, /* ALS 100+ */
176 { .id = "@P@2001", .driver_data = 0 }, /* ALS 120 */
177 { .id = "ASB16fd", .driver_data = 0 }, /* AdLib NSC16 */
178 { .id = "AZT3001", .driver_data = 0 }, /* AZT1008 */
179 { .id = "CDC0001", .driver_data = 0 }, /* Opl3-SAx */
180 { .id = "CSC0001", .driver_data = 0 }, /* CS4232 */
181 { .id = "CSC000f", .driver_data = 0 }, /* CS4236 */
182 { .id = "CSC0101", .driver_data = 0 }, /* CS4327 */
183 { .id = "CTL7001", .driver_data = 0 }, /* SB16 */
184 { .id = "CTL7002", .driver_data = 0 }, /* AWE64 */
185 { .id = "CTL7005", .driver_data = 0 }, /* Vibra16 */
186 { .id = "ENS2020", .driver_data = 0 }, /* SoundscapeVIVO */
187 { .id = "ESS0001", .driver_data = 0 }, /* ES1869 */
188 { .id = "ESS0005", .driver_data = 0 }, /* ES1878 */
189 { .id = "ESS6880", .driver_data = 0 }, /* ES688 */
190 { .id = "IBM0012", .driver_data = 0 }, /* CS4232 */
191 { .id = "OPT0001", .driver_data = 0 }, /* OPTi Audio16 */
192 { .id = "YMH0006", .driver_data = 0 }, /* Opl3-SA */
193 { .id = "YMH0022", .driver_data = 0 }, /* Opl3-SAx */
194 { .id = "PNPb02f", .driver_data = 0 }, /* Generic */
195 { .id = "", },
198 MODULE_DEVICE_TABLE(pnp, pnp_devids);
200 static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
202 int ioport, iolen;
203 struct ns558 *ns558;
204 struct gameport *port;
206 if (!pnp_port_valid(dev, 0)) {
207 printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
208 return -ENODEV;
211 ioport = pnp_port_start(dev, 0);
212 iolen = pnp_port_len(dev, 0);
214 if (!request_region(ioport, iolen, "ns558-pnp"))
215 return -EBUSY;
217 ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
218 port = gameport_allocate_port();
219 if (!ns558 || !port) {
220 printk(KERN_ERR "ns558: Memory allocation failed\n");
221 kfree(ns558);
222 gameport_free_port(port);
223 return -ENOMEM;
226 ns558->io = ioport;
227 ns558->size = iolen;
228 ns558->dev = dev;
229 ns558->gameport = port;
231 gameport_set_name(port, "NS558 PnP Gameport");
232 gameport_set_phys(port, "pnp%s/gameport0", dev->dev.bus_id);
233 port->dev.parent = &dev->dev;
234 port->io = ioport;
236 gameport_register_port(port);
238 list_add_tail(&ns558->node, &ns558_list);
239 return 0;
242 static struct pnp_driver ns558_pnp_driver = {
243 .name = "ns558",
244 .id_table = pnp_devids,
245 .probe = ns558_pnp_probe,
248 #else
250 static struct pnp_driver ns558_pnp_driver;
252 #endif
254 static int __init ns558_init(void)
256 int i = 0;
257 int error;
259 error = pnp_register_driver(&ns558_pnp_driver);
260 if (error && error != -ENODEV) /* should be ENOSYS really */
261 return error;
264 * Probe ISA ports after PnP, so that PnP ports that are already
265 * enabled get detected as PnP. This may be suboptimal in multi-device
266 * configurations, but saves hassle with simple setups.
269 while (ns558_isa_portlist[i])
270 ns558_isa_probe(ns558_isa_portlist[i++]);
272 return list_empty(&ns558_list) && error ? -ENODEV : 0;
275 static void __exit ns558_exit(void)
277 struct ns558 *ns558, *safe;
279 list_for_each_entry_safe(ns558, safe, &ns558_list, node) {
280 gameport_unregister_port(ns558->gameport);
281 release_region(ns558->io & ~(ns558->size - 1), ns558->size);
282 kfree(ns558);
285 pnp_unregister_driver(&ns558_pnp_driver);
288 module_init(ns558_init);
289 module_exit(ns558_exit);