1 /* $NetBSD: dzms.c,v 1.2 2001/11/13 12:49:45 lukem 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.2 2001/11/13 12:49:45 lukem 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/qbus/dzreg.h> 63 #include <dev/qbus/dzvar.h> 64 65 #include <dev/dec/dzkbdvar.h> 66 #include <dev/dec/lk201.h> 67 68 #include <dev/wscons/wsconsio.h> 69 #include <dev/wscons/wsmousevar.h> 70 71 #include "locators.h" 72 73 struct dzms_softc { /* driver status information */ 74 struct device dzms_dev; /* required first: base device */ 75 struct dz_linestate *dzms_ls; 76 77 int sc_enabled; /* input enabled? */ 78 int self_test; 79 80 int inputstate; 81 u_int buttons; 82 signed char dx; 83 signed char dy; 84 85 struct device *sc_wsmousedev; 86 }; 87 88 static int dzms_match __P((struct device *, struct cfdata *, void *)); 89 static void dzms_attach __P((struct device *, struct device *, void *)); 90 static int dzms_input __P((void *, int)); 91 92 struct cfattach dzms_ca = { 93 sizeof(struct dzms_softc), dzms_match, dzms_attach, 94 }; 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->self_test = 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 /* XXX mice presence test should be done in match/attach context XXX */ 161 sc->self_test = 1; 162 dzputc(sc->dzms_ls, MOUSE_SELF_TEST); 163 DELAY(100000); 164 if (sc->self_test < 0) { 165 sc->self_test = 0; 166 return EBUSY; 167 } else if (sc->self_test == 5) { 168 sc->self_test = 0; 169 sc->sc_enabled = 1; 170 } 171 sc->inputstate = 0; 172 173 dzputc(sc->dzms_ls, MOUSE_INCREMENTAL); 174 175 return 0; 176 } 177 178 static void 179 dzms_disable(v) 180 void *v; 181 { 182 struct dzms_softc *sc = v; 183 184 sc->sc_enabled = 0; 185 } 186 187 static int 188 dzms_ioctl(v, cmd, data, flag, p) 189 void *v; 190 u_long cmd; 191 caddr_t data; 192 int flag; 193 struct proc *p; 194 { 195 if (cmd == WSMOUSEIO_GTYPE) { 196 *(u_int *)data = WSMOUSE_TYPE_VSXXX; 197 return 0; 198 } 199 return -1; 200 } 201 202 static int 203 dzms_input(vsc, data) 204 void *vsc; 205 int data; 206 { 207 struct dzms_softc *sc = vsc; 208 209 /* XXX mice presence test should be done in match/attach context XXX */ 210 if (!sc->sc_enabled) { 211 if (sc->self_test > 0) { 212 if (data < 0) { 213 printf("Timeout on 1st byte of mouse self-test report\n"); 214 sc->self_test = -1; 215 } else { 216 sc->self_test++; 217 } 218 } 219 if (sc->self_test == 3) { 220 if ((data & 0x0f) != 0x2) { 221 printf("We don't have a mouse!!!\n"); 222 sc->self_test = -1; 223 } 224 } 225 /* Interrupts are not expected. Discard the byte. */ 226 return(1); 227 } 228 229 #define WSMS_BUTTON1 0x01 230 #define WSMS_BUTTON2 0x02 231 #define WSMS_BUTTON3 0x04 232 233 if ((data & MOUSE_START_FRAME) != 0) 234 sc->inputstate = 1; 235 else 236 sc->inputstate++; 237 238 if (sc->inputstate == 1) { 239 sc->buttons = 0; 240 if ((data & LEFT_BUTTON) != 0) 241 sc->buttons |= WSMS_BUTTON1; 242 if ((data & MIDDLE_BUTTON) != 0) 243 sc->buttons |= WSMS_BUTTON2; 244 if ((data & RIGHT_BUTTON) != 0) 245 sc->buttons |= WSMS_BUTTON3; 246 247 sc->dx = data & MOUSE_X_SIGN; 248 sc->dy = data & MOUSE_Y_SIGN; 249 } else if (sc->inputstate == 2) { 250 if (sc->dx == 0) 251 sc->dx = -data; 252 else 253 sc->dx = data; 254 } else if (sc->inputstate == 3) { 255 sc->inputstate = 0; 256 if (sc->dy == 0) 257 sc->dy = -data; 258 else 259 sc->dy = data; 260 wsmouse_input(sc->sc_wsmousedev, sc->buttons, 261 sc->dx, sc->dy, 0, WSMOUSE_INPUT_DELTA); 262 } 263 264 return(1); 265 } 266 267