ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / input / gameport / vortex.c
1 /*
2  * $Id: vortex.c,v 1.5 2002/07/01 15:39:30 vojtech Exp $
3  *
4  *  Copyright (c) 2000-2001 Vojtech Pavlik
5  *
6  *  Based on the work of:
7  *      Raymond Ingles
8  */
9
10 /*
11  * Trident 4DWave and Aureal Vortex gameport driver for Linux
12  */
13
14 /*
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  * Should you need to contact me, the author, you can do so either by
30  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
31  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
32  */
33
34 #include <asm/io.h>
35 #include <linux/delay.h>
36 #include <linux/errno.h>
37 #include <linux/ioport.h>
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/pci.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/gameport.h>
44
45 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
46 MODULE_DESCRIPTION("Aureal Vortex and Vortex2 gameport driver");
47 MODULE_LICENSE("GPL");
48
49 #define VORTEX_GCR              0x0c    /* Gameport control register */
50 #define VORTEX_LEG              0x08    /* Legacy port location */
51 #define VORTEX_AXD              0x10    /* Axes start */
52 #define VORTEX_DATA_WAIT        20      /* 20 ms */
53
54 struct vortex {
55         struct gameport gameport;
56         struct pci_dev *dev;
57         unsigned char *base;
58         unsigned char *io;
59         char phys[32];
60 };
61
62 static unsigned char vortex_read(struct gameport *gameport)
63 {
64         struct vortex *vortex = gameport->driver;
65         return readb(vortex->io + VORTEX_LEG);
66 }
67
68 static void vortex_trigger(struct gameport *gameport)
69 {
70         struct vortex *vortex = gameport->driver;
71         writeb(0xff, vortex->io + VORTEX_LEG);
72 }
73
74 static int vortex_cooked_read(struct gameport *gameport, int *axes, int *buttons)
75 {
76         struct vortex *vortex = gameport->driver;
77         int i;
78
79         *buttons = (~readb(vortex->base + VORTEX_LEG) >> 4) & 0xf;
80
81         for (i = 0; i < 4; i++) {
82                 axes[i] = readw(vortex->io + VORTEX_AXD + i * sizeof(u32));
83                 if (axes[i] == 0x1fff) axes[i] = -1;
84         }
85         
86         return 0;
87 }
88
89 static int vortex_open(struct gameport *gameport, int mode)
90 {
91         struct vortex *vortex = gameport->driver;
92
93         switch (mode) {
94                 case GAMEPORT_MODE_COOKED:
95                         writeb(0x40, vortex->io + VORTEX_GCR);
96                         wait_ms(VORTEX_DATA_WAIT);
97                         return 0;
98                 case GAMEPORT_MODE_RAW:
99                         writeb(0x00, vortex->io + VORTEX_GCR);
100                         return 0;
101                 default:
102                         return -1;
103         }
104
105         return 0;
106 }
107
108 static int __devinit vortex_probe(struct pci_dev *dev, const struct pci_device_id *id)
109 {
110         struct vortex *vortex;
111         int i;
112
113         if (!(vortex = kmalloc(sizeof(struct vortex), GFP_KERNEL)))
114                 return -1;
115         memset(vortex, 0, sizeof(struct vortex));
116
117         vortex->dev = dev;
118         sprintf(vortex->phys, "pci%s/gameport0", pci_name(dev));
119
120         pci_set_drvdata(dev, vortex);
121
122         vortex->gameport.driver = vortex;
123         vortex->gameport.fuzz = 64;
124         
125         vortex->gameport.read = vortex_read;
126         vortex->gameport.trigger = vortex_trigger;
127         vortex->gameport.cooked_read = vortex_cooked_read;
128         vortex->gameport.open = vortex_open;
129
130         vortex->gameport.name = pci_name(dev);
131         vortex->gameport.phys = vortex->phys;
132         vortex->gameport.id.bustype = BUS_PCI;
133         vortex->gameport.id.vendor = dev->vendor;
134         vortex->gameport.id.product = dev->device;
135
136         for (i = 0; i < 6; i++)
137                 if (~pci_resource_flags(dev, i) & IORESOURCE_IO)
138                         break;
139
140         pci_enable_device(dev);
141
142         vortex->base = ioremap(pci_resource_start(vortex->dev, i),
143                                 pci_resource_len(vortex->dev, i));
144         vortex->io = vortex->base + id->driver_data;
145
146         gameport_register_port(&vortex->gameport);
147         
148         printk(KERN_INFO "gameport at pci%s speed %d kHz\n",
149                 pci_name(dev), vortex->gameport.speed);
150
151         return 0;
152 }
153
154 static void __devexit vortex_remove(struct pci_dev *dev)
155 {
156         struct vortex *vortex = pci_get_drvdata(dev);
157         gameport_unregister_port(&vortex->gameport);
158         iounmap(vortex->base);
159         kfree(vortex);
160 }
161
162 static struct pci_device_id vortex_id_table[] =
163 {{ 0x12eb, 0x0001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0x11000 },
164  { 0x12eb, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0x28800 },
165  { 0 }};
166
167 static struct pci_driver vortex_driver = {
168         .name =         "vortex",
169         .id_table =     vortex_id_table,
170         .probe =        vortex_probe,
171         .remove =       __devexit_p(vortex_remove),
172 };
173
174 int __init vortex_init(void)
175 {
176         return pci_module_init(&vortex_driver);
177 }
178
179 void __exit vortex_exit(void)
180 {
181         pci_unregister_driver(&vortex_driver);
182 }
183
184 module_init(vortex_init);
185 module_exit(vortex_exit);