2 * $Id: lightning.c,v 1.20 2002/01/22 20:41:31 vojtech Exp $
4 * Copyright (c) 1998-2001 Vojtech Pavlik
8 * PDPI Lightning 4 gamecard driver for Linux.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Should you need to contact me, the author, you can do so either by
27 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/ioport.h>
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/gameport.h>
39 #include <linux/slab.h>
42 #define L4_SELECT_ANALOG 0xa4
43 #define L4_SELECT_DIGITAL 0xa5
44 #define L4_SELECT_SECONDARY 0xa6
45 #define L4_CMD_ID 0x80
46 #define L4_CMD_GETCAL 0x92
47 #define L4_CMD_SETCAL 0x93
50 #define L4_TIMEOUT 80 /* 80 us */
52 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
53 MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver");
54 MODULE_LICENSE("GPL");
57 struct gameport gameport
;
62 char l4_name
[] = "PDPI Lightning 4";
65 * l4_wait_ready() waits for the L4 to become ready.
68 static int l4_wait_ready(void)
72 while ((inb(L4_PORT
) & L4_BUSY
) && t
> 0) t
--;
77 * l4_cooked_read() reads data from the Lightning 4.
80 static int l4_cooked_read(struct gameport
*gameport
, int *axes
, int *buttons
)
82 struct l4
*l4
= gameport
->driver
;
86 outb(L4_SELECT_ANALOG
, L4_PORT
);
87 outb(L4_SELECT_DIGITAL
+ (l4
->port
>> 2), L4_PORT
);
89 if (inb(L4_PORT
) & L4_BUSY
) goto fail
;
90 outb(l4
->port
& 3, L4_PORT
);
92 if (l4_wait_ready()) goto fail
;
93 status
= inb(L4_PORT
);
95 for (i
= 0; i
< 4; i
++)
96 if (status
& (1 << i
)) {
97 if (l4_wait_ready()) goto fail
;
98 axes
[i
] = inb(L4_PORT
);
99 if (axes
[i
] > 252) axes
[i
] = -1;
103 if (l4_wait_ready()) goto fail
;
104 *buttons
= inb(L4_PORT
) & 0x0f;
109 fail
: outb(L4_SELECT_ANALOG
, L4_PORT
);
113 static int l4_open(struct gameport
*gameport
, int mode
)
115 struct l4
*l4
= gameport
->driver
;
116 if (l4
->port
!= 0 && mode
!= GAMEPORT_MODE_COOKED
)
118 outb(L4_SELECT_ANALOG
, L4_PORT
);
123 * l4_getcal() reads the L4 with calibration values.
126 static int l4_getcal(int port
, int *cal
)
130 outb(L4_SELECT_ANALOG
, L4_PORT
);
131 outb(L4_SELECT_DIGITAL
+ (port
>> 2), L4_PORT
);
133 if (inb(L4_PORT
) & L4_BUSY
) goto fail
;
134 outb(L4_CMD_GETCAL
, L4_PORT
);
136 if (l4_wait_ready()) goto fail
;
137 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ (port
>> 2)) goto fail
;
139 if (l4_wait_ready()) goto fail
;
140 outb(port
& 3, L4_PORT
);
142 for (i
= 0; i
< 4; i
++) {
143 if (l4_wait_ready()) goto fail
;
144 cal
[i
] = inb(L4_PORT
);
149 fail
: outb(L4_SELECT_ANALOG
, L4_PORT
);
154 * l4_setcal() programs the L4 with calibration values.
157 static int l4_setcal(int port
, int *cal
)
161 outb(L4_SELECT_ANALOG
, L4_PORT
);
162 outb(L4_SELECT_DIGITAL
+ (port
>> 2), L4_PORT
);
164 if (inb(L4_PORT
) & L4_BUSY
) goto fail
;
165 outb(L4_CMD_SETCAL
, L4_PORT
);
167 if (l4_wait_ready()) goto fail
;
168 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ (port
>> 2)) goto fail
;
170 if (l4_wait_ready()) goto fail
;
171 outb(port
& 3, L4_PORT
);
173 for (i
= 0; i
< 4; i
++) {
174 if (l4_wait_ready()) goto fail
;
175 outb(cal
[i
], L4_PORT
);
180 fail
: outb(L4_SELECT_ANALOG
, L4_PORT
);
185 * l4_calibrate() calibrates the L4 for the attached device, so
186 * that the device's resistance fits into the L4's 8-bit range.
189 static int l4_calibrate(struct gameport
*gameport
, int *axes
, int *max
)
193 struct l4
*l4
= gameport
->driver
;
195 if (l4_getcal(l4
->port
, cal
))
198 for (i
= 0; i
< 4; i
++) {
199 t
= (max
[i
] * cal
[i
]) / 200;
200 t
= (t
< 1) ? 1 : ((t
> 255) ? 255 : t
);
201 axes
[i
] = (axes
[i
] < 0) ? -1 : (axes
[i
] * cal
[i
]) / t
;
202 axes
[i
] = (axes
[i
] > 252) ? 252 : axes
[i
];
206 if (l4_setcal(l4
->port
, cal
))
212 static int __init
l4_init(void)
214 int cal
[4] = {255,255,255,255};
215 int i
, j
, rev
, cards
= 0;
216 struct gameport
*gameport
;
219 if (!request_region(L4_PORT
, 1, "lightning"))
222 for (i
= 0; i
< 2; i
++) {
224 outb(L4_SELECT_ANALOG
, L4_PORT
);
225 outb(L4_SELECT_DIGITAL
+ i
, L4_PORT
);
227 if (inb(L4_PORT
) & L4_BUSY
) continue;
228 outb(L4_CMD_ID
, L4_PORT
);
230 if (l4_wait_ready()) continue;
231 if (inb(L4_PORT
) != L4_SELECT_DIGITAL
+ i
) continue;
233 if (l4_wait_ready()) continue;
234 if (inb(L4_PORT
) != L4_ID
) continue;
236 if (l4_wait_ready()) continue;
241 if (!(l4_port
[i
* 4] = kmalloc(sizeof(struct l4
) * 4, GFP_KERNEL
))) {
242 printk(KERN_ERR
"lightning: Out of memory allocating ports.\n");
245 memset(l4_port
[i
* 4], 0, sizeof(struct l4
) * 4);
247 for (j
= 0; j
< 4; j
++) {
249 l4
= l4_port
[i
* 4 + j
] = l4_port
[i
* 4] + j
;
250 l4
->port
= i
* 4 + j
;
252 sprintf(l4
->phys
, "isa%04x/gameport%d", L4_PORT
, 4 * i
+ j
);
254 gameport
= &l4
->gameport
;
255 gameport
->driver
= l4
;
256 gameport
->open
= l4_open
;
257 gameport
->cooked_read
= l4_cooked_read
;
258 gameport
->calibrate
= l4_calibrate
;
260 gameport
->name
= l4_name
;
261 gameport
->phys
= l4
->phys
;
262 gameport
->id
.bustype
= BUS_ISA
;
265 gameport
->io
= L4_PORT
;
267 if (rev
> 0x28) /* on 2.9+ the setcal command works correctly */
268 l4_setcal(l4
->port
, cal
);
270 gameport_register_port(gameport
);
273 printk(KERN_INFO
"gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n",
274 i
? "secondary" : "primary", rev
>> 4, rev
, L4_PORT
);
279 outb(L4_SELECT_ANALOG
, L4_PORT
);
282 release_region(L4_PORT
, 1);
289 static void __exit
l4_exit(void)
292 int cal
[4] = {59, 59, 59, 59};
294 for (i
= 0; i
< 8; i
++)
296 l4_setcal(l4_port
[i
]->port
, cal
);
297 gameport_unregister_port(&l4_port
[i
]->gameport
);
299 outb(L4_SELECT_ANALOG
, L4_PORT
);
300 release_region(L4_PORT
, 1);
303 module_init(l4_init
);
304 module_exit(l4_exit
);