1 /* $NetBSD: dtms.c,v 1.10 2011/06/04 01:37:36 tsutsui Exp $ */ 2 3 /*- 4 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: dtms.c,v 1.10 2011/06/04 01:37:36 tsutsui Exp $"); 34 35 #include "locators.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 #include <sys/ioctl.h> 41 #include <sys/syslog.h> 42 43 #include <machine/bus.h> 44 45 #include <arch/pmax/tc/dtreg.h> 46 #include <arch/pmax/tc/dtvar.h> 47 48 #include <dev/wscons/wsconsio.h> 49 #include <dev/wscons/wsmousevar.h> 50 51 struct dtms_softc { 52 device_t sc_dev; 53 device_t sc_wsmousedev; 54 int sc_enabled; 55 }; 56 57 int dtms_match(device_t, cfdata_t, void *); 58 void dtms_attach(device_t, device_t, void *); 59 int dtms_input(void *, int); 60 int dtms_enable(void *); 61 int dtms_ioctl(void *, u_long, void *, int, struct lwp *); 62 void dtms_disable(void *); 63 void dtms_handler(void *, struct dt_msg *); 64 65 CFATTACH_DECL_NEW(dtms, sizeof(struct dtms_softc), 66 dtms_match, dtms_attach, NULL, NULL); 67 68 const struct wsmouse_accessops dtms_accessops = { 69 dtms_enable, 70 dtms_ioctl, 71 dtms_disable, 72 }; 73 74 int 75 dtms_match(device_t parent, cfdata_t cf, void *aux) 76 { 77 struct dt_attach_args *dta; 78 79 dta = aux; 80 return (dta->dta_addr == DT_ADDR_MOUSE); 81 } 82 83 void 84 dtms_attach(device_t parent, device_t self, void *aux) 85 { 86 struct wsmousedev_attach_args a; 87 struct dtms_softc *sc; 88 struct dt_softc *dt; 89 90 dt = device_private(parent); 91 sc = device_private(self); 92 sc->sc_dev = self; 93 94 printf("\n"); 95 96 if (dt_establish_handler(dt, &dt_ms_dv, sc, dtms_handler)) { 97 printf("%s: unable to establish handler\n", device_xname(self)); 98 return; 99 } 100 101 a.accessops = &dtms_accessops; 102 a.accesscookie = sc; 103 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 104 } 105 106 int 107 dtms_enable(void *cookie) 108 { 109 struct dtms_softc *sc; 110 111 sc = cookie; 112 if (sc->sc_enabled) 113 return (EBUSY); 114 sc->sc_enabled = 1; 115 116 return (0); 117 } 118 119 void 120 dtms_disable(void *cookie) 121 { 122 struct dtms_softc *sc; 123 124 sc = cookie; 125 sc->sc_enabled = 0; 126 } 127 128 int 129 dtms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 130 { 131 132 if (cmd == WSMOUSEIO_GTYPE) { 133 *(u_int *)data = WSMOUSE_TYPE_VSXXX; 134 return (0); 135 } 136 137 return (EPASSTHROUGH); 138 } 139 140 void 141 dtms_handler(void *cookie, struct dt_msg *msg) 142 { 143 struct dtms_softc *sc; 144 int buttons, dx, dy; 145 short tmp; 146 147 sc = cookie; 148 149 if (!sc->sc_enabled) 150 return; 151 152 tmp = DT_GET_SHORT(msg->body[0], msg->body[1]); 153 buttons = tmp & 1; 154 if (tmp & 2) 155 buttons |= 4; 156 if (tmp & 4) 157 buttons |= 2; 158 159 tmp = DT_GET_SHORT(msg->body[2], msg->body[3]); 160 if (tmp < 0) 161 dx = -(-tmp & 0x1f); 162 else 163 dx = tmp & 0x1f; 164 165 tmp = DT_GET_SHORT(msg->body[4], msg->body[5]); 166 if (tmp < 0) 167 dy = -(-tmp & 0x1f); 168 else 169 dy = tmp & 0x1f; 170 171 wsmouse_input(sc->sc_wsmousedev, 172 buttons, 173 dx, dy, 0, 0, 174 WSMOUSE_INPUT_DELTA); 175 } 176