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 static int mse_isa_probe(device_t dev);
93 static int mse_isa_attach(device_t dev);
94 static int mse_isa_detach(device_t dev);
96 static device_method_t mse_methods[] = {
97 DEVMETHOD(device_probe, mse_isa_probe),
98 DEVMETHOD(device_attach, mse_isa_attach),
99 DEVMETHOD(device_detach, mse_isa_detach),
103 static driver_t mse_driver = {
109 DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0);
111 static struct isa_pnp_id mse_ids[] = {
112 { 0x000fd041, "Bus mouse" }, /* PNP0F00 */
113 { 0x020fd041, "InPort mouse" }, /* PNP0F02 */
114 { 0x0d0fd041, "InPort mouse compatible" }, /* PNP0F0D */
115 { 0x110fd041, "Bus mouse compatible" }, /* PNP0F11 */
116 { 0x150fd041, "Logitech bus mouse" }, /* PNP0F15 */
117 { 0x180fd041, "Logitech bus mouse compatible" },/* PNP0F18 */
122 * Logitech bus mouse definitions
124 #define MSE_SETUP 0x91 /* What does this mean? */
125 /* The definition for the control port */
128 /* D7 = Mode set flag (1 = active) */
129 /* D6,D5 = Mode selection (port A) */
130 /* 00 = Mode 0 = Basic I/O */
131 /* 01 = Mode 1 = Strobed I/O */
132 /* 10 = Mode 2 = Bi-dir bus */
133 /* D4 = Port A direction (1 = input)*/
134 /* D3 = Port C (upper 4 bits) */
135 /* direction. (1 = input) */
136 /* D2 = Mode selection (port B & C) */
137 /* 0 = Mode 0 = Basic I/O */
138 /* 1 = Mode 1 = Strobed I/O */
139 /* D1 = Port B direction (1 = input)*/
140 /* D0 = Port C (lower 4 bits) */
141 /* direction. (1 = input) */
143 /* So 91 means Basic I/O on all 3 ports,*/
144 /* Port A is an input port, B is an */
145 /* output port, C is split with upper */
146 /* 4 bits being an output port and lower*/
147 /* 4 bits an input port, and enable the */
149 /* Courtesy Intel 8255 databook. Lars */
150 #define MSE_HOLD 0x80
151 #define MSE_RXLOW 0x00
152 #define MSE_RXHIGH 0x20
153 #define MSE_RYLOW 0x40
154 #define MSE_RYHIGH 0x60
155 #define MSE_DISINTR 0x10
156 #define MSE_INTREN 0x00
158 static int mse_probelogi(device_t dev, mse_softc_t *sc);
159 static void mse_disablelogi(bus_space_tag_t t,
160 bus_space_handle_t h);
161 static void mse_getlogi(bus_space_tag_t t, bus_space_handle_t h,
162 int *dx, int *dy, int *but);
163 static void mse_enablelogi(bus_space_tag_t t,
164 bus_space_handle_t h);
167 * ATI Inport mouse definitions
169 #define MSE_INPORT_RESET 0x80
170 #define MSE_INPORT_STATUS 0x00
171 #define MSE_INPORT_DX 0x01
172 #define MSE_INPORT_DY 0x02
173 #define MSE_INPORT_MODE 0x07
174 #define MSE_INPORT_HOLD 0x20
175 #define MSE_INPORT_INTREN 0x09
177 static int mse_probeati(device_t dev, mse_softc_t *sc);
178 static void mse_enableati(bus_space_tag_t t, bus_space_handle_t h);
179 static void mse_disableati(bus_space_tag_t t, bus_space_handle_t h);
180 static void mse_getati(bus_space_tag_t t, bus_space_handle_t h,
181 int *dx, int *dy, int *but);
183 static struct mse_types mse_types[] = {
185 mse_probeati, mse_enableati, mse_disableati, mse_getati,
186 { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
187 { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
188 { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
190 mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi,
191 { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
192 { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
193 { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
198 mse_isa_probe(device_t dev)
206 error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
210 sc = device_get_softc(dev);
212 sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
213 MSE_IOSIZE, RF_ACTIVE);
214 if (sc->sc_port == NULL)
216 sc->sc_iot = rman_get_bustag(sc->sc_port);
217 sc->sc_ioh = rman_get_bushandle(sc->sc_port);
220 * Check for each mouse type in the table.
223 while (mse_types[i].m_type) {
224 if ((*mse_types[i].m_probe)(dev, sc)) {
225 sc->sc_mousetype = mse_types[i].m_type;
226 sc->sc_enablemouse = mse_types[i].m_enable;
227 sc->sc_disablemouse = mse_types[i].m_disable;
228 sc->sc_getmouse = mse_types[i].m_get;
229 sc->hw = mse_types[i].m_hw;
230 sc->mode = mse_types[i].m_mode;
231 bus_release_resource(dev, SYS_RES_IOPORT, rid,
233 device_set_desc(dev, "Bus/InPort Mouse");
238 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
243 mse_isa_attach(device_t dev)
248 sc = device_get_softc(dev);
251 sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
252 MSE_IOSIZE, RF_ACTIVE);
253 if (sc->sc_port == NULL)
255 sc->sc_iot = rman_get_bustag(sc->sc_port);
256 sc->sc_ioh = rman_get_bushandle(sc->sc_port);
258 return (mse_common_attach(dev));
262 mse_isa_detach(device_t dev)
267 sc = device_get_softc(dev);
268 if (sc->sc_flags & MSESC_OPEN)
272 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih);
273 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
274 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
276 destroy_dev(sc->sc_dev);
277 destroy_dev(sc->sc_ndev);
283 * Routines for the Logitech mouse.
286 * Test for a Logitech bus mouse and return 1 if it is.
287 * (until I know how to use the signature port properly, just disable
288 * interrupts and return 1)
291 mse_probelogi(device_t dev, mse_softc_t *sc)
296 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTD, MSE_SETUP);
297 /* set the signature port */
298 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB, MSE_LOGI_SIG);
300 DELAY(30000); /* 30 ms delay */
301 sig = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB) & 0xFF;
302 if (sig == MSE_LOGI_SIG) {
303 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC,
308 device_printf(dev, "wrong signature %x\n", sig);
314 * Initialize Logitech mouse and enable interrupts.
317 mse_enablelogi(bus_space_tag_t tag, bus_space_handle_t handle)
321 bus_space_write_1(tag, handle, MSE_PORTD, MSE_SETUP);
322 mse_getlogi(tag, handle, &dx, &dy, &but);
326 * Disable interrupts for Logitech mouse.
329 mse_disablelogi(bus_space_tag_t tag, bus_space_handle_t handle)
332 bus_space_write_1(tag, handle, MSE_PORTC, MSE_DISINTR);
336 * Get the current dx, dy and button up/down state.
339 mse_getlogi(bus_space_tag_t tag, bus_space_handle_t handle, int *dx, int *dy,
344 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXLOW);
345 x = bus_space_read_1(tag, handle, MSE_PORTA);
346 *but = (x >> 5) & MOUSE_MSC_BUTTONS;
348 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
349 x |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
350 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYLOW);
351 y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0xf);
352 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
353 y |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
356 bus_space_write_1(tag, handle, MSE_PORTC, MSE_INTREN);
360 * Routines for the ATI Inport bus mouse.
363 * Test for an ATI Inport bus mouse and return 1 if it is.
364 * (do not enable interrupts)
367 mse_probeati(device_t dev, mse_softc_t *sc)
371 for (i = 0; i < 2; i++)
372 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC) == 0xde)
378 * Initialize ATI Inport mouse and enable interrupts.
381 mse_enableati(bus_space_tag_t tag, bus_space_handle_t handle)
384 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_RESET);
385 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
386 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
390 * Disable interrupts for ATI Inport mouse.
393 mse_disableati(bus_space_tag_t tag, bus_space_handle_t handle)
396 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
397 bus_space_write_1(tag, handle, MSE_PORTB, 0);
401 * Get current dx, dy and up/down button state.
404 mse_getati(bus_space_tag_t tag, bus_space_handle_t handle, int *dx, int *dy,
409 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
410 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_HOLD);
411 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_STATUS);
412 *but = ~bus_space_read_1(tag, handle, MSE_PORTB) & MOUSE_MSC_BUTTONS;
413 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DX);
414 byte = bus_space_read_1(tag, handle, MSE_PORTB);
416 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DY);
417 byte = bus_space_read_1(tag, handle, MSE_PORTB);
419 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
420 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);