patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / gameport / ns558.c
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>
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>
42
43 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44 MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver");
45 MODULE_LICENSE("GPL");
46
47 #define NS558_ISA       1
48 #define NS558_PNP       2
49
50 static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
51                                     0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
52
53 struct ns558 {
54         int type;
55         int size;
56         struct pnp_dev *dev;
57         struct list_head node;
58         struct gameport gameport;
59         char phys[32];
60         char name[32];
61 };
62
63 static LIST_HEAD(ns558_list);
64
65 /*
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.
69  */
70
71 static void ns558_isa_probe(int io)
72 {
73         int i, j, b;
74         unsigned char c, u, v;
75         struct ns558 *port;
76
77 /*
78  * No one should be using this address.
79  */
80
81         if (!request_region(io, 1, "ns558-isa"))
82                 return;
83
84 /*
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.
87  */
88
89         c = inb(io);
90         outb(~c & ~3, io);
91         if (~(u = v = inb(io)) & 3) {
92                 outb(c, io);
93                 i = 0;
94                 goto out;
95         }
96 /*
97  * After a trigger, there must be at least some bits changing.
98  */
99
100         for (i = 0; i < 1000; i++) v &= inb(io);
101
102         if (u == v) {
103                 outb(c, io);
104                 i = 0;
105                 goto out;
106         }
107         msleep(3);
108 /*
109  * After some time (4ms) the axes shouldn't change anymore.
110  */
111
112         u = inb(io);
113         for (i = 0; i < 1000; i++)
114                 if ((u ^ inb(io)) & 0xf) {
115                         outb(c, io);
116                         i = 0;
117                         goto out;
118                 }
119 /*
120  * And now find the number of mirrors of the port.
121  */
122
123         for (i = 1; i < 5; i++) {
124
125                 release_region(io & (-1 << (i-1)), (1 << (i-1)));
126
127                 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))     /* Don't disturb anyone */
128                         break;
129
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++;
133                 msleep(3);
134
135                 if (b > 300) {                                  /* We allow 30% difference */
136                         release_region(io & (-1 << i), (1 << i));
137                         break;
138                 }
139         }
140
141         i--;
142
143         if (i != 4) {
144                 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
145                         return;
146         }
147
148         if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
149                 printk(KERN_ERR "ns558: Memory allocation failed.\n");
150                 goto out;
151         }
152         memset(port, 0, sizeof(struct ns558));
153
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;
160
161         sprintf(port->phys, "isa%04x/gameport0", io & (-1 << i));
162         sprintf(port->name, "NS558 ISA");
163
164         gameport_register_port(&port->gameport);
165
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);
169
170         list_add(&port->node, &ns558_list);
171         return;
172 out:
173         release_region(io & (-1 << i), (1 << i));
174 }
175
176 #ifdef CONFIG_PNP
177
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 */
201         { .id = "", },
202 };
203
204 MODULE_DEVICE_TABLE(pnp, pnp_devids);
205
206 static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
207 {
208         int ioport, iolen;
209         struct ns558 *port;
210
211         if (!pnp_port_valid(dev, 0)) {
212                 printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
213                 return -ENODEV;
214         }
215
216         ioport = pnp_port_start(dev,0);
217         iolen = pnp_port_len(dev,0);
218
219         if (!request_region(ioport, iolen, "ns558-pnp"))
220                 return -EBUSY;
221
222         if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
223                 printk(KERN_ERR "ns558: Memory allocation failed.\n");
224                 return -ENOMEM;
225         }
226         memset(port, 0, sizeof(struct ns558));
227
228         port->type = NS558_PNP;
229         port->size = iolen;
230         port->dev = dev;
231
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;
237
238         sprintf(port->phys, "pnp%s/gameport0", dev->dev.bus_id);
239         sprintf(port->name, "%s", "NS558 PnP Gameport");
240
241         gameport_register_port(&port->gameport);
242
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);
247
248         list_add_tail(&port->node, &ns558_list);
249         return 0;
250 }
251
252 static struct pnp_driver ns558_pnp_driver = {
253         .name           = "ns558",
254         .id_table       = pnp_devids,
255         .probe          = ns558_pnp_probe,
256 };
257
258 #else
259
260 static struct pnp_driver ns558_pnp_driver;
261
262 #endif
263
264 int __init ns558_init(void)
265 {
266         int i = 0;
267
268 /*
269  * Probe for ISA ports.
270  */
271
272         while (ns558_isa_portlist[i])
273                 ns558_isa_probe(ns558_isa_portlist[i++]);
274
275         pnp_register_driver(&ns558_pnp_driver);
276         return list_empty(&ns558_list) ? -ENODEV : 0;
277 }
278
279 void __exit ns558_exit(void)
280 {
281         struct ns558 *port;
282
283         list_for_each_entry(port, &ns558_list, node) {
284                 gameport_unregister_port(&port->gameport);
285                 switch (port->type) {
286
287 #ifdef CONFIG_PNP
288                         case NS558_PNP:
289                                 /* fall through */
290 #endif
291                         case NS558_ISA:
292                                 release_region(port->gameport.io & ~(port->size - 1), port->size);
293                                 kfree(port);
294                                 break;
295
296                         default:
297                                 break;
298                 }
299         }
300         pnp_unregister_driver(&ns558_pnp_driver);
301 }
302
303 module_init(ns558_init);
304 module_exit(ns558_exit);