mse_isa.c revision 140040
1/*- 2 * Copyright (c) 2004 M. Warner Losh 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/dev/mse/mse_isa.c 140040 2005-01-11 06:24:40Z imp $ 27 */ 28 29/*- 30 * Copyright 1992 by the University of Guelph 31 * 32 * Permission to use, copy and modify this 33 * software and its documentation for any purpose and without 34 * fee is hereby granted, provided that the above copyright 35 * notice appear in all copies and that both that copyright 36 * notice and this permission notice appear in supporting 37 * documentation. 38 * University of Guelph makes no representations about the suitability of 39 * this software for any purpose. It is provided "as is" 40 * without express or implied warranty. 41 */ 42/* 43 * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and 44 * the X386 port, courtesy of 45 * Rick Macklem, rick@snowhite.cis.uoguelph.ca 46 * Caveats: The driver currently uses spltty(), but doesn't use any 47 * generic tty code. It could use splmse() (that only masks off the 48 * bus mouse interrupt, but that would require hacking in i386/isa/icu.s. 49 * (This may be worth the effort, since the Logitech generates 30/60 50 * interrupts/sec continuously while it is open.) 51 * NB: The ATI has NOT been tested yet! 52 */ 53 54/* 55 * Modification history: 56 * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com) 57 * improved probe based on input from Logitech. 58 * 59 * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu) 60 * fixes to make it work with Microsoft InPort busmouse 61 * 62 * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu) 63 * added patches for new "select" interface 64 * 65 * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu) 66 * changed position of some spl()'s in mseread 67 * 68 * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu) 69 * limit maximum negative x/y value to -127 to work around XFree problem 70 * that causes spurious button pushes. 71 */ 72 73#include <sys/param.h> 74#include <sys/systm.h> 75#include <sys/conf.h> 76#include <sys/kernel.h> 77#include <sys/module.h> 78#include <sys/bus.h> 79#include <sys/poll.h> 80#include <sys/selinfo.h> 81#include <sys/uio.h> 82#include <sys/mouse.h> 83 84#include <machine/bus.h> 85#include <machine/clock.h> 86#include <machine/resource.h> 87#include <sys/rman.h> 88 89#include <isa/isavar.h> 90 91#include <dev/mse/msevar.h> 92 93static int mse_probe(device_t dev); 94static int mse_attach(device_t dev); 95static int mse_detach(device_t dev); 96 97static device_method_t mse_methods[] = { 98 DEVMETHOD(device_probe, mse_probe), 99 DEVMETHOD(device_attach, mse_attach), 100 DEVMETHOD(device_detach, mse_detach), 101 { 0, 0 } 102}; 103 104static driver_t mse_driver = { 105 "mse", 106 mse_methods, 107 sizeof(mse_softc_t), 108}; 109 110DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0); 111 112static struct isa_pnp_id mse_ids[] = { 113 { 0x000fd041, "Bus mouse" }, /* PNP0F00 */ 114 { 0x020fd041, "InPort mouse" }, /* PNP0F02 */ 115 { 0x0d0fd041, "InPort mouse compatible" }, /* PNP0F0D */ 116 { 0x110fd041, "Bus mouse compatible" }, /* PNP0F11 */ 117 { 0x150fd041, "Logitech bus mouse" }, /* PNP0F15 */ 118 { 0x180fd041, "Logitech bus mouse compatible" },/* PNP0F18 */ 119 { 0 } 120}; 121 122/* 123 * Logitech bus mouse definitions 124 */ 125#define MSE_SETUP 0x91 /* What does this mean? */ 126 /* The definition for the control port */ 127 /* is as follows: */ 128 129 /* D7 = Mode set flag (1 = active) */ 130 /* D6,D5 = Mode selection (port A) */ 131 /* 00 = Mode 0 = Basic I/O */ 132 /* 01 = Mode 1 = Strobed I/O */ 133 /* 10 = Mode 2 = Bi-dir bus */ 134 /* D4 = Port A direction (1 = input)*/ 135 /* D3 = Port C (upper 4 bits) */ 136 /* direction. (1 = input) */ 137 /* D2 = Mode selection (port B & C) */ 138 /* 0 = Mode 0 = Basic I/O */ 139 /* 1 = Mode 1 = Strobed I/O */ 140 /* D1 = Port B direction (1 = input)*/ 141 /* D0 = Port C (lower 4 bits) */ 142 /* direction. (1 = input) */ 143 144 /* So 91 means Basic I/O on all 3 ports,*/ 145 /* Port A is an input port, B is an */ 146 /* output port, C is split with upper */ 147 /* 4 bits being an output port and lower*/ 148 /* 4 bits an input port, and enable the */ 149 /* sucker. */ 150 /* Courtesy Intel 8255 databook. Lars */ 151#define MSE_HOLD 0x80 152#define MSE_RXLOW 0x00 153#define MSE_RXHIGH 0x20 154#define MSE_RYLOW 0x40 155#define MSE_RYHIGH 0x60 156#define MSE_DISINTR 0x10 157#define MSE_INTREN 0x00 158 159static int mse_probelogi(device_t dev, mse_softc_t *sc); 160static void mse_disablelogi(bus_space_tag_t t, 161 bus_space_handle_t h); 162static void mse_getlogi(bus_space_tag_t t, bus_space_handle_t h, 163 int *dx, int *dy, int *but); 164static void mse_enablelogi(bus_space_tag_t t, 165 bus_space_handle_t h); 166 167/* 168 * ATI Inport mouse definitions 169 */ 170#define MSE_INPORT_RESET 0x80 171#define MSE_INPORT_STATUS 0x00 172#define MSE_INPORT_DX 0x01 173#define MSE_INPORT_DY 0x02 174#define MSE_INPORT_MODE 0x07 175#define MSE_INPORT_HOLD 0x20 176#define MSE_INPORT_INTREN 0x09 177 178static int mse_probeati(device_t dev, mse_softc_t *sc); 179static void mse_enableati(bus_space_tag_t t, bus_space_handle_t h); 180static void mse_disableati(bus_space_tag_t t, bus_space_handle_t h); 181static void mse_getati(bus_space_tag_t t, bus_space_handle_t h, 182 int *dx, int *dy, int *but); 183 184static struct mse_types mse_types[] = { 185 { MSE_ATIINPORT, 186 mse_probeati, mse_enableati, mse_disableati, mse_getati, 187 { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, }, 188 { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 189 { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, }, 190 { MSE_LOGITECH, 191 mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi, 192 { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, }, 193 { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 194 { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, }, 195 { 0, }, 196}; 197 198static int 199mse_probe(dev) 200 device_t dev; 201{ 202 mse_softc_t *sc; 203 int error; 204 int rid; 205 int i; 206 207 /* check PnP IDs */ 208 error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids); 209 if (error == ENXIO) 210 return error; 211 212 sc = device_get_softc(dev); 213 rid = 0; 214 sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 215 MSE_IOSIZE, RF_ACTIVE); 216 if (sc->sc_port == NULL) 217 return ENXIO; 218 sc->sc_iot = rman_get_bustag(sc->sc_port); 219 sc->sc_ioh = rman_get_bushandle(sc->sc_port); 220 221 /* 222 * Check for each mouse type in the table. 223 */ 224 i = 0; 225 while (mse_types[i].m_type) { 226 if ((*mse_types[i].m_probe)(dev, sc)) { 227 sc->sc_mousetype = mse_types[i].m_type; 228 sc->sc_enablemouse = mse_types[i].m_enable; 229 sc->sc_disablemouse = mse_types[i].m_disable; 230 sc->sc_getmouse = mse_types[i].m_get; 231 sc->hw = mse_types[i].m_hw; 232 sc->mode = mse_types[i].m_mode; 233 bus_release_resource(dev, SYS_RES_IOPORT, rid, 234 sc->sc_port); 235 device_set_desc(dev, "Bus/InPort Mouse"); 236 return 0; 237 } 238 i++; 239 } 240 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port); 241 return ENXIO; 242} 243 244static int 245mse_attach(dev) 246 device_t dev; 247{ 248 mse_softc_t *sc; 249 int rid; 250 251 sc = device_get_softc(dev); 252 253 rid = 0; 254 sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 255 MSE_IOSIZE, RF_ACTIVE); 256 if (sc->sc_port == NULL) 257 return ENXIO; 258 sc->sc_iot = rman_get_bustag(sc->sc_port); 259 sc->sc_ioh = rman_get_bushandle(sc->sc_port); 260 261 return (mse_common_attach(dev)); 262} 263 264static int 265mse_detach(dev) 266 device_t dev; 267{ 268 mse_softc_t *sc; 269 int rid; 270 271 sc = device_get_softc(dev); 272 if (sc->sc_flags & MSESC_OPEN) 273 return EBUSY; 274 275 rid = 0; 276 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih); 277 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr); 278 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port); 279 280 destroy_dev(sc->sc_dev); 281 destroy_dev(sc->sc_ndev); 282 283 return 0; 284} 285 286/* 287 * Routines for the Logitech mouse. 288 */ 289/* 290 * Test for a Logitech bus mouse and return 1 if it is. 291 * (until I know how to use the signature port properly, just disable 292 * interrupts and return 1) 293 */ 294static int 295mse_probelogi(dev, sc) 296 device_t dev; 297 mse_softc_t *sc; 298{ 299 300 int sig; 301 302 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTD, MSE_SETUP); 303 /* set the signature port */ 304 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB, MSE_LOGI_SIG); 305 306 DELAY(30000); /* 30 ms delay */ 307 sig = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB) & 0xFF; 308 if (sig == MSE_LOGI_SIG) { 309 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC, 310 MSE_DISINTR); 311 return(1); 312 } else { 313 if (bootverbose) 314 device_printf(dev, "wrong signature %x\n", sig); 315 return(0); 316 } 317} 318 319/* 320 * Initialize Logitech mouse and enable interrupts. 321 */ 322static void 323mse_enablelogi(tag, handle) 324 bus_space_tag_t tag; 325 bus_space_handle_t handle; 326{ 327 int dx, dy, but; 328 329 bus_space_write_1(tag, handle, MSE_PORTD, MSE_SETUP); 330 mse_getlogi(tag, handle, &dx, &dy, &but); 331} 332 333/* 334 * Disable interrupts for Logitech mouse. 335 */ 336static void 337mse_disablelogi(tag, handle) 338 bus_space_tag_t tag; 339 bus_space_handle_t handle; 340{ 341 342 bus_space_write_1(tag, handle, MSE_PORTC, MSE_DISINTR); 343} 344 345/* 346 * Get the current dx, dy and button up/down state. 347 */ 348static void 349mse_getlogi(tag, handle, dx, dy, but) 350 bus_space_tag_t tag; 351 bus_space_handle_t handle; 352 int *dx; 353 int *dy; 354 int *but; 355{ 356 register char x, y; 357 358 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXLOW); 359 x = bus_space_read_1(tag, handle, MSE_PORTA); 360 *but = (x >> 5) & MOUSE_MSC_BUTTONS; 361 x &= 0xf; 362 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXHIGH); 363 x |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4); 364 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYLOW); 365 y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0xf); 366 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYHIGH); 367 y |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4); 368 *dx = x; 369 *dy = y; 370 bus_space_write_1(tag, handle, MSE_PORTC, MSE_INTREN); 371} 372 373/* 374 * Routines for the ATI Inport bus mouse. 375 */ 376/* 377 * Test for an ATI Inport bus mouse and return 1 if it is. 378 * (do not enable interrupts) 379 */ 380static int 381mse_probeati(dev, sc) 382 device_t dev; 383 mse_softc_t *sc; 384{ 385 int i; 386 387 for (i = 0; i < 2; i++) 388 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC) == 0xde) 389 return (1); 390 return (0); 391} 392 393/* 394 * Initialize ATI Inport mouse and enable interrupts. 395 */ 396static void 397mse_enableati(tag, handle) 398 bus_space_tag_t tag; 399 bus_space_handle_t handle; 400{ 401 402 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_RESET); 403 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE); 404 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN); 405} 406 407/* 408 * Disable interrupts for ATI Inport mouse. 409 */ 410static void 411mse_disableati(tag, handle) 412 bus_space_tag_t tag; 413 bus_space_handle_t handle; 414{ 415 416 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE); 417 bus_space_write_1(tag, handle, MSE_PORTB, 0); 418} 419 420/* 421 * Get current dx, dy and up/down button state. 422 */ 423static void 424mse_getati(tag, handle, dx, dy, but) 425 bus_space_tag_t tag; 426 bus_space_handle_t handle; 427 int *dx; 428 int *dy; 429 int *but; 430{ 431 register char byte; 432 433 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE); 434 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_HOLD); 435 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_STATUS); 436 *but = ~bus_space_read_1(tag, handle, MSE_PORTB) & MOUSE_MSC_BUTTONS; 437 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DX); 438 byte = bus_space_read_1(tag, handle, MSE_PORTB); 439 *dx = byte; 440 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DY); 441 byte = bus_space_read_1(tag, handle, MSE_PORTB); 442 *dy = byte; 443 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE); 444 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN); 445} 446