1 /* $NetBSD: lms.c,v 1.52 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: lms.c,v 1.52 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/bus.h> 35 #include <machine/intr.h> 36 37 #include <dev/isa/isavar.h> 38 39 #include <dev/wscons/wsconsio.h> 40 #include <dev/wscons/wsmousevar.h> 41 42 #define LMS_DATA 0 /* offset for data port, read-only */ 43 #define LMS_SIGN 1 /* offset for signature port, read-write */ 44 #define LMS_INTR 2 /* offset for interrupt port, read-only */ 45 #define LMS_CNTRL 2 /* offset for control port, write-only */ 46 #define LMS_CONFIG 3 /* for configuration port, read-write */ 47 #define LMS_NPORTS 4 48 49 struct lms_softc { /* driver status information */ 50 struct device sc_dev; 51 void *sc_ih; 52 53 bus_space_tag_t sc_iot; /* bus i/o space identifier */ 54 bus_space_handle_t sc_ioh; /* bus i/o handle */ 55 56 int sc_enabled; /* device is open */ 57 int oldbuttons; /* mouse button status */ 58 59 struct device *sc_wsmousedev; 60 }; 61 62 int lmsprobe(struct device *, struct cfdata *, void *); 63 void lmsattach(struct device *, struct device *, void *); 64 int lmsintr(void *); 65 66 CFATTACH_DECL(lms, sizeof(struct lms_softc), 67 lmsprobe, lmsattach, NULL, NULL); 68 69 int lms_enable(void *); 70 int lms_ioctl(void *, u_long, void *, int, struct lwp *); 71 void lms_disable(void *); 72 73 const struct wsmouse_accessops lms_accessops = { 74 lms_enable, 75 lms_ioctl, 76 lms_disable, 77 }; 78 79 int 80 lmsprobe(struct device *parent, struct cfdata *match, 81 void *aux) 82 { 83 struct isa_attach_args *ia = aux; 84 bus_space_tag_t iot = ia->ia_iot; 85 bus_space_handle_t ioh; 86 int rv; 87 88 if (ia->ia_nio < 1) 89 return (0); 90 if (ia->ia_nirq < 1) 91 return (0); 92 93 if (ISA_DIRECT_CONFIG(ia)) 94 return (0); 95 96 /* Disallow wildcarded i/o base. */ 97 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT) 98 return 0; 99 if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) 100 return 0; 101 102 /* Map the i/o space. */ 103 if (bus_space_map(iot, ia->ia_io[0].ir_addr, LMS_NPORTS, 0, &ioh)) 104 return 0; 105 106 rv = 0; 107 108 /* Configure and check for port present. */ 109 bus_space_write_1(iot, ioh, LMS_CONFIG, 0x91); 110 delay(10); 111 bus_space_write_1(iot, ioh, LMS_SIGN, 0x0c); 112 delay(10); 113 if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x0c) 114 goto out; 115 bus_space_write_1(iot, ioh, LMS_SIGN, 0x50); 116 delay(10); 117 if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x50) 118 goto out; 119 120 /* Disable interrupts. */ 121 bus_space_write_1(iot, ioh, LMS_CNTRL, 0x10); 122 123 rv = 1; 124 ia->ia_nio = 1; 125 ia->ia_io[0].ir_size = LMS_NPORTS; 126 127 ia->ia_nirq = 1; 128 129 ia->ia_niomem = 0; 130 ia->ia_ndrq = 0; 131 132 out: 133 bus_space_unmap(iot, ioh, LMS_NPORTS); 134 return rv; 135 } 136 137 void 138 lmsattach(struct device *parent, struct device *self, void *aux) 139 { 140 struct lms_softc *sc = (void *)self; 141 struct isa_attach_args *ia = aux; 142 bus_space_tag_t iot = ia->ia_iot; 143 bus_space_handle_t ioh; 144 struct wsmousedev_attach_args a; 145 146 aprint_naive(": Mouse\n"); 147 aprint_normal(": Logitech Mouse\n"); 148 149 if (bus_space_map(iot, ia->ia_io[0].ir_addr, LMS_NPORTS, 0, &ioh)) { 150 aprint_error("%s: can't map i/o space\n", sc->sc_dev.dv_xname); 151 return; 152 } 153 154 /* Other initialization was done by lmsprobe. */ 155 sc->sc_iot = iot; 156 sc->sc_ioh = ioh; 157 sc->sc_enabled = 0; 158 159 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq, 160 IST_PULSE, IPL_TTY, lmsintr, sc); 161 162 a.accessops = &lms_accessops; 163 a.accesscookie = sc; 164 165 /* 166 * Attach the wsmouse, saving a handle to it. 167 * Note that we don't need to check this pointer against NULL 168 * here or in psmintr, because if this fails lms_enable() will 169 * never be called, so lmsintr() will never be called. 170 */ 171 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 172 } 173 174 int 175 lms_enable(void *v) 176 { 177 struct lms_softc *sc = v; 178 179 if (sc->sc_enabled) 180 return EBUSY; 181 182 sc->sc_enabled = 1; 183 sc->oldbuttons = 0; 184 185 /* Enable interrupts. */ 186 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0); 187 188 return 0; 189 } 190 191 void 192 lms_disable(void *v) 193 { 194 struct lms_softc *sc = v; 195 196 /* Disable interrupts. */ 197 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0x10); 198 199 sc->sc_enabled = 0; 200 } 201 202 int 203 lms_ioctl(void *v, u_long cmd, void *data, int flag, 204 struct lwp *l) 205 { 206 #if 0 207 struct lms_softc *sc = v; 208 #endif 209 210 switch (cmd) { 211 case WSMOUSEIO_GTYPE: 212 *(u_int *)data = WSMOUSE_TYPE_LMS; 213 return (0); 214 } 215 return (EPASSTHROUGH); 216 } 217 218 int 219 lmsintr(void *arg) 220 { 221 struct lms_softc *sc = arg; 222 bus_space_tag_t iot = sc->sc_iot; 223 bus_space_handle_t ioh = sc->sc_ioh; 224 u_char hi, lo; 225 signed char dx, dy; 226 u_int buttons; 227 int changed; 228 229 if (!sc->sc_enabled) 230 /* Interrupts are not expected. */ 231 return 0; 232 233 bus_space_write_1(iot, ioh, LMS_CNTRL, 0xab); 234 hi = bus_space_read_1(iot, ioh, LMS_DATA); 235 bus_space_write_1(iot, ioh, LMS_CNTRL, 0x90); 236 lo = bus_space_read_1(iot, ioh, LMS_DATA); 237 dx = ((hi & 0x0f) << 4) | (lo & 0x0f); 238 /* Bounding at -127 avoids a bug in XFree86. */ 239 dx = (dx == -128) ? -127 : dx; 240 241 bus_space_write_1(iot, ioh, LMS_CNTRL, 0xf0); 242 hi = bus_space_read_1(iot, ioh, LMS_DATA); 243 bus_space_write_1(iot, ioh, LMS_CNTRL, 0xd0); 244 lo = bus_space_read_1(iot, ioh, LMS_DATA); 245 dy = ((hi & 0x0f) << 4) | (lo & 0x0f); 246 dy = (dy == -128) ? 127 : -dy; 247 248 bus_space_write_1(iot, ioh, LMS_CNTRL, 0); 249 250 buttons = ((hi & 0x80) ? 0 : 0x1) | 251 ((hi & 0x40) ? 0 : 0x2) | 252 ((hi & 0x20) ? 0 : 0x4); 253 changed = (buttons ^ sc->oldbuttons); 254 sc->oldbuttons = buttons; 255 256 if (dx || dy || changed) 257 wsmouse_input(sc->sc_wsmousedev, 258 buttons, 259 dx, dy, 0, 0, 260 WSMOUSE_INPUT_DELTA); 261 262 return -1; 263 } 264