1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1998-2001 Vojtech Pavlik
7 * PDPI Lightning 4 gamecard driver for Linux.
11 #include <linux/delay.h>
12 #include <linux/errno.h>
13 #include <linux/ioport.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/gameport.h>
20 #define L4_SELECT_ANALOG 0xa4
21 #define L4_SELECT_DIGITAL 0xa5
22 #define L4_SELECT_SECONDARY 0xa6
23 #define L4_CMD_ID 0x80
24 #define L4_CMD_GETCAL 0x92
25 #define L4_CMD_SETCAL 0x93
28 #define L4_TIMEOUT 80 /* 80 us */
30 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
31 MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver");
32 MODULE_LICENSE("GPL");
35 struct gameport
*gameport
;
39 static struct l4 l4_ports
[8];
42 * l4_wait_ready() waits for the L4 to become ready.
45 static int l4_wait_ready(void)
47 unsigned int t
= L4_TIMEOUT
;
49 while ((inb(L4_PORT
) & L4_BUSY
) && t
> 0) t
--;
54 * l4_cooked_read() reads data from the Lightning 4.
57 static int l4_cooked_read(struct gameport
*gameport
, int *axes
, int *buttons
)
59 struct l4
*l4
= gameport
->port_data
;
63 outb(L4_SELECT_ANALOG
, L4_PORT
);
64 outb(L4_SELECT_DIGITAL
+ (l4
->port
>> 2), L4_PORT
);
66 if (inb(L4_PORT
) & L4_BUSY
) goto fail
;
67 outb(l4
->port
& 3, L4_PORT
);
69 if (l4_wait_ready()) goto fail
;
70 status
= inb(L4_PORT
);
72 for (i
= 0; i
< 4; i
++)
73 if (status
& (1 << i
)) {
74 if (l4_wait_ready()) goto fail
;
75 axes
[i
] = inb(L4_PORT
);
76 if (axes
[i
] > 252) axes
[i
] = -1;
80 if (l4_wait_ready()) goto fail
;
81 *buttons
= inb(L4_PORT
) & 0x0f;
86 fail
: outb(L4_SELECT_ANALOG
, L4_PORT
);
90 static int l4_open(struct gameport
*gameport
, int mode
)
92 struct l4
*l4
= gameport
->port_data
;
94 if (l4
->port
!= 0 && mode
!= GAMEPORT_MODE_COOKED
)
96 outb(L4_SELECT_ANALOG
, L4_PORT
);
101 * l4_getcal() reads the L4 with calibration values.
104 static int l4_getcal(int port
, int *cal
)
108 outb(L4_SELECT_ANALOG
, L4_PORT
);
109 outb(L4_SELECT_DIGITAL
+ (port
>> 2), L4_PORT
);
110 if (inb(L4_PORT
) & L4_BUSY
)
113 outb(L4_CMD_GETCAL
, L4_PORT
);
117 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ (port
>> 2))
122 outb(port
& 3, L4_PORT
);
124 for (i
= 0; i
< 4; i
++) {
127 cal
[i
] = inb(L4_PORT
);
132 out
: outb(L4_SELECT_ANALOG
, L4_PORT
);
137 * l4_setcal() programs the L4 with calibration values.
140 static int l4_setcal(int port
, int *cal
)
144 outb(L4_SELECT_ANALOG
, L4_PORT
);
145 outb(L4_SELECT_DIGITAL
+ (port
>> 2), L4_PORT
);
146 if (inb(L4_PORT
) & L4_BUSY
)
149 outb(L4_CMD_SETCAL
, L4_PORT
);
153 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ (port
>> 2))
158 outb(port
& 3, L4_PORT
);
160 for (i
= 0; i
< 4; i
++) {
163 outb(cal
[i
], L4_PORT
);
168 out
: outb(L4_SELECT_ANALOG
, L4_PORT
);
173 * l4_calibrate() calibrates the L4 for the attached device, so
174 * that the device's resistance fits into the L4's 8-bit range.
177 static int l4_calibrate(struct gameport
*gameport
, int *axes
, int *max
)
181 struct l4
*l4
= gameport
->port_data
;
183 if (l4_getcal(l4
->port
, cal
))
186 for (i
= 0; i
< 4; i
++) {
187 t
= (max
[i
] * cal
[i
]) / 200;
188 t
= (t
< 1) ? 1 : ((t
> 255) ? 255 : t
);
189 axes
[i
] = (axes
[i
] < 0) ? -1 : (axes
[i
] * cal
[i
]) / t
;
190 axes
[i
] = (axes
[i
] > 252) ? 252 : axes
[i
];
194 if (l4_setcal(l4
->port
, cal
))
200 static int __init
l4_create_ports(int card_no
)
203 struct gameport
*port
;
206 for (i
= 0; i
< 4; i
++) {
208 idx
= card_no
* 4 + i
;
211 if (!(l4
->gameport
= port
= gameport_allocate_port())) {
212 printk(KERN_ERR
"lightning: Memory allocation failed\n");
214 gameport_free_port(l4
->gameport
);
221 port
->port_data
= l4
;
222 port
->open
= l4_open
;
223 port
->cooked_read
= l4_cooked_read
;
224 port
->calibrate
= l4_calibrate
;
226 gameport_set_name(port
, "PDPI Lightning 4");
227 gameport_set_phys(port
, "isa%04x/gameport%d", L4_PORT
, idx
);
236 static int __init
l4_add_card(int card_no
)
238 int cal
[4] = { 255, 255, 255, 255 };
242 outb(L4_SELECT_ANALOG
, L4_PORT
);
243 outb(L4_SELECT_DIGITAL
+ card_no
, L4_PORT
);
245 if (inb(L4_PORT
) & L4_BUSY
)
247 outb(L4_CMD_ID
, L4_PORT
);
252 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ card_no
)
257 if (inb(L4_PORT
) != L4_ID
)
267 result
= l4_create_ports(card_no
);
271 printk(KERN_INFO
"gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n",
272 card_no
? "secondary" : "primary", rev
>> 4, rev
, L4_PORT
);
274 for (i
= 0; i
< 4; i
++) {
275 l4
= &l4_ports
[card_no
* 4 + i
];
277 if (rev
> 0x28) /* on 2.9+ the setcal command works correctly */
278 l4_setcal(l4
->port
, cal
);
279 gameport_register_port(l4
->gameport
);
285 static int __init
l4_init(void)
289 if (!request_region(L4_PORT
, 1, "lightning"))
292 for (i
= 0; i
< 2; i
++)
293 if (l4_add_card(i
) == 0)
296 outb(L4_SELECT_ANALOG
, L4_PORT
);
299 release_region(L4_PORT
, 1);
306 static void __exit
l4_exit(void)
309 int cal
[4] = { 59, 59, 59, 59 };
311 for (i
= 0; i
< 8; i
++)
312 if (l4_ports
[i
].gameport
) {
313 l4_setcal(l4_ports
[i
].port
, cal
);
314 gameport_unregister_port(l4_ports
[i
].gameport
);
317 outb(L4_SELECT_ANALOG
, L4_PORT
);
318 release_region(L4_PORT
, 1);
321 module_init(l4_init
);
322 module_exit(l4_exit
);