Import 2.4.0-test2pre6
[davej-history.git] / drivers / char / joystick / joy-pci.c
blobfbf9125e5115c100f8422c128f3f74eeb372d36b
1 /*
2 * joy-pci.c Version 0.4.0
4 * Copyright (c) 1999 Raymond Ingles
5 * Copyright (c) 1999 Vojtech Pavlik
7 * Sponsored by SuSE
8 */
11 * This is a module for the Linux joystick driver, supporting the
12 * gameports on Trident 4DWave and Aureal Vortex soundcards, and
13 * analog joysticks connected to them.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * Should you need to contact me, the author, you can do so either by
32 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
33 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
36 #include <asm/io.h>
37 #include <linux/delay.h>
38 #include <linux/errno.h>
39 #include <linux/ioport.h>
40 #include <linux/joystick.h>
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/string.h>
44 #include <linux/pci.h>
45 #include <linux/pci_ids.h>
46 #include <linux/init.h>
48 MODULE_AUTHOR("Raymond Ingles <sorceror@tir.com>");
49 MODULE_PARM(js_pci, "3-32i");
51 #define NUM_CARDS 8
52 static int js_pci[NUM_CARDS * 4] __initdata = { -1,0,0,0,-1,0,0,0,-1,0,0,0,-1,0,0,0,
53 -1,0,0,0,-1,0,0,0,-1,0,0,0,-1,0,0,0 };
55 static struct js_port * js_pci_port __initdata = NULL;
57 #include "joy-analog.h"
59 struct js_pci_info;
60 typedef void (*js_pci_func)(struct js_pci_info *);
62 struct js_pci_data {
63 int vendor; /* PCI Vendor ID */
64 int model; /* PCI Model ID */
65 int size; /* Memory / IO region size */
66 int lcr; /* Aureal Legacy Control Register */
67 int gcr; /* Gameport control register */
68 int buttons; /* Buttons location */
69 int axes; /* Axes start */
70 int axsize; /* Axis field size */
71 int axmax; /* Axis field max value */
72 js_pci_func init;
73 js_pci_func cleanup;
74 char *name;
77 struct js_pci_info {
78 unsigned char *base;
79 struct pci_dev *pci_p;
80 __u32 lcr;
81 struct js_pci_data *data;
82 struct js_an_info an;
86 * js_pci_*_init() sets the info->base field, disables legacy gameports,
87 * and enables the enhanced ones.
90 static void js_pci_4dwave_init(struct js_pci_info *info)
92 info->base = ioremap(BASE_ADDRESS(info->pci_p, 1), info->data->size);
93 pci_read_config_word(info->pci_p, info->data->lcr, (unsigned short *)&info->lcr);
94 pci_write_config_word(info->pci_p, info->data->lcr, info->lcr & ~0x20);
95 writeb(0x80, info->base + info->data->gcr);
98 static void js_pci_vortex_init(struct js_pci_info *info)
100 info->base = ioremap(BASE_ADDRESS(info->pci_p, 0), info->data->size);
101 info->lcr = readl(info->base + info->data->lcr);
102 writel(info->lcr & ~0x8, info->base + info->data->lcr);
103 writel(0x40, info->base + info->data->gcr);
107 * js_pci_*_cleanup does the opposite of the above functions.
110 static void js_pci_4dwave_cleanup(struct js_pci_info *info)
112 pci_write_config_word(info->pci_p, info->data->lcr, info->lcr);
113 writeb(0x00, info->base + info->data->gcr);
114 iounmap(info->base);
117 static void js_pci_vortex_cleanup(struct js_pci_info *info)
119 writel(info->lcr, info->base + info->data->lcr);
120 writel(0x00, info->base + info->data->gcr);
121 iounmap(info->base);
124 static struct js_pci_data js_pci_data[] =
125 {{ PCI_VENDOR_ID_TRIDENT, 0x2000, 0x10000, 0x00044 ,0x00030, 0x00031, 0x00034, 2, 0xffff,
126 js_pci_4dwave_init, js_pci_4dwave_cleanup, "Trident 4DWave DX" },
127 { PCI_VENDOR_ID_TRIDENT, 0x2001, 0x10000, 0x00044, 0x00030, 0x00031, 0x00034, 2, 0xffff,
128 js_pci_4dwave_init, js_pci_4dwave_cleanup, "Trident 4DWave NX" },
129 { PCI_VENDOR_ID_AUREAL, 0x0001, 0x40000, 0x1280c, 0x1100c, 0x11008, 0x11010, 4, 0x1fff,
130 js_pci_vortex_init, js_pci_vortex_cleanup, "Aureal Vortex1" },
131 { PCI_VENDOR_ID_AUREAL, 0x0002, 0x40000, 0x2a00c, 0x2880c, 0x28808, 0x28810, 4, 0x1fff,
132 js_pci_vortex_init, js_pci_vortex_cleanup, "Aureal Vortex2" },
133 { 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL }};
136 * js_pci_read() reads data from a PCI gameport.
139 static int js_pci_read(void *xinfo, int **axes, int **buttons)
141 struct js_pci_info *info = xinfo;
142 int i;
144 info->an.buttons = ~readb(info->base + info->data->buttons) >> 4;
146 for (i = 0; i < 4; i++)
147 info->an.axes[i] = readw(info->base + info->data->axes + i * info->data->axsize);
149 js_an_decode(&info->an, axes, buttons);
151 return 0;
154 static struct js_port * __init js_pci_probe(struct js_port *port, int type, int number,
155 struct pci_dev *pci_p, struct js_pci_data *data)
157 int i;
158 unsigned char u;
159 int mask0, mask1, numdev;
160 struct js_pci_info iniinfo;
161 struct js_pci_info *info = &iniinfo;
163 mask0 = mask1 = 0;
165 for (i = 0; i < NUM_CARDS; i++)
166 if (js_pci[i * 4] == type && js_pci[i * 4 + 1] == number) {
167 mask0 = js_pci[i * 4 + 2];
168 mask1 = js_pci[i * 4 + 3];
169 if (!mask0 && !mask1) return port;
170 break;
173 memset(info, 0, sizeof(struct js_pci_info));
175 info->data = data;
176 info->pci_p = pci_p;
177 data->init(info);
179 mdelay(10);
180 js_pci_read(info, NULL, NULL);
182 for (i = u = 0; i < 4; i++)
183 if (info->an.axes[i] < info->data->axmax) u |= 1 << i;
185 if ((numdev = js_an_probe_devs(&info->an, u, mask0, mask1, port)) <= 0)
186 return port;
188 port = js_register_port(port, info, numdev, sizeof(struct js_pci_info), js_pci_read);
190 info = port->info;
192 for (i = 0; i < numdev; i++)
193 printk(KERN_WARNING "js%d: %s on %s #%d\n",
194 js_register_device(port, i, js_an_axes(i, &info->an), js_an_buttons(i, &info->an),
195 js_an_name(i, &info->an), THIS_MODULE, NULL, NULL), js_an_name(i, &info->an), data->name, number);
197 js_pci_read(info, port->axes, port->buttons);
198 js_an_init_corr(&info->an, port->axes, port->corr, 32);
200 return port;
203 #ifndef MODULE
204 int __init js_pci_setup(SETUP_PARAM)
206 int i;
207 SETUP_PARSE(NUM_CARDS*4);
208 for (i = 0; i <= ints[0] && i < NUM_CARDS*4; i++)
209 js_pci[i] = ints[i+1];
210 return 1;
212 __setup("js_pci=", js_pci_setup);
213 #endif
215 #ifdef MODULE
216 int init_module(void)
217 #else
218 int __init js_pci_init(void)
219 #endif
221 struct pci_dev *pci_p = NULL;
222 int i, j;
224 for (i = 0; js_pci_data[i].vendor; i++)
225 for (j = 0; (pci_p = pci_find_device(js_pci_data[i].vendor, js_pci_data[i].model, pci_p)); j++)
226 if (pci_enable_device(pci_p) == 0)
227 js_pci_port = js_pci_probe(js_pci_port, i, j, pci_p, js_pci_data + i);
229 if (!js_pci_port) {
230 #ifdef MODULE
231 printk(KERN_WARNING "joy-pci: no joysticks found\n");
232 #endif
233 return -ENODEV;
236 return 0;
239 #ifdef MODULE
240 void cleanup_module(void)
242 int i;
243 struct js_pci_info *info;
245 while (js_pci_port) {
246 for (i = 0; i < js_pci_port->ndevs; i++)
247 if (js_pci_port->devs[i])
248 js_unregister_device(js_pci_port->devs[i]);
249 info = js_pci_port->info;
250 info->data->cleanup(info);
251 js_pci_port = js_unregister_port(js_pci_port);
254 #endif