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>
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
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");
54 struct gameport
*gameport
;
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
--;
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
;
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;
99 if (l4_wait_ready()) goto fail
;
100 *buttons
= inb(L4_PORT
) & 0x0f;
105 fail
: outb(L4_SELECT_ANALOG
, L4_PORT
);
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
)
115 outb(L4_SELECT_ANALOG
, L4_PORT
);
120 * l4_getcal() reads the L4 with calibration values.
123 static int l4_getcal(int port
, int *cal
)
127 outb(L4_SELECT_ANALOG
, L4_PORT
);
128 outb(L4_SELECT_DIGITAL
+ (port
>> 2), L4_PORT
);
129 if (inb(L4_PORT
) & L4_BUSY
)
132 outb(L4_CMD_GETCAL
, L4_PORT
);
136 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ (port
>> 2))
141 outb(port
& 3, L4_PORT
);
143 for (i
= 0; i
< 4; i
++) {
146 cal
[i
] = inb(L4_PORT
);
151 out
: outb(L4_SELECT_ANALOG
, L4_PORT
);
156 * l4_setcal() programs the L4 with calibration values.
159 static int l4_setcal(int port
, int *cal
)
163 outb(L4_SELECT_ANALOG
, L4_PORT
);
164 outb(L4_SELECT_DIGITAL
+ (port
>> 2), L4_PORT
);
165 if (inb(L4_PORT
) & L4_BUSY
)
168 outb(L4_CMD_SETCAL
, L4_PORT
);
172 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ (port
>> 2))
177 outb(port
& 3, L4_PORT
);
179 for (i
= 0; i
< 4; i
++) {
182 outb(cal
[i
], L4_PORT
);
187 out
: outb(L4_SELECT_ANALOG
, L4_PORT
);
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
)
200 struct l4
*l4
= gameport
->port_data
;
202 if (l4_getcal(l4
->port
, cal
))
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
];
213 if (l4_setcal(l4
->port
, cal
))
219 static int __init
l4_create_ports(int card_no
)
222 struct gameport
*port
;
225 for (i
= 0; i
< 4; i
++) {
227 idx
= card_no
* 4 + i
;
230 if (!(l4
->gameport
= port
= gameport_allocate_port())) {
231 printk(KERN_ERR
"lightning: Memory allocation failed\n");
233 gameport_free_port(l4
->gameport
);
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
);
255 static int __init
l4_add_card(int card_no
)
257 int cal
[4] = { 255, 255, 255, 255 };
261 outb(L4_SELECT_ANALOG
, L4_PORT
);
262 outb(L4_SELECT_DIGITAL
+ card_no
, L4_PORT
);
264 if (inb(L4_PORT
) & L4_BUSY
)
266 outb(L4_CMD_ID
, L4_PORT
);
271 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ card_no
)
276 if (inb(L4_PORT
) != L4_ID
)
286 result
= l4_create_ports(card_no
);
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
);
304 static int __init
l4_init(void)
308 if (!request_region(L4_PORT
, 1, "lightning"))
311 for (i
= 0; i
< 2; i
++)
312 if (l4_add_card(i
) == 0)
315 outb(L4_SELECT_ANALOG
, L4_PORT
);
318 release_region(L4_PORT
, 1);
325 static void __exit
l4_exit(void)
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
);