2 * Copyright (c) 2004 M. Warner Losh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 * Copyright 1992 by the University of Guelph
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
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.
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!
55 * Modification history:
56 * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
57 * improved probe based on input from Logitech.
59 * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
60 * fixes to make it work with Microsoft InPort busmouse
62 * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
63 * added patches for new "select" interface
65 * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
66 * changed position of some spl()'s in mseread
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.
73 #include <sys/param.h>
74 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/module.h>
80 #include <sys/selinfo.h>
82 #include <sys/mouse.h>
84 #include <machine/bus.h>
85 #include <machine/resource.h>
88 #include <isa/isavar.h>
90 #include <dev/mse/msevar.h>
92 devclass_t mse_devclass;
94 static d_open_t mseopen;
95 static d_close_t mseclose;
96 static d_read_t mseread;
97 static d_ioctl_t mseioctl;
98 static d_poll_t msepoll;
100 static struct cdevsw mse_cdevsw = {
101 .d_version = D_VERSION,
110 static void mseintr(void *);
111 static void mseintr_locked(mse_softc_t *sc);
112 static void msetimeout(void *);
114 #define MSE_NBLOCKIO(dev) (dev2unit(dev) != 0)
116 #define MSEPRI (PZERO + 3)
119 mse_common_attach(device_t dev)
122 int unit, flags, rid;
124 sc = device_get_softc(dev);
125 unit = device_get_unit(dev);
126 mtx_init(&sc->sc_lock, "mse", NULL, MTX_DEF);
127 callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
130 sc->sc_intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
132 if (sc->sc_intr == NULL) {
133 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
134 mtx_destroy(&sc->sc_lock);
138 if (bus_setup_intr(dev, sc->sc_intr, INTR_TYPE_TTY | INTR_MPSAFE,
139 NULL, mseintr, sc, &sc->sc_ih)) {
140 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
141 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
142 mtx_destroy(&sc->sc_lock);
145 flags = device_get_flags(dev);
146 sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
148 sc->sc_dev = make_dev(&mse_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
150 sc->sc_dev->si_drv1 = sc;
151 sc->sc_ndev = make_dev(&mse_cdevsw, 1, UID_ROOT, GID_WHEEL, 0600,
153 sc->sc_ndev->si_drv1 = sc;
158 mse_detach(device_t dev)
163 sc = device_get_softc(dev);
165 if (sc->sc_flags & MSESC_OPEN) {
170 /* Sabotage subsequent opens. */
171 sc->sc_mousetype = MSE_NONE;
174 destroy_dev(sc->sc_dev);
175 destroy_dev(sc->sc_ndev);
178 bus_teardown_intr(dev, sc->sc_intr, sc->sc_ih);
179 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
180 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
182 callout_drain(&sc->sc_callout);
183 mtx_destroy(&sc->sc_lock);
189 * Exclusive open the mouse, initialize it and enable interrupts.
192 mseopen(struct cdev *dev, int flags, int fmt, struct thread *td)
194 mse_softc_t *sc = dev->si_drv1;
197 if (sc->sc_mousetype == MSE_NONE) {
201 if (sc->sc_flags & MSESC_OPEN) {
205 sc->sc_flags |= MSESC_OPEN;
206 sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
207 sc->sc_deltax = sc->sc_deltay = 0;
208 sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
209 sc->sc_watchdog = FALSE;
210 callout_reset(&sc->sc_callout, hz * 2, msetimeout, dev);
212 sc->status.flags = 0;
213 sc->status.button = sc->status.obutton = 0;
214 sc->status.dx = sc->status.dy = sc->status.dz = 0;
217 * Initialize mouse interface and enable interrupts.
219 (*sc->sc_enablemouse)(sc->sc_port);
225 * mseclose: just turn off mouse innterrupts.
228 mseclose(struct cdev *dev, int flags, int fmt, struct thread *td)
230 mse_softc_t *sc = dev->si_drv1;
233 callout_stop(&sc->sc_callout);
234 (*sc->sc_disablemouse)(sc->sc_port);
235 sc->sc_flags &= ~MSESC_OPEN;
241 * mseread: return mouse info using the MSC serial protocol, but without
242 * using bytes 4 and 5.
243 * (Yes this is cheesy, but it makes the X386 server happy, so...)
246 mseread(struct cdev *dev, struct uio *uio, int ioflag)
248 mse_softc_t *sc = dev->si_drv1;
252 * If there are no protocol bytes to be read, set up a new protocol
256 while (sc->sc_flags & MSESC_READING) {
257 if (MSE_NBLOCKIO(dev)) {
261 sc->sc_flags |= MSESC_WANT;
262 error = mtx_sleep(sc, &sc->sc_lock, MSEPRI | PCATCH, "mseread",
269 sc->sc_flags |= MSESC_READING;
271 if (sc->sc_bytesread >= sc->mode.packetsize) {
272 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
273 (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
274 if (MSE_NBLOCKIO(dev))
276 sc->sc_flags |= MSESC_WANT;
277 error = mtx_sleep(sc, &sc->sc_lock, MSEPRI | PCATCH,
284 * Generate protocol bytes.
285 * For some reason X386 expects 5 bytes but never uses
286 * the fourth or fifth?
288 sc->sc_bytes[0] = sc->mode.syncmask[1]
289 | (sc->sc_buttons & ~sc->mode.syncmask[0]);
290 if (sc->sc_deltax > 127)
292 if (sc->sc_deltax < -127)
293 sc->sc_deltax = -127;
294 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
295 if (sc->sc_deltay > 127)
297 if (sc->sc_deltay < -127)
298 sc->sc_deltay = -127;
299 sc->sc_bytes[1] = sc->sc_deltax;
300 sc->sc_bytes[2] = sc->sc_deltay;
301 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
302 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
303 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
304 sc->sc_obuttons = sc->sc_buttons;
305 sc->sc_deltax = sc->sc_deltay = 0;
306 sc->sc_bytesread = 0;
308 xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
310 error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
313 sc->sc_flags &= ~MSESC_READING;
315 sc->sc_bytesread += xfer;
316 if (sc->sc_flags & MSESC_WANT) {
317 sc->sc_flags &= ~MSESC_WANT;
326 * mseioctl: process ioctl commands.
329 mseioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
331 mse_softc_t *sc = dev->si_drv1;
332 mousestatus_t status;
337 case MOUSE_GETHWINFO:
339 *(mousehw_t *)addr = sc->hw;
340 if (sc->mode.level == 0)
341 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
347 *(mousemode_t *)addr = sc->mode;
348 switch (sc->mode.level) {
352 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
353 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
354 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
361 switch (((mousemode_t *)addr)->level) {
369 if (((mousemode_t *)addr)->accelfactor < -1) {
372 } else if (((mousemode_t *)addr)->accelfactor >= 0)
373 sc->mode.accelfactor =
374 ((mousemode_t *)addr)->accelfactor;
375 sc->mode.level = ((mousemode_t *)addr)->level;
376 switch (sc->mode.level) {
378 sc->sc_bytesread = sc->mode.packetsize
379 = MOUSE_MSC_PACKETSIZE;
382 sc->sc_bytesread = sc->mode.packetsize
383 = MOUSE_SYS_PACKETSIZE;
391 *(int *)addr = sc->mode.level;
396 switch (*(int *)addr) {
399 sc->mode.level = *(int *)addr;
400 sc->sc_bytesread = sc->mode.packetsize
401 = MOUSE_MSC_PACKETSIZE;
406 sc->mode.level = *(int *)addr;
407 sc->sc_bytesread = sc->mode.packetsize
408 = MOUSE_SYS_PACKETSIZE;
416 case MOUSE_GETSTATUS:
419 sc->status.flags = 0;
420 sc->status.obutton = sc->status.button;
421 sc->status.button = 0;
426 *(mousestatus_t *)addr = status;
429 case MOUSE_READSTATE:
433 #if (defined(MOUSE_GETVARS))
446 * msepoll: check for mouse input to be processed.
449 msepoll(struct cdev *dev, int events, struct thread *td)
451 mse_softc_t *sc = dev->si_drv1;
455 if (events & (POLLIN | POLLRDNORM)) {
456 if (sc->sc_bytesread != sc->mode.packetsize ||
457 sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
458 (sc->sc_obuttons ^ sc->sc_buttons) != 0)
459 revents |= events & (POLLIN | POLLRDNORM);
461 selrecord(td, &sc->sc_selp);
468 * msetimeout: watchdog timer routine.
471 msetimeout(void *arg)
476 dev = (struct cdev *)arg;
478 MSE_ASSERT_LOCKED(sc);
479 if (sc->sc_watchdog) {
481 printf("%s: lost interrupt?\n", devtoname(dev));
484 sc->sc_watchdog = TRUE;
485 callout_schedule(&sc->sc_callout, hz);
489 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
494 mse_softc_t *sc = arg;
502 mseintr_locked(mse_softc_t *sc)
505 * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
506 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
508 static int butmap[8] = {
512 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
514 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
515 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
516 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
522 static int mse_intrcnt = 0;
523 if((mse_intrcnt++ % 10000) == 0)
526 if ((sc->sc_flags & MSESC_OPEN) == 0)
529 (*sc->sc_getmouse)(sc->sc_port, &dx, &dy, &but);
530 if (sc->mode.accelfactor > 0) {
532 dx = dx * dx / sc->mode.accelfactor;
538 dy = dy * dy / sc->mode.accelfactor;
546 sc->sc_buttons = but;
548 but = butmap[~but & MOUSE_MSC_BUTTONS];
551 sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
552 | (sc->status.button ^ but);
553 sc->status.button = but;
555 sc->sc_watchdog = FALSE;
558 * If mouse state has changed, wake up anyone wanting to know.
560 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
561 (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
562 if (sc->sc_flags & MSESC_WANT) {
563 sc->sc_flags &= ~MSESC_WANT;
566 selwakeuppri(&sc->sc_selp, MSEPRI);