]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * $Id: ns558.c,v 1.43 2002/01/24 19:23:21 vojtech Exp $ | |
3 | * | |
4 | * Copyright (c) 1999-2001 Vojtech Pavlik | |
5 | * Copyright (c) 1999 Brian Gerst | |
6 | */ | |
7 | ||
8 | /* | |
9 | * NS558 based standard IBM game port driver for Linux | |
10 | */ | |
11 | ||
12 | /* | |
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. | |
17 | * | |
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. | |
22 | * | |
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 | |
26 | * | |
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 | |
30 | */ | |
31 | ||
32 | #include <asm/io.h> | |
33 | ||
34 | #include <linux/module.h> | |
35 | #include <linux/ioport.h> | |
1da177e4 LT |
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> | |
41 | ||
42 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); | |
43 | MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver"); | |
44 | MODULE_LICENSE("GPL"); | |
45 | ||
46 | static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209, | |
47 | 0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 }; | |
48 | ||
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; | |
56 | }; | |
57 | ||
58 | static LIST_HEAD(ns558_list); | |
59 | ||
60 | /* | |
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. | |
64 | */ | |
65 | ||
66 | static int ns558_isa_probe(int io) | |
67 | { | |
68 | int i, j, b; | |
69 | unsigned char c, u, v; | |
70 | struct ns558 *ns558; | |
71 | struct gameport *port; | |
72 | ||
73 | /* | |
74 | * No one should be using this address. | |
75 | */ | |
76 | ||
77 | if (!request_region(io, 1, "ns558-isa")) | |
78 | return -EBUSY; | |
79 | ||
80 | /* | |
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. | |
83 | */ | |
84 | ||
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; | |
91 | } | |
92 | /* | |
93 | * After a trigger, there must be at least some bits changing. | |
94 | */ | |
95 | ||
96 | for (i = 0; i < 1000; i++) v &= inb(io); | |
97 | ||
98 | if (u == v) { | |
99 | outb(c, io); | |
100 | release_region(io, 1); | |
101 | return -ENODEV; | |
102 | } | |
103 | msleep(3); | |
104 | /* | |
105 | * After some time (4ms) the axes shouldn't change anymore. | |
106 | */ | |
107 | ||
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; | |
114 | } | |
115 | /* | |
116 | * And now find the number of mirrors of the port. | |
117 | */ | |
118 | ||
119 | for (i = 1; i < 5; i++) { | |
120 | ||
121 | release_region(io & (-1 << (i - 1)), (1 << (i - 1))); | |
122 | ||
123 | if (!request_region(io & (-1 << i), (1 << i), "ns558-isa")) | |
124 | break; /* Don't disturb anyone */ | |
125 | ||
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); | |
130 | ||
131 | if (b > 300) { /* We allow 30% difference */ | |
132 | release_region(io & (-1 << i), (1 << i)); | |
133 | break; | |
134 | } | |
135 | } | |
136 | ||
137 | i--; | |
138 | ||
139 | if (i != 4) { | |
140 | if (!request_region(io & (-1 << i), (1 << i), "ns558-isa")) | |
141 | return -EBUSY; | |
142 | } | |
143 | ||
a97e148a | 144 | ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL); |
1da177e4 LT |
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; | |
152 | } | |
153 | ||
154 | memset(ns558, 0, sizeof(struct ns558)); | |
155 | ns558->io = io; | |
156 | ns558->size = 1 << i; | |
157 | ns558->gameport = port; | |
158 | ||
159 | port->io = io; | |
160 | gameport_set_name(port, "NS558 ISA Gameport"); | |
161 | gameport_set_phys(port, "isa%04x/gameport0", io & (-1 << i)); | |
162 | ||
163 | gameport_register_port(port); | |
164 | ||
165 | list_add(&ns558->node, &ns558_list); | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | #ifdef CONFIG_PNP | |
171 | ||
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 = "", }, | |
196 | }; | |
197 | ||
198 | MODULE_DEVICE_TABLE(pnp, pnp_devids); | |
199 | ||
200 | static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did) | |
201 | { | |
202 | int ioport, iolen; | |
203 | struct ns558 *ns558; | |
204 | struct gameport *port; | |
205 | ||
206 | if (!pnp_port_valid(dev, 0)) { | |
207 | printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n"); | |
208 | return -ENODEV; | |
209 | } | |
210 | ||
211 | ioport = pnp_port_start(dev, 0); | |
212 | iolen = pnp_port_len(dev, 0); | |
213 | ||
214 | if (!request_region(ioport, iolen, "ns558-pnp")) | |
215 | return -EBUSY; | |
216 | ||
a97e148a | 217 | ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL); |
1da177e4 LT |
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; | |
224 | } | |
225 | ||
226 | ns558->io = ioport; | |
227 | ns558->size = iolen; | |
228 | ns558->dev = dev; | |
229 | ns558->gameport = port; | |
230 | ||
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; | |
235 | ||
236 | gameport_register_port(port); | |
237 | ||
238 | list_add_tail(&ns558->node, &ns558_list); | |
239 | return 0; | |
240 | } | |
241 | ||
242 | static struct pnp_driver ns558_pnp_driver = { | |
243 | .name = "ns558", | |
244 | .id_table = pnp_devids, | |
245 | .probe = ns558_pnp_probe, | |
246 | }; | |
247 | ||
248 | #else | |
249 | ||
250 | static struct pnp_driver ns558_pnp_driver; | |
251 | ||
252 | #endif | |
253 | ||
1da177e4 LT |
254 | static int __init ns558_init(void) |
255 | { | |
256 | int i = 0; | |
dd55563f | 257 | int error; |
1da177e4 | 258 | |
dd55563f BH |
259 | error = pnp_register_driver(&ns558_pnp_driver); |
260 | if (error && error != -ENODEV) /* should be ENOSYS really */ | |
261 | return error; | |
f6397cec | 262 | |
1da177e4 | 263 | /* |
f6397cec VP |
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. | |
1da177e4 LT |
267 | */ |
268 | ||
269 | while (ns558_isa_portlist[i]) | |
270 | ns558_isa_probe(ns558_isa_portlist[i++]); | |
271 | ||
dd55563f | 272 | return list_empty(&ns558_list) && error ? -ENODEV : 0; |
1da177e4 LT |
273 | } |
274 | ||
275 | static void __exit ns558_exit(void) | |
276 | { | |
22d0def9 | 277 | struct ns558 *ns558, *safe; |
1da177e4 | 278 | |
22d0def9 | 279 | list_for_each_entry_safe(ns558, safe, &ns558_list, node) { |
1da177e4 LT |
280 | gameport_unregister_port(ns558->gameport); |
281 | release_region(ns558->io & ~(ns558->size - 1), ns558->size); | |
282 | kfree(ns558); | |
283 | } | |
284 | ||
dd55563f | 285 | pnp_unregister_driver(&ns558_pnp_driver); |
1da177e4 LT |
286 | } |
287 | ||
288 | module_init(ns558_init); | |
289 | module_exit(ns558_exit); |