1 /* $NetBSD: dzms.c,v 1.5 2002/03/26 13:59:10 fredette 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.5 2002/03/26 13:59:10 fredette 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 60 #include <machine/bus.h> 61 62 #include <dev/dec/dzreg.h> 63 #include <dev/dec/dzvar.h> 64 #include <dev/dec/dzkbdvar.h> 65 #include <dev/dec/lk201.h> 66 67 #include <dev/wscons/wsconsio.h> 68 #include <dev/wscons/wsmousevar.h> 69 70 #include "locators.h" 71 72 struct dzms_softc { /* driver status information */ 73 struct device dzms_dev; /* required first: base device */ 74 struct dz_linestate *dzms_ls; 75 76 int sc_enabled; /* input enabled? */ 77 int self_test; 78 79 int inputstate; 80 u_int buttons; 81 signed char dx; 82 signed char dy; 83 84 struct device *sc_wsmousedev; 85 }; 86 87 static int dzms_match __P((struct device *, struct cfdata *, void *)); 88 static void dzms_attach __P((struct device *, struct device *, void *)); 89 static int dzms_input __P((void *, int)); 90 91 struct cfattach dzms_ca = { 92 sizeof(struct dzms_softc), dzms_match, dzms_attach, 93 }; 94 95 static int dzms_enable __P((void *)); 96 static int dzms_ioctl __P((void *, u_long, caddr_t, int, struct proc *)); 97 static void dzms_disable __P((void *)); 98 99 const struct wsmouse_accessops dzms_accessops = { 100 dzms_enable, 101 dzms_ioctl, 102 dzms_disable, 103 }; 104 105 static int 106 dzms_match(parent, cf, aux) 107 struct device *parent; 108 struct cfdata *cf; 109 void *aux; 110 { 111 struct dzkm_attach_args *daa = aux; 112 113 /* Exact match is better than wildcard. */ 114 if (cf->cf_loc[DZCF_LINE] == daa->daa_line) 115 return 2; 116 117 /* This driver accepts wildcard. */ 118 if (cf->cf_loc[DZCF_LINE] == DZCF_LINE_DEFAULT) 119 return 1; 120 121 return 0; 122 } 123 124 static void 125 dzms_attach(parent, self, aux) 126 struct device *parent, *self; 127 void *aux; 128 { 129 struct dz_softc *dz = (void *)parent; 130 struct dzms_softc *dzms = (void *)self; 131 struct dzkm_attach_args *daa = aux; 132 struct dz_linestate *ls; 133 struct wsmousedev_attach_args a; 134 135 dz->sc_dz[daa->daa_line].dz_catch = dzms_input; 136 dz->sc_dz[daa->daa_line].dz_private = dzms; 137 ls = &dz->sc_dz[daa->daa_line]; 138 dzms->dzms_ls = ls; 139 140 printf("\n"); 141 142 a.accessops = &dzms_accessops; 143 a.accesscookie = dzms; 144 145 dzms->sc_enabled = 0; 146 dzms->self_test = 0; 147 dzms->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 148 } 149 150 static int 151 dzms_enable(v) 152 void *v; 153 { 154 struct dzms_softc *sc = v; 155 156 if (sc->sc_enabled) 157 return EBUSY; 158 159 /* XXX mice presence test should be done in match/attach context XXX */ 160 sc->self_test = 1; 161 dzputc(sc->dzms_ls, MOUSE_SELF_TEST); 162 DELAY(100000); 163 DELAY(100000); 164 DELAY(100000); 165 if (sc->self_test < 0) { 166 sc->self_test = 0; 167 return EBUSY; 168 } else if (sc->self_test == 5) { 169 sc->self_test = 0; 170 sc->sc_enabled = 1; 171 } 172 sc->inputstate = 0; 173 174 dzputc(sc->dzms_ls, MOUSE_INCREMENTAL); 175 176 return 0; 177 } 178 179 static void 180 dzms_disable(v) 181 void *v; 182 { 183 struct dzms_softc *sc = v; 184 185 sc->sc_enabled = 0; 186 } 187 188 static int 189 dzms_ioctl(v, cmd, data, flag, p) 190 void *v; 191 u_long cmd; 192 caddr_t data; 193 int flag; 194 struct proc *p; 195 { 196 if (cmd == WSMOUSEIO_GTYPE) { 197 *(u_int *)data = WSMOUSE_TYPE_VSXXX; 198 return 0; 199 } 200 return EPASSTHROUGH; 201 } 202 203 static int 204 dzms_input(vsc, data) 205 void *vsc; 206 int data; 207 { 208 struct dzms_softc *sc = vsc; 209 210 /* XXX mice presence test should be done in match/attach context XXX */ 211 if (!sc->sc_enabled) { 212 if (sc->self_test > 0) { 213 if (data < 0) { 214 printf("Timeout on 1st byte of mouse self-test report\n"); 215 sc->self_test = -1; 216 } else { 217 sc->self_test++; 218 } 219 } 220 if (sc->self_test == 3) { 221 if ((data & 0x0f) != 0x2) { 222 printf("We don't have a mouse!!!\n"); 223 sc->self_test = -1; 224 } 225 } 226 /* Interrupts are not expected. Discard the byte. */ 227 return(1); 228 } 229 230 #define WSMS_BUTTON1 0x01 231 #define WSMS_BUTTON2 0x02 232 #define WSMS_BUTTON3 0x04 233 234 if ((data & MOUSE_START_FRAME) != 0) 235 sc->inputstate = 1; 236 else 237 sc->inputstate++; 238 239 if (sc->inputstate == 1) { 240 sc->buttons = 0; 241 if ((data & LEFT_BUTTON) != 0) 242 sc->buttons |= WSMS_BUTTON1; 243 if ((data & MIDDLE_BUTTON) != 0) 244 sc->buttons |= WSMS_BUTTON2; 245 if ((data & RIGHT_BUTTON) != 0) 246 sc->buttons |= WSMS_BUTTON3; 247 248 sc->dx = data & MOUSE_X_SIGN; 249 sc->dy = data & MOUSE_Y_SIGN; 250 } else if (sc->inputstate == 2) { 251 if (sc->dx == 0) 252 sc->dx = -data; 253 else 254 sc->dx = data; 255 } else if (sc->inputstate == 3) { 256 sc->inputstate = 0; 257 if (sc->dy == 0) 258 sc->dy = -data; 259 else 260 sc->dy = data; 261 wsmouse_input(sc->sc_wsmousedev, sc->buttons, 262 sc->dx, sc->dy, 0, WSMOUSE_INPUT_DELTA); 263 } 264 265 return(1); 266 } 267 268