xref: /openbmc/linux/drivers/input/gameport/ns558.c (revision 5d331b7f)
1 /*
2  *  Copyright (c) 1999-2001 Vojtech Pavlik
3  *  Copyright (c) 1999 Brian Gerst
4  */
5 
6 /*
7  * NS558 based standard IBM game port driver for Linux
8  */
9 
10 /*
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */
25 
26 #include <asm/io.h>
27 
28 #include <linux/module.h>
29 #include <linux/ioport.h>
30 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/gameport.h>
33 #include <linux/slab.h>
34 #include <linux/pnp.h>
35 
36 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
37 MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver");
38 MODULE_LICENSE("GPL");
39 
40 static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
41 				    0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
42 
43 struct ns558 {
44 	int type;
45 	int io;
46 	int size;
47 	struct pnp_dev *dev;
48 	struct gameport *gameport;
49 	struct list_head node;
50 };
51 
52 static LIST_HEAD(ns558_list);
53 
54 /*
55  * ns558_isa_probe() tries to find an isa gameport at the
56  * specified address, and also checks for mirrors.
57  * A joystick must be attached for this to work.
58  */
59 
60 static int ns558_isa_probe(int io)
61 {
62 	int i, j, b;
63 	unsigned char c, u, v;
64 	struct ns558 *ns558;
65 	struct gameport *port;
66 
67 /*
68  * No one should be using this address.
69  */
70 
71 	if (!request_region(io, 1, "ns558-isa"))
72 		return -EBUSY;
73 
74 /*
75  * We must not be able to write arbitrary values to the port.
76  * The lower two axis bits must be 1 after a write.
77  */
78 
79 	c = inb(io);
80 	outb(~c & ~3, io);
81 	if (~(u = v = inb(io)) & 3) {
82 		outb(c, io);
83 		release_region(io, 1);
84 		return -ENODEV;
85 	}
86 /*
87  * After a trigger, there must be at least some bits changing.
88  */
89 
90 	for (i = 0; i < 1000; i++) v &= inb(io);
91 
92 	if (u == v) {
93 		outb(c, io);
94 		release_region(io, 1);
95 		return -ENODEV;
96 	}
97 	msleep(3);
98 /*
99  * After some time (4ms) the axes shouldn't change anymore.
100  */
101 
102 	u = inb(io);
103 	for (i = 0; i < 1000; i++)
104 		if ((u ^ inb(io)) & 0xf) {
105 			outb(c, io);
106 			release_region(io, 1);
107 			return -ENODEV;
108 		}
109 /*
110  * And now find the number of mirrors of the port.
111  */
112 
113 	for (i = 1; i < 5; i++) {
114 
115 		release_region(io & (-1 << (i - 1)), (1 << (i - 1)));
116 
117 		if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
118 			break;				/* Don't disturb anyone */
119 
120 		outb(0xff, io & (-1 << i));
121 		for (j = b = 0; j < 1000; j++)
122 			if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++;
123 		msleep(3);
124 
125 		if (b > 300) {				/* We allow 30% difference */
126 			release_region(io & (-1 << i), (1 << i));
127 			break;
128 		}
129 	}
130 
131 	i--;
132 
133 	if (i != 4) {
134 		if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
135 			return -EBUSY;
136 	}
137 
138 	ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
139 	port = gameport_allocate_port();
140 	if (!ns558 || !port) {
141 		printk(KERN_ERR "ns558: Memory allocation failed.\n");
142 		release_region(io & (-1 << i), (1 << i));
143 		kfree(ns558);
144 		gameport_free_port(port);
145 		return -ENOMEM;
146 	}
147 
148 	ns558->io = io;
149 	ns558->size = 1 << i;
150 	ns558->gameport = port;
151 
152 	port->io = io;
153 	gameport_set_name(port, "NS558 ISA Gameport");
154 	gameport_set_phys(port, "isa%04x/gameport0", io & (-1 << i));
155 
156 	gameport_register_port(port);
157 
158 	list_add(&ns558->node, &ns558_list);
159 
160 	return 0;
161 }
162 
163 #ifdef CONFIG_PNP
164 
165 static const struct pnp_device_id pnp_devids[] = {
166 	{ .id = "@P@0001", .driver_data = 0 }, /* ALS 100 */
167 	{ .id = "@P@0020", .driver_data = 0 }, /* ALS 200 */
168 	{ .id = "@P@1001", .driver_data = 0 }, /* ALS 100+ */
169 	{ .id = "@P@2001", .driver_data = 0 }, /* ALS 120 */
170 	{ .id = "ASB16fd", .driver_data = 0 }, /* AdLib NSC16 */
171 	{ .id = "AZT3001", .driver_data = 0 }, /* AZT1008 */
172 	{ .id = "CDC0001", .driver_data = 0 }, /* Opl3-SAx */
173 	{ .id = "CSC0001", .driver_data = 0 }, /* CS4232 */
174 	{ .id = "CSC000f", .driver_data = 0 }, /* CS4236 */
175 	{ .id = "CSC0101", .driver_data = 0 }, /* CS4327 */
176 	{ .id = "CTL7001", .driver_data = 0 }, /* SB16 */
177 	{ .id = "CTL7002", .driver_data = 0 }, /* AWE64 */
178 	{ .id = "CTL7005", .driver_data = 0 }, /* Vibra16 */
179 	{ .id = "ENS2020", .driver_data = 0 }, /* SoundscapeVIVO */
180 	{ .id = "ESS0001", .driver_data = 0 }, /* ES1869 */
181 	{ .id = "ESS0005", .driver_data = 0 }, /* ES1878 */
182 	{ .id = "ESS6880", .driver_data = 0 }, /* ES688 */
183 	{ .id = "IBM0012", .driver_data = 0 }, /* CS4232 */
184 	{ .id = "OPT0001", .driver_data = 0 }, /* OPTi Audio16 */
185 	{ .id = "YMH0006", .driver_data = 0 }, /* Opl3-SA */
186 	{ .id = "YMH0022", .driver_data = 0 }, /* Opl3-SAx */
187 	{ .id = "PNPb02f", .driver_data = 0 }, /* Generic */
188 	{ .id = "", },
189 };
190 
191 MODULE_DEVICE_TABLE(pnp, pnp_devids);
192 
193 static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
194 {
195 	int ioport, iolen;
196 	struct ns558 *ns558;
197 	struct gameport *port;
198 
199 	if (!pnp_port_valid(dev, 0)) {
200 		printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
201 		return -ENODEV;
202 	}
203 
204 	ioport = pnp_port_start(dev, 0);
205 	iolen = pnp_port_len(dev, 0);
206 
207 	if (!request_region(ioport, iolen, "ns558-pnp"))
208 		return -EBUSY;
209 
210 	ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
211 	port = gameport_allocate_port();
212 	if (!ns558 || !port) {
213 		printk(KERN_ERR "ns558: Memory allocation failed\n");
214 		kfree(ns558);
215 		gameport_free_port(port);
216 		return -ENOMEM;
217 	}
218 
219 	ns558->io = ioport;
220 	ns558->size = iolen;
221 	ns558->dev = dev;
222 	ns558->gameport = port;
223 
224 	gameport_set_name(port, "NS558 PnP Gameport");
225 	gameport_set_phys(port, "pnp%s/gameport0", dev_name(&dev->dev));
226 	port->dev.parent = &dev->dev;
227 	port->io = ioport;
228 
229 	gameport_register_port(port);
230 
231 	list_add_tail(&ns558->node, &ns558_list);
232 	return 0;
233 }
234 
235 static struct pnp_driver ns558_pnp_driver = {
236 	.name		= "ns558",
237 	.id_table	= pnp_devids,
238 	.probe		= ns558_pnp_probe,
239 };
240 
241 #else
242 
243 static struct pnp_driver ns558_pnp_driver;
244 
245 #endif
246 
247 static int __init ns558_init(void)
248 {
249 	int i = 0;
250 	int error;
251 
252 	error = pnp_register_driver(&ns558_pnp_driver);
253 	if (error && error != -ENODEV)	/* should be ENOSYS really */
254 		return error;
255 
256 /*
257  * Probe ISA ports after PnP, so that PnP ports that are already
258  * enabled get detected as PnP. This may be suboptimal in multi-device
259  * configurations, but saves hassle with simple setups.
260  */
261 
262 	while (ns558_isa_portlist[i])
263 		ns558_isa_probe(ns558_isa_portlist[i++]);
264 
265 	return list_empty(&ns558_list) && error ? -ENODEV : 0;
266 }
267 
268 static void __exit ns558_exit(void)
269 {
270 	struct ns558 *ns558, *safe;
271 
272 	list_for_each_entry_safe(ns558, safe, &ns558_list, node) {
273 		gameport_unregister_port(ns558->gameport);
274 		release_region(ns558->io & ~(ns558->size - 1), ns558->size);
275 		kfree(ns558);
276 	}
277 
278 	pnp_unregister_driver(&ns558_pnp_driver);
279 }
280 
281 module_init(ns558_init);
282 module_exit(ns558_exit);
283