1 /* $NetBSD: mms.c,v 1.49 2007/03/04 05:59:58 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, 1994 Charles M. Hannum. 5 * Copyright (c) 1992, 1993 Erik Forsberg. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 17 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __KERNEL_RCSID(0, "$NetBSD: mms.c,v 1.49 2007/03/04 05:59:58 christos Exp $"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/ioctl.h> 32 #include <sys/device.h> 33 34 #include <machine/intr.h> 35 #include <machine/bus.h> 36 37 #include <dev/isa/isavar.h> 38 39 #include <dev/wscons/wsconsio.h> 40 #include <dev/wscons/wsmousevar.h> 41 42 #define MMS_ADDR 0 /* offset for register select */ 43 #define MMS_DATA 1 /* offset for InPort data */ 44 #define MMS_IDENT 2 /* offset for identification register */ 45 #define MMS_NPORTS 4 46 47 struct mms_softc { /* driver status information */ 48 struct device sc_dev; 49 void *sc_ih; 50 51 bus_space_tag_t sc_iot; 52 bus_space_handle_t sc_ioh; 53 54 int sc_enabled; /* device is open */ 55 56 struct device *sc_wsmousedev; 57 }; 58 59 int mmsprobe(struct device *, struct cfdata *, void *); 60 void mmsattach(struct device *, struct device *, void *); 61 int mmsintr(void *); 62 63 CFATTACH_DECL(mms, sizeof(struct mms_softc), 64 mmsprobe, mmsattach, NULL, NULL); 65 66 int mms_enable(void *); 67 int mms_ioctl(void *, u_long, void *, int, struct lwp *); 68 void mms_disable(void *); 69 70 const struct wsmouse_accessops mms_accessops = { 71 mms_enable, 72 mms_ioctl, 73 mms_disable, 74 }; 75 76 int 77 mmsprobe(struct device *parent, struct cfdata *match, 78 void *aux) 79 { 80 struct isa_attach_args *ia = aux; 81 bus_space_tag_t iot = ia->ia_iot; 82 bus_space_handle_t ioh; 83 int rv; 84 85 if (ia->ia_nio < 1) 86 return (0); 87 if (ia->ia_nirq < 1) 88 return (0); 89 90 if (ISA_DIRECT_CONFIG(ia)) 91 return (0); 92 93 /* Disallow wildcarded i/o address. */ 94 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT) 95 return 0; 96 97 if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) 98 return 0; 99 100 /* Map the i/o space. */ 101 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MMS_NPORTS, 0, &ioh)) 102 return 0; 103 104 rv = 0; 105 106 /* Read identification register to see if present */ 107 if (bus_space_read_1(iot, ioh, MMS_IDENT) != 0xde) 108 goto out; 109 110 /* Seems it was there; reset. */ 111 bus_space_write_1(iot, ioh, MMS_ADDR, 0x87); 112 113 rv = 1; 114 115 ia->ia_nio = 1; 116 ia->ia_io[0].ir_size = MMS_NPORTS; 117 118 ia->ia_nirq = 1; 119 120 ia->ia_niomem = 0; 121 ia->ia_ndrq = 0; 122 123 out: 124 bus_space_unmap(iot, ioh, MMS_NPORTS); 125 return rv; 126 } 127 128 void 129 mmsattach(struct device *parent, struct device *self, void *aux) 130 { 131 struct mms_softc *sc = (void *)self; 132 struct isa_attach_args *ia = aux; 133 bus_space_tag_t iot = ia->ia_iot; 134 bus_space_handle_t ioh; 135 struct wsmousedev_attach_args a; 136 137 aprint_naive(": Mouse\n"); 138 aprint_normal(": Microsoft Mouse\n"); 139 140 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MMS_NPORTS, 0, &ioh)) { 141 aprint_error("%s: can't map i/o space\n", sc->sc_dev.dv_xname); 142 return; 143 } 144 145 /* Other initialization was done by mmsprobe. */ 146 sc->sc_iot = iot; 147 sc->sc_ioh = ioh; 148 sc->sc_enabled = 0; 149 150 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq, 151 IST_PULSE, IPL_TTY, mmsintr, sc); 152 153 a.accessops = &mms_accessops; 154 a.accesscookie = sc; 155 156 /* 157 * Attach the wsmouse, saving a handle to it. 158 * Note that we don't need to check this pointer against NULL 159 * here or in psmintr, because if this fails lms_enable() will 160 * never be called, so lmsintr() will never be called. 161 */ 162 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 163 } 164 165 int 166 mms_enable(void *v) 167 { 168 struct mms_softc *sc = v; 169 170 if (sc->sc_enabled) 171 return EBUSY; 172 173 sc->sc_enabled = 1; 174 175 /* Enable interrupts. */ 176 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x07); 177 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_DATA, 0x09); 178 179 return 0; 180 } 181 182 void 183 mms_disable(void *v) 184 { 185 struct mms_softc *sc = v; 186 187 /* Disable interrupts. */ 188 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x87); 189 190 sc->sc_enabled = 0; 191 } 192 193 int 194 mms_ioctl(void *v, u_long cmd, void *data, int flag, 195 struct lwp *l) 196 { 197 #if 0 198 struct mms_softc *sc = v; 199 #endif 200 201 switch (cmd) { 202 case WSMOUSEIO_GTYPE: 203 *(u_int *)data = WSMOUSE_TYPE_MMS; 204 return (0); 205 } 206 return (EPASSTHROUGH); 207 } 208 209 int 210 mmsintr(void *arg) 211 { 212 struct mms_softc *sc = arg; 213 bus_space_tag_t iot = sc->sc_iot; 214 bus_space_handle_t ioh = sc->sc_ioh; 215 u_char status; 216 signed char dx, dy; 217 u_int buttons; 218 int changed; 219 220 if (!sc->sc_enabled) 221 /* Interrupts are not expected. */ 222 return 0; 223 224 /* Freeze InPort registers (disabling interrupts). */ 225 bus_space_write_1(iot, ioh, MMS_ADDR, 0x07); 226 bus_space_write_1(iot, ioh, MMS_DATA, 0x29); 227 228 bus_space_write_1(iot, ioh, MMS_ADDR, 0x00); 229 status = bus_space_read_1(iot, ioh, MMS_DATA); 230 231 if (status & 0x40) { 232 bus_space_write_1(iot, ioh, MMS_ADDR, 1); 233 dx = bus_space_read_1(iot, ioh, MMS_DATA); 234 /* Bounding at -127 avoids a bug in XFree86. */ 235 dx = (dx == -128) ? -127 : dx; 236 237 bus_space_write_1(iot, ioh, MMS_ADDR, 2); 238 dy = bus_space_read_1(iot, ioh, MMS_DATA); 239 dy = (dy == -128) ? 127 : -dy; 240 } else 241 dx = dy = 0; 242 243 /* Unfreeze InPort registers (reenabling interrupts). */ 244 bus_space_write_1(iot, ioh, MMS_ADDR, 0x07); 245 bus_space_write_1(iot, ioh, MMS_DATA, 0x09); 246 247 buttons = ((status & 0x04) ? 0x1 : 0) | 248 ((status & 0x02) ? 0x2 : 0) | 249 ((status & 0x01) ? 0x4 : 0); 250 changed = status & 0x38; 251 252 if (dx || dy || changed) 253 wsmouse_input(sc->sc_wsmousedev, 254 buttons, 255 dx, dy, 0, 0, 256 WSMOUSE_INPUT_DELTA); 257 258 return -1; 259 } 260