mse.c revision 4259
1/* 2 * Copyright 1992 by the University of Guelph 3 * 4 * Permission to use, copy and modify this 5 * software and its documentation for any purpose and without 6 * fee is hereby granted, provided that the above copyright 7 * notice appear in all copies and that both that copyright 8 * notice and this permission notice appear in supporting 9 * documentation. 10 * University of Guelph makes no representations about the suitability of 11 * this software for any purpose. It is provided "as is" 12 * without express or implied warranty. 13 * 14 * $Id: mse.c,v 1.8 1994/10/23 21:27:31 wollman Exp $ 15 */ 16/* 17 * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and 18 * the X386 port, courtesy of 19 * Rick Macklem, rick@snowhite.cis.uoguelph.ca 20 * Caveats: The driver currently uses spltty(), but doesn't use any 21 * generic tty code. It could use splmse() (that only masks off the 22 * bus mouse interrupt, but that would require hacking in i386/isa/icu.s. 23 * (This may be worth the effort, since the Logitech generates 30/60 24 * interrupts/sec continuously while it is open.) 25 * NB: The ATI has NOT been tested yet! 26 */ 27 28/* 29 * Modification history: 30 * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com) 31 * improved probe based on input from Logitech. 32 * 33 * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu) 34 * fixes to make it work with Microsoft InPort busmouse 35 * 36 * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu) 37 * added patches for new "select" interface 38 * 39 * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu) 40 * changed position of some spl()'s in mseread 41 * 42 * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu) 43 * limit maximum negative x/y value to -127 to work around XFree problem 44 * that causes spurious button pushes. 45 */ 46 47#include "mse.h" 48#if NMSE > 0 49#include <sys/param.h> 50#include <sys/systm.h> 51#include <sys/proc.h> 52#include <sys/user.h> 53#include <sys/buf.h> 54#include <sys/kernel.h> 55#include <sys/ioctl.h> 56#include <sys/tty.h> 57#include <sys/uio.h> 58#include <sys/devconf.h> 59 60#include <i386/isa/isa_device.h> 61#include <i386/isa/icu.h> 62 63static int mseprobe(struct isa_device *); 64static int mseattach(struct isa_device *); 65void mseintr(int); 66 67struct isa_driver msedriver = { 68 mseprobe, mseattach, "mse" 69}; 70 71/* 72 * Software control structure for mouse. The sc_enablemouse(), 73 * sc_disablemouse() and sc_getmouse() routines must be called spl'd(). 74 */ 75#define PROTOBYTES 5 76struct mse_softc { 77 int sc_flags; 78 int sc_mousetype; 79 struct selinfo sc_selp; 80 u_int sc_port; 81 void (*sc_enablemouse)(); 82 void (*sc_disablemouse)(); 83 void (*sc_getmouse)(); 84 int sc_deltax; 85 int sc_deltay; 86 int sc_obuttons; 87 int sc_buttons; 88 int sc_bytesread; 89 u_char sc_bytes[PROTOBYTES]; 90} mse_sc[NMSE]; 91 92/* Flags */ 93#define MSESC_OPEN 0x1 94#define MSESC_WANT 0x2 95 96/* and Mouse Types */ 97#define MSE_LOGITECH 0x1 98#define MSE_ATIINPORT 0x2 99#define MSE_LOGI_SIG 0xA5 100 101#define MSE_PORTA 0 102#define MSE_PORTB 1 103#define MSE_PORTC 2 104#define MSE_PORTD 3 105 106#define MSE_UNIT(dev) (minor(dev) >> 1) 107#define MSE_NBLOCKIO(dev) (minor(dev) & 0x1) 108 109/* 110 * Logitech bus mouse definitions 111 */ 112#define MSE_SETUP 0x91 /* What does this mean? */ 113 /* The definition for the control port */ 114 /* is as follows: */ 115 116 /* D7 = Mode set flag (1 = active) */ 117 /* D6,D5 = Mode selection (port A) */ 118 /* 00 = Mode 0 = Basic I/O */ 119 /* 01 = Mode 1 = Strobed I/O */ 120 /* 10 = Mode 2 = Bi-dir bus */ 121 /* D4 = Port A direction (1 = input)*/ 122 /* D3 = Port C (upper 4 bits) */ 123 /* direction. (1 = input) */ 124 /* D2 = Mode selection (port B & C) */ 125 /* 0 = Mode 0 = Basic I/O */ 126 /* 1 = Mode 1 = Strobed I/O */ 127 /* D1 = Port B direction (1 = input)*/ 128 /* D0 = Port C (lower 4 bits) */ 129 /* direction. (1 = input) */ 130 131 /* So 91 means Basic I/O on all 3 ports,*/ 132 /* Port A is an input port, B is an */ 133 /* output port, C is split with upper */ 134 /* 4 bits being an output port and lower*/ 135 /* 4 bits an input port, and enable the */ 136 /* sucker. */ 137 /* Courtesy Intel 8255 databook. Lars */ 138#define MSE_HOLD 0x80 139#define MSE_RXLOW 0x00 140#define MSE_RXHIGH 0x20 141#define MSE_RYLOW 0x40 142#define MSE_RYHIGH 0x60 143#define MSE_DISINTR 0x10 144#define MSE_INTREN 0x00 145 146static int mse_probelogi(); 147static void mse_enablelogi(), mse_disablelogi(), mse_getlogi(); 148 149/* 150 * ATI Inport mouse definitions 151 */ 152#define MSE_INPORT_RESET 0x80 153#define MSE_INPORT_STATUS 0x00 154#define MSE_INPORT_DX 0x01 155#define MSE_INPORT_DY 0x02 156#define MSE_INPORT_MODE 0x07 157#define MSE_INPORT_HOLD 0x20 158#define MSE_INPORT_INTREN 0x09 159 160static int mse_probeati(); 161static void mse_enableati(), mse_disableati(), mse_getati(); 162 163#define MSEPRI (PZERO + 3) 164 165/* 166 * Table of mouse types. 167 * Keep the Logitech last, since I haven't figured out how to probe it 168 * properly yet. (Someday I'll have the documentation.) 169 */ 170struct mse_types { 171 int m_type; /* Type of bus mouse */ 172 int (*m_probe)(); /* Probe routine to test for it */ 173 void (*m_enable)(); /* Start routine */ 174 void (*m_disable)(); /* Disable interrupts routine */ 175 void (*m_get)(); /* and get mouse status */ 176} mse_types[] = { 177 { MSE_ATIINPORT, mse_probeati, mse_enableati, mse_disableati, mse_getati }, 178 { MSE_LOGITECH, mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi }, 179 { 0, }, 180}; 181 182int 183mseprobe(idp) 184 register struct isa_device *idp; 185{ 186 register struct mse_softc *sc = &mse_sc[idp->id_unit]; 187 register int i; 188 189 /* 190 * Check for each mouse type in the table. 191 */ 192 i = 0; 193 while (mse_types[i].m_type) { 194 if ((*mse_types[i].m_probe)(idp)) { 195 sc->sc_mousetype = mse_types[i].m_type; 196 sc->sc_enablemouse = mse_types[i].m_enable; 197 sc->sc_disablemouse = mse_types[i].m_disable; 198 sc->sc_getmouse = mse_types[i].m_get; 199 return (1); 200 } 201 i++; 202 } 203 return (0); 204} 205 206static struct kern_devconf kdc_mse[NMSE] = { { 207 0, 0, 0, /* filled in by dev_attach */ 208 "mse", 0, { MDDT_ISA, 0, "tty" }, 209 isa_generic_externalize, 0, 0, ISA_EXTERNALLEN, 210 &kdc_isa0, /* parent */ 211 0, /* parentdata */ 212 DC_UNKNOWN, /* not supported */ 213 "ATI or Logitech bus mouse adapter" 214} }; 215 216static inline void 217mse_registerdev(struct isa_device *id) 218{ 219 if(id->id_unit) 220 kdc_mse[id->id_unit] = kdc_mse[0]; 221 kdc_mse[id->id_unit].kdc_unit = id->id_unit; 222 kdc_mse[id->id_unit].kdc_isa = id; 223 dev_attach(&kdc_mse[id->id_unit]); 224} 225 226int 227mseattach(idp) 228 struct isa_device *idp; 229{ 230 struct mse_softc *sc = &mse_sc[idp->id_unit]; 231 232 sc->sc_port = idp->id_iobase; 233 mse_registerdev(idp); 234 return (1); 235} 236 237/* 238 * Exclusive open the mouse, initialize it and enable interrupts. 239 */ 240int 241mseopen(dev, flag) 242 dev_t dev; 243 int flag; 244{ 245 register struct mse_softc *sc; 246 int s; 247 248 if (MSE_UNIT(dev) >= NMSE) 249 return (ENXIO); 250 sc = &mse_sc[MSE_UNIT(dev)]; 251 if (sc->sc_flags & MSESC_OPEN) 252 return (EBUSY); 253 sc->sc_flags |= MSESC_OPEN; 254 sc->sc_obuttons = sc->sc_buttons = 0x7; 255 sc->sc_deltax = sc->sc_deltay = 0; 256 sc->sc_bytesread = PROTOBYTES; 257 258 /* 259 * Initialize mouse interface and enable interrupts. 260 */ 261 s = spltty(); 262 (*sc->sc_enablemouse)(sc->sc_port); 263 splx(s); 264 return (0); 265} 266 267/* 268 * mseclose: just turn off mouse innterrupts. 269 */ 270int 271mseclose(dev, flag) 272 dev_t dev; 273 int flag; 274{ 275 struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)]; 276 int s; 277 278 s = spltty(); 279 (*sc->sc_disablemouse)(sc->sc_port); 280 sc->sc_flags &= ~MSESC_OPEN; 281 splx(s); 282 return(0); 283} 284 285/* 286 * mseread: return mouse info using the MSC serial protocol, but without 287 * using bytes 4 and 5. 288 * (Yes this is cheesy, but it makes the X386 server happy, so...) 289 */ 290int 291mseread(dev, uio) 292 dev_t dev; 293 struct uio *uio; 294{ 295 register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)]; 296 int xfer, s, error; 297 298 /* 299 * If there are no protocol bytes to be read, set up a new protocol 300 * packet. 301 */ 302 s = spltty(); /* XXX Should be its own spl, but where is imlXX() */ 303 if (sc->sc_bytesread >= PROTOBYTES) { 304 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 && 305 (sc->sc_obuttons ^ sc->sc_buttons) == 0) { 306 if (MSE_NBLOCKIO(dev)) { 307 splx(s); 308 return (0); 309 } 310 sc->sc_flags |= MSESC_WANT; 311 if (error = tsleep((caddr_t)sc, MSEPRI | PCATCH, 312 "mseread", 0)) { 313 splx(s); 314 return (error); 315 } 316 } 317 318 /* 319 * Generate protocol bytes. 320 * For some reason X386 expects 5 bytes but never uses 321 * the fourth or fifth? 322 */ 323 sc->sc_bytes[0] = 0x80 | (sc->sc_buttons & ~0xf8); 324 if (sc->sc_deltax > 127) 325 sc->sc_deltax = 127; 326 if (sc->sc_deltax < -127) 327 sc->sc_deltax = -127; 328 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */ 329 if (sc->sc_deltay > 127) 330 sc->sc_deltay = 127; 331 if (sc->sc_deltay < -127) 332 sc->sc_deltay = -127; 333 sc->sc_bytes[1] = sc->sc_deltax; 334 sc->sc_bytes[2] = sc->sc_deltay; 335 sc->sc_bytes[3] = sc->sc_bytes[4] = 0; 336 sc->sc_obuttons = sc->sc_buttons; 337 sc->sc_deltax = sc->sc_deltay = 0; 338 sc->sc_bytesread = 0; 339 } 340 splx(s); 341 xfer = min(uio->uio_resid, PROTOBYTES - sc->sc_bytesread); 342 if (error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio)) 343 return (error); 344 sc->sc_bytesread += xfer; 345 return(0); 346} 347 348/* 349 * mseselect: check for mouse input to be processed. 350 */ 351int 352mseselect(dev, rw, p) 353 dev_t dev; 354 int rw; 355 struct proc *p; 356{ 357 register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)]; 358 int s; 359 360 s = spltty(); 361 if (sc->sc_bytesread != PROTOBYTES || sc->sc_deltax != 0 || 362 sc->sc_deltay != 0 || (sc->sc_obuttons ^ sc->sc_buttons) != 0) { 363 splx(s); 364 return (1); 365 } 366 367 /* 368 * Since this is an exclusive open device, any previous proc. 369 * pointer is trash now, so we can just assign it. 370 */ 371 selrecord(p, &sc->sc_selp); 372 splx(s); 373 return (0); 374} 375 376/* 377 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative. 378 */ 379void 380mseintr(unit) 381 int unit; 382{ 383 register struct mse_softc *sc = &mse_sc[unit]; 384 pid_t p; 385 386#ifdef DEBUG 387 static int mse_intrcnt = 0; 388 if((mse_intrcnt++ % 10000) == 0) 389 printf("mseintr\n"); 390#endif /* DEBUG */ 391 if ((sc->sc_flags & MSESC_OPEN) == 0) 392 return; 393 394 (*sc->sc_getmouse)(sc->sc_port, &sc->sc_deltax, &sc->sc_deltay, &sc->sc_buttons); 395 396 /* 397 * If mouse state has changed, wake up anyone wanting to know. 398 */ 399 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 || 400 (sc->sc_obuttons ^ sc->sc_buttons) != 0) { 401 if (sc->sc_flags & MSESC_WANT) { 402 sc->sc_flags &= ~MSESC_WANT; 403 wakeup((caddr_t)sc); 404 } 405 selwakeup(&sc->sc_selp); 406 } 407} 408 409/* 410 * Routines for the Logitech mouse. 411 */ 412/* 413 * Test for a Logitech bus mouse and return 1 if it is. 414 * (until I know how to use the signature port properly, just disable 415 * interrupts and return 1) 416 */ 417static int 418mse_probelogi(idp) 419 register struct isa_device *idp; 420{ 421 422 int sig; 423 424 outb(idp->id_iobase + MSE_PORTD, MSE_SETUP); 425 /* set the signature port */ 426 outb(idp->id_iobase + MSE_PORTB, MSE_LOGI_SIG); 427 428 DELAY(30000); /* 30 ms delay */ 429 sig = inb(idp->id_iobase + MSE_PORTB) & 0xFF; 430 if (sig == MSE_LOGI_SIG) { 431 outb(idp->id_iobase + MSE_PORTC, MSE_DISINTR); 432 return(1); 433 } else { 434 printf("mse%d: wrong signature %x\n",idp->id_unit,sig); 435 return(0); 436 } 437} 438 439/* 440 * Initialize Logitech mouse and enable interrupts. 441 */ 442static void 443mse_enablelogi(port) 444 register u_int port; 445{ 446 int dx, dy, but; 447 448 outb(port + MSE_PORTD, MSE_SETUP); 449 mse_getlogi(port, &dx, &dy, &but); 450} 451 452/* 453 * Disable interrupts for Logitech mouse. 454 */ 455static void 456mse_disablelogi(port) 457 register u_int port; 458{ 459 460 outb(port + MSE_PORTC, MSE_DISINTR); 461} 462 463/* 464 * Get the current dx, dy and button up/down state. 465 */ 466static void 467mse_getlogi(port, dx, dy, but) 468 register u_int port; 469 int *dx; 470 int *dy; 471 int *but; 472{ 473 register char x, y; 474 475 outb(port + MSE_PORTC, MSE_HOLD | MSE_RXLOW); 476 x = inb(port + MSE_PORTA); 477 *but = (x >> 5) & 0x7; 478 x &= 0xf; 479 outb(port + MSE_PORTC, MSE_HOLD | MSE_RXHIGH); 480 x |= (inb(port + MSE_PORTA) << 4); 481 outb(port + MSE_PORTC, MSE_HOLD | MSE_RYLOW); 482 y = (inb(port + MSE_PORTA) & 0xf); 483 outb(port + MSE_PORTC, MSE_HOLD | MSE_RYHIGH); 484 y |= (inb(port + MSE_PORTA) << 4); 485 *dx += x; 486 *dy += y; 487 outb(port + MSE_PORTC, MSE_INTREN); 488} 489 490/* 491 * Routines for the ATI Inport bus mouse. 492 */ 493/* 494 * Test for a ATI Inport bus mouse and return 1 if it is. 495 * (do not enable interrupts) 496 */ 497static int 498mse_probeati(idp) 499 register struct isa_device *idp; 500{ 501 int i; 502 503 for (i = 0; i < 2; i++) 504 if (inb(idp->id_iobase + MSE_PORTC) == 0xde) 505 return (1); 506 return (0); 507} 508 509/* 510 * Initialize ATI Inport mouse and enable interrupts. 511 */ 512static void 513mse_enableati(port) 514 register u_int port; 515{ 516 517 outb(port + MSE_PORTA, MSE_INPORT_RESET); 518 outb(port + MSE_PORTA, MSE_INPORT_MODE); 519 outb(port + MSE_PORTB, MSE_INPORT_INTREN); 520} 521 522/* 523 * Disable interrupts for ATI Inport mouse. 524 */ 525static void 526mse_disableati(port) 527 register u_int port; 528{ 529 530 outb(port + MSE_PORTA, MSE_INPORT_MODE); 531 outb(port + MSE_PORTB, 0); 532} 533 534/* 535 * Get current dx, dy and up/down button state. 536 */ 537static void 538mse_getati(port, dx, dy, but) 539 register u_int port; 540 int *dx; 541 int *dy; 542 int *but; 543{ 544 register char byte; 545 546 outb(port + MSE_PORTA, MSE_INPORT_MODE); 547 outb(port + MSE_PORTB, MSE_INPORT_HOLD); 548 outb(port + MSE_PORTA, MSE_INPORT_STATUS); 549 *but = ~(inb(port + MSE_PORTB) & 0x7); 550 outb(port + MSE_PORTA, MSE_INPORT_DX); 551 byte = inb(port + MSE_PORTB); 552 *dx += byte; 553 outb(port + MSE_PORTA, MSE_INPORT_DY); 554 byte = inb(port + MSE_PORTB); 555 *dy += byte; 556 outb(port + MSE_PORTA, MSE_INPORT_MODE); 557 outb(port + MSE_PORTB, MSE_INPORT_INTREN); 558} 559#endif /* NMSE */ 560