ASoC: Fix Blackfin I2S _pointer() implementation return in bounds values
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / gameport / lightning.c
blob85d6ee09f11f4b7881b66acc9eaceb068b0bde7d
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>
38 #define L4_PORT 0x201
39 #define L4_SELECT_ANALOG 0xa4
40 #define L4_SELECT_DIGITAL 0xa5
41 #define L4_SELECT_SECONDARY 0xa6
42 #define L4_CMD_ID 0x80
43 #define L4_CMD_GETCAL 0x92
44 #define L4_CMD_SETCAL 0x93
45 #define L4_ID 0x04
46 #define L4_BUSY 0x01
47 #define L4_TIMEOUT 80 /* 80 us */
49 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
50 MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver");
51 MODULE_LICENSE("GPL");
53 struct l4 {
54 struct gameport *gameport;
55 unsigned char port;
58 static struct l4 l4_ports[8];
61 * l4_wait_ready() waits for the L4 to become ready.
64 static int l4_wait_ready(void)
66 unsigned int t = L4_TIMEOUT;
68 while ((inb(L4_PORT) & L4_BUSY) && t > 0) t--;
69 return -(t <= 0);
73 * l4_cooked_read() reads data from the Lightning 4.
76 static int l4_cooked_read(struct gameport *gameport, int *axes, int *buttons)
78 struct l4 *l4 = gameport->port_data;
79 unsigned char status;
80 int i, result = -1;
82 outb(L4_SELECT_ANALOG, L4_PORT);
83 outb(L4_SELECT_DIGITAL + (l4->port >> 2), L4_PORT);
85 if (inb(L4_PORT) & L4_BUSY) goto fail;
86 outb(l4->port & 3, L4_PORT);
88 if (l4_wait_ready()) goto fail;
89 status = inb(L4_PORT);
91 for (i = 0; i < 4; i++)
92 if (status & (1 << i)) {
93 if (l4_wait_ready()) goto fail;
94 axes[i] = inb(L4_PORT);
95 if (axes[i] > 252) axes[i] = -1;
98 if (status & 0x10) {
99 if (l4_wait_ready()) goto fail;
100 *buttons = inb(L4_PORT) & 0x0f;
103 result = 0;
105 fail: outb(L4_SELECT_ANALOG, L4_PORT);
106 return result;
109 static int l4_open(struct gameport *gameport, int mode)
111 struct l4 *l4 = gameport->port_data;
113 if (l4->port != 0 && mode != GAMEPORT_MODE_COOKED)
114 return -1;
115 outb(L4_SELECT_ANALOG, L4_PORT);
116 return 0;
120 * l4_getcal() reads the L4 with calibration values.
123 static int l4_getcal(int port, int *cal)
125 int i, result = -1;
127 outb(L4_SELECT_ANALOG, L4_PORT);
128 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
129 if (inb(L4_PORT) & L4_BUSY)
130 goto out;
132 outb(L4_CMD_GETCAL, L4_PORT);
133 if (l4_wait_ready())
134 goto out;
136 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
137 goto out;
139 if (l4_wait_ready())
140 goto out;
141 outb(port & 3, L4_PORT);
143 for (i = 0; i < 4; i++) {
144 if (l4_wait_ready())
145 goto out;
146 cal[i] = inb(L4_PORT);
149 result = 0;
151 out: outb(L4_SELECT_ANALOG, L4_PORT);
152 return result;
156 * l4_setcal() programs the L4 with calibration values.
159 static int l4_setcal(int port, int *cal)
161 int i, result = -1;
163 outb(L4_SELECT_ANALOG, L4_PORT);
164 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
165 if (inb(L4_PORT) & L4_BUSY)
166 goto out;
168 outb(L4_CMD_SETCAL, L4_PORT);
169 if (l4_wait_ready())
170 goto out;
172 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
173 goto out;
175 if (l4_wait_ready())
176 goto out;
177 outb(port & 3, L4_PORT);
179 for (i = 0; i < 4; i++) {
180 if (l4_wait_ready())
181 goto out;
182 outb(cal[i], L4_PORT);
185 result = 0;
187 out: outb(L4_SELECT_ANALOG, L4_PORT);
188 return result;
192 * l4_calibrate() calibrates the L4 for the attached device, so
193 * that the device's resistance fits into the L4's 8-bit range.
196 static int l4_calibrate(struct gameport *gameport, int *axes, int *max)
198 int i, t;
199 int cal[4];
200 struct l4 *l4 = gameport->port_data;
202 if (l4_getcal(l4->port, cal))
203 return -1;
205 for (i = 0; i < 4; i++) {
206 t = (max[i] * cal[i]) / 200;
207 t = (t < 1) ? 1 : ((t > 255) ? 255 : t);
208 axes[i] = (axes[i] < 0) ? -1 : (axes[i] * cal[i]) / t;
209 axes[i] = (axes[i] > 252) ? 252 : axes[i];
210 cal[i] = t;
213 if (l4_setcal(l4->port, cal))
214 return -1;
216 return 0;
219 static int __init l4_create_ports(int card_no)
221 struct l4 *l4;
222 struct gameport *port;
223 int i, idx;
225 for (i = 0; i < 4; i++) {
227 idx = card_no * 4 + i;
228 l4 = &l4_ports[idx];
230 if (!(l4->gameport = port = gameport_allocate_port())) {
231 printk(KERN_ERR "lightning: Memory allocation failed\n");
232 while (--i >= 0) {
233 gameport_free_port(l4->gameport);
234 l4->gameport = NULL;
236 return -ENOMEM;
238 l4->port = idx;
240 port->port_data = l4;
241 port->open = l4_open;
242 port->cooked_read = l4_cooked_read;
243 port->calibrate = l4_calibrate;
245 gameport_set_name(port, "PDPI Lightning 4");
246 gameport_set_phys(port, "isa%04x/gameport%d", L4_PORT, idx);
248 if (idx == 0)
249 port->io = L4_PORT;
252 return 0;
255 static int __init l4_add_card(int card_no)
257 int cal[4] = { 255, 255, 255, 255 };
258 int i, rev, result;
259 struct l4 *l4;
261 outb(L4_SELECT_ANALOG, L4_PORT);
262 outb(L4_SELECT_DIGITAL + card_no, L4_PORT);
264 if (inb(L4_PORT) & L4_BUSY)
265 return -1;
266 outb(L4_CMD_ID, L4_PORT);
268 if (l4_wait_ready())
269 return -1;
271 if (inb(L4_PORT) != L4_SELECT_DIGITAL + card_no)
272 return -1;
274 if (l4_wait_ready())
275 return -1;
276 if (inb(L4_PORT) != L4_ID)
277 return -1;
279 if (l4_wait_ready())
280 return -1;
281 rev = inb(L4_PORT);
283 if (!rev)
284 return -1;
286 result = l4_create_ports(card_no);
287 if (result)
288 return result;
290 printk(KERN_INFO "gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n",
291 card_no ? "secondary" : "primary", rev >> 4, rev, L4_PORT);
293 for (i = 0; i < 4; i++) {
294 l4 = &l4_ports[card_no * 4 + i];
296 if (rev > 0x28) /* on 2.9+ the setcal command works correctly */
297 l4_setcal(l4->port, cal);
298 gameport_register_port(l4->gameport);
301 return 0;
304 static int __init l4_init(void)
306 int i, cards = 0;
308 if (!request_region(L4_PORT, 1, "lightning"))
309 return -EBUSY;
311 for (i = 0; i < 2; i++)
312 if (l4_add_card(i) == 0)
313 cards++;
315 outb(L4_SELECT_ANALOG, L4_PORT);
317 if (!cards) {
318 release_region(L4_PORT, 1);
319 return -ENODEV;
322 return 0;
325 static void __exit l4_exit(void)
327 int i;
328 int cal[4] = { 59, 59, 59, 59 };
330 for (i = 0; i < 8; i++)
331 if (l4_ports[i].gameport) {
332 l4_setcal(l4_ports[i].port, cal);
333 gameport_unregister_port(l4_ports[i].gameport);
336 outb(L4_SELECT_ANALOG, L4_PORT);
337 release_region(L4_PORT, 1);
340 module_init(l4_init);
341 module_exit(l4_exit);