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;
160 mse_detach(device_t dev)
165 sc = device_get_softc(dev);
167 if (sc->sc_flags & MSESC_OPEN) {
172 /* Sabotage subsequent opens. */
173 sc->sc_mousetype = MSE_NONE;
176 destroy_dev(sc->sc_dev);
177 destroy_dev(sc->sc_ndev);
180 bus_teardown_intr(dev, sc->sc_intr, sc->sc_ih);
181 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
182 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
184 callout_drain(&sc->sc_callout);
185 mtx_destroy(&sc->sc_lock);
191 * Exclusive open the mouse, initialize it and enable interrupts.
194 mseopen(struct cdev *dev, int flags, int fmt, struct thread *td)
196 mse_softc_t *sc = dev->si_drv1;
199 if (sc->sc_mousetype == MSE_NONE) {
203 if (sc->sc_flags & MSESC_OPEN) {
207 sc->sc_flags |= MSESC_OPEN;
208 sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
209 sc->sc_deltax = sc->sc_deltay = 0;
210 sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
211 sc->sc_watchdog = FALSE;
212 callout_reset(&sc->sc_callout, hz * 2, msetimeout, dev);
214 sc->status.flags = 0;
215 sc->status.button = sc->status.obutton = 0;
216 sc->status.dx = sc->status.dy = sc->status.dz = 0;
219 * Initialize mouse interface and enable interrupts.
221 (*sc->sc_enablemouse)(sc->sc_port);
227 * mseclose: just turn off mouse innterrupts.
230 mseclose(struct cdev *dev, int flags, int fmt, struct thread *td)
232 mse_softc_t *sc = dev->si_drv1;
235 callout_stop(&sc->sc_callout);
236 (*sc->sc_disablemouse)(sc->sc_port);
237 sc->sc_flags &= ~MSESC_OPEN;
243 * mseread: return mouse info using the MSC serial protocol, but without
244 * using bytes 4 and 5.
245 * (Yes this is cheesy, but it makes the X386 server happy, so...)
248 mseread(struct cdev *dev, struct uio *uio, int ioflag)
250 mse_softc_t *sc = dev->si_drv1;
254 * If there are no protocol bytes to be read, set up a new protocol
258 while (sc->sc_flags & MSESC_READING) {
259 if (MSE_NBLOCKIO(dev)) {
263 sc->sc_flags |= MSESC_WANT;
264 error = mtx_sleep(sc, &sc->sc_lock, MSEPRI | PCATCH, "mseread",
271 sc->sc_flags |= MSESC_READING;
273 if (sc->sc_bytesread >= sc->mode.packetsize) {
274 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
275 (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
276 if (MSE_NBLOCKIO(dev))
278 sc->sc_flags |= MSESC_WANT;
279 error = mtx_sleep(sc, &sc->sc_lock, MSEPRI | PCATCH,
286 * Generate protocol bytes.
287 * For some reason X386 expects 5 bytes but never uses
288 * the fourth or fifth?
290 sc->sc_bytes[0] = sc->mode.syncmask[1]
291 | (sc->sc_buttons & ~sc->mode.syncmask[0]);
292 if (sc->sc_deltax > 127)
294 if (sc->sc_deltax < -127)
295 sc->sc_deltax = -127;
296 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
297 if (sc->sc_deltay > 127)
299 if (sc->sc_deltay < -127)
300 sc->sc_deltay = -127;
301 sc->sc_bytes[1] = sc->sc_deltax;
302 sc->sc_bytes[2] = sc->sc_deltay;
303 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
304 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
305 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
306 sc->sc_obuttons = sc->sc_buttons;
307 sc->sc_deltax = sc->sc_deltay = 0;
308 sc->sc_bytesread = 0;
310 xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
312 error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
315 sc->sc_flags &= ~MSESC_READING;
317 sc->sc_bytesread += xfer;
318 if (sc->sc_flags & MSESC_WANT) {
319 sc->sc_flags &= ~MSESC_WANT;
328 * mseioctl: process ioctl commands.
331 mseioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
333 mse_softc_t *sc = dev->si_drv1;
334 mousestatus_t status;
339 case MOUSE_GETHWINFO:
341 *(mousehw_t *)addr = sc->hw;
342 if (sc->mode.level == 0)
343 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
349 *(mousemode_t *)addr = sc->mode;
350 switch (sc->mode.level) {
354 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
355 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
356 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
363 switch (((mousemode_t *)addr)->level) {
371 if (((mousemode_t *)addr)->accelfactor < -1) {
374 } else if (((mousemode_t *)addr)->accelfactor >= 0)
375 sc->mode.accelfactor =
376 ((mousemode_t *)addr)->accelfactor;
377 sc->mode.level = ((mousemode_t *)addr)->level;
378 switch (sc->mode.level) {
380 sc->sc_bytesread = sc->mode.packetsize
381 = MOUSE_MSC_PACKETSIZE;
384 sc->sc_bytesread = sc->mode.packetsize
385 = MOUSE_SYS_PACKETSIZE;
393 *(int *)addr = sc->mode.level;
398 switch (*(int *)addr) {
401 sc->mode.level = *(int *)addr;
402 sc->sc_bytesread = sc->mode.packetsize
403 = MOUSE_MSC_PACKETSIZE;
408 sc->mode.level = *(int *)addr;
409 sc->sc_bytesread = sc->mode.packetsize
410 = MOUSE_SYS_PACKETSIZE;
418 case MOUSE_GETSTATUS:
421 sc->status.flags = 0;
422 sc->status.obutton = sc->status.button;
423 sc->status.button = 0;
428 *(mousestatus_t *)addr = status;
431 case MOUSE_READSTATE:
435 #if (defined(MOUSE_GETVARS))
448 * msepoll: check for mouse input to be processed.
451 msepoll(struct cdev *dev, int events, struct thread *td)
453 mse_softc_t *sc = dev->si_drv1;
457 if (events & (POLLIN | POLLRDNORM)) {
458 if (sc->sc_bytesread != sc->mode.packetsize ||
459 sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
460 (sc->sc_obuttons ^ sc->sc_buttons) != 0)
461 revents |= events & (POLLIN | POLLRDNORM);
463 selrecord(td, &sc->sc_selp);
470 * msetimeout: watchdog timer routine.
473 msetimeout(void *arg)
478 dev = (struct cdev *)arg;
480 MSE_ASSERT_LOCKED(sc);
481 if (sc->sc_watchdog) {
483 printf("%s: lost interrupt?\n", devtoname(dev));
486 sc->sc_watchdog = TRUE;
487 callout_schedule(&sc->sc_callout, hz);
491 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
496 mse_softc_t *sc = arg;
504 mseintr_locked(mse_softc_t *sc)
507 * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
508 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
510 static int butmap[8] = {
514 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
516 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
517 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
518 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
524 static int mse_intrcnt = 0;
525 if((mse_intrcnt++ % 10000) == 0)
528 if ((sc->sc_flags & MSESC_OPEN) == 0)
531 (*sc->sc_getmouse)(sc->sc_port, &dx, &dy, &but);
532 if (sc->mode.accelfactor > 0) {
534 dx = dx * dx / sc->mode.accelfactor;
540 dy = dy * dy / sc->mode.accelfactor;
548 sc->sc_buttons = but;
550 but = butmap[~but & MOUSE_MSC_BUTTONS];
553 sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
554 | (sc->status.button ^ but);
555 sc->status.button = but;
557 sc->sc_watchdog = FALSE;
560 * If mouse state has changed, wake up anyone wanting to know.
562 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
563 (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
564 if (sc->sc_flags & MSESC_WANT) {
565 sc->sc_flags &= ~MSESC_WANT;
568 selwakeuppri(&sc->sc_selp, MSEPRI);