mmc: core: Reset HPI enabled state during re-init and in case of errors
[linux/fpc-iii.git] / drivers / input / gameport / lightning.c
blobc6e74c7945cb43120100df453e66750f4c2f1ad5
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
25 #include <asm/io.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/ioport.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/gameport.h>
34 #define L4_PORT 0x201
35 #define L4_SELECT_ANALOG 0xa4
36 #define L4_SELECT_DIGITAL 0xa5
37 #define L4_SELECT_SECONDARY 0xa6
38 #define L4_CMD_ID 0x80
39 #define L4_CMD_GETCAL 0x92
40 #define L4_CMD_SETCAL 0x93
41 #define L4_ID 0x04
42 #define L4_BUSY 0x01
43 #define L4_TIMEOUT 80 /* 80 us */
45 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
46 MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver");
47 MODULE_LICENSE("GPL");
49 struct l4 {
50 struct gameport *gameport;
51 unsigned char port;
54 static struct l4 l4_ports[8];
57 * l4_wait_ready() waits for the L4 to become ready.
60 static int l4_wait_ready(void)
62 unsigned int t = L4_TIMEOUT;
64 while ((inb(L4_PORT) & L4_BUSY) && t > 0) t--;
65 return -(t <= 0);
69 * l4_cooked_read() reads data from the Lightning 4.
72 static int l4_cooked_read(struct gameport *gameport, int *axes, int *buttons)
74 struct l4 *l4 = gameport->port_data;
75 unsigned char status;
76 int i, result = -1;
78 outb(L4_SELECT_ANALOG, L4_PORT);
79 outb(L4_SELECT_DIGITAL + (l4->port >> 2), L4_PORT);
81 if (inb(L4_PORT) & L4_BUSY) goto fail;
82 outb(l4->port & 3, L4_PORT);
84 if (l4_wait_ready()) goto fail;
85 status = inb(L4_PORT);
87 for (i = 0; i < 4; i++)
88 if (status & (1 << i)) {
89 if (l4_wait_ready()) goto fail;
90 axes[i] = inb(L4_PORT);
91 if (axes[i] > 252) axes[i] = -1;
94 if (status & 0x10) {
95 if (l4_wait_ready()) goto fail;
96 *buttons = inb(L4_PORT) & 0x0f;
99 result = 0;
101 fail: outb(L4_SELECT_ANALOG, L4_PORT);
102 return result;
105 static int l4_open(struct gameport *gameport, int mode)
107 struct l4 *l4 = gameport->port_data;
109 if (l4->port != 0 && mode != GAMEPORT_MODE_COOKED)
110 return -1;
111 outb(L4_SELECT_ANALOG, L4_PORT);
112 return 0;
116 * l4_getcal() reads the L4 with calibration values.
119 static int l4_getcal(int port, int *cal)
121 int i, result = -1;
123 outb(L4_SELECT_ANALOG, L4_PORT);
124 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
125 if (inb(L4_PORT) & L4_BUSY)
126 goto out;
128 outb(L4_CMD_GETCAL, L4_PORT);
129 if (l4_wait_ready())
130 goto out;
132 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
133 goto out;
135 if (l4_wait_ready())
136 goto out;
137 outb(port & 3, L4_PORT);
139 for (i = 0; i < 4; i++) {
140 if (l4_wait_ready())
141 goto out;
142 cal[i] = inb(L4_PORT);
145 result = 0;
147 out: outb(L4_SELECT_ANALOG, L4_PORT);
148 return result;
152 * l4_setcal() programs the L4 with calibration values.
155 static int l4_setcal(int port, int *cal)
157 int i, result = -1;
159 outb(L4_SELECT_ANALOG, L4_PORT);
160 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
161 if (inb(L4_PORT) & L4_BUSY)
162 goto out;
164 outb(L4_CMD_SETCAL, L4_PORT);
165 if (l4_wait_ready())
166 goto out;
168 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
169 goto out;
171 if (l4_wait_ready())
172 goto out;
173 outb(port & 3, L4_PORT);
175 for (i = 0; i < 4; i++) {
176 if (l4_wait_ready())
177 goto out;
178 outb(cal[i], L4_PORT);
181 result = 0;
183 out: outb(L4_SELECT_ANALOG, L4_PORT);
184 return result;
188 * l4_calibrate() calibrates the L4 for the attached device, so
189 * that the device's resistance fits into the L4's 8-bit range.
192 static int l4_calibrate(struct gameport *gameport, int *axes, int *max)
194 int i, t;
195 int cal[4];
196 struct l4 *l4 = gameport->port_data;
198 if (l4_getcal(l4->port, cal))
199 return -1;
201 for (i = 0; i < 4; i++) {
202 t = (max[i] * cal[i]) / 200;
203 t = (t < 1) ? 1 : ((t > 255) ? 255 : t);
204 axes[i] = (axes[i] < 0) ? -1 : (axes[i] * cal[i]) / t;
205 axes[i] = (axes[i] > 252) ? 252 : axes[i];
206 cal[i] = t;
209 if (l4_setcal(l4->port, cal))
210 return -1;
212 return 0;
215 static int __init l4_create_ports(int card_no)
217 struct l4 *l4;
218 struct gameport *port;
219 int i, idx;
221 for (i = 0; i < 4; i++) {
223 idx = card_no * 4 + i;
224 l4 = &l4_ports[idx];
226 if (!(l4->gameport = port = gameport_allocate_port())) {
227 printk(KERN_ERR "lightning: Memory allocation failed\n");
228 while (--i >= 0) {
229 gameport_free_port(l4->gameport);
230 l4->gameport = NULL;
232 return -ENOMEM;
234 l4->port = idx;
236 port->port_data = l4;
237 port->open = l4_open;
238 port->cooked_read = l4_cooked_read;
239 port->calibrate = l4_calibrate;
241 gameport_set_name(port, "PDPI Lightning 4");
242 gameport_set_phys(port, "isa%04x/gameport%d", L4_PORT, idx);
244 if (idx == 0)
245 port->io = L4_PORT;
248 return 0;
251 static int __init l4_add_card(int card_no)
253 int cal[4] = { 255, 255, 255, 255 };
254 int i, rev, result;
255 struct l4 *l4;
257 outb(L4_SELECT_ANALOG, L4_PORT);
258 outb(L4_SELECT_DIGITAL + card_no, L4_PORT);
260 if (inb(L4_PORT) & L4_BUSY)
261 return -1;
262 outb(L4_CMD_ID, L4_PORT);
264 if (l4_wait_ready())
265 return -1;
267 if (inb(L4_PORT) != L4_SELECT_DIGITAL + card_no)
268 return -1;
270 if (l4_wait_ready())
271 return -1;
272 if (inb(L4_PORT) != L4_ID)
273 return -1;
275 if (l4_wait_ready())
276 return -1;
277 rev = inb(L4_PORT);
279 if (!rev)
280 return -1;
282 result = l4_create_ports(card_no);
283 if (result)
284 return result;
286 printk(KERN_INFO "gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n",
287 card_no ? "secondary" : "primary", rev >> 4, rev, L4_PORT);
289 for (i = 0; i < 4; i++) {
290 l4 = &l4_ports[card_no * 4 + i];
292 if (rev > 0x28) /* on 2.9+ the setcal command works correctly */
293 l4_setcal(l4->port, cal);
294 gameport_register_port(l4->gameport);
297 return 0;
300 static int __init l4_init(void)
302 int i, cards = 0;
304 if (!request_region(L4_PORT, 1, "lightning"))
305 return -EBUSY;
307 for (i = 0; i < 2; i++)
308 if (l4_add_card(i) == 0)
309 cards++;
311 outb(L4_SELECT_ANALOG, L4_PORT);
313 if (!cards) {
314 release_region(L4_PORT, 1);
315 return -ENODEV;
318 return 0;
321 static void __exit l4_exit(void)
323 int i;
324 int cal[4] = { 59, 59, 59, 59 };
326 for (i = 0; i < 8; i++)
327 if (l4_ports[i].gameport) {
328 l4_setcal(l4_ports[i].port, cal);
329 gameport_unregister_port(l4_ports[i].gameport);
332 outb(L4_SELECT_ANALOG, L4_PORT);
333 release_region(L4_PORT, 1);
336 module_init(l4_init);
337 module_exit(l4_exit);