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