2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 M. Warner Losh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright 1992 by the University of Guelph
34 * Permission to use, copy and modify this
35 * software and its documentation for any purpose and without
36 * fee is hereby granted, provided that the above copyright
37 * notice appear in all copies and that both that copyright
38 * notice and this permission notice appear in supporting
40 * University of Guelph makes no representations about the suitability of
41 * this software for any purpose. It is provided "as is"
42 * without express or implied warranty.
45 * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
46 * the X386 port, courtesy of
47 * Rick Macklem, rick@snowhite.cis.uoguelph.ca
48 * Caveats: The driver currently uses spltty(), but doesn't use any
49 * generic tty code. It could use splmse() (that only masks off the
50 * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
51 * (This may be worth the effort, since the Logitech generates 30/60
52 * interrupts/sec continuously while it is open.)
53 * NB: The ATI has NOT been tested yet!
57 * Modification history:
58 * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
59 * improved probe based on input from Logitech.
61 * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
62 * fixes to make it work with Microsoft InPort busmouse
64 * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
65 * added patches for new "select" interface
67 * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
68 * changed position of some spl()'s in mseread
70 * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
71 * limit maximum negative x/y value to -127 to work around XFree problem
72 * that causes spurious button pushes.
75 #include <sys/param.h>
76 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/module.h>
82 #include <sys/selinfo.h>
84 #include <sys/mouse.h>
86 #include <machine/bus.h>
87 #include <machine/resource.h>
90 #include <isa/isavar.h>
92 #include <dev/mse/msevar.h>
94 devclass_t mse_devclass;
96 static d_open_t mseopen;
97 static d_close_t mseclose;
98 static d_read_t mseread;
99 static d_ioctl_t mseioctl;
100 static d_poll_t msepoll;
102 static struct cdevsw mse_cdevsw = {
103 .d_version = D_VERSION,
112 static void mseintr(void *);
113 static void mseintr_locked(mse_softc_t *sc);
114 static void msetimeout(void *);
116 #define MSE_NBLOCKIO(dev) (dev2unit(dev) != 0)
118 #define MSEPRI (PZERO + 3)
121 mse_common_attach(device_t dev)
124 int unit, flags, rid;
126 sc = device_get_softc(dev);
127 unit = device_get_unit(dev);
128 mtx_init(&sc->sc_lock, "mse", NULL, MTX_DEF);
129 callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
132 sc->sc_intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
134 if (sc->sc_intr == NULL) {
135 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
136 mtx_destroy(&sc->sc_lock);
140 if (bus_setup_intr(dev, sc->sc_intr, INTR_TYPE_TTY | INTR_MPSAFE,
141 NULL, mseintr, sc, &sc->sc_ih)) {
142 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
143 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
144 mtx_destroy(&sc->sc_lock);
147 flags = device_get_flags(dev);
148 sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
150 sc->sc_dev = make_dev(&mse_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
152 sc->sc_dev->si_drv1 = sc;
153 sc->sc_ndev = make_dev(&mse_cdevsw, 1, UID_ROOT, GID_WHEEL, 0600,
155 sc->sc_ndev->si_drv1 = sc;
156 gone_in_dev(dev, 12, "mse(4) driver");
162 mse_detach(device_t dev)
167 sc = device_get_softc(dev);
169 if (sc->sc_flags & MSESC_OPEN) {
174 /* Sabotage subsequent opens. */
175 sc->sc_mousetype = MSE_NONE;
178 destroy_dev(sc->sc_dev);
179 destroy_dev(sc->sc_ndev);
182 bus_teardown_intr(dev, sc->sc_intr, sc->sc_ih);
183 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
184 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
186 callout_drain(&sc->sc_callout);
187 mtx_destroy(&sc->sc_lock);
193 * Exclusive open the mouse, initialize it and enable interrupts.
196 mseopen(struct cdev *dev, int flags, int fmt, struct thread *td)
198 mse_softc_t *sc = dev->si_drv1;
201 if (sc->sc_mousetype == MSE_NONE) {
205 if (sc->sc_flags & MSESC_OPEN) {
209 sc->sc_flags |= MSESC_OPEN;
210 sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
211 sc->sc_deltax = sc->sc_deltay = 0;
212 sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
213 sc->sc_watchdog = FALSE;
214 callout_reset(&sc->sc_callout, hz * 2, msetimeout, dev);
216 sc->status.flags = 0;
217 sc->status.button = sc->status.obutton = 0;
218 sc->status.dx = sc->status.dy = sc->status.dz = 0;
221 * Initialize mouse interface and enable interrupts.
223 (*sc->sc_enablemouse)(sc->sc_port);
229 * mseclose: just turn off mouse innterrupts.
232 mseclose(struct cdev *dev, int flags, int fmt, struct thread *td)
234 mse_softc_t *sc = dev->si_drv1;
237 callout_stop(&sc->sc_callout);
238 (*sc->sc_disablemouse)(sc->sc_port);
239 sc->sc_flags &= ~MSESC_OPEN;
245 * mseread: return mouse info using the MSC serial protocol, but without
246 * using bytes 4 and 5.
247 * (Yes this is cheesy, but it makes the X386 server happy, so...)
250 mseread(struct cdev *dev, struct uio *uio, int ioflag)
252 mse_softc_t *sc = dev->si_drv1;
256 * If there are no protocol bytes to be read, set up a new protocol
260 while (sc->sc_flags & MSESC_READING) {
261 if (MSE_NBLOCKIO(dev)) {
265 sc->sc_flags |= MSESC_WANT;
266 error = mtx_sleep(sc, &sc->sc_lock, MSEPRI | PCATCH, "mseread",
273 sc->sc_flags |= MSESC_READING;
275 if (sc->sc_bytesread >= sc->mode.packetsize) {
276 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
277 (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
278 if (MSE_NBLOCKIO(dev))
280 sc->sc_flags |= MSESC_WANT;
281 error = mtx_sleep(sc, &sc->sc_lock, MSEPRI | PCATCH,
288 * Generate protocol bytes.
289 * For some reason X386 expects 5 bytes but never uses
290 * the fourth or fifth?
292 sc->sc_bytes[0] = sc->mode.syncmask[1]
293 | (sc->sc_buttons & ~sc->mode.syncmask[0]);
294 if (sc->sc_deltax > 127)
296 if (sc->sc_deltax < -127)
297 sc->sc_deltax = -127;
298 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
299 if (sc->sc_deltay > 127)
301 if (sc->sc_deltay < -127)
302 sc->sc_deltay = -127;
303 sc->sc_bytes[1] = sc->sc_deltax;
304 sc->sc_bytes[2] = sc->sc_deltay;
305 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
306 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
307 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
308 sc->sc_obuttons = sc->sc_buttons;
309 sc->sc_deltax = sc->sc_deltay = 0;
310 sc->sc_bytesread = 0;
312 xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
314 error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
317 sc->sc_flags &= ~MSESC_READING;
319 sc->sc_bytesread += xfer;
320 if (sc->sc_flags & MSESC_WANT) {
321 sc->sc_flags &= ~MSESC_WANT;
330 * mseioctl: process ioctl commands.
333 mseioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
335 mse_softc_t *sc = dev->si_drv1;
336 mousestatus_t status;
341 case MOUSE_GETHWINFO:
343 *(mousehw_t *)addr = sc->hw;
344 if (sc->mode.level == 0)
345 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
351 *(mousemode_t *)addr = sc->mode;
352 switch (sc->mode.level) {
356 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
357 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
358 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
365 switch (((mousemode_t *)addr)->level) {
373 if (((mousemode_t *)addr)->accelfactor < -1) {
376 } else if (((mousemode_t *)addr)->accelfactor >= 0)
377 sc->mode.accelfactor =
378 ((mousemode_t *)addr)->accelfactor;
379 sc->mode.level = ((mousemode_t *)addr)->level;
380 switch (sc->mode.level) {
382 sc->sc_bytesread = sc->mode.packetsize
383 = MOUSE_MSC_PACKETSIZE;
386 sc->sc_bytesread = sc->mode.packetsize
387 = MOUSE_SYS_PACKETSIZE;
395 *(int *)addr = sc->mode.level;
400 switch (*(int *)addr) {
403 sc->mode.level = *(int *)addr;
404 sc->sc_bytesread = sc->mode.packetsize
405 = MOUSE_MSC_PACKETSIZE;
410 sc->mode.level = *(int *)addr;
411 sc->sc_bytesread = sc->mode.packetsize
412 = MOUSE_SYS_PACKETSIZE;
420 case MOUSE_GETSTATUS:
423 sc->status.flags = 0;
424 sc->status.obutton = sc->status.button;
425 sc->status.button = 0;
430 *(mousestatus_t *)addr = status;
433 case MOUSE_READSTATE:
444 * msepoll: check for mouse input to be processed.
447 msepoll(struct cdev *dev, int events, struct thread *td)
449 mse_softc_t *sc = dev->si_drv1;
453 if (events & (POLLIN | POLLRDNORM)) {
454 if (sc->sc_bytesread != sc->mode.packetsize ||
455 sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
456 (sc->sc_obuttons ^ sc->sc_buttons) != 0)
457 revents |= events & (POLLIN | POLLRDNORM);
459 selrecord(td, &sc->sc_selp);
466 * msetimeout: watchdog timer routine.
469 msetimeout(void *arg)
474 dev = (struct cdev *)arg;
476 MSE_ASSERT_LOCKED(sc);
477 if (sc->sc_watchdog) {
479 printf("%s: lost interrupt?\n", devtoname(dev));
482 sc->sc_watchdog = TRUE;
483 callout_schedule(&sc->sc_callout, hz);
487 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
492 mse_softc_t *sc = arg;
500 mseintr_locked(mse_softc_t *sc)
503 * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
504 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
506 static int butmap[8] = {
510 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
512 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
513 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
514 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
520 static int mse_intrcnt = 0;
521 if((mse_intrcnt++ % 10000) == 0)
524 if ((sc->sc_flags & MSESC_OPEN) == 0)
527 (*sc->sc_getmouse)(sc->sc_port, &dx, &dy, &but);
528 if (sc->mode.accelfactor > 0) {
530 dx = dx * dx / sc->mode.accelfactor;
536 dy = dy * dy / sc->mode.accelfactor;
544 sc->sc_buttons = but;
546 but = butmap[~but & MOUSE_MSC_BUTTONS];
549 sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
550 | (sc->status.button ^ but);
551 sc->status.button = but;
553 sc->sc_watchdog = FALSE;
556 * If mouse state has changed, wake up anyone wanting to know.
558 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
559 (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
560 if (sc->sc_flags & MSESC_WANT) {
561 sc->sc_flags &= ~MSESC_WANT;
564 selwakeuppri(&sc->sc_selp, MSEPRI);