1 /* $NetBSD: dzms.c,v 1.9 2002/10/02 16:33:44 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratory. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * @(#)ms.c 8.1 (Berkeley) 6/11/93 45 */ 46 47 /* 48 * VSXXX mice attached to line 1 of the DZ* 49 */ 50 51 #include <sys/cdefs.h> 52 __KERNEL_RCSID(0, "$NetBSD: dzms.c,v 1.9 2002/10/02 16:33:44 thorpej Exp $"); 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/device.h> 57 #include <sys/ioctl.h> 58 #include <sys/syslog.h> 59 #include <sys/kernel.h> 60 #include <sys/proc.h> 61 #include <sys/tty.h> 62 63 #include <machine/bus.h> 64 65 #include <dev/dec/dzreg.h> 66 #include <dev/dec/dzvar.h> 67 #include <dev/dec/dzkbdvar.h> 68 #include <dev/dec/lk201.h> 69 70 #include <dev/wscons/wsconsio.h> 71 #include <dev/wscons/wsmousevar.h> 72 73 #include "locators.h" 74 75 struct dzms_softc { /* driver status information */ 76 struct device dzms_dev; /* required first: base device */ 77 struct dz_linestate *dzms_ls; 78 79 int sc_enabled; /* input enabled? */ 80 int sc_selftest; 81 82 int inputstate; 83 u_int buttons; 84 int dx, dy; 85 86 struct device *sc_wsmousedev; 87 }; 88 89 static int dzms_match __P((struct device *, struct cfdata *, void *)); 90 static void dzms_attach __P((struct device *, struct device *, void *)); 91 static int dzms_input __P((void *, int)); 92 93 CFATTACH_DECL(dzms, sizeof(struct dzms_softc), 94 dzms_match, dzms_attach, NULL, NULL); 95 96 static int dzms_enable __P((void *)); 97 static int dzms_ioctl __P((void *, u_long, caddr_t, int, struct proc *)); 98 static void dzms_disable __P((void *)); 99 100 const struct wsmouse_accessops dzms_accessops = { 101 dzms_enable, 102 dzms_ioctl, 103 dzms_disable, 104 }; 105 106 static int 107 dzms_match(parent, cf, aux) 108 struct device *parent; 109 struct cfdata *cf; 110 void *aux; 111 { 112 struct dzkm_attach_args *daa = aux; 113 114 /* Exact match is better than wildcard. */ 115 if (cf->cf_loc[DZCF_LINE] == daa->daa_line) 116 return 2; 117 118 /* This driver accepts wildcard. */ 119 if (cf->cf_loc[DZCF_LINE] == DZCF_LINE_DEFAULT) 120 return 1; 121 122 return 0; 123 } 124 125 static void 126 dzms_attach(parent, self, aux) 127 struct device *parent, *self; 128 void *aux; 129 { 130 struct dz_softc *dz = (void *)parent; 131 struct dzms_softc *dzms = (void *)self; 132 struct dzkm_attach_args *daa = aux; 133 struct dz_linestate *ls; 134 struct wsmousedev_attach_args a; 135 136 dz->sc_dz[daa->daa_line].dz_catch = dzms_input; 137 dz->sc_dz[daa->daa_line].dz_private = dzms; 138 ls = &dz->sc_dz[daa->daa_line]; 139 dzms->dzms_ls = ls; 140 141 printf("\n"); 142 143 a.accessops = &dzms_accessops; 144 a.accesscookie = dzms; 145 146 dzms->sc_enabled = 0; 147 dzms->sc_selftest = 0; 148 dzms->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 149 } 150 151 static int 152 dzms_enable(v) 153 void *v; 154 { 155 struct dzms_softc *sc = v; 156 157 if (sc->sc_enabled) 158 return EBUSY; 159 160 sc->sc_selftest = 4; /* wait for 4 byte reply upto 1/2 sec */ 161 dzputc(sc->dzms_ls, MOUSE_SELF_TEST); 162 (void)tsleep(dzms_enable, TTIPRI, "dzmsopen", hz / 2); 163 if (sc->sc_selftest != 0) { 164 sc->sc_selftest = 0; 165 return ENXIO; 166 } 167 DELAY(150); 168 dzputc(sc->dzms_ls, MOUSE_INCREMENTAL); 169 sc->sc_enabled = 1; 170 sc->inputstate = 0; 171 return 0; 172 } 173 174 static void 175 dzms_disable(v) 176 void *v; 177 { 178 struct dzms_softc *sc = v; 179 180 sc->sc_enabled = 0; 181 } 182 183 static int 184 dzms_ioctl(v, cmd, data, flag, p) 185 void *v; 186 u_long cmd; 187 caddr_t data; 188 int flag; 189 struct proc *p; 190 { 191 if (cmd == WSMOUSEIO_GTYPE) { 192 *(u_int *)data = WSMOUSE_TYPE_VSXXX; 193 return 0; 194 } 195 return EPASSTHROUGH; 196 } 197 198 static int 199 dzms_input(vsc, data) 200 void *vsc; 201 int data; 202 { 203 struct dzms_softc *sc = vsc; 204 205 if (sc->sc_enabled == 0) { 206 if (sc->sc_selftest > 0) { 207 sc->sc_selftest -= 1; 208 if (sc->sc_selftest == 0) 209 wakeup(dzms_enable); 210 } 211 return (1); 212 } 213 214 #define WSMS_BUTTON1 0x01 215 #define WSMS_BUTTON2 0x02 216 #define WSMS_BUTTON3 0x04 217 218 if ((data & MOUSE_START_FRAME) != 0) 219 sc->inputstate = 1; 220 else 221 sc->inputstate++; 222 223 if (sc->inputstate == 1) { 224 sc->buttons = 0; 225 if ((data & LEFT_BUTTON) != 0) 226 sc->buttons |= WSMS_BUTTON1; 227 if ((data & MIDDLE_BUTTON) != 0) 228 sc->buttons |= WSMS_BUTTON2; 229 if ((data & RIGHT_BUTTON) != 0) 230 sc->buttons |= WSMS_BUTTON3; 231 232 sc->dx = data & MOUSE_X_SIGN; 233 sc->dy = data & MOUSE_Y_SIGN; 234 } else if (sc->inputstate == 2) { 235 if (sc->dx == 0) 236 sc->dx = -data; 237 else 238 sc->dx = data; 239 } else if (sc->inputstate == 3) { 240 sc->inputstate = 0; 241 if (sc->dy == 0) 242 sc->dy = -data; 243 else 244 sc->dy = data; 245 wsmouse_input(sc->sc_wsmousedev, sc->buttons, 246 sc->dx, sc->dy, 0, WSMOUSE_INPUT_DELTA); 247 } 248 249 return(1); 250 } 251 252