Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
494 giacomo 1
/*
2
 * $Id: ns558.c,v 1.1 2004-03-08 18:47:38 giacomo 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 <linuxcomp.h>
33
 
34
#include <asm/io.h>
35
 
36
#include <linux/module.h>
37
#include <linux/ioport.h>
38
#include <linux/config.h>
39
#include <linux/init.h>
40
#include <linux/gameport.h>
41
#include <linux/slab.h>
42
#include <linux/pnp.h>
43
 
44
MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45
MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver");
46
MODULE_LICENSE("GPL");
47
 
48
#define NS558_ISA       1
49
#define NS558_PNP       2
50
 
51
static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
52
                                    0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
53
 
54
struct ns558 {
55
        int type;
56
        int size;
57
        struct pnp_dev *dev;
58
        struct list_head node;
59
        struct gameport gameport;
60
        char phys[32];
61
        char name[32];
62
};
63
 
64
static LIST_HEAD(ns558_list);
65
 
66
/*
67
 * ns558_isa_probe() tries to find an isa gameport at the
68
 * specified address, and also checks for mirrors.
69
 * A joystick must be attached for this to work.
70
 */
71
 
72
static void ns558_isa_probe(int io)
73
{
74
        int i, j, b;
75
        unsigned char c, u, v;
76
        struct ns558 *port;
77
 
78
/*
79
 * No one should be using this address.
80
 */
81
 
82
        //!!!if (check_region(io, 1))
83
        //!!!   return;
84
 
85
/*
86
 * We must not be able to write arbitrary values to the port.
87
 * The lower two axis bits must be 1 after a write.
88
 */
89
 
90
        c = inb(io);
91
        outb(~c & ~3, io);
92
        if (~(u = v = inb(io)) & 3) {
93
                outb(c, io);
94
                return;
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
                return;
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
                        return;
116
                }
117
/*
118
 * And now find the number of mirrors of the port.
119
 */
120
 
121
        for (i = 1; i < 5; i++) {
122
 
123
                //!!!if (check_region(io & (-1 << i), (1 << i)))        /* Don't disturb anyone */
124
                //!!!   break;
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
                wait_ms(3);
130
 
131
                if (b > 300)                                    /* We allow 30% difference */
132
                        break;
133
        }
134
 
135
        i--;
136
 
137
        if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
138
                printk(KERN_ERR "ns558: Memory allocation failed.\n");
139
                return;
140
        }
141
        memset(port, 0, sizeof(struct ns558));
142
 
143
        port->type = NS558_ISA;
144
        port->size = (1 << i);
145
        port->gameport.io = io;
146
        port->gameport.phys = port->phys;
147
        port->gameport.name = port->name;
148
        port->gameport.id.bustype = BUS_ISA;
149
 
150
        sprintf26(port->phys, "isa%04x/gameport0", io & (-1 << i));
151
        sprintf26(port->name, "NS558 ISA");
152
 
153
        request_region(io & (-1 << i), (1 << i), "ns558-isa");
154
 
155
        gameport_register_port(&port->gameport);
156
 
157
        printk(KERN_INFO "gameport: NS558 ISA at %#x", port->gameport.io);
158
        if (port->size > 1) printk(" size %d", port->size);
159
        printk(" speed %d kHz\n", port->gameport.speed);
160
 
161
        list_add(&port->node, &ns558_list);
162
}
163
 
164
#ifdef CONFIG_PNP
165
 
