summaryrefslogtreecommitdiffstats
path: root/drivers/char/joystick/pcigame.c
blob: 0348ba08b37306a0f7bc26891bbe8a65b43643fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * $Id: pcigame.c,v 1.6 2000/05/25 12:05:24 vojtech Exp $
 *
 *  Copyright (c) 2000 Vojtech Pavlik
 *
 *  Based on the work of:
 *	Raymond Ingles
 *
 *  Sponsored by SuSE
 */

/*
 * Trident 4DWave and Aureal Vortex gameport driver for Linux
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Should you need to contact me, the author, you can do so either by
 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
 */

#include <asm/io.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/pci_ids.h>
#include <linux/init.h>
#include <linux/gameport.h>

#define PCI_VENDOR_ID_AUREAL	0x12eb

#define PCIGAME_DATA_WAIT	20	/* 20 ms */

#define PCIGAME_4DWAVE		0
#define PCIGAME_VORTEX		1
#define PCIGAME_VORTEX2		2

struct pcigame_data {
	int gcr;	/* Gameport control register */
	int legacy;	/* Legacy port location */
	int axes;	/* Axes start */
	int axsize;	/* Axis field size */
	int axmax;	/* Axis field max value */
	int adcmode;	/* Value to enable ADC mode in GCR */
};

static struct pcigame_data pcigame_data[] __devinitdata =
{{ 0x00030, 0x00031, 0x00034, 2, 0xffff, 0x80 },
 { 0x1100c, 0x11008, 0x11010, 4, 0x1fff, 0x40 },
 { 0x2880c, 0x28808, 0x28810, 4, 0x1fff, 0x40 },
 { 0 }};

struct pcigame {
	struct gameport gameport;
	struct pci_dev *dev;
        unsigned char *base;
	struct pcigame_data *data;
};

static unsigned char pcigame_read(struct gameport *gameport)
{
	struct pcigame *pcigame = gameport->driver;
	return readb(pcigame->base + pcigame->data->legacy);
}

static void pcigame_trigger(struct gameport *gameport)
{
	struct pcigame *pcigame = gameport->driver;
	writeb(0xff, pcigame->base + pcigame->data->legacy);
}

static int pcigame_cooked_read(struct gameport *gameport, int *axes, int *buttons)
{
        struct pcigame *pcigame = gameport->driver;
	int i;

	*buttons = (~readb(pcigame->base + pcigame->data->legacy) >> 4) & 0xf;

	for (i = 0; i < 4; i++) {
		axes[i] = readw(pcigame->base + pcigame->data->axes + i * pcigame->data->axsize);
		if (axes[i] == pcigame->data->axmax) axes[i] = -1;
	}
        
        return 0;
}

static int pcigame_open(struct gameport *gameport, int mode)
{
	struct pcigame *pcigame = gameport->driver;

	switch (mode) {
		case GAMEPORT_MODE_COOKED:
			writeb(pcigame->data->adcmode, pcigame->base + pcigame->data->gcr);
			wait_ms(PCIGAME_DATA_WAIT);
			return 0;
		case GAMEPORT_MODE_RAW:
			writeb(0, pcigame->base + pcigame->data->gcr);
			return 0;
		default:
			return -1;
	}

	return 0;
}

static int __devinit pcigame_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
	struct pcigame *pcigame;
	int i;

	if (!(pcigame = kmalloc(sizeof(struct pcigame), GFP_KERNEL)))
		return -1;
        memset(pcigame, 0, sizeof(struct pcigame));


	pcigame->data = pcigame_data + id->driver_data;

	pcigame->dev = dev;
	dev->driver_data = pcigame;

	pcigame->gameport.driver = pcigame;
	pcigame->gameport.type = GAMEPORT_EXT;
	pcigame->gameport.fuzz = 64;
	
	pcigame->gameport.read = pcigame_read;
	pcigame->gameport.trigger = pcigame_trigger;
	pcigame->gameport.cooked_read = pcigame_cooked_read;
	pcigame->gameport.open = pcigame_open;

	for (i = 0; i < 6; i++)
		if (~pci_resource_flags(dev, i) & IORESOURCE_IO)
			break;

	pci_enable_device(dev);

	pcigame->base = ioremap(pci_resource_start(pcigame->dev, i),
				pci_resource_len(pcigame->dev, i));

	gameport_register_port(&pcigame->gameport);
	
	printk(KERN_INFO "gameport%d: %s at pci%02x:%02x.%x speed %d kHz\n",
		pcigame->gameport.number, dev->name, dev->bus->number,
			PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), pcigame->gameport.speed);

	return 0;
}

static void __devexit pcigame_remove(struct pci_dev *dev)
{
	struct pcigame *pcigame = dev->driver_data;
	gameport_unregister_port(&pcigame->gameport);
	iounmap(pcigame->base);
	kfree(pcigame);
}

static struct pci_device_id pcigame_id_table[] __devinitdata =
{{ PCI_VENDOR_ID_TRIDENT, 0x2000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PCIGAME_4DWAVE  },
 { PCI_VENDOR_ID_TRIDENT, 0x2001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PCIGAME_4DWAVE  },
 { PCI_VENDOR_ID_AUREAL,  0x0001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PCIGAME_VORTEX  },
 { PCI_VENDOR_ID_AUREAL,  0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PCIGAME_VORTEX2 },
 { 0 }};

static struct pci_driver pcigame_driver = {
	name:		"pcigame",
	id_table:	pcigame_id_table,
	probe:		pcigame_probe,
	remove:		pcigame_remove,
};

int __init pcigame_init(void)
{
	return pci_module_init(&pcigame_driver);
}

void __exit pcigame_exit(void)
{
	pci_unregister_driver(&pcigame_driver);
}

module_init(pcigame_init);
module_exit(pcigame_exit);