2 * Copyright (c) 1998-2001 Vojtech Pavlik
6 * PDPI Lightning 4 gamecard driver for Linux.
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
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>
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
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");
55 struct gameport *gameport;
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--;
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;
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;
100 if (l4_wait_ready()) goto fail;
101 *buttons = inb(L4_PORT) & 0x0f;
106 fail: outb(L4_SELECT_ANALOG, L4_PORT);
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)
116 outb(L4_SELECT_ANALOG, L4_PORT);
121 * l4_getcal() reads the L4 with calibration values.
124 static int l4_getcal(int port, int *cal)
128 outb(L4_SELECT_ANALOG, L4_PORT);
129 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
130 if (inb(L4_PORT) & L4_BUSY)
133 outb(L4_CMD_GETCAL, L4_PORT);
137 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
142 outb(port & 3, L4_PORT);
144 for (i = 0; i < 4; i++) {
147 cal[i] = inb(L4_PORT);
152 out: outb(L4_SELECT_ANALOG, L4_PORT);
157 * l4_setcal() programs the L4 with calibration values.
160 static int l4_setcal(int port, int *cal)
164 outb(L4_SELECT_ANALOG, L4_PORT);
165 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
166 if (inb(L4_PORT) & L4_BUSY)
169 outb(L4_CMD_SETCAL, L4_PORT);
173 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
178 outb(port & 3, L4_PORT);
180 for (i = 0; i < 4; i++) {
183 outb(cal[i], L4_PORT);
188 out: outb(L4_SELECT_ANALOG, L4_PORT);
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)
201 struct l4 *l4 = gameport->port_data;
203 if (l4_getcal(l4->port, cal))
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];
214 if (l4_setcal(l4->port, cal))
220 static int __init l4_create_ports(int card_no)
223 struct gameport *port;
226 for (i = 0; i < 4; i++) {
228 idx = card_no * 4 + i;
231 if (!(l4->gameport = port = gameport_allocate_port())) {
232 printk(KERN_ERR "lightning: Memory allocation failed\n");
234 gameport_free_port(l4->gameport);
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);
256 static int __init l4_add_card(int card_no)
258 int cal[4] = { 255, 255, 255, 255 };
262 outb(L4_SELECT_ANALOG, L4_PORT);
263 outb(L4_SELECT_DIGITAL + card_no, L4_PORT);
265 if (inb(L4_PORT) & L4_BUSY)
267 outb(L4_CMD_ID, L4_PORT);
272 if (inb(L4_PORT) != L4_SELECT_DIGITAL + card_no)
277 if (inb(L4_PORT) != L4_ID)
287 result = l4_create_ports(card_no);
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);
305 static int __init l4_init(void)
309 if (!request_region(L4_PORT, 1, "lightning"))
312 for (i = 0; i < 2; i++)
313 if (l4_add_card(i) == 0)
316 outb(L4_SELECT_ANALOG, L4_PORT);
319 release_region(L4_PORT, 1);
326 static void __exit l4_exit(void)
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);