166
static struct pnp_device_id pnp_devids[] = {
167
        { .id = "@P@0001", .driver_data = 0 }, /* ALS 100 */
168
        { .id = "@P@0020", .driver_data = 0 }, /* ALS 200 */
169
        { .id = "@P@1001", .driver_data = 0 }, /* ALS 100+ */
170
        { .id = "@P@2001", .driver_data = 0 }, /* ALS 120 */
171
        { .id = "ASB16fd", .driver_data = 0 }, /* AdLib NSC16 */
172
        { .id = "AZT3001", .driver_data = 0 }, /* AZT1008 */
173
        { .id = "CDC0001", .driver_data = 0 }, /* Opl3-SAx */
174
        { .id = "CSC0001", .driver_data = 0 }, /* CS4232 */
175
        { .id = "CSC000f", .driver_data = 0 }, /* CS4236 */
176
        { .id = "CSC0101", .driver_data = 0 }, /* CS4327 */
177
        { .id = "CTL7001", .driver_data = 0 }, /* SB16 */
178
        { .id = "CTL7002", .driver_data = 0 }, /* AWE64 */
179
        { .id = "CTL7005", .driver_data = 0 }, /* Vibra16 */
180
        { .id = "ENS2020", .driver_data = 0 }, /* SoundscapeVIVO */
181
        { .id = "ESS0001", .driver_data = 0 }, /* ES1869 */
182
        { .id = "ESS0005", .driver_data = 0 }, /* ES1878 */
183
        { .id = "ESS6880", .driver_data = 0 }, /* ES688 */
184
        { .id = "IBM0012", .driver_data = 0 }, /* CS4232 */
185
        { .id = "OPT0001", .driver_data = 0 }, /* OPTi Audio16 */
186
        { .id = "YMH0006", .driver_data = 0 }, /* Opl3-SA */
187
        { .id = "YMH0022", .driver_data = 0 }, /* Opl3-SAx */
188
        { .id = "PNPb02f", .driver_data = 0 }, /* Generic */
189
        { .id = "", },
190
};
191
 
192
MODULE_DEVICE_TABLE(pnp, pnp_devids);
193
 
194
static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
195
{
196
        int ioport, iolen;
197
        struct ns558 *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
        if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
211
                printk(KERN_ERR "ns558: Memory allocation failed.\n");
212
                return -ENOMEM;
213
        }
214
        memset(port, 0, sizeof(struct ns558));
215
 
216
        port->type = NS558_PNP;
217
        port->size = iolen;
218
        port->dev = dev;
219
 
220
        port->gameport.io = ioport;
221
        port->gameport.phys = port->phys;
222
        port->gameport.name = port->name;
223
        port->gameport.id.bustype = BUS_ISAPNP;
224
        port->gameport.id.version = 0x100;
225
 
226
        sprintf26(port->phys, "pnp%s/gameport0", dev->dev.bus_id);
227
        sprintf26(port->name, "%s", "NS558 PnP Gameport");
228
 
229
        gameport_register_port(&port->gameport);
230
 
231
        printk(KERN_INFO "gameport: NS558 PnP at pnp%s io %#x",
232
                dev->dev.bus_id, port->gameport.io);
233
        if (iolen > 1) printk(" size %d", iolen);
234
        printk(" speed %d kHz\n", port->gameport.speed);
235
 
236
        list_add_tail(&port->node, &ns558_list);
237
        return 0;
238
}
239
 
240
static struct pnp_driver ns558_pnp_driver = {
241
        .name           = "ns558",
242
        .id_table       = pnp_devids,
243
        .probe          = ns558_pnp_probe,
244
};
245
 
246
#else
247
 
248
static struct pnp_driver ns558_pnp_driver;
249
 
250
#endif
251
 
252
int __init ns558_init(void)
253
{
254
        int i = 0;
255
 
256
/*
257
 * Probe for ISA ports.
258
 */
259
 
260
        while (ns558_isa_portlist[i])
261
                ns558_isa_probe(ns558_isa_portlist[i++]);
262
 
263
        pnp_register_driver(&ns558_pnp_driver);
264
        return list_empty(&ns558_list) ? -ENODEV : 0;
265
}
266
 
267
void __exit ns558_exit(void)
268
{
269
        struct ns558 *port;
270
 
271
        list_for_each_entry(port, &ns558_list, node) {
272
                gameport_unregister_port(&port->gameport);
273
                switch (port->type) {
274
 
275
#ifdef CONFIG_PNP
276
                        case NS558_PNP:
277
                                /* fall through */
278
#endif
279
                        case NS558_ISA:
280
                                release_region(port->gameport.io & ~(port->size - 1), port->size);
281
                                break;
282
 
283
                        default:
284
                                break;
285
                }
286
        }
287
        pnp_unregister_driver(&ns558_pnp_driver);
288
}
289
 
290
module_init(ns558_init);
291
module_exit(ns558_exit);