1 /* $NetBSD: lms.c,v 1.17 1994/11/18 22:03:26 mycroft Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, 1994 Charles Hannum. 5 * Copyright (c) 1992, 1993 Erik Forsberg. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 17 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/param.h> 27 #include <sys/kernel.h> 28 #include <sys/systm.h> 29 #include <sys/buf.h> 30 #include <sys/malloc.h> 31 #include <sys/ioctl.h> 32 #include <sys/tty.h> 33 #include <sys/file.h> 34 #include <sys/select.h> 35 #include <sys/proc.h> 36 #include <sys/vnode.h> 37 #include <sys/device.h> 38 39 #include <machine/cpu.h> 40 #include <machine/pio.h> 41 #include <machine/mouse.h> 42 43 #include <i386/isa/isavar.h> 44 45 #define LMS_DATA 0 /* offset for data port, read-only */ 46 #define LMS_SIGN 1 /* offset for signature port, read-write */ 47 #define LMS_INTR 2 /* offset for interrupt port, read-only */ 48 #define LMS_CNTRL 2 /* offset for control port, write-only */ 49 #define LMS_CONFIG 3 /* for configuration port, read-write */ 50 #define LMS_NPORTS 4 51 52 #define LMS_CHUNK 128 /* chunk size for read */ 53 #define LMS_BSIZE 1020 /* buffer size */ 54 55 struct lms_softc { /* driver status information */ 56 struct device sc_dev; 57 struct intrhand sc_ih; 58 59 struct clist sc_q; 60 struct selinfo sc_rsel; 61 int sc_iobase; /* I/O port base */ 62 u_char sc_state; /* mouse driver state */ 63 #define LMS_OPEN 0x01 /* device is open */ 64 #define LMS_ASLP 0x02 /* waiting for mouse data */ 65 u_char sc_status; /* mouse button status */ 66 int sc_x, sc_y; /* accumulated motion in the X,Y axis */ 67 }; 68 69 int lmsprobe __P((struct device *, void *, void *)); 70 void lmsattach __P((struct device *, struct device *, void *)); 71 int lmsintr __P((struct lms_softc *)); 72 73 struct cfdriver lmscd = { 74 NULL, "lms", lmsprobe, lmsattach, DV_TTY, sizeof(struct lms_softc) 75 }; 76 77 #define LMSUNIT(dev) (minor(dev)) 78 79 int 80 lmsprobe(parent, match, aux) 81 struct device *parent; 82 void *match, *aux; 83 { 84 struct isa_attach_args *ia = aux; 85 int iobase = ia->ia_iobase; 86 87 /* Configure and check for port present. */ 88 outb(iobase + LMS_CONFIG, 0x91); 89 delay(10); 90 outb(iobase + LMS_SIGN, 0x0c); 91 delay(10); 92 if (inb(iobase + LMS_SIGN) != 0x0c) 93 return 0; 94 outb(iobase + LMS_SIGN, 0x50); 95 delay(10); 96 if (inb(iobase + LMS_SIGN) != 0x50) 97 return 0; 98 99 /* Disable interrupts. */ 100 outb(iobase + LMS_CNTRL, 0x10); 101 102 ia->ia_iosize = LMS_NPORTS; 103 ia->ia_msize = 0; 104 return 1; 105 } 106 107 void 108 lmsattach(parent, self, aux) 109 struct device *parent, *self; 110 void *aux; 111 { 112 struct lms_softc *sc = (void *)self; 113 struct isa_attach_args *ia = aux; 114 int iobase = ia->ia_iobase; 115 116 printf("\n"); 117 118 /* Other initialization was done by lmsprobe. */ 119 sc->sc_iobase = iobase; 120 sc->sc_state = 0; 121 122 sc->sc_ih.ih_fun = lmsintr; 123 sc->sc_ih.ih_arg = sc; 124 sc->sc_ih.ih_level = IPL_NONE; 125 intr_establish(ia->ia_irq, &sc->sc_ih); 126 } 127 128 int 129 lmsopen(dev, flag) 130 dev_t dev; 131 int flag; 132 { 133 int unit = LMSUNIT(dev); 134 struct lms_softc *sc; 135 136 if (unit >= lmscd.cd_ndevs) 137 return ENXIO; 138 sc = lmscd.cd_devs[unit]; 139 if (!sc) 140 return ENXIO; 141 142 if (sc->sc_state & LMS_OPEN) 143 return EBUSY; 144 145 if (clalloc(&sc->sc_q, LMS_BSIZE, 0) == -1) 146 return ENOMEM; 147 148 sc->sc_state |= LMS_OPEN; 149 sc->sc_status = 0; 150 sc->sc_x = sc->sc_y = 0; 151 152 /* Enable interrupts. */ 153 outb(sc->sc_iobase + LMS_CNTRL, 0); 154 155 return 0; 156 } 157 158 int 159 lmsclose(dev, flag) 160 dev_t dev; 161 int flag; 162 { 163 struct lms_softc *sc = lmscd.cd_devs[LMSUNIT(dev)]; 164 165 /* Disable interrupts. */ 166 outb(sc->sc_iobase + LMS_CNTRL, 0x10); 167 168 sc->sc_state &= ~LMS_OPEN; 169 170 clfree(&sc->sc_q); 171 172 return 0; 173 } 174 175 int 176 lmsread(dev, uio, flag) 177 dev_t dev; 178 struct uio *uio; 179 int flag; 180 { 181 struct lms_softc *sc = lmscd.cd_devs[LMSUNIT(dev)]; 182 int s; 183 int error; 184 size_t length; 185 u_char buffer[LMS_CHUNK]; 186 187 /* Block until mouse activity occured. */ 188 189 s = spltty(); 190 while (sc->sc_q.c_cc == 0) { 191 if (flag & IO_NDELAY) { 192 splx(s); 193 return EWOULDBLOCK; 194 } 195 sc->sc_state |= LMS_ASLP; 196 if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0)) { 197 sc->sc_state &= ~LMS_ASLP; 198 splx(s); 199 return error; 200 } 201 } 202 splx(s); 203 204 /* Transfer as many chunks as possible. */ 205 206 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) { 207 length = min(sc->sc_q.c_cc, uio->uio_resid); 208 if (length > sizeof(buffer)) 209 length = sizeof(buffer); 210 211 /* Remove a small chunk from the input queue. */ 212 (void) q_to_b(&sc->sc_q, buffer, length); 213 214 /* Copy the data to the user process. */ 215 if (error = uiomove(buffer, length, uio)) 216 break; 217 } 218 219 return error; 220 } 221 222 int 223 lmsioctl(dev, cmd, addr, flag) 224 dev_t dev; 225 u_long cmd; 226 caddr_t addr; 227 int flag; 228 { 229 struct lms_softc *sc = lmscd.cd_devs[LMSUNIT(dev)]; 230 struct mouseinfo info; 231 int s; 232 int error; 233 234 switch (cmd) { 235 case MOUSEIOCREAD: 236 s = spltty(); 237 238 info.status = sc->sc_status; 239 if (sc->sc_x || sc->sc_y) 240 info.status |= MOVEMENT; 241 242 if (sc->sc_x > 127) 243 info.xmotion = 127; 244 else if (sc->sc_x < -127) 245 /* Bounding at -127 avoids a bug in XFree86. */ 246 info.xmotion = -127; 247 else 248 info.xmotion = sc->sc_x; 249 250 if (sc->sc_y > 127) 251 info.ymotion = 127; 252 else if (sc->sc_y < -127) 253 info.ymotion = -127; 254 else 255 info.ymotion = sc->sc_y; 256 257 /* Reset historical information. */ 258 sc->sc_x = sc->sc_y = 0; 259 sc->sc_status &= ~BUTCHNGMASK; 260 ndflush(&sc->sc_q, sc->sc_q.c_cc); 261 262 splx(s); 263 error = copyout(&info, addr, sizeof(struct mouseinfo)); 264 break; 265 266 default: 267 error = EINVAL; 268 break; 269 } 270 271 return error; 272 } 273 274 int 275 lmsintr(sc) 276 struct lms_softc *sc; 277 { 278 int iobase = sc->sc_iobase; 279 u_char hi, lo, buttons, changed; 280 char dx, dy; 281 u_char buffer[5]; 282 283 if ((sc->sc_state & LMS_OPEN) == 0) 284 /* Interrupts are not expected. */ 285 return 0; 286 287 outb(iobase + LMS_CNTRL, 0xab); 288 hi = inb(iobase + LMS_DATA); 289 outb(iobase + LMS_CNTRL, 0x90); 290 lo = inb(iobase + LMS_DATA); 291 dx = ((hi & 0x0f) << 4) | (lo & 0x0f); 292 /* Bounding at -127 avoids a bug in XFree86. */ 293 dx = (dx == -128) ? -127 : dx; 294 295 outb(iobase + LMS_CNTRL, 0xf0); 296 hi = inb(iobase + LMS_DATA); 297 outb(iobase + LMS_CNTRL, 0xd0); 298 lo = inb(iobase + LMS_DATA); 299 dy = ((hi & 0x0f) << 4) | (lo & 0x0f); 300 dy = (dy == -128) ? 127 : -dy; 301 302 outb(iobase + LMS_CNTRL, 0); 303 304 buttons = (~hi >> 5) & 0x07; 305 changed = ((buttons ^ sc->sc_status) & 0x07) << 3; 306 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed; 307 308 if (dx || dy || changed) { 309 /* Update accumulated movements. */ 310 sc->sc_x += dx; 311 sc->sc_y += dy; 312 313 /* Add this event to the queue. */ 314 buffer[0] = 0x80 | (buttons ^ BUTSTATMASK); 315 buffer[1] = dx; 316 buffer[2] = dy; 317 buffer[3] = buffer[4] = 0; 318 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q); 319 320 if (sc->sc_state & LMS_ASLP) { 321 sc->sc_state &= ~LMS_ASLP; 322 wakeup((caddr_t)sc); 323 } 324 selwakeup(&sc->sc_rsel); 325 } 326 327 return -1; 328 } 329 330 int 331 lmsselect(dev, rw, p) 332 dev_t dev; 333 int rw; 334 struct proc *p; 335 { 336 struct lms_softc *sc = lmscd.cd_devs[LMSUNIT(dev)]; 337 int s; 338 int ret; 339 340 if (rw == FWRITE) 341 return 0; 342 343 s = spltty(); 344 if (!sc->sc_q.c_cc) { 345 selrecord(p, &sc->sc_rsel); 346 ret = 0; 347 } else 348 ret = 1; 349 splx(s); 350 351 return ret; 352 } 353