Subversion Repositories shark

Rev

Rev 494 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
494 giacomo 1
/*
538 mauro 2
 * $Id: ns558.c,v 1.2 2004-03-29 18:27:42 mauro Exp $
494 giacomo 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
        }
538 mauro 96
 
494 giacomo 97
/*
98
 * After a trigger, there must be at least some bits changing.
99
 */
100
 
101
        for (i = 0; i < 1000; i++) v &= inb(io);
102
 
103
        if (u == v) {
104
                outb(c, io);
105
                return;
106
        }
107
        wait_ms(3);
538 mauro 108
 
494 giacomo 109
/*
110
 * After some time (4ms) the axes shouldn't change anymore.
111
 */
112
 
113
        u = inb(io);
114
        for (i = 0; i < 1000; i++)
115
                if ((u ^ inb(io)) & 0xf) {
116
                        outb(c, io);
117
                        return;
118
                }
119
/*
120
 * And now find the number of mirrors of the port.
121
 */
122
 
123
        for (i = 1; i < 5; i++) {
124
 
125
                //!!!if (check_region(io & (-1 << i), (1 << i)))        /* Don't disturb anyone */
126
                //!!!   break;
127
 
128
                outb(0xff, io & (-1 << i));
129
                for (j = b = 0; j < 1000; j++)
130
                        if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++;
131
                wait_ms(3);
132
 
133
                if (b > 300)                                    /* We allow 30% difference */
134
                        break;
135
        }
136
 
137
        i--;
138
 
139
        if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
140
                printk(KERN_ERR "ns558: Memory allocation failed.\n");
141
                return;
142
        }
143
        memset(port, 0, sizeof(struct ns558));
144
 
145
        port->type = NS558_ISA;
146
        port->size = (1 << i);
147
        port->gameport.io = io;
148
        port->gameport.phys = port->phys;
149
        port->gameport.name = port->name;
150
        port->gameport.id.bustype = BUS_ISA;
151
 
152
        sprintf26(port->phys, "isa%04x/gameport0", io & (-1 << i));
153
        sprintf26(port->name, "NS558 ISA");
154
 
155
        request_region(io & (-1 << i), (1 << i), "ns558-isa");
156
 
157
        gameport_register_port(&port->gameport);
158
 
159
        printk(KERN_INFO "gameport: NS558 ISA at %#x", port->gameport.io);
160
        if (port->size > 1) printk(" size %d", port->size);
161
        printk(" speed %d kHz\n", port->gameport.speed);
162
 
163
        list_add(&port->node, &ns558_list);
164
}
165
 
166
#ifdef CONFIG_PNP
167
 
168
static struct pnp_device_id pnp_devids[] = {
169
        { .id = "@P@0001", .driver_data = 0 }, /* ALS 100 */
170
        { .id = "@P@0020", .driver_data = 0 }, /* ALS 200 */
171
        { .id = "@P@1001", .driver_data = 0 }, /* ALS 100+ */
172
        { .id = "@P@2001", .driver_data = 0 }, /* ALS 120 */
173
        { .id = "ASB16fd", .driver_data = 0 }, /* AdLib NSC16 */
174
        { .id = "AZT3001", .driver_data = 0 }, /* AZT1008 */
175
        { .id = "CDC0001", .driver_data = 0 }, /* Opl3-SAx */
176
        { .id = "CSC0001", .driver_data = 0 }, /* CS4232 */
177
        { .id = "CSC000f", .driver_data = 0 }, /* CS4236 */
178
        { .id = "CSC0101", .driver_data = 0 }, /* CS4327 */
179
        { .id = "CTL7001", .driver_data = 0 }, /* SB16 */
180
        { .id = "CTL7002", .driver_data = 0 }, /* AWE64 */
181
        { .id = "CTL7005", .driver_data = 0 }, /* Vibra16 */
182
        { .id = "ENS2020", .driver_data = 0 }, /* SoundscapeVIVO */
183
        { .id = "ESS0001", .driver_data = 0 }, /* ES1869 */
184
        { .id = "ESS0005", .driver_data = 0 }, /* ES1878 */
185
        { .id = "ESS6880", .driver_data = 0 }, /* ES688 */
186
        { .id = "IBM0012", .driver_data = 0 }, /* CS4232 */
187
        { .id = "OPT0001", .driver_data = 0 }, /* OPTi Audio16 */
188
        { .id = "YMH0006", .driver_data = 0 }, /* Opl3-SA */
189
        { .id = "YMH0022", .driver_data = 0 }, /* Opl3-SAx */
190
        { .id = "PNPb02f", .driver_data = 0 }, /* Generic */
191
        { .id = "", },
192
};
193
 
194
MODULE_DEVICE_TABLE(pnp, pnp_devids);
195
 
196
static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
197
{
198
        int ioport, iolen;
199
        struct ns558 *port;
200
 
201
        if (!pnp_port_valid(dev, 0)) {
202
                printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
203
                return -ENODEV;
204
        }
205
 
206
        ioport = pnp_port_start(dev,0);
207
        iolen = pnp_port_len(dev,0);
208
 
209
        if (!request_region(ioport, iolen, "ns558-pnp"))
210
                return -EBUSY;
211
 
212
        if (!(port = kmalloc(sizeof(struct ns558), GFP_KERNEL))) {
213
                printk(KERN_ERR "ns558: Memory allocation failed.\n");
214
                return -ENOMEM;
215
        }
216
        memset(port, 0, sizeof(struct ns558));
217
 
218
        port->type = NS558_PNP;
219
        port->size = iolen;
220
        port->dev = dev;
221
 
222
        port->gameport.io = ioport;
223
        port->gameport.phys = port->phys;
224
        port->gameport.name = port->name;
225
        port->gameport.id.bustype = BUS_ISAPNP;
226
        port->gameport.id.version = 0x100;
227
 
228
        sprintf26(port->phys, "pnp%s/gameport0", dev->dev.bus_id);
229
        sprintf26(port->name, "%s", "NS558 PnP Gameport");
230
 
231
        gameport_register_port(&port->gameport);
232
 
233
        printk(KERN_INFO "gameport: NS558 PnP at pnp%s io %#x",
234
                dev->dev.bus_id, port->gameport.io);
235
        if (iolen > 1) printk(" size %d", iolen);
236
        printk(" speed %d kHz\n", port->gameport.speed);
237
 
238
        list_add_tail(&port->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
 
254
int __init ns558_init(void)
255
{
256
        int i = 0;
257
 
258
/*
259
 * Probe for ISA ports.
260
 */
261
 
262
        while (ns558_isa_portlist[i])
263
                ns558_isa_probe(ns558_isa_portlist[i++]);
264
 
265
        pnp_register_driver(&ns558_pnp_driver);
266
        return list_empty(&ns558_list) ? -ENODEV : 0;
267
}
268
 
269
void __exit ns558_exit(void)
270
{
271
        struct ns558 *port;
272
 
273
        list_for_each_entry(port, &ns558_list, node) {
274
                gameport_unregister_port(&port->gameport);
275
                switch (port->type) {
276
 
277
#ifdef CONFIG_PNP
278
                        case NS558_PNP:
279
                                /* fall through */
280
#endif
281
                        case NS558_ISA:
282
                                release_region(port->gameport.io & ~(port->size - 1), port->size);
283
                                break;
284
 
285
                        default:
286
                                break;
287
                }
288
        }
289
        pnp_unregister_driver(&ns558_pnp_driver);
290
}
291
 
292
module_init(ns558_init);
293
module_exit(ns558_exit);