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,
102 .d_flags = D_NEEDGIANT,
111 static void mseintr(void *);
112 static timeout_t msetimeout;
114 #define MSE_UNIT(dev) (minor(dev) >> 1)
115 #define MSE_NBLOCKIO(dev) (minor(dev) & 0x1)
117 #define MSEPRI (PZERO + 3)
120 mse_common_attach(device_t dev)
123 int unit, flags, rid;
125 sc = device_get_softc(dev);
126 unit = device_get_unit(dev);
129 sc->sc_intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
131 if (sc->sc_intr == NULL) {
132 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
136 if (bus_setup_intr(dev, sc->sc_intr,
137 INTR_TYPE_TTY, NULL, mseintr, sc, &sc->sc_ih)) {
138 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
139 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
142 flags = device_get_flags(dev);
143 sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
144 callout_handle_init(&sc->sc_callout);
146 sc->sc_dev = make_dev(&mse_cdevsw, unit << 1, 0, 0, 0600,
148 sc->sc_ndev = make_dev(&mse_cdevsw, (unit<<1)+1, 0, 0, 0600,
154 * Exclusive open the mouse, initialize it and enable interrupts.
157 mseopen(struct cdev *dev, int flags, int fmt, struct thread *td)
162 sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
165 if (sc->sc_mousetype == MSE_NONE)
167 if (sc->sc_flags & MSESC_OPEN)
169 sc->sc_flags |= MSESC_OPEN;
170 sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
171 sc->sc_deltax = sc->sc_deltay = 0;
172 sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
173 sc->sc_watchdog = FALSE;
174 sc->sc_callout = timeout(msetimeout, dev, hz*2);
176 sc->status.flags = 0;
177 sc->status.button = sc->status.obutton = 0;
178 sc->status.dx = sc->status.dy = sc->status.dz = 0;
181 * Initialize mouse interface and enable interrupts.
184 (*sc->sc_enablemouse)(sc->sc_iot, sc->sc_ioh);
190 * mseclose: just turn off mouse innterrupts.
193 mseclose(struct cdev *dev, int flags, int fmt, struct thread *td)
195 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
198 untimeout(msetimeout, dev, sc->sc_callout);
199 callout_handle_init(&sc->sc_callout);
201 (*sc->sc_disablemouse)(sc->sc_iot, sc->sc_ioh);
202 sc->sc_flags &= ~MSESC_OPEN;
208 * mseread: return mouse info using the MSC serial protocol, but without
209 * using bytes 4 and 5.
210 * (Yes this is cheesy, but it makes the X386 server happy, so...)
213 mseread(struct cdev *dev, struct uio *uio, int ioflag)
215 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
219 * If there are no protocol bytes to be read, set up a new protocol
222 s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
223 if (sc->sc_bytesread >= sc->mode.packetsize) {
224 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
225 (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
226 if (MSE_NBLOCKIO(dev)) {
230 sc->sc_flags |= MSESC_WANT;
231 error = tsleep(sc, MSEPRI | PCATCH,
240 * Generate protocol bytes.
241 * For some reason X386 expects 5 bytes but never uses
242 * the fourth or fifth?
244 sc->sc_bytes[0] = sc->mode.syncmask[1]
245 | (sc->sc_buttons & ~sc->mode.syncmask[0]);
246 if (sc->sc_deltax > 127)
248 if (sc->sc_deltax < -127)
249 sc->sc_deltax = -127;
250 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
251 if (sc->sc_deltay > 127)
253 if (sc->sc_deltay < -127)
254 sc->sc_deltay = -127;
255 sc->sc_bytes[1] = sc->sc_deltax;
256 sc->sc_bytes[2] = sc->sc_deltay;
257 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
258 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
259 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
260 sc->sc_obuttons = sc->sc_buttons;
261 sc->sc_deltax = sc->sc_deltay = 0;
262 sc->sc_bytesread = 0;
265 xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
266 error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
269 sc->sc_bytesread += xfer;
274 * mseioctl: process ioctl commands.
277 mseioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
279 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
280 mousestatus_t status;
286 case MOUSE_GETHWINFO:
288 *(mousehw_t *)addr = sc->hw;
289 if (sc->mode.level == 0)
290 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
296 *(mousemode_t *)addr = sc->mode;
297 switch (sc->mode.level) {
301 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
302 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
303 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
310 switch (((mousemode_t *)addr)->level) {
317 if (((mousemode_t *)addr)->accelfactor < -1)
319 else if (((mousemode_t *)addr)->accelfactor >= 0)
320 sc->mode.accelfactor =
321 ((mousemode_t *)addr)->accelfactor;
322 sc->mode.level = ((mousemode_t *)addr)->level;
323 switch (sc->mode.level) {
325 sc->sc_bytesread = sc->mode.packetsize
326 = MOUSE_MSC_PACKETSIZE;
329 sc->sc_bytesread = sc->mode.packetsize
330 = MOUSE_SYS_PACKETSIZE;
336 *(int *)addr = sc->mode.level;
340 switch (*(int *)addr) {
342 sc->mode.level = *(int *)addr;
343 sc->sc_bytesread = sc->mode.packetsize
344 = MOUSE_MSC_PACKETSIZE;
347 sc->mode.level = *(int *)addr;
348 sc->sc_bytesread = sc->mode.packetsize
349 = MOUSE_SYS_PACKETSIZE;
356 case MOUSE_GETSTATUS:
359 sc->status.flags = 0;
360 sc->status.obutton = sc->status.button;
361 sc->status.button = 0;
366 *(mousestatus_t *)addr = status;
369 case MOUSE_READSTATE:
373 #if (defined(MOUSE_GETVARS))
386 * msepoll: check for mouse input to be processed.
389 msepoll(struct cdev *dev, int events, struct thread *td)
391 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
396 if (events & (POLLIN | POLLRDNORM)) {
397 if (sc->sc_bytesread != sc->mode.packetsize ||
398 sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
399 (sc->sc_obuttons ^ sc->sc_buttons) != 0)
400 revents |= events & (POLLIN | POLLRDNORM);
403 * Since this is an exclusive open device, any previous
404 * proc pointer is trash now, so we can just assign it.
406 selrecord(td, &sc->sc_selp);
414 * msetimeout: watchdog timer routine.
417 msetimeout(void *arg)
422 dev = (struct cdev *)arg;
423 sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
424 if (sc->sc_watchdog) {
426 printf("mse%d: lost interrupt?\n", MSE_UNIT(dev));
429 sc->sc_watchdog = TRUE;
430 sc->sc_callout = timeout(msetimeout, dev, hz);
434 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
440 * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
441 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
443 static int butmap[8] = {
447 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
449 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
450 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
451 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
453 mse_softc_t *sc = arg;
458 static int mse_intrcnt = 0;
459 if((mse_intrcnt++ % 10000) == 0)
462 if ((sc->sc_flags & MSESC_OPEN) == 0)
465 (*sc->sc_getmouse)(sc->sc_iot, sc->sc_ioh, &dx, &dy, &but);
466 if (sc->mode.accelfactor > 0) {
468 dx = dx * dx / sc->mode.accelfactor;
474 dy = dy * dy / sc->mode.accelfactor;
482 sc->sc_buttons = but;
484 but = butmap[~but & MOUSE_MSC_BUTTONS];
487 sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
488 | (sc->status.button ^ but);
489 sc->status.button = but;
491 sc->sc_watchdog = FALSE;
494 * If mouse state has changed, wake up anyone wanting to know.
496 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
497 (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
498 if (sc->sc_flags & MSESC_WANT) {
499 sc->sc_flags &= ~MSESC_WANT;
502 selwakeuppri(&sc->sc_selp, MSEPRI);