1 /* 2 * Copyright (c) 1998-2001 Vojtech Pavlik 3 */ 4 5 /* 6 * PDPI Lightning 4 gamecard driver for Linux. 7 */ 8 9 /* 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. 14 * 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. 19 * 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 23 * 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 27 */ 28 29 #include <asm/io.h> 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> 37 #include <linux/slab.h> 38 39 #define L4_PORT 0x201 40 #define L4_SELECT_ANALOG 0xa4 41 #define L4_SELECT_DIGITAL 0xa5 42 #define L4_SELECT_SECONDARY 0xa6 43 #define L4_CMD_ID 0x80 44 #define L4_CMD_GETCAL 0x92 45 #define L4_CMD_SETCAL 0x93 46 #define L4_ID 0x04 47 #define L4_BUSY 0x01 48 #define L4_TIMEOUT 80 /* 80 us */ 49 50 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 51 MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver"); 52 MODULE_LICENSE("GPL"); 53 54 struct l4 { 55 struct gameport *gameport; 56 unsigned char port; 57 }; 58 59 static struct l4 l4_ports[8]; 60 61 /* 62 * l4_wait_ready() waits for the L4 to become ready. 63 */ 64 65 static int l4_wait_ready(void) 66 { 67 unsigned int t = L4_TIMEOUT; 68 69 while ((inb(L4_PORT) & L4_BUSY) && t > 0) t--; 70 return -(t <= 0); 71 } 72 73 /* 74 * l4_cooked_read() reads data from the Lightning 4. 75 */ 76 77 static int l4_cooked_read(struct gameport *gameport, int *axes, int *buttons) 78 { 79 struct l4 *l4 = gameport->port_data; 80 unsigned char status; 81 int i, result = -1; 82 83 outb(L4_SELECT_ANALOG, L4_PORT); 84 outb(L4_SELECT_DIGITAL + (l4->port >> 2), L4_PORT); 85 86 if (inb(L4_PORT) & L4_BUSY) goto fail; 87 outb(l4->port & 3, L4_PORT); 88 89 if (l4_wait_ready()) goto fail; 90 status = inb(L4_PORT); 91 92 for (i = 0; i < 4; i++) 93 if (status & (1 << i)) { 94 if (l4_wait_ready()) goto fail; 95 axes[i] = inb(L4_PORT); 96 if (axes[i] > 252) axes[i] = -1; 97 } 98 99 if (status & 0x10) { 100 if (l4_wait_ready()) goto fail; 101 *buttons = inb(L4_PORT) & 0x0f; 102 } 103 104 result = 0; 105 106 fail: outb(L4_SELECT_ANALOG, L4_PORT); 107 return result; 108 } 109 110 static int l4_open(struct gameport *gameport, int mode) 111 { 112 struct l4 *l4 = gameport->port_data; 113 114 if (l4->port != 0 && mode != GAMEPORT_MODE_COOKED) 115 return -1; 116 outb(L4_SELECT_ANALOG, L4_PORT); 117 return 0; 118 } 119 120 /* 121 * l4_getcal() reads the L4 with calibration values. 122 */ 123 124 static int l4_getcal(int port, int *cal) 125 { 126 int i, result = -1; 127 128 outb(L4_SELECT_ANALOG, L4_PORT); 129 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT); 130 if (inb(L4_PORT) & L4_BUSY) 131 goto out; 132 133 outb(L4_CMD_GETCAL, L4_PORT); 134 if (l4_wait_ready()) 135 goto out; 136 137 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2)) 138 goto out; 139 140 if (l4_wait_ready()) 141 goto out; 142 outb(port & 3, L4_PORT); 143 144 for (i = 0; i < 4; i++) { 145 if (l4_wait_ready()) 146 goto out; 147 cal[i] = inb(L4_PORT); 148 } 149 150 result = 0; 151 152 out: outb(L4_SELECT_ANALOG, L4_PORT); 153 return result; 154 } 155 156 /* 157 * l4_setcal() programs the L4 with calibration values. 158 */ 159 160 static int l4_setcal(int port, int *cal) 161 { 162 int i, result = -1; 163 164 outb(L4_SELECT_ANALOG, L4_PORT); 165 outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT); 166 if (inb(L4_PORT) & L4_BUSY) 167 goto out; 168 169 outb(L4_CMD_SETCAL, L4_PORT); 170 if (l4_wait_ready()) 171 goto out; 172 173 if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2)) 174 goto out; 175 176 if (l4_wait_ready()) 177 goto out; 178 outb(port & 3, L4_PORT); 179 180 for (i = 0; i < 4; i++) { 181 if (l4_wait_ready()) 182 goto out; 183 outb(cal[i], L4_PORT); 184 } 185 186 result = 0; 187 188 out: outb(L4_SELECT_ANALOG, L4_PORT); 189 return result; 190 } 191 192 /* 193 * l4_calibrate() calibrates the L4 for the attached device, so 194 * that the device's resistance fits into the L4's 8-bit range. 195 */ 196 197 static int l4_calibrate(struct gameport *gameport, int *axes, int *max) 198 { 199 int i, t; 200 int cal[4]; 201 struct l4 *l4 = gameport->port_data; 202 203 if (l4_getcal(l4->port, cal)) 204 return -1; 205 206 for (i = 0; i < 4; i++) { 207 t = (max[i] * cal[i]) / 200; 208 t = (t < 1) ? 1 : ((t > 255) ? 255 : t); 209 axes[i] = (axes[i] < 0) ? -1 : (axes[i] * cal[i]) / t; 210 axes[i] = (axes[i] > 252) ? 252 : axes[i]; 211 cal[i] = t; 212 } 213 214 if (l4_setcal(l4->port, cal)) 215 return -1; 216 217 return 0; 218 } 219 220 static int __init l4_create_ports(int card_no) 221 { 222 struct l4 *l4; 223 struct gameport *port; 224 int i, idx; 225 226 for (i = 0; i < 4; i++) { 227 228 idx = card_no * 4 + i; 229 l4 = &l4_ports[idx]; 230 231 if (!(l4->gameport = port = gameport_allocate_port())) { 232 printk(KERN_ERR "lightning: Memory allocation failed\n"); 233 while (--i >= 0) { 234 gameport_free_port(l4->gameport); 235 l4->gameport = NULL; 236 } 237 return -ENOMEM; 238 } 239 l4->port = idx; 240 241 port->port_data = l4; 242 port->open = l4_open; 243 port->cooked_read = l4_cooked_read; 244 port->calibrate = l4_calibrate; 245 246 gameport_set_name(port, "PDPI Lightning 4"); 247 gameport_set_phys(port, "isa%04x/gameport%d", L4_PORT, idx); 248 249 if (idx == 0) 250 port->io = L4_PORT; 251 } 252 253 return 0; 254 } 255 256 static int __init l4_add_card(int card_no) 257 { 258 int cal[4] = { 255, 255, 255, 255 }; 259 int i, rev, result; 260 struct l4 *l4; 261 262 outb(L4_SELECT_ANALOG, L4_PORT); 263 outb(L4_SELECT_DIGITAL + card_no, L4_PORT); 264 265 if (inb(L4_PORT) & L4_BUSY) 266 return -1; 267 outb(L4_CMD_ID, L4_PORT); 268 269 if (l4_wait_ready()) 270 return -1; 271 272 if (inb(L4_PORT) != L4_SELECT_DIGITAL + card_no) 273 return -1; 274 275 if (l4_wait_ready()) 276 return -1; 277 if (inb(L4_PORT) != L4_ID) 278 return -1; 279 280 if (l4_wait_ready()) 281 return -1; 282 rev = inb(L4_PORT); 283 284 if (!rev) 285 return -1; 286 287 result = l4_create_ports(card_no); 288 if (result) 289 return result; 290 291 printk(KERN_INFO "gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n", 292 card_no ? "secondary" : "primary", rev >> 4, rev, L4_PORT); 293 294 for (i = 0; i < 4; i++) { 295 l4 = &l4_ports[card_no * 4 + i]; 296 297 if (rev > 0x28) /* on 2.9+ the setcal command works correctly */ 298 l4_setcal(l4->port, cal); 299 gameport_register_port(l4->gameport); 300 } 301 302 return 0; 303 } 304 305 static int __init l4_init(void) 306 { 307 int i, cards = 0; 308 309 if (!request_region(L4_PORT, 1, "lightning")) 310 return -EBUSY; 311 312 for (i = 0; i < 2; i++) 313 if (l4_add_card(i) == 0) 314 cards++; 315 316 outb(L4_SELECT_ANALOG, L4_PORT); 317 318 if (!cards) { 319 release_region(L4_PORT, 1); 320 return -ENODEV; 321 } 322 323 return 0; 324 } 325 326 static void __exit l4_exit(void) 327 { 328 int i; 329 int cal[4] = { 59, 59, 59, 59 }; 330 331 for (i = 0; i < 8; i++) 332 if (l4_ports[i].gameport) { 333 l4_setcal(l4_ports[i].port, cal); 334 gameport_unregister_port(l4_ports[i].gameport); 335 } 336 337 outb(L4_SELECT_ANALOG, L4_PORT); 338 release_region(L4_PORT, 1); 339 } 340 341 module_init(l4_init); 342 module_exit(l4_exit); 343