patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / gameport / lightning.c
1 /*
2  * $Id: lightning.c,v 1.20 2002/01/22 20:41:31 vojtech Exp $
3  *
4  *  Copyright (c) 1998-2001 Vojtech Pavlik
5  */
6
7 /*
8  * PDPI Lightning 4 gamecard driver for Linux.
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29  */
30
31 #include <asm/io.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/ioport.h>
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/gameport.h>
39 #include <linux/slab.h>
40
41 #define L4_PORT                 0x201
42 #define L4_SELECT_ANALOG        0xa4
43 #define L4_SELECT_DIGITAL       0xa5
44 #define L4_SELECT_SECONDARY     0xa6
45 #define L4_CMD_ID               0x80
46 #define L4_CMD_GETCAL           0x92
47 #define L4_CMD_SETCAL           0x93
48 #define L4_ID                   0x04
49 #define L4_BUSY                 0x01
50 #define L4_TIMEOUT              80      /* 80 us */
51
52 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
53 MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver");
54 MODULE_LICENSE("GPL");
55
56 struct l4 {
57         struct gameport gameport;
58         unsigned char port;
59         char phys[32];
60 } *l4_port[8];
61
62 char l4_name[] = "PDPI Lightning 4";
63
64 /*
65  * l4_wait_ready() waits for the L4 to become ready.
66  */
67
68 static int l4_wait_ready(void)
69 {
70         unsigned int t;
71         t = L4_TIMEOUT;
72         while ((inb(L4_PORT) & L4_BUSY) && t > 0) t--;
73         return -(t<=0);
74 }
75
76 /*
77  * l4_cooked_read() reads data from the Lightning 4.
78  */
79
80 static int l4_cooked_read(struct gameport *gameport, int *axes, int *buttons)
81 {
82         struct l4 *l4 = gameport->driver;
83         unsigned char status;
84         int i, result = -1;
85
86         outb(L4_SELECT_ANALOG, L4_PORT);
87         outb(L4_SELECT_DIGITAL + (l4->port >> 2), L4_PORT);
88
89         if (inb(L4_PORT) & L4_BUSY) goto fail;
90         outb(l4->port & 3, L4_PORT);
91
92         if (l4_wait_ready()) goto fail;
93         status = inb(L4_PORT);
94
95         for (i = 0; i < 4; i++)
96                 if (status & (1 << i)) {
97                         if (l4_wait_ready()) goto fail;
98                         axes[i] = inb(L4_PORT);
99                         if (axes[i] > 252) axes[i] = -1;
100                 }
101
102         if (status & 0x10) {
103                 if (l4_wait_ready()) goto fail;
104                 *buttons = inb(L4_PORT) & 0x0f;
105         }
106
107         result = 0;
108
109 fail:   outb(L4_SELECT_ANALOG, L4_PORT);
110         return result;
111 }
112
113 static int l4_open(struct gameport *gameport, int mode)
114 {
115         struct l4 *l4 = gameport->driver;
116         if (l4->port != 0 && mode != GAMEPORT_MODE_COOKED)
117                 return -1;
118         outb(L4_SELECT_ANALOG, L4_PORT);
119         return 0;
120 }
121
122 /*
123  * l4_getcal() reads the L4 with calibration values.
124  */
125
126 static int l4_getcal(int port, int *cal)
127 {
128         int i, result = -1;
129
130         outb(L4_SELECT_ANALOG, L4_PORT);
131         outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
132
133         if (inb(L4_PORT) & L4_BUSY) goto fail;
134         outb(L4_CMD_GETCAL, L4_PORT);
135
136         if (l4_wait_ready()) goto fail;
137         if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2)) goto fail;
138
139         if (l4_wait_ready()) goto fail;
140         outb(port & 3, L4_PORT);
141
142         for (i = 0; i < 4; i++) {
143                 if (l4_wait_ready()) goto fail;
144                 cal[i] = inb(L4_PORT);
145         }
146
147         result = 0;
148
149 fail:   outb(L4_SELECT_ANALOG, L4_PORT);
150         return result;
151 }
152
153 /*
154  * l4_setcal() programs the L4 with calibration values.
155  */
156
157 static int l4_setcal(int port, int *cal)
158 {
159         int i, result = -1;
160
161         outb(L4_SELECT_ANALOG, L4_PORT);
162         outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
163
164         if (inb(L4_PORT) & L4_BUSY) goto fail;
165         outb(L4_CMD_SETCAL, L4_PORT);
166
167         if (l4_wait_ready()) goto fail;
168         if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2)) goto fail;
169
170         if (l4_wait_ready()) goto fail;
171         outb(port & 3, L4_PORT);
172
173         for (i = 0; i < 4; i++) {
174                 if (l4_wait_ready()) goto fail;
175                 outb(cal[i], L4_PORT);
176         }
177
178         result = 0;
179
180 fail:   outb(L4_SELECT_ANALOG, L4_PORT);
181         return result;
182 }
183
184 /*
185  * l4_calibrate() calibrates the L4 for the attached device, so
186  * that the device's resistance fits into the L4's 8-bit range.
187  */
188
189 static int l4_calibrate(struct gameport *gameport, int *axes, int *max)
190 {
191         int i, t;
192         int cal[4];
193         struct l4 *l4 = gameport->driver;
194
195         if (l4_getcal(l4->port, cal))
196                 return -1;
197
198         for (i = 0; i < 4; i++) {
199                 t = (max[i] * cal[i]) / 200;
200                 t = (t < 1) ? 1 : ((t > 255) ? 255 : t);
201                 axes[i] = (axes[i] < 0) ? -1 : (axes[i] * cal[i]) / t;
202                 axes[i] = (axes[i] > 252) ? 252 : axes[i];
203                 cal[i] = t;
204         }
205
206         if (l4_setcal(l4->port, cal))
207                 return -1;
208
209         return 0;
210 }
211
212 static int __init l4_init(void)
213 {
214         int cal[4] = {255,255,255,255};
215         int i, j, rev, cards = 0;
216         struct gameport *gameport;
217         struct l4 *l4;
218
219         if (!request_region(L4_PORT, 1, "lightning"))
220                 return -1;
221
222         for (i = 0; i < 2; i++) {
223
224                 outb(L4_SELECT_ANALOG, L4_PORT);
225                 outb(L4_SELECT_DIGITAL + i, L4_PORT);
226
227                 if (inb(L4_PORT) & L4_BUSY) continue;
228                 outb(L4_CMD_ID, L4_PORT);
229
230                 if (l4_wait_ready()) continue;
231                 if (inb(L4_PORT) != L4_SELECT_DIGITAL + i) continue;
232
233                 if (l4_wait_ready()) continue;
234                 if (inb(L4_PORT) != L4_ID) continue;
235
236                 if (l4_wait_ready()) continue;
237                 rev = inb(L4_PORT);
238
239                 if (!rev) continue;
240
241                 if (!(l4_port[i * 4] = kmalloc(sizeof(struct l4) * 4, GFP_KERNEL))) {
242                         printk(KERN_ERR "lightning: Out of memory allocating ports.\n");
243                         continue;
244                 }
245                 memset(l4_port[i * 4], 0, sizeof(struct l4) * 4);
246
247                 for (j = 0; j < 4; j++) {
248
249                         l4 = l4_port[i * 4 + j] = l4_port[i * 4] + j;
250                         l4->port = i * 4 + j;
251
252                         sprintf(l4->phys, "isa%04x/gameport%d", L4_PORT, 4 * i + j);
253
254                         gameport = &l4->gameport;
255                         gameport->driver = l4;
256                         gameport->open = l4_open;
257                         gameport->cooked_read = l4_cooked_read;
258                         gameport->calibrate = l4_calibrate;
259
260                         gameport->name = l4_name;
261                         gameport->phys = l4->phys;
262                         gameport->id.bustype = BUS_ISA;
263
264                         if (!i && !j)
265                                 gameport->io = L4_PORT;
266
267                         if (rev > 0x28)         /* on 2.9+ the setcal command works correctly */
268                                 l4_setcal(l4->port, cal);
269
270                         gameport_register_port(gameport);
271                 }
272
273                 printk(KERN_INFO "gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n",
274                         i ? "secondary" : "primary", rev >> 4, rev, L4_PORT);
275
276                 cards++;
277         }
278
279         outb(L4_SELECT_ANALOG, L4_PORT);
280
281         if (!cards) {
282                 release_region(L4_PORT, 1);
283                 return -1;
284         }
285
286         return 0;
287 }
288
289 static void __exit l4_exit(void)
290 {
291         int i;
292         int cal[4] = {59, 59, 59, 59};
293
294         for (i = 0; i < 8; i++)
295                 if (l4_port[i]) {
296                         l4_setcal(l4_port[i]->port, cal);
297                         gameport_unregister_port(&l4_port[i]->gameport);
298                 }
299         outb(L4_SELECT_ANALOG, L4_PORT);
300         release_region(L4_PORT, 1);
301 }
302
303 module_init(l4_init);
304 module_exit(l4_exit);