rt2x00: Simplify rt2x00_check_rev
[linux/fpc-iii.git] / drivers / input / gameport / lightning.c
blob06ad36ed348329e9ff4d4dabb7141eb07cd61d56
1 /*
2 * Copyright (c) 1998-2001 Vojtech Pavlik
3 */
5 /*
6 * PDPI Lightning 4 gamecard driver for Linux.
7 */
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 #include <asm/io.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
32 #include <linux/ioport.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/gameport.h>
37 #include <linux/slab.h>
39 #define L4_PORT 0x201
40 #define L4_SELECT_ANALOG 0xa4
41 #define L4_SELECT_DIGITAL 0xa5
42 #define L4_SELECT_SECONDARY 0xa6
43 #define L4_CMD_ID 0x80
44 #define L4_CMD_GETCAL 0x92
45 #define L4_CMD_SETCAL 0x93
46 #define L4_ID 0x04
47 #define L4_BUSY 0x01
48 #define L4_TIMEOUT 80 /* 80 us */
50 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
51 MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver");
52 MODULE_LICENSE("GPL");
54 struct l4 {
55 struct gameport *gameport;
56 unsigned char port;
59 static struct l4 l4_ports[8];
62 * l4_wait_ready() waits for the L4 to become ready.
65 static int l4_wait_ready(void)
67 unsigned int t = L4_TIMEOUT;
69 while ((inb(L4_PORT) & L4_BUSY) && t > 0) t--;
70 return -(t <= 0);
74 * l4_cooked_read() reads data from the Lightning 4.
77 static int l4_cooked_read(struct gameport *gameport, int *axes, int *buttons)
79 struct l4 *l4 = gameport->port_data;
80 unsigned char status;
81 int i, result = -1;
83 outb(L4_SELECT_ANALOG, L4_PORT);
84 outb(L4_SELECT_DIGITAL + (l4->port >> 2), L4_PORT);
86 if (inb(L4_PORT) & L4_BUSY) goto fail;
87 outb(l4->port & 3, L4_PORT);
89 if (l4_wait_ready()) goto fail;
90 status = inb(L4_PORT);
92 for (i = 0; i < 4; i++)
93 if (status & (1 << i)) {
94 if (l4_wait_ready()) goto fail;
95 axes[i] = inb(L4_PORT);
96 if (axes[i] > 252) axes[i] = -1;
99 if (status & 0x10) {
100 if (l4_wait_ready()) goto fail;
101 *buttons = inb(L4_PORT) & 0x0f;
104 result = 0;
106 fail: outb(L4_SELECT_ANALOG, L4_PORT);
107 return result;
110 static int l4_open(struct gameport *gameport, int mode)
112 struct l4 *l4 = gameport->port_data;
114 if (l4->port != 0 && mode != GAMEPORT_MODE_COOKED)
115 return -1;
116 outb(L4_SELECT_ANALOG, L4_PORT);
117 return 0;
121 * l4_getcal() reads the L4 with calibration values.
124 static int l4_getcal(int port, int *cal)
126 int i, result = -1;
128 outb(L4_SELECT_ANALOG, L4_PORT);
129 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
130 if (inb(L4_PORT) & L4_BUSY)
131 goto out;
133 outb(L4_CMD_GETCAL, L4_PORT);
134 if (l4_wait_ready())
135 goto out;
137 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
138 goto out;
140 if (l4_wait_ready())
141 goto out;
142 outb(port & 3, L4_PORT);
144 for (i = 0; i < 4; i++) {
145 if (l4_wait_ready())
146 goto out;
147 cal[i] = inb(L4_PORT);
150 result = 0;
152 out: outb(L4_SELECT_ANALOG, L4_PORT);
153 return result;
157 * l4_setcal() programs the L4 with calibration values.
160 static int l4_setcal(int port, int *cal)
162 int i, result = -1;
164 outb(L4_SELECT_ANALOG, L4_PORT);
165 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
166 if (inb(L4_PORT) & L4_BUSY)
167 goto out;
169 outb(L4_CMD_SETCAL, L4_PORT);
170 if (l4_wait_ready())
171 goto out;
173 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
174 goto out;
176 if (l4_wait_ready())
177 goto out;
178 outb(port & 3, L4_PORT);
180 for (i = 0; i < 4; i++) {
181 if (l4_wait_ready())
182 goto out;
183 outb(cal[i], L4_PORT);
186 result = 0;
188 out: outb(L4_SELECT_ANALOG, L4_PORT);
189 return result;
193 * l4_calibrate() calibrates the L4 for the attached device, so
194 * that the device's resistance fits into the L4's 8-bit range.
197 static int l4_calibrate(struct gameport *gameport, int *axes, int *max)
199 int i, t;
200 int cal[4];
201 struct l4 *l4 = gameport->port_data;
203 if (l4_getcal(l4->port, cal))
204 return -1;
206 for (i = 0; i < 4; i++) {
207 t = (max[i] * cal[i]) / 200;
208 t = (t < 1) ? 1 : ((t > 255) ? 255 : t);
209 axes[i] = (axes[i] < 0) ? -1 : (axes[i] * cal[i]) / t;
210 axes[i] = (axes[i] > 252) ? 252 : axes[i];
211 cal[i] = t;
214 if (l4_setcal(l4->port, cal))
215 return -1;
217 return 0;
220 static int __init l4_create_ports(int card_no)
222 struct l4 *l4;
223 struct gameport *port;
224 int i, idx;
226 for (i = 0; i < 4; i++) {
228 idx = card_no * 4 + i;
229 l4 = &l4_ports[idx];
231 if (!(l4->gameport = port = gameport_allocate_port())) {
232 printk(KERN_ERR "lightning: Memory allocation failed\n");
233 while (--i >= 0) {
234 gameport_free_port(l4->gameport);
235 l4->gameport = NULL;
237 return -ENOMEM;
239 l4->port = idx;
241 port->port_data = l4;
242 port->open = l4_open;
243 port->cooked_read = l4_cooked_read;
244 port->calibrate = l4_calibrate;
246 gameport_set_name(port, "PDPI Lightning 4");
247 gameport_set_phys(port, "isa%04x/gameport%d", L4_PORT, idx);
249 if (idx == 0)
250 port->io = L4_PORT;
253 return 0;
256 static int __init l4_add_card(int card_no)
258 int cal[4] = { 255, 255, 255, 255 };
259 int i, rev, result;
260 struct l4 *l4;
262 outb(L4_SELECT_ANALOG, L4_PORT);
263 outb(L4_SELECT_DIGITAL + card_no, L4_PORT);
265 if (inb(L4_PORT) & L4_BUSY)
266 return -1;
267 outb(L4_CMD_ID, L4_PORT);
269 if (l4_wait_ready())
270 return -1;
272 if (inb(L4_PORT) != L4_SELECT_DIGITAL + card_no)
273 return -1;
275 if (l4_wait_ready())
276 return -1;
277 if (inb(L4_PORT) != L4_ID)
278 return -1;
280 if (l4_wait_ready())
281 return -1;
282 rev = inb(L4_PORT);
284 if (!rev)
285 return -1;
287 result = l4_create_ports(card_no);
288 if (result)
289 return result;
291 printk(KERN_INFO "gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n",
292 card_no ? "secondary" : "primary", rev >> 4, rev, L4_PORT);
294 for (i = 0; i < 4; i++) {
295 l4 = &l4_ports[card_no * 4 + i];
297 if (rev > 0x28) /* on 2.9+ the setcal command works correctly */
298 l4_setcal(l4->port, cal);
299 gameport_register_port(l4->gameport);
302 return 0;
305 static int __init l4_init(void)
307 int i, cards = 0;
309 if (!request_region(L4_PORT, 1, "lightning"))
310 return -EBUSY;
312 for (i = 0; i < 2; i++)
313 if (l4_add_card(i) == 0)
314 cards++;
316 outb(L4_SELECT_ANALOG, L4_PORT);
318 if (!cards) {
319 release_region(L4_PORT, 1);
320 return -ENODEV;
323 return 0;
326 static void __exit l4_exit(void)
328 int i;
329 int cal[4] = { 59, 59, 59, 59 };
331 for (i = 0; i < 8; i++)
332 if (l4_ports[i].gameport) {
333 l4_setcal(l4_ports[i].port, cal);
334 gameport_unregister_port(l4_ports[i].gameport);
337 outb(L4_SELECT_ANALOG, L4_PORT);
338 release_region(L4_PORT, 1);
341 module_init(l4_init);
342 module_exit(l4_exit);