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_NBLOCKIO(dev) dev2unit(dev)
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);
128 sc->sc_intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
130 if (sc->sc_intr == NULL) {
131 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
135 if (bus_setup_intr(dev, sc->sc_intr,
136 INTR_TYPE_TTY, NULL, mseintr, sc, &sc->sc_ih)) {
137 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
138 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
141 flags = device_get_flags(dev);
142 sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
143 callout_handle_init(&sc->sc_callout);
145 sc->sc_dev = make_dev(&mse_cdevsw, 0, 0, 0, 0600, "mse%d", unit);
146 sc->sc_dev->si_drv1 = sc;
147 sc->sc_ndev = make_dev(&mse_cdevsw, 1, 0, 0, 0600, "nmse%d", unit);
148 sc->sc_ndev->si_drv1 = sc;
153 * Exclusive open the mouse, initialize it and enable interrupts.
156 mseopen(struct cdev *dev, int flags, int fmt, struct thread *td)
158 mse_softc_t *sc = dev->si_drv1;
161 if (sc->sc_mousetype == MSE_NONE)
163 if (sc->sc_flags & MSESC_OPEN)
165 sc->sc_flags |= MSESC_OPEN;
166 sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
167 sc->sc_deltax = sc->sc_deltay = 0;
168 sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
169 sc->sc_watchdog = FALSE;
170 sc->sc_callout = timeout(msetimeout, dev, hz*2);
172 sc->status.flags = 0;
173 sc->status.button = sc->status.obutton = 0;
174 sc->status.dx = sc->status.dy = sc->status.dz = 0;
177 * Initialize mouse interface and enable interrupts.
180 (*sc->sc_enablemouse)(sc->sc_iot, sc->sc_ioh);
186 * mseclose: just turn off mouse innterrupts.
189 mseclose(struct cdev *dev, int flags, int fmt, struct thread *td)
191 mse_softc_t *sc = dev->si_drv1;
194 untimeout(msetimeout, dev, sc->sc_callout);
195 callout_handle_init(&sc->sc_callout);
197 (*sc->sc_disablemouse)(sc->sc_iot, sc->sc_ioh);
198 sc->sc_flags &= ~MSESC_OPEN;
204 * mseread: return mouse info using the MSC serial protocol, but without
205 * using bytes 4 and 5.
206 * (Yes this is cheesy, but it makes the X386 server happy, so...)
209 mseread(struct cdev *dev, struct uio *uio, int ioflag)
211 mse_softc_t *sc = dev->si_drv1;
215 * If there are no protocol bytes to be read, set up a new protocol
218 s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
219 if (sc->sc_bytesread >= sc->mode.packetsize) {
220 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
221 (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
222 if (MSE_NBLOCKIO(dev)) {
226 sc->sc_flags |= MSESC_WANT;
227 error = tsleep(sc, MSEPRI | PCATCH,
236 * Generate protocol bytes.
237 * For some reason X386 expects 5 bytes but never uses
238 * the fourth or fifth?
240 sc->sc_bytes[0] = sc->mode.syncmask[1]
241 | (sc->sc_buttons & ~sc->mode.syncmask[0]);
242 if (sc->sc_deltax > 127)
244 if (sc->sc_deltax < -127)
245 sc->sc_deltax = -127;
246 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
247 if (sc->sc_deltay > 127)
249 if (sc->sc_deltay < -127)
250 sc->sc_deltay = -127;
251 sc->sc_bytes[1] = sc->sc_deltax;
252 sc->sc_bytes[2] = sc->sc_deltay;
253 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
254 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
255 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
256 sc->sc_obuttons = sc->sc_buttons;
257 sc->sc_deltax = sc->sc_deltay = 0;
258 sc->sc_bytesread = 0;
261 xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
262 error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
265 sc->sc_bytesread += xfer;
270 * mseioctl: process ioctl commands.
273 mseioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
275 mse_softc_t *sc = dev->si_drv1;
276 mousestatus_t status;
282 case MOUSE_GETHWINFO:
284 *(mousehw_t *)addr = sc->hw;
285 if (sc->mode.level == 0)
286 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
292 *(mousemode_t *)addr = sc->mode;
293 switch (sc->mode.level) {
297 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
298 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
299 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
306 switch (((mousemode_t *)addr)->level) {
313 if (((mousemode_t *)addr)->accelfactor < -1)
315 else if (((mousemode_t *)addr)->accelfactor >= 0)
316 sc->mode.accelfactor =
317 ((mousemode_t *)addr)->accelfactor;
318 sc->mode.level = ((mousemode_t *)addr)->level;
319 switch (sc->mode.level) {
321 sc->sc_bytesread = sc->mode.packetsize
322 = MOUSE_MSC_PACKETSIZE;
325 sc->sc_bytesread = sc->mode.packetsize
326 = MOUSE_SYS_PACKETSIZE;
332 *(int *)addr = sc->mode.level;
336 switch (*(int *)addr) {
338 sc->mode.level = *(int *)addr;
339 sc->sc_bytesread = sc->mode.packetsize
340 = MOUSE_MSC_PACKETSIZE;
343 sc->mode.level = *(int *)addr;
344 sc->sc_bytesread = sc->mode.packetsize
345 = MOUSE_SYS_PACKETSIZE;
352 case MOUSE_GETSTATUS:
355 sc->status.flags = 0;
356 sc->status.obutton = sc->status.button;
357 sc->status.button = 0;
362 *(mousestatus_t *)addr = status;
365 case MOUSE_READSTATE:
369 #if (defined(MOUSE_GETVARS))
382 * msepoll: check for mouse input to be processed.
385 msepoll(struct cdev *dev, int events, struct thread *td)
387 mse_softc_t *sc = dev->si_drv1;
392 if (events & (POLLIN | POLLRDNORM)) {
393 if (sc->sc_bytesread != sc->mode.packetsize ||
394 sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
395 (sc->sc_obuttons ^ sc->sc_buttons) != 0)
396 revents |= events & (POLLIN | POLLRDNORM);
399 * Since this is an exclusive open device, any previous
400 * proc pointer is trash now, so we can just assign it.
402 selrecord(td, &sc->sc_selp);
410 * msetimeout: watchdog timer routine.
413 msetimeout(void *arg)
418 dev = (struct cdev *)arg;
420 if (sc->sc_watchdog) {
422 printf("%s: lost interrupt?\n", devtoname(dev));
425 sc->sc_watchdog = TRUE;
426 sc->sc_callout = timeout(msetimeout, dev, hz);
430 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
436 * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
437 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
439 static int butmap[8] = {
443 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
445 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
446 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
447 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
449 mse_softc_t *sc = arg;
454 static int mse_intrcnt = 0;
455 if((mse_intrcnt++ % 10000) == 0)
458 if ((sc->sc_flags & MSESC_OPEN) == 0)
461 (*sc->sc_getmouse)(sc->sc_iot, sc->sc_ioh, &dx, &dy, &but);
462 if (sc->mode.accelfactor > 0) {
464 dx = dx * dx / sc->mode.accelfactor;
470 dy = dy * dy / sc->mode.accelfactor;
478 sc->sc_buttons = but;
480 but = butmap[~but & MOUSE_MSC_BUTTONS];
483 sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
484 | (sc->status.button ^ but);
485 sc->status.button = but;
487 sc->sc_watchdog = FALSE;
490 * If mouse state has changed, wake up anyone wanting to know.
492 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
493 (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
494 if (sc->sc_flags & MSESC_WANT) {
495 sc->sc_flags &= ~MSESC_WANT;
498 selwakeuppri(&sc->sc_selp, MSEPRI);