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
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>
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
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");
50 struct gameport
*gameport
;
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
--;
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
;
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;
95 if (l4_wait_ready()) goto fail
;
96 *buttons
= inb(L4_PORT
) & 0x0f;
101 fail
: outb(L4_SELECT_ANALOG
, L4_PORT
);
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
)
111 outb(L4_SELECT_ANALOG
, L4_PORT
);
116 * l4_getcal() reads the L4 with calibration values.
119 static int l4_getcal(int port
, int *cal
)
123 outb(L4_SELECT_ANALOG
, L4_PORT
);
124 outb(L4_SELECT_DIGITAL
+ (port
>> 2), L4_PORT
);
125 if (inb(L4_PORT
) & L4_BUSY
)
128 outb(L4_CMD_GETCAL
, L4_PORT
);
132 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ (port
>> 2))
137 outb(port
& 3, L4_PORT
);
139 for (i
= 0; i
< 4; i
++) {
142 cal
[i
] = inb(L4_PORT
);
147 out
: outb(L4_SELECT_ANALOG
, L4_PORT
);
152 * l4_setcal() programs the L4 with calibration values.
155 static int l4_setcal(int port
, int *cal
)
159 outb(L4_SELECT_ANALOG
, L4_PORT
);
160 outb(L4_SELECT_DIGITAL
+ (port
>> 2), L4_PORT
);
161 if (inb(L4_PORT
) & L4_BUSY
)
164 outb(L4_CMD_SETCAL
, L4_PORT
);
168 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ (port
>> 2))
173 outb(port
& 3, L4_PORT
);
175 for (i
= 0; i
< 4; i
++) {
178 outb(cal
[i
], L4_PORT
);
183 out
: outb(L4_SELECT_ANALOG
, L4_PORT
);
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
)
196 struct l4
*l4
= gameport
->port_data
;
198 if (l4_getcal(l4
->port
, cal
))
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
];
209 if (l4_setcal(l4
->port
, cal
))
215 static int __init
l4_create_ports(int card_no
)
218 struct gameport
*port
;
221 for (i
= 0; i
< 4; i
++) {
223 idx
= card_no
* 4 + i
;
226 if (!(l4
->gameport
= port
= gameport_allocate_port())) {
227 printk(KERN_ERR
"lightning: Memory allocation failed\n");
229 gameport_free_port(l4
->gameport
);
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
);
251 static int __init
l4_add_card(int card_no
)
253 int cal
[4] = { 255, 255, 255, 255 };
257 outb(L4_SELECT_ANALOG
, L4_PORT
);
258 outb(L4_SELECT_DIGITAL
+ card_no
, L4_PORT
);
260 if (inb(L4_PORT
) & L4_BUSY
)
262 outb(L4_CMD_ID
, L4_PORT
);
267 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ card_no
)
272 if (inb(L4_PORT
) != L4_ID
)
282 result
= l4_create_ports(card_no
);
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
);
300 static int __init
l4_init(void)
304 if (!request_region(L4_PORT
, 1, "lightning"))
307 for (i
= 0; i
< 2; i
++)
308 if (l4_add_card(i
) == 0)
311 outb(L4_SELECT_ANALOG
, L4_PORT
);
314 release_region(L4_PORT
, 1);
321 static void __exit
l4_exit(void)
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
);