ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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/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 #define NS558_ISA       1
47 #define NS558_PNP       2
48
49 static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
50                                     0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
51
52 struct ns558 {
53         int type;
54         int size;
55         struct pnp_dev *dev;
56         struct list_head node;
57         struct gameport gameport;
58         char phys[32];
59         char name[32];
60 };
61         
62 static LIST_HEAD(ns558_list);
63
64 /*
65  * ns558_isa_probe() tries to find an isa gameport at the
66  * specified address, and also checks for mirrors.
67  * A joystick must be attached for this to work.
68  */
69
70 static void ns558_isa_probe(int io)
71 {
72         int i, j, b;
73         unsigned char c, u, v;
74         struct ns558 *port;
75
76 /*
77  * No one should be using this address.
78  */
79
80         if (!request_region(io, 1, "ns558-isa"))
81                 return;
82
83 /*
84  * We must not be able to write arbitrary values to the port.
85  * The lower two axis bits must be 1 after a write.
86  */
87
88         c = inb(io);
89         outb(~c & ~3, io);
90         if (~(u = v = inb(io)) & 3) {
91                 outb(c, io);
92                 i = 0;
93                 goto out;
94         }
95 /*
96  * After a trigger, there must be at least some bits changing.
97  */
98
99         for (i = 0; i < 1000; i++) v &= inb(io);
100
101         if (u == v) {
102                 outb(c, io);
103                 i = 0;
104                 goto out;
105         }
106         wait_ms(3);
107 /*
108  * After some time (4ms) the axes shouldn't change anymore.
109  */
110
111         u = inb(io);
112         for (i = 0; i < 1000; i++)
113                 if ((u ^ inb(io)) & 0xf) {
114                         outb(c, io);
115                         i = 0;
116                         goto out;
117                 }
118 /* 
119  * And now find the number of mirrors of the port.
120  */
121
122         for (i = 1; i < 5; i++) {
123
124                 release_region(io & (-1 << (i-1)), (1 << (i-1)));
125
126                 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))     /* Don't disturb anyone */
127                         break;
128
129                 outb(0xff, io & (-1 << i));
130                 for (j = b = 0; j < 1000; j++)
131                         if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++;
132                 wait_ms(3);
133
134                 if (b > 300) {                                  /* We allow 30% difference */
135                         release_region(io & (-1 << i), (1 << i));
136                         break;
137                 }
138         }
139
140         i--;
141
142         if (i != 4) {
143                 if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
144                         return;
145         }
146
147         if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
148                 printk(KERN_ERR "ns558: Memory allocation failed.\n");
149                 goto out;
150         }
151         memset(port, 0, sizeof(struct ns558));
152
153         port->type = NS558_ISA;
154         port->size = (1 << i);
155         port->gameport.io = io;
156         port->gameport.phys = port->phys;
157         port->gameport.name = port->name;
158         port->gameport.id.bustype = BUS_ISA;
159
160         sprintf(port->phys, "isa%04x/gameport0", io & (-1 << i));
161         sprintf(port->name, "NS558 ISA");
162
163         gameport_register_port(&port->gameport);
164
165         printk(KERN_INFO "gameport: NS558 ISA at %#x", port->gameport.io);
166         if (port->size > 1) printk(" size %d", port->size);
167         printk(" speed %d kHz\n", port->gameport.speed);
168
169         list_add(&port->node, &ns558_list);
170         return;
171 out:
172         release_region(io & (-1 << i), (1 << i));
173 }
174
175 #ifdef CONFIG_PNP
176
177 static struct pnp_device_id pnp_devids[] = {
178         { .id = "@P@0001", .driver_data = 0 }, /* ALS 100 */
179         { .id = "@P@0020", .driver_data = 0 }, /* ALS 200 */
180         { .id = "@P@1001", .driver_data = 0 }, /* ALS 100+ */
181         { .id = "@P@2001", .driver_data = 0 }, /* ALS 120 */
182         { .id = "ASB16fd", .driver_data = 0 }, /* AdLib NSC16 */
183         { .id = "AZT3001", .driver_data = 0 }, /* AZT1008 */
184         { .id = "CDC0001", .driver_data = 0 }, /* Opl3-SAx */
185         { .id = "CSC0001", .driver_data = 0 }, /* CS4232 */
186         { .id = "CSC000f", .driver_data = 0 }, /* CS4236 */
187         { .id = "CSC0101", .driver_data = 0 }, /* CS4327 */
188         { .id = "CTL7001", .driver_data = 0 }, /* SB16 */
189         { .id = "CTL7002", .driver_data = 0 }, /* AWE64 */
190         { .id = "CTL7005", .driver_data = 0 }, /* Vibra16 */
191         { .id = "ENS2020", .driver_data = 0 }, /* SoundscapeVIVO */
192         { .id = "ESS0001", .driver_data = 0 }, /* ES1869 */
193         { .id = "ESS0005", .driver_data = 0 }, /* ES1878 */
194         { .id = "ESS6880", .driver_data = 0 }, /* ES688 */
195         { .id = "IBM0012", .driver_data = 0 }, /* CS4232 */
196         { .id = "OPT0001", .driver_data = 0 }, /* OPTi Audio16 */
197         { .id = "YMH0006", .driver_data = 0 }, /* Opl3-SA */
198         { .id = "YMH0022", .driver_data = 0 }, /* Opl3-SAx */
199         { .id = "PNPb02f", .driver_data = 0 }, /* Generic */
200         { .id = "", },
201 };
202
203 MODULE_DEVICE_TABLE(pnp, pnp_devids);
204
205 static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
206 {
207         int ioport, iolen;
208         struct ns558 *port;
209
210         if (!pnp_port_valid(dev, 0)) {
211                 printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
212                 return -ENODEV;
213         }
214
215         ioport = pnp_port_start(dev,0);
216         iolen = pnp_port_len(dev,0);
217
218         if (!request_region(ioport, iolen, "ns558-pnp"))
219                 return -EBUSY;
220
221         if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
222                 printk(KERN_ERR "ns558: Memory allocation failed.\n");
223                 return -ENOMEM;
224         }
225         memset(port, 0, sizeof(struct ns558));
226
227         port->type = NS558_PNP;
228         port->size = iolen;
229         port->dev = dev;
230
231         port->gameport.io = ioport;
232         port->gameport.phys = port->phys;
233         port->gameport.name = port->name;
234         port->gameport.id.bustype = BUS_ISAPNP;
235         port->gameport.id.version = 0x100;
236
237         sprintf(port->phys, "pnp%s/gameport0", dev->dev.bus_id);
238         sprintf(port->name, "%s", "NS558 PnP Gameport");
239
240         gameport_register_port(&port->gameport);
241
242         printk(KERN_INFO "gameport: NS558 PnP at pnp%s io %#x",
243                 dev->dev.bus_id, port->gameport.io);
244         if (iolen > 1) printk(" size %d", iolen);
245         printk(" speed %d kHz\n", port->gameport.speed);
246
247         list_add_tail(&port->node, &ns558_list);
248         return 0;
249 }
250
251 static struct pnp_driver ns558_pnp_driver = {
252         .name           = "ns558",
253         .id_table       = pnp_devids,
254         .probe          = ns558_pnp_probe,
255 };
256
257 #else
258
259 static struct pnp_driver ns558_pnp_driver;
260
261 #endif
262
263 int __init ns558_init(void)
264 {
265         int i = 0;
266
267 /*
268  * Probe for ISA ports.
269  */
270
271         while (ns558_isa_portlist[i])
272                 ns558_isa_probe(ns558_isa_portlist[i++]);
273
274         pnp_register_driver(&ns558_pnp_driver);
275         return list_empty(&ns558_list) ? -ENODEV : 0;
276 }
277
278 void __exit ns558_exit(void)
279 {
280         struct ns558 *port;
281
282         list_for_each_entry(port, &ns558_list, node) {
283                 gameport_unregister_port(&port->gameport);
284                 switch (port->type) {
285
286 #ifdef CONFIG_PNP
287                         case NS558_PNP:
288                                 /* fall through */
289 #endif
290                         case NS558_ISA:
291                                 release_region(port->gameport.io & ~(port->size - 1), port->size);
292                                 kfree(port);
293                                 break;
294                 
295                         default:
296                                 break;
297                 }
298         }
299         pnp_unregister_driver(&ns558_pnp_driver);
300 }
301
302 module_init(ns558_init);
303 module_exit(ns558_exit);