1 /* $NetBSD: mms.c,v 1.13 2007/03/04 05:59:44 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: mms.c,v 1.13 2007/03/04 05:59:44 christos Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/device.h> 44 #include <sys/proc.h> 45 #include <sys/systm.h> 46 47 #include "wsmouse.h" 48 49 #include <dev/wscons/wsconsio.h> 50 #include <dev/wscons/wsmousevar.h> 51 52 #include <dreamcast/dev/maple/maple.h> 53 #include <dreamcast/dev/maple/mapleconf.h> 54 55 struct mms_condition { 56 uint32_t func_code; /* function code (big endian) */ 57 uint32_t buttons; 58 uint16_t axis1; /* X */ 59 uint16_t axis2; /* Y */ 60 uint16_t axis3; /* wheel */ 61 uint16_t axis4; 62 uint16_t axis5; 63 uint16_t axis6; 64 uint16_t axis7; 65 uint16_t axis8; 66 }; 67 68 #define MMS_BUTTON_C 0x01 /* middle */ 69 #define MMS_BUTTON_B 0x02 /* right */ 70 #define MMS_BUTTON_A 0x04 /* left */ 71 #define MMS_BUTTON_START 0x08 /* thumb */ 72 #define MMS_BUTTON_MASK 0x0f 73 74 #define MMS_MOVEMENT_BASE 0x200 75 #define MMS_MOVEMENT_MAX 0x3ff 76 77 #define MMS_FUNCDATA_AXIS1 0x001 78 #define MMS_FUNCDATA_AXIS2 0x002 79 #define MMS_FUNCDATA_AXIS3 0x004 80 #define MMS_FUNCDATA_AXIS4 0x008 81 #define MMS_FUNCDATA_AXIS5 0x010 82 #define MMS_FUNCDATA_AXIS6 0x020 83 #define MMS_FUNCDATA_AXIS7 0x040 84 #define MMS_FUNCDATA_AXIS8 0x080 85 #define MMS_FUNCDATA_C 0x100 86 #define MMS_FUNCDATA_B 0x200 87 #define MMS_FUNCDATA_A 0x400 88 #define MMS_FUNCDATA_START 0x800 89 90 struct mms_softc { 91 struct device sc_dev; 92 93 struct device *sc_parent; 94 struct maple_unit *sc_unit; 95 96 uint32_t sc_oldbuttons; 97 98 struct device *sc_wsmousedev; 99 }; 100 101 int mms_match(struct device *, struct cfdata *, void *); 102 void mms_attach(struct device *, struct device *, void *); 103 int mms_detach(struct device *, int); 104 105 CFATTACH_DECL(mms, sizeof(struct mms_softc), 106 mms_match, mms_attach, mms_detach, NULL); 107 108 int mms_enable(void *); 109 int mms_ioctl(void *, u_long, void *, int, struct lwp *); 110 void mms_disable(void *); 111 112 const struct wsmouse_accessops mms_accessops = { 113 mms_enable, 114 mms_ioctl, 115 mms_disable, 116 }; 117 118 void mms_intr(void *, struct maple_response *, int, int); 119 120 int 121 mms_match(struct device *parent, struct cfdata *cf, void *aux) 122 { 123 struct maple_attach_args *ma = aux; 124 125 return ma->ma_function == MAPLE_FN_MOUSE ? MAPLE_MATCH_FUNC : 0; 126 } 127 128 void 129 mms_attach(struct device *parent, struct device *self, void *aux) 130 { 131 struct mms_softc *sc = (void *) self; 132 struct maple_attach_args *ma = aux; 133 struct wsmousedev_attach_args a; 134 uint32_t data; 135 136 printf(": SEGA Dreamcast Mouse\n"); 137 138 sc->sc_parent = parent; 139 sc->sc_unit = ma->ma_unit; 140 141 data = maple_get_function_data(ma->ma_devinfo, 142 MAPLE_FN_MOUSE); 143 144 printf("%s: buttons:", sc->sc_dev.dv_xname); 145 if (data & MMS_FUNCDATA_A) 146 printf(" left"); 147 if (data & MMS_FUNCDATA_C) 148 printf(" middle"); 149 if (data & MMS_FUNCDATA_B) 150 printf(" right"); 151 if (data & MMS_FUNCDATA_START) 152 printf(" thumb"); 153 printf("\n"); 154 155 sc->sc_oldbuttons = 0; 156 157 a.accessops = &mms_accessops; 158 a.accesscookie = sc; 159 160 /* 161 * Attach the mouse, saving a handle to it. 162 */ 163 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 164 if (sc->sc_wsmousedev == NULL) { 165 /* Nothing more to do here. */ 166 return; 167 } 168 169 maple_set_callback(parent, sc->sc_unit, MAPLE_FN_MOUSE, mms_intr, sc); 170 } 171 172 int 173 mms_detach(struct device *self, int flags) 174 { 175 struct mms_softc *sc = (void *) self; 176 int rv = 0; 177 178 if (sc->sc_wsmousedev != NULL) 179 rv = config_detach(sc->sc_wsmousedev, flags); 180 181 return rv; 182 } 183 184 int 185 mms_enable(void *v) 186 { 187 struct mms_softc *sc = v; 188 189 maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 1); 190 return 0; 191 } 192 193 void 194 mms_disable(void *v) 195 { 196 struct mms_softc *sc = v; 197 198 maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 0); 199 } 200 201 int 202 mms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 203 { 204 205 switch (cmd) { 206 case WSMOUSEIO_GTYPE: 207 *(u_int *) data = WSMOUSE_TYPE_MAPLE; 208 break; 209 210 case WSMOUSEIO_SRES: 211 /* XXX */ 212 return EOPNOTSUPP; 213 214 default: 215 return EPASSTHROUGH; 216 } 217 218 return 0; 219 } 220 221 void 222 mms_intr(void *arg, struct maple_response *response, int size, int flags) 223 { 224 struct mms_softc *sc = arg; 225 struct mms_condition *data = (void *) response->data; 226 int dx = 0, dy = 0, dz = 0, buttons = 0; 227 uint32_t buttonchg; 228 229 if ((flags & MAPLE_FLAG_PERIODIC) == 0 || 230 size < sizeof(*data)) 231 return; 232 233 data->buttons &= MMS_BUTTON_MASK; 234 buttonchg = sc->sc_oldbuttons ^ data->buttons; 235 sc->sc_oldbuttons = data->buttons; 236 237 dx = (data->axis1 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE; 238 dy = (data->axis2 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE; 239 dz = (data->axis3 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE; 240 241 if (dx || dy || dz || buttonchg) { 242 if ((data->buttons & MMS_BUTTON_A) == 0) 243 buttons |= 0x01; 244 if ((data->buttons & MMS_BUTTON_C) == 0) 245 buttons |= 0x02; 246 if ((data->buttons & MMS_BUTTON_B) == 0) 247 buttons |= 0x04; 248 if ((data->buttons & MMS_BUTTON_START) == 0) 249 buttons |= 0x08; 250 251 wsmouse_input(sc->sc_wsmousedev, 252 buttons, 253 dx, -dy, dz, 0, 254 WSMOUSE_INPUT_DELTA); 255 } 256 } 257