1 /* $OpenBSD: mms.c,v 1.12 2000/05/16 18:12:14 mickey Exp $ */ 2 /* $NetBSD: mms.c,v 1.24 1996/05/12 23:12:18 mycroft Exp $ */ 3 4 /*- 5 * Copyright (c) 1993, 1994 Charles Hannum. 6 * Copyright (c) 1992, 1993 Erik Forsberg. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED 16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 18 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/kernel.h> 29 #include <sys/systm.h> 30 #include <sys/buf.h> 31 #include <sys/malloc.h> 32 #include <sys/ioctl.h> 33 #include <sys/tty.h> 34 #include <sys/file.h> 35 #include <sys/select.h> 36 #include <sys/proc.h> 37 #include <sys/signalvar.h> 38 #include <sys/vnode.h> 39 #include <sys/device.h> 40 41 #include <machine/cpu.h> 42 #include <machine/intr.h> 43 #include <machine/pio.h> 44 #include <machine/mouse.h> 45 #include <machine/conf.h> 46 47 #include <dev/isa/isavar.h> 48 #include <dev/rndvar.h> 49 50 #define MMS_ADDR 0 /* offset for register select */ 51 #define MMS_DATA 1 /* offset for InPort data */ 52 #define MMS_IDENT 2 /* offset for identification register */ 53 #define MMS_NPORTS 4 54 55 #define MMS_CHUNK 128 /* chunk size for read */ 56 #define MMS_BSIZE 1020 /* buffer size */ 57 58 struct mms_softc { /* driver status information */ 59 struct device sc_dev; 60 void *sc_ih; 61 62 struct clist sc_q; 63 struct selinfo sc_rsel; 64 struct proc *sc_io; /* process that opened mms (can get SIGIO) */ 65 char sc_async; /* send SIGIO on input ready */ 66 int sc_iobase; /* I/O port base */ 67 u_char sc_state; /* mouse driver state */ 68 #define MMS_OPEN 0x01 /* device is open */ 69 #define MMS_ASLP 0x02 /* waiting for mouse data */ 70 u_char sc_status; /* mouse button status */ 71 int sc_x, sc_y; /* accumulated motion in the X,Y axis */ 72 }; 73 74 int mmsprobe __P((struct device *, void *, void *)); 75 void mmsattach __P((struct device *, struct device *, void *)); 76 int mmsintr __P((void *)); 77 78 struct cfattach mms_ca = { 79 sizeof(struct mms_softc), mmsprobe, mmsattach 80 }; 81 82 struct cfdriver mms_cd = { 83 NULL, "mms", DV_TTY 84 }; 85 86 #define MMSUNIT(dev) (minor(dev)) 87 88 int 89 mmsprobe(parent, match, aux) 90 struct device *parent; 91 void *match, *aux; 92 { 93 struct isa_attach_args *ia = aux; 94 int iobase = ia->ia_iobase; 95 96 /* Read identification register to see if present */ 97 if (inb(iobase + MMS_IDENT) != 0xde) 98 return 0; 99 100 /* Seems it was there; reset. */ 101 outb(iobase + MMS_ADDR, 0x87); 102 103 ia->ia_iosize = MMS_NPORTS; 104 ia->ia_msize = 0; 105 return 1; 106 } 107 108 void 109 mmsattach(parent, self, aux) 110 struct device *parent, *self; 111 void *aux; 112 { 113 struct mms_softc *sc = (void *)self; 114 struct isa_attach_args *ia = aux; 115 int iobase = ia->ia_iobase; 116 117 printf("\n"); 118 119 /* Other initialization was done by mmsprobe. */ 120 sc->sc_iobase = iobase; 121 sc->sc_state = 0; 122 123 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE, 124 IPL_TTY, mmsintr, sc, sc->sc_dev.dv_xname); 125 } 126 127 int 128 mmsopen(dev, flag, mode, p) 129 dev_t dev; 130 int flag; 131 int mode; 132 struct proc *p; 133 { 134 int unit = MMSUNIT(dev); 135 struct mms_softc *sc; 136 137 if (unit >= mms_cd.cd_ndevs) 138 return ENXIO; 139 sc = mms_cd.cd_devs[unit]; 140 if (!sc) 141 return ENXIO; 142 143 if (sc->sc_state & MMS_OPEN) 144 return EBUSY; 145 146 if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1) 147 return ENOMEM; 148 149 sc->sc_state |= MMS_OPEN; 150 sc->sc_status = 0; 151 sc->sc_x = sc->sc_y = 0; 152 sc->sc_async = 0; 153 sc->sc_io = p; 154 155 /* Enable interrupts. */ 156 outb(sc->sc_iobase + MMS_ADDR, 0x07); 157 outb(sc->sc_iobase + MMS_DATA, 0x09); 158 159 return 0; 160 } 161 162 int 163 mmsclose(dev, flag, mode, p) 164 dev_t dev; 165 int flag; 166 int mode; 167 struct proc *p; 168 { 169 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)]; 170 171 /* Disable interrupts. */ 172 outb(sc->sc_iobase + MMS_ADDR, 0x87); 173 174 sc->sc_state &= ~MMS_OPEN; 175 sc->sc_io = NULL; 176 177 clfree(&sc->sc_q); 178 179 return 0; 180 } 181 182 int 183 mmsread(dev, uio, flag) 184 dev_t dev; 185 struct uio *uio; 186 int flag; 187 { 188 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)]; 189 int s; 190 int error = 0; 191 size_t length; 192 u_char buffer[MMS_CHUNK]; 193 194 /* Block until mouse activity occured. */ 195 196 s = spltty(); 197 while (sc->sc_q.c_cc == 0) { 198 if (flag & IO_NDELAY) { 199 splx(s); 200 return EWOULDBLOCK; 201 } 202 sc->sc_state |= MMS_ASLP; 203 error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0); 204 if (error) { 205 sc->sc_state &= ~MMS_ASLP; 206 splx(s); 207 return error; 208 } 209 } 210 splx(s); 211 212 /* Transfer as many chunks as possible. */ 213 214 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) { 215 length = min(sc->sc_q.c_cc, uio->uio_resid); 216 if (length > sizeof(buffer)) 217 length = sizeof(buffer); 218 219 /* Remove a small chunk from the input queue. */ 220 (void) q_to_b(&sc->sc_q, buffer, length); 221 222 /* Copy the data to the user process. */ 223 if ((error = uiomove(buffer, length, uio)) != 0) 224 break; 225 } 226 227 return error; 228 } 229 230 int 231 mmsioctl(dev, cmd, addr, flag, p) 232 dev_t dev; 233 u_long cmd; 234 caddr_t addr; 235 int flag; 236 struct proc *p; 237 { 238 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)]; 239 struct mouseinfo info; 240 int s; 241 int error; 242 243 switch (cmd) { 244 case FIONBIO: /* we will remove this someday (soon???) */ 245 return (0); 246 247 case FIOASYNC: 248 sc->sc_async = *(int *)addr != 0; 249 break; 250 251 case TIOCSPGRP: 252 if (*(int *)addr != sc->sc_io->p_pgid) 253 return (EPERM); 254 break; 255 256 case MOUSEIOCREAD: 257 s = spltty(); 258 259 info.status = sc->sc_status; 260 if (sc->sc_x || sc->sc_y) 261 info.status |= MOVEMENT; 262 263 if (sc->sc_x > 127) 264 info.xmotion = 127; 265 else if (sc->sc_x < -127) 266 /* Bounding at -127 avoids a bug in XFree86. */ 267 info.xmotion = -127; 268 else 269 info.xmotion = sc->sc_x; 270 271 if (sc->sc_y > 127) 272 info.ymotion = 127; 273 else if (sc->sc_y < -127) 274 info.ymotion = -127; 275 else 276 info.ymotion = sc->sc_y; 277 278 /* Reset historical information. */ 279 sc->sc_x = sc->sc_y = 0; 280 sc->sc_status &= ~BUTCHNGMASK; 281 ndflush(&sc->sc_q, sc->sc_q.c_cc); 282 283 splx(s); 284 error = copyout(&info, addr, sizeof(struct mouseinfo)); 285 break; 286 287 case MOUSEIOCSRAW: 288 error = ENODEV; 289 break; 290 291 case MOUSEIOCSCOOKED: /* Do nothing. */ 292 break; 293 294 default: 295 error = EINVAL; 296 break; 297 } 298 299 return error; 300 } 301 302 int 303 mmsintr(arg) 304 void *arg; 305 { 306 struct mms_softc *sc = arg; 307 int iobase = sc->sc_iobase; 308 u_char buttons, changed, status; 309 char dx, dy; 310 u_char buffer[5]; 311 312 if ((sc->sc_state & MMS_OPEN) == 0) 313 /* Interrupts are not expected. */ 314 return 0; 315 316 /* Freeze InPort registers (disabling interrupts). */ 317 outb(iobase + MMS_ADDR, 0x07); 318 outb(iobase + MMS_DATA, 0x29); 319 320 outb(iobase + MMS_ADDR, 0x00); 321 status = inb(iobase + MMS_DATA); 322 323 if (status & 0x40) { 324 outb(iobase + MMS_ADDR, 1); 325 dx = inb(iobase + MMS_DATA); 326 dx = (dx == -128) ? -127 : dx; 327 outb(iobase + MMS_ADDR, 2); 328 dy = inb(iobase + MMS_DATA); 329 dy = (dy == -128) ? 127 : -dy; 330 } else 331 dx = dy = 0; 332 333 /* Unfreeze InPort registers (reenabling interrupts). */ 334 outb(iobase + MMS_ADDR, 0x07); 335 outb(iobase + MMS_DATA, 0x09); 336 337 buttons = status & BUTSTATMASK; 338 changed = status & BUTCHNGMASK; 339 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed; 340 341 if (dx || dy || changed) { 342 /* Update accumulated movements. */ 343 sc->sc_x += dx; 344 sc->sc_y += dy; 345 346 /* Add this event to the queue. */ 347 buffer[0] = 0x80 | (buttons ^ BUTSTATMASK); 348 buffer[1] = dx; 349 buffer[2] = dy; 350 buffer[3] = buffer[4] = 0; 351 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q); 352 add_mouse_randomness(*(u_int32_t*)buffer); 353 354 if (sc->sc_state & MMS_ASLP) { 355 sc->sc_state &= ~MMS_ASLP; 356 wakeup((caddr_t)sc); 357 } 358 selwakeup(&sc->sc_rsel); 359 if (sc->sc_async) { 360 psignal(sc->sc_io, SIGIO); 361 } 362 363 } 364 365 return -1; 366 } 367 368 int 369 mmsselect(dev, rw, p) 370 dev_t dev; 371 int rw; 372 struct proc *p; 373 { 374 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)]; 375 int s; 376 int ret; 377 378 if (rw == FWRITE) 379 return 0; 380 381 s = spltty(); 382 if (!sc->sc_q.c_cc) { 383 selrecord(p, &sc->sc_rsel); 384 ret = 0; 385 } else 386 ret = 1; 387 splx(s); 388 389 return ret; 390 } 391