treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / gameport / lightning.c
blob87eeb4b5b5b57e1a1c7d206734b77268f4f42fa3
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 1998-2001 Vojtech Pavlik
4 */
6 /*
7 * PDPI Lightning 4 gamecard driver for Linux.
8 */
13 #include <asm/io.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/ioport.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/gameport.h>
22 #define L4_PORT 0x201
23 #define L4_SELECT_ANALOG 0xa4
24 #define L4_SELECT_DIGITAL 0xa5
25 #define L4_SELECT_SECONDARY 0xa6
26 #define L4_CMD_ID 0x80
27 #define L4_CMD_GETCAL 0x92
28 #define L4_CMD_SETCAL 0x93
29 #define L4_ID 0x04
30 #define L4_BUSY 0x01
31 #define L4_TIMEOUT 80 /* 80 us */
33 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
34 MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver");
35 MODULE_LICENSE("GPL");
37 struct l4 {
38 struct gameport *gameport;
39 unsigned char port;
42 static struct l4 l4_ports[8];
45 * l4_wait_ready() waits for the L4 to become ready.
48 static int l4_wait_ready(void)
50 unsigned int t = L4_TIMEOUT;
52 while ((inb(L4_PORT) & L4_BUSY) && t > 0) t--;
53 return -(t <= 0);
57 * l4_cooked_read() reads data from the Lightning 4.
60 static int l4_cooked_read(struct gameport *gameport, int *axes, int *buttons)
62 struct l4 *l4 = gameport->port_data;
63 unsigned char status;
64 int i, result = -1;
66 outb(L4_SELECT_ANALOG, L4_PORT);
67 outb(L4_SELECT_DIGITAL + (l4->port >> 2), L4_PORT);
69 if (inb(L4_PORT) & L4_BUSY) goto fail;
70 outb(l4->port & 3, L4_PORT);
72 if (l4_wait_ready()) goto fail;
73 status = inb(L4_PORT);
75 for (i = 0; i < 4; i++)
76 if (status & (1 << i)) {
77 if (l4_wait_ready()) goto fail;
78 axes[i] = inb(L4_PORT);
79 if (axes[i] > 252) axes[i] = -1;
82 if (status & 0x10) {
83 if (l4_wait_ready()) goto fail;
84 *buttons = inb(L4_PORT) & 0x0f;
87 result = 0;
89 fail: outb(L4_SELECT_ANALOG, L4_PORT);
90 return result;
93 static int l4_open(struct gameport *gameport, int mode)
95 struct l4 *l4 = gameport->port_data;
97 if (l4->port != 0 && mode != GAMEPORT_MODE_COOKED)
98 return -1;
99 outb(L4_SELECT_ANALOG, L4_PORT);
100 return 0;
104 * l4_getcal() reads the L4 with calibration values.
107 static int l4_getcal(int port, int *cal)
109 int i, result = -1;
111 outb(L4_SELECT_ANALOG, L4_PORT);
112 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
113 if (inb(L4_PORT) & L4_BUSY)
114 goto out;
116 outb(L4_CMD_GETCAL, L4_PORT);
117 if (l4_wait_ready())
118 goto out;
120 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
121 goto out;
123 if (l4_wait_ready())
124 goto out;
125 outb(port & 3, L4_PORT);
127 for (i = 0; i < 4; i++) {
128 if (l4_wait_ready())
129 goto out;
130 cal[i] = inb(L4_PORT);
133 result = 0;
135 out: outb(L4_SELECT_ANALOG, L4_PORT);
136 return result;
140 * l4_setcal() programs the L4 with calibration values.
143 static int l4_setcal(int port, int *cal)
145 int i, result = -1;
147 outb(L4_SELECT_ANALOG, L4_PORT);
148 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
149 if (inb(L4_PORT) & L4_BUSY)
150 goto out;
152 outb(L4_CMD_SETCAL, L4_PORT);
153 if (l4_wait_ready())
154 goto out;
156 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
157 goto out;
159 if (l4_wait_ready())
160 goto out;
161 outb(port & 3, L4_PORT);
163 for (i = 0; i < 4; i++) {
164 if (l4_wait_ready())
165 goto out;
166 outb(cal[i], L4_PORT);
169 result = 0;
171 out: outb(L4_SELECT_ANALOG, L4_PORT);
172 return result;
176 * l4_calibrate() calibrates the L4 for the attached device, so
177 * that the device's resistance fits into the L4's 8-bit range.
180 static int l4_calibrate(struct gameport *gameport, int *axes, int *max)
182 int i, t;
183 int cal[4];
184 struct l4 *l4 = gameport->port_data;
186 if (l4_getcal(l4->port, cal))
187 return -1;
189 for (i = 0; i < 4; i++) {
190 t = (max[i] * cal[i]) / 200;
191 t = (t < 1) ? 1 : ((t > 255) ? 255 : t);
192 axes[i] = (axes[i] < 0) ? -1 : (axes[i] * cal[i]) / t;
193 axes[i] = (axes[i] > 252) ? 252 : axes[i];
194 cal[i] = t;
197 if (l4_setcal(l4->port, cal))
198 return -1;
200 return 0;
203 static int __init l4_create_ports(int card_no)
205 struct l4 *l4;
206 struct gameport *port;
207 int i, idx;
209 for (i = 0; i < 4; i++) {
211 idx = card_no * 4 + i;
212 l4 = &l4_ports[idx];
214 if (!(l4->gameport = port = gameport_allocate_port())) {
215 printk(KERN_ERR "lightning: Memory allocation failed\n");
216 while (--i >= 0) {
217 gameport_free_port(l4->gameport);
218 l4->gameport = NULL;
220 return -ENOMEM;
222 l4->port = idx;
224 port->port_data = l4;
225 port->open = l4_open;
226 port->cooked_read = l4_cooked_read;
227 port->calibrate = l4_calibrate;
229 gameport_set_name(port, "PDPI Lightning 4");
230 gameport_set_phys(port, "isa%04x/gameport%d", L4_PORT, idx);
232 if (idx == 0)
233 port->io = L4_PORT;
236 return 0;
239 static int __init l4_add_card(int card_no)
241 int cal[4] = { 255, 255, 255, 255 };
242 int i, rev, result;
243 struct l4 *l4;
245 outb(L4_SELECT_ANALOG, L4_PORT);
246 outb(L4_SELECT_DIGITAL + card_no, L4_PORT);
248 if (inb(L4_PORT) & L4_BUSY)
249 return -1;
250 outb(L4_CMD_ID, L4_PORT);
252 if (l4_wait_ready())
253 return -1;
255 if (inb(L4_PORT) != L4_SELECT_DIGITAL + card_no)
256 return -1;
258 if (l4_wait_ready())
259 return -1;
260 if (inb(L4_PORT) != L4_ID)
261 return -1;
263 if (l4_wait_ready())
264 return -1;
265 rev = inb(L4_PORT);
267 if (!rev)
268 return -1;
270 result = l4_create_ports(card_no);
271 if (result)
272 return result;
274 printk(KERN_INFO "gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n",
275 card_no ? "secondary" : "primary", rev >> 4, rev, L4_PORT);
277 for (i = 0; i < 4; i++) {
278 l4 = &l4_ports[card_no * 4 + i];
280 if (rev > 0x28) /* on 2.9+ the setcal command works correctly */
281 l4_setcal(l4->port, cal);
282 gameport_register_port(l4->gameport);
285 return 0;
288 static int __init l4_init(void)
290 int i, cards = 0;
292 if (!request_region(L4_PORT, 1, "lightning"))
293 return -EBUSY;
295 for (i = 0; i < 2; i++)
296 if (l4_add_card(i) == 0)
297 cards++;
299 outb(L4_SELECT_ANALOG, L4_PORT);
301 if (!cards) {
302 release_region(L4_PORT, 1);
303 return -ENODEV;
306 return 0;
309 static void __exit l4_exit(void)
311 int i;
312 int cal[4] = { 59, 59, 59, 59 };
314 for (i = 0; i < 8; i++)
315 if (l4_ports[i].gameport) {
316 l4_setcal(l4_ports[i].port, cal);
317 gameport_unregister_port(l4_ports[i].gameport);
320 outb(L4_SELECT_ANALOG, L4_PORT);
321 release_region(L4_PORT, 1);
324 module_init(l4_init);
325 module_exit(l4_exit);